-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·151 lines (126 loc) · 4.39 KB
/
main.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
#!/usr/bin/env python3
import sys
import subprocess
import time
import signal
import atexit
import sqlite3
from pprint import pprint
from scapy.layers.dot11 import *
db = None
cur = None
def initdb():
global db, cur
db = sqlite3.connect("signals.db")
cur = db.cursor()
cur.execute("""CREATE TABLE IF NOT EXISTS "SIGNALS" (
"TIME" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
"SSID" TEXT,
"MAC" TEXT,
"BSSID" TEXT,
"SIGNAL" INTEGER,
"RSSI" INTEGER,
"FREQ" INTEGER,
"CH" INTEGER
);""")
def cleanup(card):
print("cleanup")
subprocess.run(["sudo", "ip", "link", "set", card, "down"])
subprocess.run(["sudo", "iw", "dev", card, "set", "type", "managed"])
subprocess.run(["sudo", "ip", "link", "set", card, "up"])
def signal_handler(sig, frame):
exit(0)
def lerp(x1: float, x2: float, y1: float, y2: float, d: float):
return ((y2 - y1) * d + x2 * y1 - x1 * y2) / (x2 - x1)
def getChannel(frequency):
base = 2407 # 2.4Ghz
if frequency//1000 == 5:
base = 5000 # 5Ghz
# 2.4 and 5Ghz channels increment by 5
return (frequency-base)//5
def monitor_mode(card):
try:
mode = None
while mode != "monitor":
iw = subprocess.run(["iw", card, "info"], capture_output=True, text=True).stdout
mode = iw.split("type ", 1)[1].split("\n", 1)[0]
print("Wifi card is in", mode, "mode")
if mode == "managed":
print("Setting up monitor mode")
subprocess.run(["sudo", "airmon-ng", "check", "kill"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
subprocess.run(["sudo", "ip", "link", "set", card, "down"])
subprocess.run(["sudo", "iw", "dev", card, "set", "type", "monitor"])
subprocess.run(["sudo", "ip", "link", "set", card, "up"])
return True
except:
print("DANGER WILL ROBINSON")
return False
def packet_handler(p):
global db
if p.haslayer(Dot11ProbeReq):
# Extract SSID from the packet
if p[Dot11ProbeReq].info:
print("Probe Request")
print("==============================")
# Extract SSID from the packet
try:
ssid = p[Dot11ProbeReq].info.decode()
except UnicodeDecodeError:
return
# Extract MAC address of the client sending the probe request
client_mac = p.addr2
# Extract MAC address of the access point (BSSID)
bssid = p.addr3
# Extract signal strength percentage
# Mapping the range of -100 to -50 to the range of 0-100%
signal_strength = lerp(-100, -50, 0, 100, p[RadioTap].dBm_AntSignal)
signal_strength = min(signal_strength, 100)
# Extract RSSI (dBm)
rssi = p[RadioTap].dBm_AntSignal
# Get timestamp
timestamp = int(time.time())
# Extract frequency (MHz)
frequency = p[RadioTap].ChannelFrequency
# Extract channel
channel = getChannel(frequency)
# Display the extracted information
print(f"SSID: {ssid}")
print(f"Client MAC: {client_mac}")
print(f"BSSID: {bssid}")
print(f"Signal Strength: {signal_strength}%")
print(f"RSSI: {rssi}")
print(f"Timestamp: {timestamp}")
print(f"Frequency: {frequency} MHz")
print(f"Channel: {channel}")
print("==============================\n")
cur.execute(
"""INSERT INTO "main"."SIGNALS" ("SSID", "MAC", "BSSID", "SIGNAL", "RSSI", "FREQ", "CH") VALUES (?, ?, ?, ?, ?, ?, ?);""",
(
ssid,
client_mac,
bssid,
signal_strength,
rssi,
frequency,
channel
)
)
db.commit()
def capture_probes(card):
sniff(
iface=card,
prn=packet_handler,
store=0
)
def main():
# TODO: Better arg parsing
card = sys.argv[1]
if not monitor_mode(card):
print("No Monitor mode")
atexit.register(cleanup, card)
signal.signal(signal.SIGINT, signal_handler)
initdb()
while True:
capture_probes(card)
if __name__ == '__main__':
main()