-
Notifications
You must be signed in to change notification settings - Fork 6
/
sensor.py
executable file
·99 lines (78 loc) · 2.88 KB
/
sensor.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
from scapy.all import *
from datetime import datetime
import socket, os, re, sys, sqlite3
DB_FILE = 'node.db'
PROBE_REQUEST_TYPE=0
PROBE_REQUEST_SUBTYPE=4
newfile = False
if not os.path.exists(DB_FILE):
print "creating .db"
newfile = True
conn = sqlite3.connect(DB_FILE)
cursor = conn.cursor()
if newfile == True:
# cursor.execute("CREATE TABLE clients (id, node_id, macaddr, dstaddr, ssid, rssi, timestamp);")
conn.execute('''CREATE TABLE clients
(ID INTEGER PRIMARY KEY autoincrement NOT NULL,
node_id char(20) NOT NULL,
macaddr char(50) NOT NULL,
dstaddr char(50) NOT NULL,
ssid text NOT NULL,
rssi char(4) NOT NULL,
timestamp char(50) NOT NULL);''')
conn.execute('''CREATE TABLE aps
(ID INTEGER PRIMARY KEY autoincrement NOT NULL,
node_id char(20) NOT NULL,
macaddr char(50) NOT NULL,
ssid text NOT NULL,
rssi char(10) NOT NULL,
timestamp char(50) NOT NULL);''')
def updateClients(mac, dstaddr, ssid, rssi, timestamp) :
conn.execute("insert into clients (node_id, macaddr, dstaddr, ssid, rssi, timestamp) values (?,?,?,?,?,?)", ("test", mac, dstaddr, ssid, rssi, timestamp))
conn.commit()
def updateAP(mac, ssid, rssi, timestamp) :
cursor.execute("SELECT * FROM aps WHERE macaddr=?", (mac,))
dat = cursor.fetchone()
if not dat:
print "New AP found: %s" %(mac)
conn.execute("insert into aps (node_id, macaddr, ssid, rssi, timestamp) values (?,?,?,?,?)", ("test", mac, ssid, rssi, timestamp))
conn.commit()
def PacketHandler(pkt):
if pkt.haslayer(Dot11):
if pkt.type==PROBE_REQUEST_TYPE and pkt.subtype == PROBE_REQUEST_SUBTYPE:
recordClient(pkt)
elif pkt.type==PROBE_REQUEST_TYPE and pkt.subtype == 8:
recordAP(pkt)
def recordClient(pkt):
#print "Probe Request Captured:"
try:
extra = pkt.notdecoded
except:
extra = None
if extra!=None:
signal_strength = -(256-ord(extra[-4:-3]))
else:
signal_strength = -100
print "No signal strength found"
print "Target: %s Source: %s SSID: %s RSSi: %d TIMESTAMP: %s"%(pkt.addr3,pkt.addr2,pkt.getlayer(Dot11ProbeReq).info,signal_strength,datetime.now())
updateClients(pkt.addr2, pkt.addr3, pkt.info, signal_strength, datetime.now())
def recordAP(pkt):
try:
extra = pkt.notdecoded
except:
extra = None
if extra!=None:
signal_strength = -(256-ord(extra[-4:-3]))
else:
signal_strength = -100
print "No signal strength found"
updateAP(pkt.addr2, pkt.info, signal_strength, datetime.now())
def main():
from datetime import datetime
print "[%s] Starting scan"%datetime.now()
sniff(iface='mon0',prn=PacketHandler, store=0)
connection = sqlite3.connect(DB_FILE)
ouis = open("manuf").read().split('\n')
if __name__=="__main__":
main()
connection.close