Skip to content

Commit 3d407b4

Browse files
authored
Merge pull request Pennyw0rth#961 from Pennyw0rth/neff-fix-winrm-ps-output
Fix winrm output for powershell
2 parents 136b45c + 0da48a3 commit 3d407b4

1 file changed

Lines changed: 24 additions & 5 deletions

File tree

nxc/protocols/winrm.py

Lines changed: 24 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,30 @@ 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+
for out_type in ["debug", "verbose", "information", "progress", "warning", "error"]:
269+
stream: list[str] = getattr(result[1], out_type)
270+
for msg in stream:
271+
if str(msg) != "None":
272+
if out_type == "error":
273+
self.logger.fail(str(msg).rstrip())
274+
else:
275+
self.logger.display(str(msg).rstrip())
276+
# Display stdout
277+
for line in result[0].splitlines():
278+
self.logger.highlight(line.rstrip())
266279
else:
267-
for line in result[1].replace("\r", "").splitlines():
268-
self.logger.fail(line.strip())
280+
# Tuple of (stdout, stderr, returncode)
281+
result: tuple[str, str, int]
282+
if result[2] == 0:
283+
for line in result[0].replace("\r", "").splitlines():
284+
self.logger.highlight(line.rstrip())
285+
else:
286+
for line in result[1].replace("\r", "").splitlines():
287+
self.logger.fail(line.rstrip())
269288

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

0 commit comments

Comments
 (0)