forked from WLAN-Pi/profiler-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofiler.py
executable file
·917 lines (708 loc) · 31.9 KB
/
profiler.py
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
#!/usr/bin/python
# FIXME: Turn this script in to one or more modules to ensure we have a byte-code
# compiled verion of the script to reduce load times
########################################################################
# define fake AP parameters
CHANNEL = 36
SSID = 'WLAN Pi'
INTERFACE = 'wlan0'
#
# !!!!!!!!!!!!!! Do Not Touch Anything Below Here (Please) !!!!!!!!!!!!
#
########################################################################
# we must be root to run this script - exit with msg if not
import os
import sys
import socket
if not os.geteuid()==0:
print("\n#####################################################################################")
print("You must be root to run this script (use 'sudo profiler.py') - exiting" )
print("#####################################################################################\n")
sys.exit()
#import libraries
from fakeap import *
from fakeap.constants import *
from scapy.all import *
from scapy.layers.dot11 import *
from pymongo import MongoClient
import subprocess
from types import MethodType
import time
import csv
import getopt
from manuf import manuf
import ConfigParser
# FIXME: Move CLI processing up to here and perform conditional loading of modules to speed
# up load time. For example -h does not require all of these modules being loaded. I
# the fakeap and oui lookup modules are bad offenders for causing very slow load times
# for the script.
__author__ = 'Jerry Olla, Nigel Bowden, Kobe Watkins'
__version__ = '0.06'
__email__ = '[email protected]'
__status__ = 'beta'
# Switch off menu mode reporting by default
MENU_REPORTING = False
MENU_REPORT_FILE = '/tmp/profiler_menu_report.txt' # FIXME: Move this to the external config file
CLIENT_COUNT = 0
LAST_MANUF = ''
NO_AP = False
NO_FT = False
FT_REPORTING = True # Toggled by --no11r option
FILE_ONLY = False
HOSTNAME_SSID = False
CRUST_STORE = False
FILES_ROOT = '/var/www/html'
######################################################################################
# Figure out what our config file will be called (config.ini in same dir as script)
######################################################################################
config_file = os.path.dirname(os.path.realpath(__file__)) + "/config.ini"
# If file exists, read values in and over-ride the defaults at the top of this script
if os.path.isfile(config_file):
print("#####CONFIG FILE LOAD######\n")
config = ConfigParser.SafeConfigParser()
config.read(config_file)
if config.has_option('General', 'channel'):
CHANNEL = int(config.get('General', 'channel'))
print("CHANNEL: {}").format(CHANNEL)
if config.has_option('General', 'ssid'):
SSID = config.get('General', 'ssid')
print("SSID: {}").format(SSID)
if config.has_option('General', 'no11r'):
if config.get('General', 'no11r') == 'True':
FT_REPORTING = False
else:
FT_REPORTING = True
print("FT_REPORTING: {}").format(FT_REPORTING)
if config.has_option('General', 'noAP'):
if config.get('General', 'noAP') == 'True':
NO_AP = True
else:
NO_AP = False
print("NO_AP: {}").format(NO_AP)
if config.has_option('General', 'host_ssid'):
if config.get('General', 'host_ssid') == 'True':
HOSTNAME_SSID = True
else:
HOSTNAME_SSID = False
print("HOSTNAME_SSID: {}").format(HOSTNAME_SSID)
if config.has_option('General', 'crust'):
if config.get('General', 'crust') == 'True':
CRUST_STORE = True
else:
CRUST_STORE = False
print("CRUST_STORE: {}").format(CRUST_STORE)
if config.has_option('General', 'files_root'):
FILES_ROOT = config.get('General', 'files_root')
print("FILES_ROOT: {}").format(FILES_ROOT)
################################
# Set up working directories
################################
# Report & file dump directories
DIRS = {
'dump_dir': FILES_ROOT + '/files/profiler', # reporting root dir
'clients_dir': FILES_ROOT + '/files/profiler/clients', # client data dir
'reports_dir': FILES_ROOT + '/files/profiler/reports', # reports dir
}
# check if each dir exists, create if not
for dir_key in ['dump_dir', 'clients_dir', 'reports_dir']:
dest_dir = DIRS[dir_key]
if not os.path.isdir(dest_dir):
try:
os.mkdir(dest_dir)
except Exception as ex:
print("Trying to create directory: {} but having an issue: {}".format(dest_dir, ex))
print("Exiting...")
sys.exit()
# FIXME: Move these directory defitnitions in to the external config file
# for future flexibility
###################################################
# figure out the dest address for this SSH session
# (Gives us the IP address for the WLANPi Web GUI)
###################################################
try:
netstat_output = subprocess.check_output("netstat -tnpa | grep 'ESTABLISHED.*sshd'", shell=True)
dest_ip_re = re.search('(\d+?\.\d+?\.\d+?\.\d+?)\:22', netstat_output)
except Exception as ex:
print("Netstat for SSH session failed - expected when launched from front panel menu")
dest_ip_re = None
if dest_ip_re is None:
SSH_DEST_IP = False
else:
SSH_DEST_IP = dest_ip_re.group(1)
######################################
# assoc req frame tag list numbers
######################################
# power information
POWER_MIN_MAX_TAG = "33"
# channels supported by client
SUPPORTED_CHANNELS_TAG = "36"
# 802.11n support info
HT_CAPABILITIES_TAG = "45"
# 802.11w support info
RSN_CAPABILITIES_TAG = "48"
# 802.11r support info
FT_CAPABILITIES_TAG = "54"
# 802.11k support info
RM_CAPABILITIES_TAG = "70"
# 802.11v
EXT_CAPABILITIES_TAG = "127"
# 802.11ac support info
VHT_CAPABILITIES_TAG = "191"
# Extended Tags
EXT_IE_TAG = "255"
# list of detected clients
detected_clients = []
# get our start time for csv timestamp
time_now = time.strftime("%Y-%m-%d-%H-%M-%S")
# build csv filename
csv_file = DIRS['reports_dir'] + '/db-' + time_now + '.csv'
# Setup up our MAC OUI lookup
oui_lookup = manuf.MacParser(manuf_name="/usr/local/lib/python2.7/dist-packages/manuf/manuf", update=False)
# FIXME: Suspect this module causes extended load times as it loads the whole
# OUI db file each time. Need to moved to solution to read the DB in to
# an sqlite DB the first time the script runs. Then just look up OUIs
# as required. Adding a cache for popular OUIs in addition could also
# help. Would need to check the timestamp of the OUI db file each time
# check if it has changed since he last run in case we need to update
# the sqlite DB.
##################
# Functions
##################
# FIXME: Add some comments to describe the function and operation of each
# of these functions
def generate_menu_report(CHANNEL, FT_REPORTING, SSID, CLIENT_COUNT, LAST_MANUF):
global MENU_REPORT_FILE
status = 'Status: running\r'
channel = "Ch:{} 11r:{}\r".format(CHANNEL, FT_REPORTING)
ssid = "SSID: {}\r".format(SSID)
clients = "Clients:{} ({})".format(CLIENT_COUNT, LAST_MANUF)
# create file and dump info
f = open(MENU_REPORT_FILE, "w")
f.writelines([status, channel, ssid, clients])
f.close()
def analyze_frame_cb(self, packet):
analyze_frame(packet)
def analyze_frame(packet):
global CLIENT_COUNT
global LAST_MANUF
global FT_REPORTING
# If we're not running the fake AP, make sure we only let through assoc req frames
if NO_AP == True:
if not packet.haslayer(Dot11):
return(False)
if not packet.haslayer(Dot11AssoReq):
return(False)
# pull off the RadioTap, Dot11 & Dot11AssoReq layers
dot11 = packet.payload
frame_src_addr = dot11.addr2
if frame_src_addr in detected_clients:
# already analysed this client, moving on
print("Detected " + str(frame_src_addr) + " again, ignoring..." )
return(False)
CLIENT_COUNT +=1
# add client to detected clients list
detected_clients.append(frame_src_addr)
# lookup client OUI
mac_oui_manuf = oui_lookup.get_manuf(frame_src_addr)
LAST_MANUF = mac_oui_manuf
# get mac address in dashed format to use later
mac_addr = frame_src_addr.replace(':', '-', 5)
# create a dir to dump client data into
client_dir = DIRS['clients_dir'] + '/' + mac_addr
if not os.path.isdir(client_dir):
try:
os.mkdir(client_dir)
except Exception as ex:
print("Trying to create directory: {} but having an issue: {}".format(client_dir, ex))
print("Exiting...")
sys.exit()
# dump out the frame to a file
dump_filename = client_dir + '/' + mac_addr + '.pcap'
wrpcap(dump_filename, [packet])
capabilites = dot11.getfieldval("cap")
dot11_assoreq = dot11.payload.payload
dot11_elt = dot11_assoreq
# common dictionary to store all tag lists
dot11_elt_dict = {}
# analyse the 802.11 frame tag lists & store in a dictionary
while dot11_elt:
# get tag number
dot11_elt_id = str(dot11_elt.ID)
# get tag list
dot11_elt_info = dot11_elt.getfieldval("info")
# covert tag list in to useable format (decimal list of values)
dec_array = map(ord, str(dot11_elt_info))
#hex_array = map(hex, dec_array)
# store each tag list in a common tag dictionary
dot11_elt_dict[dot11_elt_id] = dec_array
# move to next layer - end of while loop
dot11_elt = dot11_elt.payload
# dictionary to store capabilities as we decode them
capability_dict = {}
capability_dict_db = {}
# check if 11n supported
if HT_CAPABILITIES_TAG in dot11_elt_dict.keys():
capability_dict['802.11n'] = 'Supported'
capability_dict_db['802.11n'] = 1
spatial_streams = 0
# mcs octets 1 - 4 indicate # streams supported (up to 4 streams only)
for mcs_octet in range(3, 7):
mcs_octet_value = dot11_elt_dict[HT_CAPABILITIES_TAG][mcs_octet]
if (mcs_octet_value & 255):
spatial_streams += 1
capability_dict['802.11n'] = 'Supported (' + str(spatial_streams) + 'ss)'
capability_dict_db['802.11n_ss'] = spatial_streams
else:
capability_dict['802.11n'] = 'Not reported*'
capability_dict_db['802.11n'] = 0
capability_dict_db['802.11n_ss'] = 0
# check if 11ac supported
if VHT_CAPABILITIES_TAG in dot11_elt_dict.keys():
# Check for number streams supported
mcs_upper_octet = dot11_elt_dict[VHT_CAPABILITIES_TAG][5]
mcs_lower_octet = dot11_elt_dict[VHT_CAPABILITIES_TAG][4]
mcs_rx_map = (mcs_upper_octet * 256) + mcs_lower_octet
# define the bit pair we need to look at
spatial_streams = 0
stream_mask = 3
# move through each bit pair & test for '10' (stream supported)
for mcs_bits in range(1,9):
if (mcs_rx_map & stream_mask) != stream_mask:
# stream mask bits both '1' when mcs map range not supported
spatial_streams += 1
# shift to next mcs range bit pair (stream)
stream_mask = stream_mask * 4
vht_support = 'Supported (' + str(spatial_streams) + 'ss)'
capability_dict_db['802.11ac'] = 1
capability_dict_db['802.11ac_ss'] = spatial_streams
# check for SU & MU beam formee support
mu_octet = dot11_elt_dict[VHT_CAPABILITIES_TAG][2]
su_octet = dot11_elt_dict[VHT_CAPABILITIES_TAG][1]
beam_form_mask = 16
# bit 4 indicates support for both octets (1 = supported, 0 = not supported)
if (su_octet & beam_form_mask):
vht_support += ", SU BF supported"
capability_dict_db['802.11ac_su_bf'] = 1
else:
vht_support += ", SU BF not supported"
capability_dict_db['802.11ac_su_bf'] = 0
if (mu_octet & beam_form_mask):
vht_support += ", MU BF supported"
capability_dict_db['802.11ac_mu_bf'] = 1
else:
vht_support += ", MU BF not supported"
capability_dict_db['802.11ac_mu_bf'] = 0
capability_dict['802.11ac'] = vht_support
else:
capability_dict['802.11ac'] = 'Not reported*'
capability_dict_db['802.11ac'] = 0
capability_dict_db['802.11ac_ss'] = 0
capability_dict_db['802.11ac_su_bf'] = 0
capability_dict_db['802.11ac_mu_bf'] = 0
# check if 11k supported
if RM_CAPABILITIES_TAG in dot11_elt_dict.keys():
capability_dict['802.11k'] = 'Supported'
capability_dict_db['802.11k'] = 1
else:
capability_dict['802.11k'] = 'Not reported* - treat with caution, many clients lie about this'
capability_dict_db['802.11k'] = 0
# check if 11r supported
global FT_REPORTING
if FT_REPORTING == False:
capability_dict['802.11r'] = 'Reporting disabled (--no11r option used)'
elif FT_CAPABILITIES_TAG in dot11_elt_dict.keys():
capability_dict['802.11r'] = 'Supported'
capability_dict_db['802.11r'] = 1
else:
capability_dict['802.11r'] = 'Not reported*'
capability_dict_db['802.11r'] = 0
# check if 11v supported
capability_dict['802.11v'] = 'Not reported*'
capability_dict_db['802.11v'] = 0
if EXT_CAPABILITIES_TAG in dot11_elt_dict.keys():
ext_cap_list = dot11_elt_dict[EXT_CAPABILITIES_TAG]
# check octet 3 exists
if 3 <= len(ext_cap_list):
# bit 4 of octet 3 in the extended capabilites field
octet3 = ext_cap_list[2]
bss_trans_support = int('00001000', 2)
# 'And' octet 3 to test for bss transition support
if octet3 & bss_trans_support:
capability_dict['802.11v'] = 'Supported'
capability_dict_db['802.11v'] = 1
# check if 11w supported
capability_dict['802.11w'] = 'Not reported'
capability_dict_db['802.11w'] = 0
if RSN_CAPABILITIES_TAG in dot11_elt_dict.keys():
rsn_cap_list = dot11_elt_dict[RSN_CAPABILITIES_TAG]
rsn_len = len(rsn_cap_list) - 2
pmf_oct = rsn_cap_list[rsn_len]
# bit 8 of 2nd last octet in the rsn capabilites field
if 127 <= pmf_oct:
capability_dict['802.11w'] = 'Supported'
capability_dict_db['802.11w'] = 1
# check if power capabilites supported
capability_dict['Max_Power'] = 'Not reported'
capability_dict_db['max_power'] = 'Not reported'
capability_dict['Min_Power'] = 'Not reported'
capability_dict_db['min_power'] = 'Not reported'
if POWER_MIN_MAX_TAG in dot11_elt_dict.keys():
# octet 3 of power capabilites
max_power = dot11_elt_dict[POWER_MIN_MAX_TAG][1]
min_power = dot11_elt_dict[POWER_MIN_MAX_TAG][0]
# check if signed
if min_power > 127:
signed_min_power = (256-min_power) * (-1)
else:
signed_min_power = min_power
capability_dict['Max_Power'] = str(max_power) + " dBm"
capability_dict_db['max_power'] = max_power
capability_dict['Min_Power'] = str(signed_min_power) + " dBm"
capability_dict_db['min_power'] = signed_min_power
# check supported channels
if SUPPORTED_CHANNELS_TAG in dot11_elt_dict.keys():
channel_sets_list = dot11_elt_dict[SUPPORTED_CHANNELS_TAG]
channel_list = []
while (channel_sets_list):
start_channel = channel_sets_list.pop(0)
channel_range = channel_sets_list.pop(0)
# check for if 2.4Ghz or 5GHz
if start_channel > 14:
channel_multiplier = 4
else:
channel_multiplier = 1
for i in range(channel_range):
channel_list.append(start_channel + (i * channel_multiplier))
capability_dict['Supported_Channels'] = ', '.join(map(str, channel_list))
capability_dict_db['Supported_Channels'] = channel_list
else:
capability_dict['Supported_Channels'] = "Not reported"
# check for Ext tags (e.g. 802.11ax draft support)
capability_dict['802.11ax_draft'] = 'Not Supported'
capability_dict_db['802.11ax_draft'] = 0
if EXT_IE_TAG in dot11_elt_dict.keys():
ext_ie_id = str(dot11_elt_dict[EXT_IE_TAG][0])
dot11ax_draft_ids = { "35": "802.11ax (Draft)"}
# check for 802.11ax support
if ext_ie_id in dot11ax_draft_ids.keys():
capability_dict['802.11ax_draft'] = 'Supported (Draft)'
capability_dict_db['802.11ax_draft'] = 1
# FIXME: Need to add more 11ax detection features and add them in to the
# report. For example: support for OFDMA UL, OFDMA DL, MU-MIMO UL
# MU-MIMO DL, BSS Colouring etc.
# print our report to stdout
text_report(frame_src_addr, mac_oui_manuf, capability_dict, mac_addr, client_dir, csv_file)
# if we're running in menu mode, create temp report
if MENU_REPORTING == True:
generate_menu_report(CHANNEL, FT_REPORTING, SSID, CLIENT_COUNT, LAST_MANUF)
# add to wlanpi-crust
if CRUST_STORE == True:
db_report(frame_src_addr, mac_oui_manuf, capability_dict_db, mac_addr)
return True
def PktHandler(frame):
analyze_frame(frame)
def db_report(frame_src_addr, mac_oui_manuf, capability_dict_db, mac_addr):
client = MongoClient()
db = client.wlanpi
insert_data = {
"mac_addr" : mac_addr,
"mac_oui_manuf" : mac_oui_manuf,
"802_11n" : capability_dict_db['802.11n'],
"802_11n_ss" : capability_dict_db['802.11n_ss'],
"802_11ac" : capability_dict_db['802.11ac'],
"802_11ac_mu_bf" : capability_dict_db['802.11ac_mu_bf'],
"802_11ac_su_bf" : capability_dict_db['802.11ac_su_bf'],
"802_11ac_ss" : capability_dict_db['802.11ac_ss'],
"802_11ax_draft" : capability_dict_db['802.11ax_draft'],
"802_11w" : capability_dict_db['802.11w'],
"802_11k" : capability_dict_db['802.11k'],
"802_11r" : capability_dict_db['802.11r'],
"802_11v" : capability_dict_db['802.11v'],
"max_power" : capability_dict_db['max_power'],
"min_power" : capability_dict_db['min_power']
}
for channel in capability_dict_db['Supported_Channels']:
insert_data["channel_" + str(channel)] = 1
inserted_id = db.profiler_results.insert_one(insert_data).inserted_id
print("\t\tAdded result to database. objectid={0}".format(inserted_id))
return True
def text_report(frame_src_addr, mac_oui_manuf, capability_dict, mac_addr, client_dir, csv_file):
report_text = ''
# start report
report_text += '\n'
report_text += '-' * 60
report_text += "\nClient capabilities report - Client MAC: " + frame_src_addr + "\n"
report_text += "(OUI manufacturer lookup: " + (mac_oui_manuf or "Unknown") + ")\n"
report_text += '-' * 60
report_text += '\n'
# print out capabilities (in nice format)
capabilities = ['802.11k', '802.11r', '802.11v', '802.11w', '802.11n', '802.11ac', '802.11ax_draft', 'Max_Power', 'Min_Power', 'Supported_Channels']
for key in capabilities:
report_text += "{:<20} {:<20}".format(key, capability_dict[key]) + "\n"
report_text += "\n\n" + "* Reported client capabilities are dependent on these features being available from the wireless network at time of client association\n\n"
print(report_text)
# print results URL
global SSH_DEST_IP
if SSH_DEST_IP:
print("[View PCAP & Client Report : http://{}/profiler/clients/{} ]\n".format(SSH_DEST_IP, mac_addr))
# dump out the text to a file
dump_filename = client_dir + '/' + mac_addr + '.txt'
try:
f = open(dump_filename, 'w')
f.write(report_text)
f.close()
except Exception as ex:
print("Error creating file to dump client info ({}):".format(dump_filename))
print(ex)
sys.exit()
# Check if csv file exists
if not os.path.exists(csv_file):
# create file with csv headers
with open(csv_file, mode='w') as file_obj:
writer = csv.DictWriter(file_obj, fieldnames=['Client_Mac'] + ['OUI_Manuf'] + capabilities)
writer.writeheader()
# append data to csv file
with open(csv_file, mode='a') as file_obj:
writer = csv.DictWriter(file_obj, fieldnames=['Client_Mac'] + ['OUI_Manuf'] + capabilities)
writer.writerow({
'Client_Mac': frame_src_addr,
'OUI_Manuf': mac_oui_manuf,
'802.11k': capability_dict['802.11k'],
'802.11r': capability_dict['802.11r'],
'802.11v': capability_dict['802.11v'],
'802.11w': capability_dict['802.11w'],
'802.11n': capability_dict['802.11n'],
'802.11ac': capability_dict['802.11ac'],
'802.11ax_draft': capability_dict['802.11ax_draft'],
'Max_Power': capability_dict['Max_Power'],
'Min_Power': capability_dict['Min_Power'],
'Supported_Channels': capability_dict['Supported_Channels']
})
return True
def get_ie(ap_channel, ap_ssid, ft=True):
#Added Information element tags here
p= Dot11Elt(ID='SSID', info=ap_ssid) #SSID
p=p / Dot11Elt(ID='Rates', info="\x8c\x12\x98\x24\xb0\x48\x60\x6c") #Supported Data Rates
p=p / Dot11Elt(ID=0x46, info="\x02\x00\x00\x00\x00") #RM Enabled Capabilties
if ft:
p=p / Dot11Elt(ID=0x36, info="\x45\xc2\x00") #Mobility Domain(802.11r/FT enabled)
p=p / Dot11Elt(ID=0x30, info="\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x02\x00\x00\x0f\xac\x02\x00\x0f\xac\x04\x8c\x00") #RSN FT Enabled
else:
p=p / Dot11Elt(ID=0x30, info="\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x02\x80\x00") #RSN FT disabled
p=p / Dot11Elt(ID=0xdd, info="\x00\x50\xf2\x02\x01\x01\x8a\x00\x03\xa4\x00\x00\x27\xa4\x00\x00\x42\x43\x5e\x00\x62\x32\x2f\x00") #Vendor specific MS corp. WMM/WME:parameter element
p=p / Dot11Elt(ID=0x2d, info="\xef\x19\x1b\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x20\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00") #HT Capabilities
p=p / Dot11Elt(ID=0x3d, info=chr(ap_channel) + "\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00") #HT Information
p=p / Dot11Elt(ID=0x7f, info="\x00\x00\x08\x00\x00\x00\x00\x40") #Extended Capatibilities
p=p / Dot11Elt(ID=0xbf, info="\x32\x00\x80\x03\xaa\xff\x00\x00\xaa\xff\x00\x00") #VHT Capabilties
p=p / Dot11Elt(ID=0xc0, info="\x00\x24\x00\x00\x00") #VHT Operation
p=p / Dot11Elt(ID=0xff, info="\x23\x09\x01\x00\x02\x40\x00\x04\x70\x0c\x80\x02\x03\x80\x04\x00\x00\x00\xaa\xff\xaa\xff\x7b\x1c\xc7\x71\x1c\xc7\x71\x1c\xc7\x71\x1c\xc7\x71") # HE Capabilties
p=p / Dot11Elt(ID=0xff, info="\x24\xf4\x3f\x00\x19\xfc\xff") #HE Operation
return p
def my_dot11_probe_resp(self, source, ssid):
# Create probe response packet
probe_response_packet = self.ap.get_radiotap_header() \
/ Dot11(subtype=5, addr1=source, addr2=self.ap.mac, addr3=self.ap.mac, SC=self.ap.next_sc()) \
/ Dot11ProbeResp(timestamp=self.ap.current_timestamp(), beacon_interval=0x0064, cap=0x1111) \
#Update information elements
ft = self.ap.ft
probe_response_packet = probe_response_packet /get_ie(self.ap.channel, ssid, ft)
#Send probe response
self.ap.s2.send(probe_response_packet)
def my_dot11_beacon(self, ssid):
# Create beacon packet
beacon_packet = self.ap.get_radiotap_header() \
/ Dot11(subtype=8, addr1='ff:ff:ff:ff:ff:ff', addr2=self.ap.mac, addr3=self.ap.mac) \
/ Dot11Beacon(beacon_interval=0x0064, cap=0x1111) \
/ Dot11Elt(ID='TIM', info="\x05\x04\x00\x03\x00\x00") \
#Update information elements
ft = self.ap.ft
beacon_packet = beacon_packet / get_ie(self.ap.channel, ssid, ft)
# Update sequence number
beacon_packet.SC = self.ap.next_sc()
# Update timestamp
beacon_packet[Dot11Beacon].timestamp = self.ap.current_timestamp()
# Send beacon
self.ap.s1.send(beacon_packet)
def run_fakeap(ap_interface, ap_ssid, ap_channel, ft):
ap = FakeAccessPoint(ap_interface, ap_ssid)
ap.wpa = AP_WLAN_TYPE_WPA2 # Enable WPA2
my_callbacks = Callbacks(ap)
my_callbacks.cb_recv_pkt = MethodType(my_recv_pkt, my_callbacks)
my_callbacks.cb_analyze_frame = MethodType(analyze_frame_cb, my_callbacks)
my_callbacks.cb_dot11_beacon = MethodType(my_dot11_beacon , my_callbacks)
my_callbacks.cb_dot11_probe_req = MethodType(my_dot11_probe_resp , my_callbacks)
ap.callbacks = my_callbacks
# signal whether to use ft IE or not
ap.ft = ft
# set fake AP channel
ap.channel = ap_channel
# lower the beacon interval used to account for execution time of script
ap.beaconTransmitter.interval = 0.020
ap.run()
def run_msg(ap_interface, ap_ssid, ap_channel):
global SSH_DEST_IP
if NO_AP == True:
print("\n" + "-" * 44)
print("Listening for association frames...")
print("Channel: {}".format(ap_channel))
print("Interface: {}".format(ap_interface))
if SSH_DEST_IP:
print("Results: http://{}/profiler/".format(SSH_DEST_IP))
print("-" * 44 + '\n')
else:
print("\n" + "-" * 44)
print("Profiler AP Started \n")
print("SSID: {} ".format(ap_ssid))
print("PSK: 12345678")
print("Channel: {}".format(ap_channel))
print("Interface: {}".format(ap_interface))
if SSH_DEST_IP:
print("Results: http://{}/profiler/".format(SSH_DEST_IP))
print("-" * 44)
print("\n####################################################################################################")
print("Connect a Wi-Fi client to SSID:",ap_ssid, "enter any random 8 characters for the PSK")
print("we don't really need the device to associate, we only need get the client to send an association request")
print("####################################################################################################\n")
def my_recv_pkt(self, packet): # We override recv_pkt to include a trigger for our callback
if packet.haslayer(Dot11AssoReq):
self.cb_analyze_frame(packet)
self.recv_pkt(packet)
def usage():
print("\nProfiler verison : {}".format(__version__))
print("\n Usage:\n")
print(" profiler.py")
print(' profiler.py [ -c <channel num> ] [ -s "SSID Name" ] [ -i interface_name ] [ --no11r ]')
print(' profiler.py --noAP -c <channel num>')
print(" profiler.py -f <pcap filename>")
print(" profiler.py -h")
print(" profiler.py -v")
print(" profiler.py --help")
print(" profiler.py --clean (Clean out old CSV reports)")
print ("\n Command line options:\n")
print(" -h Shows help")
print(" -c Sets channel for fake AP")
print(" -s Sets name of fake AP SSID")
print(" -i Sets name of fake AP wireless interface on WLANPi")
print(" -f Read pcap file of assoc frame")
print(" -h Prints help page")
print(" --no11r Disables 802.11r information elements")
print(" --noAP Disables fake AP and just listens for assoc req frames")
print(" --host_ssid Use the WLANPI hostname as SSID")
print(" --crust Use the WLANPI-crust datastore")
print(" --help Prints help page")
print(" --clean Cleans out all CSV report files\n\n")
sys.exit()
def report_cleanup():
global DIRS
reports_dir = DIRS['reports_dir']
for file in os.listdir(reports_dir):
try:
print("Removing old file: " + file)
os.unlink(reports_dir + "/" + file)
except Exception as ex:
print("Issue removing file:" + str(ex))
sys.exit()
def main():
##########################
# Process CLI parameters
##########################
ap_interface = INTERFACE
ap_ssid = SSID
ap_channel = CHANNEL
ft = True
global FT_REPORTING
global MENU_REPORTING
global MENU_REPORT_FILE
global CLIENT_COUNT
global NO_AP
global FILE_ONLY
global HOSTNAME_SSID
global CRUST_STORE
# Default action run fakeap & analyze assoc req frames
if len(sys.argv) < 2:
# fall through to run ap with default parameters
pass
# Process CLI parameters if we have any
elif len(sys.argv) >= 2:
try:
opts, args = getopt.getopt(sys.argv[1:],'c:s:i:f:hv', ['no11r', 'clean', 'help', 'menu_mode', 'noAP','host_ssid','crust'])
except getopt.GetoptError:
print("\nOops...syntax error, please re-check: \n")
usage()
for opt, arg in opts:
if opt == '-h':
usage()
elif opt == ("--help"):
usage()
elif opt == ("-v"):
print("\nProfiler version: {}\n".format(__version__))
sys.exit()
elif opt == ("--clean"):
report_cleanup()
elif opt in ("-c"):
ap_channel = int(arg)
elif opt in ("-s"):
ap_ssid = str(arg)
elif opt in ("-i"):
ap_interface = str(arg)
elif opt in ("--no11r"):
ft = False
FT_REPORTING = False
elif opt in ("--menu_mode"):
MENU_REPORTING = True
elif opt in ("--noAP"):
NO_AP = True
elif opt in ("-f"):
FILE_ONLY = True
pcap_file = str(arg)
elif opt in ("--host_ssid"):
HOSTNAME_SSID = True
elif opt in ("--crust"):
CRUST_STORE = True
else:
usage()
# FIXME: Move the CLI parameters processing to the start of the script to
# allow conditional loading of modules. Not all options require fake
# AP to be loaded (as an example). Once options have been processed,
# set global variables to check modeules/options required (many of
# these already exist)
##########################
# set up the WLAN adapter
##########################
if_cmds = [
'airmon-ng check kill',
'ifconfig {} down'.format(ap_interface),
'iwconfig {} mode monitor'.format(ap_interface),
'ifconfig {} up'.format(ap_interface),
'iw {} set channel '.format(ap_interface) + str(ap_channel)
]
# run WLAN adapter setup commands & check for failures
for cmd in if_cmds:
try:
subprocess.check_output(cmd + " 2>&1", shell=True)
except Exception as ex:
print("Error setting wlan interface config:")
print(ex)
sys.exit()
if HOSTNAME_SSID == True:
ap_ssid = socket.gethostname()
# initialize the menu report file if required
if MENU_REPORTING == True:
generate_menu_report(CHANNEL, FT_REPORTING, ap_ssid, CLIENT_COUNT, 'N/A')
if NO_AP == True:
# no fake AP, just listen for assoc req frames
run_msg(ap_interface, "", ap_channel)
sniff(iface=ap_interface, prn=PktHandler)
elif FILE_ONLY == True:
# read in the pcap file
frame = rdpcap(pcap_file)
# extract the first frame object
assoc_req_frame = frame[0]
# perform analysis
analyze_frame(assoc_req_frame)
else:
# run the fakeap
run_msg(ap_interface, ap_ssid, ap_channel)
run_fakeap(ap_interface, ap_ssid, ap_channel, ft)
if __name__ == "__main__":
main()