@@ -95,12 +95,10 @@ def execute_WithOutput(self, command):
9595 f' reg add \\ "HKLM\\ { self .__registry_Path } \\ " /v $name /t REG_SZ /d $chunk /f }}; '
9696 f'reg add \\ "HKLM\\ { self .__registry_Path } \\ " /v \\ "{ keyName } \\ " /t REG_DWORD /d $count /f"'
9797 )
98- time .sleep (1 )
99-
100- # 4. Delete temporary files
101- self .execute_remote (f'{ self .__shell } del /q /f "{ result_output } " "{ result_output_b64 } "' )
98+ time .sleep (0.1 )
10299
103100 self .queryRegistry (keyName )
101+ self .clean_up (result_output , result_output_b64 )
104102
105103 def queryRegistry (self , keyName ):
106104 try :
@@ -110,7 +108,14 @@ def queryRegistry(self, keyName):
110108 descriptor = descriptor .SpawnInstance ()
111109
112110 # Get the number of chunks stored in the registry
113- num_chunks = descriptor .GetDWORDValue (0x80000002 , self .__registry_Path , keyName ).uValue
111+ num_chunks = None
112+ for _ in range (10 ):
113+ self .logger .debug (f"Retrieving number of chunks for key: { keyName } " )
114+ num_chunks = descriptor .GetDWORDValue (0x80000002 , self .__registry_Path , keyName ).uValue
115+ if num_chunks is not None :
116+ break
117+ time .sleep (1 )
118+
114119 self .logger .debug (f"Number of chunks: { num_chunks } " )
115120
116121 # Retrieve each chunk and decode the base64 content
@@ -120,11 +125,18 @@ def queryRegistry(self, keyName):
120125 self .logger .debug (f"Retrieving chunk: { chunk_name } " )
121126 outputBuffer_b64 += descriptor .GetStringValue (0x80000002 , self .__registry_Path , chunk_name ).sValue
122127 self .__outputBuffer = base64 .b64decode (outputBuffer_b64 ).decode (self .__codec , errors = "replace" ).rstrip ("\r \n " )
123- except Exception :
124- self .logger .fail ("WMIEXEC: Could not retrieve output file, it may have been detected by AV. Please try increasing the timeout with the '--exec-timeout' option. If it is still failing, try the 'smb' protocol or another exec method" )
128+ except Exception as e :
129+ print (e )
130+ self .logger .fail ("WMIEXEC: Could not retrieve output file! Either command timed out or AV killed the process. Please try increasing the timeout: '--exec-timeout 10'" )
131+
132+ def clean_up (self , result_output , result_output_b64 ):
133+ """Deletes the output file, the base64 output file, and the registry path where the base64 content was stored."""
134+ self .execute_remote (f'{ self .__shell } del /q /f "{ result_output } " "{ result_output_b64 } "' )
125135
126136 try :
127137 self .logger .debug (f"Removing temporary registry path: HKLM\\ { self .__registry_Path } " )
138+ descriptor , _ = self .__iWbemServices .GetObject ("StdRegProv" )
139+ descriptor = descriptor .SpawnInstance ()
128140 descriptor .DeleteKey (0x80000002 , self .__registry_Path )
129141 except Exception as e :
130- self .logger .debug (f"Target: { self .__target } removing temporary registry path error: { e !s} " )
142+ self .logger .fail (f"Target: { self .__target } removing temporary registry path error: { e !s} " )
0 commit comments