Skip to content

Fix ccache Principal encoding for non-ASCII names - #2377

Merged
anadrianmanrique merged 1 commit into
fortra:masterfrom
azoxlpf:fix/azox-ccache-utf8-principal
Nov 18, 2026
Merged

anadrianmanrique merged 1 commit into
fortra:masterfrom
azoxlpf:fix/azox-ccache-utf8-principal

Conversation

@azoxlpf

@azoxlpf azoxlpf commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

CCache.Principal.fromPrincipal() stored str parts as-is. Structure then packed them as latin-1, which raises UnicodeEncodeError for any character beyond U+00FF (e.g. a Cyrillic account name), and length counted characters instead of bytes, so even latin-1 names with multi-byte UTF-8 forms produced a wrong length prefix.

Parts are now encoded to UTF-8 up front, so the byte count is correct and non-ASCII principals serialize. bytes input is passed through unchanged, so existing callers are unaffected. All principals go through fromPrincipal (fromTGT, fromTGS, fromKirbi), so this single change covers them all.

Found while dumping TGTs from a Russian-locale Windows host with NetExec, where the built-in Администратор account could not be written to a ccache.

Before :

image
uv run -q nxc smb 192.168.56.115 -u 'Администратор' -p 'P@wned123!' --klist-dump                                                                                󰥔 15:44 
SMB         192.168.56.115  445    RUSSIAN          [*] Windows Server 2022 Build 20348 x64 (name:RUSSIAN) (domain:Russian.local) (signing:True) (SMBv1:False) (Null Auth:True)
SMB         192.168.56.115  445    RUSSIAN          [+] Russian.local\Администратор:P@wned123! (Pwn3d!)
[15:44:42] ERROR    Exception while calling proto_flow() on target 192.168.56.115: 'latin-1' codec can't encode characters in position 0-12: ordinal not in connection.py:161
                    range(256)                                                                                                                                               
                    ╭───────────────────────────────────────────────── Traceback (most recent call last) ─────────────────────────────────────────────────╮                  
                    │ /home/azox/Documents/github/NetExec/nxc/connection.py:149 in __init__                                                           │                  
                    │                                                                                                                                     │                  
                    │   146 │   │   self.logger.debug(f"Socket info: host={self.host}, hostname={self.hostname},                                          │                  
                    │       kerberos={self.kerberos}, ipv6={self.is_ipv6}, link-local                                                                     │                  
                    │       ipv6={self.is_link_local_ipv6}")                                                                                              │                  
                    │   147 │   │                                                                                                                         │                  
                    │   148 │   │   try:                                                                                                                  │                  
                    │ ❱ 149 │   │   │   self.proto_flow()                                                                                                 │                  
                    │   150 │   │   except FileNotFoundError as e:                                                                                        │                  
                    │   151 │   │   │   self.logger.error(f"File not found error on target {target}: {e}")                                                │                  
                    │   152 │   │   except ConnectionRefusedError as e:                                                                                   │                  
                    │                                                                                                                                     │                  
                    │ /home/azox/Documents/github/NetExec/nxc/connection.py:230 in proto_flow                                                         │                  
                    │                                                                                                                                     │                  
                    │   227 │   │   │   self.print_host_info()                                                                                            │                  
                    │   228 │   │   │   if self.login() or (self.username == "" and self.password == "" and                                               │                  
                    │       self.protocol != "mssql" and not self.certificate_auth):   # a failed certificate auth                                        │                  
                    │       is not a null session                                                                                                         │                  
                    │   229 │   │   │   │   self.logger.debug("Calling command arguments")                                                                │                  
                    │ ❱ 230 │   │   │   │   self.call_cmd_args()                                                                                          │                  
                    │   231 │   │   │   │   if self.args.module:                                                                                          │                  
                    │   232 │   │   │   │   │   self.load_modules()                                                                                       │                  
                    │   233 │   │   │   │   │   self.logger.debug("Calling modules")                                                                      │                  
                    │                                                                                                                                     │                  
                    │ /home/azox/Documents/github/NetExec/nxc/connection.py:256 in call_cmd_args                                                      │                  
                    │                                                                                                                                     │                  
                    │   253 │   │   for attr, value in vars(self.args).items():                                                                           │                  
                    │   254 │   │   │   if hasattr(self, attr) and callable(getattr(self, attr)) and value is not                                         │                  
                    │       False and value is not None:                                                                                                  │                  
                    │   255 │   │   │   │   self.logger.debug(f"Calling {attr}()")                                                                        │                  
                    │ ❱ 256 │   │   │   │   getattr(self, attr)()                                                                                         │                  
                    │   257 │                                                                                                                             │                  
                    │   258 │   def call_modules(self):                                                                                                   │                  
                    │   259 │   │   """Calls modules and performs various actions based on the module's attributes.                                       │                  
                    │                                                                                                                                     │                  
                    │ /home/azox/Documents/github/NetExec/nxc/connection.py:94 in _decorator                                                          │                  
                    │                                                                                                                                     │                  
                    │    91 │   │   │   if hasattr(self.args, "exec_method") and self.args.exec_method == "mmcexec":                                      │                  
                    │    92 │   │   │   │   return func(self, *args, **kwargs)                                                                            │                  
                    │    93 │   │   │   return None                                                                                                       │                  
                    │ ❱  94 │   │   return func(self, *args, **kwargs)                                                                                    │                  
                    │    95 │                                                                                                                             │                  
                    │    96 │   return wraps(func)(_decorator)                                                                                            │                  
                    │    97                                                                                                                               │                  
                    │                                                                                                                                     │                  
                    │ /home/azox/Documents/github/NetExec/nxc/protocols/smb.py:1380 in klist_dump                                                     │                  
                    │                                                                                                                                     │                  
                    │   1377 │                                                                                                                            │                  
                    │   1378 │   @requires_admin                                                                                                          │                  
                    │   1379 │   def klist_dump(self):                                                                                                    │                  
                    │ ❱ 1380 │   │   Klist(self).dump(self.args.klist_dump)                                                                               │                  
                    │   1381 │                                                                                                                            │                  
                    │   1382 │   @requires_admin                                                                                                          │                  
                    │   1383 │   def tasklist(self):                                                                                                      │                  
                    │                                                                                                                                     │                  
                    │ /home/azox/Documents/github/NetExec/nxc/helpers/klist.py:67 in dump                                                             │                  
                    │                                                                                                                                     │                  
                    │    64 │   │   os.makedirs(os.path.dirname(output_prefix), exist_ok=True)                                                            │                  
                    │    65 │   │                                                                                                                         │                  
                    │    66 │   │   for logon_hex, account, info in tgts:                                                                                 │                  
                    │ ❱  67 │   │   │   self.write_ccache(info, self.ccache_path(output_prefix, info, logon_hex))                                         │                  
                    │    68 │   │   │   self.logger.highlight(f"{account}  (expires                                                                       │                  
                    │       {self.fmt_time(info['end_time'])} UTC)")                                                                                      │                  
                    │    69 │   │   self.logger.success(f"Dumped {len(tgts)} TGT(s) to                                                                    │                  
                    │       {os.path.dirname(output_prefix)}")                                                                                            │                  
                    │    70                                                                                                                               │                  
                    │                                                                                                                                     │                  
                    │ /home/azox/Documents/github/NetExec/nxc/helpers/klist.py:263 in write_ccache                                                    │                  
                    │                                                                                                                                     │                  
                    │   260 │   │   credential.secondTicket["length"] = 0                                                                                 │                  
                    │   261 │   │                                                                                                                         │                  
                    │   262 │   │   ccache.credentials = [credential]                                                                                     │                  
                    │ ❱ 263 │   │   ccache.saveFile(path)                                                                                                 │                  
                    │   264 │                                                                                                                             │                  
                    │   265 │   def fmt_time(self, timestamp):                                                                                            │                  
                    │   266 │   │   if not timestamp:                                                                                                     │                  
                    │                                                                                                                                     │                  
                    │ /home/azox/Documents/github/NetExec/.venv/lib/python3.13/site-packages/impacket/krb5/ccache.py:631 in saveFile                  │                  
                    │                                                                                                                                     │                  
                    │   628 │                                                                                                                             │                  
                    │   629 │   def saveFile(self, fileName):                                                                                             │                  
                    │   630 │   │   f = open(fileName, 'wb+')                                                                                             │                  
                    │ ❱ 631 │   │   f.write(self.getData())                                                                                               │                  
                    │   632 │   │   f.close()                                                                                                             │                  
                    │   633 │                                                                                                                             │                  
                    │   634 │   @classmethod                                                                                                              │                  
                    │                                                                                                                                     │                  
                    │ /home/azox/Documents/github/NetExec/.venv/lib/python3.13/site-packages/impacket/krb5/ccache.py:411 in getData                   │                  
                    │                                                                                                                                     │                  
                    │   408 │   │   data = self.MiniHeader().getData()                                                                                    │                  
                    │   409 │   │   for header in self.headers:                                                                                           │                  
                    │   410 │   │   │   data += header.getData()                                                                                          │                  
                    │ ❱ 411 │   │   data += self.principal.getData()                                                                                      │                  
                    │   412 │   │   for credential in self.credentials:                                                                                   │                  
                    │   413 │   │   │   data += credential.getData()                                                                                      │                  
                    │   414 │   │   return data                                                                                                           │                  
                    │                                                                                                                                     │                  
                    │ /home/azox/Documents/github/NetExec/.venv/lib/python3.13/site-packages/impacket/krb5/ccache.py:152 in getData                   │                  
                    │                                                                                                                                     │                  
                    │   149 │   def getData(self):                                                                                                        │                  
                    │   150 │   │   data = self.header.getData() + self.realm.getData()                                                                   │                  
                    │   151 │   │   for component in self.components:                                                                                     │                  
                    │ ❱ 152 │   │   │   data += component.getData()                                                                                       │                  
                    │   153 │   │   return data                                                                                                           │                  
                    │   154 │                                                                                                                             │                  
                    │   155 │   def __str__(self):                                                                                                        │                  
                    │                                                                                                                                     │                  
                    │ /home/azox/Documents/github/NetExec/.venv/lib/python3.13/site-packages/impacket/structure.py:140 in getData                     │                  
                    │                                                                                                                                     │                  
                    │   137 │   │   data = bytes()                                                                                                        │                  
                    │   138 │   │   for field in self.commonHdr+self.structure:                                                                           │                  
                    │   139 │   │   │   try:                                                                                                              │                  
                    │ ❱ 140 │   │   │   │   data += self.packField(field[0], field[1])                                                                    │                  
                    │   141 │   │   │   except Exception as e:                                                                                            │                  
                    │   142 │   │   │   │   if field[0] in self.fields:                                                                                   │                  
                    │   143 │   │   │   │   │   e.args += ("When packing field '%s | %s | %r' in %s" % (field[0],                                         │                  
                    │       field[1], self[field[0]], self.__class__),)                                                                                   │                  
                    │                                                                                                                                     │                  
                    │ /home/azox/Documents/github/NetExec/.venv/lib/python3.13/site-packages/impacket/structure.py:125 in packField                   │                  
                    │                                                                                                                                     │                  
                    │   122 │   │   │   format = self.formatForField(fieldName)                                                                           │                  
                    │   123 │   │                                                                                                                         │                  
                    │   124 │   │   if fieldName in self.fields:                                                                                          │                  
                    │ ❱ 125 │   │   │   ans = self.pack(format, self.fields[fieldName], field = fieldName)                                                │                  
                    │   126 │   │   else:                                                                                                                 │                  
                    │   127 │   │   │   ans = self.pack(format, None, field = fieldName)                                                                  │                  
                    │   128                                                                                                                               │                  
                    │                                                                                                                                     │                  
                    │ /home/azox/Documents/github/NetExec/.venv/lib/python3.13/site-packages/impacket/structure.py:292 in pack                        │                  
                    │                                                                                                                                     │                  
                    │   289 │   │   │   elif isinstance(data, int):                                                                                       │                  
                    │   290 │   │   │   │   return bytes(data)                                                                                            │                  
                    │   291 │   │   │   elif isinstance(data, bytes) is not True:                                                                         │                  
                    │ ❱ 292 │   │   │   │   return bytes(self.b(data))                                                                                    │                  
                    │   293 │   │   │   else:                                                                                                             │                  
                    │   294 │   │   │   │   return data                                                                                                   │                  
                    │   295                                                                                                                               │                  
                    │                                                                                                                                     │                  
                    │ /home/azox/Documents/github/NetExec/.venv/lib/python3.13/site-packages/impacket/structure.py:98 in <lambda>                     │                  
                    │                                                                                                                                     │                  
                    │    95 │   │   self.fields    = {}                                                                                                   │                  
                    │    96 │   │   self.rawData   = data                                                                                                 │                  
                    │    97 │   │                                                                                                                         │                  
                    │ ❱  98 │   │   self.b = lambda x: six.ensure_binary(x, encoding=self.ENCODING)                                                       │                  
                    │    99 │   │                                                                                                                         │                  
                    │   100 │   │   if data is not None:                                                                                                  │                  
                    │   101 │   │   │   self.fromString(data)                                                                                             │                  
                    │                                                                                                                                     │                  
                    │ /home/azox/Documents/github/NetExec/.venv/lib/python3.13/site-packages/six.py:917 in ensure_binary                              │                  
                    │                                                                                                                                     │                  
                    │    914 │   if isinstance(s, binary_type):                                                                                           │                  
                    │    915 │   │   return s                                                                                                             │                  
                    │    916 │   if isinstance(s, text_type):                                                                                             │                  
                    │ ❱  917 │   │   return s.encode(encoding, errors)                                                                                    │                  
                    │    918 │   raise TypeError("not expecting type '%s'" % type(s))                                                                     │                  
                    │    919                                                                                                                              │                  
                    │    920                                                                                                                              │                  
                    ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯                  
                    UnicodeEncodeError: 'latin-1' codec can't encode characters in position 0-12: ordinal not in range(256)     

After :

image

@anadrianmanrique

Copy link
Copy Markdown
Collaborator

tested ok. will follow up the unicode chars in SPN case in a separate PR
thanks!

@anadrianmanrique
anadrianmanrique merged commit 691fc18 into fortra:master Nov 18, 2026
8 checks passed
@anadrianmanrique anadrianmanrique self-assigned this Nov 18, 2026
@anadrianmanrique anadrianmanrique added bug Unexpected problem or unintended behavior to_be_followed_up to be followed up labels Nov 18, 2026
@azoxlpf

azoxlpf commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

tested ok. will follow up the unicode chars in SPN case in a separate PR thanks!

Thanks for being so responsive!

@azoxlpf
azoxlpf deleted the fix/azox-ccache-utf8-principal branch November 18, 2026 22:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Unexpected problem or unintended behavior to_be_followed_up to be followed up

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants