Appearance
Tshark 网络数据包分析工具
Tshark 是 Wireshark 的命令行版本,是一个功能强大的网络协议分析器。它可以捕获网络流量并进行深度分析,广泛应用于网络故障排除、安全分析、性能监控等场景。
核心特性
- 实时数据包捕获:直接从网络接口捕获数据包
- 离线分析:读取已保存的 pcap 文件进行分析
- 丰富的过滤器:支持复杂的显示过滤器和捕获过滤器
- 多种输出格式:文本、JSON、XML、CSV 等格式输出
- 协议解析:支持数百种网络协议的解析
基础命令语法
bash
tshark [选项] [-i 接口] [-f 捕获过滤器] [-Y 显示过滤器] [-w 输出文件]1. 数据包捕获
1.1 基础捕获命令
bash
# 查看可用网络接口
tshark -D
# 捕获指定接口的数据包(默认显示前10个包)
tshark -i eth0
# 持续捕获并显示数据包
tshark -i eth0 -c 0TIP
使用 -c 0 表示无限制捕获数据包,直到手动停止(Ctrl+C)
1.2 指定捕获数量
bash
# 捕获100个数据包后自动停止
tshark -i eth0 -c 100
# 捕获30秒后自动停止
tshark -i eth0 -a duration:30
# 当文件大小达到10MB时停止
tshark -i eth0 -a filesize:100001.3 保存到文件
bash
# 将捕获的数据包保存到文件
tshark -i eth0 -w capture.pcap
# 捕获时同时显示和保存
tshark -i eth0 -w capture.pcap -P
# 滚动文件保存(每个文件最大10MB,保留5个文件)
tshark -i eth0 -w capture.pcap -b filesize:10000 -b files:52. 捕获过滤器(在捕获时过滤)
捕获过滤器在数据包进入缓冲区之前就进行过滤,可以减少资源消耗。
2.1 协议过滤
bash
# 只捕获HTTP流量
tshark -i eth0 -f "port 80"
# 只捕获HTTPS流量
tshark -i eth0 -f "port 443"
# 捕获DNS查询
tshark -i eth0 -f "port 53"
# 捕获SSH连接
tshark -i eth0 -f "port 22"2.2 IP 地址过滤
bash
# 捕获特定主机的流量
tshark -i eth0 -f "host 192.168.1.100"
# 捕获来自特定源IP的流量
tshark -i eth0 -f "src host 192.168.1.100"
# 捕获发往特定目标IP的流量
tshark -i eth0 -f "dst host 192.168.1.100"
# 捕获特定网段的流量
tshark -i eth0 -f "net 192.168.1.0/24"2.3 组合过滤条件
bash
# 捕获特定主机的HTTP流量
tshark -i eth0 -f "host 192.168.1.100 and port 80"
# 捕获HTTP或HTTPS流量
tshark -i eth0 -f "port 80 or port 443"
# 排除SSH流量
tshark -i eth0 -f "not port 22"3. 显示过滤器(分析时过滤)
显示过滤器用于对已捕获或已保存的数据包进行过滤显示。
3.1 协议过滤
bash
# 显示HTTP协议的数据包
tshark -r capture.pcap -Y "http"
# 显示HTTPS/TLS协议的数据包
tshark -r capture.pcap -Y "tls"
# 显示DNS查询和响应
tshark -r capture.pcap -Y "dns"
# 显示TCP协议的数据包
tshark -r capture.pcap -Y "tcp"3.2 HTTP 特定过滤
bash
# 显示HTTP请求
tshark -r capture.pcap -Y "http.request"
# 显示HTTP响应
tshark -r capture.pcap -Y "http.response"
# 显示特定HTTP方法
tshark -r capture.pcap -Y "http.request.method == GET"
# 显示HTTP错误响应
tshark -r capture.pcap -Y "http.response.code >= 400"
# 显示访问特定域名的请求
tshark -r capture.pcap -Y "http.host == \"example.com\""3.3 TCP 连接分析
bash
# 显示TCP握手包(SYN)
tshark -r capture.pcap -Y "tcp.flags.syn == 1"
# 显示TCP连接重置包
tshark -r capture.pcap -Y "tcp.flags.reset == 1"
# 显示TCP重传包
tshark -r capture.pcap -Y "tcp.analysis.retransmission"
# 显示特定端口的TCP流量
tshark -r capture.pcap -Y "tcp.port == 80"4. 输出格式控制
4.1 基础输出格式
bash
# 详细显示数据包信息
tshark -r capture.pcap -V
# 输出为JSON格式
tshark -r capture.pcap -T json
# 输出为CSV格式
tshark -r capture.pcap -T tabs
# 自定义字段输出
tshark -r capture.pcap -T fields -e frame.time -e ip.src -e ip.dst -e tcp.port4.2 JSON 输出示例
bash
# 将HTTPS流量输出为JSON格式
tshark -r input.pcap -o "tls.keylog_file:sslkeys.log" -T json > output.json
# 只输出HTTP请求的JSON数据
tshark -r capture.pcap -Y "http.request" -T json5. 实际业务场景应用
5.1 Web 服务器性能分析
bash
# 监控Web服务器的HTTP请求
tshark -i eth0 -f "port 80" -Y "http" -T fields -e frame.time -e ip.src -e http.request.method -e http.request.uri
# 分析HTTP响应时间
tshark -r capture.pcap -Y "http" -T fields -e tcp.stream -e frame.time -e http.response.code5.2 安全监控
bash
# 检测端口扫描行为
tshark -i eth0 -Y "tcp.flags.syn == 1 and tcp.flags.ack == 0" -T fields -e ip.src -e tcp.dstport
# 监控DNS查询
tshark -i eth0 -f "port 53" -Y "dns" -T fields -e ip.src -e dns.qry.name
# 检测异常大的数据包
tshark -i eth0 -Y "frame.len > 1500"5.3 网络故障排除
bash
# 检测TCP连接问题
tshark -r capture.pcap -Y "tcp.analysis.flags" -T fields -e ip.src -e ip.dst -e tcp.analysis.flags
# 分析网络延迟
tshark -r capture.pcap -Y "tcp" -T fields -e tcp.stream -e tcp.analysis.ack_rtt
# 检测丢包情况
tshark -r capture.pcap -Y "tcp.analysis.lost_segment or tcp.analysis.retransmission"6. 高级用法
6.1 统计分析
bash
# 协议统计
tshark -r capture.pcap -q -z io,phs
# 端点统计
tshark -r capture.pcap -q -z endpoints,ip
# HTTP统计
tshark -r capture.pcap -q -z http,stat
# 流量统计(每秒)
tshark -r capture.pcap -q -z io,stat,16.2 流分析
bash
# 跟踪特定TCP流
tshark -r capture.pcap -Y "tcp.stream == 0" -T fields -e tcp.payload
# 重建HTTP对象
tshark -r capture.pcap --export-objects http,./http_objects/
# 提取TLS证书
tshark -r capture.pcap -Y "tls.handshake.certificate" -T fields -e tls.handshake.certificate7. 工作流程图
8. 常用参数速查
| 参数 | 说明 | 示例 |
|---|---|---|
-i | 指定捕获接口 | -i eth0 |
-f | 捕获过滤器 | -f "port 80" |
-Y | 显示过滤器 | -Y "http" |
-w | 保存到文件 | -w capture.pcap |
-r | 读取文件 | -r input.pcap |
-c | 捕获包数量 | -c 100 |
-T | 输出格式 | -T json |
-V | 详细输出 | -V |
-q | 静默模式 | -q |
-z | 统计分析 | -z io,stat,1 |
IMPORTANT
在生产环境中使用 Tshark 时,需要考虑性能影响。大量数据包捕获可能会消耗大量 CPU 和存储资源。
WARNING
数据包捕获可能包含敏感信息,请确保遵守相关的隐私和安全政策。
若需特定数据包(如仅 HTTP),需用-Y 添加显示过滤器(如-Y "http"),否则输出全部包。