NTLM Step by Step
NTLM is the authentication protocol Windows falls back to whenever Kerberos can't be used: when a server is addressed by IP instead of name, when a machine isn't in the domain, when the client can't reach a DC, or when an application simply asks for it. In most domains it still carries a large share of all authentications. It is also the protocol behind three of the most common attack techniques in Windows networks: pass-the-hash, offline guessing of captured responses and relay. All three follow directly from how the protocol computes its answer, so this lesson computes it, in Python, with lab values.
The three messages
NTLM is a challenge-response protocol embedded in another protocol (SMB, HTTP, LDAP, RPC):
- NEGOTIATE (client → server): flags, "I can do NTLM".
- CHALLENGE (server → client): an 8-byte random server challenge, plus target information (domain and server names, a timestamp) as AV pairs.
- AUTHENTICATE (client → server): user name, domain, and the NTLMv2 response computed from the challenge and the user's NT hash.
A domain member can't check the response itself, because it doesn't know the user's hash. It forwards the challenge and the response to a DC over Netlogon (pass-through authentication); the DC recomputes and answers yes or no, and returns the session key. On the DC this appears as event 4776 ("The computer attempted to validate the credentials for an account"); on the server as 4624 with Authentication Package: NTLM.
The computation
For NTLMv2 (MS-NLMP), with NT the NT hash from LSASS and Stored Credentials:
NTOWFv2 = HMAC-MD5(NT, UTF16LE(UPPER(user) + domain))blob = 01 01 00 00 | 00 00 00 00 | timestamp (8) | client challenge (8) | 00 00 00 00 | AV pairs | 00 00 00 00NTProofStr = HMAC-MD5(NTOWFv2, server challenge + blob); the response isNTProofStr + blobSessionBaseKey = HMAC-MD5(NTOWFv2, NTProofStr), from which signing and sealing keys are derived
The password appears only in the first line, through the hash. The program runs one exchange with fixed challenges (real implementations use 8 random bytes from a secure generator each time) and lets the "DC" verify it.
import hmac, hashlib, struct
from Crypto.Hash import MD4
def nt_hash(pw): return MD4.new(pw.encode("utf-16-le")).digest()
def hm(key, data): return hmac.new(key, data, hashlib.md5).digest()
def ntowf_v2(nt, user, dom): return hm(nt, (user.upper() + dom).encode("utf-16-le"))
def av_pairs(target): # MsvAvNbDomainName (2) + MsvAvNbComputerName (1) + MsvAvEOL (0)
out = b""
for av_id, val in [(2, "CORP"), (1, target)]:
v = val.encode("utf-16-le"); out += struct.pack("<HH", av_id, len(v)) + v
return out + b"\x00\x00\x00\x00"
def respond(nt, user, dom, server_chal, client_chal, timestamp, target):
blob = b"\x01\x01" + b"\x00" * 6 + struct.pack("<Q", timestamp) + client_chal + b"\x00" * 4 + av_pairs(target) + b"\x00" * 4
key = ntowf_v2(nt, user, dom)
proof = hm(key, server_chal + blob)
return proof + blob, hm(key, proof) # response, session base key
def dc_verify(nt_of, user, dom, server_chal, response):
proof, blob = response[:16], response[16:]
key = ntowf_v2(nt_of[user], user, dom)
ok = hmac.compare_digest(hm(key, server_chal + blob), proof)
return ok, (hm(key, proof) if ok else None)
directory = {"alice": nt_hash("Winter2026!")} # what the DC knows: hashes, not passwords
server_chal, client_chal = bytes.fromhex("0123456789abcdef"), bytes.fromhex("aaaaaaaaaaaaaaaa")
ts = 133_800_000_000_000_000 # FILETIME, fixed for the example
resp, client_key = respond(nt_hash("Winter2026!"), "alice", "CORP", server_chal, client_chal, ts, "FS01")
print("NTProofStr ", resp[:16].hex())
print("response length ", len(resp), "bytes")
ok, dc_key = dc_verify(directory, "alice", "CORP", server_chal, resp)
print("DC verifies ", ok, "| same session key on both sides:", dc_key == client_key)
# pass-the-hash: the hash alone produces a valid response; the password is never needed
resp2, _ = respond(directory["alice"], "alice", "CORP", server_chal, client_chal, ts, "FS01")
print("hash-only response accepted:", dc_verify(directory, "alice", "CORP", server_chal, resp2)[0])
print("wrong password accepted: ", dc_verify(directory, "alice", "CORP", server_chal,
respond(nt_hash("Winter2025!"), "alice", "CORP", server_chal, client_chal, ts, "FS01")[0])[0])NTProofStr 16d8c578178dfc6b7e16cf4aabbe720e
response length 76 bytes
DC verifies True | same session key on both sides: True
hash-only response accepted: True
wrong password accepted: False
Three properties of NTLM are visible here, and each is the root of an attack class:
- The NT hash is the key (
hash-only response accepted: True). Whoever steals it can authenticate as the user: pass-the-hash. - The response is a keyed hash of public values (the challenges, the blob). An observer who captures a response can test password guesses offline by recomputing it. NTLMv2 makes that slow only to the extent that the password is long and random.
- Nothing in the response says which server the client meant to reach in a way the server must check. The target name sits in the blob, but an ordinary SMB or HTTP server doesn't compare it against itself unless extra protections are on. So a response produced for one server's challenge is valid at any server that issued that challenge. That is relay.
Relay in one picture
The relay only forwards bytes. It never learns Alice's hash or the session key (the key is derived from NTOWFv2, which needs the hash). That last fact is the defence: if the target requires signing, every message after authentication must carry a MAC made with the session key, and the relay can't produce one. The full relay scenario, coercion techniques and the protections per protocol are in NTLM Relay, Coercion and Signing.
How NTLM use is detected
- 4776 on DCs (NTLM credential validation) with the workstation name and the result code (
0xC000006Awrong password,0xC0000064unknown user); 4624 withAuthentication Package: NTLMandPackage Name (NTLM only): NTLM V2orNTLM V1on servers. AnyNTLM V1orLMis a finding. - Pass-the-hash pattern: a 4624 logon type 3 with NTLM for an account that normally uses Kerberos, from a workstation the account doesn't use; on the source machine, logon type 9 (
NewCredentials) with logon processseclogo. - NTLM auditing ("Network security: Restrict NTLM: Audit …" policies) writes the operational NTLM log (8001–8004), which tells you which clients and servers still use NTLM, the inventory you need before restricting it.
- A 4624 whose
Workstation Namedoesn't match the source IP's DNS name is a classic relay signal.
How it is hardened
LmCompatibilityLevel = 5everywhere (NTLMv2 only); no LM hashes stored.- SMB signing required, LDAP signing and channel binding on DCs, Extended Protection for Authentication (EPA) on HTTP services such as AD CS web enrollment and Exchange.
- Reduce NTLM: use names (so Kerberos works), audit, then restrict NTLM per server and per account (Removing NTLM); members of Protected Users can't use NTLM at all. Microsoft deprecated all versions of NTLM in June 2024 and removed NTLMv1 starting with Windows 11 24H2 and Windows Server 2025; plan for NTLM's removal rather than wait for it.
- Long random passwords for every account whose responses might be captured, which makes offline guessing impractical.
Pitfalls
- "We use Kerberos." Check 4776 and the NTLM log; nearly every domain has more NTLM than its admins think.
- Accessing servers by IP address in scripts and shortcuts forces NTLM, because there is no SPN for an IP by default.
- Signing "enabled" vs "required". Enabled means "if the other side asks"; only required blocks relay.
- Assuming the challenge is secret. It isn't, and it doesn't need to be: security depends on the hash being secret, and on binding the session to the right server.
Practice
1. In the program, change the server challenge passed to dc_verify but keep the response. What happens and why?
Verification fails: the NTProofStr covers the server challenge, so a response is valid only for the challenge it was computed for. That's why a relay must pass the target's challenge to the victim.
2. Which line of the computation would you change to make a stolen NT hash useless, and why can't Windows do that?
The first, deriving the key from the password with a salted, slow function. NTLM's protocol definition fixes the key as the NT hash; changing it would break every NTLM client and server. That is why the answer is to replace NTLM with Kerberos, not to fix it.
3. A 4624 on FS01: account alice, logon type 3, package NTLM, workstation name WS01, source address 10.10.0.66. WS01 is 10.10.0.21. What do you suspect?
Relay: the authentication was produced by WS01 (the name inside the NTLM message) but arrived from another host. Check what 10.10.0.66 is, whether FS01 requires SMB signing, and what alice's session did.
Test yourself
What key does NTLMv2 use to compute the response?
- The cleartext password
- A key derived from the NT hash, user name and domain (NTOWFv2)
- The Kerberos TGT
- The server's certificate
Which event ID on a domain controller records NTLM credential validation?
- 4768
- 4769
- 4776
- 5136
Why can't a relay attacker continue a session when the target requires signing?
- Signing encrypts the challenge
- The signing key derives from the session key, which needs the user's NT hash that the relay never sees
- Signing blocks port 445
- The DC refuses relays
What makes pass-the-hash possible?
- Weak TLS
- NTLM uses the NT hash itself as the secret, so the hash is enough to authenticate
- Kerberos pre-authentication
- UAC