forked from vanhauser-thc/thc-hydra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hydra-smb.c
1542 lines (1323 loc) · 54.4 KB
/
hydra-smb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "hydra-mod.h"
#ifndef LIBOPENSSL
void dummy_smb() { printf("\n"); }
#else
#include "hmacmd5.h"
#include "sasl.h"
#include <openssl/des.h>
#include <openssl/md4.h>
// FIXME XXX BUG: several malloc()s without return code checking
/*
http://technet.microsoft.com/en-us/library/cc960646.aspx
Most of the new code comes from Medusa smbnt module
------------------------------------------------------------------------
Copyright (C) 2009 Joe Mondloch
JoMo-Kun / jmk@foofus.net
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2,
as published by the Free Software Foundation
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
http://www.gnu.org/licenses/gpl.txt
This program is released under the GPL with the additional exemption
that compiling, linking, and/or using OpenSSL is allowed.
------------------------------------------------------------------------
Based on code from: SMB Auditing Tool
[Copyright (C) Patrik Karlsson 2001]
This code allows Hydra to directly test NTLM hashes against
a Windows. This may be useful for an auditor who has aquired
a sam._ or pwdump file and would like to quickly determine
which are valid entries. This module can also be used to test
SMB passwords against devices that do not allow clear text
LanMan passwords.
The "-m 'METHOD'" option is required for this module. The
following are valid methods: Local, Domain, Hash, Machine,
NTLMV2, NTLM, LMV2, LM (in quotes).
Local == Check local account.
Domain == Check credentials against this hosts primary
domain controller via this host.
Hash == Use a NTLM hash rather than a password.
Machine == Use the Machine's NetBIOS name as the password.
NTLMV2, NTLM, LMV2, LM == set the dialect
Be careful of mass domain account lockout with this. For
example, assume you are checking several accounts against
many domain workstations. If you are not using the 'L'
options and these accounts do not exist locally on the
workstations, each workstation will in turn check their
respective domain controller. This could cause a bunch of
lockouts. Of course, it'd look like the workstations, not
you, were doing it. ;)
**FYI, this code is unable to test accounts on default XP
hosts which are not part of a domain and do not have normal
file sharing enabled. Default XP does not allow shares and
returns STATUS_LOGON_FAILED for both valid and invalid
credentials. XP with simple sharing enabled returns SUCCESS
for both valid and invalid credentials. If anyone knows a
way to test in these configurations...
*/
#define WIN2000_NATIVEMODE 1
#define WIN_NETBIOSMODE 2
#define PLAINTEXT 10
#define ENCRYPTED 11
#ifndef CHAR_BIT
#define CHAR_BIT 8
#endif
#ifndef TIME_T_MIN
#define TIME_T_MIN ((time_t)0 < (time_t)-1 ? (time_t)0 : ~(time_t)0 << (sizeof(time_t) * CHAR_BIT - 1))
#endif
#ifndef TIME_T_MAX
#define TIME_T_MAX (~(time_t)0 - TIME_T_MIN)
#endif
#define IVAL_NC(buf, pos) (*(uint32_t *)((char *)(buf) + (pos))) /* Non const version of above. */
#define SIVAL(buf, pos, val) IVAL_NC(buf, pos) = ((uint32_t)(val))
#define TIME_FIXUP_CONSTANT_INT 11644473600LL
extern char *HYDRA_EXIT;
static unsigned char challenge[8];
static unsigned char workgroup[16];
static unsigned char domain[16];
static unsigned char machine_name[16];
int32_t hashFlag, accntFlag, protoFlag;
int32_t smb_auth_mechanism = AUTH_NTLM;
int32_t security_mode = ENCRYPTED;
static size_t UTF8_UTF16LE(unsigned char *in, int32_t insize, unsigned char *out, int32_t outsize) {
int32_t i = 0, j = 0;
uint64_t ch;
if (debug) {
hydra_report(stderr, "[DEBUG] UTF8_UTF16LE in:\n");
hydra_dump_asciihex(in, insize);
}
for (i = 0; i < insize; i++) {
if (in[i] < 128) { // one byte
out[j] = in[i];
out[j + 1] = 0;
j = j + 2;
} else if ((in[i] >= 0xc0) && (in[i] <= 0xdf)) { // Two bytes
out[j + 1] = 0x07 & (in[i] >> 2);
out[j] = (0xc0 & (in[i] << 6)) | (0x3f & in[i + 1]);
j = j + 2;
i = i + 1;
} else if ((in[i] >= 0xe0) && (in[i] <= 0xef)) { // Three bytes
out[j] = (0xc0 & (in[i + 1] << 6)) | (0x3f & in[i + 2]);
out[j + 1] = (0xf0 & (in[i] << 4)) | (0x0f & (in[i + 1] >> 2));
j = j + 2;
i = i + 2;
} else if ((in[i] >= 0xf0) && (in[i] <= 0xf7)) { // Four bytes
ch = ((in[i] & 0x07) << 18) + ((0x3f & in[i + 1]) << 12) + ((0x3f & in[i + 2]) << 6) + (0x3f & in[i + 3]) - 0x10000;
out[j] = (ch >> 10) & 0xff;
out[j + 1] = 0xd8 | ((ch >> 18) & 0xff);
out[j + 2] = ch & 0xff;
out[j + 3] = 0xdc | ((ch >> 8) & 0x3);
j = j + 4;
i = i + 3;
}
if (j - 2 > outsize)
break;
}
if (debug) {
hydra_report(stderr, "[DEBUG] UTF8_UTF16LE out:\n");
hydra_dump_asciihex(out, j);
}
return j;
}
static unsigned char Get7Bits(unsigned char *input, int32_t startBit) {
register uint32_t word;
word = (unsigned)input[startBit / 8] << 8;
word |= (unsigned)input[startBit / 8 + 1];
word >>= 15 - (startBit % 8 + 7);
return word & 0xFE;
}
/* Make the key */
static void MakeKey(unsigned char *key, unsigned char *DES_key) {
DES_key[0] = Get7Bits(key, 0);
DES_key[1] = Get7Bits(key, 7);
DES_key[2] = Get7Bits(key, 14);
DES_key[3] = Get7Bits(key, 21);
DES_key[4] = Get7Bits(key, 28);
DES_key[5] = Get7Bits(key, 35);
DES_key[6] = Get7Bits(key, 42);
DES_key[7] = Get7Bits(key, 49);
DES_set_odd_parity((DES_cblock *)DES_key);
}
/* Do the DesEncryption */
void DesEncrypt(unsigned char *clear, unsigned char *key, unsigned char *cipher) {
DES_cblock DES_key;
DES_key_schedule key_schedule;
MakeKey(key, DES_key);
DES_set_key(&DES_key, &key_schedule);
DES_ecb_encrypt((DES_cblock *)clear, (DES_cblock *)cipher, &key_schedule, 1);
}
/*
HashLM
Function: Create a LM hash from the challenge
Variables:
lmhash = the hash created from this function
pass = users password
challenge = the challenge recieved from the server
*/
int32_t HashLM(unsigned char **lmhash, unsigned char *pass, unsigned char *challenge) {
static unsigned char magic[] = {0x4b, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25};
unsigned char password[14 + 1];
unsigned char lm_hash[21];
unsigned char lm_response[24];
int32_t i = 0, j = 0;
unsigned char *p = NULL;
char HexChar;
int32_t HexValue;
memset(password, 0, 14 + 1);
memset(lm_hash, 0, 21);
memset(lm_response, 0, 24);
/* Use LM Hash instead of password */
/* D42E35E1A1E4C22BD32E2170E4857C20:5E20780DD45857A68402938C7629D3B2::: */
if (hashFlag == 1) {
p = pass;
while ((*p != '\0') && (i < 1)) {
if (*p == ':')
i++;
p++;
}
if (*p == '\0') {
hydra_report(stderr, "[ERROR] Reading PwDump file.\n");
return -1;
} else if (*p == 'N') {
if (verbose)
hydra_report(stderr, "[VERBOSE] Found \"NO PASSWORD\" for LM Hash.\n");
/* Generate 16-byte LM hash */
DesEncrypt(magic, &password[0], &lm_hash[0]);
DesEncrypt(magic, &password[7], &lm_hash[8]);
} else {
if (verbose)
hydra_report(stderr, "[VERBOSE] Convert ASCII PwDump LM Hash (%s).\n", p);
for (i = 0; i < 16; i++) {
HexValue = 0x0;
for (j = 0; j < 2; j++) {
HexChar = (char)p[2 * i + j];
if (HexChar > 0x39)
HexChar = HexChar | 0x20; /* convert upper case to lower */
if (!(((HexChar >= 0x30) && (HexChar <= 0x39)) || /* 0 - 9 */
((HexChar >= 0x61) && (HexChar <= 0x66)))) { /* a - f */
hydra_report(stderr, "[ERROR] Invalid char (%c) for hash.\n", HexChar);
HexChar = 0x30;
}
HexChar -= 0x30;
if (HexChar > 0x09) /* HexChar is "a" - "f" */
HexChar -= 0x27;
HexValue = (HexValue << 4) | (char)HexChar;
}
lm_hash[i] = (unsigned char)HexValue;
}
}
} else {
/* Password == Machine Name */
if (hashFlag == 2) {
for (i = 0; i < 16; i++) {
if (machine_name[i] > 0x39)
machine_name[i] = machine_name[i] | 0x20; /* convert upper case to lower */
pass = machine_name;
}
}
/* convert lower case characters to upper case */
strncpy((char *)password, (char *)pass, 14);
for (i = 0; i < 14; i++) {
if ((password[i] >= 0x61) && (password[i] <= 0x7a)) /* a - z */
password[i] -= 0x20;
}
/* Generate 16-byte LM hash */
DesEncrypt(magic, &password[0], &lm_hash[0]);
DesEncrypt(magic, &password[7], &lm_hash[8]);
}
/*
NULL-pad 16-byte LM hash to 21-bytes
Split resultant value into three 7-byte thirds
DES-encrypt challenge using each third as a key
Concatenate three 8-byte resulting values to form 24-byte LM response
*/
DesEncrypt(challenge, &lm_hash[0], &lm_response[0]);
DesEncrypt(challenge, &lm_hash[7], &lm_response[8]);
DesEncrypt(challenge, &lm_hash[14], &lm_response[16]);
memcpy(*lmhash, lm_response, 24);
return 0;
}
/*
MakeNTLM
Function: Create a NTLM hash from the password
*/
int32_t MakeNTLM(unsigned char *ntlmhash, unsigned char *pass) {
MD4_CTX md4Context;
unsigned char hash[16]; /* MD4_SIGNATURE_SIZE = 16 */
unsigned char unicodePassword[256 * 2]; /* MAX_NT_PASSWORD = 256 */
int32_t i = 0, j = 0;
int32_t mdlen;
unsigned char *p = NULL;
char HexChar;
int32_t HexValue;
/* Use NTLM Hash instead of password */
if (hashFlag == 1) {
/* 1000:D42E35E1A1E4C22BD32E2170E4857C20:5E20780DD45857A68402938C7629D3B2:::
*/
p = pass;
while ((*p != '\0') && (i < 1)) {
if (*p == ':')
i++;
p++;
}
if (*p == '\0') {
hydra_report(stderr, "[ERROR] reading PWDUMP file.\n");
return -1;
}
for (i = 0; i < 16; i++) {
HexValue = 0x0;
for (j = 0; j < 2; j++) {
HexChar = (char)p[2 * i + j];
if (HexChar > 0x39)
HexChar = HexChar | 0x20; /* convert upper case to lower */
if (!(((HexChar >= 0x30) && (HexChar <= 0x39)) || /* 0 - 9 */
((HexChar >= 0x61) && (HexChar <= 0x66)))) { /* a - f */
/*
* fprintf(stderr, "Error invalid char (%c) for hash.\n", HexChar);
* hydra_child_exit(0);
*/
HexChar = 0x30;
}
HexChar -= 0x30;
if (HexChar > 0x09) /* HexChar is "a" - "f" */
HexChar -= 0x27;
HexValue = (HexValue << 4) | (char)HexChar;
}
hash[i] = (unsigned char)HexValue;
}
} else {
/* Password == Machine Name */
if (hashFlag == 2) {
for (i = 0; i < 16; i++) {
if (machine_name[i] > 0x39)
machine_name[i] = machine_name[i] | 0x20; /* convert upper case to lower */
pass = machine_name;
}
}
/* Initialize the Unicode version of the secret (== password). */
/* This implicitly supports most UTF8 characters. */
j = UTF8_UTF16LE(pass, strlen((char *)pass), unicodePassword, sizeof(unicodePassword));
mdlen = j; /* length in bytes */
MD4_Init(&md4Context);
MD4_Update(&md4Context, unicodePassword, mdlen);
MD4_Final(hash, &md4Context); /* Tell MD4 we're done */
}
memcpy(ntlmhash, hash, 16);
return 0;
}
/*
HashLMv2
This function implements the LMv2 response algorithm. The LMv2 response is
used to provide pass-through authentication compatibility with older servers.
The response is based on the NTLM password hash and is exactly 24 bytes.
The below code is based heavily on the following resources:
http://davenport.sourceforge.net/ntlm.html#theLmv2Response
samba-3.0.28a - libsmb/smbencrypt.c
jcifs - packet capture of LMv2-only connection
*/
int32_t HashLMv2(unsigned char **LMv2hash, unsigned char *szLogin, unsigned char *szPassword) {
unsigned char ntlm_hash[16];
unsigned char lmv2_response[24];
unsigned char unicodeUsername[20 * 2];
unsigned char unicodeTarget[256 * 2];
HMACMD5Context ctx;
unsigned char kr_buf[16];
int32_t ret, i;
unsigned char client_challenge[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
memset(ntlm_hash, 0, 16);
memset(lmv2_response, 0, 24);
memset(kr_buf, 0, 16);
/* --- HMAC #1 Caculations --- */
/* Calculate and set NTLM password hash */
ret = MakeNTLM((unsigned char *)&ntlm_hash, (unsigned char *)szPassword);
if (ret == -1)
return -1;
/*
The Unicode uppercase username is concatenated with the Unicode
authentication target (the domain or server name specified in the Target
Name field of the Type 3 message). Note that this calculation always uses
the Unicode representation, even if OEM encoding has been negotiated; also
note that the username is converted to uppercase, while the authentication
target is case-sensitive and must match the case presented in the Target
Name field.
The HMAC-MD5 message authentication code algorithm (described in RFC 2104)
is applied to this value using the 16-byte NTLM hash as the key. This
results in a 16-byte value - the NTLMv2 hash.
*/
/* Initialize the Unicode version of the username and target. */
/* This implicitly supports 8-bit ISO8859/1 characters. */
/* convert lower case characters to upper case */
bzero(unicodeUsername, sizeof(unicodeUsername));
for (i = 0; i < strlen((char *)szLogin); i++) {
if ((szLogin[i] >= 0x61) && (szLogin[i] <= 0x7a)) /* a - z */
unicodeUsername[i * 2] = (unsigned char)szLogin[i] - 0x20;
else
unicodeUsername[i * 2] = (unsigned char)szLogin[i];
}
bzero(unicodeTarget, sizeof(unicodeTarget));
for (i = 0; i < strlen((char *)workgroup); i++)
unicodeTarget[i * 2] = (unsigned char)workgroup[i];
hmac_md5_init_limK_to_64(ntlm_hash, 16, &ctx);
hmac_md5_update((const unsigned char *)unicodeUsername, 2 * strlen((char *)szLogin), &ctx);
hmac_md5_update((const unsigned char *)unicodeTarget, 2 * strlen((char *)workgroup), &ctx);
hmac_md5_final(kr_buf, &ctx);
/* --- HMAC #2 Calculations --- */
/*
The challenge from the Type 2 message is concatenated with our fixed client
nonce. The HMAC-MD5 message authentication code algorithm is applied to
this value using the 16-byte NTLMv2 hash (calculated above) as the key.
This results in a 16-byte output value.
*/
hmac_md5_init_limK_to_64(kr_buf, 16, &ctx);
hmac_md5_update((const unsigned char *)challenge, 8, &ctx);
hmac_md5_update(client_challenge, 8, &ctx);
hmac_md5_final(lmv2_response, &ctx);
/* --- 24-byte LMv2 Response Complete --- */
if ((*LMv2hash = malloc(24)) == NULL)
return -1;
memset(*LMv2hash, 0, 24);
memcpy(*LMv2hash, lmv2_response, 16);
memcpy(*LMv2hash + 16, client_challenge, 8);
return 0;
}
/*
HashNTLMv2
This function implements the NTLMv2 response algorithm. Support for this
algorithm was added with Microsoft Windows with NT 4.0 SP4. It should be noted
that code doesn't currently work with Microsoft Vista. While NTLMv2
authentication with Samba and Windows 2003 functions as expected, Vista
systems respond with the oh-so-helpful "INVALID_PARAMETER" error code.
LMv2-only authentication appears to work against Vista in cases where LM and
NTLM are refused.
The below code is based heavily on the following two resources:
http://davenport.sourceforge.net/ntlm.html#theNtlmv2Response
samba-3.0.28 - libsmb/smbencrypt.c
NTLMv2 network authentication is required when attempting to authenticated to
a system which has the following policy enforced:
GPO: "Network Security: LAN Manager authentication level"
Setting: "Send NTLMv2 response only\refuse LM & NTLM"
*/
int32_t HashNTLMv2(unsigned char **NTLMv2hash, int32_t *iByteCount, unsigned char *szLogin, unsigned char *szPassword) {
unsigned char ntlm_hash[16];
unsigned char ntlmv2_response[56 + 20 * 2 + 256 * 2];
unsigned char unicodeUsername[20 * 2];
unsigned char unicodeTarget[256 * 2];
HMACMD5Context ctx;
unsigned char kr_buf[16];
int32_t ret, i, iTargetLen;
unsigned char client_challenge[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
/*
-- Example NTLMv2 Response Data --
[0] HMAC: (16 bytes)
[16] Header: Blob Signature [01 01 00 00] (4 bytes)
[20] Reserved: [00 00 00 00] (4 bytes)
[24] Time: Little-endian, 64-bit signed value representing the number
of tenths of a microsecond since January 1, 1601. (8 bytes) [32] Client
Nonce: (8 bytes) [40] Unknown: 00 00 00 00 (4 bytes) [44] Target
Information (from the Type 2 message) NetBIOS domain/workgroup: Type:
domain 02 00 (2 bytes) Length: 12 00 (2 bytes) Name: WORKGROUP [NULL
spacing -> 57 00 4f 00 ...] (18 bytes) End-of-list: 00 00 00 00 (4 bytes)
Termination: 00 00 00 00 (4 bytes)
*/
iTargetLen = 2 * strlen((char *)workgroup);
memset(ntlm_hash, 0, 16);
memset(ntlmv2_response, 0, 56 + 20 * 2 + 256 * 2);
memset(kr_buf, 0, 16);
/* --- HMAC #1 Caculations --- */
/* Calculate and set NTLM password hash */
ret = MakeNTLM((unsigned char *)&ntlm_hash, (unsigned char *)szPassword);
if (ret == -1)
return -1;
/*
The Unicode uppercase username is concatenated with the Unicode
authentication target (the domain or server name specified in the Target
Name field of the Type 3 message). Note that this calculation always uses
the Unicode representation, even if OEM encoding has been negotiated; also
note that the username is converted to uppercase, while the authentication
target is case-sensitive and must match the case presented in the Target
Name field.
The HMAC-MD5 message authentication code algorithm (described in RFC 2104)
is applied to this value using the 16-byte NTLM hash as the key. This
results in a 16-byte value - the NTLMv2 hash.
*/
/* Initialize the Unicode version of the username and target. */
/* This implicitly supports 8-bit ISO8859/1 characters. */
/* convert lower case characters to upper case */
bzero(unicodeUsername, sizeof(unicodeUsername));
for (i = 0; i < strlen((char *)szLogin); i++) {
if ((szLogin[i] >= 0x61) && (szLogin[i] <= 0x7a)) /* a - z */
unicodeUsername[i * 2] = (unsigned char)szLogin[i] - 0x20;
else
unicodeUsername[i * 2] = (unsigned char)szLogin[i];
}
bzero(unicodeTarget, sizeof(unicodeTarget));
for (i = 0; i < strlen((char *)workgroup); i++)
unicodeTarget[i * 2] = (unsigned char)workgroup[i];
hmac_md5_init_limK_to_64(ntlm_hash, 16, &ctx);
hmac_md5_update((const unsigned char *)unicodeUsername, 2 * strlen((char *)szLogin), &ctx);
hmac_md5_update((const unsigned char *)unicodeTarget, 2 * strlen((char *)workgroup), &ctx);
hmac_md5_final(kr_buf, &ctx);
/* --- Blob Construction --- */
memset(ntlmv2_response + 16, 1, 2); /* Blob Signature 0x01010000 */
memset(ntlmv2_response + 18, 0, 2);
memset(ntlmv2_response + 20, 0, 4); /* Reserved */
/* Time -- Take a Unix time and convert to an NT TIME structure:
Little-endian, 64-bit signed value representing the number of tenths of a
microsecond since January 1, 1601.
*/
struct timespec ts;
unsigned long long nt;
ts.tv_sec = (time_t)time(NULL);
ts.tv_nsec = 0;
if (ts.tv_sec == 0)
nt = 0;
else if (ts.tv_sec == TIME_T_MAX)
nt = 0x7fffffffffffffffLL;
else if (ts.tv_sec == (time_t)-1)
nt = (unsigned long)-1;
else {
nt = ts.tv_sec;
nt += TIME_FIXUP_CONSTANT_INT;
nt *= 1000 * 1000 * 10; /* nt is now in the 100ns units */
}
SIVAL(ntlmv2_response + 24, 0, nt & 0xFFFFFFFF);
SIVAL(ntlmv2_response + 24, 4, nt >> 32);
/* End time calculation */
/* Set client challenge - using a non-random value in this case. */
memcpy(ntlmv2_response + 32, client_challenge, 8); /* Client Nonce */
memset(ntlmv2_response + 40, 0, 4); /* Unknown */
/* Target Information Block */
/*
0x0100 Server name
0x0200 Domain name
0x0300 Fully-qualified DNS host name
0x0400 DNS domain name
TODO: Need to rework negotiation code to correctly extract target
information
*/
memset(ntlmv2_response + 44, 0x02, 1); /* Type: Domain */
memset(ntlmv2_response + 45, 0x00, 1);
memset(ntlmv2_response + 46, iTargetLen, 1); /* Length */
memset(ntlmv2_response + 47, 0x00, 1);
/* Name of domain or workgroup */
for (i = 0; i < strlen((char *)workgroup); i++)
ntlmv2_response[48 + i * 2] = (unsigned char)workgroup[i];
memset(ntlmv2_response + 48 + iTargetLen, 0, 4); /* End-of-list */
/* --- HMAC #2 Caculations --- */
/*
The challenge from the Type 2 message is concatenated with the blob. The
HMAC-MD5 message authentication code algorithm is applied to this value
using the 16-byte NTLMv2 hash (calculated above) as the key. This results
in a 16-byte output value.
*/
hmac_md5_init_limK_to_64(kr_buf, 16, &ctx);
hmac_md5_update(challenge, 8, &ctx);
hmac_md5_update(ntlmv2_response + 16, 48 - 16 + iTargetLen + 4, &ctx);
hmac_md5_final(ntlmv2_response, &ctx);
*iByteCount = 48 + iTargetLen + 4;
if ((*NTLMv2hash = malloc(*iByteCount)) == NULL)
return -1;
memset(*NTLMv2hash, 0, *iByteCount);
memcpy(*NTLMv2hash, ntlmv2_response, *iByteCount);
return 0;
}
/*
HashNTLM
Function: Create a NTLM hash from the challenge
Variables:
ntlmhash = the hash created from this function
pass = users password
challenge = the challenge recieved from the server
*/
int32_t HashNTLM(unsigned char **ntlmhash, unsigned char *pass, unsigned char *challenge, char *miscptr) {
int32_t ret;
unsigned char hash[16]; /* MD4_SIGNATURE_SIZE = 16 */
unsigned char p21[21];
unsigned char ntlm_response[24];
ret = MakeNTLM((unsigned char *)&hash, (unsigned char *)pass);
if (ret == -1)
hydra_child_exit(0);
memset(p21, '\0', 21);
memcpy(p21, hash, 16);
DesEncrypt(challenge, p21 + 0, ntlm_response + 0);
DesEncrypt(challenge, p21 + 7, ntlm_response + 8);
DesEncrypt(challenge, p21 + 14, ntlm_response + 16);
memcpy(*ntlmhash, ntlm_response, 24);
return 0;
}
/*
NBS Session Request
Function: Request a new session from the server
Returns: TRUE on success else FALSE.
*/
int32_t NBSSessionRequest(int32_t s) {
char nb_name[32]; /* netbiosname */
char nb_local[32]; /* netbios localredirector */
unsigned char rqbuf[7] = {0x81, 0x00, 0x00, 0x44, 0x20, 0x00, 0x20};
char *buf;
unsigned char rbuf[400];
int32_t k;
/* if we are running in native mode (aka port 445) don't do netbios */
if (protoFlag == WIN2000_NATIVEMODE)
return 0;
/* convert computer name to netbios name */
memset(nb_name, 0, 32);
memset(nb_local, 0, 32);
memcpy(nb_name, "CKFDENECFDEFFCFGEFFCCACACACACACA", 32); /* *SMBSERVER */
memcpy(nb_local, "EIFJEEFCEBCACACACACACACACACACACA", 32); /* HYDRA */
if ((buf = (char *)malloc(100)) == NULL)
return -1;
memset(buf, 0, 100);
memcpy(buf, (char *)rqbuf, 5);
memcpy(buf + 5, nb_name, 32);
memcpy(buf + 37, (char *)rqbuf + 5, 2);
memcpy(buf + 39, nb_local, 32);
memcpy(buf + 71, (char *)rqbuf + 5, 1);
hydra_send(s, buf, 72, 0);
free(buf);
memset(rbuf, 0, 400);
k = hydra_recv(s, (char *)rbuf, sizeof(rbuf));
if (k > 0 && (rbuf[0] == 0x82))
return 0; /* success */
else
return -1; /* failed */
}
/*
SMBNegProt
Function: Negotiate protocol with server ...
Actually a pseudo negotiation since the whole
program counts on NTLM support :)
The challenge is retrieved from the answer
No error checking is performed i.e cross your fingers....
*/
int32_t SMBNegProt(int32_t s) {
unsigned char buf[] = {
0x00, 0x00, 0x00, 0xbe, 0xff, 0x53, 0x4d, 0x42, 0x72, 0x00, 0x00, 0x00, 0x00, 0x08, 0x01, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x7d, 0x00, 0x00, 0x01, 0x00, 0x00, 0x9b, 0x00, 0x02, 0x50, 0x43, 0x20, 0x4e, 0x45, 0x54, 0x57, 0x4f, 0x52, 0x4b, 0x20, 0x50, 0x52, 0x4f, 0x47, 0x52, 0x41, 0x4d, 0x20, 0x31, 0x2e, 0x30, 0x00, 0x02, 0x4d,
0x49, 0x43, 0x52, 0x4f, 0x53, 0x4f, 0x46, 0x54, 0x20, 0x4e, 0x45, 0x54, 0x57, 0x4f, 0x52, 0x4b, 0x53, 0x20, 0x31, 0x2e, 0x30, 0x33, 0x00, 0x02, 0x4d, 0x49, 0x43, 0x52, 0x4f, 0x53, 0x4f, 0x46, 0x54, 0x20, 0x4e, 0x45, 0x54, 0x57, 0x4f, 0x52, 0x4b, 0x53, 0x20, 0x33, 0x2e, 0x30, 0x00, 0x02, 0x4c, 0x41, 0x4e, 0x4d, 0x41, 0x4e, 0x31, 0x2e, 0x30, 0x00, 0x02, 0x4c, 0x4d, 0x31, 0x2e, 0x32, 0x58,
0x30, 0x30, 0x32, 0x00, 0x02, 0x44, 0x4f, 0x53, 0x20, 0x4c, 0x41, 0x4e, 0x4d, 0x41, 0x4e, 0x32, 0x2e, 0x31, 0x00, 0x02, 0x4c, 0x41, 0x4e, 0x4d, 0x41, 0x4e, 0x32, 0x2e, 0x31, 0x00, 0x02, 0x53, 0x61, 0x6d, 0x62, 0x61, 0x00, 0x02, 0x4e, 0x54, 0x20, 0x4c, 0x41, 0x4e, 0x4d, 0x41, 0x4e, 0x20, 0x31, 0x2e, 0x30, 0x00, 0x02, 0x4e, 0x54, 0x20, 0x4c, 0x4d, 0x20, 0x30, 0x2e, 0x31, 0x32, 0x00
/*
0x02,
0x50, 0x43, 0x20, 0x4e, 0x45, 0x54, 0x57, 0x4f,
0x52, 0x4b, 0x20, 0x50, 0x52, 0x4f, 0x47, 0x52,
0x41, 0x4d, 0x20, 0x31, 0x2e, 0x30, 0x00, 0x02,
0x4d, 0x49, 0x43, 0x52, 0x4f, 0x53, 0x4f, 0x46,
0x54, 0x20, 0x4e, 0x45, 0x54, 0x57, 0x4f, 0x52,
0x4b, 0x53, 0x20, 0x31, 0x2e, 0x30, 0x33, 0x00,
0x02, 0x4d, 0x49, 0x43, 0x52, 0x4f, 0x53, 0x4f,
0x46, 0x54, 0x20, 0x4e, 0x45, 0x54, 0x57, 0x4f,
0x52, 0x4b, 0x53, 0x20, 0x33, 0x2e, 0x30, 0x00,
0x02, 0x4c, 0x41, 0x4e, 0x4d, 0x41, 0x4e, 0x31,
0x2e, 0x30, 0x00, 0x02, 0x4c, 0x4d, 0x31, 0x2e,
0x32, 0x58, 0x30, 0x30, 0x32, 0x00, 0x02, 0x53,
0x61, 0x6d, 0x62, 0x61, 0x00, 0x02, 0x4e, 0x54,
0x20, 0x4c, 0x41, 0x4e, 0x4d, 0x41, 0x4e, 0x20,
0x31, 0x2e, 0x30, 0x00, 0x02, 0x4e, 0x54, 0x20,
0x4c, 0x4d, 0x20, 0x30, 0x2e, 0x31, 0x32, 0x00
*/
};
unsigned char rbuf[400];
unsigned char sess_key[2];
unsigned char userid[2] = {0xCD, 0xEF};
int32_t i = 0, j = 0, k;
int32_t iLength = 194;
int32_t iResponseOffset = 73;
memset((char *)rbuf, 0, 400);
/* set session key */
sess_key[1] = getpid() / 100;
sess_key[0] = getpid() - (100 * sess_key[1]);
memcpy(buf + 30, sess_key, 2);
memcpy(buf + 32, userid, 2);
if (smb_auth_mechanism == AUTH_LM) {
if (verbose)
hydra_report(stderr, "[VERBOSE] Setting Negotiate Protocol Response for LM.\n");
buf[3] = 0xA3; // Set message length
buf[37] = 0x80; // Set byte count for dialects
iLength = 167;
iResponseOffset = 65;
}
hydra_send(s, (char *)buf, iLength, 0);
k = hydra_recv(s, (char *)rbuf, sizeof(rbuf));
if (k == 0)
return 3;
/* retrieve the security mode */
/*
[0] Mode: (0) ? (1) USER security
mode [1] Password: (0) PLAINTEXT password (1) ENCRYPTED
password. Use challenge/response [2] Signatures: (0) Security signatures
NOT enabled (1) ENABLED [3] Sig Req: (0) Security signatures NOT
required (1) REQUIRED
SAMBA: 0x01 (default)
WinXP: 0x0F (default)
WinXP: 0x07 (Windows 2003 / DC)
*/
switch (rbuf[39]) {
case 0x01:
// real plaintext should be used with LM auth
if (verbose)
hydra_report(stderr, "[VERBOSE] Server requested PLAINTEXT password.\n");
security_mode = PLAINTEXT;
if (hashFlag == 1) {
if (verbose)
hydra_report(stderr, "[VERBOSE] Server requested PLAINTEXT password. HASH "
"password mode not supported for this configuration.\n");
return 3;
}
if (hashFlag == 2) {
if (verbose)
hydra_report(stderr, "[VERBOSE] Server requested PLAINTEXT password. MACHINE "
"password mode not supported for this configuration.\n");
return 3;
}
break;
case 0x03:
if (verbose)
hydra_report(stderr, "[VERBOSE] Server requested ENCRYPTED password "
"without security signatures.\n");
security_mode = ENCRYPTED;
break;
case 0x07:
case 0x0F:
if (verbose)
hydra_report(stderr, "[VERBOSE] Server requested ENCRYPTED password.\n");
security_mode = ENCRYPTED;
break;
default:
if (verbose)
hydra_report(stderr,
"[VERBOSE] Unknown security mode request: %2.2X. Proceeding "
"using ENCRYPTED password mode.\n",
rbuf[39]);
security_mode = ENCRYPTED;
break;
}
/* Retrieve the challenge */
memcpy(challenge, (char *)rbuf + iResponseOffset, sizeof(challenge));
/* Find the primary domain/workgroup name */
memset(workgroup, 0, 16);
memset(machine_name, 0, 16);
// seems using LM only the domain is returned not the server
// and the domain is not padded with null chars
if (smb_auth_mechanism == AUTH_LM) {
while ((rbuf[iResponseOffset + 8 + i] != 0) && (i < 16)) {
workgroup[i] = rbuf[iResponseOffset + 8 + i];
i++;
}
} else {
while ((rbuf[iResponseOffset + 8 + i * 2] != 0) && (i < 16)) {
workgroup[i] = rbuf[iResponseOffset + 8 + i * 2];
i++;
}
while ((rbuf[iResponseOffset + 8 + (i + j + 1) * 2] != 0) && (j < 16)) {
machine_name[j] = rbuf[iResponseOffset + 8 + (i + j + 1) * 2];
j++;
}
}
if (verbose) {
hydra_report(stderr, "[VERBOSE] Server machine name: %s\n", machine_name);
hydra_report(stderr, "[VERBOSE] Server primary domain: %s\n", workgroup);
}
// success
return 2;
}
/*
SMBSessionSetup
Function: Send username + response to the challenge from
the server.
Returns: TRUE on success else FALSE.
*/
unsigned long SMBSessionSetup(int32_t s, char *szLogin, char *szPassword, char *miscptr) {
unsigned char buf[512];
unsigned char *LMv2hash = NULL;
unsigned char *NTLMv2hash = NULL;
unsigned char *NTLMhash = NULL;
unsigned char *LMhash = NULL;
// unsigned char unicodeLogin[32 * 2];
int32_t j;
char bufReceive[512];
int32_t nReceiveBufferSize = 0;
int32_t ret;
int32_t iByteCount = 0, iOffset = 0;
if (accntFlag == 0) {
strcpy((char *)workgroup, "localhost");
} else if (accntFlag == 2) {
memset(workgroup, 0, 16);
}
// domain flag is not needed here, it will be auto set,
// below it's domain specified on cmd line
else if (accntFlag == 4) {
strncpy((char *)workgroup, (char *)domain, 16);
}
/* NetBIOS Session Service */
unsigned char szNBSS[4] = {
0x00, /* Message Type: Session Message */
0x00, 0x00, 0x85 /* Length -- MUST SET */
};
/* SMB Header */
unsigned char szSMB[32] = {
0xff,
0x53,
0x4d,
0x42, /* Server Component */
0x73, /* SMB Command: Session Setup AndX */
0x00,
0x00,
0x00,
0x00, /* NT Status: STATUS_SUCCESS */
0x08, /* Flags */
0x01,
0xc0,
/* Flags2 */ /* add Unicode */
0x00,
0x00, /* Process ID High */
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00, /* Signature */
0x00,
0x00, /* Reserved */
0x00,
0x00, /* Tree ID */
0x13,
0x37, /* Process ID */
0x00,
0x00, /* User ID */
0x01,
0x00 /* Multiplx ID */
};
memset(buf, 0, 512);
memcpy(buf, szNBSS, 4);
memcpy(buf + 4, szSMB, 32);
if (security_mode == ENCRYPTED) {
/* Session Setup AndX Request */
if (smb_auth_mechanism == AUTH_LM) {
if (verbose)
hydra_report(stderr, "[VERBOSE] Attempting LM password authentication.\n");
unsigned char szSessionRequest[23] = {
0x0a, /* Word Count */
0xff, /* AndXCommand: No further commands */
0x00, /* Reserved */
0x00, 0x00, /* AndXOffset */
0xff, 0xff, /* Max Buffer */
0x02, 0x00, /* Max Mpx Count */
0x3c, 0x7d, /* VC Number */
0x00, 0x00, 0x00, 0x00, /* Session Key */
0x18, 0x00, /* LAN Manager Password Hash Length */
0x00, 0x00, 0x00, 0x00, /* Reserved */
0x49, 0x00 /* Byte Count -- MUST SET */
};
iOffset = 59; /* szNBSS + szSMB + szSessionRequest */
iByteCount = 24; /* Start with length of LM hash */
/* Set Session Setup AndX Request header information */
memcpy(buf + 36, szSessionRequest, 23);
/* Calculate and set LAN Manager password hash */
if ((LMhash = (unsigned char *)malloc(24)) == NULL)
return -1;
memset(LMhash, 0, 24);
ret = HashLM(&LMhash, (unsigned char *)szPassword, (unsigned char *)challenge);
if (ret == -1) {
free(LMhash);
return -1;
}
memcpy(buf + iOffset, LMhash, 24);
free(LMhash);
} else if (smb_auth_mechanism == AUTH_NTLM) {
if (verbose)
hydra_report(stderr, "[VERBOSE] Attempting NTLM password authentication.\n");
unsigned char szSessionRequest[29] = {
0x0d, /* Word Count */
0xff, /* AndXCommand: No further commands */
0x00, /* Reserved */
0x00,
0x00, /* AndXOffset */
0xff,
0xff, /* Max Buffer */
0x02,
0x00, /* Max Mpx Count */
0x3c,
0x7d, /* VC Number */
0x00,