TCP/UDP 网络协议教程 / 14-网络调试工具
14 - 网络调试工具
14.1 工具总览
| 工具 | 功能 | 场景 |
|---|---|---|
| tcpdump | 命令行抓包 | 服务器端抓包分析 |
| Wireshark | 图形化协议分析 | 深度协议分析 |
| nc (netcat) | 网络瑞士军刀 | 端口测试、数据传输 |
| ss | Socket 统计 | 查看连接状态 |
| netstat | 网络统计(旧) | 传统工具 |
| nmap | 端口扫描 | 网络探测、安全审计 |
| curl | HTTP 客户端 | API 测试 |
| iperf3 | 带宽测试 | 网络性能测试 |
14.2 tcpdump
基本用法
# 查看可用接口
tcpdump -D
# 抓取指定接口的所有包
sudo tcpdump -i eth0
# 不解析主机名和端口号
sudo tcpdump -i eth0 -nn
# 抓取指定主机
sudo tcpdump -i eth0 -nn host 192.168.1.1
# 抓取指定端口
sudo tcpdump -i eth0 -nn port 80
# 抓取 TCP 包
sudo tcpdump -i eth0 -nn tcp
# 抓取 UDP 包
sudo tcpdump -i eth0 -nn udp
# 显示详细信息
sudo tcpdump -i eth0 -vvv
# 显示十六进制内容
sudo tcpdump -i eth0 -XX
BPF 过滤
# 组合过滤
sudo tcpdump -i eth0 'tcp and port 80 and host 192.168.1.1'
# TCP 标志位过滤
sudo tcpdump -i eth0 'tcp[tcpflags] & (tcp-syn) != 0' # SYN 包
sudo tcpdump -i eth0 'tcp[tcpflags] & (tcp-fin) != 0' # FIN 包
sudo tcpdump -i eth0 'tcp[tcpflags] & (tcp-rst) != 0' # RST 包
# 纯 SYN(新连接)
sudo tcpdump -i eth0 'tcp[tcpflags] == tcp-syn'
# 端口范围
sudo tcpdump -i eth0 'portrange 8000-9000'
# 源/目的过滤
sudo tcpdump -i eth0 'src 192.168.1.1 and dst port 80'
保存与读取
# 保存到 pcap 文件
sudo tcpdump -i eth0 -w capture.pcap
# 限制文件大小(每个 100MB)
sudo tcpdump -i eth0 -w capture.pcap -C 100
# 限制包数量
sudo tcpdump -i eth0 -w capture.pcap -c 1000
# 读取 pcap 文件
tcpdump -r capture.pcap
# 读取并过滤
tcpdump -r capture.pcap 'tcp port 80'
实用场景
# 抓取三次握手
sudo tcpdump -i eth0 -nn 'tcp[tcpflags] == tcp-syn or tcp[tcpflags] == (tcp-syn|tcp-ack)'
# 抓取 DNS
sudo tcpdump -i eth0 -nn port 53
# 抓取 ICMP
sudo tcpdump -i eth0 -nn icmp
# 抓取重传
sudo tcpdump -i eth0 -nn 'tcp' | grep -i retransmit
# 查找 RST 包(连接异常断开)
sudo tcpdump -i eth0 -nn 'tcp[tcpflags] & (tcp-rst) != 0'
14.3 Wireshark
显示过滤语法
# 协议过滤
tcp
udp
http
dns
tls
# IP 过滤
ip.addr == 192.168.1.1
ip.src == 192.168.1.1
ip.dst == 192.168.1.1
# 端口过滤
tcp.port == 80
tcp.dstport == 443
# TCP 标志位
tcp.flags.syn == 1
tcp.flags.fin == 1
tcp.flags.rst == 1
# 序列号
tcp.seq == 0
tcp.ack > 1000000
# HTTP
http.request.method == "GET"
http.response.code == 200
# 组合
tcp.port == 80 && ip.addr == 192.168.1.1
tcp.analysis.retransmission # 重传检测
TCP 流追踪
1. 右键点击 TCP 包
2. Follow → TCP Stream
3. 查看完整 TCP 对话
快捷键:Ctrl+Alt+Shift+T
解密 HTTPS
# 设置环境变量
export SSLKEYLOGFILE=/tmp/sslkey.log
# 启动浏览器
firefox & # 或 chrome
# Wireshark 中设置:
# Edit → Preferences → Protocols → TLS
# (Pre)-Master-Secret log filename: /tmp/sslkey.log
14.4 nc (netcat)
# 连接到远程端口
nc example.com 80
# 监听端口
nc -l 8080
# 端口扫描
nc -zv example.com 80-100
# 传输文件
# 接收方:
nc -l 9999 > received_file
# 发送方:
nc remote_host 9999 < file_to_send
# 发送 HTTP 请求
echo -e "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n" | nc example.com 80
# 测试 UDP
echo "test" | nc -u 8.8.8.8 53
# 设置超时
nc -w 5 example.com 80
# 简易聊天
# 主机 A:nc -l 9999
# 主机 B:nc host_a_ip 9999
14.5 ss
# 查看所有 TCP 连接
ss -tn
# 查看所有 UDP 连接
ss -un
# 查看监听端口
ss -tlnp
# 查看所有连接和进程
ss -tunap
# 按状态过滤
ss -tn state established
ss -tn state time-wait
ss -tn state close-wait
ss -tn state syn-recv
# 按地址过滤
ss -tn dst 192.168.1.1
ss -tn src :80
# 统计信息
ss -s
# 查看连接详细信息(窗口、RTT、cwnd)
ss -tnpi
# 输出示例:
# State Recv-Q Send-Q Local Address:Port Peer Address:Port
# ESTAB 0 0 192.168.1.1:22 192.168.1.100:52345
# cubic wscale:7,7 rto:204 rtt:1.5/0.75 ato:40 mss:1448
# rcv_space:29200 snd_cwnd:10 ssthresh:7
ss 输出字段
| 字段 | 说明 |
|---|---|
| Recv-Q | 接收队列中等待读取的字节数 |
| Send-Q | 发送队列中等待发送的字节数 |
| rto | 重传超时 |
| rtt | 往返时间估计/偏差 |
| mss | 最大段大小 |
| snd_cwnd | 拥塞窗口大小(段数) |
| snd_wnd | 发送窗口大小 |
| rcv_space | 接收窗口空间 |
高级过滤
# 查找大量 Send-Q 积压的连接
ss -tn | awk '$2 > 10000 {print}'
# 统计每个状态的连接数
ss -tan | awk 'NR>1 {print $1}' | sort | uniq -c | sort -rn
# 查看 TIME_WAIT 数量
ss -tan state time-wait | wc -l
14.6 netstat
# 查看所有连接
netstat -tunap
# 查看监听端口
netstat -tlnp
# 查看路由表
netstat -rn
# 查看网络统计
netstat -s
# 查看特定进程
netstat -tunap | grep nginx
💡 推荐使用
ss替代netstat,ss 更快、功能更强
14.7 nmap
端口扫描
# 扫描常用端口
nmap 192.168.1.1
# 扫描所有端口
nmap -p- 192.168.1.1
# 扫描指定端口
nmap -p 22,80,443 192.168.1.1
# TCP SYN 扫描(半开扫描,需 root)
sudo nmap -sS 192.168.1.1
# TCP 连接扫描
nmap -sT 192.168.1.1
# UDP 扫描
sudo nmap -sU 192.168.1.1
# 快速扫描
nmap -F 192.168.1.1
服务检测
# 服务版本检测
nmap -sV 192.168.1.1
# 操作系统检测
sudo nmap -O 192.168.1.1
# 综合扫描
sudo nmap -A 192.168.1.1
网络发现
# Ping 扫描(发现活跃主机)
nmap -sn 192.168.1.0/24
# ARP 扫描(局域网)
sudo nmap -PR -sn 192.168.1.0/24
14.8 curl
# 详细输出
curl -v https://example.com
# 时间统计
curl -w "\nDNS: %{time_namelookup}s\nConnect: %{time_connect}s\nTTFB: %{time_starttransfer}s\nTotal: %{time_total}s\n" -o /dev/null -s https://example.com
# 只看 HTTP 头
curl -I https://example.com
# HTTP/3
curl --http3 https://example.com
# 指定 DNS 服务器
curl --dns-servers 8.8.8.8 https://example.com
# IPv4/IPv6
curl -4 https://example.com
curl -6 https://example.com
# 超时设置
curl --connect-timeout 5 --max-time 30 https://example.com
14.9 iperf3
# 服务端
iperf3 -s
# TCP 测试
iperf3 -c server_ip
iperf3 -c server_ip -t 30 # 测试 30 秒
iperf3 -c server_ip -P 4 # 4 个并发流
# UDP 测试
iperf3 -c server_ip -u
iperf3 -c server_ip -u -b 100M # 限制 100Mbps
# 反向测试
iperf3 -c server_ip -R
14.10 问题排查流程
1. 本地网络检查
├── ip addr show # 检查 IP
├── ip route show # 检查路由
├── ping gateway # 测试网关
└── ping 8.8.8.8 # 测试公网
2. DNS 检查
├── nslookup domain # DNS 查询
└── dig domain # 详细 DNS
3. 连接测试
├── nc -zv host port # TCP 测试
├── nc -zuv host port # UDP 测试
└── curl -v host # HTTP 测试
4. 抓包分析
├── tcpdump -i eth0 -w capture.pcap port 80
└── wireshark capture.pcap
5. 系统状态
├── ss -s # 连接统计
└── dmesg | grep network
14.11 注意事项
⚠️ tcpdump 需要 root 权限:抓包需要 raw socket 权限
⚠️ pcap 文件可能很大:使用 -C 限制文件大小
⚠️ Wireshark 解密 HTTPS:需要 SSLKEYLOGFILE 环境变量
⚠️ nmap 扫描可能触发告警:未经授权的扫描可能被视为攻击
14.12 扩展阅读
下一章:15 - 最佳实践 - 性能优化、安全配置、协议选择