You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Scapy捕获Wi-Fi Beacon数据包无响应的问题排查

解决Scapy捕获Wi-Fi Beacon包无响应的问题

核心错误点及修复方案

1. 监控模式接口创建异常

  • 问题:硬编码的wlp3s0mon接口名不一定匹配实际生成结果(部分驱动/系统会生成wlp3s0mon0这类变体);同时sudo -S需要交互输入密码,脚本未处理会导致命令卡住,后续抓包无法执行。
  • 修复:
    • 从airmon-ng start的输出中动态提取监控接口名,避免硬编码;
    • 直接以sudo权限运行整个脚本,去掉脚本内的sudo -S交互逻辑;
    • 用subprocess替代os.system,捕获命令输出确保接口创建成功:
      import subprocess
      interface = "wlp3s0"
      interface_mon = None
      
      subprocess.run(["sudo", "airmon-ng", "check", "kill"], check=True)
      result = subprocess.run(["sudo", "airmon-ng", "start", interface], capture_output=True, text=True)
      for line in result.stdout.splitlines():
          if "monitor mode enabled on" in line:
              interface_mon = line.split()[-1]
              break
      

2. BPF抓包过滤器格式错误

  • 问题:原过滤器"type mgt subtype beacon"缺少wlan前缀,Scapy的BPF过滤器需明确指定Wi-Fi层,否则无法正确匹配Beacon帧。
  • 修复:将过滤器改为"wlan type mgt subtype beacon"

3. RSSI提取方式不可靠

  • 问题:依赖pkt.notdecoded提取信号强度的方式受驱动Radiotap头格式影响,兼容性极差,容易返回错误值或空值。
  • 修复:使用Scapy内置的Radiotap层字段提取:
    if pkt.haslayer(RadioTap):
        rssi = pkt[RadioTap].dBm_AntSignal
    
    若该字段不存在,可通过pkt[RadioTap].show()查看驱动支持的信号强度字段(如dBm_AntNoise)。

4. 权限不足导致静默失败

  • 问题:Wi-Fi抓包必须以root权限执行,若未用sudo启动脚本,sniff会静默失败无输出。
  • 修复:始终以sudo python3 your_script.py启动脚本,避免在脚本内部嵌套sudo逻辑。

修复后的完整代码

from scapy.all import sniff, RadioTap, Dot11
import subprocess

interface = "wlp3s0"
interface_mon = None

# 终止干扰进程
subprocess.run(["sudo", "airmon-ng", "check", "kill"], check=True)
# 启动监控模式并获取接口名
result = subprocess.run(["sudo", "airmon-ng", "start", interface], capture_output=True, text=True)
for line in result.stdout.splitlines():
    if "monitor mode enabled on" in line:
        interface_mon = line.split()[-1]
        break

if not interface_mon:
    print("无法创建监控模式接口")
    exit(1)

print(f"使用监控接口:{interface_mon}")

# 捕获Beacon帧
packets = sniff(iface=interface_mon, filter="wlan type mgt subtype beacon", count=10)

access_points = []
for pkt in packets:
    if pkt.haslayer(Dot11):
        mac = pkt[Dot11].addr3
        if pkt.haslayer(RadioTap):
            rssi = pkt[RadioTap].dBm_AntSignal
            if rssi is not None:
                access_points.append({'mac': mac, 'rssi': rssi})

print(access_points)

内容的提问来源于stack exchange,提问作者Amir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 23:00:25