在调试 Matter 设备配网、控制链路或重传问题时,光看日志往往不够直观。matter-dissector 是 CSA 官方维护的 Wireshark 解析插件(project-chip/matter-dissector),用于解析通过 TCP 或 UDP(IPv6/IPv4) 传输的 Matter 报文。
1. 工作原理:为什么能解开加密包?
Matter 的 PASE 和 CASE 会话每次建立都会通过密钥交换动态协商临时会话密钥,外网和网卡侧无法直接拿到这些密钥。为了让 Wireshark 在本地研发环境中能够解密报文,matter-dissector 配合 Matter SDK(connectedhomeip)采用了一套测试密钥模式:

- 在研发固件与测试端(如
chip-tool)中开启CHIP_CONFIG_SECURITY_TEST_MODE。 - SDK 内部会跳过正常动态密钥派生,强制使用固定的共享密钥字符串
"Test secret for key derivation."生成两把对称密钥:- I2R Key(Initiator-to-Responder):发起方加密请求、接收方解密。
- R2I Key(Responder-to-Initiator):接收方加密响应、发起方解密。
- 在 Wireshark 中预先导入这两把 128 位密钥后,插件就能解开 AES-128-CCM 密文并校验 MIC,进而把里面的 Protocol Header、Interaction Model(Read/Write/Invoke/Subscribe)和 TLV 字段完整展开。
CHIP_CONFIG_SECURITY_TEST_MODE会彻底绕过 Matter 的动态会话密钥保护,仅限研发调试使用,严禁合入量产固件。
2. 编译安装 Wireshark 与插件
matter-dissector 依赖 Wireshark 3.6 及以上版本,且编译插件时需要链接对应版本的 Wireshark 源码头文件。在 Linux(Ubuntu/Debian)下通常需要从源码编译一遍 Wireshark。
2.1 编译安装 Wireshark(以 3.6 分支为例)
如果系统 /etc/apt/sources.list 启用了 deb-src,可以直接用 apt-get build-dep wireshark 拉取构建依赖;否则手动安装依赖包:
bash
# 方式一:若已开启 deb-src 源
sudo apt-get build-dep wireshark
# 方式二:手动安装常见编译依赖
sudo apt-get install -y build-essential git cmake flex bison \
qttools5-dev qttools5-dev-tools libqt5svg5-dev qtmultimedia5-dev \
libpcap-dev libc-ares-dev libgcrypt20-dev libglib2.0-dev \
libpcre2-dev libnghttp2-dev libqt5core5a
拉取 Wireshark 源码并编译安装(Make 与 Ninja 二选一,推荐 Ninja 速度更快):
bash
git clone https://gitlab.com/wireshark/wireshark.git
cd wireshark
git checkout release-3.6
mkdir build && cd build
# 方式 A:使用 Ninja(推荐)
cmake -GNinja ..
ninja
sudo ninja install
sudo ldconfig
# 方式 B:使用 Make
# cmake ..
# make -j$(nproc)
# sudo make install
# sudo ldconfig
2.2 编译并安装 matter-dissector
确保 matter-dissector 与 wireshark 源码目录对应(WIRESHARK_SRC_DIR 默认指向 ../wireshark):
bash
git clone git@github.com:project-chip/matter-dissector.git
cd matter-dissector
WIRESHARK_SRC_DIR=/path/to/wireshark make
# 自动将编译出的插件安装到当前用户的 Wireshark 插件目录
make install
2.3 调试插件本身(可选)
如果需要修改或排查 matter-dissector 自身的解析逻辑,可以用 gdb 挂载断点:
bash
make debug
(gdb) b DissectMatter
Function "DissectMatter" not defined.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (DissectMatter) pending.
(gdb) run
3. 配置 SDK 与导入测试密钥
3.1 在 Matter SDK 中启用测试密钥
在通信双方(例如设备端固件与 chip-tool)的编译配置中开启测试模式:
cpp
#define CHIP_CONFIG_SECURITY_TEST_MODE 1
在 connectedhomeip 的 CHIPConfig.h 中启用该宏会触发编译期 #warning 提示,默认开启 -Werror 的 GN/Ninja 构建可能会报错中断。如果遇到编译失败,构建时需加上 treat_warnings_as_errors=false:
bash
gn gen out/debug --args='treat_warnings_as_errors=false'
开启后,CryptoContext::InitFromSecret 会统一进入测试模式(InitTestMode),默认使用以下参数派生会话密钥:
Secret = "Test secret for key derivation."
Size = 32 # 注意:31 个可见字符 + 末尾 '\0' 共 32 字节
Salt = null
Initiator-to-Responder (I2R) Test Key: 5EDED244E5532B3CDC23409DBAD052D2
Responder-to-Initiator (R2I) Test Key: A9E011B1737C6D4B70E4C0A2FE660476
3.2 在 Wireshark 中导入密钥
必须同时添加 I2R 和 R2I 两条密钥,否则只能解开单向报文:
- 打开 Wireshark,进入
Edit -> Preferences(macOS 为Wireshark -> Settings)。 - 在左侧面板展开
Protocols -> Matter。 - 点击右侧
Matter Encryption Keys [Edit...]。 - 点击
[+],依次填入上面的两组 32 位十六进制字符串(128 位密钥),回车确认并保存。
4. 抓包过滤与常用排查技巧
4.1 过滤 Matter 与 mDNS 发现流量
-
仅看 Matter 业务报文:在显示过滤框中输入:
matter -
同时查看 Matter 与 mDNS 服务发现(
_matterc._udp/_matter._tcp):matter || dns.qry.name contains matter || dns.resp.name contains matter注:早期的
dns.ptr.domain_name==_matterc._udp.local || dns.ptr.domain_name==_matter._tcp.local写法在较新 Wireshark 中已失效,请使用上面的contains matter表达式。 -
非 5540 端口的报文 :
matter解析器默认只监听 5540 端口。如果某些测试工具或设备使用了自定义端口,Wireshark 不会自动识别。此时在报文列表右键目标 UDP/TCP 包 -> 选择Decode As...-> 将当前协议指定为Matter即可。
4.2 追踪单次 Exchange 交互
在排查 MRP 重传、ACK 丢失或超时问题时,把同一次交换的所有报文单独拎出来看会清晰很多:
- 在报文列表中选中一条属于目标交互的 Matter 报文。
- 点击顶部菜单
Analyze -> Conversation Filter -> Matter Exchange,即可过滤出该次 Exchange 的全部往返报文。
4.3 追踪单条 IM 订阅(Subscription)
- 选中属于目标订阅的任意一条报文(例如
SubscribeRequest、SubscribeResponse、ReportData,或相关的StatusReport/StandaloneAck)。 - 点击顶部菜单
Analyze -> Conversation Filter -> Matter IM Subscription,即可只保留该订阅通道的所有周期上报与心跳报文。
4.4 从串口/文本十六进制日志导入单条报文
如果手头只有设备串口打印的 IP 报文 Hex Dump(没有 .pcap 文件),也可以直接导入 Wireshark 解析:
-
新建文本文件,按以下格式写入十六进制数据(每行以十六进制字节偏移开头,冒号和末尾 ASCII 注释均可选;多条报文之间用空行分隔):
0000: 60 00 00 00 00 1e 11 fd fd f8 a1 1b c2 bf 00 06 `............... 0010: 1a b4 30 00 01 40 95 18 fd f8 a1 1b c2 bf 00 05 ..0..@.......... 0020: 1a b4 30 02 00 00 00 13 2b 57 2b 57 00 1e 9f a7 ..0.....+W+W.... 0030: 00 20 76 78 18 cd 17 10 e7 ae 0d 00 00 00 17 6b . vx...........k 0040: ca 42 9d 00 00 00 -
在 Wireshark 中依次选择
File -> Import from Hex Dump...。 -
选中该文本文件,将
Encapsulation Type设置为Raw IP后点击导入即可。
5. 常见问题:无网卡抓包权限
在 Linux 下启动自编译的 Wireshark 实时抓包时,可能会遇到错误:
You don't have permission to capture on that device
这是因为普通用户没有权限调用 dumpcap 访问原始网络接口。如果是从源码编译安装 (dumpcap 位于 /usr/local/bin/dumpcap),按以下步骤授予 wireshark 用户组权限和能力:
bash
# 1. 创建 wireshark 用户组并将当前用户加入
sudo groupadd wireshark
sudo usermod -a -G wireshark $USER
# 2. 将 /usr/local/bin/dumpcap 授权给 wireshark 组
sudo chgrp wireshark /usr/local/bin/dumpcap
sudo chmod 750 /usr/local/bin/dumpcap
sudo setcap 'CAP_NET_RAW+eip CAP_NET_ADMIN+eip' /usr/local/bin/dumpcap
# 3. 刷新当前终端的用户组生效(或重新登录桌面会话)
newgrp wireshark
提示 :如果是通过
apt安装的系统 Wireshark(dumpcap位于/usr/bin/dumpcap),只需执行sudo dpkg-reconfigure wireshark-common并在弹窗中选择Yes,再把当前用户加入wireshark组即可,无需手动修改/usr/local/bin/dumpcap。