@@ -91,7 +91,7 @@ def on_admin_login(self, context, connection):
9191 except Exception as e :
9292 if "SCHED_S_TASK_HAS_NOT_RUN" in str (e ):
9393 self .logger .fail ("Task was not run, seems like the specified user has no active session on the target" )
94-
94+ exec_method . deleteartifact ()
9595
9696class TSCH_EXEC :
9797 def __init__ (self , target , share_name , username , password , domain , user , cmd , file , task , location , doKerberos = False , aesKey = None , remoteHost = None , kdcHost = None , hashes = None , logger = None , tries = None , share = None ):
@@ -143,6 +143,18 @@ def __init__(self, target, share_name, username, password, domain, user, cmd, fi
143143 )
144144 self .__rpctransport .set_kerberos (self .__doKerberos , self .__kdcHost )
145145
146+ def deleteartifact (self ):
147+ dce = self .__rpctransport .get_dce_rpc ()
148+ if self .__doKerberos :
149+ dce .set_auth_type (RPC_C_AUTHN_GSS_NEGOTIATE )
150+ dce .set_credentials (* self .__rpctransport .get_credentials ())
151+ dce .connect ()
152+ dce .set_auth_level (RPC_C_AUTHN_LEVEL_PKT_PRIVACY )
153+ dce .bind (tsch .MSRPC_UUID_TSCHS )
154+ self .logger .display (f"Deleting task \\ { tmpName } " )
155+ tsch .hSchRpcDelete (dce , f"\\ { tmpName } " )
156+ dce .disconnect ()
157+
146158 def execute (self , command , output = False ):
147159 self .__retOutput = output
148160 self .execute_handler (command )
@@ -223,7 +235,9 @@ def gen_xml(self, command, fileless=False):
223235 return xml
224236
225237 def execute_handler (self , command , fileless = False ):
238+ global tmpName
226239 dce = self .__rpctransport .get_dce_rpc ()
240+
227241 if self .__doKerberos :
228242 dce .set_auth_type (RPC_C_AUTHN_GSS_NEGOTIATE )
229243
@@ -243,19 +257,23 @@ def execute_handler(self, command, fileless=False):
243257 except Exception as e :
244258 if "ERROR_NONE_MAPPED" in str (e ):
245259 self .logger .fail (f"User { self .user } is not connected on the target, cannot run the task" )
260+ tsch .hSchRpcDelete (dce , f"\\ { tmpName } " )
246261 if e .error_code and hex (e .error_code ) == "0x80070005" :
247262 self .logger .fail ("Schtask_as: Create schedule task got blocked." )
263+ tsch .hSchRpcDelete (dce , f"\\ { tmpName } " )
248264 if "ERROR_TRUSTED_DOMAIN_FAILURE" in str (e ):
249265 self .logger .fail (f"User { self .user } does not exist in the domain." )
266+ tsch .hSchRpcDelete (dce , f"\\ { tmpName } " )
267+ if "SCHED_S_TASK_HAS_NOT_RUN" in str (e ):
268+ tsch .hSchRpcDelete (dce , f"\\ { tmpName } " )
250269 else :
251270 self .logger .fail (f"Schtask_as: Create schedule task failed: { e } " )
271+ tsch .hSchRpcDelete (dce , f"\\ { tmpName } " )
252272 return
253273 else :
254- taskCreated = True
255-
274+ taskCreated = True
256275 self .logger .info (f"Running task \\ { tmpName } " )
257- tsch .hSchRpcRun (dce , f"\\ { tmpName } " )
258-
276+ tsch .hSchRpcRun (dce , f"\\ { tmpName } " )
259277 done = False
260278 while not done :
261279 self .logger .debug (f"Calling SchRpcGetLastRunInfo for \\ { tmpName } " )
0 commit comments