|
| 1 | +import contextlib |
1 | 2 | import os |
2 | 3 | from time import sleep |
3 | 4 | from datetime import datetime, timedelta |
@@ -91,7 +92,8 @@ def on_admin_login(self, context, connection): |
91 | 92 | except Exception as e: |
92 | 93 | if "SCHED_S_TASK_HAS_NOT_RUN" in str(e): |
93 | 94 | self.logger.fail("Task was not run, seems like the specified user has no active session on the target") |
94 | | - exec_method.deleteartifact() |
| 95 | + with contextlib.suppress(Exception): |
| 96 | + exec_method.deleteartifact() |
95 | 97 | else: |
96 | 98 | self.logger.fail(f"Failed to execute command: {e}") |
97 | 99 |
|
@@ -255,20 +257,25 @@ def execute_handler(self, command, fileless=False): |
255 | 257 | except Exception as e: |
256 | 258 | if "ERROR_NONE_MAPPED" in str(e): |
257 | 259 | self.logger.fail(f"User {self.user} is not connected on the target, cannot run the task") |
258 | | - tsch.hSchRpcDelete(dce, f"\\{self.task}") |
259 | | - if e.error_code and hex(e.error_code) == "0x80070005": |
260 | | - self.logger.fail("Schtask_as: Create schedule task got blocked.") |
261 | | - tsch.hSchRpcDelete(dce, f"\\{self.task}") |
262 | | - if "ERROR_TRUSTED_DOMAIN_FAILURE" in str(e): |
| 260 | + with contextlib.suppress(Exception): |
| 261 | + tsch.hSchRpcDelete(dce, f"\\{self.task}") |
| 262 | + elif e.error_code and hex(e.error_code) == "0x80070005": |
| 263 | + self.logger.fail("Create schedule task got blocked.") |
| 264 | + with contextlib.suppress(Exception): |
| 265 | + tsch.hSchRpcDelete(dce, f"\\{self.task}") |
| 266 | + elif "ERROR_TRUSTED_DOMAIN_FAILURE" in str(e): |
263 | 267 | self.logger.fail(f"User {self.user} does not exist in the domain.") |
264 | | - tsch.hSchRpcDelete(dce, f"\\{self.task}") |
265 | | - if "SCHED_S_TASK_HAS_NOT_RUN" in str(e): |
266 | | - tsch.hSchRpcDelete(dce, f"\\{self.task}") |
267 | | - if "ERROR_ALREADY_EXISTS" in str(e): |
268 | | - self.logger.fail(f"Schtask_as: Create schedule task failed: {e}") |
| 268 | + with contextlib.suppress(Exception): |
| 269 | + tsch.hSchRpcDelete(dce, f"\\{self.task}") |
| 270 | + elif "SCHED_S_TASK_HAS_NOT_RUN" in str(e): |
| 271 | + with contextlib.suppress(Exception): |
| 272 | + tsch.hSchRpcDelete(dce, f"\\{self.task}") |
| 273 | + elif "ERROR_ALREADY_EXISTS" in str(e): |
| 274 | + self.logger.fail(f"Create schedule task failed: {e}") |
269 | 275 | else: |
270 | | - self.logger.fail(f"Schtask_as: Create schedule task failed: {e}") |
271 | | - tsch.hSchRpcDelete(dce, f"\\{self.task}") |
| 276 | + self.logger.fail(f"Create schedule task failed: {e}") |
| 277 | + with contextlib.suppress(Exception): |
| 278 | + tsch.hSchRpcDelete(dce, f"\\{self.task}") |
272 | 279 | return |
273 | 280 |
|
274 | 281 | self.logger.info(f"Running task \\{self.task}") |
|
0 commit comments