[Demo] NTLM Authentication Scheme for HTTP

The content on this page is mostly recovered from https://www.innovation.ch/personal/ronald/ntlm.html or https://web.archive.org/web/20210126065105/https://www.innovation.ch/personal/ronald/ntlm.html. This is the best content that I found on the internet talking about NTLM. I have added a sequence diagram. All credit goes to the original author.

Some URLs are dead and kept as is. The content is largely not changed except for the diagram. The original HTML files is kept below.

TL;DR

  1. NTLM is a connection authentication schema, so once the connection is built, you don’t need to pass any authentication information within the request. Remember, request is HTTP. NTLM is to build a safe and trusted connection.

  2. Client does not talk to domain controller at all. The server communicates with the domain controller. That happens when the server needs to obtain the NTLM has to validate the response from Client.


Introduction

This is an attempt at documenting the undocumented NTLM authentication scheme used by M$'s browsers, proxies, and servers (MSIE and IIS); this scheme is also sometimes referred to as the NT challenge/response (NTCR) scheme. Most of the info here is derived from three sources (see also the Resources section at the end of this document): Paul Ashton's work on the NTLM security holes, the encryption documentation from Samba, and network snooping. Since most of this info is reverse-engineered it is bound to contain errors; however, at least one client and one server have been implemented according to this data and work successfully in conjunction with M$'s browsers, proxies and servers.

Note that this scheme is not as secure as Digest and some other schemes; it is slightly better than the Basic authentication scheme, however.

Also note that this scheme is not an http authentication scheme - it's a connection authentication scheme which happens to (mis-)use http status codes and headers (and even those incorrectly).

NTLM Handshake

When a client needs to authenticate itself to a proxy or server using the NTLM scheme then the following 4-way handshake takes place (only parts of the request and status line and the relevant headers are shown here; "C" is the client, "S" the server):

1: C --> S GET ... 2: C <-- S 401 Unauthorized WWW-Authenticate: NTLM 3: C --> S GET ... Authorization: NTLM <base64-encoded type-1-message> 4: C <-- S 401 Unauthorized WWW-Authenticate: NTLM <base64-encoded type-2-message> 5: C --> S GET ... Authorization: NTLM <base64-encoded type-3-message> 6: C <-- S 200 Ok

 

Messages

The three messages sent in the handshake are binary structures. Each one is described below as a pseudo-C struct and in a memory layout diagram. byte is an 8-bit field; short is a 16-bit field. All fields are unsigned. Numbers are stored in little-endian order. Struct fields named zero contain all zeroes. An array length of "*" indicates a variable length field. Hexadecimal numbers and quoted characters in the comments of the struct indicate fixed values for the given field.

The field flags is presumed to contain flags, but their significance is unknown; the values given are just those found in the packet traces.

Type-1 Message

This message contains the host name and the NT domain name of the client.

struct { byte protocol[8]; // 'N', 'T', 'L', 'M', 'S', 'S', 'P', '\0' byte type; // 0x01 byte zero[3]; short flags; // 0xb203 byte zero[2]; short dom_len; // domain string length short dom_len; // domain string length short dom_off; // domain string offset byte zero[2]; short host_len; // host string length short host_len; // host string length short host_off; // host string offset (always 0x20) byte zero[2]; byte host[*]; // host string (ASCII) byte dom[*]; // domain string (ASCII) } type-1-message 0 1 2 3 +-------+-------+-------+-------+ 0: | 'N' | 'T' | 'L' | 'M' | +-------+-------+-------+-------+ 4: | 'S' | 'S' | 'P' | 0 | +-------+-------+-------+-------+ 8: | 1 | 0 | 0 | 0 | +-------+-------+-------+-------+ 12: | 0x03 | 0xb2 | 0 | 0 | +-------+-------+-------+-------+ 16: | domain length | domain length | +-------+-------+-------+-------+ 20: | domain offset | 0 | 0 | +-------+-------+-------+-------+ 24: | host length | host length | +-------+-------+-------+-------+ 28: | host offset | 0 | 0 | +-------+-------+-------+-------+ 32: | host string | + + . . . . + +-----------------+ | | domain string | +-------------+ + . . . . +-------+-------+-------+-------+

The host and domain strings are ASCII (or possibly ISO-8859-1), are uppercased, and are not nul-terminated. The host name is only the host name, not the FQDN (e.g. just "GOOFY", not "GOOFY.DISNEY.COM"). The offsets refer to the offset of the specific field within the message, and the lengths are the length of specified field. For example, in the above message host_off = 32 and dom_off = host_off + host_len. Note that the lengths are included twice (for some unfathomable reason).

Type-2 Message

This message contains the server's NTLM challenge.

struct { byte protocol[8]; // 'N', 'T', 'L', 'M', 'S', 'S', 'P', '\0' byte type; // 0x02 byte zero[7]; short msg_len; // 0x28 byte zero[2]; short flags; // 0x8201 byte zero[2]; byte nonce[8]; // nonce byte zero[8]; } type-2-message 0 1 2 3 +-------+-------+-------+-------+ 0: | 'N' | 'T' | 'L' | 'M' | +-------+-------+-------+-------+ 4: | 'S' | 'S' | 'P' | 0 | +-------+-------+-------+-------+ 8: | 2 | 0 | 0 | 0 | +-------+-------+-------+-------+ 12: | 0 | 0 | 0 | 0 | +-------+-------+-------+-------+ 16: | message len | 0 | 0 | +-------+-------+-------+-------+ 20: | 0x01 | 0x82 | 0 | 0 | +-------+-------+-------+-------+ 24: | | + server nonce | 28: | | +-------+-------+-------+-------+ 32: | 0 | 0 | 0 | 0 | +-------+-------+-------+-------+ 36: | 0 | 0 | 0 | 0 | +-------+-------+-------+-------+

The nonce is used by the client to create the LanManager and NT responses (see Password Hashes). It is an array of 8 arbitrary bytes. The message length field contains the length of the complete message, which in this case is always 40.

Type-3 Message

This message contains the username, host name, NT domain name, and the two "responses".

The host, domain, and username strings are in Unicode (UTF-16, little-endian) and are not nul-terminated; the host and domain names are in upper case. The lengths of the response strings are 24.

Password Hashes

To calculate the two response strings two password hashes are used: the LanManager password hash and the NT password hash. These are described in detail at the beginning of the Samba ENCRYPTION.html document. However, a few things are not clear (such as what the magic constant for the LanManager hash is), so here is some almost-C code which calculates the two responses. Inputs are passw and nonce, the results are in lm_resp and nt_resp.

Helpers:

Keeping the connection alive

As mentioned above, this scheme authenticates connections, not requests. This manifests itself in that the network connection must be kept alive during the second part of the handshake, i.e. between the receiving of the type-2 message from the server (step 4) and the sending of the type-3 message (step 5). Each time the connection is closed this second part (steps 3 through 6) must be repeated over the new connection (i.e. it's not enough to just keep sending the last type-3 message). Also, once the connection is authenticated, the Authorization header need not be sent anymore while the connection stays open, no matter what resource is accessed.

For implementations wishing to work with M$'s software this means that they must make sure they use either HTTP/1.0 keep-alive's or HTTP/1.1 persistent connections, and that they must be prepared to do the second part of the handshake each time the connection was closed and is reopened. Server implementations must also make sure that HTTP/1.0 responses contain a Content-length header (as otherwise the connection must be closed after the response), and that HTTP/1.1 responses either contain a Content-length header or use the chunked transfer encoding.

Example

Here is an actual example of all the messages. Assume the host name is "LightCity", the NT domain name is "Ursa-Minor", the username is "Zaphod", the password is "Beeblebrox", and the server sends the nonce "SrvNonce". Then the handshake is:

and the unencoded messages are:

Type-1 Message:

Type-2 Message:

Type-3 Message:

For reference, the intermediate hashed passwords are:

lm_hpw (LanManager hashed password):91 90 16 f6 4e c7 b0 0b a2 35 02 8c a5 0c 7a 03 00 00 00 00 00nt_hpw (NT hashed password):8c 1b 59 e3 2e 66 6d ad f1 75 74 5f ad 62 c1 33 00 00 00 00 00

Resources

Acknowledgments

Special thanks to the following people who helped with the collection and debugging of the above information: