-
Notifications
You must be signed in to change notification settings - Fork 37
/
cowpatty.c
1161 lines (958 loc) · 31.6 KB
/
cowpatty.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
/*
* coWPAtty - Brute-force dictionary attack against WPA-PSK.
*
* Copyright (c) 2004-2018, Joshua Wright <[email protected]>
*
* This software may be modified and distributed under the terms
* of the BSD-3-clause license. See the LICENSE file for details.
*/
/*
* Significant code is graciously taken from the following:
* wpa_supplicant by Jouni Malinen. This tool would have been MUCH more
* difficult for me if not for this code. Thanks Jouni.
*/
/*
* Right off the bat, this code isn't very useful. The PBKDF2 function makes
* 4096 SHA-1 passes for each passphrase, which takes quite a bit of time. On
* my Pentium II development system, I'm getting ~2.5 passphrases/second.
* I've done my best to optimize the PBKDF2 function, but it's still pretty
* slow.
*/
#define PROGNAME "cowpatty"
#define VER "4.8"
#define MAXPASSPHRASE 256
#define DOT1X_LLCTYPE "\x88\x8e"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pcap.h>
#include <signal.h>
#include <sys/types.h>
#include <fcntl.h>
#include "cowpatty.h"
#include "common.h"
#include "utils.h"
#include "sha1.h"
#include "md5.h"
#include "radiotap.h"
/* Globals */
pcap_t *p = NULL;
unsigned char *packet;
struct pcap_pkthdr *h;
char errbuf[PCAP_ERRBUF_SIZE];
int sig = 0; /* Used for handling signals */
char *words;
/* temporary storage for the password from the hash record, instead of
malloc/free for each entry. */
char password_buf[65];
unsigned long wordstested = 0;
/* Prototypes */
void wpa_pmk_to_ptk(u8 * pmk, u8 * addr1, u8 * addr2,
u8 * nonce1, u8 * nonce2, u8 * ptk, size_t ptk_len);
void hexdump(unsigned char *data, int len);
void usage(char *message);
void testopts(struct user_opt *opt);
void cleanup();
void parseopts(struct user_opt *opt, int argc, char **argv);
void closepcap(struct capture_data *capdata);
void handle_dot1x(struct crack_data *cdata, struct capture_data *capdata,
struct user_opt *opt);
void dump_all_fields(struct crack_data cdata, struct user_opt *opt);
void printstats(struct timeval start, struct timeval end,
unsigned long int wordcount);
int nextdictword(char *word, FILE * fp);
int nexthashrec(FILE * fp, struct hashdb_rec *rec);
void usage(char *message)
{
if (strlen(message) > 0) {
printf("%s: %s\n", PROGNAME, message);
}
printf("\nUsage: %s [options]\n", PROGNAME);
printf("\n"
"\t-f \tDictionary file\n"
"\t-d \tHash file (genpmk)\n"
"\t-r \tPacket capture file\n"
"\t-s \tNetwork SSID (enclose in quotes if SSID includes spaces)\n"
"\t-c \tCheck for valid 4-way frames, does not crack\n"
"\t-h \tPrint this help information and exit\n"
"\t-v \tPrint verbose information (more -v for more verbosity)\n"
"\t-V \tPrint program version and exit\n" "\n");
}
void cleanup()
{
/* lame-o-meter++ */
sig = 1;
}
void wpa_pmk_to_ptk(u8 * pmk, u8 * addr1, u8 * addr2,
u8 * nonce1, u8 * nonce2, u8 * ptk, size_t ptk_len)
{
u8 data[2 * ETH_ALEN + 2 * 32];
memset(&data, 0, sizeof(data));
/* PTK = PRF-X(PMK, "Pairwise key expansion",
* Min(AA, SA) || Max(AA, SA) ||
* Min(ANonce, SNonce) || Max(ANonce, SNonce)) */
if (memcmp(addr1, addr2, ETH_ALEN) < 0) {
memcpy(data, addr1, ETH_ALEN);
memcpy(data + ETH_ALEN, addr2, ETH_ALEN);
} else {
memcpy(data, addr2, ETH_ALEN);
memcpy(data + ETH_ALEN, addr1, ETH_ALEN);
}
if (memcmp(nonce1, nonce2, 32) < 0) {
memcpy(data + 2 * ETH_ALEN, nonce1, 32);
memcpy(data + 2 * ETH_ALEN + 32, nonce2, 32);
} else {
memcpy(data + 2 * ETH_ALEN, nonce2, 32);
memcpy(data + 2 * ETH_ALEN + 32, nonce1, 32);
}
sha1_prf(pmk, 32, "Pairwise key expansion", data, sizeof(data),
ptk, ptk_len);
}
void hexdump(unsigned char *data, int len)
{
int i;
for (i = 0; i < len; i++) {
printf("%02x ", data[i]);
}
}
void parseopts(struct user_opt *opt, int argc, char **argv)
{
int c;
while ((c = getopt(argc, argv, "f:r:s:d:cnhvV")) != EOF) {
switch (c) {
case 'f':
strncpy(opt->dictfile, optarg, sizeof(opt->dictfile));
break;
case 'r':
strncpy(opt->pcapfile, optarg, sizeof(opt->pcapfile));
break;
case 's':
strncpy(opt->ssid, optarg, sizeof(opt->ssid));
break;
case 'd':
strncpy(opt->hashfile, optarg, sizeof(opt->hashfile));
break;
case 'n':
case 'c':
opt->checkonly++;
break;
case 'h':
usage("");
exit(0);
break;
case 'v':
opt->verbose++;
break;
case 'V':
printf
("$Id: cowpatty.c 264 2009-07-03 15:15:50Z jwright $\n");
exit(0);
break;
default:
usage("");
exit(-1);
}
}
}
void testopts(struct user_opt *opt)
{
struct stat teststat;
/* Test for a pcap file */
if (IsBlank(opt->pcapfile)) {
usage("Must supply a pcap file with -r");
exit(-1);
}
if (opt->checkonly == 1) {
/* Special case where we only need pcap file */
return;
}
/* test for required parameters */
if (IsBlank(opt->dictfile) && IsBlank(opt->hashfile)) {
usage("Must supply a list of passphrases in a file with -f "
"or a hash file\n\t with -d. "
"Use \"-f -\" to accept words on stdin.");
exit(-1);
}
if (IsBlank(opt->ssid)) {
usage("Must supply the SSID for the network with -s");
exit(-1);
}
/* Test that the files specified exist and are greater than 0 bytes */
if (!IsBlank(opt->hashfile) && strncmp(opt->hashfile, "-", 1) != 0) {
if (stat(opt->hashfile, &teststat)) {
usage("Could not stat hashfile. Check file path.");
exit(-1);
} else if (teststat.st_size == 0) {
usage("Empty hashfile (0 bytes). Check file contents.");
exit(-1);
}
}
if (!IsBlank(opt->dictfile) && strncmp(opt->dictfile, "-", 1) != 0) {
if (stat(opt->dictfile, &teststat)) {
usage
("Could not stat the dictionary file. Check file path.");
exit(-1);
} else if (teststat.st_size == 0) {
usage
("Empty dictionary file (0 bytes). Check file contents.");
exit(-1);
}
}
if (stat(opt->pcapfile, &teststat) && strncmp(opt->hashfile, "-", 1) != 0) {
usage("Could not stat the pcap file. Check file path.");
exit(-1);
} else if (teststat.st_size == 0) {
usage("Empty pcap file (0 bytes). Check file contents.");
exit(-1);
}
}
int openpcap(struct capture_data *capdata)
{
/* Assume for now it's a libpcap file */
p = pcap_open_offline(capdata->pcapfilename, errbuf);
if (p == NULL) {
perror("Unable to open capture file");
return (-1);
}
/* Determine link type */
capdata->pcaptype = pcap_datalink(p);
/* Determine offset to EAP frame based on link type */
switch (capdata->pcaptype) {
case DLT_NULL:
case DLT_EN10MB:
case DLT_IEEE802_11:
case DLT_PRISM_HEADER:
case DLT_IEEE802_11_RADIO:
case DLT_PPI:
break;
default:
/* Unknown/unsupported pcap type */
return (1);
}
return (0);
}
void closepcap(struct capture_data *capdata)
{
/* Assume it's a libpcap file for now */
pcap_close(p);
}
/* Populates global *packet, returns status */
int getpacket(struct capture_data *capdata)
{
/* Assume it's a libpcap file for now */
int ret;
struct ieee80211_radiotap_header *rtaphdr;
struct ieee80211_radiotap_header *ppihdr;
int rtaphdrlen=0;
int ppihdrlen=0;
struct dot11hdr *dot11 = NULL;
/* Loop on pcap_next_ex until we get a packet we want, return from
* this while loop. This is kinda hack.
*/
while ((ret = pcap_next_ex(p, &h, (const u_char **)&packet))
&& ret > 0) {
/* Determine offset to EAP frame based on link type */
switch (capdata->pcaptype) {
case DLT_NULL:
case DLT_EN10MB:
/* Standard ethernet header */
capdata->dot1x_offset = 14;
capdata->l2type_offset = 12;
capdata->dstmac_offset = 0;
capdata->srcmac_offset = 6;
return(ret);
break;
case DLT_IEEE802_11:
/* If this is not a data packet, get the next frame */
dot11 = (struct dot11hdr *)packet;
if (dot11->u1.fc.type != DOT11_FC_TYPE_DATA) {
continue;
}
capdata->dstmac_offset = 4;
capdata->srcmac_offset = 10;
/* differentiate QoS data and non-QoS data frames */
if (dot11->u1.fc.subtype == DOT11_FC_SUBTYPE_QOSDATA) {
/* 26 bytes 802.11 header, 8 for 802.2 header */
capdata->dot1x_offset = 34;
capdata->l2type_offset = 32;
} else if (dot11->u1.fc.subtype == DOT11_FC_SUBTYPE_DATA) {
/* 24 bytes 802.11 header, 8 for 802.2 header */
capdata->dot1x_offset = 32;
capdata->l2type_offset = 30;
} else {
/* Not a data frame we support */
continue;
}
return(ret);
break;
case DLT_PRISM_HEADER:
/* 802.11 frames with AVS header, AVS header is 144
* bytes */
/* If this is not a data packet, get the next frame */
dot11 = ((struct dot11hdr *)(packet+144));
if (dot11->u1.fc.type != DOT11_FC_TYPE_DATA) {
continue;
}
capdata->dstmac_offset = 4 + 144;
capdata->srcmac_offset = 10 + 144;
/* differentiate QoS data and non-QoS data frames */
if (dot11->u1.fc.subtype == DOT11_FC_SUBTYPE_QOSDATA) {
capdata->dot1x_offset = 34 + 144;
capdata->l2type_offset = 32 + 144;
} else if (dot11->u1.fc.subtype == DOT11_FC_SUBTYPE_DATA) {
capdata->dot1x_offset = 32 + 144;
capdata->l2type_offset = 30 + 144;
} else {
/* Not a data frame we support */
continue;
}
return(ret);
break;
case DLT_IEEE802_11_RADIO:
/* Radiotap header, need to calculate offset to payload
on a per-packet basis.
*/
rtaphdr = (struct ieee80211_radiotap_header *)packet;
/* rtap is LE */
rtaphdrlen = le16_to_cpu(rtaphdr->it_len);
/* Sanity check on header length, 10 bytes is min
802.11 len */
if (rtaphdrlen > (h->len - 10)) {
return -2; /* Bad radiotap data */
}
capdata->dstmac_offset = 4 + rtaphdrlen;
capdata->srcmac_offset = 10 + rtaphdrlen;
dot11 = ((struct dot11hdr *)(packet+rtaphdrlen));
/* differentiate QoS data and non-QoS data frames */
if (dot11->u1.fc.subtype == DOT11_FC_SUBTYPE_QOSDATA) {
capdata->dot1x_offset = 34 + rtaphdrlen;
capdata->l2type_offset = 32 + rtaphdrlen;
} else if (dot11->u1.fc.subtype ==
DOT11_FC_SUBTYPE_DATA) {
capdata->dot1x_offset = 32 + rtaphdrlen;
capdata->l2type_offset = 30 + rtaphdrlen;
} else {
/* Not a data frame we support */
continue;
}
return(ret);
break;
case DLT_PPI:
ppihdr = (struct ieee80211_radiotap_header *)packet;
ppihdrlen = le16_to_cpu(ppihdr->it_len);
if (ppihdrlen > (h->len - 10)) {
return -2;
}
if (ppihdrlen == 24)
ppihdrlen = 32;
capdata->dstmac_offset = 4 + ppihdrlen;
capdata->srcmac_offset = 10 + ppihdrlen;
dot11 = ((struct dot11hdr *)(packet+ppihdrlen));
/* differentiate QoS data and non-QoS data frames */
if (dot11->u1.fc.subtype == DOT11_FC_SUBTYPE_QOSDATA) {
capdata->dot1x_offset = 34 + ppihdrlen;
capdata->l2type_offset = 32 + ppihdrlen;
} else if (dot11->u1.fc.subtype ==
DOT11_FC_SUBTYPE_DATA) {
capdata->dot1x_offset = 32 + ppihdrlen;
capdata->l2type_offset = 30 + ppihdrlen;
} else {
/* Not a data frame we support */
continue;
}
return(ret);
break;
default:
/* Unknown/unsupported pcap type */
return (1);
}
}
return (ret);
}
void handle_dot1x(struct crack_data *cdata, struct capture_data *capdata,
struct user_opt *opt)
{
struct ieee8021x *dot1xhdr;
struct wpa_eapol_key *eapolkeyhdr;
int eapolkeyoffset;
int key_info, index;
/* We're going after the last three frames in the 4-way handshake.
In the last frame of the TKIP exchange, the authenticator nonce is
omitted. In cases where there is a unicast and a multicast key
distributed, frame 4 will include the authenticator nonce. In some
cases however, there is no multicast key distribution, so frame 4 has
no authenticator nonce. For this reason, we need to capture information
from the 2nd, 3rd and 4th frames to accommodate cases where there is no
multicast key delivery. Suckage.
*/
dot1xhdr = (struct ieee8021x *)&packet[capdata->dot1x_offset];
eapolkeyoffset = capdata->dot1x_offset + sizeof(struct ieee8021x);
eapolkeyhdr = (struct wpa_eapol_key *)&packet[eapolkeyoffset];
/* Bitwise fields in the key_info field of the EAPOL-Key header */
key_info = be_to_host16(eapolkeyhdr->key_info);
cdata->ver = key_info & WPA_KEY_INFO_TYPE_MASK;
index = key_info & WPA_KEY_INFO_KEY_INDEX_MASK;
/* Check for type EAPOL-Key */
if (dot1xhdr->type != 3) {
return;
}
if (cdata->ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
cdata->ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
return;
}
if (cdata->ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
/* Check for WPA key, and pairwise key type */
if ((eapolkeyhdr->type != 2 && eapolkeyhdr->type != 254) ||
(key_info & WPA_KEY_INFO_KEY_TYPE) == 0) {
return;
}
} else if (cdata->ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
if ((eapolkeyhdr->type != 2 && eapolkeyhdr->type != 254) ||
(key_info & WPA_KEY_INFO_KEY_TYPE) == 0) {
return;
}
}
if (opt->nonstrict == 0) {
/* Check for frame 2 of the 4-way handshake */
if ((key_info & WPA_KEY_INFO_MIC)
&& (key_info & WPA_KEY_INFO_ACK) == 0
&& (key_info & WPA_KEY_INFO_INSTALL) == 0
&& eapolkeyhdr->key_data_length > 0) {
/* All we need from this frame is the authenticator nonce */
memcpy(cdata->snonce, eapolkeyhdr->key_nonce,
sizeof(cdata->snonce));
cdata->snonceset = 1;
memcpy(cdata->replay_counter1,
eapolkeyhdr->replay_counter, 8);
cdata->replay_counter1[7] = cdata->replay_counter1[7] + 1;
/* Check for frame 3 of the 4-way handshake */
} else if ((key_info & WPA_KEY_INFO_MIC)
&& (key_info & WPA_KEY_INFO_INSTALL)
&& (key_info & WPA_KEY_INFO_ACK)) {
memcpy(cdata->spa, &packet[capdata->dstmac_offset],
sizeof(cdata->spa));
memcpy(cdata->aa, &packet[capdata->srcmac_offset],
sizeof(cdata->aa));
memcpy(cdata->anonce, eapolkeyhdr->key_nonce,
sizeof(cdata->anonce));
cdata->aaset = 1;
cdata->spaset = 1;
cdata->anonceset = 1;
/* We save the replay counter value in the 3rd frame to match
against the 4th frame of the four-way handshake */
memcpy(cdata->replay_counter2,
eapolkeyhdr->replay_counter, 8);
/* Check for frame 4 of the four-way handshake */
} else if ((key_info & WPA_KEY_INFO_MIC)
&& (key_info & WPA_KEY_INFO_ACK) == 0
&& (key_info & WPA_KEY_INFO_INSTALL) == 0
&& (memcmp (cdata->replay_counter1,
cdata->replay_counter2, 8) == 0)
&& (memcmp (cdata->replay_counter2,
eapolkeyhdr->replay_counter, 8) == 0)) {
memcpy(cdata->keymic, eapolkeyhdr->key_mic,
sizeof(cdata->keymic));
memcpy(cdata->eapolframe, &packet[capdata->dot1x_offset],
sizeof(cdata->eapolframe));
cdata->keymicset = 1;
cdata->eapolframeset = 1;
cdata->counters = 1;
}
} else {
/* Check for frame 1 of the 4-way handshake */
if ((key_info & WPA_KEY_INFO_MIC) == 0
&& (key_info & WPA_KEY_INFO_ACK)
&& (key_info & WPA_KEY_INFO_INSTALL) == 0 ) {
/* All we need from this frame is the authenticator nonce */
memcpy(cdata->anonce, eapolkeyhdr->key_nonce,
sizeof(cdata->anonce));
cdata->anonceset = 1;
memcpy(cdata->replay_counter1,
eapolkeyhdr->replay_counter, 8);
cdata->replay_counter1[7] = cdata->replay_counter1[7] + 1;
/* Check for frame 2 or 4 of the 4-way handshake */
} else if ((key_info & WPA_KEY_INFO_MIC)
&& (key_info & WPA_KEY_INFO_INSTALL) == 0
&& (key_info & WPA_KEY_INFO_ACK) == 0) {
cdata->eapolframe_size = ( packet[capdata->dot1x_offset + 2] << 8 )
+ packet[capdata->dot1x_offset + 3] + 4;
memcpy(cdata->spa, &packet[capdata->dstmac_offset],
sizeof(cdata->spa));
cdata->spaset = 1;
memcpy(cdata->aa, &packet[capdata->srcmac_offset],
sizeof(cdata->aa));
cdata->aaset = 1;
memcpy(cdata->snonce, eapolkeyhdr->key_nonce,
sizeof(cdata->snonce));
cdata->snonceset = 1;
memcpy(cdata->keymic, eapolkeyhdr->key_mic,
sizeof(cdata->keymic));
cdata->keymicset = 1;
memcpy(cdata->eapolframe, &packet[capdata->dot1x_offset],
cdata->eapolframe_size);
cdata->eapolframeset = 1;
memcpy(cdata->replay_counter2,
eapolkeyhdr->replay_counter, 8);
cdata->replay_counter2[7] = cdata->replay_counter2[7] + 1;
memcpy(cdata->replay_counter3,
eapolkeyhdr->replay_counter, 8);
cdata->replay_counter3[7] = cdata->replay_counter3[7] + 2;
/* Check for frame 3 of the 4-way handshake */
} else if ((key_info & WPA_KEY_INFO_MIC)
&& (key_info & WPA_KEY_INFO_ACK)
&& (key_info & WPA_KEY_INFO_INSTALL)) {
/* All we need from this frame is the authenticator nonce */
memcpy(cdata->anonce, eapolkeyhdr->key_nonce,
sizeof(cdata->anonce));
cdata->anonceset = 1;
memcpy(cdata->replay_counter4,
eapolkeyhdr->replay_counter, 8);
cdata->replay_counter4[7] = cdata->replay_counter4[7] + 1;
}
}
}
void dump_all_fields(struct crack_data cdata, struct user_opt *opt)
{
printf("AA is:");
lamont_hdump(cdata.aa, 6);
printf("\n");
printf("SPA is:");
lamont_hdump(cdata.spa, 6);
printf("\n");
printf("snonce is:");
lamont_hdump(cdata.snonce, 32);
printf("\n");
printf("anonce is:");
lamont_hdump(cdata.anonce, 32);
printf("\n");
printf("keymic is:");
lamont_hdump(cdata.keymic, 16);
printf("\n");
printf("eapolframe is:");
if (opt->nonstrict == 0) {
lamont_hdump(cdata.eapolframe, 99);
} else {
lamont_hdump(cdata.eapolframe, 125);
}
printf("\n");
}
void printstats(struct timeval start, struct timeval end,
unsigned long int wordcount)
{
float elapsed = 0;
if (end.tv_usec < start.tv_usec) {
end.tv_sec -= 1;
end.tv_usec += 1000000;
}
end.tv_sec -= start.tv_sec;
end.tv_usec -= start.tv_usec;
elapsed = end.tv_sec + end.tv_usec / 1000000.0;
printf("\n%lu passphrases tested in %.2f seconds: %.2f passphrases/"
"second\n", wordcount, elapsed, wordcount / elapsed);
}
int nexthashrec(FILE * fp, struct hashdb_rec *rec)
{
int recordlength, wordlen;
if (fread(&rec->rec_size, sizeof(rec->rec_size), 1, fp) != 1) {
perror("fread");
return -1;
}
recordlength = rec->rec_size;
wordlen = recordlength - (sizeof(rec->pmk) + sizeof(rec->rec_size));
if (wordlen > 63 || wordlen < 8) {
fprintf(stderr, "Invalid word length: %d\n", wordlen);
return -1;
}
/* hackity, hack, hack, hack */
rec->word = password_buf;
if (fread(rec->word, wordlen, 1, fp) != 1) {
perror("fread");
return -1;
}
if (fread(rec->pmk, sizeof(rec->pmk), 1, fp) != 1) {
perror("fread");
return -1;
}
return recordlength;
}
int nextdictword(char *word, FILE * fp)
{
if (fgets(word, MAXPASSLEN + 1, fp) == NULL) {
return (-1);
}
/* Remove newline */
word[strlen(word) - 1] = '\0';
if (feof(fp)) {
return (-1);
}
return (strlen(word));
}
void hmac_hash(int ver, u8 *key, int hashlen, u8 *buf, int buflen, u8 *mic)
{
u8 hash[SHA1_MAC_LEN];
if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
hmac_md5(key, hashlen, buf, buflen, mic);
} else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
hmac_sha1(key, hashlen, buf, buflen, hash, NOCACHED);
memcpy(mic, hash, MD5_DIGEST_LENGTH); /* only 16 bytes, not 20 */
}
}
int hashfile_attack(struct user_opt *opt, char *passphrase,
struct crack_data *cdata)
{
FILE *fp;
int reclen, wordlen;
u8 pmk[32];
u8 ptk[64];
u8 keymic[16];
struct wpa_ptk *ptkset;
struct hashdb_rec rec;
struct hashdb_head hf_head;
char headerssid[33];
/* Open the hash file */
if (*opt->hashfile == '-') {
printf("Using STDIN for hashfile contents.\n");
fp = stdin;
} else {
fp = fopen(opt->hashfile, "rb");
if (fp == NULL) {
perror("fopen");
return(-1);
}
}
/* Read the record header contents */
if (fread(&hf_head, sizeof(hf_head), 1, fp) != 1) {
perror("fread");
return(-1);
}
/* Ensure selected SSID matches what's stored in the header record */
if (memcmp(hf_head.ssid, opt->ssid, hf_head.ssidlen) != 0) {
memcpy(&headerssid, hf_head.ssid, hf_head.ssidlen);
headerssid[hf_head.ssidlen] = 0; /* NULL terminate string */
fprintf(stderr, "\nSSID in hashfile (\"%s\") does not match "
"SSID specified on the \n"
"command line (\"%s\"). You cannot "
"mix and match SSID's for this\nattack.\n\n",
headerssid, opt->ssid);
return(-1);
}
while (feof(fp) == 0 && sig == 0) {
/* Populate the hashdb_rec with the next record */
reclen = nexthashrec(fp, &rec);
/* nexthashrec returns the length of the record, test to ensure
passphrase is greater than 8 characters */
wordlen = rec.rec_size -
(sizeof(rec.pmk) + sizeof(rec.rec_size));
if (wordlen < 8) {
printf("Found a record that was too short, this "
"shouldn't happen in practice!\n");
return(-1);
}
/* Populate passphrase with the record contents */
memcpy(passphrase, rec.word, wordlen);
/* NULL terminate passphrase string */
passphrase[wordlen] = 0;
if (opt->verbose > 1) {
printf("Testing passphrase: %s\n", passphrase);
}
/* Increment the words tested counter */
wordstested++;
/* Status display */
if ((wordstested % 10000) == 0) {
printf("key no. %ld: %s\n", wordstested, passphrase);
fflush(stdout);
}
if (opt->verbose > 1) {
printf("Calculating PTK for \"%s\".\n", passphrase);
}
if (opt->verbose > 2) {
printf("PMK is");
lamont_hdump(pmk, sizeof(pmk));
}
if (opt->verbose > 1) {
printf("Calculating PTK with collected data and "
"PMK.\n");
}
wpa_pmk_to_ptk(rec.pmk, cdata->aa, cdata->spa, cdata->anonce,
cdata->snonce, ptk, sizeof(ptk));
if (opt->verbose > 2) {
printf("Calculated PTK for \"%s\" is", passphrase);
lamont_hdump(ptk, sizeof(ptk));
}
ptkset = (struct wpa_ptk *)ptk;
if (opt->verbose > 1) {
printf("Calculating hmac-MD5 Key MIC for this "
"frame.\n");
}
if (opt->nonstrict == 0) {
hmac_hash(cdata->ver, ptkset->mic_key, 16, cdata->eapolframe,
sizeof(cdata->eapolframe), keymic);
} else {
hmac_hash(cdata->ver, ptkset->mic_key, 16, cdata->eapolframe,
cdata->eapolframe_size, keymic);
}
if (opt->verbose > 2) {
printf("Calculated MIC with \"%s\" is", passphrase);
lamont_hdump(keymic, sizeof(keymic));
}
if (memcmp(&cdata->keymic, &keymic, sizeof(keymic)) == 0) {
return 0;
} else {
continue;
}
}
return 1;
}
int dictfile_attack(struct user_opt *opt, char *passphrase,
struct crack_data *cdata)
{
FILE *fp;
int fret;
u8 pmk[32];
u8 ptk[64];
u8 keymic[16];
struct wpa_ptk *ptkset;
/* Open the dictionary file */
if (*opt->dictfile == '-') {
printf("Using STDIN for words.\n");
fp = stdin;
} else {
fp = fopen(opt->dictfile, "r");
if (fp == NULL) {
perror("fopen");
exit(-1);
}
}
while (feof(fp) == 0 && sig == 0) {
/* Populate "passphrase" with the next word */
fret = nextdictword(passphrase, fp);
if (fret < 0) {
break;
}
if (opt->verbose > 1) {
printf("Testing passphrase: %s\n", passphrase);
}
/*
* Test length of word. IEEE 802.11i indicates the passphrase
* must be at least 8 characters in length, and no more than 63
* characters in length.
*/
if (fret < 8 || fret > 63) {
if (opt->verbose) {
printf("Invalid passphrase length: %s (%zd).\n",
passphrase, strlen(passphrase));
}
continue;
} else {
/* This word is good, increment the words tested
counter */
wordstested++;
}
/* Status display */
if ((wordstested % 1000) == 0) {
printf("key no. %ld: %s\n", wordstested, passphrase);
fflush(stdout);
}
if (opt->verbose > 1) {
printf("Calculating PMK for \"%s\".\n", passphrase);
}
pbkdf2_sha1(passphrase, opt->ssid, strlen(opt->ssid), 4096,
pmk, sizeof(pmk), USECACHED);
if (opt->verbose > 2) {
printf("PMK is");
lamont_hdump(pmk, sizeof(pmk));
}
if (opt->verbose > 1) {
printf("Calculating PTK with collected data and "
"PMK.\n");
}
wpa_pmk_to_ptk(pmk, cdata->aa, cdata->spa, cdata->anonce,
cdata->snonce, ptk, sizeof(ptk));
if (opt->verbose > 2) {
printf("Calculated PTK for \"%s\" is", passphrase);
lamont_hdump(ptk, sizeof(ptk));
}
ptkset = (struct wpa_ptk *)ptk;
if (opt->verbose > 1) {
printf("Calculating hmac-MD5 Key MIC for this "
"frame.\n");
}
if (opt->nonstrict == 0) {
hmac_hash(cdata->ver, ptkset->mic_key, 16, cdata->eapolframe,
sizeof(cdata->eapolframe), keymic);
} else {
hmac_hash(cdata->ver, ptkset->mic_key, 16, cdata->eapolframe,
cdata->eapolframe_size, keymic);
}
if (opt->verbose > 2) {
printf("Calculated MIC with \"%s\" is", passphrase);
lamont_hdump(keymic, sizeof(keymic));
}
if (memcmp(&cdata->keymic, &keymic, sizeof(keymic)) == 0) {
return 0;
} else {
continue;
}
}
return 1;
}
int main(int argc, char **argv)
{
struct user_opt opt;
struct crack_data cdata;
struct capture_data capdata;
struct wpa_eapol_key *eapkeypacket;
u8 eapolkey_nomic[99];
struct timeval start, end;
int ret;
char passphrase[MAXPASSLEN + 1];
printf("%s %s - WPA-PSK dictionary attack. <[email protected]>\n",
PROGNAME, VER);
memset(&opt, 0, sizeof(struct user_opt));
memset(&capdata, 0, sizeof(struct capture_data));
memset(&cdata, 0, sizeof(struct crack_data));
memset(&eapolkey_nomic, 0, sizeof(eapolkey_nomic));
/* Collect and test command-line arguments */
parseopts(&opt, argc, argv);
testopts(&opt);
printf("\n");
/* Populate capdata struct */
strncpy(capdata.pcapfilename, opt.pcapfile,
sizeof(capdata.pcapfilename));
if (openpcap(&capdata) != 0) {
printf("Unsupported or unrecognized pcap file.\n");
exit(-1);
}