Search code examples
pythonubuntuscapy

Using Scapy to sniff beacon packets


I'm trying to use Scapy to capture beacon type packets from wi-fi access points and then extract their mac address and rssi. When i run the code nothing seems to happen. What am i doing wrong? Is the filter i'm using not the right one or is it something else?

from scapy.all import sniff
import os

interface = "wlp3s0"
interface_mon = "wlp3s0mon"

os.system(f"sudo -S airmon-ng check kill")
os.system(f"sudo airmon-ng start {interface} ")
os.system(f"sudo iwconfig")

# Capture Wi-Fi packets with a filter for beacon frames
packets = sniff(iface=interface_mon, filter="type mgt subtype beacon", count=10)
# Extract the MAC address and signal strength (RSSI) of each access point
access_points = []
for pkt in packets:
    mac = pkt.addr3
    rssi = -(256-ord(pkt.notdecoded[-2:-1]))
    access_points.append({'mac': mac, 'rssi': rssi})

print(access_points)

Solution

  • The problem was caused by my current adapter driver not supporting monitor mode. So the code was working right but the adapter wasn't capturing anything. Rolling back the driver to an earlier version fixed it.