Refactor get_aps_on_channel function to improve packet filtering and BSSID handling

This commit is contained in:
Yaro Kasear 2025-04-15 08:33:00 -05:00
parent 17c17b41c2
commit 0a14ecebf4

View file

@ -155,52 +155,41 @@ def get_aps_on_channel(capture, ap_channel):
print(f"[!] Could not parse channel number: {ap_channel}") print(f"[!] Could not parse channel number: {ap_channel}")
return 0 return 0
from_channel_freq = get_channel_from_freq(ap_channel)
if not from_channel_freq:
print(f"[!] Invalid channel: {ap_channel}")
return 0
aps = set() aps = set()
for packet in capture: for packet in capture:
try: try:
if not hasattr(packet, 'radiotap') or not hasattr(packet, 'wlan'): if 'radiotap' not in packet or 'wlan' not in packet:
continue continue
packet_freq = getattr(packet.radiotap.channel, 'freq', None) # Match channel frequency
if packet_freq is None: freq = int(getattr(packet.radiotap, 'channel_freq', -1))
print("[DEBUG] No channel frequency found.") if freq != from_channel_freq:
continue continue
packet_freq = int(packet_freq) # Check for beacon or probe response
packet_channel = get_channel_from_freq(packet_freq) ts_hex = getattr(packet.wlan, 'type_subtype', None)
if ts_hex is None:
if packet_channel != ap_channel:
print(f"[DEBUG] Skipped packet on channel {packet_channel}, looking for {ap_channel}")
continue continue
fc_raw = getattr(packet.wlan, 'fc', None) ts = int(ts_hex, 16)
if not fc_raw: if ts not in (5, 8): # Probe Response or Beacon
print("[DEBUG] No FC field found.")
continue
fc_int = int(fc_raw, 16)
frame_type = (fc_int >> 2) & 0b11
subtype = (fc_int >> 4) & 0b1111
print(f"[DEBUG] FC: {fc_raw}, frame_type: {frame_type}, subtype: {subtype}")
if frame_type != 0 or subtype not in (5, 8):
print("[DEBUG] Not a Beacon or Probe Response.")
continue continue
# Grab BSSID
bssid = getattr(packet.wlan, 'bssid', '').lower() bssid = getattr(packet.wlan, 'bssid', '').lower()
if bssid: if bssid and bssid != 'ff:ff:ff:ff:ff:ff':
print(f"[DEBUG] Adding BSSID: {bssid}")
aps.add(bssid) aps.add(bssid)
else:
print("[DEBUG] No BSSID found.")
except Exception as e: except Exception as e:
print(f"[DEBUG] Failed packet: {e}") print(f"[DEBUG] Packet parse error: {e}")
continue continue
print(f"[DEBUG] Final AP count: {len(aps)} | APs: {sorted(aps)}")
return len(aps) return len(aps)
def analyze_pcap(pcapng_path, start_ts, end_ts, ap_bssid, ap_channel): def analyze_pcap(pcapng_path, start_ts, end_ts, ap_bssid, ap_channel):