forked from ghostop14/sparrow-wifi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wirelessengine.py
executable file
·849 lines (692 loc) · 31 KB
/
wirelessengine.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
#!/usr/bin/python3
#
# Copyright 2017 ghostop14
#
# This is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this software; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
#
import os
import subprocess
import re
import datetime
from dateutil import parser
import json
import copy
from time import sleep
from sparrowgps import SparrowGPS
from sparrowcommon import stringtobool
# ------------------ Global channel to frequency definitions ------------------------------
channelToFreq = {}
channelToFreq['1'] = '2412'
channelToFreq['2'] = '2417'
channelToFreq['3'] = '2422'
channelToFreq['4'] = '2427'
channelToFreq['5'] = '2432'
channelToFreq['6'] = '2437'
channelToFreq['7'] = '2442'
channelToFreq['8'] = '2447'
channelToFreq['9'] = '2452'
channelToFreq['10'] = '2457'
channelToFreq['11'] = '2462'
channelToFreq['12'] = '2467'
channelToFreq['13'] = '2472'
channelToFreq['14'] = '2484'
channelToFreq['16'] = '5080'
channelToFreq['34'] = '5170'
channelToFreq['36'] = '5180'
channelToFreq['38'] = '5190'
channelToFreq['40'] = '5200'
channelToFreq['42'] = '5210'
channelToFreq['44'] = '5220'
channelToFreq['46'] = '5230'
channelToFreq['48'] = '5240'
channelToFreq['50'] = '5250'
channelToFreq['52'] = '5260'
channelToFreq['54'] = '5270'
channelToFreq['56'] = '5280'
channelToFreq['58'] = '5290'
channelToFreq['60'] = '5300'
channelToFreq['62'] = '5310'
channelToFreq['64'] = '5320'
channelToFreq['100'] = '5500'
channelToFreq['102'] = '5510'
channelToFreq['104'] = '5520'
channelToFreq['106'] = '5530'
channelToFreq['108'] = '5540'
channelToFreq['110'] = '5550'
channelToFreq['112'] = '5560'
channelToFreq['114'] = '5570'
channelToFreq['116'] = '5580'
channelToFreq['118'] = '5590'
channelToFreq['120'] = '5600'
channelToFreq['122'] = '5610'
channelToFreq['124'] = '5620'
channelToFreq['126'] = '5630'
channelToFreq['128'] = '5640'
channelToFreq['132'] = '5660'
channelToFreq['134'] = '5670'
channelToFreq['136'] = '5680'
channelToFreq['138'] = '5690'
channelToFreq['140'] = '5700'
channelToFreq['142'] = '5710'
channelToFreq['144'] = '5720'
channelToFreq['149'] = '5745'
channelToFreq['151'] = '5755'
channelToFreq['153'] = '5765'
channelToFreq['155'] = '5775'
channelToFreq['157'] = '5785'
channelToFreq['159'] = '5795'
channelToFreq['161'] = '5805'
channelToFreq['165'] = '5825'
channelToFreq['169'] = '5845'
channelToFreq['173'] = '5865'
channelToFreq['183'] = '4915'
channelToFreq['184'] = '4920'
channelToFreq['185'] = '4925'
channelToFreq['187'] = '4935'
channelToFreq['188'] = '4940'
channelToFreq['189'] = '4945'
channelToFreq['192'] = '4960'
channelToFreq['196'] = '4980'
# ------------------ WirelessNetwork class ------------------------------------
class WirelessClient(object):
def __init__(self):
self.macAddr = ""
self.apMacAddr = ""
self.ssid = ""
self.channel = 0
self.signal = -1000 # dBm
now=datetime.datetime.now()
self.firstSeen = now
self.lastSeen = now
self.gps = SparrowGPS()
self.strongestsignal = self.signal
self.strongestgps = SparrowGPS()
self.probedSSIDs = []
# Used for tracking in network table
self.foundInList = False
def __str__(self):
retVal = ""
retVal += "MAC Address: " + self.macAddr + "\n"
retVal += "Associated Access Point Mac Address: " + self.apMacAddr + "\n"
retVal += "SSID: " + self.ssid + "\n"
retVal += "Channel: " + str(self.channel) + "\n"
retVal += "Signal: " + str(self.signal) + " dBm\n"
retVal += "Strongest Signal: " + str(self.strongestsignal) + " dBm\n"
retVal += "First Seen: " + str(self.firstSeen) + "\n"
retVal += "Last Seen: " + str(self.lastSeen) + "\n"
retVal += "Probed SSIDs:"
if (len(self.probedSSIDs) > 0):
for curSSID in self.probedSSIDs:
retVal += " " + curSSID
retVal += "\n"
else:
retVal += " No probes observed\n"
retVal += "Last GPS:\n"
retVal += str(self.gps)
retVal += "Strongest GPS:\n"
retVal += str(self.strongestgps)
return retVal
def copy(self):
return copy.deepcopy(self)
def __eq__(self, obj):
# This is equivance.... ==
if not isinstance(obj, WirelessClient):
return False
if self.macAddr != obj.macAddr:
return False
if self.apMacAddr != obj.apMacAddr:
return False
return True
def __ne__(self, other):
return not self.__eq__(other)
def getKey(self):
return self.macAddr
def associated(self):
if len(self.apMacAddr) == 0 or (self.apMacAddr == "(not associated)"):
return False
return True
def createFromJsonDict(jsondict):
retVal = WirelessClient()
retVal.fromJsondict(jsondict)
return retVal
def fromJsondict(self, dictjson):
# Note: if the json dictionary isn't correct, this will naturally throw an exception that may
# need to be caught for error detection
self.macAddr = dictjson['macAddr']
self.apMacAddr = dictjson['apMacAddr']
self.ssid = dictjson['ssid']
self.channel = int(dictjson['channel'])
self.signal = int(dictjson['signal'])
self.strongestsignal = int(dictjson['strongestsignal'])
self.firstSeen = parser.parse(dictjson['firstseen'])
self.lastSeen = parser.parse(dictjson['lastseen'])
self.gps.latitude = float(dictjson['lat'])
self.gps.longitude = float(dictjson['lon'])
self.gps.altitude = float(dictjson['alt'])
self.gps.speed = float(dictjson['speed'])
self.gps.isValid = stringtobool(dictjson['gpsvalid'])
self.strongestgps.latitude = float(dictjson['strongestlat'])
self.strongestgps.longitude = float(dictjson['strongestlon'])
self.strongestgps.altitude = float(dictjson['strongestalt'])
self.strongestgps.speed = float(dictjson['strongestspeed'])
self.strongestgps.isValid = stringtobool(dictjson['strongestgpsvalid'])
self.probedSSIDs = dictjson['probedssids']
def fromJson(self, jsonstr):
dictjson = json.loads(jsonstr)
self.fromJsondict(dictjson)
def toJson(self):
dictjson = self.toJsondict()
return json.dumps(dictjson)
def toJsondict(self):
dictjson = {}
dictjson['type'] = 'wifi-client'
dictjson['macAddr'] = self.macAddr
dictjson['apMacAddr'] = self.apMacAddr
dictjson['ssid'] = self.ssid
dictjson['channel'] = self.channel
dictjson['signal'] = self.signal
dictjson['firstseen'] = str(self.firstSeen)
dictjson['lastseen'] = str(self.lastSeen)
dictjson['lat'] = str(self.gps.latitude)
dictjson['lon'] = str(self.gps.longitude)
dictjson['alt'] = str(self.gps.altitude)
dictjson['speed'] = str(self.gps.speed)
dictjson['gpsvalid'] = str(self.gps.isValid)
dictjson['strongestsignal'] = self.strongestsignal
dictjson['strongestlat'] = str(self.strongestgps.latitude)
dictjson['strongestlon'] = str(self.strongestgps.longitude)
dictjson['strongestalt'] = str(self.strongestgps.altitude)
dictjson['strongestspeed'] = str(self.strongestgps.speed)
dictjson['strongestgpsvalid'] = str(self.strongestgps.isValid)
dictjson['probedssids'] = self.probedSSIDs
return dictjson
class WirelessNetwork(object):
ERR_NETDOWN = 156
ERR_OPNOTSUPPORTED = 161
ERR_DEVICEBUSY = 240
ERR_OPNOTPERMITTED = 255
def __init__(self):
self.macAddr = ""
self.ssid = ""
self.mode = "" # master, managed, monitor, etc.
self.security = "Open" # on or off
self.privacy = "None" # group cipher
self.cipher = "" # pairwise cipher
self.channel = 0 # Channel #
self.frequency = 0
self.signal = -1000 # dBm
self.bandwidth = 20 # Default to 20. we'll bump it up as we see params. max BW in any protocol 20 or 40 or 80 or 160 MHz
self.secondaryChannel = 0 # used for 40+ MHz
self.thirdChannel = 0 # used for 80+ MHz channels
self.secondaryChannelLocation = '' # above/below
now=datetime.datetime.now()
self.firstSeen = now
self.lastSeen = now
self.gps = SparrowGPS()
self.strongestsignal = self.signal
self.strongestgps = SparrowGPS()
# Used for tracking in network table
self.foundInList = False
super().__init__()
def __str__(self):
retVal = ""
retVal += "MAC Address: " + self.macAddr + "\n"
retVal += "SSID: " + self.ssid + "\n"
retVal += "Mode: " + self.mode + "\n"
retVal += "Security: " + self.security + "\n"
retVal += "Privacy: " + self.privacy + "\n"
retVal += "Cipher: " + self.cipher + "\n"
retVal += "Frequency: " + str(self.frequency) + " MHz\n"
retVal += "Channel: " + str(self.channel) + "\n"
retVal += "Secondary Channel: " + str(self.secondaryChannel) + "\n"
retVal += "Secondary Channel Location: " + self.secondaryChannelLocation + "\n"
retVal += "Third Channel: " + str(self.thirdChannel) + "\n"
retVal += "Signal: " + str(self.signal) + " dBm\n"
retVal += "Strongest Signal: " + str(self.strongestsignal) + " dBm\n"
retVal += "Bandwidth: " + str(self.bandwidth) + "\n"
retVal += "First Seen: " + str(self.firstSeen) + "\n"
retVal += "Last Seen: " + str(self.lastSeen) + "\n"
retVal += "Last GPS:\n"
retVal += str(self.gps)
retVal += "Strongest GPS:\n"
retVal += str(self.strongestgps)
return retVal
def copy(self):
return copy.deepcopy(self)
def __eq__(self, obj):
# This is equivance.... ==
if not isinstance(obj, WirelessNetwork):
return False
if self.macAddr != obj.macAddr:
return False
if self.ssid != obj.ssid:
return False
if self.mode != obj.mode:
return False
if self.security != obj.security:
return False
if self.channel != obj.channel:
return False
return True
def __ne__(self, other):
return not self.__eq__(other)
def createFromJsonDict(jsondict):
retVal = WirelessNetwork()
retVal.fromJsondict(jsondict)
return retVal
def fromJsondict(self, dictjson):
# Note: if the json dictionary isn't correct, this will naturally throw an exception that may
# need to be caught for error detection
self.macAddr = dictjson['macAddr']
self.ssid = dictjson['ssid']
self.mode = dictjson['mode']
self.security = dictjson['security']
self.privacy = dictjson['privacy']
self.cipher = dictjson['cipher']
self.frequency = int(dictjson['frequency'])
self.channel = int(dictjson['channel'])
self.secondaryChannel = int(dictjson['secondaryChannel'])
self.secondaryChannelLocation = dictjson['secondaryChannelLocation']
self.thirdChannel = int(dictjson['thirdChannel'])
self.signal = int(dictjson['signal'])
self.strongestsignal = int(dictjson['strongestsignal'])
self.bandwidth = int(dictjson['bandwidth'])
self.firstSeen = parser.parse(dictjson['firstseen'])
self.lastSeen = parser.parse(dictjson['lastseen'])
self.gps.latitude = float(dictjson['lat'])
self.gps.longitude = float(dictjson['lon'])
self.gps.altitude = float(dictjson['alt'])
self.gps.speed = float(dictjson['speed'])
self.gps.isValid = stringtobool(dictjson['gpsvalid'])
self.strongestgps.latitude = float(dictjson['strongestlat'])
self.strongestgps.longitude = float(dictjson['strongestlon'])
self.strongestgps.altitude = float(dictjson['strongestalt'])
self.strongestgps.speed = float(dictjson['strongestspeed'])
self.strongestgps.isValid = stringtobool(dictjson['strongestgpsvalid'])
def fromJson(self, jsonstr):
dictjson = json.loads(jsonstr)
self.fromJsondict(dictjson)
def toJsondict(self):
dictjson = {}
dictjson['type'] = 'wifi-ap'
dictjson['macAddr'] = self.macAddr
dictjson['ssid'] = self.ssid
dictjson['mode'] = self.mode
dictjson['security'] = self.security
dictjson['privacy'] = self.privacy
dictjson['cipher'] = self.cipher
dictjson['frequency'] = self.frequency
dictjson['channel'] = self.channel
dictjson['secondaryChannel'] = self.secondaryChannel
dictjson['secondaryChannelLocation'] = self.secondaryChannelLocation
dictjson['thirdChannel'] = self.thirdChannel
dictjson['signal'] = self.signal
dictjson['strongestsignal'] = self.strongestsignal
dictjson['bandwidth'] = self.bandwidth
dictjson['firstseen'] = str(self.firstSeen)
dictjson['lastseen'] = str(self.lastSeen)
dictjson['lat'] = str(self.gps.latitude)
dictjson['lon'] = str(self.gps.longitude)
dictjson['alt'] = str(self.gps.altitude)
dictjson['speed'] = str(self.gps.speed)
dictjson['gpsvalid'] = str(self.gps.isValid)
dictjson['strongestlat'] = str(self.strongestgps.latitude)
dictjson['strongestlon'] = str(self.strongestgps.longitude)
dictjson['strongestalt'] = str(self.strongestgps.altitude)
dictjson['strongestspeed'] = str(self.strongestgps.speed)
dictjson['strongestgpsvalid'] = str(self.strongestgps.isValid)
return dictjson
def toJson(self):
dictjson = self.toJsondict()
return json.dumps(dictjson)
def getChannelString(self):
if self.bandwidth == 40 and self.secondaryChannel > 0:
retVal = str(self.channel) + '+' + str(self.secondaryChannel)
else:
retVal = str(self.channel)
return retVal
def getKey(self):
return self.macAddr + self.ssid+str(self.channel)
class WirelessEngine(object):
def __init__(self):
super().__init__()
def getMacAddress(interface):
macaddr = ""
try:
f = open('/sys/class/net/'+interface+'/address', 'r')
macaddr = f.readline().strip()
f.close()
except:
pass
return macaddr
def getFrequencyForChannel(channelNumber):
channelStr = str(channelNumber)
if channelStr in channelToFreq:
return channelToFreq[channelStr]
else:
return None
def getSignalQualityFromDB0To5(dBm):
# Based on same scale tha Microsoft uses.
# See https://stackoverflow.com/questions/15797920/how-to-convert-wifi-signal-strength-from-quality-percent-to-rssi-dbm
if (dBm <= -100):
quality = 0
elif dBm >= -50:
quality = 100
else:
quality = 2 * (dBm + 100)
return int(4*quality/100)
def getSignalQualityFromDB(dBm):
# Based on same scale tha Microsoft uses.
# See https://stackoverflow.com/questions/15797920/how-to-convert-wifi-signal-strength-from-quality-percent-to-rssi-dbm
if (dBm <= -100):
quality = 0
elif dBm >= -50:
quality = 100
else:
quality = 2 * (dBm + 100)
return quality
def convertUnknownToString(ssid):
if '\\x00' not in ssid:
return ssid
retVal = ssid.replace('\\x00', '')
numblanks = ssid.count('\\x00')
if len(retVal) == 0:
if numblanks > 0:
return '<Unknown (' + str(numblanks )+ ')>'
else:
return '<Unknown>'
else:
return ssid
def getInterfaces(printResults=False):
result = subprocess.run(['iwconfig'], stdout=subprocess.PIPE,stderr=subprocess.DEVNULL)
wirelessResult = result.stdout.decode('ASCII')
p = re.compile('^(.*?) IEEE', re.MULTILINE)
tmpInterfaces = p.findall(wirelessResult)
retVal = []
if (len(tmpInterfaces) > 0):
for curInterface in tmpInterfaces:
tmpStr=curInterface.replace(' ','')
retVal.append(tmpStr)
# debug
if (printResults):
print(tmpStr)
else:
# debug
if (printResults):
print("Error: No wireless interfaces found.")
return retVal
def getMonitoringModeInterfaces(printResults=False):
# Note: for standard scans with iw, this isn't required. Just root access.
# This is only required for some of the more advanced pen testing capabilities
result = subprocess.run(['iwconfig'], stdout=subprocess.PIPE,stderr=subprocess.DEVNULL)
wirelessResult = result.stdout.decode('ASCII')
p = re.compile('^(.*?) IEEE.*?Mode:Monitor', re.MULTILINE)
tmpInterfaces = p.findall(wirelessResult)
retVal = []
if (len(tmpInterfaces) > 0):
for curInterface in tmpInterfaces:
tmpStr=curInterface.replace(' ','')
retVal.append(tmpStr)
# debug
if (printResults):
print(tmpStr)
else:
# debug
if (printResults):
print("Error: No monitoring mode wireless interfaces found.")
return retVal
def getNetworksAsJson(interfaceName, gpsData, huntChannelList=None):
# This is only used by the remote agent to get and return networks
if (huntChannelList is None) or (len(huntChannelList) == 0):
# This code handles the thought that "what if we query for networks and the interface
# reports busy (it does happen if we query too fast.)
retries = 0
retCode = WirelessNetwork.ERR_DEVICEBUSY
while (retCode == WirelessNetwork.ERR_DEVICEBUSY) and (retries < 3):
# Handle retries in case we get a busy response
retCode, errString, wirelessNetworks = WirelessEngine.scanForNetworks(interfaceName)
retries += 1
if retCode == WirelessNetwork.ERR_DEVICEBUSY:
sleep(0.4)
else:
wirelessNetworks = {}
for curFrequency in huntChannelList:
# Handle if the device reports busy with some retries
retries = 0
retCode = WirelessNetwork.ERR_DEVICEBUSY
while (retCode == WirelessNetwork.ERR_DEVICEBUSY) and (retries < 3):
# Handle retries in case we get a busy response
retCode, errString, tmpWirelessNetworks = WirelessEngine.scanForNetworks(interfaceName,curFrequency )
retries += 1
if retCode == WirelessNetwork.ERR_DEVICEBUSY:
sleep(0.2)
for curKey in tmpWirelessNetworks.keys():
curNet = tmpWirelessNetworks[curKey]
wirelessNetworks[curNet.getKey()] = tmpWirelessNetworks[curNet.getKey()]
retVal = {}
retVal['errCode'] = retCode
retVal['errString'] = errString
netList = []
for curKey in wirelessNetworks.keys():
curNet = wirelessNetworks[curKey]
if gpsData is not None:
curNet.gps.copy(gpsData)
netList.append(curNet.toJsondict())
gpsdict = {}
gpsloc = SparrowGPS()
if (gpsData is not None):
gpsloc.copy(gpsData)
gpsdict['latitude'] = gpsloc.latitude
gpsdict['longitude'] = gpsloc.longitude
gpsdict['altitude'] = gpsloc.altitude
gpsdict['speed'] = gpsloc.speed
retVal['gps'] = gpsdict
retVal['networks'] = netList
jsonstr = json.dumps(retVal)
return retCode, errString, jsonstr
def scanForNetworks(interfaceName, frequency=0, printResults=False):
if frequency == 0:
result = subprocess.run(['iw', 'dev', interfaceName, 'scan'], stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
else:
result = subprocess.run(['iw', 'dev', interfaceName, 'scan', 'freq', str(frequency)], stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
retCode = result.returncode
errString = ""
wirelessResult = result.stdout.decode('ASCII')
# debug
if (printResults):
print('Return Code ' + str(retCode))
print(wirelessResult)
wirelessNetworks = {}
if (retCode == 0):
wirelessNetworks = WirelessEngine.parseIWoutput(wirelessResult)
else:
# errCodes:
# 156 = Network is down (i.e. switch may be turned off)
# 240 = command failed: Device or resource busy (-16)
errString = wirelessResult.replace("\n", "")
if (retCode == WirelessNetwork.ERR_NETDOWN):
errString = 'Interface appears down'
elif (retCode == WirelessNetwork.ERR_DEVICEBUSY):
errString = 'Device is busy'
elif (retCode == WirelessNetwork.ERR_OPNOTPERMITTED):
errString = errString + '. Did you run as root?'
return retCode, errString, wirelessNetworks
def getFieldValue(p, curLine):
matchobj = p.search(curLine)
if not matchobj:
return ""
try:
retVal = matchobj.group(1)
except:
retVal = ""
return retVal
def parseIWoutput(iwOutput):
# Define search regexes once:
p_bss = re.compile('^BSS (.*?)\(')
p_ssid = re.compile('^.+?SSID: +(.*)')
p_ess = re.compile('^ capability:.*(ESS)')
p_ess_privacy = re.compile('^ capability:.*(ESS Privacy)')
p_ibss = re.compile('^ capability:.*(IBSS)')
p_ibss_privacy = re.compile('^ capability:.*(IBSS Privacy)')
p_auth_suites = re.compile('.*?Authentication suites: *(.*)')
p_pw_ciphers = re.compile('.*?Pairwise ciphers: *(.*)')
p_param_channel = re.compile('^.*?DS Parameter set: channel +([0-9]+).*')
p_primary_channel = re.compile('^.*?primary channel: +([0-9]+).*')
p_freq = re.compile('^.*?freq:.*?([0-9]+).*')
p_signal = re.compile('^.*?signal:.*?([\-0-9]+).*?dBm')
p_ht = re.compile('.*?HT20/HT40.*')
p_bw = re.compile('.*?\\* channel width:.*?([0-9]+) MHz.*')
p_secondary = re.compile('^.*?secondary channel offset: *([^ \\t]+).*')
p_thirdfreq = re.compile('^.*?center freq segment 1: *([^ \\t]+).*')
# start
retVal = {}
curNetwork = None
now=datetime.datetime.now()
# This now supports direct from STDOUT via scanForNetworks,
# and input from a file as f.readlines() which returns a list
if type(iwOutput) == str:
inputLines = iwOutput.splitlines()
else:
inputLines = iwOutput
for curLine in inputLines:
fieldValue = WirelessEngine.getFieldValue(p_bss, curLine)
if (len(fieldValue) > 0):
# New object
if curNetwork is not None:
# Store first
if curNetwork.channel > 0:
# I did see incomplete output from iw where not all the data was there
retVal[curNetwork.getKey()] = curNetwork
# Create a new netowrk. BSSID will be the header for each network
curNetwork = WirelessNetwork()
curNetwork.lastSeen = now
curNetwork.firstSeen = now
curNetwork.macAddr = fieldValue
continue
if curNetwork is None:
# If we don't have a network object yet, then we haven't
# seen a BSSID so just keep going through the lines.
continue
fieldValue = WirelessEngine.getFieldValue(p_ssid, curLine)
if (len(fieldValue) > 0):
curNetwork.ssid = WirelessEngine.convertUnknownToString(fieldValue)
fieldValue = WirelessEngine.getFieldValue(p_ess, curLine)
if (len(fieldValue) > 0):
curNetwork.mode = "AP"
# Had issue with WEP not showing up.
# If capability has "ESS Privacy" there's something there.
# If it's PSK, etc. there will be other RSN fields, etc.
# So for now start by assuming WEP
# See: https://wiki.archlinux.org/index.php/Wireless_network_configuration
fieldValue = WirelessEngine.getFieldValue(p_ess_privacy, curLine)
if (len(fieldValue) > 0):
curNetwork.security = "WEP"
curNetwork.privacy = "WEP"
continue #Found the item
fieldValue = WirelessEngine.getFieldValue(p_ibss, curLine)
if (len(fieldValue) > 0):
curNetwork.mode = "Ad Hoc"
curNetwork.security = "[Ad-Hoc] Open"
fieldValue = WirelessEngine.getFieldValue(p_ibss_privacy, curLine)
if (len(fieldValue) > 0):
curNetwork.security = "[Ad-Hoc] WEP"
curNetwork.privacy = "WEP"
continue #Found the item
fieldValue = WirelessEngine.getFieldValue(p_auth_suites, curLine)
if (len(fieldValue) > 0):
curNetwork.security = fieldValue
continue #Found the item
# p = re.compile('.*?Group cipher: *(.*)')
fieldValue = WirelessEngine.getFieldValue(p_pw_ciphers, curLine)
fieldValue = fieldValue.replace(' ', '/')
if (len(fieldValue) > 0):
curNetwork.privacy = fieldValue
curNetwork.cipher = fieldValue
continue #Found the item
fieldValue = WirelessEngine.getFieldValue(p_param_channel, curLine)
if (len(fieldValue) > 0):
curNetwork.channel = int(fieldValue)
continue #Found the item
fieldValue = WirelessEngine.getFieldValue(p_primary_channel, curLine)
if (len(fieldValue) > 0):
curNetwork.channel = int(fieldValue)
continue #Found the item
fieldValue = WirelessEngine.getFieldValue(p_freq, curLine)
if (len(fieldValue) > 0):
curNetwork.frequency = int(fieldValue)
continue #Found the item
fieldValue = WirelessEngine.getFieldValue(p_signal, curLine)
# This test is different. dBm is negative so can't test > 0. 10dBm is really high so lets use that
if (len(fieldValue) > 0):
curNetwork.signal = int(fieldValue)
curNetwork.strongestsignal = curNetwork.signal
continue #Found the item
fieldValue = WirelessEngine.getFieldValue(p_ht, curLine)
if (len(fieldValue) > 0):
if (curNetwork.bandwidth == 20):
curNetwork.bandwidth = 40
continue #Found the item
fieldValue = WirelessEngine.getFieldValue(p_bw, curLine)
if (len(fieldValue) > 0):
curNetwork.bandwidth = int(fieldValue)
continue #Found the item
fieldValue = WirelessEngine.getFieldValue(p_secondary, curLine)
if (len(fieldValue) > 0):
curNetwork.secondaryChannelLocation = fieldValue
if (fieldValue == 'above'):
curNetwork.secondaryChannel = curNetwork.channel + 4
elif (fieldValue == 'below'):
curNetwork.secondaryChannel = curNetwork.channel - 4
# else it'll say 'no secondary'
continue #Found the item
fieldValue = WirelessEngine.getFieldValue(p_thirdfreq, curLine)
if (len(fieldValue) > 0):
curNetwork.thirdChannel = int(fieldValue)
continue #Found the item
# #### End loop ######
# Add the last network
if curNetwork is not None:
if curNetwork.channel > 0:
# I did see incomplete output from iw where not all the data was there
retVal[curNetwork.getKey()] = curNetwork
return retVal
if __name__ == '__main__':
# WirelessEngine.getMacAddress('wlan0mon')
if os.geteuid() != 0:
print("ERROR: You need to have root privileges to run this script. Please try again, this time using 'sudo'. Exiting.\n")
exit(2)
# for debugging
# change this interface name to test it.
wirelessInterfaces = WirelessEngine.getInterfaces()
if len(wirelessInterfaces) == 0:
print("ERROR: Unable to find wireless interface.\n")
exit(1)
winterface = wirelessInterfaces[0]
print('Scanning for wireless networks on ' + winterface + '...')
# Testing to/from Json
# convert to Json
retCode, errString, jsonstr=WirelessEngine.getNetworksAsJson(winterface, None)
# Convert back
j=json.loads(jsonstr)
# print results
print('Error Code: ' + str(j['errCode']) + '\n')
if j['errCode'] == 0:
for curNetDict in j['networks']:
newNet = WirelessNetwork.createFromJsonDict(curNetDict)
print(newNet)
else:
print('Error String: ' + j['errString'] + '\n')
print('Done.\n')