-
Notifications
You must be signed in to change notification settings - Fork 0
/
import-wigle.py
163 lines (140 loc) · 8.5 KB
/
import-wigle.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
import sqlite3, requests, os, urllib.parse
from py2neo.database import Graph, Node, Relationship
from sys import argv
def get_human_address_display_string(road, housenumber, city, region, country):
s = ''
if road is not None and road.strip() != '':
s += road.strip()
if housenumber is not None and housenumber.strip() != '':
s += housenumber.strip()
s += ', '
if city is not None and city.strip() != '':
s += city.strip() + ', '
if region is not None and region.strip() != '':
s += region.strip() + ', '
if country is not None and country.strip() != '':
s += country.strip()
return s
def register_connection(connection, from_node_name, to_node_name):
from_node, to_node = None, None
if connection in ['WIFI/MGMT/BEACON']:
from_node = Node('device', mac_address=from_node_name)
to_node = Node('network', essid=to_node_name)
elif connection in ['WIFI/WIGLE/BSSID/POS/BEST', 'WIFI/WIGLE/BSSID/POS/LAST', 'WIFI/WIGLE/BSSID/POS/ONLINE']:
from_node = Node('device', mac_address=from_node_name)
to_node = Node('pos2d', lat=to_node_name[0], lon=to_node_name[1], display_string=','.join([str(_) for _ in to_node_name]))
elif connection in ['WIFI/WIGLE/SSID/POS/BEST', 'WIFI/WIGLE/SSID/POS/LAST', 'WIFI/WIGLE/SSID/POS/ONLINE']:
from_node = Node('network', essid=from_node_name)
to_node = Node('pos2d', lat=to_node_name[0], lon=to_node_name[1], display_string=','.join([str(_) for _ in to_node_name]))
elif connection in ['WIFI/WIGLE/BSSID/SIGNAL_STRENGTH']:
(lat, lon, altitude, level, accuracy) = to_node_name
from_node = Node('device', mac_address=from_node_name)
to_node = Node('signal_measurement', lat=lat, lon=lon, altitude=altitude, level=level, accuracy=accuracy)
elif connection in ['WIFI/WIGLE/SSID/SIGNAL_STRENGTH']:
(lat, lon, altitude, level, accuracy) = to_node_name
from_node = Node('network', essid=from_node_name)
to_node = Node('signal_measurement', lat=lat, lon=lon, altitude=altitude, level=level, accuracy=accuracy)
elif connection in ['WIFI/WIGLE/BSSID/POS/HUMAN']:
(road, housenumber, city, region, country) = to_node_name
from_node = Node('device', mac_address=from_node_name)
to_node = Node('human_address', road=road, housenumber=housenumber, city=city, region=region, country=country, display_string=get_human_address_display_string(road, housenumber, city, region, country))
elif connection in ['WIFI/WIGLE/SSID/POS/HUMAN']:
(road, housenumber, city, region, country) = to_node_name
from_node = Node('network', essid=from_node_name)
to_node = Node('human_address', road=road, housenumber=housenumber, city=city, region=region, country=country, display_string=get_human_address_display_string(road, housenumber, city, region, country))
if None in [from_node, to_node]:
raise Exception('Unknown connection type', connection)
rel = Relationship(from_node, connection, to_node)
graph.merge(rel)
def handle_db(filename):
print('Loading', filename)
conn = sqlite3.connect(filename)
c = conn.cursor()
print(' * Loading location')
for (_id, bssid, level, lat, lon, altitude, accuracy, time) in c.execute('SELECT * FROM location WHERE bssid LIKE "%:%" AND time!=0'):
print(_id, bssid, level, lat, lon, altitude, accuracy, time)
register_connection('WIFI/WIGLE/BSSID/SIGNAL_STRENGTH', bssid, (lat, lon, altitude, level, accuracy))
print(' * Loading network')
for (bssid, ssid, frequency, capabilities, lasttime, lastlat, lastlon, _type, bestlevel, bestlat, bestlon) in c.execute('SELECT * FROM network WHERE type="W"'):
print(bssid, ssid, frequency, capabilities, lasttime, lastlat, lastlon, _type, bestlevel, bestlat, bestlon)
if ssid != '':
register_connection('WIFI/MGMT/BEACON', bssid, ssid)
register_connection('WIFI/WIGLE/SSID/POS/BEST', ssid, (bestlat, bestlon))
register_connection('WIFI/WIGLE/SSID/POS/LAST', ssid, (lastlat, lastlon))
register_connection('WIFI/WIGLE/BSSID/POS/BEST', bssid, (bestlat, bestlon))
register_connection('WIFI/WIGLE/BSSID/POS/LAST', bssid, (lastlat, lastlon))
conn.close()
def get_wigle_data(*args, **kwargs):
url = 'https://api.wigle.net/api/v2/' + '/'.join(args) + '?' + urllib.parse.urlencode(kwargs)
return requests.get(url, headers={
'Authorization': 'Basic ' + os.environ['WIGLE_AUTH']
}).json()
def get_wigle_network_search_data(ap_mac=None, essid=None):
if ap_mac is not None:
if essid is not None:
return get_wigle_data('network', 'search', netid=ap_mac, ssid=essid)
else:
return get_wigle_data('network', 'search', netid=ap_mac)
elif essid is not None:
return get_wigle_data('network', 'search', ssid=essid)
else:
raise Exception('Neither ap_mac nor essid set')
def get_wigle_network_detail_data(ap_mac):
return get_wigle_data('network', 'detail', netid=ap_mac)
def handle_online():
print('Loading online')
if 'WIGLE_AUTH' not in os.environ:
raise Exception('WIGLE_AUTH not set')
load_network_search_data = True
load_network_detail_data = True
def _get_network_search_data(ap_mac, essid):
j = get_wigle_network_search_data(ap_mac, essid)
if not j['success']:
print(j['message'])
return False
register_connection('WIFI/MGMT/BEACON', ap_mac, essid)
for result in j['results']:
trilat, trilong, housenumber, road, city, region, country = result['trilat'], result['trilong'], result['housenumber'], result['road'], result['city'], result['region'], result['country']
register_connection('WIFI/WIGLE/BSSID/POS/ONLINE', ap_mac, (trilat, trilong))
register_connection('WIFI/WIGLE/SSID/POS/ONLINE', essid, (trilat, trilong))
register_connection('WIFI/WIGLE/BSSID/POS/HUMAN', ap_mac, (road, housenumber, city, region, country))
register_connection('WIFI/WIGLE/SSID/POS/HUMAN', essid, (road, housenumber, city, region, country))
return True
def _get_network_detail_data(ap_mac):
j = get_wigle_network_detail_data(ap_mac)
if not j['success']:
print(j['message'])
return False
for result in j['results']:
trilat, trilong, ssid, housenumber, road, city, region, country = result['trilat'], result['trilong'], result['ssid'], result['housenumber'], result['road'], result['city'], result['region'], result['country']
register_connection('WIFI/MGMT/BEACON', ap_mac, ssid)
register_connection('WIFI/WIGLE/BSSID/POS/ONLINE', ap_mac, (trilat, trilong))
register_connection('WIFI/WIGLE/SSID/POS/ONLINE', ssid, (trilat, trilong))
if not any([_ is None for _ in (housenumber, road, city, region, country)]):
register_connection('WIFI/WIGLE/BSSID/POS/HUMAN', ap_mac, (road, housenumber, city, region, country))
register_connection('WIFI/WIGLE/SSID/POS/HUMAN', ssid, (road, housenumber, city, region, country))
for measurement in result['locationData']:
register_connection('WIFI/WIGLE/BSSID/SIGNAL_STRENGTH', ap_mac, (measurement['latitude'], measurement['longitude'], measurement['alt'], measurement['signal'], measurement['accuracy']))
register_connection('WIFI/WIGLE/SSID/SIGNAL_STRENGTH', ssid, (measurement['latitude'], measurement['longitude'], measurement['alt'], measurement['signal'], measurement['accuracy']))
return True
pairs_done = []
for _ in graph.run(
'MATCH p=(d:device)-[:`WIFI/MGMT/BEACON`|:`WIFI/MGMT/PROBE_RESPONSE/MAC_SENT_SSID`]-(n:network)'
'WHERE NOT (d)-[:`WIFI/WIGLE/BSSID/POS/ONLINE`]-()'
'RETURN p'):
rel = _['p']
ap_mac, essid = rel.start_node()['mac_address'], rel.end_node()['essid']
print(ap_mac, essid)
if (ap_mac, essid) in pairs_done:
continue
pairs_done.append((ap_mac, essid))
if load_network_search_data and not _get_network_search_data(ap_mac, essid):
load_network_search_data = False
if load_network_detail_data and not _get_network_detail_data(ap_mac):
load_network_detail_data = False
if (not load_network_search_data) and (not load_network_detail_data):
raise Exception('Something went dead')
graph = Graph(password='password') # TODO: parameterize, don't hardcode password
for _ in argv[1:]:
handle_db(_)
handle_online()