-
Notifications
You must be signed in to change notification settings - Fork 0
/
wifi-sniffer.py
executable file
·78 lines (49 loc) · 1.76 KB
/
wifi-sniffer.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
#! /usr/bin/python3
# installation:
# apt install python3-serial python3-paho-mqtt
# This code was written by Folkert van Heusden <[email protected]>
tty = '/dev/ttyUSB0' # you may want to change this
mqtt_host = 'mqtt.vm.nurd.space' # you may want to change this
import json
import paho.mqtt.publish as publish
import serial
import time
mac_lookup = dict()
for line in open('mac-vendor.txt', 'r'):
mac_lookup[line[0:8].replace('-', '').lower()] = line[9:].strip()
s = serial.Serial(tty, 115200, timeout=1)
history = dict()
prev = None
while True:
try:
line = s.readline().decode('ascii').rstrip('\n').rstrip('\r')
if line == '':
continue
now = time.time()
parts = line.split('|')
rssi = parts[0]
channel = parts[1]
mac1 = parts[2]
mac2 = parts[3]
mac3 = parts[4]
ssid = parts[5]
if mac2 == 'ff:ff:ff:ff:ff:ff':
mac = mac1 if mac3 == 'ff:ff:ff:ff:ff:ff' else mac3
else:
mac = mac2
if not mac in history:
lookup_str = mac[0:8].replace(':', '')
if lookup_str in mac_lookup:
vendor = mac_lookup[lookup_str]
else:
vendor = lookup_str
history[mac] = { 'rssi' : rssi, 'channel' : channel, 'mac' : mac, 'ssid' : ssid, 'vendor' : vendor }
else:
history[mac]['rssi'] = rssi
if prev != mac and (not 'ts' in history[mac] or now - history[mac]['ts'] >= 30):
prev = mac
history[mac]['ts'] = now
print(history[mac])
publish.single('wifi-sniffer', hostname=mqtt_host, payload=json.dumps(history[mac]))
except Exception as e:
print(e)