Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
Back to tool/Back to issues
#21919·metasploit-framework

NTLM relay: identity strings keep embedded null bytes when the client negotiates UNICODE

Author: PushpenderrathoreCreated Sep 19, 2026Updated Sep 19, 2026

Summary

Net::NTLM::Message::Type3#domain and #user return the raw bytes straight off the wire (confirmed in rubyntlm's security_buffer accessor, which is a plain self['name'].value, no decoding). When NTLMSSP_NEGOTIATE_UNICODE is negotiated, those bytes are UTF-16LE, i.e. every character is followed by a null byte.

Code that turns this into a display/identity string with .to_s.force_encoding('UTF-8') doesn't transcode anything, it just relabels the same bytes as UTF-8. The null bytes stay in the string.

ruby
domain = ntlm_message.domain.to_s.force_encoding('UTF-8')
user = ntlm_message.user.to_s.force_encoding('UTF-8')
identity = "#{domain}\\#{user}" unless user.empty?

Reproducible directly (using the raw bytes for the ASCII text "DC1$" encoded as UTF-16LE, i.e. each character followed by a null byte):

ruby
raw = "D".b + "\x00".b + "C".b + "\x00".b + "1".b + "\x00".b + "$".b + "\x00".b
raw.dup.force_encoding("UTF-8").end_with?("$")
# => false

Where this lives today

lib/msf/core/exploit/remote/http_server/relay/ntlm/server_client.rb:198-200 (currently on master, in handle_type3):

ruby
domain = parsed_type3.domain.to_s.force_encoding('UTF-8')
user = parsed_type3.user.to_s.force_encoding('UTF-8')
identity = "#{domain}\\#{user}" unless user.empty?

Concrete downstream impact

modules/auxiliary/server/relay/esc8.rb decides whether a relayed identity is a machine account with:

ruby
cert_template = relay_identity.end_with?('$') ? ['DomainController', 'Machine'] : ['User']

With this encoding bug, a genuine machine account like DOMAIN\DC1$ never satisfies end_with?('$'), because the actual last byte is a trailing null, not $. In MODE AUTO, a coerced machine-account relay gets silently misclassified as a regular user account and requests the wrong certificate template.

Confirmed live against a real lab DC (KERBEROS\DC1$, coerced via PetitPotam, relayed through smb_to_ldap.rb): the identity string logged with a literal null byte after every character.

Also affects an unmerged PR

PR #21908 adds the same pattern (.to_s.force_encoding('UTF-8')) to lib/msf/core/exploit/remote/smb/relay/ntlm/server_client.rb, by design, since it explicitly copies the HTTP relay's approach. That PR is still open, so it isn't broken on master yet, but whoever reviews/merges it should be aware this pattern needs the same fix, ideally before merge rather than as a followup.

Suggested fix

Decode based on the negotiated flag instead of relabeling:

ruby
def self.decode_ntlm_string(raw, unicode:)
  return '' if raw.nil?
  bytes = raw.to_s.dup
  if unicode
    bytes.force_encoding('UTF-16LE').encode('UTF-8')
  else
    bytes.force_encoding('Windows-1252').encode('UTF-8')
  end
rescue EncodingError
  raw.to_s.dup.force_encoding('UTF-8') # best-effort fallback, matches today's behavior
end

called as decode_ntlm_string(parsed_type3.domain, unicode: parsed_type3.has_flag?(:UNICODE)).

Windows-1252 for the non-UNICODE (OEM) branch is a practical default, not a protocol guarantee, the OEM codepage is negotiated out of band and isn't specified. It's correct for ASCII identities, which covers what's demonstrated here, but isn't universal for non-ASCII OEM strings.

Environment

  • Found while live-testing PR #21908's fix in a real AD CS lab (Windows Server 2022 DC, PetitPotam coercion, smb_to_ldap.rb relay).
  • The root issue is an identity-decoding/correctness bug. It can have security-sensitive downstream effects where consumers use the identity string for account-type detection or, as with esc8.rb, certificate-template selection.

Source: rapid7/metasploit-framework

View original on GitHubView discussion on GitHub