理解网络协议栈,掌握抓包分析技能
网络攻击本质上是利用协议缺陷或流量特征进行的。不懂网络协议,就无法理解攻击原理,也无法有效防御。从TCP三次握手到DNS查询,每个环节都可能成为攻击面。
# TCP三次握手过程
客户端 服务器
│── SYN (seq=x) ──────────→│ 1. 客户端发起连接
│←─ SYN+ACK (seq=y,ack=x+1)│ 2. 服务器确认并回应
│── ACK (ack=y+1) ────────→│ 3. 客户端确认,连接建立
# 用tcpdump观察三次握手
tcpdump -i any -Snn "tcp[tcpflags] & (tcp-syn|tcp-ack) != 0"
# 用hping3手动构造SYN包
hping3 -S -p 80 -c 3 target.com
| 协议 | 端口 | 安全风险 | 安全版本 |
|---|---|---|---|
| HTTP | 80 | 明文传输,可嗅探 | HTTPS (443) |
| FTP | 21/20 | 明文传输,匿名访问 | SFTP (22)/FTPS (990) |
| Telnet | 23 | 明文传输,无加密 | SSH (22) |
| SMTP | 25 | 邮件伪造,明文 | SMTPS (465) |
| DNS | 53 | DNS劫持,DNS放大攻击 | DNS over HTTPS |
| SMB | 445 | 永恒之蓝,信息泄露 | SMBv3 + 签名 |
| RDP | 3389 | 暴力破解,BlueKeep | Network Level Auth |
| MySQL | 3306 | SQL注入,弱密码 | TLS加密连接 |
# 查看网络接口
ip addr show # 新版工具
ifconfig -a # 旧版工具
# 查看路由表
ip route show
netstat -rn # 旧版
route -n # 旧版
# 查看网络连接
ss -tlnp # TCP监听端口
ss -tunap # 所有TCP/UDP连接
netstat -tlnp # 旧版
# DNS查询
dig google.com A # 查A记录
dig google.com MX # 查MX记录
dig google.com TXT # 查TXT记录
nslookup google.com # 简单查询
host google.com # 简洁输出
# 跟踪路由
traceroute google.com # Linux
mtr google.com # 持续跟踪(更实用)
tcpdump是网络安全分析师最重要的工具之一,可以在命令行实时抓取和分析网络流量。
# 安装tcpdump
apt install tcpdump # Debian/Ubuntu
yum install tcpdump # CentOS/RHEL
# 抓取所有流量
tcpdump -i any # 所有接口
tcpdump -i eth0 # 指定接口
# 抓取指定端口
tcpdump -i any port 80 # HTTP流量
tcpdump -i any port 53 # DNS流量
tcpdump -i any port 22 # SSH流量
# 抓取指定主机
tcpdump -i any host 192.168.1.100
tcpdump -i any src host 192.168.1.100 # 源地址
tcpdump -i any dst host 10.0.0.1 # 目标地址
# 抓取指定网段
tcpdump -i any net 192.168.1.0/24
# 组合过滤条件
tcpdump -i any "host 192.168.1.100 and port 80"
tcpdump -i any "port 80 and (tcp[tcpflags] & tcp-syn != 0)"
tcpdump -i any "port 443 and greater 1000" # 大于1000字节
# 抓取TCP标志位
tcpdump -i any "tcp[tcpflags] & tcp-syn != 0" # SYN包
tcpdump -i any "tcp[tcpflags] & tcp-rst != 0" # RST包
tcpdump -i any "tcp[tcpflags] & tcp-fin != 0" # FIN包
# 抓取DNS查询
tcpdump -i any -nn port 53
# 显示ASCII内容
tcpdump -i any -A port 80 # ASCII显示
tcpdump -i any -XX port 80 # 十六进制+ASCII
# 保存到文件
tcpdump -i any -w capture.pcap # 保存为pcap格式
tcpdump -i any -w capture.pcap -c 1000 # 只抓1000个包
# 读取pcap文件
tcpdump -r capture.pcap
tcpdump -r capture.pcap -A port 80
# 抓取HTTP流量并显示内容
tcpdump -i any -A -s 0 'tcp port 80 and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0)'
# 典型HTTP请求抓包输出
# GET /login HTTP/1.1
# Host: example.com
# Cookie: session=abc123
# Content-Type: application/x-www-form-urlencoded
#
# username=admin&password=secret123
Wireshark是图形化的网络协议分析器,比tcpdump更直观。
# 安装Wireshark
apt install wireshark # Linux
# 或下载: https://www.wireshark.org/
# 命令行版本 tshark
tshark -i eth0 # 实时抓包
tshark -i eth0 -f "port 80" # BPF过滤
tshark -r capture.pcap -Y "http.request" # 显示过滤器
tshark -r capture.pcap -Y "dns" # DNS流量
tshark -r capture.pcap -Y "tcp.analysis.retransmission" # 重传
# 提取HTTP对象(图片、文件等)
tshark -r capture.pcap --export-objects http,/tmp/http_objects/
# 提取凭证
tshark -r capture.pcap -Y "http.request.method == POST" \
-T fields -e frame.number -e ip.src -e http.file_data
# 统计分析
tshark -r capture.pcap -z conv,tcp # TCP会话统计
tshark -r capture.pcap -z proto,colinfo,ip.src,ip.src # 协议统计
# Wireshark显示过滤器(在GUI中使用)
http # 所有HTTP流量
dns # 所有DNS流量
ip.addr == 192.168.1.100 # 指定IP
tcp.port == 80 # 指定端口
http.request.method == "POST" # POST请求
http.cookie contains "session" # 包含Cookie
tcp.analysis.retransmission # TCP重传
dns.qry.name contains "evil" # 恶意域名查询
# 连通性测试
ping -c 4 google.com # ICMP测试
ping -c 4 -i 0.1 google.com # 快速ping
traceroute google.com # 路由跟踪
# 端口连通性测试
nc -zv target.com 80 # TCP端口测试
nc -zvu target.com 53 # UDP端口测试
nc -zv target.com 22 80 443 # 多端口测试
# DNS故障排查
dig @8.8.8.8 google.com # 指定DNS服务器
dig +trace google.com # 完整DNS解析路径
dig google.com +short # 只显示IP
# 查看ARP表
arp -a # 查看ARP缓存
ip neigh show # 新版工具
# 查看网络统计
netstat -s # 协议统计
ss -s # 套接字统计
cat /proc/net/dev # 接口流量统计
# DNS区域传输尝试(信息收集)
dig @ns1.target.com target.com AXFR
# DNS记录查询
dig target.com ANY # 查所有记录
dig target.com CNAME # 查CNAME
dig -x 192.168.1.1 # 反向DNS查询
# DNS缓存投毒检测
dig @local-dns target.com # 查询本地DNS
dig @8.8.8.8 target.com # 对比公共DNS
# 结果不一致可能意味着DNS劫持
# DNS放大攻击验证
dig +dnssec target.com ANY # DNSSEC查询
# 观察响应大小是否异常大
# 1. 禁用不必要的协议和服务
# 检查监听端口
ss -tlnp
# 关闭不必要的服务
systemctl disable telnet
systemctl disable rsh
systemctl disable rlogin
# 2. 网络分段
# 使用VLAN隔离不同安全级别区域
# DMZ区、内网区、管理区分别隔离
# 3. 加密所有敏感通信
# SSH替代Telnet
# HTTPS替代HTTP
# SFTP替代FTP
# 4. 网络监控
# 部署IDS/IPS监控异常流量
# 定期分析DNS日志发现C2通信
# 监控异常出站连接