auth.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  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.
  6. */
  7. #include <u.h>
  8. #include <libc.h>
  9. #include <mp.h>
  10. #include <auth.h>
  11. #include <libsec.h>
  12. #include <ctype.h>
  13. #include <fcall.h>
  14. #include <thread.h>
  15. #include <9p.h>
  16. #include "cifs.h"
  17. #define DEF_AUTH "ntlmv2"
  18. static enum {
  19. MACkeylen = 40, /* MAC key len */
  20. MAClen = 8, /* signature length */
  21. MACoff = 14, /* sign. offset from start of SMB (not netbios) pkt */
  22. Bliplen = 8, /* size of LMv2 client nonce */
  23. };
  24. static void
  25. dmp(char *s, int seq, void *buf, int n)
  26. {
  27. int i;
  28. char *p = buf;
  29. print("%s %3d ", s, seq);
  30. while(n > 0){
  31. for(i = 0; i < 16 && n > 0; i++, n--)
  32. print("%02x ", *p++ & 0xff);
  33. if(n > 0)
  34. print("\n");
  35. }
  36. print("\n");
  37. }
  38. static Auth *
  39. auth_plain(char *windom, char *keyp, uchar *chal, int len)
  40. {
  41. UserPasswd *up;
  42. static Auth *ap;
  43. USED(chal, len);
  44. up = auth_getuserpasswd(auth_getkey, "windom=%s proto=pass service=cifs %s",
  45. windom, keyp);
  46. if(! up)
  47. sysfatal("cannot get key - %r");
  48. ap = emalloc9p(sizeof(Auth));
  49. memset(ap, 0, sizeof(ap));
  50. ap->user = estrdup9p(up->user);
  51. ap->windom = estrdup9p(windom);
  52. ap->resp[0] = estrdup9p(up->passwd);
  53. ap->len[0] = strlen(up->passwd);
  54. memset(up->passwd, 0, strlen(up->passwd));
  55. free(up);
  56. return ap;
  57. }
  58. static Auth *
  59. auth_lm_and_ntlm(char *windom, char *keyp, uchar *chal, int len)
  60. {
  61. int err;
  62. char user[64];
  63. Auth *ap;
  64. MSchapreply mcr;
  65. err = auth_respond(chal, len, user, sizeof user, &mcr, sizeof mcr,
  66. auth_getkey, "windom=%s proto=mschap role=client service=cifs %s",
  67. windom, keyp);
  68. if(err == -1)
  69. sysfatal("cannot get key - %r");
  70. ap = emalloc9p(sizeof(Auth));
  71. memset(ap, 0, sizeof(ap));
  72. ap->user = estrdup9p(user);
  73. ap->windom = estrdup9p(windom);
  74. /* LM response */
  75. ap->len[0] = sizeof(mcr.LMresp);
  76. ap->resp[0] = emalloc9p(ap->len[0]);
  77. memcpy(ap->resp[0], mcr.LMresp, ap->len[0]);
  78. /* NTLM response */
  79. ap->len[1] = sizeof(mcr.NTresp);
  80. ap->resp[1] = emalloc9p(ap->len[1]);
  81. memcpy(ap->resp[1], mcr.NTresp, ap->len[1]);
  82. return ap;
  83. }
  84. /*
  85. * NTLM response only, the LM response is a just
  86. * copy of the NTLM one. we do this because the lm
  87. * response is easily reversed - Google for l0pht
  88. * for more info.
  89. */
  90. static Auth *
  91. auth_ntlm(char *windom, char *keyp, uchar *chal, int len)
  92. {
  93. Auth *ap;
  94. if((ap = auth_lm_and_ntlm(windom, keyp, chal, len)) == nil)
  95. return nil;
  96. free(ap->resp[0]);
  97. ap->len[0] = ap->len[1];
  98. ap->resp[0] = emalloc9p(ap->len[0]);
  99. memcpy(ap->resp[0], ap->resp[1], ap->len[0]);
  100. return ap;
  101. }
  102. /*
  103. * This is not really nescessary as all fields hmac_md5'ed
  104. * in the ntlmv2 protocol are less than 64 bytes long, however
  105. * I still do this for completeness
  106. */
  107. static DigestState *
  108. hmac_t64(uchar *data, ulong dlen, uchar *key, ulong klen, uchar *digest,
  109. DigestState *state)
  110. {
  111. if(klen > 64)
  112. klen = 64;
  113. return hmac_md5(data, dlen, key, klen, digest, state);
  114. }
  115. static int
  116. ntv2_blob(uchar *blob, int len, char *windom)
  117. {
  118. int n;
  119. uvlong nttime;
  120. Rune r;
  121. char *d;
  122. uchar *p;
  123. enum { /* name types */
  124. Beof, /* end of name list */
  125. Bnetbios, /* Netbios machine name */
  126. Bdomain, /* Windows Domain name (NT) */
  127. Bdnsfqdn, /* DNS Fully Qualified Domain Name */
  128. Bdnsname, /* DNS machine name (win2k) */
  129. };
  130. p = blob;
  131. *p++ = 1; /* response type */
  132. *p++ = 1; /* max response type understood by client */
  133. *p++ = 0;
  134. *p++ = 0; /* 2 bytes reserved */
  135. *p++ = 0;
  136. *p++ = 0;
  137. *p++ = 0;
  138. *p++ = 0; /* 4 bytes unknown */
  139. nttime = time(nil); /* nt time now */
  140. nttime += 11644473600LL;
  141. nttime *= 10000000LL;
  142. *p++ = nttime;
  143. *p++ = nttime >> 8;
  144. *p++ = nttime >> 16;
  145. *p++ = nttime >> 24;
  146. *p++ = nttime >> 32;
  147. *p++ = nttime >> 40;
  148. *p++ = nttime >> 48;
  149. *p++ = nttime >> 56;
  150. genrandom(p, 8);
  151. p += 8; /* client nonce */
  152. *p++ = 0x6f;
  153. *p++ = 0;
  154. *p++ = 0x6e;
  155. *p++ = 0; /* unknown data */
  156. *p++ = Bdomain;
  157. *p++ = 0; /* name type */
  158. n = utflen(windom) * 2;
  159. *p++ = n;
  160. *p++ = n >> 8; /* name length */
  161. d = windom;
  162. while(*d && p-blob < (len-8)){
  163. d += chartorune(&r, d);
  164. r = toupperrune(r);
  165. *p++ = r;
  166. *p++ = r >> 8;
  167. }
  168. *p++ = 0;
  169. *p++ = Beof; /* name type */
  170. *p++ = 0;
  171. *p++ = 0; /* name length */
  172. *p++ = 0x65;
  173. *p++ = 0;
  174. *p++ = 0;
  175. *p++ = 0; /* unknown data */
  176. return p - blob;
  177. }
  178. static Auth *
  179. auth_ntlmv2(char *windom, char *keyp, uchar *chal, int len)
  180. {
  181. int i, n;
  182. Rune r;
  183. char *p, *u;
  184. uchar v1hash[MD5dlen], blip[Bliplen], blob[1024], v2hash[MD5dlen];
  185. uchar c, lm_hmac[MD5dlen], nt_hmac[MD5dlen], nt_sesskey[MD5dlen],
  186. lm_sesskey[MD5dlen];
  187. DigestState *ds;
  188. UserPasswd *up;
  189. static Auth *ap;
  190. up = auth_getuserpasswd(auth_getkey, "windom=%s proto=pass service=cifs-ntlmv2 %s",
  191. windom, keyp);
  192. if(!up)
  193. sysfatal("cannot get key - %r");
  194. ap = emalloc9p(sizeof(Auth));
  195. memset(ap, 0, sizeof(ap));
  196. /* Standard says unlimited length, experience says 128 max */
  197. if((n = strlen(up->passwd)) > 128)
  198. n = 128;
  199. ds = md4(nil, 0, nil, nil);
  200. for(i=0, p=up->passwd; i < n; i++) {
  201. p += chartorune(&r, p);
  202. c = r;
  203. md4(&c, 1, nil, ds);
  204. c = r >> 8;
  205. md4(&c, 1, nil, ds);
  206. }
  207. md4(nil, 0, v1hash, ds);
  208. /*
  209. * Some documentation insists that the username must be forced to
  210. * uppercase, but the domain name should not be. Other shows both
  211. * being forced to uppercase. I am pretty sure this is irrevevant as the
  212. * domain name passed from the remote server always seems to be in
  213. * uppercase already.
  214. */
  215. ds = hmac_t64(nil, 0, v1hash, MD5dlen, nil, nil);
  216. u = up->user;
  217. while(*u){
  218. u += chartorune(&r, u);
  219. r = toupperrune(r);
  220. c = r;
  221. hmac_t64(&c, 1, v1hash, MD5dlen, nil, ds);
  222. c = r >> 8;
  223. hmac_t64(&c, 1, v1hash, MD5dlen, nil, ds);
  224. }
  225. u = windom;
  226. while(*u){
  227. u += chartorune(&r, u);
  228. c = r;
  229. hmac_t64(&c, 1, v1hash, MD5dlen, nil, ds);
  230. c = r >> 8;
  231. hmac_t64(&c, 1, v1hash, MD5dlen, nil, ds);
  232. }
  233. hmac_t64(nil, 0, v1hash, MD5dlen, v2hash, ds);
  234. ap->user = estrdup9p(up->user);
  235. ap->windom = estrdup9p(windom);
  236. /* LM v2 */
  237. genrandom(blip, Bliplen);
  238. ds = hmac_t64(chal, len, v2hash, MD5dlen, nil, nil);
  239. hmac_t64(blip, Bliplen, v2hash, MD5dlen, lm_hmac, ds);
  240. ap->len[0] = MD5dlen+Bliplen;
  241. ap->resp[0] = emalloc9p(ap->len[0]);
  242. memcpy(ap->resp[0], lm_hmac, MD5dlen);
  243. memcpy(ap->resp[0]+MD5dlen, blip, Bliplen);
  244. /* LM v2 session key */
  245. hmac_t64(lm_hmac, MD5dlen, v2hash, MD5dlen, lm_sesskey, nil);
  246. /* LM v2 MAC key */
  247. ap->mackey[0] = emalloc9p(MACkeylen);
  248. memcpy(ap->mackey[0], lm_sesskey, MD5dlen);
  249. memcpy(ap->mackey[0]+MD5dlen, ap->resp[0], MACkeylen-MD5dlen);
  250. /* NTLM v2 */
  251. n = ntv2_blob(blob, sizeof(blob), windom);
  252. ds = hmac_t64(chal, len, v2hash, MD5dlen, nil, nil);
  253. hmac_t64(blob, n, v2hash, MD5dlen, nt_hmac, ds);
  254. ap->len[1] = MD5dlen+n;
  255. ap->resp[1] = emalloc9p(ap->len[1]);
  256. memcpy(ap->resp[1], nt_hmac, MD5dlen);
  257. memcpy(ap->resp[1]+MD5dlen, blob, n);
  258. /*
  259. * v2hash definitely OK by
  260. * the time we get here.
  261. */
  262. /* NTLM v2 session key */
  263. hmac_t64(nt_hmac, MD5dlen, v2hash, MD5dlen, nt_sesskey, nil);
  264. /* NTLM v2 MAC key */
  265. ap->mackey[1] = emalloc9p(MACkeylen);
  266. memcpy(ap->mackey[1], nt_sesskey, MD5dlen);
  267. memcpy(ap->mackey[1]+MD5dlen, ap->resp[1], MACkeylen-MD5dlen);
  268. free(up);
  269. return ap;
  270. }
  271. struct {
  272. char *name;
  273. Auth *(*func)(char *, char *, uchar *, int);
  274. } methods[] = {
  275. { "plain", auth_plain },
  276. { "lm+ntlm", auth_lm_and_ntlm },
  277. { "ntlm", auth_ntlm },
  278. { "ntlmv2", auth_ntlmv2 },
  279. // { "kerberos", auth_kerberos },
  280. };
  281. void
  282. autherr(void)
  283. {
  284. int i;
  285. fprint(2, "supported auth methods:\t");
  286. for(i = 0; i < nelem(methods); i++)
  287. fprint(2, "%s ", methods[i].name);
  288. fprint(2, "\n");
  289. exits("usage");
  290. }
  291. Auth *
  292. getauth(char *name, char *windom, char *keyp, int secmode, uchar *chal, int len)
  293. {
  294. int i;
  295. Auth *ap;
  296. if(name == nil){
  297. name = DEF_AUTH;
  298. if((secmode & SECMODE_PW_ENCRYPT) == 0)
  299. sysfatal("plaintext authentication required, use '-a plain'");
  300. }
  301. ap = nil;
  302. for(i = 0; i < nelem(methods); i++)
  303. if(strcmp(methods[i].name, name) == 0){
  304. ap = methods[i].func(windom, keyp, chal, len);
  305. break;
  306. }
  307. if(! ap){
  308. fprint(2, "%s: %s - unknown auth method\n", argv0, name);
  309. autherr(); /* never returns */
  310. }
  311. return ap;
  312. }
  313. static int
  314. genmac(uchar *buf, int len, int seq, uchar key[MACkeylen], uchar ours[MAClen])
  315. {
  316. DigestState *ds;
  317. uchar *sig, digest[MD5dlen], theirs[MAClen];
  318. sig = buf+MACoff;
  319. memcpy(theirs, sig, MAClen);
  320. memset(sig, 0, MAClen);
  321. sig[0] = seq;
  322. sig[1] = seq >> 8;
  323. sig[2] = seq >> 16;
  324. sig[3] = seq >> 24;
  325. ds = md5(key, MACkeylen, nil, nil);
  326. md5(buf, len, digest, ds);
  327. memcpy(ours, digest, MAClen);
  328. return memcmp(theirs, ours, MAClen);
  329. }
  330. int
  331. macsign(Pkt *p, int seq)
  332. {
  333. int rc, len;
  334. uchar *sig, *buf, mac[MAClen];
  335. sig = p->buf + NBHDRLEN + MACoff;
  336. buf = p->buf + NBHDRLEN;
  337. len = (p->pos - p->buf) - NBHDRLEN;
  338. #ifdef DEBUG_MAC
  339. if(seq & 1)
  340. dmp("rx", seq, sig, MAClen);
  341. #endif
  342. rc = 0;
  343. if(! p->s->seqrun)
  344. memcpy(mac, "BSRSPYL ", 8); /* no idea, ask MS */
  345. else
  346. rc = genmac(buf, len, seq, p->s->auth->mackey[0], mac);
  347. #ifdef DEBUG_MAC
  348. if(!(seq & 1))
  349. dmp("tx", seq, mac, MAClen);
  350. #endif
  351. memcpy(sig, mac, MAClen);
  352. return rc;
  353. }