From 6c8f5424488e10eda32e78ba0c212740e79f4f14 Mon Sep 17 00:00:00 2001 From: Yaro Kasear Date: Tue, 29 Apr 2025 10:56:59 -0500 Subject: [PATCH] Remove debugging print statements from IndexedCapture to clean up code --- enrichment/indexed_capture.py | 81 ++++++++++++++++++++++------------- 1 file changed, 52 insertions(+), 29 deletions(-) diff --git a/enrichment/indexed_capture.py b/enrichment/indexed_capture.py index f178227..32f25b7 100644 --- a/enrichment/indexed_capture.py +++ b/enrichment/indexed_capture.py @@ -72,27 +72,6 @@ class IndexedCapture: capture.close() - # Debugging indexing - # 1. Inspect self.channel_to_aps and self.channel_to_clients - # Are they empty? - print(f"Indexed {len(self.time_index)} packets.") - # Are they missing just specific channels? - print(f"Channels with APs: {len(self.channel_to_aps)}") - # Are they populated, but wrong? - print(f"Channels with clients: {len(self.channel_to_clients)}") - # How many channels were seen? - print(f"Total channels: {len(set(self.channel_to_aps.keys()).union(set(self.channel_to_clients.keys())))}") - # How many APS were put into channel_to_aps? - print(f"Total APs: {sum(len(aps) for aps in self.channel_to_aps.values())}") - # How many clients were put into channel_to_clients? - print(f"Total clients: {sum(len(clients) for clients in self.channel_to_clients.values())}") - # Print out a few entries from channel_to_clients. - for channel, clients in list(self.channel_to_clients.items())[:5]: - print(f"Channel {channel}: {len(clients)} clients") - # Print out a few entries from channel_to_aps. - for channel, aps in list(self.channel_to_aps.items())[:5]: - print(f"Channel {channel}: {len(aps)} APs") - def _process_management_frame(self, packet, wlan, radio, channel): try: mgt = packet.get_multiple_layers('wlan.mgt')[0] @@ -159,22 +138,65 @@ class IndexedCapture: def query_metrics(self, start_ts, end_ts, ap_bssid, ap_channel): packets = self.get_packets_in_time_range(start_ts, end_ts) - - # Use indexed data instead of recalculating + print(f"[DEBUG] Packets in window: {len(packets)} between {start_ts} and {end_ts}") + + # Build local windowed structures + window_clients = defaultdict(set) + window_aps = defaultdict(set) + window_signals = defaultdict(list) + + for packet in packets: + try: + if 'radiotap' not in packet or 'wlan' not in packet: + continue + + wlan = packet.wlan + radio = packet.radiotap + + sa = getattr(wlan, 'sa', '').lower() + da = getattr(wlan, 'da', '').lower() + bssid = getattr(wlan, 'bssid', '').lower() + + if hasattr(radio, 'channel') and hasattr(radio.channel, 'freq'): + freq = int(radio.channel.freq) + channel = get_channel_from_freq(freq) + else: + continue + + # Track APs (beacons / probe responses only!) + subtype = int(getattr(wlan, 'type_subtype', '0'), 16) + if subtype in (5, 8) and bssid: + window_aps[channel].add(bssid) + + # Track clients + for mac in (sa, da): + if mac and mac != 'ff:ff:ff:ff:ff:ff': + window_clients[channel].add(mac) + + # Track signals + signal = getattr(radio, 'dbm_antsignal', None) + if signal: + window_signals[channel].append(int(signal)) + + except Exception: + continue + clients_on_ap = self._count_clients_on_ap(packets, ap_bssid) - clients_on_channel = len(self.channel_to_clients.get(ap_channel, [])) - aps_on_channel = len(self.channel_to_aps.get(ap_channel, [])) - - avg_ap_signal, max_ap_signal = self._calc_signal_stats(ap_channel) + clients_on_channel = len(window_clients.get(ap_channel, [])) + aps_on_channel = len(window_aps.get(ap_channel, [])) + + avg_ap_signal = mean(window_signals[ap_channel]) if window_signals.get(ap_channel) else 0 + max_ap_signal = max(window_signals[ap_channel]) if window_signals.get(ap_channel) else 0 + unlinked_devices = self._count_unlinked_devices(packets, ap_channel) - + our_ssid = self.bssid_to_ssid.get(ap_bssid) num_bssids = len(self.ssid_to_bssids.get(our_ssid, [])) if our_ssid else 0 avg_ssid_signal = mean(self.ssid_signals.get(our_ssid, [])) if our_ssid else 0 max_ssid_signal = max(self.ssid_signals.get(our_ssid, [])) if our_ssid else 0 num_channels_ssid = len(self.ssid_to_bssids.get(our_ssid, [])) if our_ssid else 0 packet_count = len(packets) - + return ( clients_on_ap, clients_on_channel, aps_on_channel, avg_ap_signal, max_ap_signal, unlinked_devices, @@ -183,6 +205,7 @@ class IndexedCapture: num_channels_ssid, packet_count ) + def _count_clients_on_ap(self, packets, ap_bssid): clients = defaultdict(int) ap_bssid = ap_bssid.lower()