Fix issue where wlanapi never shows security type

This commit is contained in:
iamdoubz committed 2026-06-10 16:36:03 -05:00
1 parent d8d05c2318
commit 44b9e1be09
1 file changed
+85 -1
+85 -1
View File
@@ -225,6 +225,75 @@ 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:
@@ -414,10 +483,25 @@ def _wlanapi_enumerate_and_scan(wlan, handle) -> list[AccessPoint]:
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
aps.append(AccessPoint(
ssid=ssid, bssid=bssid, signal_dbm=signal_dbm,
channel=channel, freq_mhz=freq_mhz,
mode=mode,
mode=mode, security=security,
))
wlan.WlanFreeMemory(bss_list_ptr)