traffic-analysis
Scannednpx machina-cli add skill chaterm/terminal-skills/traffic-analysis --openclawFiles (1)
SKILL.md
4.4 KB
流量分析与抓包
概述
tcpdump、Wireshark、流量分析与网络诊断技能。
tcpdump
基础用法
# 监听所有接口
tcpdump -i any
# 指定接口
tcpdump -i eth0
# 详细输出
tcpdump -v
tcpdump -vv
tcpdump -vvv
# 显示 ASCII
tcpdump -A
# 显示十六进制
tcpdump -X
tcpdump -XX
# 不解析主机名
tcpdump -n
# 不解析端口名
tcpdump -nn
过滤表达式
# 主机过滤
tcpdump host 192.168.1.100
tcpdump src host 192.168.1.100
tcpdump dst host 192.168.1.100
# 网段过滤
tcpdump net 192.168.1.0/24
# 端口过滤
tcpdump port 80
tcpdump src port 80
tcpdump dst port 443
tcpdump portrange 8000-9000
# 协议过滤
tcpdump tcp
tcpdump udp
tcpdump icmp
tcpdump arp
# 组合过滤
tcpdump 'host 192.168.1.100 and port 80'
tcpdump 'src 192.168.1.100 and dst port 443'
tcpdump 'tcp and (port 80 or port 443)'
tcpdump 'not port 22'
保存与读取
# 保存到文件
tcpdump -w capture.pcap
tcpdump -w capture.pcap -c 1000 # 限制包数
# 读取文件
tcpdump -r capture.pcap
tcpdump -r capture.pcap -nn
# 轮转文件
tcpdump -w capture-%H%M%S.pcap -G 3600 # 每小时
tcpdump -w capture.pcap -C 100 # 每 100MB
高级过滤
# TCP 标志
tcpdump 'tcp[tcpflags] & tcp-syn != 0'
tcpdump 'tcp[tcpflags] & tcp-rst != 0'
tcpdump 'tcp[tcpflags] == tcp-syn'
# HTTP 请求
tcpdump -A 'tcp port 80 and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0)'
# DNS 查询
tcpdump -i any 'udp port 53'
# 大包
tcpdump 'greater 1000'
tcpdump 'less 100'
tshark (Wireshark CLI)
基础用法
# 安装
apt install tshark
# 监听
tshark -i eth0
# 指定过滤器
tshark -i eth0 -f "port 80"
# 显示过滤器
tshark -i eth0 -Y "http"
字段提取
# 提取特定字段
tshark -r capture.pcap -T fields -e ip.src -e ip.dst -e tcp.port
# HTTP 请求
tshark -r capture.pcap -Y "http.request" -T fields -e http.host -e http.request.uri
# DNS 查询
tshark -r capture.pcap -Y "dns.qry.name" -T fields -e dns.qry.name
统计分析
# 协议统计
tshark -r capture.pcap -q -z io,phs
# 会话统计
tshark -r capture.pcap -q -z conv,tcp
# HTTP 统计
tshark -r capture.pcap -q -z http,tree
# 端点统计
tshark -r capture.pcap -q -z endpoints,ip
ngrep
基础用法
# 安装
apt install ngrep
# 搜索内容
ngrep -q 'GET' port 80
ngrep -q 'password' port 80
# 指定接口
ngrep -d eth0 'pattern'
# 忽略大小写
ngrep -qi 'error'
iftop / nethogs
iftop
# 安装
apt install iftop
# 基础用法
iftop
iftop -i eth0
# 不解析主机名
iftop -n
# 显示端口
iftop -P
# 过滤
iftop -f "dst port 80"
nethogs
# 安装
apt install nethogs
# 按进程显示
nethogs
nethogs eth0
# 刷新间隔
nethogs -d 2
常见场景
场景 1:HTTP 流量分析
# 抓取 HTTP 请求
tcpdump -i any -A -s 0 'tcp port 80 and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0)'
# 提取 HTTP 头
tcpdump -i any -A 'tcp port 80' | grep -E "^(GET|POST|HTTP|Host:|Content-)"
# tshark 分析
tshark -i any -Y "http.request" -T fields -e http.host -e http.request.method -e http.request.uri
场景 2:DNS 问题排查
# 抓取 DNS
tcpdump -i any -nn port 53
# 详细 DNS 信息
tcpdump -i any -vvv port 53
# tshark DNS 分析
tshark -i any -Y "dns" -T fields -e dns.qry.name -e dns.a
场景 3:连接问题诊断
# TCP 握手
tcpdump -i any 'tcp[tcpflags] & (tcp-syn|tcp-fin|tcp-rst) != 0'
# 重传
tcpdump -i any 'tcp[tcpflags] & tcp-syn != 0 and tcp[tcpflags] & tcp-ack != 0'
# 连接重置
tcpdump -i any 'tcp[tcpflags] & tcp-rst != 0'
场景 4:带宽分析
# 实时带宽
iftop -i eth0 -P -n
# 按进程
nethogs eth0
# 统计流量
tcpdump -i eth0 -w - | pv > /dev/null
故障排查
| 问题 | 排查方法 |
|---|---|
| 丢包 | 检查 tcpdump 统计、网卡队列 |
| 延迟高 | 分析 RTT、重传 |
| 连接失败 | 检查 SYN/RST 包 |
| 带宽占用 | 使用 iftop/nethogs |
# 查看丢包
tcpdump -i eth0 -w /dev/null 2>&1 | grep -i drop
# 网卡统计
ethtool -S eth0 | grep -i error
cat /proc/net/dev
# 连接状态
ss -s
netstat -s | grep -i retrans
Source
git clone https://github.com/chaterm/terminal-skills/blob/main/network/traffic-analysis/SKILL.mdView on GitHub Overview
traffic-analysis 提供对网络流量的捕获、筛选、分析和带宽监控能力。你可以实时监听接口、应用过滤表达式、保存为 pcap,并使用 tshark 提取字段、执行统计分析;同时借助 ngrep、iftop 与 nethogs 进行模式搜索与带宽/进程级监控。开发者使用它来排查 HTTP、DNS、连接问题,以及带宽异常与网络故障的根因。
How This Skill Works
你先用 tcpdump 在目标接口捕获数据包,并通过 -i、-w、-c、-G 等参数实现目标接口、轮转文件和限包数;再用过滤表达式聚焦目标流量。接着使用 tshark 进行字段提取与统计分析,利用 -r、-Y、-T 等选项组合获得可读结果。ngrep 提供实时模式匹配搜索,iftop/nethogs 提供实时带宽与进程级流量视图,帮助你从不同维度诊断问题并生成报告。
When to Use It
- 场景 1:HTTP 流量分析。你需要抓取并分析 HTTP 请求/响应头与行为,可以结合 tcpdump 进行报文捕获并用 tshark 提取 http.host、http.request.method、http.request.uri 等字段。
- 场景 2:DNS 问题排查。遇到域名解析异常时,直接抓取域名解析流量(port 53),并用 tshark 提取 dns.qry.name、dns.a 等信息。
- 场景 3:连接问题诊断。你要定位 TCP 握手、重传、RST 等包的出现时序,如使用 tcpdump 'tcp[tcpflags] & (tcp-syn|tcp-fin|tcp-rst) != 0' 来检查连接状态。
- 场景 4:带宽分析。需要实时监控带宽与分布,可以使用 iftop、nethogs 观察接口或进程级流量,并结合 tcpdump 记录用于离线分析。
- 场景 5:综合故障排查。结合以上工具链,对丢包、高延迟、连接中断和带宽异常等故障场景做跨工具对比分析。
Quick Start
- 安装并准备工具:确保系统上已安装 tcpdump、tshark(Wireshark)、ngrep、iftop、nethogs。
- 第一步:选择接口并开始抓包,例如 tcpdump -i eth0 -w capture.pcap -c 1000。
- 第二步:应用过滤,聚焦目标流量,例如 tcpdump 'host 192.168.1.100 and port 80' 或 -f 过滤表达式。
- 第三步:后续分析:使用 tshark 读取 capture.pcap 并提取字段,如 tshark -r capture.pcap -Y 'http.request' -T fields -e http.host -e http.request.uri。
- 第四步(可选):实时监控带宽与进程流量,使用 iftop -i eth0 或 nethogs eth0。
Best Practices
- 在生产环境进行抓包时,优先使用 -c、-G、-C 等轮转/限包选项,避免对网络造成不可控压力。
- 对输出进行最小化处理:tcpdump -n -nn 以避免DNS/端口名解析带来的额外负载;将关键字段输出到 tshark 以生成易分析的统计数据。
- 结合 -s 0 全载荷抓包(谨慎使用,确保符合隐私与合规要求),并将数据保存为 pcap 以便离线分析。
- 对复杂流量使用组合过滤表达式,如 tcpdump 'tcp and (port 80 or port 443) and host 192.168.1.100',以减少不必要的数据。
- 利用 tshark 的 -T fields/-e 参数提取稳定的字段集,方便后续自动化报告与告警。
Example Use Cases
- 场景:HTTP 请求分析。你使用 tcpdump -i any -A -s 0 'tcp port 80 and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0)' 捕获明文请求体,然后用 tshark -i any -Y 'http.request' -T fields -e http.host -e http.request.method -e http.request.uri 提取关键字段以定位慢请求源。
- 场景:DNS 查询分析。针对域名解析异常,执行 tcpdump -i any -nn port 53,结合 tshark -Y 'dns' -T fields -e dns.qry.name -e dns.a,快速定位被解析的域名与解析结果。
- 场景:连接问题诊断。定位 TCP 握手与重传问题,使用 tcpdump -i any 'tcp[tcpflags] & (tcp-syn|tcp-fin|tcp-rst) != 0' 观察握手/断开时序,辅助判断是否存在中间设备重置或丢包导致的连接失败。
- 场景:带宽分析。实时带宽可通过 iftop -i eth0 -P -n 观察端口/对等方的带宽占用,nethogs eth0 显示按进程的流量分布,结合 tcpdump 记录用于离线分析。
- 场景:综合故障排查。将上述工具链结合,针对丢包、高延迟、抖动和异常带宽进行跨工具对比分析,形成可复现的诊断步骤与报告。
Frequently Asked Questions
Add this skill to your agents