2 * Beware the LM hash is easy to crack (google for l0phtCrack)
3 * and though NTLM is more secure it is still breakable.
4 * Ntlmv2 is better and seen as good enough by the windows community.
5 * For real security use kerberos.
18 #define DEF_AUTH "ntlmv2"
21 MACkeylen = 40, /* MAC key len */
22 MAClen = 8, /* signature length */
23 MACoff = 14, /* sign. offset from start of SMB (not netbios) pkt */
24 Bliplen = 8, /* size of LMv2 client nonce */
28 dmp(char *s, int seq, void *buf, int n)
33 print("%s %3d ", s, seq);
35 for(i = 0; i < 16 && n > 0; i++, n--)
36 print("%02x ", *p++ & 0xff);
44 auth_plain(char *windom, char *keyp, uchar *chal, int len)
51 up = auth_getuserpasswd(auth_getkey, "windom=%s proto=pass service=cifs %s",
54 sysfatal("cannot get key - %r");
56 ap = emalloc9p(sizeof(Auth));
57 memset(ap, 0, sizeof(ap));
58 ap->user = estrdup9p(up->user);
59 ap->windom = estrdup9p(windom);
61 ap->resp[0] = estrdup9p(up->passwd);
62 ap->len[0] = strlen(up->passwd);
63 memset(up->passwd, 0, strlen(up->passwd));
70 auth_lm_and_ntlm(char *windom, char *keyp, uchar *chal, int len)
77 err = auth_respond(chal, len, user, sizeof user, &mcr, sizeof mcr,
78 auth_getkey, "windom=%s proto=mschap role=client service=cifs %s",
81 sysfatal("cannot get key - %r");
83 ap = emalloc9p(sizeof(Auth));
84 memset(ap, 0, sizeof(ap));
85 ap->user = estrdup9p(user);
86 ap->windom = estrdup9p(windom);
89 ap->len[0] = sizeof(mcr.LMresp);
90 ap->resp[0] = emalloc9p(ap->len[0]);
91 memcpy(ap->resp[0], mcr.LMresp, ap->len[0]);
94 ap->len[1] = sizeof(mcr.NTresp);
95 ap->resp[1] = emalloc9p(ap->len[1]);
96 memcpy(ap->resp[1], mcr.NTresp, ap->len[1]);
102 * NTLM response only, the LM response is a just
103 * copy of the NTLM one. we do this because the lm
104 * response is easily reversed - Google for l0pht
108 auth_ntlm(char *windom, char *keyp, uchar *chal, int len)
112 if((ap = auth_lm_and_ntlm(windom, keyp, chal, len)) == nil)
116 ap->len[0] = ap->len[1];
117 ap->resp[0] = emalloc9p(ap->len[0]);
118 memcpy(ap->resp[0], ap->resp[1], ap->len[0]);
123 * This is not really nescessary as all fields hmac_md5'ed
124 * in the ntlmv2 protocol are less than 64 bytes long, however
125 * I still do this for completeness
128 hmac_t64(uchar *data, ulong dlen, uchar *key, ulong klen, uchar *digest,
133 return hmac_md5(data, dlen, key, klen, digest, state);
138 ntv2_blob(uchar *blob, int len, char *windom)
145 enum { /* name types */
146 Beof, /* end of name list */
147 Bnetbios, /* Netbios machine name */
148 Bdomain, /* Windows Domain name (NT) */
149 Bdnsfqdn, /* DNS Fully Qualified Domain Name */
150 Bdnsname, /* DNS machine name (win2k) */
154 *p++ = 1; /* response type */
155 *p++ = 1; /* max response type understood by client */
158 *p++ = 0; /* 2 bytes reserved */
163 *p++ = 0; /* 4 bytes unknown */
165 nttime = time(nil); /* nt time now */
166 nttime += 11644473600LL;
167 nttime *= 10000000LL;
178 p += 8; /* client nonce */
182 *p++ = 0; /* unknown data */
185 *p++ = 0; /* name type */
187 n = utflen(windom) * 2;
189 *p++ = n >> 8; /* name length */
192 while(*d && p-blob < (len-8)){
193 d += chartorune(&r, d);
200 *p++ = Beof; /* name type */
203 *p++ = 0; /* name length */
208 *p++ = 0; /* unknown data */
213 auth_ntlmv2(char *windom, char *keyp, uchar *chal, int len)
218 uchar v1hash[MD5dlen], blip[Bliplen], blob[1024], v2hash[MD5dlen];
219 uchar c, lm_hmac[MD5dlen], nt_hmac[MD5dlen], nt_sesskey[MD5dlen],
225 up = auth_getuserpasswd(auth_getkey, "windom=%s proto=pass service=cifs-ntlmv2 %s",
228 sysfatal("cannot get key - %r");
230 ap = emalloc9p(sizeof(Auth));
231 memset(ap, 0, sizeof(ap));
233 /* Standard says unlimited length, experience says 128 max */
234 if((n = strlen(up->passwd)) > 128)
237 ds = md4(nil, 0, nil, nil);
238 for(i=0, p=up->passwd; i < n; i++) {
239 p += chartorune(&r, p);
245 md4(nil, 0, v1hash, ds);
248 * Some documentation insists that the username must be forced to
249 * uppercase, but the domain name should not be. Other shows both
250 * being forced to uppercase. I am pretty sure this is irrevevant as the
251 * domain name passed from the remote server always seems to be in
254 ds = hmac_t64(nil, 0, v1hash, MD5dlen, nil, nil);
257 u += chartorune(&r, u);
260 hmac_t64(&c, 1, v1hash, MD5dlen, nil, ds);
262 hmac_t64(&c, 1, v1hash, MD5dlen, nil, ds);
267 u += chartorune(&r, u);
269 hmac_t64(&c, 1, v1hash, MD5dlen, nil, ds);
271 hmac_t64(&c, 1, v1hash, MD5dlen, nil, ds);
273 hmac_t64(nil, 0, v1hash, MD5dlen, v2hash, ds);
274 ap->user = estrdup9p(up->user);
275 ap->windom = estrdup9p(windom);
279 genrandom(blip, Bliplen);
280 ds = hmac_t64(chal, len, v2hash, MD5dlen, nil, nil);
281 hmac_t64(blip, Bliplen, v2hash, MD5dlen, lm_hmac, ds);
282 ap->len[0] = MD5dlen+Bliplen;
283 ap->resp[0] = emalloc9p(ap->len[0]);
284 memcpy(ap->resp[0], lm_hmac, MD5dlen);
285 memcpy(ap->resp[0]+MD5dlen, blip, Bliplen);
287 /* LM v2 session key */
288 hmac_t64(lm_hmac, MD5dlen, v2hash, MD5dlen, lm_sesskey, nil);
291 ap->mackey[0] = emalloc9p(MACkeylen);
292 memcpy(ap->mackey[0], lm_sesskey, MD5dlen);
293 memcpy(ap->mackey[0]+MD5dlen, ap->resp[0], MACkeylen-MD5dlen);
296 n = ntv2_blob(blob, sizeof(blob), windom);
297 ds = hmac_t64(chal, len, v2hash, MD5dlen, nil, nil);
298 hmac_t64(blob, n, v2hash, MD5dlen, nt_hmac, ds);
299 ap->len[1] = MD5dlen+n;
300 ap->resp[1] = emalloc9p(ap->len[1]);
301 memcpy(ap->resp[1], nt_hmac, MD5dlen);
302 memcpy(ap->resp[1]+MD5dlen, blob, n);
305 * v2hash definitely OK by
306 * the time we get here.
308 /* NTLM v2 session key */
309 hmac_t64(nt_hmac, MD5dlen, v2hash, MD5dlen, nt_sesskey, nil);
311 /* NTLM v2 MAC key */
312 ap->mackey[1] = emalloc9p(MACkeylen);
313 memcpy(ap->mackey[1], nt_sesskey, MD5dlen);
314 memcpy(ap->mackey[1]+MD5dlen, ap->resp[1], MACkeylen-MD5dlen);
322 Auth *(*func)(char *, char *, uchar *, int);
324 { "plain", auth_plain },
325 { "lm+ntlm", auth_lm_and_ntlm },
326 { "ntlm", auth_ntlm },
327 { "ntlmv2", auth_ntlmv2 },
328 // { "kerberos", auth_kerberos },
336 fprint(2, "supported auth methods:\t");
337 for(i = 0; i < nelem(methods); i++)
338 fprint(2, "%s ", methods[i].name);
344 getauth(char *name, char *windom, char *keyp, int secmode, uchar *chal, int len)
351 if((secmode & SECMODE_PW_ENCRYPT) == 0)
352 sysfatal("plaintext authentication required, use '-a plain'");
356 for(i = 0; i < nelem(methods); i++)
357 if(strcmp(methods[i].name, name) == 0){
358 ap = methods[i].func(windom, keyp, chal, len);
363 fprint(2, "%s: %s - unknown auth method\n", argv0, name);
364 autherr(); /* never returns */
370 genmac(uchar *buf, int len, int seq, uchar key[MACkeylen], uchar ours[MAClen])
373 uchar *sig, digest[MD5dlen], theirs[MAClen];
376 memcpy(theirs, sig, MAClen);
378 memset(sig, 0, MAClen);
384 ds = md5(key, MACkeylen, nil, nil);
385 md5(buf, len, digest, ds);
386 memcpy(ours, digest, MAClen);
388 return memcmp(theirs, ours, MAClen);
392 macsign(Pkt *p, int seq)
395 uchar *sig, *buf, mac[MAClen];
397 sig = p->buf + NBHDRLEN + MACoff;
398 buf = p->buf + NBHDRLEN;
399 len = (p->pos - p->buf) - NBHDRLEN;
403 dmp("rx", seq, sig, MAClen);
407 memcpy(mac, "BSRSPYL ", 8); /* no idea, ask MS */
409 rc = genmac(buf, len, seq, p->s->auth->mackey[0], mac);
412 dmp("tx", seq, mac, MAClen);
414 memcpy(sig, mac, MAClen);