-
Notifications
You must be signed in to change notification settings - Fork 1
/
main
executable file
·502 lines (423 loc) · 17.1 KB
/
main
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
#!/usr/bin/env python3
from datetime import datetime, timezone
from dateutil import tz
from scapy.all import *
from time import sleep
from threading import Thread
from pprint import pprint
import atexit
import argparse
import csv
import dateutil.parser
import gps
import os
import sqlite3
parser = argparse.ArgumentParser()
parser.add_argument("--interface",
default='',
required=True,
help="Interface of choice")
parser.add_argument("--nomanagement",
default=False,
action='store_true',
help="Assume another program is already handling int setup/restoration and channel switching. We're just along for the ride.")
parser.add_argument("--importcsv",
default='',
help="CSV file to import")
parser.add_argument("--exportcsv",
default='',
help="CSV filename to export with. If --google used this is a basename and CSVs get saved to your CWD")
parser.add_argument("--google",
default='',
action='store_true',
help="Break CSV export into multiple files of 2000 data point each. For Google's 'My Maps' which has a 2000-per-csv limit.")
parser.add_argument("--skipgps",
default='',
action='store_true',
help="Don't wait for GPS info. For developing this script without a GPS present.")
parser.add_argument("--loglevel",
default=0,
type=int,
help="Optional verbosity. Verbose=1, Debug=2")
parser.add_argument("--debug",
default='',
action='store_const',
dest='loglevel',
const=2,
help="Quick --loglevel 2")
parser.add_argument("--verbose",
default='',
action='store_const',
dest='loglevel',
const=1,
help="Quick --loglevel 1")
args = parser.parse_args()
# ____ _
# / ___| | __ _ ___ ___ ___ ___
# | | | |/ _` / __/ __|/ _ \/ __|
# | |___| | (_| \__ \__ \ __/\__ \
# \____|_|\__,_|___/___/\___||___/
#
class GPS(): # Generate GPS data for other threads.
def __init__(self):
self.gpsData = { "gpsTimestamp": None, "gpsTimestampAge": None, "Longitude": None, "Latitude": None } # Init gpsData
# Longitude/Latitude The location data received at gpsTimestamp.
# gpsTimestamp The timestamp with Lon / Lat from GPS
# gpsTimestampAge The difference in seconds between your host's time and the gpsTimestamp stamp.
# Useful to know if your GPS has lost its lock when writing results.
try:
print('Initializing GPS...')
self.session = gps.gps(mode=gps.WATCH_ENABLE)
except ConnectionRefusedError:
print('Local gpsd refused our connection. Is it running?')
print('If you are debugging with no GPS, you can launch again using --skipgps')
exit(1)
except Exception as e:
print('Something else went wrong trying to speak with gpsd:')
print(e)
exit(1)
finally:
print('Initialized.')
def readGps(self):
time=lat=lon=None
# Do initial read from gpsd
self.session.read()
# If invalid TPV result, keep trying until we get one...
# May be inappropriate to gpsd.
while not (gps.MODE_SET & self.session.valid):
self.session.read() # Try reading from gpsd.
# Capture GPS state for data purposes.
# Not Yet Implemented!!!!!!!!
#gpsFixStatus = ("Invalid", "NO_FIX", "2D", "3D")[session.fix.mode]
if gps.TIME_SET & self.session.valid:
time = self.session.fix.time
if ((gps.isfinite(self.session.fix.latitude) and gps.isfinite(self.session.fix.longitude))):
lat = self.session.fix.latitude
lon = self.session.fix.longitude
if time and lat and lon:
self.gpsData["gpsTimestamp"] = time
self.gpsData["Latitude"] = lat
self.gpsData["Longitude"] = lon
if self.gpsData['gpsTimestamp']: # Update staleness periodically
gpsTime = datetime.strptime(str(self.gpsData['gpsTimestamp']),'%Y-%m-%dT%H:%M:%S.%f%z')
gpsTime = gpsTime.replace(tzinfo=tz.tzutc()).astimezone(tz=tz.tzlocal())
localTime = datetime.now().astimezone()
self.gpsData["gpsTimestampAge"] = ((localTime-gpsTime).total_seconds())
def _gpsThread(self):
while True:
self.readGps()
def startGpsThread(self):
try:
self.gpsThread = Thread(target=self._gpsThread)
self.gpsThread.daemon = True
self.gpsThread.start()
finally:
print('GPS Data thread started.')
def waitForLock(self):
if args.skipgps:
self.gpsData['gpsTimestamp'] = 'NA';
self.gpsData['gpsTimestampAge'] = 'NA';
self.gpsData['Latitude'] = 0;
self.gpsData['Longitude'] = 0;
print("Skipping GPS wait and set dummy values: " + str(list(gpsData.values())))
else:
print('Waiting for GPS lock...')
while not self.gpsData['gpsTimestamp']: # Wait for data
self.readGps()
print("Got a lock: " + str(list(self.gpsData.values())))
class Database: # Our database object
def __init__(self, databaseFile):
try:
self.con = sqlite3.connect(databaseFile,check_same_thread=False)
self.cur = self.con.cursor()
# Create our database and tables, including a secondary table to evict old data.
# Dict-powered DB creation. Probably a stupid idea
schema = { "columns": {
"bssid": {
"type": "VARCHAR(17)"
},
"ssid": {
"type": ""
},
"channel": {
"type": "INT"
},
"security": {
"type": "TEXT"
},
"rssi": {
"type": "INT"
},
"timestamp": {
"type": "INT"
},
"timestampAge": {
"type": "REAL"
},
"lat": {
"type": "REAL"
},
"lon": {
"type": "REAL"
}
},
"tables": {
"wifi": {
"tableSpecial": "unique (bssid)"
},
"wifi_evicted": {
"tableSpecial": ""
}
}
}
for table in list(schema['tables'].keys()): # Build the database
tableQuery = "create table if not exists %s" %(table)
columnCount = len(schema['columns'])
counter = 1
columnQuery = ''
for column in schema['columns']:
columnQuery += column + ' ' + schema['columns'][column]['type']
if counter < 2:
columnQuery = ' ' + columnQuery
if counter < columnCount:
columnQuery += ', '
else:
columnQuery += ' '
counter += 1
if schema['tables'][table]['tableSpecial']:
columnQuery += ', ' + schema['tables'][table]['tableSpecial']
tableQuery += ' (' + columnQuery + ')'
self.exec(tableQuery)
except Exception as e:
print('Failed to prepare Database: ', e)
exit(1)
finally:
print('Database connected')
def exec(self, query):
logger('About to execute this query: ' + '[' + query + ']',2)
self.cur.execute(query)
self.con.commit()
def execFetchAll(self, query):
self.exec(query)
return(self.cur.fetchall())
def checkBssidExists(self,bssid): # Check if we have a point for a given BSSID already.
self.exec("select * from wifi where bssid = '%s' limit 1" %(bssid))
result = self.cur.fetchone()
if result: # Truthy check for whether we got any result at all.
logger('BSSID Exists in database already',2)
return(result)
else:
logger('BSSID is new',2)
return(False)
def checkNewPointBetter(self,storeCheck,bssid,ssid,rssi,channel,security,timestamp,timestampAge,lat,lon): # Check if we're replacing stored data.
if int(rssi) > storeCheck[2]:
logger('RSSI improved',2)
return(True)
else:
logger('No RSSI improvement, leaving alone.',2)
return(False)
def retirePoint(self,bssid):
self.exec("INSERT OR REPLACE INTO wifi_evicted select * from wifi where bssid = '%s'" %(bssid))
self.exec("DELETE from wifi where bssid = '%s'" %(bssid))
def writePoint(self,bssid,ssid,rssi,channel,security,timestamp,timestampAge,lat,lon):
if args.skipgps:
print('Skipping point write due to --skipgps: ' + bssid + ' ' + ssid)
else:
ssid = ssid.replace("'","''")
self.exec("insert or ignore into wifi values ('%s','%s','%s','%s','%s','%s','%s','%s','%s')" %(bssid,ssid,rssi,channel,security,timestamp,timestampAge,lat,lon))
def handleWifiPoint(self,bssid,ssid,rssi,channel,security,timestamp,timestampAge,lat,lon):
storeCheck = self.checkBssidExists(bssid)
if storeCheck:
if self.checkNewPointBetter(storeCheck,bssid,ssid,rssi,channel,security,timestamp,timestampAge,lat,lon):
logger('Updating point:\t' + bssid + '\tStaleness:\t' + str(timestampAge), 1)
self.retirePoint(bssid)
self.writePoint(bssid,ssid,rssi,channel,security,timestamp,timestampAge,lat,lon)
else:
logger('Writing new point:\t' + bssid + '\tStaleness:\t' + str(timestampAge), 1)
self.writePoint(bssid,ssid,rssi,channel,security,timestamp,timestampAge,lat,lon)
# _____ _ _
#| ___| _ _ __ ___| |_(_) ___ _ __ ___
#| |_ | | | | '_ \ / __| __| |/ _ \| '_ \/ __|
#| _|| |_| | | | | (__| |_| | (_) | | | \__ \
#|_| \__,_|_| |_|\___|\__|_|\___/|_| |_|___/
#
def cleanup():
print('Cleaning up...')
manageInt(args,'managed')
def logger(content,loglevel=0):
loglevelString = '[' + ("info", "verbose", "debug")[loglevel] + '] '
if loglevel <= args.loglevel:
print(loglevelString + content)
def date2unix(date):
if type(date) == int: # If already a number, assume it's already a unix timestamp
return(date)
else: # Otherwise, try reading it
try:
result = str(dateutil.parser.parse(date).timestamp()).split('.')[0]
except:
return(False)
finally:
return(int(result))
def importCsv(args):
if os.path.isfile(args.importcsv):
with open(args.importcsv) as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if row['bssid'] and row['ssid'] and row['Latitude'] and row['Longitude']: # Check for these during import at a minimum
print('Importing: ' + row['bssid'] + ' to our database.')
if row['gpsTimestamp']: # Convert the timestamp to UNIX
try:
gpsTimestamp = date2unix(row['gpsTimestamp'])
except:
print('Failed to convert date...')
gpsTimestamp = row['gpsTimestamp']
Db.handleWifiPoint(row['bssid'],
row['ssid'],
row['rssi'],
row['channel'],
row['security'],
gpsTimestamp,
row['gpsTimestampAge'],
row['Latitude'],
row['Longitude'])
else:
print('Failed to import: ' + row['bssid'] + '/' + row['ssid'] + ' to our database.')
print('We need at least bssid,ssid,Latitude,Longitide to import to the database.')
print('If this datapoint has all of these, please check your CSV for errors!')
else:
print('Argument given is not a file')
exit(1)
exit(0)
def writeFile(filename,headers,items):
try:
with open(filename, 'w') as csvFile:
w = csv.DictWriter(csvFile, headers)
w = csv.writer(csvFile)
# We can either select the data out converting the timestamp row (select datetime(timestamp, 'auto')) for example
# Or we can take the tuple row here and convert it in python to a local timezone. This would easily specify the +TZ as well.
# e.g. date = print(time.replace(tzinfo=tz.tzutc()).astimezone(tz=tz.tzlocal()))
# Make a unix2date function?
w.writerow(headers) # Write the header
for row in items:
# for csv consistency, export SSID as unicode escaped rather than interpreting newlines and linefeed chars literally.
row = list(row) # To modify the tuple result
row[1] = row[1].encode('unicode_escape')
row[1] = row[1].decode('utf-8')
w.writerow(row) # Write the data
finally:
print('Written: ' + filename)
def exportCsv(args):
filename = args.exportcsv
headerQuery = Db.execFetchAll('PRAGMA table_info(wifi)')
headers = []
for column in headerQuery: # Put headers together for the csv.
headers.append(column[1])
# Read out our data into memory
wifiQuery = Db.execFetchAll('select * from wifi')
if args.google: # Limit each csv to 2000 points each.
split = 2000
wifiQueryCount = len(wifiQuery) - 1
iter = 0
while iter < wifiQueryCount:
print(str(iter) + ':' + str(wifiQueryCount+2000))
# Insert an incrementing 'split' number into the desired filename before the suffix for visibility.
splitFilename = str('.'.join(filename.split('.')[:-1]) + '-' + str(iter) + '.' + filename.split('.')[-1])
writeFile(splitFilename,headers,wifiQuery[iter:iter+split])
iter = iter + split
else: # Just write the csv filename specified.
writeFile(filename,headers,wifiQuery)
exit(0)
def processPacket(packet):
try:
if packet.haslayer(Dot11Beacon): # If the packet looks good, proceed
bssid = packet[Dot11].addr2
ssid = packet[Dot11Elt].info.decode().rstrip('\x00')
if len(ssid) == 0: ssid = "<Hidden>"
rssi = packet.dBm_AntSignal
stats = packet[Dot11Beacon].network_stats()
channel = stats.get("channel")
security = ''.join(stats.get("crypto"))
# Handle new and seen APs.
# If new: Add to array.
# If seen: Compare RSSI, GPS Coordinates if better.
logger('Processing: ' + bssid + str(ssid) + str(channel) + security + str(rssi),2)
Db.handleWifiPoint(bssid,
ssid,
rssi,
channel,
security,
Gps.gpsData['gpsTimestamp'],
Gps.gpsData['gpsTimestampAge'],
Gps.gpsData['Latitude'],
Gps.gpsData['Longitude'])
except Exception as e: # Ignore busted packets
print('Packet issue: ' + str(e))
pass
def executeCmd(cmd):
try:
subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True, shell=True)
except subprocess.CalledProcessError as e:
print('Command [' + cmd + '] failed: ' + e.stderr.decode("utf-8"))
def manageInt(args,mode):
if args.nomanagement:
print('--nomanagement specified, not touching ' + args.interface)
return
if mode == 'monitor':
txmode = 'fixed 3000'
elif mode == 'managed':
txmode = 'auto'
try:
executeCmd('ip link set ' + args.interface + ' down')
executeCmd('iw ' + args.interface + ' set type ' + mode)
executeCmd('ip link set ' + args.interface + ' up')
# Not all interfaces like being told to go into auto
executeCmd('iw ' + args.interface + ' set txpower ' + txmode)
except Exception as e:
print('Something went wrong prepping interface: ' + args.interface)
print(e)
finally:
print(args.interface + ' is now a ' + mode + ' interface.')
def snifferThread(args):
try:
sniff(prn=processPacket, iface=args.interface)
except PermissionError:
print('No access to interface ' + args.interface + ' Please give this user access or run the script as root.')
exit(1)
except Exception as e:
print('Interface trouble: ' + str(e))
exit(1)
def channelSwitcher():
print('Channel switching thread started...')
ch = 1
while True:
executeCmd('iwconfig {interface} channel {ch}'.format(interface = args.interface, ch = ch))
ch = ch % 14 + 1
sleep(0.25)
# ____
# / ___| ___
#| | _ / _ \
#| |_| | (_) |
# \____|\___/
#
if __name__ == "__main__":
scriptRoot = os.path.dirname(os.path.realpath(__file__))
databaseFile = scriptRoot + '/wifi.db'
Db = Database(databaseFile)
if args.importcsv:
importCsv(args)
if args.exportcsv:
exportCsv(args)
logger('Loglevel is: ' + str(args.loglevel),1)
Gps = GPS()
Gps.waitForLock() # Don't take off until we get a lock.
Gps.startGpsThread() # Get started
manageInt(args,'monitor')
atexit.register(cleanup)
if not args.nomanagement:
channelSwitcher = Thread(target=channelSwitcher)
channelSwitcher.daemon = True
channelSwitcher.start()
print('Beginning recon...')
snifferThread = Thread(target=snifferThread(args))
snifferThread.daemon = True
snifferThread.start()