Repository navigation
Conversation
|
Nice :) Gonna turn it into a draft until the impacket PR is merged |
mpgn
left a comment
There was a problem hiding this comment.
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=).
| # 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") |
There was a problem hiding this comment.
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:
| # 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)
Description
--klistand--klist-dumpproduced 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)klistwrites 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-8for smb,cp437for 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 65001and 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\sas-is, no per-language handling needed.nxc/protocols/smb.py, nxc/protocols/winrm.py:
execute()gains an optionalcodec=Noneparameter (backward compatible, defaults toself.args.codec).This PR requires fortra/impacket#2377 to be merged first. It fixes
CCache.Principal.fromPrincipal(), which packedstrparts 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--debugoutput 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
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:
Screenshots (if appropriate):
Russian Before :
CHINA Before :
Japanese Before :
Russian After :
China After :
Japanese After :
Checklist:
Insert an "x" inside the brackets for completed and relevant items (do not delete options)
poetry run ruff check ., use--fixto automatically fix what it can)tests/e2e_commands.txtfile if necessary (new modules or features are required to be added to the e2e tests)