Better security, width, vendor, and type detection
This commit is contained in:
1 parent
44b9e1be09
commit
bc2c3168bc
2 files changed
+321
-116
No files matched your search
@@ -322,6 +322,18 @@ class WiFiHeatmapApp:
|
||||
tk.Label(auto_bar, text="seconds",
|
||||
font=("Courier", 8), bg=BG3, fg=TEXT_DIM).pack(side="left", padx=(2, 16))
|
||||
|
||||
# Show Hidden toggle — when unchecked, networks with no SSID ("<hidden>")
|
||||
# are filtered out of the table. Defaults to unchecked.
|
||||
self._show_hidden_var = tk.BooleanVar(value=False)
|
||||
self.show_hidden_chk = tk.Checkbutton(
|
||||
auto_bar, variable=self._show_hidden_var,
|
||||
text="Show Hidden", font=("Courier", 8),
|
||||
bg=BG3, fg=ACCENT2, selectcolor=BG2,
|
||||
activebackground=BG3, activeforeground=ACCENT2,
|
||||
relief="flat", cursor="hand2",
|
||||
command=self._on_show_hidden_toggle)
|
||||
self.show_hidden_chk.pack(side="left", padx=(0, 16))
|
||||
|
||||
self.auto_scan_countdown = tk.Label(auto_bar, text="",
|
||||
font=("Courier", 8, "bold"), bg=BG3, fg=WARNING)
|
||||
self.auto_scan_countdown.pack(side="left")
|
||||
@@ -448,14 +460,25 @@ class WiFiHeatmapApp:
|
||||
now = time.monotonic()
|
||||
all_bssids = self._known_bssids_ordered()
|
||||
|
||||
for i, bssid in enumerate(all_bssids):
|
||||
show_hidden = self._show_hidden_var.get()
|
||||
visible_idx = 0
|
||||
for bssid in all_bssids:
|
||||
ap = current_map.get(bssid)
|
||||
stats = self._ap_stats.get(bssid)
|
||||
is_live = ap is not None
|
||||
row_num = i + 1 # row 0 is the header
|
||||
|
||||
row_bg = BG if i % 2 == 0 else BG2
|
||||
# Resolve SSID early so hidden networks can be filtered before any
|
||||
# widgets are built. Use last-known SSID from stats when AP is stale.
|
||||
ssid = ap.ssid if ap else self._ap_stats_ssid(bssid)
|
||||
|
||||
# "Show Hidden" unchecked → skip networks broadcasting no SSID.
|
||||
if not show_hidden and (not ssid or ssid == "<hidden>"):
|
||||
continue
|
||||
|
||||
row_num = visible_idx + 1 # row 0 is the header
|
||||
row_bg = BG if visible_idx % 2 == 0 else BG2
|
||||
dim_fg = "#3a3a5a" # very dim — used for stale rows
|
||||
visible_idx += 1
|
||||
|
||||
row = tk.Frame(self.ap_inner, bg=row_bg)
|
||||
row.grid(row=row_num, column=0, sticky="ew")
|
||||
@@ -479,7 +502,6 @@ class WiFiHeatmapApp:
|
||||
anchor="w", padx=4).place(relwidth=1, relheight=1)
|
||||
|
||||
# Resolve display values — use last-known from stats when AP is stale
|
||||
ssid = ap.ssid if ap else self._ap_stats_ssid(bssid)
|
||||
channel = str(ap.channel) if ap and ap.channel else "—"
|
||||
freq = ap.freq_label() if ap else "—"
|
||||
width_s = ap.channel_width or "—" if ap else "—"
|
||||
@@ -578,6 +600,10 @@ class WiFiHeatmapApp:
|
||||
self._sync_heatmap_bssids()
|
||||
self._update_heatmap_ap_selector()
|
||||
|
||||
def _on_show_hidden_toggle(self):
|
||||
"""Rebuild the AP table when the Show Hidden checkbox changes."""
|
||||
self._populate_ap_table(self.last_scan)
|
||||
|
||||
def _ap_select_all(self):
|
||||
self.selected_bssids = {r["bssid"] for r in self._ap_row_widgets}
|
||||
for row in self._ap_row_widgets:
|
||||
|
||||
+291
-112
@@ -23,17 +23,27 @@ is more robust against the burst spikes that WiFi RSSI produces and better
|
||||
matches what professional tools such as NetSpot report.
|
||||
"""
|
||||
|
||||
import csv
|
||||
import subprocess
|
||||
import platform
|
||||
import re
|
||||
import threading
|
||||
import time
|
||||
import statistics
|
||||
import urllib.request
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
|
||||
# ── OUI vendor table ──────────────────────────────────────────────────────────
|
||||
_OUI_TABLE: dict[str, str] = {
|
||||
# ── OUI vendor lookup — IEEE database with built-in fallback ─────────────────
|
||||
|
||||
_OUI_CACHE = Path.home() / ".pyfi" / "oui.csv"
|
||||
_OUI_URL = "https://standards-oui.ieee.org/oui/oui.csv"
|
||||
_OUI_MAX_AGE_DAYS = 30
|
||||
|
||||
# Small built-in table used until the full IEEE DB loads (or if offline forever)
|
||||
_OUI_BUILTIN: dict[str, str] = {
|
||||
"000C29": "Cisco", "001A2F": "Cisco", "00E0F7": "Cisco",
|
||||
"00173F": "Linksys", "001346": "Linksys",
|
||||
"001B2F": "Netgear", "00224C": "Netgear", "20E52A": "Netgear",
|
||||
@@ -44,42 +54,86 @@ _OUI_TABLE: dict[str, str] = {
|
||||
"107B44": "ASUS", "1C872C": "ASUS", "2C56DC": "ASUS",
|
||||
"305A3A": "ASUS", "382C4A": "ASUS", "40167E": "ASUS",
|
||||
"50465D": "ASUS", "6045CB": "ASUS", "AC220B": "ASUS",
|
||||
"000A27": "Apple", "000A95": "Apple", "001124": "Apple",
|
||||
"001451": "Apple", "001B63": "Apple", "001CB3": "Apple",
|
||||
"001E52": "Apple", "001FF3": "Apple", "0021E9": "Apple",
|
||||
"0023DF": "Apple", "002500": "Apple", "3C0754": "Apple",
|
||||
"70CD60": "Apple", "A45E60": "Apple", "F0DBF8": "Apple",
|
||||
"001632": "Samsung", "002339": "Samsung", "5425EA": "Samsung",
|
||||
"000A27": "Apple", "000A95": "Apple", "001124": "Apple",
|
||||
"001451": "Apple", "001B63": "Apple", "001CB3": "Apple",
|
||||
"001E52": "Apple", "001FF3": "Apple", "0021E9": "Apple",
|
||||
"0023DF": "Apple", "002500": "Apple", "3C0754": "Apple",
|
||||
"70CD60": "Apple", "A45E60": "Apple", "F0DBF8": "Apple",
|
||||
"001632": "Samsung", "002339": "Samsung", "5425EA": "Samsung",
|
||||
"8C7712": "Samsung", "A0821F": "Samsung",
|
||||
"001195": "D-Link", "0015E9": "D-Link", "001CF0": "D-Link",
|
||||
"001195": "D-Link", "0015E9": "D-Link", "001CF0": "D-Link",
|
||||
"1C7EE5": "D-Link", "280DFC": "D-Link",
|
||||
"002722": "Ubiquiti", "04186A": "Ubiquiti", "0418D6": "Ubiquiti",
|
||||
"24A43C": "Ubiquiti", "44D9E7": "Ubiquiti", "687278": "Ubiquiti",
|
||||
"002722": "Ubiquiti", "04186A": "Ubiquiti", "0418D6": "Ubiquiti",
|
||||
"24A43C": "Ubiquiti", "44D9E7": "Ubiquiti", "687278": "Ubiquiti",
|
||||
"788A20": "Ubiquiti", "802AA8": "Ubiquiti",
|
||||
"000B86": "Aruba", "001A1E": "Aruba", "20A6CD": "Aruba",
|
||||
"000B86": "Aruba", "001A1E": "Aruba", "20A6CD": "Aruba",
|
||||
"6C3B6B": "Aruba", "94B4CF": "Aruba",
|
||||
"F88FCA": "Google", "1AC487": "Google", "54607E": "Google",
|
||||
"F88FCA": "Google", "1AC487": "Google", "54607E": "Google",
|
||||
"A47733": "Google", "3C5AB4": "Google",
|
||||
"F0272D": "Amazon", "44650D": "Amazon", "AC63BE": "Amazon",
|
||||
"F0272D": "Amazon", "44650D": "Amazon", "AC63BE": "Amazon",
|
||||
"34D270": "Amazon",
|
||||
"001150": "Belkin", "001CDF": "Belkin", "0030BD": "Belkin",
|
||||
"001150": "Belkin", "001CDF": "Belkin", "0030BD": "Belkin",
|
||||
"94103E": "Belkin", "EC1A59": "Belkin",
|
||||
"001E10": "Huawei", "002568": "Huawei", "28311C": "Huawei",
|
||||
"3440B5": "Huawei", "4C1FCC": "Huawei", "6C8D37": "Huawei",
|
||||
"001E10": "Huawei", "002568": "Huawei", "28311C": "Huawei",
|
||||
"3440B5": "Huawei", "4C1FCC": "Huawei", "6C8D37": "Huawei",
|
||||
"788C8A": "Huawei", "AC853D": "Huawei",
|
||||
"0012F0": "Intel", "001320": "Intel", "0016EA": "Intel",
|
||||
"0012F0": "Intel", "001320": "Intel", "0016EA": "Intel",
|
||||
"001E64": "Intel", "002129": "Intel",
|
||||
"000F66": "Qualcomm", "001374": "Qualcomm",
|
||||
"000AF7": "Broadcom", "00904C": "Broadcom",
|
||||
"000C43": "Ralink", "00E04C": "Ralink",
|
||||
"4C5E0C": "MikroTik", "CC2DE0": "MikroTik", "E48D8C": "MikroTik",
|
||||
"4C5E0C": "MikroTik", "CC2DE0": "MikroTik", "E48D8C": "MikroTik",
|
||||
"B8690E": "MikroTik",
|
||||
}
|
||||
|
||||
_oui_db: dict[str, str] = {}
|
||||
_oui_lock: threading.Lock = threading.Lock()
|
||||
|
||||
|
||||
def _parse_oui_csv(path: Path) -> dict[str, str]:
|
||||
db: dict[str, str] = {}
|
||||
with open(path, newline="", encoding="utf-8", errors="replace") as f:
|
||||
for row in csv.reader(f):
|
||||
if len(row) >= 3 and len(row[1]) == 6:
|
||||
db[row[1].upper()] = row[2].strip()
|
||||
return db
|
||||
|
||||
|
||||
def _load_oui_db_worker() -> None:
|
||||
global _oui_db
|
||||
try:
|
||||
needs_download = (
|
||||
not _OUI_CACHE.exists()
|
||||
or (time.time() - _OUI_CACHE.stat().st_mtime) > _OUI_MAX_AGE_DAYS * 86400
|
||||
)
|
||||
if needs_download:
|
||||
_OUI_CACHE.parent.mkdir(parents=True, exist_ok=True)
|
||||
urllib.request.urlretrieve(_OUI_URL, _OUI_CACHE)
|
||||
db = _parse_oui_csv(_OUI_CACHE)
|
||||
with _oui_lock:
|
||||
_oui_db = db
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
# Start loading in the background immediately so it's ready before the first scan
|
||||
threading.Thread(target=_load_oui_db_worker, daemon=True).start()
|
||||
|
||||
|
||||
def lookup_vendor(bssid: str) -> str:
|
||||
oui = bssid.upper().replace(":", "").replace("-", "")[:6]
|
||||
return _OUI_TABLE.get(oui, "-")
|
||||
hexstr = bssid.upper().replace(":", "").replace("-", "")
|
||||
# Locally-administered MAC (U/L bit, 0x02, set in the first octet): the OUI
|
||||
# is randomized — not a real IEEE vendor assignment — so a lookup is
|
||||
# meaningless. These are the virtual/guest/mesh BSSIDs a single radio spawns.
|
||||
try:
|
||||
if int(hexstr[:2], 16) & 0x02:
|
||||
return "Local"
|
||||
except ValueError:
|
||||
pass
|
||||
oui = hexstr[:6]
|
||||
with _oui_lock:
|
||||
db = _oui_db
|
||||
return db.get(oui) or _OUI_BUILTIN.get(oui, "-")
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -208,6 +262,116 @@ def _freq_from_channel(ch: int) -> int:
|
||||
if 32 <= ch <= 177: return 5000 + ch * 5
|
||||
return 0
|
||||
|
||||
def _security_from_ies(ie_bytes: bytes) -> str:
|
||||
"""
|
||||
Determine security by walking the raw beacon / probe-response IEs.
|
||||
|
||||
This is the only reliable source for HIDDEN networks: their security can't
|
||||
be looked up by SSID via WlanGetAvailableNetworkList, but the RSN/WPA IEs
|
||||
are present in the beacon regardless of whether the SSID is broadcast.
|
||||
|
||||
Element 48 (RSN) → WPA2 / WPA3, with AKM suite type telling us
|
||||
PSK (Personal) vs SAE (WPA3) vs 802.1X (Enterprise).
|
||||
Element 221 (Vendor, OUI
|
||||
00:50:F2 type 1) → legacy WPA1.
|
||||
|
||||
Returns "" when no security IE is found, so the caller can fall back to the
|
||||
Privacy capability bit to distinguish Open from "secured but unknown".
|
||||
"""
|
||||
has_rsn = has_wpa1 = has_psk = has_sae = is_ent = is_owe = False
|
||||
i, n = 0, len(ie_bytes)
|
||||
while i + 2 <= n:
|
||||
eid = ie_bytes[i]
|
||||
ln = ie_bytes[i + 1]
|
||||
if i + 2 + ln > n:
|
||||
break
|
||||
body = ie_bytes[i + 2:i + 2 + ln]
|
||||
if eid == 48 and ln >= 8: # RSN IE (WPA2 / WPA3)
|
||||
has_rsn = True
|
||||
# RSN body: version[2] group-cipher[4] pairwise-count[2] ...
|
||||
# so the pairwise-cipher count starts at offset 6, the AKM list
|
||||
# follows the pairwise list.
|
||||
pairwise_count = int.from_bytes(body[6:8], "little")
|
||||
akm_off = 8 + pairwise_count * 4
|
||||
if akm_off + 2 <= len(body):
|
||||
akm_count = int.from_bytes(body[akm_off:akm_off + 2], "little")
|
||||
for k in range(akm_count):
|
||||
s = akm_off + 2 + k * 4
|
||||
if s + 4 <= len(body):
|
||||
t = body[s + 3] # AKM suite selector type
|
||||
if t in (1, 3, 5, 11, 12, 13): is_ent = True # 802.1X
|
||||
elif t in (8, 9): has_sae = True # SAE/WPA3
|
||||
elif t in (2, 4, 6): has_psk = True # PSK/WPA2
|
||||
elif t == 18: is_owe = True # OWE
|
||||
elif eid == 221 and ln >= 4 and bytes(body[:4]) == b"\x00\x50\xf2\x01":
|
||||
has_wpa1 = True # WPA1 vendor IE
|
||||
|
||||
i += 2 + ln
|
||||
|
||||
if is_owe:
|
||||
return "OWE (Enhanced Open)"
|
||||
if is_ent:
|
||||
return "WPA3-Enterprise" if has_sae else "WPA2-Enterprise"
|
||||
if has_sae and has_psk:
|
||||
return "WPA2/WPA3-Personal"
|
||||
if has_sae:
|
||||
return "WPA3-Personal"
|
||||
if has_rsn:
|
||||
return "WPA2-Personal"
|
||||
if has_wpa1:
|
||||
return "WPA-Personal"
|
||||
return ""
|
||||
|
||||
|
||||
def _mode_from_ies(ie_bytes: bytes, freq_mhz: int = 0) -> str:
|
||||
"""
|
||||
Determine the Wi-Fi generation by walking the raw 802.11 Information Elements
|
||||
from a beacon / probe response.
|
||||
|
||||
This is the authoritative source for Wi-Fi 7 detection on Windows: the scan
|
||||
list's dot11BssPhyType field caps at HE (10) and never reports EHT (11), so
|
||||
802.11be can only be recognised from the EHT Capabilities element here.
|
||||
|
||||
Relevant elements (all carried inside Element ID 255 = Element ID Extension):
|
||||
Ext ID 108 — EHT Capabilities → Wi-Fi 7 (802.11be)
|
||||
Ext ID 35 — HE Capabilities → Wi-Fi 6 (802.11ax)
|
||||
Ext ID 59 — HE 6 GHz Band Capabilities → confirms 6 GHz operation (6E)
|
||||
Plus the classic elements:
|
||||
Element 191 — VHT Capabilities → Wi-Fi 5 (802.11ac)
|
||||
Element 45 — HT Capabilities → Wi-Fi 4 (802.11n)
|
||||
"""
|
||||
has_eht = has_he = has_he6 = has_vht = has_ht = False
|
||||
i, n = 0, len(ie_bytes)
|
||||
while i + 2 <= n:
|
||||
eid = ie_bytes[i]
|
||||
ln = ie_bytes[i + 1]
|
||||
if i + 2 + ln > n:
|
||||
break
|
||||
body = ie_bytes[i + 2:i + 2 + ln]
|
||||
if eid == 45:
|
||||
has_ht = True
|
||||
elif eid == 191:
|
||||
has_vht = True
|
||||
elif eid == 255 and ln >= 1: # Element ID Extension
|
||||
ext = body[0]
|
||||
if ext == 108: has_eht = True
|
||||
elif ext == 35: has_he = True
|
||||
elif ext == 59: has_he6 = True
|
||||
i += 2 + ln
|
||||
|
||||
if has_eht:
|
||||
return "be (Wi-Fi 7)"
|
||||
if has_he:
|
||||
if has_he6 or 5925 <= freq_mhz <= 7125:
|
||||
return "ax (Wi-Fi 6E)"
|
||||
return "ax (Wi-Fi 6)"
|
||||
if has_vht:
|
||||
return "ac (Wi-Fi 5)"
|
||||
if has_ht:
|
||||
return "n (Wi-Fi 4)"
|
||||
return ""
|
||||
|
||||
|
||||
def _mode_from_flags(s: str) -> str:
|
||||
u = s.upper()
|
||||
if any(x in u for x in ("EHT", "802.11BE", "WIFI 7", "WI-FI 7")): return "be (Wi-Fi 7)"
|
||||
@@ -225,75 +389,6 @@ def _channel_width_from_str(s: str) -> str:
|
||||
if w in s: return f"{w} MHz"
|
||||
return s or "-"
|
||||
|
||||
def _parse_ie_security(ie_data: bytes) -> str:
|
||||
"""
|
||||
Walk raw 802.11 Information Elements and return a human-readable security string.
|
||||
|
||||
Relevant IEs:
|
||||
Tag 48 (0x30) — RSN IE: tells us WPA2-Personal/Enterprise and WPA3 (SAE AKM).
|
||||
Tag 221 (0xDD) — Vendor Specific: OUI 00:50:F2:01 is the WPA1 IE.
|
||||
|
||||
AKM suite types inside the RSN IE (last byte of the 4-byte OUI+type):
|
||||
1,3,5,11,12,13 → 802.1X variants (Enterprise)
|
||||
2,4,6 → PSK variants (WPA2-Personal)
|
||||
8,9 → SAE variants (WPA3-Personal)
|
||||
18 → OWE (Enhanced Open)
|
||||
"""
|
||||
has_rsn = False
|
||||
has_wpa1 = False
|
||||
has_psk = False
|
||||
has_sae = False
|
||||
is_enterprise= False
|
||||
is_owe = False
|
||||
|
||||
i = 0
|
||||
while i + 2 <= len(ie_data):
|
||||
tag_id = ie_data[i]
|
||||
tag_len = ie_data[i + 1]
|
||||
if i + 2 + tag_len > len(ie_data):
|
||||
break
|
||||
tag_data = ie_data[i + 2:i + 2 + tag_len]
|
||||
|
||||
if tag_id == 48 and tag_len >= 8: # RSN IE (WPA2 / WPA3)
|
||||
has_rsn = True
|
||||
pairwise_count = int.from_bytes(tag_data[4:6], "little")
|
||||
akm_off = 6 + pairwise_count * 4
|
||||
if akm_off + 2 <= len(tag_data):
|
||||
akm_count = int.from_bytes(tag_data[akm_off:akm_off + 2], "little")
|
||||
for k in range(akm_count):
|
||||
s = akm_off + 2 + k * 4
|
||||
if s + 4 <= len(tag_data):
|
||||
akm_type = tag_data[s + 3]
|
||||
if akm_type in (1, 3, 5, 11, 12, 13): # 802.1X / Enterprise
|
||||
is_enterprise = True
|
||||
elif akm_type in (8, 9): # SAE = WPA3-Personal
|
||||
has_sae = True
|
||||
elif akm_type in (2, 4, 6): # PSK = WPA2-Personal
|
||||
has_psk = True
|
||||
elif akm_type == 18: # OWE
|
||||
is_owe = True
|
||||
|
||||
elif tag_id == 221 and len(tag_data) >= 4: # Vendor Specific
|
||||
if tag_data[:4] == bytes([0x00, 0x50, 0xF2, 0x01]): # WPA1 IE
|
||||
has_wpa1 = True
|
||||
|
||||
i += 2 + tag_len
|
||||
|
||||
if is_owe:
|
||||
return "OWE (Enhanced Open)"
|
||||
if is_enterprise:
|
||||
return "WPA3-Enterprise" if has_sae else "WPA2-Enterprise"
|
||||
if has_sae and has_psk:
|
||||
return "WPA2/WPA3-Personal"
|
||||
if has_sae:
|
||||
return "WPA3-Personal"
|
||||
if has_rsn:
|
||||
return "WPA2-Personal"
|
||||
if has_wpa1:
|
||||
return "WPA-Personal"
|
||||
return "Open"
|
||||
|
||||
|
||||
def _security_from_flags(auth: str, enc: str = "") -> str:
|
||||
a, e = auth.upper(), enc.upper()
|
||||
if "WPA3" in a:
|
||||
@@ -390,13 +485,16 @@ def _wlanapi_enumerate_and_scan(wlan, handle) -> list[AccessPoint]:
|
||||
("dot11BssPhyType", wt.DWORD),
|
||||
("lRssi", ctypes.c_long), # ← raw hardware dBm
|
||||
("uLinkQuality", wt.ULONG),
|
||||
("bInRegDomain", wt.BOOL),
|
||||
("bInRegDomain", ctypes.c_ubyte), # BOOLEAN = 1 byte
|
||||
("usBeaconPeriod", wt.USHORT),
|
||||
("ullTimestamp", ctypes.c_ulonglong),
|
||||
("ullHostTimestamp", ctypes.c_ulonglong),
|
||||
("usCapabilityInformation", wt.USHORT),
|
||||
("ulChCenterFrequency", wt.ULONG), # kHz
|
||||
("wlanRateSet", ctypes.c_ubyte * 16),
|
||||
# WLAN_RATE_SET = { ULONG uRateSetLength; USHORT usRateSet[126]; } = 256 B.
|
||||
# Was wrongly c_ubyte*16, which shifted ulIeOffset/ulIeSize by 240 bytes
|
||||
# and made them read garbage (the cause of the earlier IE-read crash).
|
||||
("wlanRateSet", ctypes.c_ubyte * 256),
|
||||
("ulIeOffset", wt.ULONG),
|
||||
("ulIeSize", wt.ULONG),
|
||||
]
|
||||
@@ -406,6 +504,43 @@ def _wlanapi_enumerate_and_scan(wlan, handle) -> list[AccessPoint]:
|
||||
("dwNumberOfItems", wt.DWORD),
|
||||
("wlanBssEntries", WLAN_BSS_ENTRY * 512)]
|
||||
|
||||
# ── WLAN_AVAILABLE_NETWORK — carries auth/cipher per SSID ────────────────
|
||||
# dot11DefaultAuthAlgorithm values (DOT11_AUTH_ALGORITHM):
|
||||
# 1=Open 2=WEP 3=WPA-Ent 4=WPA-PSK 5=WPA-None
|
||||
# 6=WPA2-Ent(RSNA) 7=WPA2-PSK(RSNA)
|
||||
# 8=WPA3-Ent 9=WPA3-SAE(Personal) 10=OWE 11=WPA3-Ent-192
|
||||
class WLAN_AVAILABLE_NETWORK(ctypes.Structure):
|
||||
_fields_ = [
|
||||
("strProfileName", ctypes.c_wchar * 256),
|
||||
("dot11Ssid", DOT11_SSID),
|
||||
("dot11BssType", wt.DWORD),
|
||||
("uNumberOfBssids", wt.ULONG),
|
||||
("bNetworkConnectable", wt.BOOL),
|
||||
("wlanNotConnectableReason", wt.DWORD),
|
||||
("uNumberOfPhyTypes", wt.ULONG),
|
||||
("dot11PhyTypes", wt.DWORD * 8),
|
||||
("bMorePhyTypes", wt.BOOL),
|
||||
("wlanSignalQuality", wt.ULONG),
|
||||
("bSecurityEnabled", wt.BOOL),
|
||||
("dot11DefaultAuthAlgorithm", wt.DWORD),
|
||||
("dot11DefaultCipherAlgorithm", wt.DWORD),
|
||||
("dwFlags", wt.DWORD),
|
||||
("dwReserved", wt.DWORD),
|
||||
]
|
||||
|
||||
class WLAN_AVAILABLE_NETWORK_LIST(ctypes.Structure):
|
||||
_fields_ = [("dwNumberOfItems", wt.DWORD),
|
||||
("dwIndex", wt.DWORD),
|
||||
("Network", WLAN_AVAILABLE_NETWORK * 256)]
|
||||
|
||||
_AUTH_MAP = {
|
||||
1: "Open", 2: "WEP (insecure)",
|
||||
3: "WPA-Enterprise", 4: "WPA-Personal", 5: "WPA-Personal",
|
||||
6: "WPA2-Enterprise", 7: "WPA2-Personal",
|
||||
8: "WPA3-Enterprise", 9: "WPA3-Personal",
|
||||
10: "OWE (Enhanced Open)", 11: "WPA3-Enterprise",
|
||||
}
|
||||
|
||||
# ── Enumerate interfaces ──────────────────────────────────────────────────
|
||||
iface_list_ptr = ctypes.POINTER(WLAN_INTERFACE_INFO_LIST)()
|
||||
ret = wlan.WlanEnumInterfaces(handle, None, ctypes.byref(iface_list_ptr))
|
||||
@@ -427,7 +562,28 @@ def _wlanapi_enumerate_and_scan(wlan, handle) -> list[AccessPoint]:
|
||||
wlan.WlanScan(handle, ctypes.byref(iface_guid), None, None, None)
|
||||
time.sleep(2.0) # allow the radio time to complete the scan sweep
|
||||
|
||||
# Retrieve BSS list
|
||||
# Build SSID → security string from WlanGetAvailableNetworkList.
|
||||
# This API returns DOT11_AUTH_ALGORITHM directly — no raw IE parsing needed.
|
||||
ssid_security: dict[str, str] = {}
|
||||
net_list_ptr = ctypes.POINTER(WLAN_AVAILABLE_NETWORK_LIST)()
|
||||
if wlan.WlanGetAvailableNetworkList(
|
||||
handle, ctypes.byref(iface_guid), 0, None, ctypes.byref(net_list_ptr)
|
||||
) == 0:
|
||||
net_list = net_list_ptr.contents
|
||||
for k in range(net_list.dwNumberOfItems):
|
||||
net = net_list.Network[k]
|
||||
n_len = net.dot11Ssid.uSSIDLength
|
||||
try:
|
||||
n_ssid = bytes(net.dot11Ssid.ucSSID[:n_len]).decode(
|
||||
"utf-8", errors="replace").strip("\x00")
|
||||
except Exception:
|
||||
n_ssid = ""
|
||||
if n_ssid:
|
||||
ssid_security[n_ssid] = _AUTH_MAP.get(
|
||||
net.dot11DefaultAuthAlgorithm, "-")
|
||||
wlan.WlanFreeMemory(net_list_ptr)
|
||||
|
||||
# Retrieve per-BSSID list (raw RSSI, phy type, frequency)
|
||||
bss_list_ptr = ctypes.POINTER(WLAN_BSS_LIST)()
|
||||
ret = wlan.WlanGetNetworkBssList(
|
||||
handle, ctypes.byref(iface_guid),
|
||||
@@ -442,6 +598,15 @@ def _wlanapi_enumerate_and_scan(wlan, handle) -> list[AccessPoint]:
|
||||
|
||||
bss_list = bss_list_ptr.contents
|
||||
|
||||
# Snapshot the whole BSS list buffer once, bounded by dwTotalSize.
|
||||
# All IE access below slices this bytes object, so a bad offset yields
|
||||
# wrong data — never an out-of-bounds read / crash.
|
||||
list_base = ctypes.cast(bss_list_ptr, ctypes.c_void_p).value
|
||||
try:
|
||||
buf = ctypes.string_at(list_base, bss_list.dwTotalSize)
|
||||
except Exception:
|
||||
buf = b""
|
||||
|
||||
for j in range(bss_list.dwNumberOfItems):
|
||||
entry = bss_list.wlanBssEntries[j]
|
||||
|
||||
@@ -468,35 +633,49 @@ def _wlanapi_enumerate_and_scan(wlan, handle) -> list[AccessPoint]:
|
||||
# Map frequency to channel
|
||||
channel = _channel_from_freq(freq_mhz)
|
||||
|
||||
# dot11BssPhyType → mode hint
|
||||
# dot11BssPhyType → mode hint (fallback only — see IE override below).
|
||||
# https://docs.microsoft.com/en-us/windows/win32/nativewifi/dot11-phy-type
|
||||
phy_type = entry.dot11BssPhyType
|
||||
mode = {
|
||||
4: "a", # dot11_phy_type_ofdm (legacy 802.11a)
|
||||
6: "g", # dot11_phy_type_erp
|
||||
7: "a", # dot11_phy_type_ofdm (legacy 802.11a)
|
||||
8: "n (Wi-Fi 4)", # dot11_phy_type_ht
|
||||
9: "ac (Wi-Fi 5)", # dot11_phy_type_vht
|
||||
10: "ax (Wi-Fi 6)", # dot11_phy_type_he (covers Wi-Fi 6 and 6E)
|
||||
7: "n (Wi-Fi 4)", # dot11_phy_type_ht
|
||||
8: "ac (Wi-Fi 5)", # dot11_phy_type_vht
|
||||
9: "ad", # dot11_phy_type_dmg (802.11ad)
|
||||
10: "ax (Wi-Fi 6)", # dot11_phy_type_he
|
||||
11: "be (Wi-Fi 7)", # dot11_phy_type_eht (802.11be)
|
||||
}.get(phy_type, "-")
|
||||
# Wi-Fi 6E is 802.11ax (phy_type 10) operating in the 6 GHz band
|
||||
if mode == "ax (Wi-Fi 6)" and 5925 <= freq_mhz <= 7125:
|
||||
mode = "ax (Wi-Fi 6E)"
|
||||
|
||||
# Parse security from raw 802.11 IEs embedded after the BSS entry struct.
|
||||
# ulIeOffset is the byte offset from the start of the WLAN_BSS_ENTRY to
|
||||
# the first IE; ulIeSize is the total length of the IE block.
|
||||
security = "Open"
|
||||
ie_size = entry.ulIeSize
|
||||
if ie_size > 0:
|
||||
try:
|
||||
import ctypes as _ct
|
||||
raw = (_ct.c_ubyte * ie_size).from_address(
|
||||
_ct.addressof(entry) + entry.ulIeOffset
|
||||
)
|
||||
security = _parse_ie_security(bytes(raw))
|
||||
except Exception:
|
||||
pass
|
||||
# Slice this BSS entry's beacon IEs from the bounded snapshot.
|
||||
# A bad offset yields empty/wrong bytes — never an out-of-bounds read.
|
||||
ie_bytes = b""
|
||||
if buf:
|
||||
ie_off = (ctypes.addressof(entry) - list_base) + entry.ulIeOffset
|
||||
ie_end = ie_off + entry.ulIeSize
|
||||
if 0 <= ie_off <= ie_end <= len(buf):
|
||||
ie_bytes = buf[ie_off:ie_end]
|
||||
|
||||
# Authoritative mode from the beacon IEs. The scan list's phy type
|
||||
# caps at HE (10) even for 802.11be, so EHT (Wi-Fi 7) is ONLY
|
||||
# detectable from the EHT Capabilities element here.
|
||||
if ie_bytes:
|
||||
ie_mode = _mode_from_ies(ie_bytes, freq_mhz)
|
||||
if ie_mode:
|
||||
mode = ie_mode
|
||||
|
||||
# Security: prefer the SSID-keyed auth from WlanGetAvailableNetworkList.
|
||||
# Hidden networks aren't in that list (no SSID to key on), so fall back
|
||||
# to parsing the RSN/WPA IEs directly, then to the Privacy capability
|
||||
# bit (0x10) to at least distinguish "Secured" from "Open".
|
||||
security = ssid_security.get(ssid, "")
|
||||
if not security and ie_bytes:
|
||||
security = _security_from_ies(ie_bytes)
|
||||
if not security:
|
||||
privacy = bool(entry.usCapabilityInformation & 0x10)
|
||||
security = "Secured" if privacy else "Open"
|
||||
|
||||
aps.append(AccessPoint(
|
||||
ssid=ssid, bssid=bssid, signal_dbm=signal_dbm,
|
||||
|
||||
Reference in new issue
Block a user