当前位置:网站首页>【MQTT从入门到提高系列 | 09】WireShark抓包分析MQTT报文
【MQTT从入门到提高系列 | 09】WireShark抓包分析MQTT报文
2022-07-29 01:38:00 【机器未来】
这是机器未来的第37篇文章
原文首发地址:https://blog.csdn.net/RobotFutures/article/details/126032312

文章目录
本文记录如何使用WireShark来抓取MQTT报文,分析报文并于MQTT3.1.1规约印证。
1. 环境配置
详见MQTT从入门到提高(一):从0到1快速搭建MQTT测试环境
2. 启动Wireshark监听
启动后,点击【捕获选项】按钮,如图1,然后点击回环网卡【BSD loopback】,如图2,然后点击【开始】按钮,如图3
注意:本测试Broker与客户端均运行于本地,因此捕获网卡为本地回环网卡。配置可根据实际情况使用响应网卡。

- 启动后,输入过滤规则【tcp.port == 1883】,注意大小写敏感,1883是MQTT Broker无加密时的监听端口

3. 启动Mqtt broker
启动【terminal】终端命令行工具,输入
mosquitto.exe -v
输出如下:
PS C:\Users\25267> mosquitto.exe -v
1648199085: mosquitto version 2.0.14 starting
1648199085: Using default config.
1648199085: Starting in local only mode. Connections will only be possible from clients running on this machine.
1648199085: Create a configuration file which defines a listener to allow remote access.
1648199085: For more details see https://mosquitto.org/documentation/authentication-methods/
1648199085: Opening ipv4 listen socket on port 1883.
1648199085: Opening ipv6 listen socket on port 1883.
1648199085: mosquitto version 2.0.14 running
4. 订阅主题sensors/temperature
4.1 启动订阅
- 新开一个终端输入
mosquitto_sub.exe -t sensors/temperature -q 1
- Broker的输出如下:
1648199291: New connection from 127.0.0.1:12245 on port 1883.
1648199291: New client connected from 127.0.0.1:12245 as auto-1744742D-43CA-CFDB-7C5D-19BE38B45F05 (p2, c1, k60).
1648199291: No will message specified.
1648199291: Sending CONNACK to auto-1744742D-43CA-CFDB-7C5D-19BE38B45F05 (0, 0)
1648199291: Received SUBSCRIBE from auto-1744742D-43CA-CFDB-7C5D-19BE38B45F05
1648199291: sensors/temperature (QoS 1)
1648199291: auto-1744742D-43CA-CFDB-7C5D-19BE38B45F05 1 sensors/temperature
1648199291: Sending SUBACK to auto-1744742D-43CA-CFDB-7C5D-19BE38B45F05
可以看到,有三个MQTT相关的命令:CONNACK、SUBSCRIBE、SUBACK
- Wireshark监听记录如图:

可以看到wireshark已经监听到4个MQTT报文:Connect、Connect Ack、Subscribe Request、Subscribe Ack。接下来,依次分析4个报文。
Wireshark工具自带MQTT解析功能,下方是MQTT原始报文,上方是MQTT解析后的数据描述。
4.2 CONNECT报文解析
- Connect控制报文的协议规约为

- 手撕Connect报文
10 0C 00 04 4D 51 54 54 04 02 00 3C 00 00
| 序号 | 数据 | 描述 | 备注 |
|---|---|---|---|
| 1 | 10 | Fixed Header, 高4位为报文消息标识符,0x01代表Connect | |
| 2 | 0C | Remaining Length, 剩余长度,不包含长度域本身,包含可变消息头和负载的长度, 0C到0x00的长度正好是12 | |
| 3 | 00 04 | Protocol Name Length, 协议名称的UTF-8字符串的长度域,MSB在前,LSB在后,其值为4Bytes | |
| 4 | 4D 51 54 54 | Protocol Name Value,为ASCII编码,对应的值为MQTT,为协议名称UTF-8字符串的值域 | |
| 5 | 04 | Protocol Level, 协议级别,值为4,对应MQTT3.1.1的协议版本 | |
| 6 | 02 | ConnectFlags,连接标志,bit1为1,即CleanSession为1,表示会话为清洁会话, 之前服务器存储的历史会话都不会继承,会创建新的会话。 | |
| 7 | 00 3C | KeepAlive, 保活时间,值为60,表示客户端和服务器之间至少60s通讯一次,如果空 闲则发送PINGREQ报文 | |
| 8 | 00 00 | Client Identifier,客户端标识符,必须存在 |
4.3 CONNACK报文解析
- CONNACK报文规约

- CONNACK Wireshark解析

4.4 SUBSCRIBE报文解析
- SUBSCRIBE报文规约

- SUBSCRIBE Wireshark解析

4.5 SUBACK
- SUBACK协议规约

- SUBACK Wireshark解包分析

注意:返回码为01,表示成功,其对应的SUB QoS=1.详见【MQTT从入门到提高系列 | 03】一文掌握MQTT3.1.1协议框架 P4.9章节。
5. 启动Broker TLS加密模式
- 启动Broker,以加密TLS模式, 详情请参考MQTT从入门到提高(二):MQTT TLS加密传输
mosquitto.exe -c .\mosquitto.conf -v
Broker输出如下:
PS D:\110_Work_ProductsResearch\111-Router\mosquitto> mosquitto.exe -c .\mosquitto.conf -v
1648202386: mosquitto version 2.0.14 starting
1648202386: Config loaded from .\mosquitto.conf.
1648202386: Opening ipv6 listen socket on port 8883.
1648202386: Opening ipv4 listen socket on port 8883.
1648202386: mosquitto version 2.0.14 running
- 订阅主题hello
mosquitto_sub.exe --cafile D:\110_Work_ProductsResearch\111-Router\mosquitto\cert\ca.crt -t hello -h 127.0.0.1 -u demo -P 123456 -v
Broker输出如下:
1648202564: New connection from 127.0.0.1:24723 on port 8883.
1648202564: New client connected from 127.0.0.1:24723 as auto-EEA03522-3A30-0904-F73B-A3854D233BC3 (p2, c1, k60, u'zsm').
1648202564: No will message specified.
1648202564: Sending CONNACK to auto-EEA03522-3A30-0904-F73B-A3854D233BC3 (0, 0)
1648202564: Received SUBSCRIBE from auto-EEA03522-3A30-0904-F73B-A3854D233BC3
1648202564: hello (QoS 0)
1648202564: auto-EEA03522-3A30-0904-F73B-A3854D233BC3 0 hello
1648202564: Sending SUBACK to auto-EEA03522-3A30-0904-F73B-A3854D233BC3
- Wireshark监听
输入过滤规则【tcp.port == 8883】,TLS加密访问的服务器监听端口为8883,可以看到消息类型已经变为TLSv1.3
MQTT TLS客户端与服务端的交互流程将来再研究。
《MQTT快速入门系列》快速导航:
- 【MQTT从入门到提高系列 | 01】从0到1快速搭建MQTT测试环境
- 【MQTT从入门到提高系列 | 02】MQTT3.1.1TLS加密传输
- 【MQTT从入门到提高系列 | 03】一文掌握MQTT3.1.1协议框架
- 【MQTT从入门到提高系列 | 04】MQTT应用协议之CONNECT
- 【MQTT从入门到提高系列 | 05】MQTT3.1.1之PUBLISH发布工作流
- 【MQTT从入门到提高系列 | 06】MQTT3.1.1之SUBSCRIBE订阅工作流
- 【MQTT从入门到提高系列 | 07】MQTT3.1.1之链路保活及断开
- 【MQTT从入门到提高系列 | 08】MQTT3.1.1主题Topic详解
写在末尾:
- 博客简介:专注AIoT领域,追逐未来时代的脉搏,记录路途中的技术成长!
- 专栏简介:从0到1掌握MQTT分布式协议。
- 面向人群:零基础编程爱好者
- 专栏计划:接下来会逐步发布跨入人工智能的系列博文,敬请期待
- Python零基础快速入门系列
- 快速入门Python数据科学系列
- 人工智能开发环境搭建系列
- 机器学习系列
- 物体检测快速入门系列
- 自动驾驶物体检测系列
- …

边栏推荐
- Promise solves asynchrony
- Jetpack -- navigation realizes page Jump
- Click the button to slide to the specified position
- MotionLayout--在可视化编辑器中实现动画
- MySQL之数据查询(多表查询)
- Custom MVC principle and framework implementation
- 年中总结 | 与自己对话,活在当下,每走一步都算数
- Summarize in the middle of the year | talk to yourself, live in the present, and count every step
- Excel 用countif 统计
- LM13丨形态量化-动量周期分析
猜你喜欢

TI C6000 TMS320C6678 DSP+ Zynq-7045的PS + PL异构多核案例开发手册(2)

弹性布局 单选

Monadic linear function perceptron: Rosenblatt perceptron

IDEA 连接 数据库
![[云原生]微服务架构是什么](/img/84/a0ec68646083f3539aa39ad9d98749.png)
[云原生]微服务架构是什么

Understand the clock tree in STM32 in simple terms

Blind separation of speech signals based on ICA and DL

Navigation -- realize data transmission and data sharing between fragments

物联网开发--MQTT消息服务器EMQX

webview攻击
随机推荐
QT source code analysis -- QObject (4)
Understand the clock tree in STM32 in simple terms
Probability Density Reweight
leetcode 242. Valid Anagram(有效的字母异位词)
Detailed explanation of IVX low code platform series -- Overview (II)
Mathematical modeling -- the laying of water pipes
(CVPR-2019)选择性的内核网络
“蔚来杯“2022牛客暑期多校训练营3,签到题CAJHF
Resolve the conflict with vetur when using eslint, resulting in double quotation marks and comma at the end of saving
LeetCode 练习——剑指 Offer 45. 把数组排成最小的数
MySQL stores JSON format data
Custom MVC principle and framework implementation
Try to understand the essence of low code platform design from another angle
The number of consecutive subarrays whose leetcode/ product is less than k
Leetcode exercise - Sword finger offer 45. arrange the array into the smallest number
第十五天(VLAN相关知识)
(arxiv-2018) 重新审视基于视频的 Person ReID 的时间建模
Probability Density Reweight
【ONE·Data || 数组堆简单实现及其延伸】
Internet of things development -- mqtt message server emqx