-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner.py
More file actions
443 lines (353 loc) · 13.7 KB
/
scanner.py
File metadata and controls
443 lines (353 loc) · 13.7 KB
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
#!/usr/bin/env python3
import subprocess
import json
import csv
import requests
import time
import os
import re
from datetime import datetime
from pathlib import Path
import threading
try:
from PIL import Image, ImageDraw, ImageFont
import spidev
import RPi.GPIO as GPIO
DISPLAY_AVAILABLE = True
except ImportError:
DISPLAY_AVAILABLE = False
print("Display modules not available")
CONFIG_FILE = "/boot/firmware/wigle_config.json"
if not os.path.exists(CONFIG_FILE):
CONFIG_FILE = "/boot/wigle_config.json"
CSV_DIR = "/home/pi/wigle_scans"
SCAN_INTERVAL = 10
UPLOAD_INTERVAL = 300
class ST7735S:
def __init__(self):
self.DC_PIN = 25
self.RST_PIN = 27
self.BL_PIN = 24
self.width = 128
self.height = 128
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(self.DC_PIN, GPIO.OUT)
GPIO.setup(self.RST_PIN, GPIO.OUT)
GPIO.setup(self.BL_PIN, GPIO.OUT)
self.spi = spidev.SpiDev()
self.spi.open(0, 0)
self.spi.max_speed_hz = 4000000
self.spi.mode = 0
self.reset()
self.init_display()
GPIO.output(self.BL_PIN, GPIO.HIGH)
def reset(self):
GPIO.output(self.RST_PIN, GPIO.HIGH)
time.sleep(0.1)
GPIO.output(self.RST_PIN, GPIO.LOW)
time.sleep(0.1)
GPIO.output(self.RST_PIN, GPIO.HIGH)
time.sleep(0.1)
def write_cmd(self, cmd):
GPIO.output(self.DC_PIN, GPIO.LOW)
self.spi.writebytes([cmd])
def write_data(self, data):
GPIO.output(self.DC_PIN, GPIO.HIGH)
if isinstance(data, int):
self.spi.writebytes([data])
else:
self.spi.writebytes(data)
def init_display(self):
self.write_cmd(0x11)
time.sleep(0.12)
self.write_cmd(0xB1)
self.write_data([0x01, 0x2C, 0x2D])
self.write_cmd(0xB2)
self.write_data([0x01, 0x2C, 0x2D])
self.write_cmd(0xB3)
self.write_data([0x01, 0x2C, 0x2D, 0x01, 0x2C, 0x2D])
self.write_cmd(0xB4)
self.write_data(0x07)
self.write_cmd(0xC0)
self.write_data([0xA2, 0x02, 0x84])
self.write_cmd(0xC1)
self.write_data(0xC5)
self.write_cmd(0xC2)
self.write_data([0x0A, 0x00])
self.write_cmd(0xC3)
self.write_data([0x8A, 0x2A])
self.write_cmd(0xC4)
self.write_data([0x8A, 0xEE])
self.write_cmd(0xC5)
self.write_data(0x0E)
self.write_cmd(0x36)
self.write_data(0xC8)
self.write_cmd(0x3A)
self.write_data(0x05)
self.write_cmd(0x2A)
self.write_data([0x00, 0x02, 0x00, 0x81])
self.write_cmd(0x2B)
self.write_data([0x00, 0x01, 0x00, 0x80])
self.write_cmd(0xE0)
self.write_data([0x02, 0x1c, 0x07, 0x12, 0x37, 0x32, 0x29, 0x2d,
0x29, 0x25, 0x2B, 0x39, 0x00, 0x01, 0x03, 0x10])
self.write_cmd(0xE1)
self.write_data([0x03, 0x1d, 0x07, 0x06, 0x2E, 0x2C, 0x29, 0x2D,
0x2E, 0x2E, 0x37, 0x3F, 0x00, 0x00, 0x02, 0x10])
self.write_cmd(0x13)
time.sleep(0.01)
self.write_cmd(0x29)
time.sleep(0.12)
def display_image(self, image):
if image.size != (self.width, self.height):
image = image.resize((self.width, self.height))
rgb_image = image.convert('RGB')
pixels = []
for y in range(self.height):
for x in range(self.width):
r, g, b = rgb_image.getpixel((x, y))
rgb565 = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3)
pixels.append(rgb565 >> 8)
pixels.append(rgb565 & 0xFF)
self.write_cmd(0x2C)
self.write_data(pixels)
class Display:
def __init__(self):
self.available = DISPLAY_AVAILABLE
if self.available:
try:
self.disp = ST7735S()
self.width = 128
self.height = 128
self.image = Image.new('RGB', (self.width, self.height), color=(0, 0, 0))
self.draw = ImageDraw.Draw(self.image)
try:
self.font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 10)
self.font_large = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 14)
except:
self.font = ImageFont.load_default()
self.font_large = ImageFont.load_default()
except Exception as e:
print(f"Display error: {e}")
self.available = False
def clear(self):
if self.available:
self.draw.rectangle((0, 0, self.width, self.height), outline=0, fill=(0, 0, 0))
def show_status(self, devices_found, total_scans, last_upload, status_msg):
if not self.available:
return
try:
self.clear()
y = 5
self.draw.text((5, y), "Wartooth Scanner", font=self.font_large, fill=(0, 255, 0))
y += 20
self.draw.line((5, y, self.width - 5, y), fill=(0, 150, 0))
y += 8
self.draw.text((5, y), f"Found: {devices_found}", font=self.font, fill=(255, 255, 255))
y += 15
self.draw.text((5, y), f"Scans: {total_scans}", font=self.font, fill=(255, 255, 255))
y += 15
if last_upload:
upload_time = last_upload.strftime("%H:%M")
self.draw.text((5, y), f"Upload: {upload_time}", font=self.font, fill=(255, 255, 0))
else:
self.draw.text((5, y), "Upload: pending", font=self.font, fill=(255, 255, 0))
y += 20
self.draw.line((5, y, self.width - 5, y), fill=(0, 150, 0))
y += 8
words = status_msg.split()
line = ""
for word in words:
test_line = line + word + " "
if len(test_line) * 6 < self.width - 10:
line = test_line
else:
self.draw.text((5, y), line, font=self.font, fill=(0, 200, 255))
y += 12
line = word + " "
if line:
self.draw.text((5, y), line, font=self.font, fill=(0, 200, 255))
self.disp.display_image(self.image)
except Exception as e:
print(f"Display update error: {e}")
class WigleConfig:
def __init__(self):
self.api_name = ""
self.api_token = ""
self.load_config()
def load_config(self):
if os.path.exists(CONFIG_FILE):
with open(CONFIG_FILE, 'r') as f:
config = json.load(f)
self.api_name = config.get('api_name', '')
self.api_token = config.get('api_token', '')
else:
self.create_default_config()
def create_default_config(self):
config = {
'api_name': 'YOUR_WIGLE_API_NAME',
'api_token': 'YOUR_WIGLE_API_TOKEN',
'scan_interval': SCAN_INTERVAL,
'upload_interval': UPLOAD_INTERVAL
}
with open(CONFIG_FILE, 'w') as f:
json.dump(config, f, indent=4)
print(f"Config created: {CONFIG_FILE}")
class BluetoothScanner:
def __init__(self, config, display):
self.config = config
self.display = display
self.csv_file = None
self.csv_writer = None
self.devices_found = 0
self.total_scans = 0
self.last_upload = None
self.status_msg = "Initializing..."
self.setup_csv()
def setup_csv(self):
Path(CSV_DIR).mkdir(parents=True, exist_ok=True)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"{CSV_DIR}/wartooth_{timestamp}.csv"
self.csv_file = open(filename, 'w', newline='')
self.csv_writer = csv.writer(self.csv_file)
self.csv_writer.writerow([
'WigleWifi-1.4',
'appRelease=2.0',
'model=Wartooth',
'release=RaspberryPiZero2W',
'device=btscanner',
'display=waveshare144',
'board=bcm2710',
'brand=wartooth'
])
self.csv_writer.writerow([
'MAC', 'SSID', 'AuthMode', 'FirstSeen', 'Channel',
'RSSI', 'CurrentLatitude', 'CurrentLongitude',
'AltitudeMeters', 'AccuracyMeters', 'Type'
])
self.csv_file.flush()
print(f"CSV created: {filename}")
self.current_csv = filename
def scan_bluetooth(self):
try:
self.status_msg = "Scanning..."
self.update_display()
subprocess.run(['sudo', 'hciconfig', 'hci0', 'up'],
capture_output=True, timeout=5)
result = subprocess.run(
['sudo', 'hcitool', 'scan', '--flush'],
capture_output=True,
text=True,
timeout=15
)
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
new_devices = 0
for line in result.stdout.split('\n'):
line = line.strip()
if line and not line.startswith('Scanning'):
parts = line.split(maxsplit=1)
if len(parts) >= 1:
mac = parts[0]
name = parts[1] if len(parts) > 1 else 'Unknown'
self.csv_writer.writerow([
mac,
name,
'[BT]',
timestamp,
'0',
'0',
'0.0',
'0.0',
'0.0',
'0.0',
'BT'
])
new_devices += 1
self.csv_file.flush()
self.devices_found += new_devices
self.total_scans += 1
self.status_msg = f"OK: {new_devices} new"
print(f"Scan #{self.total_scans}: {new_devices} devices")
except subprocess.TimeoutExpired:
self.status_msg = "Scan timeout"
print("Scan timeout")
except Exception as e:
self.status_msg = f"Error: {str(e)[:20]}"
print(f"Scan error: {e}")
self.update_display()
def upload_to_wigle(self):
if not self.config.api_name or self.config.api_name == 'YOUR_WIGLE_API_NAME':
self.status_msg = "No API keys"
self.update_display()
return
if not os.path.exists(self.current_csv):
self.status_msg = "No data"
self.update_display()
return
try:
self.status_msg = "Uploading..."
self.update_display()
with open(self.current_csv, 'rb') as f:
files = {'file': (os.path.basename(self.current_csv), f, 'text/csv')}
auth = (self.config.api_name, self.config.api_token)
response = requests.post(
'https://api.wigle.net/api/v2/file/upload',
auth=auth,
files=files,
timeout=30
)
if response.status_code == 200:
self.status_msg = "Upload OK"
print(f"Upload successful")
self.last_upload = datetime.now()
self.csv_file.close()
self.setup_csv()
else:
self.status_msg = f"Error {response.status_code}"
print(f"Upload failed: {response.status_code}")
except Exception as e:
self.status_msg = f"Error: {str(e)[:15]}"
print(f"Upload error: {e}")
self.update_display()
def update_display(self):
self.display.show_status(
self.devices_found,
self.total_scans,
self.last_upload,
self.status_msg
)
def run(self):
last_upload_time = time.time()
self.status_msg = "Ready"
self.update_display()
while True:
self.scan_bluetooth()
if time.time() - last_upload_time >= UPLOAD_INTERVAL:
self.upload_to_wigle()
last_upload_time = time.time()
time.sleep(SCAN_INTERVAL)
def main():
print("=" * 50)
print("Wartooth - Bluetooth Wardriving Scanner v2.0")
print("=" * 50)
config = WigleConfig()
if config.api_name == 'YOUR_WIGLE_API_NAME':
print(f"\nWARNING: API not configured!")
print(f"Edit: {CONFIG_FILE}\n")
display = Display()
if display.available:
print("Display OK")
else:
print("No display")
scanner = BluetoothScanner(config, display)
try:
scanner.run()
except KeyboardInterrupt:
print("\n\nStopped")
if scanner.csv_file:
scanner.csv_file.close()
if display.available:
GPIO.cleanup()
if __name__ == "__main__":
main()