-
Notifications
You must be signed in to change notification settings - Fork 23
/
airmon-ng
executable file
·1441 lines (1336 loc) · 44.2 KB
/
airmon-ng
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
#!/bin/sh
DEBUG="0"
VERBOSE="0"
ELITE="0"
USERID=""
IFACE=""
checkvm_status=""
MAC80211=0
IW_SOURCE="https://www.kernel.org/pub/software/network/iw/iw-4.3.tar.gz"
IW_ERROR=""
if [ ! -d /sys/ ]; then
printf "CONFIG_SYSFS is disabled in your kernel, this program will almost certainly not work.\n"
fi
if [ "${1}" = "--elite" ]; then
shift
ELITE="1"
fi
if [ "${1}" = "--verbose" ]; then
shift
VERBOSE="1"
fi
if [ "${1}" = "--debug" ]; then
shift
DEBUG="1"
VERBOSE="1"
fi
#yes, I know this is in here twice
if [ "${1}" = "--elite" ]; then
shift
ELITE="1"
fi
if [ -n "${3}" ];then
if [ "${3}" -gt 0 ] > /dev/null 2>&1; then
CH="${3}"
else
printf "\nYou have entered an invalid channel \"${3}\" which will be ignored\n"
CH=3
fi
else
CH=10
fi
#TODO LIST
#cleanup getDriver()
#fix to not assume wifi drivers are modules
#rewrite to not have two devices at any one time
if [ -n "$(command -v id 2> /dev/null)" ]; then
USERID="$(id -u 2> /dev/null)"
fi
if [ -z "${USERID}" ] && [ -n "$(id -ru)" ]; then
USERID="$(id -ru)"
fi
if [ -n "${USERID}" ] && [ "${USERID}" != "0" ]; then
printf "Run it as root\n" ; exit 1;
elif [ -z "${USERID}" ]; then
printf "Unable to determine user id, permission errors may occur.\n"
fi
#check for all needed binaries
if [ ! -x "$(command -v uname 2>&1)" ]; then
printf "How in the world do you not have uname installed?\n"
printf "Please select a linux distro which has at least basic functionality (or install uname).\n"
exit 1
else
OS=$(uname -s)
#Recognized values are Linux and Darwin
fi
if [ ! -x "$(command -v ip 2>&1)" ] && [ ! -x "$(command -v ifconfig 2>&1)" ]; then
printf "You have neither ip (iproute2) nor ifconfig installed.\n"
printf "Please install one of them from your distro's package manager.\n"
exit 1
fi
if [ ! -x "$(command -v iw 2>&1)" ]; then
printf "You don't have iw installed, please install it from your distro's package manager.\n"
printf "If your distro doesn't have a recent version you can download it from this link:\n"
printf "${IW_SOURCE}\n"
exit 1
fi
if [ ! -x "$(command -v ethtool 2>&1)" ]; then
printf "Please install the ethtool package for your distro.\n"
exit 1
fi
if [ -d /sys/bus/usb ]; then
if [ ! -x "$(command -v lsusb 2>&1)" ]; then
printf "Please install lsusb from your distro's package manager.\n"
exit 1
else
LSUSB=1
fi
else
LSUSB=0
fi
if [ -d /sys/bus/pci ] || [ -d /sys/bus/pci_express ] || [ -d /proc/bus/pci ]; then
if [ ! -x "$(command -v lspci 2>&1)" ]; then
printf "Please install lspci from your distro's package manager.\n"
exit 1
else
LSPCI=1
fi
else
LSPCI=0
fi
if [ -f /proc/modules ] || [ -d /sys/module ]; then
if [ ! -x "$(command -v modprobe 2>&1)" ]; then
printf "Your kernel has module support but you don't have modprobe installed.\n"
printf "It is highly recommended to install modprobe (typically from kmod).\n"
MODPROBE=0
else
MODPROBE=1
fi
if [ ! -x "$(command -v modinfo 2>&1)" ]; then
printf "Your kernel has module support but you don't have modinfo installed.\n"
printf "It is highly recommended to install modinfo (typically from kmod).\n"
printf "Warning: driver detection without modinfo may yield inaccurate results.\n"
MODINFO=0
else
MODINFO=1
fi
else
MODINFO=0
fi
if [ -c /dev/rfkill ]; then
if [ ! -x "$(command -v rfkill 2>&1)" ];then
printf "Your kernel supports rfkill but you don't have rfkill installed.\n"
printf "To ensure devices are unblocked you must install rfkill.\n"
RFKILL=0
else
RFKILL=1
fi
else
RFKILL=0
fi
if [ ! -x "$(command -v awk 2>&1)" ]; then
printf "How in the world do you not have awk installed?\n"
printf "Please select a linux distro which has at least basic functionality (or install awk).\n"
exit 1
fi
if [ ! -x "$(command -v grep 2>&1)" ]; then
printf "How in the world do you not have grep installed?\n"
printf "Please select a linux distro which has at least basic functionality (or install grep).\n"
exit 1
fi
#done checking for binaries
usage() {
printf "usage: $(basename $0) <start|stop|check> <interface> [channel or frequency]\n\n"
exit 0
}
handleLostPhys() {
MISSING_INTERFACE=""
for i in $(ls /sys/class/ieee80211/); do
if [ ! -d /sys/class/ieee80211/${i}/device/net ]; then
MISSING_INTERFACE="${i}"
printf "\nFound ${MISSING_INTERFACE} with no interfaces assigned, would you like to assign one to it? [y/n] "
yesorno
retcode=$?
if [ "${retcode}" = "1" ]; then
printf "PHY ${MISSING_INTERFACE} will remain lost.\n"
elif [ "${retcode}" = "0" ]; then
PHYDEV=${MISSING_INTERFACE}
findFreeInterface monitor
fi
fi
done
#add some spacing so this doesn't make the display hard to read
printf "\n"
}
findFreeInterface() {
if [ -z "${1}" ]; then
printf "findFreeInterface needs a target mode.\n"
exit 1
fi
if [ "${1}" != "monitor" ] && [ "${1}" != "station" ]; then
printf "findFreeInterface only supports monitor and station for target mode.\n"
exit 1
fi
target_mode="${1}"
if [ "$target_mode" = "monitor" ]; then
target_suffix="mon"
target_type="803"
else
target_suffix=""
target_type="1"
fi
for i in $(seq 0 100); do
if [ "$i" = "100" ]; then
printf "\n\tUnable to find a free name between wlan0 and wlan99, you are on your own from here.\n"
return 1
fi
if [ "$DEBUG" = "1" ]; then
printf "\nChecking candidate wlan${i}\n"
fi
if [ ! -e /sys/class/net/wlan${i} ]; then
if [ "$DEBUG" = "1" ]; then
printf "\nCandidate wlan${i} is not in use\n"
fi
if [ ! -e /sys/class/net/wlan${i}mon ]; then
if [ "$DEBUG" = "1" ]; then
printf "\nCandidate wlan${i} and wlan${i}mon are both clear, creating wlan${i}${target_suffix}\n"
fi
IW_ERROR="$(iw phy ${PHYDEV} interface add wlan${i}${target_suffix} type ${target_mode} 2>&1)"
if [ -z "${IW_ERROR}" ]; then
if [ -d /sys/class/ieee80211/${PHYDEV}/device/net ]; then
for j in $(ls /sys/class/ieee80211/${PHYDEV}/device/net/); do
if [ "$(cat /sys/class/ieee80211/${PHYDEV}/device/net/${j}/type)" = "${target_type}" ]; then
#here is where we catch udev renaming our interface
k=${j#wlan}
i=${k%mon}
fi
done
else
printf "Unable to create wlan${i}${target_suffix} and no error recieved.\n"
return 1
fi
printf "\n\t\t(mac80211 ${target_mode} mode vif enabled on [${PHYDEV}]wlan${i}${target_suffix}\n"
unset IW_ERROR
break
else
printf "\n\n ERROR adding ${target_mode} mode interface: ${IW_ERROR}\n"
break
fi
else
if [ "$DEBUG" = "1" ]; then
printf "\nCandidate wlan${i} does not exist, but wlan${i}mon does, skipping...\n"
fi
fi
else
if [ "$DEBUG" = "1" ]; then
printf "\nCandidate wlan${i} is in use already.\n"
fi
fi
done
}
rfkill_check() {
#take phy and check blocks
if [ "${RFKILL}" = 0 ]; then
#immediatly return if rfkill isn't supported
return 0
fi
if [ -z "${1}" ]; then
printf "Fatal, rfkill_check requires a phy to be passed in\n"
exit 1
fi
#first we have to find the rfkill index
#this is available as /sys/class/net/wlan0/phy80211/rfkill## but that's a bit difficult to parse
index="$(rfkill list | grep ${1} | awk -F: '{print $1}')"
if [ -z "$index" ]; then
return 187
fi
rfkill_status="$(rfkill list ${index} 2>&1)"
if [ $? != 0 ]; then
printf "rfkill error: ${rfkill_status}\n"
return 187
elif [ -z "${rfkill_status}" ]; then
printf "rfkill had no output, something went wrong.\n"
exit 1
else
soft=$(printf "${rfkill_status}" | grep -i soft | awk '{print $3}')
hard=$(printf "${rfkill_status}" | grep -i hard | awk '{print $3}')
if [ "${soft}" = "yes" ] && [ "${hard}" = "no" ]; then
return 1
elif [ "${soft}" = "no" ] && [ "${hard}" = "yes" ]; then
return 2
elif [ "${soft}" = "yes" ] && [ "${hard}" = "yes" ]; then
return 3
fi
fi
return 0
}
rfkill_unblock() {
#attempt unblock and CHECK SUCCESS
if [ "${RFKILL}" = 0 ]; then
#immediatly return if rfkill isn't supported
return 0
fi
rfkill_status="$(rfkill unblock ${1#phy} 2>&1)"
if [ $? != 0 ]; then
printf "rfkill error: ${rfkill_status}\n"
printf "Unable to unblock.\n"
return 1
else
sleep 1
return 0
fi
}
setLink() {
if [ -x "$(command -v ip 2>&1)" ]; then
ip link set dev ${1} ${2} > /dev/null 2>&1 || printf "\nFailed to set ${1} ${2} using ip\n"
elif [ -x "$(command -v ifconfig 2>&1)" ]; then
ifconfig ${1} ${2} > /dev/null 2>&1 || printf "\nFailed to set ${1} ${2} using ifconfig\n"
fi
return
}
ifaceIsUp() {
if [ -x "$(command -v ip 2>&1)" ]; then
ifaceIsUpCmd="ip link show dev"
elif [ -x "$(command -v ifconfig 2>&1)" ]; then
ifaceIsUpCmd="ifconfig"
fi
if ${ifaceIsUpCmd} ${1} 2>&1 | grep -q UP
then
return
else
return 1
fi
}
#listIfaceUnspec() {
# if [ -x "$(command -v ip 2>&1)" ]; then
# ip link 2>/dev/null | awk -F"[: ]+" '/UNSPEC/ {print $2}'
# elif [ -x "$(command -v ifconfig 2>&1)" ]; then
# ifconfig -a 2>/dev/null | awk -F"[: ]+" '/UNSPEC/ {print $1}'
# fi
#}
#startDeprecatedIface() {
# iwconfig ${1} mode monitor > /dev/null 2>&1
# if [ -n "${2}" ]; then
# if [ ${2} -lt 1000 ]; then
# iwconfig ${1} channel ${2} > /dev/null 2>&1
# else
# iwconfig ${1} freq ${2}000000 > /dev/null 2>&1
# fi
# else
# iwconfig ${1} channel ${CH} > /dev/null 2>&1
# fi
# iwconfig ${1} key off > /dev/null 2>&1
# setLink ${1} up
# printf " (monitor mode enabled)"
#}
yesorno() {
read input
case $input in
y) return 0 ;;
yes) return 0 ;;
n) return 1 ;;
no) return 1 ;;
*) printf "\nInvalid input. Yes, or no? [y/n] "
yesorno;;
esac
}
startMac80211Iface() {
#check if rfkill is set and cry if it is
rfkill_check ${PHYDEV}
rfkill_retcode="$?"
case ${rfkill_retcode} in
1) printf "\t${1} is soft blocked, please run \"rfkill unblock ${1#phy}\" to use this interface.\n" ;;
2) printf "\t${1} is hard blocked, please flip the hardware wifi switch to on.\n"
printf "\tIt may also be possible to unblock with \"rfkill unblock ${1#phy}\"\n"
if [ "${checkvm_status}" != "run" ]; then
checkvm
fi
if [ -n "${vm}" ]; then
printf "Detected VM using ${vm_from}\n"
printf "This appears to be a ${vm} Virtual Machine\n"
printf "Some distributions have bugs causing rfkill hard block to be forced on in a VM.\n"
printf "If toggling the rfkill hardware switch and \"rfkill unblock ${1#phy}\" both fail\n"
printf "to fix this, please try not running in a VM.\n"
fi
;;
3) printf "\t${1} is hard and soft blocked, please flip the hardware wifi switch to on.\n"
printf "\tIt may also be needed to unblock with \"rfkill unblock ${1#phy}\"\n" ;;
esac
if [ "${rfkill_retcode}" != 0 ]; then
printf "rfkill error, unable to start ${1}\n\n"
printf "Would you like to try and automatically resolve this? [y/n] "
yesorno
retcode="$?"
if [ "${retcode}" = "1" ]; then
return 1
elif [ "${retcode}" = "0" ]; then
rfkill_unblock ${PHYDEV}
fi
fi
#check if $1 already has a mon interface on the same phy and bail if it does
if [ -d /sys/class/ieee80211/${PHYDEV}/device/net ]; then
for i in $(ls /sys/class/ieee80211/${PHYDEV}/device/net/); do
if [ "$(cat /sys/class/ieee80211/${PHYDEV}/device/net/${i}/type)" = "803" ]; then
setChannelMac80211 ${i}
printf "\n\t\t(mac80211 monitor mode already enabled for [${PHYDEV}]${1} on [${PHYDEV}]${i})\n"
exit
fi
done
fi
#we didn't bail means we need a monitor interface
if [ ${#1} -gt 12 ]; then
printf "Interface ${#1}mon is too long for linux so it will be renamed to the old style (wlan#) name.\n"
findFreeInterface monitor
else
if [ -e /sys/class/net/${1}mon ]; then
printf "\nYou already have a ${1}mon device but it is NOT in monitor mode."
printf "\nWhatever you did, don't do it again."
printf "\nPlease run \"iw ${1}mon del\" before attempting to continue\n"
exit 1
fi
#we didn't bail means our target interface is available
setLink ${1} down
IW_ERROR="$(iw phy ${PHYDEV} interface add ${1}mon type monitor 2>&1)"
if [ -z "${IW_ERROR}" ]; then
sleep 1
if [ "$(cat /sys/class/ieee80211/${PHYDEV}/device/net/${1}mon/type)" = "803" ]; then
setChannelMac80211 ${1}mon
else
printf "\nNewly created monitor mode interface ${1}mon is *NOT* in monitor mode.\n"
printf "Removing non-monitor ${1}mon interface...\n"
stopMac80211Iface ${1}mon abort
exit 1
fi
printf "\n\t\t(mac80211 monitor mode vif enabled for [${PHYDEV}]${1} on [${PHYDEV}]${1}mon)\n"
else
printf "\n\nERROR adding monitor mode interface: ${IW_ERROR}\n"
exit 1
fi
fi
if [ "${ELITE}" = "1" ]; then
#check if $1 is still down, warn if not
if ifaceIsUp ${1}
then
printf "\nInterface ${1} is up, but it should be down. Something is interferring."
printf "\nPlease run \"airmon-ng check kill\" and/or kill your network manager."
fi
#### we like to keep our second interface as it is
# else
# iw ${1} del
# printf "\t\t(mac80211 station mode vif disabled for [${PHYDEV}]${1})\n"
fi
}
startwlIface() {
if [ -f "/proc/brcm_monitor0" ]; then
if [ -r "/proc/brcm_monitor0" ]; then
local brcm_monitor="$(cat /proc/brcm_monitor0)"
if [ "$brcm_monitor" = "1" ]; then
printf "\n\t\t(experimental wl monitor mode vif already enabled for [${PHYDEV}]${1} on [${PHYDEV}]prism0)\n"
return 0
fi
fi
if [ -w "/proc/brcm_monitor0" ]; then
printf "1" > /proc/brcm_monitor0
if [ "$?" = "0" ]; then
printf "\n\t\t(experimental wl monitor mode vif enabled for [${PHYDEV}]${1} on [${PHYDEV}]prism0)\n"
else
printf "\n\t\t(failed to enable experimental wl monitor mode for [${PHYDEV}${1})\n"
fi
else
printf "\n\tUnable to write to /proc/brcm_monitor0, cannot enable monitor mode.\n"
fi
else
printf "\n\tThis version of wl does not appear to suport monitor mode.\n"
fi
}
#startDarwinIface() {
# if [ -x /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport ]; then
# if [ -n "${CH}" ] && [ ${CH} -lt 220 ]; then
# /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport $1 sniff ${CH}
# else
# printf "Channel is set to none channel value of ${CH}
# fi
# fi
#}
setChannelMac80211() {
setLink ${1} up
if [ -n "${CH}" ]; then
if [ ${CH} -lt 1000 ]; then
#mac80211 specific check to see if hardware supports the channel number being assigned (requires grep with pcre)
if [ -r "/sys/class/net/$1/phy80211/name" ]; then
channel_list="$(iw phy $(cat /sys/class/net/$1/phy80211/name) info 2>&1 | grep -oP '\[\K[^\]]+')"
local hardware_valid_channel=1
for i in $channel_list; do
if [ "${CH}" = "${i}" ]; then
hardware_valid_channel=0
break
fi
done
if [ "${hardware_valid_channel}" = "1" ]; then
printf "Channel ${CH} does not appear to be supported by ${1} hardware, defaulting to channel 3.\n\n"
CH=3
fi
fi
IW_ERROR="$(iw dev ${1} set channel ${CH} 2>&1)"
else
#mac80211 specific check to see if hardware supports the frequency number being assigned (requires grep with pcre)
if [ -r "/sys/class/net/$1/phy80211/name" ]; then
frequency_list="$(iw phy $(cat /sys/class/net/$1/phy80211/name) info 2>&1 | grep -oP '\*\ \K[\d]+\ ')"
local hardware_valid_freq=1
for i in $frequency_list; do
if [ "${CH}" = "${i}" ]; then
hardware_valid_freq=0
break
fi
print
done
if [ "${hardware_valid_freq}" = "1" ]; then
printf "Frequency ${CH} does not appear to be supported by ${1} hardware, defaulting to 2422 Mhz.\n\n"
CH="2422"
fi
fi
IW_ERROR="$(iw dev ${1} set freq "${CH}" 2>&1)"
fi
else
printf "CH is unset, this should not be possible.\n"
exit 1
fi
if [ -n "${IW_ERROR}" ]; then
printf "\nError setting channel: ${IW_ERROR}\n"
if printf "${IW_ERROR}" | grep -q "(-16)"; then
printf "Error -16 likely means your card was set back to station mode by something.\n"
printf "Removing non-monitor ${1} interface...\n"
stopMac80211Iface "${1}" abort
exit 1
fi
if printf "${IW_ERROR}" | grep -q "(-22)"; then
printf "Unable to set channel/frequency ${CH}, most likely it is outside of regulatory domain\n\n"
fi
fi
}
#stopDeprecatedIface() {
# setLink $1 down
# iwconfig $1 mode Managed > /dev/null 2>&1
# setLink $1 up
# printf " (monitor mode disabled)"
#}
stopMac80211Iface() {
if [ -f /sys/class/net/${1}/type ]; then
if [ "${2}" != "abort" ] && [ "$(cat /sys/class/net/${1}/type)" != "803" ]; then
printf "\n\nYou are trying to stop a device that isn't in monitor mode.\n"
printf "Doing so is a terrible idea, if you really want to do it then you\n"
printf "need to type 'iw ${1} del' yourself since it is a terrible idea.\n"
printf "Most likely you want to remove an interface called wlan[0-9]mon\n"
printf "If you feel you have reached this warning in error,\n"
printf "please report it.\n"
exit 1
else
if [ "${ELITE}" = "0" ]; then
local need_sta=1
if [ -d /sys/class/ieee80211/${PHYDEV}/device/net ]; then
for i in $(ls /sys/class/ieee80211/${PHYDEV}/device/net/); do
if [ "$(cat /sys/class/ieee80211/${PHYDEV}/device/net/${i}/type)" = "1" ]; then
[ "${2}" != "abort" ] && printf "\n\t\t(mac80211 station mode vif already available for [${PHYDEV}]${1} on [${PHYDEV}]${i})\n"
need_sta=0
fi
done
fi
if [ "${need_sta}" = "1" ] && [ -e /sys/class/net/${1%mon}/phy80211/name ]; then
if [ "$(cat /sys/class/net/${1%mon}/phy80211/name)" = "${PHYDEV}" ]; then
printf "\nYou already have a ${1%mon} device but it is NOT in station mode."
printf "\nWhatever you did, don't do it again."
printf "\nPlease run \"iw ${1%mon} del\" before attempting to continue\n"
exit 1
else
printf "\nYou already have a ${1%mon} device, but is is not on the same phy as ${1}.\n"
printf "\nAttemping to pick a new name...\n"
findFreeInterface station
fi
fi
if [ "${need_sta}" = "1" ]; then
IW_ERROR="$(iw phy ${PHYDEV} interface add ${1%mon} type station 2>&1)"
if [ -z "${IW_ERROR}" ]; then
interface="${1%mon}"
if [ -d /sys/class/ieee80211/${PHYDEV}/device/net ]; then
for i in $(ls /sys/class/ieee80211/${PHYDEV}/device/net/); do
if [ "$(cat /sys/class/ieee80211/${PHYDEV}/device/net/${i}/type)" = "1" ]; then
#here is where we catch udev renaming our interface
interface="${i}"
fi
done
fi
printf "\n\t\t(mac80211 station mode vif enabled on [${PHYDEV}]${interface})\n"
else
printf "\n\n ERROR adding station mode interface: ${IW_ERROR}\n"
fi
fi
fi
setLink ${1} down
IW_ERROR="$(iw dev "${1}" del 2>&1 | grep "nl80211 not found")"
if [ -z "$IW_ERROR" ]; then
if [ "${2}" != "abort" ]; then
printf "\n\t\t(mac80211 monitor mode vif disabled for [${PHYDEV}]${1})\n"
elif [ "${2}" = "abort" ]; then
printf "\nWARNING: unable to start monitor mode, please run \"airmon-ng check kill\"\n"
fi
else
if [ -f /sys/class/ieee80211/${PHYDEV}/remove_iface ]; then
printf "${1}" > /sys/class/ieee80211/${PHYDEV}/remove_iface
printf "\n\t\t(mac80211 monitor mode vif disabled for [${PHYDEV}]${1})\n"
else
printf "\n\nERROR: Neither the sysfs interface links nor the iw command is available.\nPlease download and install iw from\n$IW_SOURCE\n"
fi
fi
fi
fi
}
stopwlIface() {
if [ -f "/proc/brcm_monitor0" ]; then
if [ -r "/proc/brcm_monitor0" ]; then
local brcm_monitor="$(cat /proc/brcm_monitor0)"
if [ "$brcm_monitor" = "0" ]; then
printf "\n\t\t(experimental wl monitor mode vif already disabled for [${PHYDEV}]${1})\n"
return 0
fi
fi
if [ -w "/proc/brcm_monitor0" ]; then
printf "0" > /proc/brcm_monitor0
if [ "$?" = "0" ]; then
printf "\n\t\t(experimental wl monitor mode vif disabled for [${PHYDEV}]${1})\n"
else
printf "\n\t\t(failed to disable experimental wl monitor mode for [${PHYDEV}${1})\n"
fi
else
printf "\n\tUnable to write to /proc/brcm_monitor0, cannot disable monitor mode.\n"
fi
else
printf "\n\tThis version of wl does not appear to suport monitor mode.\n"
fi
}
getDriver() {
#standard detection path, this is all that is needed for proper drivers
#DRIVER=$(printf "$ethtool_output" | awk '/driver/ {print $2}')
#if $(modinfo -n ${DRIVER} > /dev/null 2>&1)
#then
# true
#else
# unset DRIVER
#fi
#if [ "$DRIVER" = "" ]
#then
if [ -f /sys/class/net/$1/device/uevent ]; then
DRIVER="$(awk -F'=' '$1 == "DRIVER" {print $2}' /sys/class/net/$1/device/uevent)"
else
#DRIVER we put SOMETHING in DRIVER here if we are unable to find anything real
DRIVER="??????"
fi
#fi
#here we test for driver usb, ath9k_htc,rt2870, possibly others show this
if [ "$DRIVER" = "usb" ]; then
printf "Warn ON: USB\n"
BUSADDR="$(printf "$ethtool_output" | awk '/bus-info/ {print $2}'):1.0"
if [ "$DEBUG" = "1" ]; then
printf "${BUSADDR}\n"
fi
if [ -n "$BUSADDR" ]; then
if [ -f /sys/class/net/$1/device/"$BUSADDR"/uevent ]; then
DRIVER="$(awk -F'=' '$1 == "DRIVER" {print $2}' /sys/class/net/$1/device/$BUSADDR/uevent)"
fi
fi
#here we can normalize driver names we don't like
if [ "$DRIVER" = "rt2870" ]; then
DRIVER="rt2870sta"
fi
if [ -f /sys/class/net/$1/device/idProduct ]; then
if [ "$(cat /sys/class/net/$1/device/idProduct)" = "3070" ]; then
DRIVER="rt3070sta"
fi
fi
fi
if [ "$DRIVER" = "rtl8187L" ]; then
DRIVER="r8187l"
fi
if [ "$DRIVER" = "rtl8187" ] && [ "$STACK" = "ieee80211" ]; then
DRIVER="r8187"
fi
#Here we will catch the broken lying drivers not caught above
#currently this only functions for pci devices and not usb since lsusb has no -k option
if [ "${MODINFO}" = "1" ]; then
if $(modinfo -n $DRIVER > /dev/null 2>&1)
then
true
else
if [ -n "${DEVICEID}" ] && [ "$BUS" = "pci" ]; then
DRIVER="$(lspci -d $DEVICEID -k | awk '/modules/ {print $3}')"
fi
if [ -n "$DRIVER" ]; then
DRIVER="??????"
fi
fi
fi
if [ "$DEBUG" = "1" ]; then
printf "getdriver() $DRIVER\n"
fi
}
getFrom() {
#from detection
FROM="K"
if [ "${MODINFO}" = "1" ] && [ -f /proc/modules ]; then
if modinfo -n $DRIVER 2>&1 | grep -q 'kernel/drivers'
then
FROM="K"
#we add special handling here because we hate the vendor drivers AND they install in the wrong place
if [ "$DRIVER" = "r8187" ]; then
FROM="V"
elif [ "$DRIVER" = "r8187l" ]; then
FROM="V"
elif [ "$DRIVER" = "rt5390sta" ]; then
FROM="V"
fi
elif modinfo -n $DRIVER 2>&1 | grep -q 'updates/drivers'
then
FROM="C"
elif modinfo -n $DRIVER 2>&1 | grep -q misc
then
FROM="V"
elif modinfo -n $DRIVER 2>&1 | grep -q staging
then
FROM="S"
else
FROM="?"
fi
else
FROM="K"
fi
if [ "$DEBUG" = "1" ]; then
printf "getFrom() $FROM\n"
fi
}
getFirmware() {
FIRMWARE="$(printf "$ethtool_output" | awk '/firmware-version/ {print $2}')"
#ath9k_htc firmware is a shorter version number than most so trap and make it pretty
if [ "$DRIVER" = "ath9k_htc" ]; then
FIRMWARE="$FIRMWARE\t"
fi
if [ "$FIRMWARE" = "N/A" ]; then
FIRMWARE="$FIRMWARE\t"
elif [ -z "$FIRMWARE" ]; then
FIRMWARE="unavailable"
fi
if [ "$DEBUG" = "1" ]; then
printf "getFirmware $FIRMWARE\n"
fi
}
getChipset() {
#this needs cleanup, we shouldn't have multiple lines assigning chipset per bus
#fix this to be one line per bus
if [ -f /sys/class/net/$1/device/modalias ]; then
BUS="$(cut -d ":" -f 1 /sys/class/net/$1/device/modalias)"
if [ "$BUS" = "usb" ]; then
if [ "${LSUSB}" = "1" ]; then
BUSINFO="$(cut -d ":" -f 2 /sys/class/net/$1/device/modalias | cut -b 1-10 | sed 's/^.//;s/p/:/')"
CHIPSET="$(lsusb -d "$BUSINFO" | head -n1 - | cut -f3- -d ":" | sed 's/^....//;s/ Network Connection//g;s/ Wireless Adapter//g;s/^ //')"
elif [ "${LSUSB}" = "0" ]; then
printf "Your system doesn't seem to support usb but we found usb hardware, please report this.\n"
exit 1
fi
#yes the below line looks insane, but broadcom appears to define all the internal buses so we have to detect them here
elif [ "${BUS}" = "pci" -o "${BUS}" = "pcmcia" ] && [ "${LSPCI}" = "1" ]; then
if [ -f /sys/class/net/$1/device/vendor ] && [ -f /sys/class/net/$1/device/device ]; then
DEVICEID="$(cat /sys/class/net/$1/device/vendor):$(cat /sys/class/net/$1/device/device)"
CHIPSET="$(lspci -d $DEVICEID | cut -f3- -d ":" | sed 's/Wireless LAN Controller //g;s/ Network Connection//g;s/ Wireless Adapter//;s/^ //')"
else
BUSINFO="$(printf "$ethtool_output" | grep bus-info | cut -d ":" -f "3-" | sed 's/^ //')"
CHIPSET="$(lspci | grep "$BUSINFO" | head -n1 - | cut -f3- -d ":" | sed 's/Wireless LAN Controller //g;s/ Network Connection//g;s/ Wireless Adapter//;s/^ //')"
DEVICEID="$(lspci -nn | grep "$BUSINFO" | grep '[[0-9][0-9][0-9][0-9]:[0-9][0-9][0-9][0-9]' -o)"
fi
elif [ "${BUS}" = "sdio" ]; then
if [ -f /sys/class/net/$1/device/vendor ] && [ -f /sys/class/net/$1/device/device ]; then
DEVICEID="$(cat /sys/class/net/$1/device/vendor):$(cat /sys/class/net/$1/device/device)"
fi
if [ "${DEVICEID}" = '0x02d0:0x4330' ]; then
CHIPSET='Broadcom 4330'
elif [ "${DEVICEID}" = '0x02d0:0x4329' ]; then
CHIPSET='Broadcom 4329'
elif [ "${DEVICEID}" = '0x02d0:0x4334' ]; then
CHIPSET='Broadcom 4334'
elif [ "${DEVICEID}" = '0x02d0:0xa94c' ]; then
CHIPSET='Broadcom 43340'
elif [ "${DEVICEID}" = '0x02d0:0xa94d' ]; then
CHIPSET='Broadcom 43341'
elif [ "${DEVICEID}" = '0x02d0:0x4324' ]; then
CHIPSET='Broadcom 43241'
elif [ "${DEVICEID}" = '0x02d0:0x4335' ]; then
CHIPSET='Broadcom 4335/4339'
elif [ "${DEVICEID}" = '0x02d0:0xa962' ]; then
CHIPSET='Broadcom 43362'
elif [ "${DEVICEID}" = '0x02d0:0xa9a6' ]; then
CHIPSET='Broadcom 43430'
elif [ "${DEVICEID}" = '0x02d0:0x4345' ]; then
CHIPSET='Broadcom 43455'
elif [ "${DEVICEID}" = '0x02d0:0x4354' ]; then
CHIPSET='Broadcom 4354'
elif [ "${DEVICEID}" = '0x02d0:0xa887' ]; then
CHIPSET='Broadcom 43143'
else
CHIPSET="unable to detect for sdio $DEVICEID"
fi
else
CHIPSET="Not pci, usb, or sdio"
fi
#we don't do a check for usb here but it is obviously only going to work for usb
elif [ -f /sys/class/net/$1/device/idVendor ] && [ -f /sys/class/net/$1/device/idProduct ]; then
DEVICEID="$(cat /sys/class/net/$1/device/idVendor):$(cat /sys/class/net/$1/device/idProduct)"
if [ "${LSUSB}" = "1" ]; then
CHIPSET="$(lsusb | grep -i "$DEVICEID" | head -n1 - | cut -f3- -d ":" | sed 's/^....//;s/ Network Connection//g;s/ Wireless Adapter//g;s/^ //')"
elif [ "${LSUSB}" = "0" ]; then
CHIPSET="idVendor and idProduct found on non-usb device, please report this."
fi
elif [ "${DRIVER}" = "mac80211_hwsim" ]; then
CHIPSET="Software simulator of 802.11 radio(s) for mac80211"
elif $(printf "$ethtool_output" | awk '/bus-info/ {print $2}' | grep -q bcma)
then
BUS="bcma"
if [ "${DRIVER}" = "brcmsmac" ] || [ "${DRIVER}" = "brcmfmac" ] || [ "${DRIVER}" = "b43" ]; then
CHIPSET="Broadcom on bcma bus, information limited"
else
CHIPSET="Unrecognized driver \"${DRIVER}\" on bcma bus"
fi
else
CHIPSET="non-mac80211 device? (report this!)"
fi
if [ "$DEBUG" = "1" ]; then
printf "getchipset() $CHIPSET\n"
printf "BUS = $BUS\n"
printf "BUSINFO = $BUSINFO\n"
printf "DEVICEID = $DEVICEID\n"
fi
}
getStack() {
if [ -z "$1" ]; then
return
fi
if [ -d /sys/class/net/$1/phy80211/ ]; then
MAC80211="1"
STACK="mac80211"
else
MAC80211="0"
STACK="ieee80211"
fi
if [ -e /proc/sys/dev/$1/fftxqmin ]; then
MAC80211="0"
STACK="net80211"
fi
if [ "$DEBUG" = "1" ]; then
printf "getStack $STACK\n"
fi
}
getExtendedInfo() {
#stuff rfkill info into extended if nothing else is there
rfkill_check ${PHYDEV}
rfkill_retcode="$?"
if [ "${rfkill_retcode}" = "1" ]; then
EXTENDED="rfkill soft blocked"
elif [ "${rfkill_retcode}" = "2" ]; then
EXTENDED="rfkill hard blocked"
elif [ "${rfkill_redcode}" = "3" ]; then
EXTENDED="rfkill hard and soft blocked"
fi
if [ "$DRIVER" = "??????" ]; then
EXTENDED="\t Failure detecting driver properly please report"
fi
#first we set all the real (useful) info we can find
if [ -f /sys/class/net/$1/device/product ]; then
EXTENDED="\t$(cat /sys/class/net/$1/device/product)"
fi
#then we sweep for known broken drivers with no available better drivers
if [ "$DRIVER" = "wl" ]; then
if [ -f "/proc/brcm_monitor0" ]; then
EXTENDED="Experimental monitor mode support"
else
EXTENDED="No known monitor support, try a newer version or b43"
fi
fi
if [ "$DRIVER" = "brcmsmac" ]; then
EXTENDED="Driver commonly referred to as brcm80211 (no injection yet)"
fi
if [ "$DRIVER" = "r8712u" ]; then
EXTENDED="\t\t\t\tNo monitor or injection support"
fi
#lastly we detect all the broken drivers which have working alternatives
KV="$(uname -r | awk -F'-' '{print $1}')"
KVMAJOR="$(printf ${KV} | awk -F'.' '{print $1$2}')"
KVMINOR="$(printf ${KV} | awk -F'.' '{print $3}')"
if [ $KVMAJOR -lt 26 ]; then
printf "You are running a kernel older than 2.6, I'm surprised it didn't error before now."
if [ "$DEBUG" = "1" ]; then
printf "${KVMAJOR} ${KVMINOR}\n"
fi
exit 1
fi
if [ "$DRIVER" = "rt2870sta" ]; then
if [ "$KVMAJOR" = "26" ] && [ "$KVMINOR" -ge "35" ]; then
EXTENDED="\tBlacklist rt2870sta and use rt2800usb"
else
EXTENDED="\tUpgrade to kernel 2.6.35 or install compat-wireless stable"
fi
#add in a flag for "did you tell use to do X" and emit instructions
elif [ "$DRIVER" = "rt3070sta" ]; then
if [ "$KVMAJOR" = "26" ] && [ "$KVMINOR" -ge "35" ]; then
EXTENDED="\tBlacklist rt3070sta and use rt2800usb"
else
EXTENDED="\tUpgrade to kernel 2.6.35 or install compat-wireless stable"
fi
elif [ "$DRIVER" = "rt5390sta" ]; then
if [ "$KVMAJOR" = "26" ] && [ "$KVMINOR" -ge "39" ]; then
EXTENDED="\tBlacklist rt5390sta and use rt2800usb"
else
EXTENDED="\tUpgrade to kernel 2.6.39 or install compat-wireless stable"
fi
elif [ "$DRIVER" = "ar9170usb" ]; then
if [ "$KVMAJOR" = "26" ] && [ "$KVMINOR" -ge "37" ]; then
EXTENDED="\tBlacklist ar9170usb and use carl9170"
else
EXTENDED="\tUpgrade to kernel 2.6.37 or install compat-wireless stable"
fi
elif [ "$DRIVER" = "arusb_lnx" ]; then
if [ "$KVMAJOR" = "26" ] && [ "$KVMINOR" -ge "37" ]; then
EXTENDED="\tBlacklist arusb_lnx and use carl9170"
else
EXTENDED="\tUpgrade to kernel 2.6.37 or install compat-wireless stable"
fi
elif [ "$DRIVER" = "r8187" ]; then
if [ "$KVMAJOR" = "26" ] && [ "$KVMINOR" -ge "29" ]; then
EXTENDED="\t\tBlacklist r8187 and use rtl8187 from the kernel"
else
EXTENDED="\t\tUpgrade to kernel 2.6.29 or install compat-wireless stable"
fi
elif [ "$DRIVER" = "r8187l" ]; then
if [ "$KVMAJOR" = "26" ] && [ "$KVMINOR" -ge "29" ]; then
EXTENDED="\t\tBlacklist r8187l and use rtl8187 from the kernel"
else
EXTENDED="\t\tUpgrade to kernel 2.6.29 or install compat-wireless stable"
fi
fi
}
scanProcesses() {
#this test means it errored and said it was busybox since busybox doesn't print without error
if (ps -A 2>&1 | grep -q BusyBox)
then
#busybox in openwrt cannot handle -A but its output by default is -A
psopts=""
else
psopts="-A"
fi
if ( ps -o comm= 2>&1 | grep -q BusyBox )
then
#busybox in openwrt cannot handle -o
pso="0"
else
pso="1"
fi
PROCESSES="wpa_action\|wpa_supplicant\|wpa_cli\|dhclient\|ifplugd\|dhcdbd\|dhcpcd\|udhcpc\|NetworkManager\|knetworkmanager\|avahi-autoipd\|avahi-daemon\|wlassistant\|wifibox"
#PS_ERROR="invalid\|illegal"