tshark
2026/5/29工具工具流量分析tshark大约 3 分钟
tshark
是什么
tshark 是 Wireshark 的命令行版本,适合在终端里过滤、提取和批量分析 pcap。
CTF 中常用于:
- 提取 HTTP 对象
- 过滤 DNS/HTTP/TCP 流
- 批量导出字段
- 在没有图形界面的服务器上分析流量
安装与配置
Ubuntu/Kali:
sudo apt update
sudo apt install tshark验证:
tshark -v基本用法
查看包概览
tshark -r capture.pcap过滤 HTTP
tshark -r capture.pcap -Y "http"过滤 DNS
tshark -r capture.pcap -Y "dns"导出字段
tshark -r capture.pcap -Y "http.request" -T fields -e http.host -e http.request.uri追踪 TCP 流
tshark -r capture.pcap -q -z follow,tcp,ascii,0更多过滤器
HTTP 过滤
# HTTP 请求
tshark -r capture.pcap -Y "http.request"
# HTTP 响应
tshark -r capture.pcap -Y "http.response"
# HTTP POST 请求
tshark -r capture.pcap -Y "http.request.method == POST"
# 特定主机
tshark -r capture.pcap -Y "http.host == target.com"
# 特定 URI
tshark -r capture.pcap -Y "http.request.uri contains /api"
# 特定状态码
tshark -r capture.pcap -Y "http.response.code == 200"DNS 过滤
# DNS 查询
tshark -r capture.pcap -Y "dns"
# DNS 请求
tshark -r capture.pcap -Y "dns.flags.response == 0"
# DNS 响应
tshark -r capture.pcap -Y "dns.flags.response == 1"
# 特定域名
tshark -r capture.pcap -Y "dns.qry.name contains example.com"TCP 过滤
# 特定端口
tshark -r capture.pcap -Y "tcp.port == 80"
# 特定源端口
tshark -r capture.pcap -Y "tcp.srcport == 12345"
# TCP 流
tshark -r capture.pcap -Y "tcp.stream eq 0"FTP 过滤
# FTP 请求
tshark -r capture.pcap -Y "ftp.request"
# FTP 数据
tshark -r capture.pcap -Y "ftp-data"
# FTP 密码
tshark -r capture.pcap -Y "ftp.request.command == PASS"ICMP 过滤
# ICMP 包
tshark -r capture.pcap -Y "icmp"
# ICMP 数据
tshark -r capture.pcap -Y "icmp.type == 8"输出格式
导出字段
# 导出 HTTP URI
tshark -r capture.pcap -Y "http.request" -T fields -e http.request.full_uri
# 导出多个字段
tshark -r capture.pcap -Y "http.request" -T fields -e http.host -e http.request.uri -e http.request.method
# 导出 DNS 查询
tshark -r capture.pcap -Y "dns.qry.name" -T fields -e dns.qry.name导出为 JSON
tshark -r capture.pcap -T json
tshark -r capture.pcap -T json -Y "http"导出为 CSV
tshark -r capture.pcap -T fields -e frame.number -e ip.src -e ip.dst -e http.host -E header=y -E separator=,导出为 PDML
tshark -r capture.pcap -T pdml > capture.pdml对象导出
导出 HTTP 对象
tshark -r capture.pcap --export-objects http,out导出后继续用 file、strings、binwalk 分析。
导出 SMB 对象
tshark -r capture.pcap --export-objects smb,out导出 IMF(邮件)
tshark -r capture.pcap --export-objects imf,out导出 TFTP 对象
tshark -r capture.pcap --export-objects tftp,out统计信息
协议层级统计
tshark -r capture.pcap -q -z io,phs会话统计
tshark -r capture.pcap -q -z conv,tcp
tshark -r capture.pcap -q -z conv,ipHTTP 请求统计
tshark -r capture.pcap -q -z http,treeDNS 统计
tshark -r capture.pcap -q -z dns,tree端点统计
tshark -r capture.pcap -q -z endpoints,tcp
tshark -r capture.pcap -q -z endpoints,ipCTF常用技巧
提取 HTTP URI
tshark -r capture.pcap -Y "http.request" -T fields -e http.request.full_uri提取 DNS 查询
tshark -r capture.pcap -Y "dns.qry.name" -T fields -e dns.qry.name导出 HTTP 对象
tshark -r capture.pcap --export-objects http,out导出后继续用 file、strings、binwalk 分析。
查找 flag
# 搜索 HTTP 响应中的 flag
tshark -r capture.pcap -Y "http contains flag"
# 搜索所有包中的 flag
tshark -r capture.pcap -Y "frame contains flag"
# 导出所有字符串
tshark -r capture.pcap -T fields -e data | xxd -r -p | strings | grep flag分析 FTP 流量
# 查看 FTP 命令
tshark -r capture.pcap -Y "ftp.request"
# 查看 FTP 数据
tshark -r capture.pcap -Y "ftp-data"
# 提取 FTP 文件
tshark -r capture.pcap --export-objects ftp-data,out分析 DNS 隧道
# 查看 DNS 查询
tshark -r capture.pcap -Y "dns" -T fields -e dns.qry.name
# 提取长域名(可能是数据外带)
tshark -r capture.pcap -Y "dns" -T fields -e dns.qry.name | awk '{print length, $0}' | sort -rn批量分析脚本
#!/bin/bash
# 批量分析 pcap 文件
for f in *.pcap; do
echo "=== $f ==="
echo "HTTP requests:"
tshark -r "$f" -Y "http.request" -T fields -e http.host -e http.request.uri
echo "DNS queries:"
tshark -r "$f" -Y "dns.qry.name" -T fields -e dns.qry.name
echo ""
done常见问题
过滤表达式和 tcpdump 一样吗
不一样。-Y 使用 Wireshark display filter,例如 http.request、dns.qry.name。
中文或二进制显示乱
先导出对象或流,再用文件工具分析,不要只看终端输出。
tshark 和 Wireshark 用哪个
图形观察用 Wireshark,批量提取和复现命令用 tshark。
输出太多
# 限制输出行数
tshark -r capture.pcap -Y "http" | head -20
# 使用 -c 限制包数
tshark -r capture.pcap -c 100 -Y "http"内存不足
# 使用 -2 两次遍历(减少内存)
tshark -r capture.pcap -2 -Y "http"关联
- 流量包基础
- Wireshark
- file
- binwalk
- xxd与hexdump