Skip to content

Commit 917dcb5

Browse files
committed
Clean up after retrieving files and dynamically try 10s to get the output in case stuff takes time
1 parent bab59de commit 917dcb5

2 files changed

Lines changed: 22 additions & 13 deletions

File tree

nxc/protocols/wmi.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -435,14 +435,11 @@ def execute(self, command=None, get_output=False):
435435
output = exec_method.execute(command, get_output)
436436

437437
self.conn.disconnect()
438-
if output == "" and get_output:
439-
self.logger.fail("Execute command failed, probabaly got detection by AV.")
440-
return ""
441-
elif self.args.execute and get_output:
438+
if self.args.execute and get_output:
442439
self.logger.success(f'Executed command: "{command}" via {self.args.exec_method}')
443440
buf = StringIO(output).readlines()
444441
for line in buf:
445442
self.logger.highlight(line.strip())
446443
return output
447-
elif get_output:
444+
else:
448445
return output

nxc/protocols/wmi/wmiexec.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)