Skip to content

Fix klist parsing on non-English targets - #1527

Open
azoxlpf wants to merge 1 commit into
mainfrom
fix/azox-klist-non-english-locale
Open

azoxlpf wants to merge 1 commit into
mainfrom
fix/azox-klist-non-english-locale

Conversation

@azoxlpf

@azoxlpf azoxlpf commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator

Description

--klist and --klist-dump produced garbage on non-English Windows targets: mojibake account names, expires N/A, and ccache files with an empty principal (_@_0x3e7.ccache).

Russian DC, main :

SMB 192.168.56.115 445 RUSSIAN RUSSIAN0\Çñ¼¿¡¿ßΓαáΓ«α (expires N/A UTC)

klist writes its output in the target's OEM codepage (cp866 Russian, cp932 Japanese, cp936 Chinese, cp850 French), but nxc decoded it with the protocol's default --codec (utf-8 for smb, cp437 for winrm). Any non-ASCII byte either broke the decode (falling back to cp437 mojibake) or, under cp437, turned locale-specific separators into non-whitespace characters, French klist puts a NBSP before every , so the label-based regexes (ClientName\s*:, EndTime\s*:…) stopped matching entirely.

Changes:

  • nxc/helpers/klist.py: all klist commands now run under chcp 65001 and are decoded as UTF-8, independently of --codec. The target always emits UTF-8, nxc always reads UTF-8. Once properly decoded, NBSP and other locale separators are plain Unicode whitespace and match \s as-is, no per-language handling needed.

  • nxc/protocols/smb.py, nxc/protocols/winrm.py: execute() gains an optional codec=None parameter (backward compatible, defaults to self.args.codec).

This PR requires fortra/impacket#2377 to be merged first. It fixes CCache.Principal.fromPrincipal(), which packed str parts as latin-1 and counted characters instead of bytes, so a non-ASCII principal could not be written to a ccache at all.

AI Assistance

Claude Opus 5.5 helped me locate the bug, tracing the mojibake byte by byte to a codepage mismatch (cp866 output decoded as cp437), then spotting the French NBSP before : in the raw --debug output as the cause of the regression. The fix was written and every change manually tested against real targets in a lab.

Type of change

Insert an "x" inside the brackets for relevant items

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Deprecation of feature or functionality
  • This change requires a documentation update
  • This requires a third party update (such as Impacket, Dploot, lsassy, etc)
  • This PR was created with the assistance of AI (list what type of assistance, tool(s)/model(s) in the description)

Setup guide for the review

Please provide guidance on what setup is needed to test the introduced changes, such as the target(s) you tested against or software versions.

In particular:

  • Bug Fix: Please provide a short description on how to trigger the bug, to make the bug reproducable for the reviewer.
  • Added Feature/Enhancement: Please specify what setup is needed in order to test the changes, such as:
    • Is additional software needed?
    • GPO changes required?
    • Specific registry settings that need to be changed?

Screenshots (if appropriate):

Russian Before :

image

CHINA Before :

image

Japanese Before :

image

Russian After :

image

China After :

image

Japanese After :

image

Checklist:

Insert an "x" inside the brackets for completed and relevant items (do not delete options)

  • I have ran Ruff against my changes (poetry: poetry run ruff check ., use --fix to automatically fix what it can)
  • I have added or updated the tests/e2e_commands.txt file if necessary (new modules or features are required to be added to the e2e tests)
  • If reliant on changes of third party dependencies, such as Impacket, dploot, lsassy, etc, I have linked the relevant PRs in those projects
  • I have linked relevant sources that describes the added technique (blog posts, documentation, etc)
  • I have performed a self-review of my own code (not an AI review)
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation (PR here: https://github.com/Pennyw0rth/NetExec-Wiki)

@azoxlpf azoxlpf added the bug-fix This Pull Request fixes a bug label Nov 13, 2026
Comment thread nxc/protocols/winrm.py Dismissed
@NeffIsBack

Copy link
Copy Markdown
Member

Nice :) Gonna turn it into a draft until the impacket PR is merged

@NeffIsBack
NeffIsBack marked this pull request as draft November 13, 2026 23:04
@azoxlpf
azoxlpf marked this pull request as ready for review November 18, 2026 20:00

@mpgn mpgn left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you bump impacket in uv.lock and poetry.lock to include fortra/impacket#2377 (691fc183)? Heads-up: it also pulls fortra/impacket#2339, so remove-mic.py needs a rewrite (TypeError on version=).

Comment thread nxc/helpers/klist.py
Comment on lines +35 to +36
# 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")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks --klist/--klist-dump with --exec-method atexec and smbexec, which work on main: atexec puts the command unescaped in the task XML, so the & makes it invalid, and smbexec echoes the command into a batch file, so the > and & split the echo.

$ nxc smb 10.4.10.22 -u Administrator -p NgtI75cKV+Pu -d north.sevenkingdoms.local --klist --exec-method atexec
SMB                      10.4.10.22      445    CASTELBLACK      [-] TSCH SessionError: code: 0x8004131a - SCHED_E_MALFORMEDXML - The task XML is malformed.
SMB                      10.4.10.22      445    CASTELBLACK      [-] Failed to enumerate Kerberos sessions
$ nxc smb 10.4.10.22 -u Administrator -p NgtI75cKV+Pu -d north.sevenkingdoms.local --klist --exec-method smbexec
SMB                      10.4.10.22      445    CASTELBLACK      [-] SMBEXEC: Could not retrieve output file, it may have been detected by AV. Please increase the number of tries with the option '--get-output-tries'. If it is still failing, try the 'wmi' protocol or another exec method
SMB                      10.4.10.22      445    CASTELBLACK      [-] Failed to enumerate Kerberos sessions

On main both list [+] Found 4 TGT(s). Quoting the command fixes smbexec:

Suggested change
# 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")
# klist prints in the target's OEM codepage: switch it to UTF-8 (quoted so smbexec's echo keeps & and >)
return self.connection.execute(f'cmd /c "chcp 65001 > nul & {command}"', True, codec="utf-8")

and atexec needs the XML escaping that impacket's atexec.py already does. This also fixes -x '(whoami & hostname)' on atexec, which fails on main with the same SCHED_E_MALFORMEDXML:

--- a/nxc/protocols/smb/atexec.py
+++ b/nxc/protocols/smb/atexec.py
@@ -5,2 +5,3 @@
 from time import sleep
+from xml.sax.saxutils import escape
@@ -92 +93 @@
-        full_command = f"{random.choice(random_cmd_arg)} {command}"
+        full_command = f"{random.choice(random_cmd_arg)} {escape(command)}"

With both, atexec, smbexec, wmiexec, mmcexec and winrm list the same 4 TGTs as main, and the code page is still switched on each (-x 'cmd /c "chcp 65001 > nul & chcp"' gives Active code page: 65001):

$ nxc smb 10.4.10.22 -u Administrator -p NgtI75cKV+Pu -d north.sevenkingdoms.local --klist --exec-method atexec
SMB                      10.4.10.22      445    CASTELBLACK      [+] Found 4 TGT(s)
SMB                      10.4.10.22      445    CASTELBLACK        [1] NORTH\sql_svc       0x17f09  (expires 2026-11-19 06:00:49 UTC)
SMB                      10.4.10.22      445    CASTELBLACK        [2] NORTH\CASTELBLACK$  0x3e4  (expires 2026-11-19 04:37:42 UTC)
SMB                      10.4.10.22      445    CASTELBLACK        [3] NORTH\robb.stark    0x6a769  (expires 2026-11-19 00:05:03 UTC)
SMB                      10.4.10.22      445    CASTELBLACK        [4] NORTH\CASTELBLACK$  0x3e7  (expires 2026-11-18 22:47:09 UTC)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug-fix This Pull Request fixes a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants