Skip to content

Commit b717f30

Browse files
committed
Bug fixes for when the command already starts with 'powershell ...'
1 parent af822e5 commit b717f30

2 files changed

Lines changed: 6 additions & 4 deletions

File tree

nxc/protocols/wmi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ def execute_psh(self, command=None, get_output=False):
458458
self.logger.success(f'Executed PowerShell command: "{command}" via {self.args.exec_method}')
459459
buf = StringIO(output).readlines()
460460
for line in buf:
461-
if line.strip().rstrip("\ufeff"):
461+
if line.strip():
462462
self.logger.highlight(line.strip())
463463
return output
464464
else:

nxc/protocols/wmi/wmiexec.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,12 @@ def execute_WithOutput_psh(self, command):
122122
result_output = f"C:\\windows\\temp\\{uuid.uuid4()!s}.txt"
123123
result_output_b64 = f"C:\\windows\\temp\\{uuid.uuid4()!s}.txt"
124124
keyName = str(uuid.uuid4())
125-
self.__registry_Path = f"Software\\Classes\\test_nxc_{gen_random_string(6)}"
125+
self.__registry_Path = f"Software\\Classes\\{gen_random_string(6)}"
126126

127127
# 1. Run the command and write output to file
128-
self.execute_remote(f'powershell -Command {command} 1> "{result_output}" 2>&1')
128+
if not command.lower().startswith("powershell"):
129+
command = f"powershell -Command {command}"
130+
self.execute_remote(f'{command} > "{result_output}" 2>&1')
129131
self.logger.info(f"Waiting {self.__exec_timeout}s for command to complete.")
130132
time.sleep(self.__exec_timeout)
131133

@@ -173,7 +175,7 @@ def queryRegistry_psh(self, keyName):
173175
chunk_name = f"{keyName}_chunk_{i}"
174176
self.logger.debug(f"Retrieving chunk: {chunk_name}")
175177
outputBuffer_b64 += descriptor.GetStringValue(0x80000002, self.__registry_Path, chunk_name).sValue
176-
self.__outputBuffer = base64.b64decode(outputBuffer_b64).decode("utf-16le", errors="replace").rstrip("\r\n")
178+
self.__outputBuffer = base64.b64decode(outputBuffer_b64).decode("utf-16le", errors="replace").rstrip("\r\n").lstrip("\ufeff") # Remove BOM if present
177179
except Exception:
178180
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'")
179181

0 commit comments

Comments
 (0)