#!/usr/bin/env python3 """CSV parsing, interface discovery, and monitor-mode + card-state handling.""" import os import re import subprocess import sys import tempfile import time def run(cmd, timeout=30): return subprocess.run(cmd, capture_output=True, text=True, timeout=timeout) def list_wifi_interfaces(): """[{iface, phy, type, addr, monitor_capable}] from `iw dev`.""" try: out = run(["iw", "dev"]).stdout except Exception: return [] ifaces, cur_phy, cur = [], None, None for line in out.splitlines(): m = re.match(r"phy#(\d+)", line) if m: cur_phy = "phy" + m.group(1) m = re.match(r"\s+Interface (\S+)", line) if m: if cur: ifaces.append(cur) cur = {"iface": m.group(1), "phy": cur_phy, "type": "?", "addr": "?"} m = re.match(r"\s+addr (\S+)", line) if m and cur: cur["addr"] = m.group(1) m = re.match(r"\s+type (\S+)", line) if m and cur: cur["type"] = m.group(1) if cur: ifaces.append(cur) for it in ifaces: it["monitor_capable"] = _phy_has_monitor(it["phy"]) return ifaces def _phy_has_monitor(phy): if not phy: return False out = run(["iw", phy, "info"]).stdout grab = False for line in out.splitlines(): if "Supported interface modes:" in line: grab = True continue if grab: if re.match(r"\s+\*", line): if "monitor" in line: return True else: break return False class CardState: def __init__(self, iface): self.orig_iface = iface["iface"] self.orig_addr = iface["addr"] self.nm_was_active = ( run(["systemctl", "is-active", "NetworkManager"]).stdout.strip() == "active" ) self.mon_iface = None self.used_airmon = False def enable_monitor(state): run(["airmon-ng", "check", "kill"]) before = {i["iface"] for i in list_wifi_interfaces()} res = run(["airmon-ng", "start", state.orig_iface]) state.used_airmon = True time.sleep(1) after = list_wifi_interfaces() new = {i["iface"] for i in after} - before if new: state.mon_iface = new.pop() else: m = re.search(r"enabled (?:for \[[^\]]+\]\S+ )?on (?:\[[^\]]+\])?(\S+?)\)", res.stdout) if m: state.mon_iface = m.group(1) else: for i in after: if i["iface"] == state.orig_iface and i["type"] == "monitor": state.mon_iface = state.orig_iface if not state.mon_iface: raise RuntimeError("Could not determine monitor interface:\n" + res.stdout + res.stderr) return state.mon_iface def reset_card(state): if not state: return if state.mon_iface and state.used_airmon: run(["airmon-ng", "stop", state.mon_iface]) names = {i["iface"]: i for i in list_wifi_interfaces()} if state.orig_iface not in names and state.mon_iface in names: run(["iw", state.mon_iface, "set", "type", "managed"]) run(["ip", "link", "set", state.mon_iface, "name", state.orig_iface]) names = {i["iface"]: i for i in list_wifi_interfaces()} it = names.get(state.orig_iface) if it and state.orig_addr not in ("?", None) and it["addr"].lower() != state.orig_addr.lower(): run(["ip", "link", "set", state.orig_iface, "down"]) run(["ip", "link", "set", state.orig_iface, "address", state.orig_addr]) run(["ip", "link", "set", state.orig_iface, "up"]) if state.nm_was_active: run(["systemctl", "start", "NetworkManager"]) def parse_airodump_csv(path): try: with open(path, encoding="utf-8", errors="replace") as f: text = f.read() except OSError: return [], [] aps, stations, section = [], [], None for line in text.splitlines(): s = line.strip() if not s: continue if s.startswith("BSSID,"): section = "ap" continue if s.startswith("Station MAC,"): section = "sta" continue # naive comma split; an ESSID containing a comma will skew fields. parts = [c.strip() for c in line.split(",")] if section == "ap" and len(parts) >= 14: aps.append({ "bssid": parts[0], "channel": parts[3], "power": parts[8], "privacy": parts[5], "essid": parts[13], }) elif section == "sta" and len(parts) >= 6: stations.append({ "station": parts[0], "power": parts[3], "packets": parts[4], "bssid": parts[5], }) return aps, stations def _selftest(): sample = ( "BSSID, First time seen, Last time seen, channel, Speed, Privacy, Cipher, " "Authentication, Power, # beacons, # IV, LAN IP, ID-length, ESSID, Key\r\n" "\r\n" "AA:BB:CC:DD:EE:FF, 2024-01-01 00:00:00, 2024-01-01 00:01:00, 6, 130, " "WPA2, CCMP, PSK, -40, 10, 0, 0.0.0.0, 6, MyWiFi, \r\n" "\r\n" "Station MAC, First time seen, Last time seen, Power, # packets, BSSID, " "Probed ESSIDs\r\n" "\r\n" "11:22:33:44:55:66, 2024-01-01 00:00:10, 2024-01-01 00:01:00, -50, 25, " "AA:BB:CC:DD:EE:FF, MyWiFi\r\n" "99:88:77:66:55:44, 2024-01-01 00:00:10, 2024-01-01 00:01:00, -60, 5, " "(not associated), Other\r\n" ) p = os.path.join(tempfile.gettempdir(), "wd_selftest.csv") with open(p, "w") as f: f.write(sample) aps, sts = parse_airodump_csv(p) os.remove(p) assert len(aps) == 1, aps assert aps[0]["essid"] == "MyWiFi" and aps[0]["channel"] == "6", aps assert len(sts) == 2, sts assoc = [s for s in sts if s["bssid"].lower() == "aa:bb:cc:dd:ee:ff"] assert len(assoc) == 1 and assoc[0]["station"] == "11:22:33:44:55:66", sts print("selftest ok") if __name__ == "__main__": if "--selftest" in sys.argv: _selftest() sys.exit(0)