Wireshark 流量分析
Wireshark 流量分析
本节介绍如何使用 Wireshark 分析 MQTT 流量,对比加密前后数据传输的差异。Wireshark 分析是验证 TLS 加密效果最直观的方法。学习完成后,您将能够:
- 使用 Wireshark 抓取 MQTT 网络包
- 识别 MQTT 协议数据包
- 对比加密和未加密的数据包差异
- 演示加密的实际效果
在开始本节之前,请确保:
- Wireshark 已安装
- MQTT Broker 支持 1883 和 8883 双端口
- 有 ESP32 或 MQTT 客户端可以发送消息
Wireshark Setup
Section titled “Wireshark Setup”安装 Wireshark
Section titled “安装 Wireshark”# Ubuntusudo apt install wireshark
# macOSbrew install --cask wireshark
# 启动 WiresharkwiresharkMQTT 协议配置
Section titled “MQTT 协议配置”在 Wireshark 中启用 MQTT 解析:
1. 打开 Wireshark2. 菜单: Analyze → Enabled Protocols3. 搜索 "MQTT"4. 勾选 MQTT 协议5. OK# 仅捕获 MQTT 流量tcp.port == 1883 or tcp.port == 8883
# 或捕获特定主机的所有流量ip.addr == 192.168.1.100
# 组合过滤器(tcp.port == 1883 or tcp.port == 8883) and ip.addr == 192.168.1.100Capturing Unencrypted MQTT
Section titled “Capturing Unencrypted MQTT”抓取明文 MQTT
Section titled “抓取明文 MQTT”# 1. 启动 Wireshark 捕获# 2. 在另一个终端发送 MQTT 消息
# 发布明文消息(端口 1883)mosquitto_pub -h localhost -p 1883 \ -t "factory/temperature" \ -m "{\"temp\": 24.5, \"hum\": 65.2}" \ -u "testuser" -P "testpass" \ -dWireshark 分析明文包
Section titled “Wireshark 分析明文包”Wireshark 中看到的明文 MQTT 数据包:
Frame 1: 72 bytes on wire Ethernet II, Src: ... Dst: ... Internet Protocol Version 4, Src: 192.168.1.100, Dst: 192.168.1.50 Transmission Control Protocol, Src Port: 54321, Dst Port: 1883 MQ Telemetry Transport Protocol Type: CONNECT Protocol Level: 4 Client Id: mosqpub User Name: testuser ← 明文可见! Password: testpass ← 明文可见!Frame 2: 118 bytes on wire MQ Telemetry Transport Protocol Type: PUBLISH Topic: factory/temperature ← 明文可见! Payload: {"temp": 24.5, "hum": 65.2} ← 明文可见!明文 MQTT 泄露的信息:
| 泄露信息 | 敏感级别 | 攻击者能做什么 |
|---|---|---|
| 用户名/密码 | 🔴 极高 | 冒充设备连接 |
| Topic 名称 | 🟡 中 | 了解系统结构 |
| 传感器数据 | 🟡 中 | 了解生产数据 |
| 控制指令 | 🔴 高 | 伪造控制命令 |
| 客户端 ID | 🟢 低 | 了解设备标识 |
Capturing Encrypted MQTT (TLS)
Section titled “Capturing Encrypted MQTT (TLS)”抓取加密 MQTT
Section titled “抓取加密 MQTT”# 发布加密消息(端口 8883)mosquitto_pub -h localhost -p 8883 \ --cafile /opt/mosquitto/certs/fullchain.pem \ -t "factory/temperature" \ -m "{\"temp\": 24.5, \"hum\": 65.2}" \ -u "testuser" -P "testpass" \ -dWireshark 中看到的 TLS 加密包
Section titled “Wireshark 中看到的 TLS 加密包”Frame 1: 571 bytes on wire Ethernet II, Src: ... Dst: ... Internet Protocol Version 4, Src: 192.168.1.100, Dst: 192.168.1.50 Transmission Control Protocol, Src Port: 54322, Dst Port: 8883 Transport Layer Security TLSv1.3 Record Layer: Handshake Protocol: Client Hello Content Type: Handshake (22) Version: TLS 1.0 (0x0301) Length: 512 Handshake Protocol: Client Hello Version: TLS 1.2 (0x0303) Random: ... Session ID: ... Cipher Suites: ... ...
Frame 2: (后续帧 - 所有数据加密) Transport Layer Security TLSv1.3 Record Layer: Application Data Protocol: Application Data Content Type: Application Data (23) Version: TLS 1.3 (0x0304) Length: 64 Encrypted Data: ...[不可读]...加密 MQTT 隐藏的信息:
| 信息 | 明文 MQTT | TLS 加密 |
|---|---|---|
| 用户名 | testuser | ❌ 不可见 |
| 密码 | testpass | ❌ 不可见 |
| Topic | factory/temperature | ❌ 不可见 |
| Payload | {"temp":24.5} | ❌ 不可见 |
| 可见的信息 | 以上全部 | IP 地址、端口、TLS 版本 |
Side-by-Side Comparison
Section titled “Side-by-Side Comparison”对比截图说明
Section titled “对比截图说明”Wireshark 分屏对比:┌─────────────────────────────────────┐│ 明文 MQTT (端口 1883) │├─────────────────────────────────────┤│ No. Protocol Info ││ 1 MQTT CONNECT ││ 2 MQTT CONNACK ││ 3 MQTT PUBLISH: factory/... ││ ├─ Topic: factory/temp │ ← 可见│ └─ Payload: {"temp":24.5} │ ← 可见│ 4 MQTT PINGREQ ││ 5 MQTT PINGRESP │└─────────────────────────────────────┘
┌─────────────────────────────────────┐│ TLS 加密 MQTT (端口 8883) │├─────────────────────────────────────┤│ No. Protocol Info ││ 1 TLSv1.3 Client Hello ││ 2 TLSv1.3 Server Hello ││ 3 TLSv1.3 Certificate ││ 4 TLSv1.3 Change Cipher Spec ││ 5 TLSv1.3 Application Data │ ← 加密│ 6 TLSv1.3 Application Data │ ← 加密│ 7 TLSv1.3 Application Data │ ← 加密└─────────────────────────────────────┘Wireshark Filters for MQTT
Section titled “Wireshark Filters for MQTT”# 显示所有 MQTT 数据包mqtt
# 显示特定类型的 MQTT 包mqtt.type == publishmqtt.type == connectmqtt.type == suback
# 显示特定 Topic 的包mqtt.topic contains "factory"mqtt.topic == "factory/temperature"
# 显示 TLS 包tlstls.handshake.type == 1 # Client Hellotls.handshake.type == 11 # Certificatetls.handshake.type == 14 # ServerHelloDone
# 显示加密的应用数据tls.record.content_type == 23 # Application DataDemonstration Script
Section titled “Demonstration Script”自动对比演示
Section titled “自动对比演示”#!/bin/bash# demo-mqtt-tls.sh - TLS 加密效果演示
BROKER="localhost"TOPIC="demo/$(date +%s)"
echo "========================================="echo "MQTT TLS 加密效果演示"echo "========================================="echo ""
echo "步骤 1: 启动 Wireshark 抓包"echo " 过滤器: tcp.port == 1883 or tcp.port == 8883"echo ""
echo "步骤 2: 发送明文 MQTT 消息 (端口 1883)"echo " Wireshark 中可以看到:"echo " - 消息内容: Hello World"echo " - Topic: demo/..."echo ""mosquitto_pub -h $BROKER -p 1883 \ -t "$TOPIC" -m "Hello World (明文)" \ -d 2>&1 | head -5echo ""
echo "步骤 3: 发送加密 MQTT 消息 (端口 8883)"echo " Wireshark 中只能看到:"echo " - Application Data (加密数据)"echo " - 消息内容不可见"echo ""mosquitto_pub -h $BROKER -p 8883 \ --cafile /opt/mosquitto/certs/fullchain.pem \ -t "$TOPIC" -m "Hello World (TLS加密)" \ -d 2>&1 | head -5echo ""
echo "========================================="echo "演示完成"echo "========================================="Pre-sales Key Points
Section titled “Pre-sales Key Points”演示验证的最佳方式
Section titled “演示验证的最佳方式”| 演示要点 | 操作 | 买家看到的效果 |
|---|---|---|
| 明文风险 | 1883 端口抓包 | ”看,用户名、密码、数据全部可见” |
| 加密保护 | 8883 端口抓包 | ”所有内容都是加密乱码,无法读取” |
| 对比展示 | 分屏对比 | ”同一份数据,加密前后天壤之别” |
| 实际案例 | 真实传感器数据 | ”您的生产数据也会得到同样的保护” |
Summary
Section titled “Summary”本节介绍了使用 Wireshark 分析 MQTT 流量:
- 明文 MQTT:用户名、密码、Topic、Payload 全部可见
- TLS 加密:所有应用数据加密,仅可见 TLS 握手信息
- 对比分析:加密后攻击者无法获取任何 MQTT 内容
- Wireshark 技巧:MQTT 和 TLS 过滤器快速定位相关包
- 演示方法:展示加密前后对比,直观说明安全价值