From faf25e237cd95888fcd30a3db8fbdc7c56e965fe Mon Sep 17 00:00:00 2001 From: azoxlpf <213314124+azoxlpf@users.noreply.github.com> Date: Fri, 13 Nov 2026 15:54:54 +0100 Subject: [PATCH] Fix klist parsing on non-English targets --- nxc/helpers/klist.py | 10 +++++++--- nxc/protocols/smb.py | 5 +++-- nxc/protocols/winrm.py | 4 ++-- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/nxc/helpers/klist.py b/nxc/helpers/klist.py index 75ea8439d7..1473196682 100644 --- a/nxc/helpers/klist.py +++ b/nxc/helpers/klist.py @@ -31,6 +31,10 @@ def __init__(self, connection): self.logger = connection.logger self.bias_minutes = 0 + def run(self, command): + # klist prints non-ASCII names, and a NBSP before ':' on French Windows, in the target's OEM codepage; force UTF-8 on both ends so parsing is locale-independent + return self.connection.execute(f"chcp 65001 > nul & {command}", True, codec="utf-8") + def display(self): tgts = self.load_tgts() if tgts is None: @@ -66,7 +70,7 @@ def dump(self, selection): def load_tgts(self): """Returns None when klist itself could not run, a (possibly empty) list otherwise.""" - output = self.connection.execute("klist sessions", True) + output = self.run("klist sessions") if not output: self.logger.fail("Failed to enumerate Kerberos sessions") return None @@ -109,7 +113,7 @@ def target_utc_bias(self): klist prints local wallclock with no zone, so without this the ccache times and the expiry check are off by the target's UTC offset whenever it differs from ours. """ - output = self.connection.execute(r'reg query "HKLM\SYSTEM\CurrentControlSet\Control\TimeZoneInformation" /v ActiveTimeBias', True) + output = self.run(r'reg query "HKLM\SYSTEM\CurrentControlSet\Control\TimeZoneInformation" /v ActiveTimeBias') match = re.search(r"ActiveTimeBias\s+REG_DWORD\s+0x([0-9a-fA-F]+)", output) if output else None if not match: self.logger.debug("Could not read ActiveTimeBias, assuming target is in UTC") @@ -123,7 +127,7 @@ def collect_tgts(self, sessions): now = datetime.now(UTC).timestamp() tgts = [] for logon_hex, account in sessions: - tgt_text = self.connection.execute(f"klist tgt -li {logon_hex}", True) + tgt_text = self.run(f"klist tgt -li {logon_hex}") if not tgt_text: self.logger.debug(f"{account} ({logon_hex}): no output from klist tgt") continue diff --git a/nxc/protocols/smb.py b/nxc/protocols/smb.py index 6e5f70dece..a925ea3f3a 100755 --- a/nxc/protocols/smb.py +++ b/nxc/protocols/smb.py @@ -1014,7 +1014,7 @@ def trigger_winreg(self): return False @requires_admin - def execute(self, payload=None, get_output=False, methods=None) -> str: + def execute(self, payload=None, get_output=False, methods=None, codec=None) -> str: """ Executes a command on the target host using CMD.exe and the specified method(s). @@ -1023,6 +1023,7 @@ def execute(self, payload=None, get_output=False, methods=None) -> str: payload (str): The command to execute get_output (bool): Whether to get the output of the command (can be useful for AV evasion) methods (list): The method(s) to use for command execution + codec (str): Decode the output with this codec instead of the --codec default Returns: ------- @@ -1130,7 +1131,7 @@ def execute(self, payload=None, get_output=False, methods=None) -> str: output = exec_method.execute(payload, get_output) try: if not isinstance(output, str): - output = output.decode(self.args.codec) + output = output.decode(codec or self.args.codec) except UnicodeDecodeError: self.logger.debug("Decoding error detected, consider running chcp.com at the target, map the result with https://docs.python.org/3/library/codecs.html#standard-encodings") output = output.decode("cp437") diff --git a/nxc/protocols/winrm.py b/nxc/protocols/winrm.py index 8482042acf..07cc66393d 100644 --- a/nxc/protocols/winrm.py +++ b/nxc/protocols/winrm.py @@ -253,12 +253,12 @@ def hash_login(self, domain, username, ntlm_hash): self.logger.fail(f"{self.domain}\\{self.username}:{process_secret(self.nthash)} {e!s}") return False - def execute(self, payload=None, get_output=False, shell_type="cmd"): + def execute(self, payload=None, get_output=False, shell_type="cmd", codec=None): if not payload: payload = self.args.execute try: - result = self.conn.execute_cmd(payload, encoding=self.args.codec) if shell_type == "cmd" else self.conn.execute_ps(payload) + result = self.conn.execute_cmd(payload, encoding=codec or self.args.codec) if shell_type == "cmd" else self.conn.execute_ps(payload) except Exception as e: # Reference: https://github.com/diyan/pywinrm/issues/275 if hasattr(e, "code") and e.code == 5: