4. 解析Authentication帧

任务目标

本案例通过抓取真实WiFi认证阶段的空中数据包,解析Authentication帧的帧体结构,对比开放系统(Open System)与共享密钥(Shared Key)两种认证方式的交互差异。完成本案例后,你应能独立使用Wireshark或tshark从抓包文件中提取并解读Authentication帧的关键字段。

环境与依赖

  • 操作系统:Linux(推荐Ubuntu 22.04+)或Windows/macOS(需安装Wireshark)
  • 工具:tshark(Wireshark命令行版,版本≥3.4)
  • 抓包文件:包含Authentication帧的pcapng文件(本案例使用公开样例文件 auth-open.pcapng 和 auth-shared.pcapng,可从Wireshark wiki测试捕获页下载)

Authentication帧结构

Authentication帧属于管理帧子类型(Subtype=0xB),帧体包含以下关键字段:

  • Auth Algorithm Number(2字节):0=开放系统,1=共享密钥
  • Auth Transaction Sequence Number(2字节):认证交互的步骤序号
  • Status Code(2字节):0表示成功,非0表示失败原因
  • Challenge Text(仅共享密钥认证第2帧出现):包含AP发送的挑战文本

完整解析代码

以下Python脚本使用tshark作为后端,自动解析两种认证方式的帧交互流程:

```python #!/usr/bin/env python3 """解析Authentication帧:开放系统 vs 共享密钥""" import subprocess import sys def parse_auth_frames(pcap_file): """使用tshark提取Authentication帧关键字段""" cmd = [ 'tshark', '-r', pcap_file, '-Y', 'wlan.fc.type_subtype == 0x0b', '-T', 'fields', '-e', 'frame.number', '-e', 'wlan.fc.type_subtype', '-e', 'wlan.ta', '-e', 'wlan.ra', '-e', 'wlan.auth.algorithm', '-e', 'wlan.auth.seq', '-e', 'wlan.auth.status', '-e', 'wlan.auth.challenge', '-E', 'separator=|', '-E', 'header=y' ] try: result = subprocess.run(cmd, capture_output=True, text=True, check=True) return result.stdout except subprocess.CalledProcessError as e: print(f"tshark执行失败: {e.stderr}", file=sys.stderr) sys.exit(1) def analyze_auth_flow(pcap_file): """分析认证流程并输出交互摘要""" output = parse_auth_frames(pcap_file) lines = output.strip().split('\n') if len(lines) < 2: print("未找到Authentication帧") return print(f"文件: {pcap_file}") print(f"{'帧号':<6}{'方向':<20}{'算法':<12}{'序号':<6}{'状态':<8}挑战文本") print("-" * 70) for line in lines[1:]: # 跳过表头 parts = line.split('|') if len(parts) < 7: continue frame_num = parts[0] ta, ra = parts[2], parts[3] algorithm = "开放系统" if parts[4] == "0" else "共享密钥" seq = parts[5] status = parts[6] if parts[6] else "N/A" challenge = parts[7] if len(parts) > 7 and parts[7] else "-" # 判断方向:TA为AP地址(通常以特定前缀标识,此处简化处理) direction = f"{ta} → {ra}" print(f"{frame_num:<6}{direction:<20}{algorithm:<12}{seq:<6}{status:<8}{challenge}") if __name__ == "__main__": if len(sys.argv) != 2: print("用法: python3 parse_auth.py ") sys.exit(1) analyze_auth_flow(sys.argv[1]) ```

运行与验证

  1. 下载样例抓包文件(或使用自己抓取的包含认证过程的pcapng)
  2. 运行脚本解析开放系统认证:
    python3 parse_auth.py auth-open.pcapng
  3. 运行脚本解析共享密钥认证:
    python3 parse_auth.py auth-shared.pcapng

预期输出特征:

  • 开放系统认证:应看到2个Authentication帧(seq=1请求,seq=2响应),算法编号为0,状态码为0
  • 共享密钥认证:应看到4个Authentication帧(seq=1请求,seq=2含挑战文本,seq=3含加密挑战响应,seq=4最终结果),算法编号为1,第2帧的挑战文本字段非空

判定通过条件:脚本能正确输出两种认证的帧序号、方向、算法类型和状态码;共享密钥认证的第2帧能显示挑战文本内容(十六进制),且两种认证的帧数量与上述预期一致。

常见问题

  • 若tshark提示找不到字段,请确认Wireshark版本≥3.4,旧版本可能使用不同的字段名(如 wlan.auth.algorithm 在旧版为 wlan.auth.algo)
  • 抓包文件需包含完整的认证过程,若从关联阶段中途开始抓包可能缺少前序帧
  • 共享密钥认证的挑战文本在Wireshark中可能显示为十六进制字符串,这是正常现象
Authentication帧交互流程对比 开放系统认证(2帧) STA AP ① Auth(seq=1, algo=0) ② Auth(seq=2, status=0) ✓ 认证成功 共享密钥认证(4帧) STA AP ① Auth(seq=1, algo=1) ② Auth(seq=2, challenge) ③ Auth(seq=3, encrypted) ④ Auth(seq=4, status=0) ✓ 认证成功(需WEP密钥) algo=0: 开放系统 | algo=1: 共享密钥 | seq: 事务序号 | status=0: 成功