Skip to content

Commit b232520

Browse files
committed
Fix winrm output when executing powershell
1 parent 136b45c commit b232520

1 file changed

Lines changed: 29 additions & 5 deletions

File tree

nxc/protocols/winrm.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from datetime import datetime
99
from pypsrp.wsman import NAMESPACES
1010
from pypsrp.client import Client
11+
from pypsrp.powershell import PSDataStreams
1112
from termcolor import colored
1213

1314
from impacket.examples.secretsdump import LocalOperations, LSASecrets, SAMHashes
@@ -260,12 +261,35 @@ def execute(self, payload=None, get_output=False, shell_type="cmd"):
260261
return result[0]
261262
self.logger.success(f"Executed command (shell type: {shell_type})")
262263
if not self.args.no_output:
263-
if result[2] == 0:
264-
for line in result[0].replace("\r", "").splitlines():
265-
self.logger.highlight(line.strip())
264+
if shell_type == "powershell":
265+
result: tuple[str, PSDataStreams, bool]
266+
if result[2]:
267+
self.logger.fail("Error executing powershell command, non-zero return code")
268+
# Display all channels of the PSDataStreams
269+
for msg in result[1].debug:
270+
self.logger.debug(str(msg).rstrip())
271+
for msg in result[1].verbose:
272+
self.logger.display(str(msg).rstrip())
273+
for msg in result[1].information:
274+
self.logger.display(str(msg).rstrip())
275+
for msg in result[1].progress:
276+
self.logger.display(str(msg).rstrip())
277+
for msg in result[1].warning:
278+
self.logger.display(str(msg).rstrip())
279+
for msg in result[1].error:
280+
self.logger.fail(str(msg).rstrip())
281+
# Display stdout
282+
for line in result[0].splitlines():
283+
self.logger.highlight(line.rstrip())
266284
else:
267-
for line in result[1].replace("\r", "").splitlines():
268-
self.logger.fail(line.strip())
285+
# Tuple of (stdout, stderr, returncode)
286+
result: tuple[str, str, int]
287+
if result[2] == 0:
288+
for line in result[0].replace("\r", "").splitlines():
289+
self.logger.highlight(line.rstrip())
290+
else:
291+
for line in result[1].replace("\r", "").splitlines():
292+
self.logger.fail(line.rstrip())
269293

270294
def ps_execute(self, payload=None, get_output=False):
271295
command = payload if payload else self.args.ps_execute

0 commit comments

Comments
 (0)