This file is indexed.

/usr/sbin/airmon-zc is in aircrack-ng 1:1.2-0~beta3-4.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
#!/bin/sh
DEBUG="0"
VERBOSE="0"
ELITE="0"
USERID=""
IFACE=""
MADWIFI=0
MAC80211=0
IW_SOURCE="https://www.kernel.org/pub/software/network/iw/iw-3.11.tar.bz2"
IW_ERROR=""
UDEV_ISSUE=0
SYSFS=0
if [ -d /sys/ ]
then
	SYSFS=1
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


CH=${3}; [ x${3} = "x" ] && CH=10

#TODO LIST

#cleanup getDriver()
#add band detection
#fix to not assume wifi drivers are modules
#allow users to start the interface multiple times to change the channel
#rewrite scanprocesses and checkprocessesiface to be sane

if [ x"$(command -v id 2> /dev/null)" != "x" ]
then
	USERID="$(id -u 2> /dev/null)"
fi

if [ x${USERID} = "x" -a x${UID} != "x" ]
then
	USERID=${UID}
fi

if [ x${USERID} != "x" -a x${USERID} != "x0" ]
then
	printf "Run it as root\n" ; exit 1;
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
	echo
	#write something in here to test uname for Darwin/Linux and set a variable
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 [ ! -x "$(command -v lsusb 2>&1)" ]
then
	printf "Please install lsusb from your distro's package manager.\n"
	exit 1
fi

if [ ! -x "$(command -v lspci 2>&1)" ]
then
	printf "Please install lspci from your distro's package manager.\n"
	exit 1
fi

if [ ! -x "$(command -v modprobe 2>&1)" ]
then
	printf "How in the world do you not have modprobe installed?\n"
	printf "Please select a linux distro which has at least basic functionality (or install modprobe).\n"
	exit 1
fi

if [ ! -x "$(command -v modinfo 2>&1)" ]
then
	printf "How in the world do you not have modinfo installed?\n"
	printf "Please select a linux distro which has at least basic functionality (or install modinfo).\n"
	exit 1
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

PROCESSES="wpa_action\|wpa_supplicant\|wpa_cli\|dhclient\|ifplugd\|dhcdbd\|dhcpcd\|NetworkManager\|knetworkmanager\|avahi-autoipd\|avahi-daemon\|wlassistant\|wifibox"
PS_ERROR="invalid"

usage() {
	printf "usage: $(basename $0) <start|stop|check> <interface> [channel or frequency]\n\n"
	exit
}

startDeprecatedIface() {
	iwconfig $1 mode monitor > /dev/null 2>&1
	if [ ! -z $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
	ifconfig $1 up
	printf " (monitor mode enabled)"
}

startMac80211Iface() {
	#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
				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 [ -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
	ifconfig ${1} down
	IW_ERROR=$(iw phy ${PHYDEV} interface add ${1}mon type monitor 2>&1 | grep "nl80211 not found")
	if [ x${IW_ERROR} = "x" ]
	then
		sleep 1s
		ifconfig ${1}mon up
		if [ ! -z ${3} ]
		then
			if [ ${3} -lt 1000 ]
			then
				iw dev ${1}mon set channel ${3}
			else
				iw dev ${1}mon set freq "${3}"
			fi
		else
			iw dev ${1}mon set channel ${CH}
		fi
		printf "\n\t\t(mac80211 monitor mode vif enabled for [${PHYDEV}]${1} on [${PHYDEV}]${1}mon)\n"
	else
		printf "\n\nERROR: something went wrong with iw."
	fi
	if [ ${ELITE} = "1" ]
	then
		#check if $1 is still down, warn if not
		if $(ifconfig ${1} | grep -q UP)
		then
			printf "\nInterface ${1} is up, but it should be down. Something is interferring."
			printf "\nPlease run \"airmon-zc check kill\" and/or kill your network manager."
		fi
	else
		iw ${1} del
		printf "\t\t(mac80211 station mode vif disabled for [${PHYDEV}]${1})\n"
	fi
}

startDarwinIface() {
	if [ -x /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport ]
	then
		/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport $1 sniff
	fi
}

stopDeprecatedIface() {
	ifconfig $1 down > /dev/null 2>&1
	iwconfig $1 mode Managed > /dev/null 2>&1
	ifconfig $1 down > /dev/null 2>&1
	printf " (monitor mode disabled)"
}

stopMac80211Iface() {
	if [ -f /sys/class/net/${1}/type ]
	then
		if [ "$(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."
			exit 1
		else
			if [ "${ELITE}" = "0" ]
			then
				#check if $1 already has a sta 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)" = "1" ]
						then
							printf "\n\t\t(mac80211 station mode vif already available for [${PHYDEV}]${1} on [${PHYDEV}]${i})\n"
							exit
						fi
					done
				fi
				if [ -e /sys/class/net/${1%mon} ]
				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
				fi
				IW_ERROR=$(iw phy ${PHYDEV} interface add ${1%mon} type station 2>&1)
				if [ x${IW_ERROR} = "x" ]
				then
					printf "\n\t\t(mac80211 station mode vif enabled on [${PHYDEV}]${1%mon})"
					unset IW_ERROR
				else
					printf "\n\n ERROR: iw command error"
				fi
			fi
			IW_ERROR=$(iw dev "${1}" del 2>&1 | grep "nl80211 not found")
			if [ x$IW_ERROR = "x" ]
			then
				printf "\n\t\t(mac80211 monitor mode vif disabled for [${PHYDEV}]${1})"
			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})"
				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
}

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
		echo "Warn ON: USB"
		BUSADDR="$(printf "$ethtool_output" | awk '/bus-info/ {print $2}'):1.0"
		if [ "$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 -n $DRIVER  > /dev/null 2>&1)
	then
		true
	else
		if [ "${DEVICEID}" != "" ] && [ "$BUS" = "pci" ]
		then
			DRIVER="$(lspci -d $DEVICEID -k | awk '/modules/ {print $3}')"
		fi
		if [ "$DRIVER" = "" ]
		then
			DRIVER="??????"
		fi
	fi
	if [ "$DEBUG" = "1" ]
	then
		printf "getdriver() $DRIVER\n"
	fi
}

getFrom() {
	#from detection
	if [ $(modinfo -n $DRIVER 2>&1 | grep '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 'updates/drivers') ]
	then
		FROM="C"
	elif [ $(modinfo -n $DRIVER 2>&1 | grep misc) ]
	then
		FROM="M"
		#add a yell at the user in here
	else
		FROM="?"
	fi

	#check for staging drivers seperately
	if [ $(modinfo -n $DRIVER 2>&1 | grep staging) ]
	then
		FROM="S"
        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=$(cat /sys/class/net/$1/device/modalias | cut -d ":" -f 1)
		if [ "$BUS" = "usb" ]
		then
			BUSINFO=$(cat /sys/class/net/$1/device/modalias | cut -d ":" -f 2 | cut -b 1-10 | sed 's/^.//;s/p/:/')
			CHIPSET=$(lsusb | grep -i "$BUSINFO" | head -n1 - | cut -f3- -d ":" | sed 's/^....//;s/ Network Connection//g;s/ Wireless Adapter//g;s/^ //')
		#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" ] || [ "${BUS}" = "ssb" ] || [ "${BUS}"= "bcma" ] || [ "${BUS}" = "pcmcia" ]
		then
			if [ -f /sys/class/net/$1/device/vendor -a -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 -a -f /sys/class/net/$1/device/device ]
			then
				DEVICEID=$(cat /sys/class/net/$1/device/vendor):$(cat /sys/class/net/$1/device/device)
			fi
			CHIPSET="unable to detect for sdio $DEVICEID"
		else
			CHIPSET="Not pci or usb"
		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 -a -f /sys/class/net/$1/device/idProduct ]
	then
		DEVICEID=$(cat /sys/class/net/$1/device/idVendor):$(cat /sys/class/net/$1/device/idProduct)
		CHIPSET=$(lsusb | grep -i "$DEVICEID" | head -n1 - | cut -f3- -d ":" | sed 's/^....//;s/ Network Connection//g;s/ Wireless Adapter//g;s/^ //')
	elif [ "${DRIVER}" = "mac80211_hwsim" ]
	then
		CHIPSET="Software simulator of 802.11 radio(s) for mac80211"
	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 [ x"$1" = "x" ]
	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() {
	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" = "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\n"
	fi

	#lastly we detect all the broken drivers which have working alturnatives
	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}
		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
	EXTENDED="We Todd Ed"
}

scanProcesses() {
	if [ -f "$(command -v service 2>&1)" ] && [ x"$1" = "xkill" ]
	then
		service network-manager stop 2> /dev/null > /dev/null
		service avahi-daemon stop 2> /dev/null > /dev/null
	fi

	match=$(ps -A -o comm= | grep $PROCESSES | grep -v grep | wc -l)
	if [ $match -gt 0 -a x"$1" != "xkill" ]
	then
		printf "\nFound $match processes that could cause trouble.\n"
		printf "If airodump-ng, aireplay-ng or airtun-ng stops working after\n"
		printf "a short period of time, you may want to kill (some of) them!\n"
		printf "\nPID\tName\n"
	else
		if [ x"$1" != "xkill" ]
		then
			return
		fi
	fi

	if [ $match -gt 0 -a x"$1" = "xkill" ]
	then
		printf "Killing all those processes..."
	fi

	i=1
	while [ $i -le $match ]
	do
		pid=$(ps -A -o pid= -o comm= | grep $PROCESSES | grep -v grep | head -n $i | tail -n 1 | awk '{print $1}')
		pname=$(ps -A -o pid= -o comm= | grep $PROCESSES | grep -v grep | head -n $i | tail -n 1 | awk '{print $2}')
		if [ x"$1" != "xkill" ]
		then
			printf "$pid\t$pname\n"
		else
			kill $pid
		fi
		i=$(($i+1))
	done
}

checkProcessesIface() {
	if [ x"$1" = "x" ]
	then
		return
	fi

	match2=$(ps -o comm= -p 1 2>&1 | grep $PS_ERROR | grep -v grep | wc -l)
	if [ $match2 -gt 0 ]
	then
		return
	fi

	for i in $(ps auxw | grep $1 | grep -v "grep" | grep -v "airmon-zc" | awk '{print $2}')
	do
		pname=$(ps -o comm= -p $i)
		printf "Process with PID $i ($pname) is running on interface $1\n"
	done
}

getPhy() {
	if [ x"$1" = "x" ]
	then
		return
	fi

	if [ $MAC80211 = "0" ]
	then
		PHYDEV="null"
		return
	fi

	if [ -d /sys/class/net/$1/phy80211/ ]
	then
		PHYDEV="$(ls -l "/sys/class/net/$1/phy80211" | sed 's/^.*\/\([a-zA-Z0-9_-]*\)$/\1/')"
	fi
}

checkvm() {
	#this entire section of code is completely stolen from Carlos Perez's work in checkvm.rb for metasploit and rewritten (poorly) in sh
	#Check dmi info
	if [ -x "$(command -v dmidecode 2>&1)" ]
	then
		dmi_info=$(dmidecode)
		if [ dmi_info ]
		then
			echo ${dmi_info} | grep -iq "microsoft corporation" 2> /dev/null && vm="MS Hyper-V"
			echo ${dmi_info} | grep -iq "vmware" 2> /dev/null && vm="VMware"
			echo ${dmi_info} | grep -iq "virtualbox" 2> /dev/null && vm="VirtualBox"
			echo ${dmi_info} | grep -iq "qemu" 2> /dev/null && vm="Qemu/KVM"
			echo ${dmi_info} | grep -iq "domu" 2> /dev/null && vm="Xen"
			vm_from="dmi_info"
		fi
	fi

	#check loaded modules
	if [ -z ${vm} ]
	then
		if [ -x "$(command -v lsmod 2>&1)" ]
		then
			lsmod_data=$(lsmod)
			if [ lsmod ]
			then
				echo ${lsmod_data} | grep -iqE "vboxsf|vboxguest" 2> /dev/null && vm="VirtualBox"
				echo ${lsmod_data} | grep -iqE "vmw_ballon|vmxnet|vmw" 2> /dev/null && vm="VMware"
				echo ${lsmod_data} | grep -iqE "xen-vbd|xen-vnif" 2> /dev/null && vm="Xen"
				echo ${lsmod_data} | grep -iqE "virtio_pci|virtio_net" 2> /dev/null && vm="Qemu/KVM"
				echo ${lsmod_data} | grep -iqE "hv_vmbus|hv_blkvsc|hv_netvsc|hv_utils|hv_storvsc" && vm="MS Hyper-V"
				vm_from="lsmod"
			fi
		fi
	fi

	#check scsi driver
	if [ -z ${vm} ]
	then
		if [ -f /proc/scsi/scsi ]
		then
			grep -iq "vmware" /proc/scsi/scsi 2> /dev/null && vm="VMware"
			grep -iq "vbox" /proc/scsi/scsi 2> /dev/null && vm="VirtualBox"
			vm_from="/pro/scsi/scsi"
		fi
	fi

	# Check IDE Devices
	if [ -z ${vm} ]
	then
		if [ -d /proc/ide ]
		then
			ide_model=$(cat /proc/ide/hd*/model)
			echo ${ide_model} | grep -iq "vbox" 2> /dev/null && vm="VirtualBox"
			echo ${ide_model} | grep -iq "vmware" 2> /dev/null && vm="VMware"
			echo ${ide_model} | grep -iq "qemu" 2> /dev/null && vm="Qemu/KVM"
			echo ${ide_model} | grep -iqE "virtual (hd|cd)" 2> /dev/null && vm="Hyper-V/Virtual PC"
			vm_from="ide_model"
		fi
	fi

	# Check using lspci
	if [ -z ${vm} ]
	then
			lspci_data=$(lspci)
			echo ${lspci_data} | grep -iq "vmware" 2> /dev/null && vm="VMware"
			echo ${lspci_data} | grep -iq "virtualbox" 2> /dev/null && vm="VirtualBox"
			vm_from="lspci"
	fi

	# Xen bus check
	## XXX: Removing unsafe check
	# this check triggers if CONFIG_XEN_PRIVILEGED_GUEST=y et al are set in kconfig (debian default) even in not actually a guest
	#if [ -z ${vm} ]
	#then
	#	ls -1 /sys/bus | grep -iq "xen" 2> /dev/null && vm="Xen"
	#	vm_from="/sys/bus/xen"
	#fi

	# Check using lscpu
	if [ !${vm} ]
	then
		if [ -x "$(command -v lscpu 2>&1)" ]
                then
                        lscpu_data=$(lscpu)
			echo ${lscpu_data} | grep -iq "Xen" 2> /dev/null && vm="Xen"
			echo ${lscpu_data} | grep -iq "KVM" 2> /dev/null && vm="KVM"
			echo ${lscpu_data} | grep -iq "Microsoft" 2> /dev/null && vm="MS Hyper-V"
			vm_from="lscpu"
		fi
	fi

	#Check vmnet
	if [ -z ${vm} ]
	then
		if [ -e /dev/vmnet ]
		then
			vm="VMware"
			vm_from="/dev/vmnet"
		fi
	fi

	# Check dmesg Output
	if [ -z ${vm} ]
	then
		if [ -x "$(command -v dmesg 2>&1)" ]
                then
			dmesg_output=$(dmesg)
			echo ${dmesg_output} | grep -iqE "vboxbios|vboxcput|vboxfacp|vboxxsdt|(vbox cd-rom)|(vbox harddisk)" && vm="VirtualBox"
			echo ${dmesg_output} | grep -iqE "(vmware virtual ide)|(vmware pvscsi)|(vmware virtual platform)" && vm="VMware"
			echo ${dmesg_output} | grep -iqE "(xen_mem)|(xen-vbd)" && vm="Xen"
			echo ${dmesg_output} | grep -iqE "(qemu virtual cpu version)" && vm="Qemu/KVM"
			vm_from="dmesg"
		fi
	fi
}

#other startup checks

if [ x"$1" = "xcheck" ] || [ x"$1" = "xstart" ]
then
	scanProcesses
	for iface in $(ls -1 /sys/class/net)
	do
		if [ -f /sys/class/net/${iface}/uevent ]; then
			if $(grep -q DEVTYPE=wlan /sys/class/net/${iface}/uevent)
			then
				iface_list="${iface_list}\n ${iface}"
			fi
		fi
	done
	if [ -x "$(command -v iwconfig 2>&1)" ] && [ -x "$(command -v sort 2>&1)" ]
	then
		for iface in $(iwconfig 2> /dev/null | sed 's/^\([a-zA-Z0-9_.]*\) .*/\1/')
		do
			iface_list="${iface_list}\n ${iface}"
		done
	fi
	for iface in $(printf "${iface_list}" | sort -bu)
	do
		checkProcessesIface ${iface}
	done

	if [ x"$2" = "xkill" ]
	then
		scanProcesses "$2"
	fi
	if [ x"$1" = "xcheck" ]
	then
		exit
	fi
fi

if [ $# -ne "0" ]
then
	if [ x$1 != "xstart" ] && [ x$1 != "xstop" ]
	then
		usage
	fi

	if [ x$2 = "x" ]
	then
		usage
	fi
fi

#startup checks complete, headers then main

if [ "$VERBOSE" = "1" ]
then
	uname -a

	checkvm
	if [ ${vm} ]
	then
		printf "Detected VM using ${vm_from}\n"
		printf "This appears to be a ${vm} Virtual Machine\n"
		printf "If your system supports VT-d, it may be possible to use PCI devices\n"
		printf "If your system does not support VT-d, you can only use USB wifi cards\n"
	fi

	printf "\nK indicates driver is from $(uname -r)\n"
	if $(modinfo compat > /dev/null 2>&1)
	then
		modprobe compat
	fi
	if [ -f /sys/module/compat/parameters/compat_version ]
	then
		printf "C indicates driver is from $(cat /sys/module/compat/parameters/compat_version)\n"
	fi
	printf "V indicates driver comes directly from the vendor, almost certainly a bad thing\n"
	printf "S indicates driver comes from the staging tree, these drivers are meant for reference not actual use, BEWARE\n"
	printf "? indicates we do not know where the driver comes from... report this\n\n"
fi

if [ ${VERBOSE} = "1" ]
then
	printf "\nX[PHY]Interface\t\tDriver[Stack]-FirmwareRev\t\tChipset\t\t\t\t\t\t\t\t\t\tExtended Info\n\n"
else
	printf "PHY\tInterface\tDriver\t\tChipset\n\n"
fi

for iface in $(ifconfig -a 2> /dev/null | egrep UNSPEC | sed 's/^\([a-zA-Z0-9_]*\) .*/\1/')
do

	if [ -e "/proc/sys/dev/$iface/fftxqmin" ]
	then
		MADWIFI=1
		ifconfig $iface up
		printf "$iface\t\tAtheros\t\tmadwifi-ng"
		if [ x$1 = "xstart" ] && [ x$2 = x$iface ]
		then
			IFACE=$(wlanconfig ath create wlandev $iface wlanmode monitor -bssid | grep ath)
			ifconfig $iface up 2> /dev/null > /dev/null
			if [ $CH -lt 1000 ]
			then
				iwconfig $IFACE channel $CH 2> /dev/null > /dev/null
			else
				iwconfig $IFACE freq "$CH"000000 2> /dev/null > /dev/null
			fi
		ifconfig $IFACE up 2> /dev/null > /dev/null
		UDEV_ISSUE=$?
		fi

		if [ x$1 = "xstop" ] && [ x$2 = x$iface ]
		then
			printf "$iface does not support 'stop', do it on ath interface\n"
		fi

		#why, dear god why is there a random newline here?
		printf "\n"
		continue
	fi
done

	if [ ${MADWIFI} -eq 1 ]
	then
		sleep 1s
	fi

for iface in $(ls -1 /sys/class/net)
do
	if [ -f /sys/class/net/${iface}/uevent ]; then
		if $(grep -q DEVTYPE=wlan /sys/class/net/${iface}/uevent)
		then
			iface_list="${iface_list}\n ${iface}"
		fi
	fi
done
if [ -x "$(command -v iwconfig 2>&1)" ] && [ -x "$(command -v sort 2>&1)" ]
then
	for iface in $(iwconfig 2> /dev/null | sed 's/^\([a-zA-Z0-9_.]*\) .*/\1/')
	do
		iface_list="${iface_list}\n ${iface}"
	done
fi
for iface in $(printf "${iface_list}" | sort -bu)
do
	unset ethtool_output DRIVER FROM FIRMWARE STACK MADWIFI MAC80211 BUS BUSADDR BUSINFO DEVICEID CHIPSET EXTENDED PHYDEV ifacet DRIVERt FIELD1 FIELD1t FIELD2 FIELD2t CHIPSETt
	#add a RUNNING check here and up the device if it isn't already
	ethtool_output="$(ethtool -i $iface 2>&1)"
	if [ "$ethtool_output" != "Cannot get driver information: Operation not supported" ]
	then
		getStack  $iface
		getDriver   $iface
		getChipset $iface
		if [ ${VERBOSE} = "1" ]
		then
			getFrom $iface
			getFirmware $iface
			getExtendedInfo $iface
		fi
		getPhy     $iface
	else
 		printf "\nethtool failed...\n"
		printf "Only mac80211 devices on kernel 2.6.33 or higher are officially supported by airmon-ng.\n"
		exit 1
	fi

	#yes this really is the main output loop
	if [ ${VERBOSE} = "1" ]
	then
		#beautify output spacing (within reason)
		FIELD1="${FROM}[${PHYDEV}]${iface}"
		if [ ${#FIELD1} -gt 15 ]
		then
			FIELD1t="\t"
		else
			FIELD1t="\t\t"
		fi
		FIELD2="${DRIVER}[${STACK}]-${FIRMWARE}"
		if [ ${#FIELD2} -gt 27 ]
		then
			FIELD2t="\t"
		else
			FIELD2t="\t\t"
		fi
		if [ -n "${EXTENDED}" ]
		then
			CHIPSETt="\t\t\t\t\t\t\t\t\t\t"
			if [ ${#CHIPSET} -gt 70 ]
			then
				CHIPSETt="\t"
			elif [ ${#CHIPSET} -gt 63 ]
			then
				CHIPSETt="\t\t"
			elif [ ${#CHIPSET} -gt 56 ]
			then
				CHIPSETt="\t\t\t"
			elif [ ${#CHIPSET} -gt 49 ]
			then
				CHIPSETt="\t\t\t\t"
			elif [ ${#CHIPSET} -gt 42 ]
			then
				CHIPSETt="\t\t\t\t\t"
			elif [ ${#CHIPSET} -gt 35 ]
			then
				CHIPSETt="\t\t\t\t\t\t"
			elif [ ${#CHIPSET} -gt 28 ]
			then
				CHIPSETt="\t\t\t\t\t\t\t"
			elif [ ${#CHIPSET} -gt 21 ]
			then
				CHIPSETt="\t\t\t\t\t\t\t\t"
			elif [ ${#CHIPSET} -gt 14 ]
			then
				CHIPSETt="\t\t\t\t\t\t\t\t\t"
			fi
		fi
		printf "${FROM}[${PHYDEV}]${iface}${FIELD1t}${DRIVER}[${STACK}]-${FIRMWARE}${FIELD2t}${CHIPSET}${CHIPSETt}${EXTENDED}"
	else
		#beautify output spacing (within reason, interface/driver max length is 15 and phy max length is 7))
		if [ ${#DRIVER} -gt 7 ]
		then
			DRIVERt="\t"
		else
			DRIVERt="\t\t"
		fi
		if [ ${#iface} -gt 7 ]
		then
			ifacet="\t"
		else
			ifacet="\t\t"
		fi
		printf "${PHYDEV}\t${iface}${ifacet}${DRIVER}${DRIVERt}${CHIPSET}"
	fi

	if [ x$MAC80211 = "x1" ]
	then
		if [ x$1 = "xstart" ] && [ x$2 = x$iface ]
		then
			startMac80211Iface $iface
		fi

		if [ x$1 = "xstop" ] && [ x$2 = x$iface ]
		then
			stopMac80211Iface $iface
		fi

		#why, dear god why is there a random newline here?
		printf "\n"
		continue
	fi
done

#why, dear god why is there a random newline here?
printf "\n"

if [ $UDEV_ISSUE != 0 ]
then
	printf "udev renamed the interface. Read the following for a solution:\n"
	printf "http://www.aircrack-ng.org/doku.php?id=airmon-ng#interface_athx_number_rising_ath0_ath1_ath2...._ath45\n\n"
fi