Enhance error handling and debugging in analyze_pcap for SSID and Cisco client count extraction

This commit is contained in:
Yaro Kasear 2025-04-16 11:21:41 -05:00
parent dd82940684
commit 53ec21227c

View file

@ -318,52 +318,59 @@ def analyze_pcap(pcapng_path, start_ts, end_ts, ap_bssid, ap_channel):
try: try:
if 'radiotap' not in packet or 'wlan' not in packet: if 'radiotap' not in packet or 'wlan' not in packet:
continue continue
radio = packet.radiotap radio = packet.radiotap
wlan = packet.wlan wlan = packet.wlan
if not hasattr(radio.channel, 'freq'): if not hasattr(radio.channel, 'freq'):
continue continue
packet_freq = int(radio.channel.freq) packet_freq = int(radio.channel.freq)
packet_channel = get_channel_from_freq(packet_freq) packet_channel = get_channel_from_freq(packet_freq)
subtype = int(getattr(wlan, 'type_subtype', 0), 16) subtype = int(getattr(wlan, 'type_subtype', 0), 16)
if subtype not in (5, 8): # Probe Response or Beacon if subtype not in (5, 8): # Probe Response or Beacon
continue continue
# Grab management layer once
try: try:
mgt = packet.get_multiple_layers('wlan.mgt')[0] mgt = packet.get_multiple_layers('wlan.mgt')[0]
tags = mgt._all_fields.get('wlan.tagged.all', {}).get('wlan.tag', []) tags = mgt._all_fields.get('wlan.tagged.all', {}).get('wlan.tag', [])
ssid = None except Exception as e:
print(f"[DEBUG] Error accessing wlan.mgt tags: {e}")
continue
for tag in tags: ssid = None
if tag.get('wlan.tag.number') == '0' and 'wlan.ssid' in tag: for tag in tags:
tag_number = tag.get('wlan.tag.number')
# SSID
if tag_number == '0' and 'wlan.ssid' in tag:
try:
raw_ssid = tag['wlan.ssid'] raw_ssid = tag['wlan.ssid']
ssid_bytes = bytes.fromhex(raw_ssid.replace(':', '')) ssid_bytes = bytes.fromhex(raw_ssid.replace(':', ''))
ssid = ssid_bytes.decode('utf-8', errors='replace') ssid = ssid_bytes.decode('utf-8', errors='replace')
elif tag.get('wlan.tag.number') == '133': except Exception as e:
try: print(f"[DEBUG] Error decoding SSID: {e}")
num_clients = int(tag.get('wlan.cisco.ccx1.clients')) ssid = None
cisco_reported_clients.append(num_clients)
except (TypeError, ValueError): # Cisco Client Count
pass # Garbage? We ignore it. if tag_number == '133':
try:
if not ssid: num_clients = int(tag.get('wlan.cisco.ccx1.clients'))
print("[DEBUG] No SSID found in tags.") cisco_reported_clients.append(num_clients)
continue except (TypeError, ValueError):
except Exception as e: pass
print(f"[DEBUG] Error parsing SSID or Cisco client count: {e}")
if not ssid:
print("[DEBUG] No SSID found in tags.")
continue continue
bssid = getattr(wlan, 'bssid', '').lower() bssid = getattr(wlan, 'bssid', '').lower()
if not bssid or bssid == 'ff:ff:ff:ff:ff:ff':
# For debugging purposes, print the SSID and BSSID
print(f"SSID: {ssid}, BSSID: {bssid}")
if not ssid or not bssid or bssid == 'ff:ff:ff:ff:ff:ff':
continue continue
print(f"SSID: {ssid}, BSSID: {bssid}")
bssid_to_ssid[bssid] = ssid bssid_to_ssid[bssid] = ssid
ssid_to_bssids[ssid].add(bssid) ssid_to_bssids[ssid].add(bssid)
@ -371,7 +378,9 @@ def analyze_pcap(pcapng_path, start_ts, end_ts, ap_bssid, ap_channel):
signal = getattr(radio, 'dbm_antsignal', None) signal = getattr(radio, 'dbm_antsignal', None)
if signal: if signal:
ssid_signals[ssid].append(int(signal)) ssid_signals[ssid].append(int(signal))
except:
except Exception as e:
print(f"[DEBUG] General packet parse error: {e}")
continue continue
print(f"[DEBUG] SSID to BSSIDs: {ssid_to_bssids}") print(f"[DEBUG] SSID to BSSIDs: {ssid_to_bssids}")