2. Zigbee终端设备入网流程观察
任务目标
使用抓包工具捕获Zigbee终端设备(End Device)加入网络(Association)的完整过程,识别并分析关键帧:Beacon Request、Beacon、Association Request、Association Response,以及后续的Transport Key和Device Announce。
环境与依赖
- 硬件:Zigbee协调器(如CC2531 USB Dongle)+ 1个终端设备(如Xiaomi温湿度传感器);
- 软件:Ubuntu 20.04+ / Windows 10+,安装
Wireshark 4.x; - 固件:CC2531刷写
ZNP-Host-firmware(Z-Stack 3.0.2),确保协调器允许加入(Permit Join); - 驱动:CC2531需识别为串口设备(Linux下为
/dev/ttyUSB0)。
抓包配置
- 将CC2531插入电脑,确认串口枚举成功;
- 打开Wireshark,选择接口
Zigbee over Serial(若未显示,需安装zigbee解析器插件); - 在Capture Options中设置
Channel: 11(默认),勾选Enable Zigbee decoding; - 开始抓包前,先通过串口工具(如
minicom)向协调器发送0x01 0x08 0x00(ZDO Permit Join Request,允许入网60秒)。
关键完整代码(Python串口控制协调器)
import serial
import time
import struct
# 配置串口(根据实际设备修改)
ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=1)
def send_znp(cmd0, cmd1, payload):
"""构造并发送ZNP帧(无FCS校验,适用于Z-Stack 3.0.2)"""
length = len(payload)
frame = bytes([0xFE, length, cmd0, cmd1]) + payload
# 计算FCS(异或和)
fcs = 0
for b in frame[1:]:
fcs ^= b
frame += bytes([fcs])
ser.write(frame)
time.sleep(0.1)
# 1. 发送ZDO Permit Join Request (0x25 0x04)
# 参数: DstAddr=0x0000 (协调器), PermitDuration=60, TC_Significance=1
payload = struct.pack('<HB', 0x0000, 60) + bytes([1])
send_znp(0x25, 0x04, payload)
print("已发送允许入网命令,持续60秒")
# 2. 等待并读取协调器返回的确认帧(可选,用于验证命令是否被接受)
response = ser.read(10)
if response:
print(f"协调器返回: {response.hex()}")
ser.close()
入网流程观察与帧分析
执行上述Python脚本后,立即给终端设备上电或触发其重新入网(如长按重置)。在Wireshark中按 zbee_nwk 过滤,观察以下帧序列:
| 顺序 | 帧类型 | 关键字段 | 作用 |
|---|---|---|---|
| 1 | Beacon Request | Protocol ID=0x00, Stack Profile=0x01 | 终端设备广播扫描附近协调器 |
| 2 | Beacon | PAN ID, Permit Joining=TRUE | 协调器响应,表明允许入网 |
| 3 | Association Request | Device Type=End Device, Power Source=0x01 | 终端设备请求加入PAN |
| 4 | Association Response | Status=Successful, Short Address=0x1234 | 协调器分配16位短地址 |
| 5 | Transport Key | Key Type=Network Key, Key=0x... | 协调器发送网络密钥(加密) |
| 6 | Device Announce | New Address=0x1234, IEEE Address=0x... | 终端设备广播入网成功 |
验证方法与判定标准
- 通过条件:在Wireshark中完整捕获上述6类帧,且Association Response中的Status字段为
Successful(0x00); - 关键检查点:Beacon帧中
Permit Joining必须为TRUE,否则终端设备不会发起关联请求; - 数据一致性:Device Announce中的短地址应与Association Response中分配的短地址一致;
- 失败排查:若未捕获到Beacon Request,检查终端设备是否处于入网状态(部分设备需长按按键3秒);若Association Response返回Failure(0x01),说明PAN容量已满或协调器不允许入网。
常见问题
- Wireshark无法解析Zigbee帧:确认已安装
zigbee插件(Wireshark 4.x自带),并在接口设置中启用Zigbee协议解码器; - 串口命令无响应:检查CC2531固件是否为ZNP模式,部分设备需先发送
0xEF 0x01进入ZNP模式; - 抓包不到任何帧:确认终端设备与协调器在同一信道(默认11),且协调器已上电并建立PAN。