-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathhs_capture.py
More file actions
340 lines (284 loc) · 12.3 KB
/
Copy pathhs_capture.py
File metadata and controls
340 lines (284 loc) · 12.3 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
#!/usr/bin/env python3
"""
hs_capture.py — WatchDogsGo Handshake Capture Script
Standalone handshake/PMKID capture for AWUS036ACM (wlan2) via airodump-ng.
Dispatched by wdg_wifi_bridge.py on start_handshake command.
Prints SSID:name AP:bssid to stdout for each new capture — the bridge
streams these to the game to trigger the handshake event (200 XP + badge).
Usage (via bridge dispatch):
python3 hs_capture.py --iface wlan2 --loot-dir /path/to/loot
Usage (standalone test):
sudo python3 hs_capture.py --iface wlan2 --loot-dir ./loot
Dependencies: airodump-ng, hcxpcapngtool (system), running as root
"""
import argparse
import logging
import os
import re
import signal
import subprocess
import sys
import time
log = logging.getLogger("hs_capture")
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)s %(message)s",
stream=sys.stderr, # stderr so stdout stays clean for bridge
)
# Seconds between hcxpcapngtool polls
POLL_INTERVAL = 10.0
def set_monitor_mode(iface: str) -> bool:
try:
subprocess.run(["ip", "link", "set", iface, "down"], check=True,
capture_output=True)
subprocess.run(["iw", "dev", iface, "set", "type", "monitor"], check=True,
capture_output=True)
subprocess.run(["ip", "link", "set", iface, "up"], check=True,
capture_output=True)
log.info("Set %s to monitor mode", iface)
return True
except subprocess.CalledProcessError as e:
log.error("Failed to set monitor mode on %s: %s", iface, e)
return False
def restore_managed_mode(iface: str):
try:
subprocess.run(["ip", "link", "set", iface, "down"], check=False,
capture_output=True)
subprocess.run(["iw", "dev", iface, "set", "type", "managed"], check=False,
capture_output=True)
subprocess.run(["ip", "link", "set", iface, "up"], check=False,
capture_output=True)
log.info("Restored %s to managed mode", iface)
except Exception as e:
log.warning("Could not restore managed mode on %s: %s", iface, e)
def check_tool(name: str) -> bool:
import shutil
return shutil.which(name) is not None
class HsCapture:
def __init__(self, iface: str, loot_dir: str):
self.iface = iface
self.loot_dir = loot_dir
self._proc = None
self._running = False
self._hs_file = ""
self._hs_prefix = ""
self._seen_bssids: set = set()
def start(self):
if not check_tool("airodump-ng"):
print("error: airodump-ng not installed", flush=True)
sys.exit(1)
if not check_tool("hcxpcapngtool"):
print("error: hcxpcapngtool not installed", flush=True)
sys.exit(1)
# Set up signal handlers for clean teardown
signal.signal(signal.SIGTERM, self._handle_signal)
signal.signal(signal.SIGINT, self._handle_signal)
hs_dir = os.path.join(self.loot_dir, "handshakes")
os.makedirs(hs_dir, exist_ok=True)
ts = time.strftime("%Y%m%d_%H%M%S")
self._hs_prefix = os.path.join(hs_dir, f"hs_{ts}")
# airodump-ng appends -01.cap to the prefix
self._hs_file = self._hs_prefix + "-01.cap"
# Check if already in monitor mode (e.g. left by deauth)
check = subprocess.run(
["iw", "dev", self.iface, "info"],
capture_output=True, text=True
)
already_monitor = "type monitor" in check.stdout
if not already_monitor:
if not set_monitor_mode(self.iface):
print(f"error: could not set {self.iface} to monitor mode", flush=True)
sys.exit(1)
else:
log.info("%s already in monitor mode — skipping mode set", self.iface)
try:
self._proc = subprocess.Popen(
["airodump-ng", self.iface,
"-w", self._hs_prefix,
"--output-format", "pcapng"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
except Exception as e:
log.error("airodump-ng failed to start: %s", e)
print(f"error: airodump-ng failed: {e}", flush=True)
restore_managed_mode(self.iface)
sys.exit(1)
self._running = True
log.info("airodump-ng running on %s, saving to %s", self.iface, self._hs_file)
print(f"handshake capture started on {self.iface}", flush=True)
self._poll_loop()
def _poll_loop(self):
"""Poll hcxpcapngtool every POLL_INTERVAL seconds for new hashes."""
hash_file = self._hs_prefix + "-01.hc22000"
while self._running:
# Sleep in short intervals so SIGTERM wakes us quickly
for _ in range(int(POLL_INTERVAL / 0.5)):
if not self._running:
break
time.sleep(0.5)
if not self._running:
break
# Check if airodump-ng died unexpectedly
if self._proc and self._proc.poll() is not None:
log.warning("airodump-ng exited unexpectedly (rc=%d)", self._proc.returncode)
print("error: airodump-ng exited unexpectedly", flush=True)
break
# Check for capture file
cap_file = self._hs_prefix + "-01.cap"
if not os.path.exists(cap_file):
# Try pcapng extension
cap_file = self._hs_prefix + "-01.pcapng"
if not os.path.exists(cap_file):
log.debug("No capture file yet")
continue
try:
result = subprocess.run(
["hcxpcapngtool", cap_file, "-o", hash_file],
capture_output=True, text=True, timeout=15
)
output = result.stdout + result.stderr
eapol_m = re.search(
r"EAPOL pairs written to 22000 hash file[^:]*:\s*(\d+)", output)
pmkid_m = re.search(
r"PMKID written to 22000 hash file[^:]*:\s*(\d+)", output)
eapol_count = int(eapol_m.group(1)) if eapol_m else 0
pmkid_count = int(pmkid_m.group(1)) if pmkid_m else 0
log.debug("Poll: %d EAPOL, %d PMKID", eapol_count, pmkid_count)
if os.path.exists(hash_file):
self._process_hash_file(hash_file)
except subprocess.TimeoutExpired:
log.warning("hcxpcapngtool timed out")
except Exception as e:
log.debug("Poll error: %s", e)
self._cleanup()
def _process_hash_file(self, hash_file: str):
"""Parse hc22000 file, emit SSID:/AP: lines, and write per-BSSID loot files."""
try:
with open(hash_file, "r") as f:
lines = f.readlines()
except Exception as e:
log.debug("Hash file read error: %s", e)
return
for line in lines:
parts = line.strip().split("*")
if len(parts) < 4:
continue
# Handle both WPA* and 22000* hash formats
if parts[0] == "WPA":
if len(parts) < 6:
continue
bssid_hex = parts[3]
essid_hex = parts[5]
cap_type = "EAPOL" if parts[1] == "02" else "PMKID"
elif parts[0] in ("22000", "22301"):
bssid_hex = parts[1]
essid_hex = parts[3]
cap_type = "PMKID" if parts[0] == "22301" else "EAPOL"
else:
continue
# Validate bssid_hex before formatting
if len(bssid_hex) < 12:
log.debug("Skipping malformed hash line: bssid_hex=%r", bssid_hex)
continue
try:
bssid = ":".join(
bssid_hex[i:i+2] for i in range(0, 12, 2)
).upper()
essid = bytes.fromhex(essid_hex).decode(
"utf-8", errors="replace")
except Exception:
bssid = bssid_hex
essid = ""
if bssid not in self._seen_bssids:
self._seen_bssids.add(bssid)
log.info("New handshake: %s %s (%s)", bssid, essid, cap_type)
# Write per-BSSID loot files matching loot_manager expectations:
# handshakes/<ssid>_<bssid>.txt / .22000 / .pcap
self._write_loot_files(bssid, essid, line.strip(), cap_type)
# This line is what the bridge writes to the game
print(f"SSID:{essid} AP:{bssid}", flush=True)
def _find_active_session(self) -> str:
"""Find the most recently modified session directory in loot_dir."""
import glob
pattern = os.path.join(self.loot_dir, "????-??-??_??-??-??")
sessions = sorted(glob.glob(pattern), key=os.path.getmtime, reverse=True)
if sessions:
return os.path.join(sessions[0], "handshakes")
return os.path.join(self.loot_dir, "handshakes")
def _write_loot_files(self, bssid: str, essid: str, hash_line: str, cap_type: str):
"""Write per-BSSID loot files matching loot_manager.py naming convention."""
import shutil
# Sanitize for filename
safe_ssid = re.sub(r"[^\w\-]", "_", essid) if essid else "hidden"
safe_bssid = bssid.replace(":", "")
hs_dir = self._find_active_session()
os.makedirs(hs_dir, exist_ok=True)
base = os.path.join(hs_dir, f"{safe_ssid}_{safe_bssid}")
# .txt metadata
try:
with open(base + ".txt", "w") as f:
f.write(f"ssid={essid}\n")
f.write(f"bssid={bssid}\n")
f.write(f"type={cap_type}\n")
f.write(f"captured={time.strftime('%Y-%m-%d %H:%M:%S')}\n")
f.write(f"source=hs_capture.py\n")
log.debug("Wrote %s.txt", base)
except Exception as e:
log.debug("Could not write .txt: %s", e)
# .22000 hash file — just this BSSID's line
try:
with open(base + ".22000", "w") as f:
f.write(hash_line + "\n")
log.debug("Wrote %s.22000", base)
except Exception as e:
log.debug("Could not write .22000: %s", e)
# .pcap — extract BSSID-filtered frames from combined cap via tshark
cap_file = self._hs_prefix + "-01.cap"
if not os.path.exists(cap_file):
cap_file = self._hs_prefix + "-01.pcapng"
if os.path.exists(cap_file) and shutil.which("tshark"):
try:
subprocess.run(
["tshark", "-r", cap_file,
"-w", base + ".pcap",
"-Y", f"wlan.bssid == {bssid}"],
capture_output=True, timeout=30
)
log.debug("Wrote %s.pcap", base)
except Exception as e:
log.debug("tshark pcap extraction failed: %s", e)
def _handle_signal(self, signum, frame):
log.info("Signal %d received — stopping", signum)
self._running = False
def _cleanup(self):
if self._proc:
try:
self._proc.terminate()
self._proc.wait(timeout=5)
except Exception:
try:
self._proc.kill()
except Exception:
pass
self._proc = None
restore_managed_mode(self.iface)
captured = len(self._seen_bssids)
log.info("Capture complete: %d unique handshakes", captured)
print(f"handshake capture stopped — {captured} captured", flush=True)
def main():
parser = argparse.ArgumentParser(
description="WatchDogsGo Handshake Capture — dispatched by wdg_wifi_bridge.py")
parser.add_argument("--iface", default="wlan2",
help="Monitor mode interface (default: wlan2)")
parser.add_argument("--loot-dir", default="",
help="Loot directory for captures")
args = parser.parse_args()
if os.geteuid() != 0:
print("error: must run as root", file=sys.stderr)
sys.exit(1)
loot_dir = args.loot_dir or os.path.expanduser("~/python/WatchDogsGo/loot")
capture = HsCapture(iface=args.iface, loot_dir=loot_dir)
capture.start()
if __name__ == "__main__":
main()