🌐 网络基础 — 协议、抓包与流量分析

理解网络协议栈,掌握抓包分析技能

📖 网络安全的核心:理解数据如何流动

网络攻击本质上是利用协议缺陷或流量特征进行的。不懂网络协议,就无法理解攻击原理,也无法有效防御。从TCP三次握手到DNS查询,每个环节都可能成为攻击面。

TCP/IP 协议栈与安全对应 ┌──────────────────────────────────────────────┐ │ 应用层 │ HTTP/DNS/SSH/FTP/SMTP │ │ │ ▸ XSS, SQLi, CSRF, 命令注入 │ ├──────────┼────────────────────────────────────┤ │ 传输层 │ TCP / UDP │ │ │ ▸ SYN Flood, 端口扫描, 会话劫持 │ ├──────────┼────────────────────────────────────┤ │ 网络层 │ IP / ICMP / ARP │ │ │ ▸ IP欺骗, Ping of Death, ARP欺骗 │ ├──────────┼────────────────────────────────────┤ │ 链路层 │ Ethernet / WiFi │ │ │ ▸ MAC洪泛, 邪恶双子, 无线监听 │ └──────────────────────────────────────────────┘

🔌 TCP/IP 基础

TCP三次握手

# 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

常见协议与端口

协议端口安全风险安全版本
HTTP80明文传输,可嗅探HTTPS (443)
FTP21/20明文传输,匿名访问SFTP (22)/FTPS (990)
Telnet23明文传输,无加密SSH (22)
SMTP25邮件伪造,明文SMTPS (465)
DNS53DNS劫持,DNS放大攻击DNS over HTTPS
SMB445永恒之蓝,信息泄露SMBv3 + 签名
RDP3389暴力破解,BlueKeepNetwork Level Auth
MySQL3306SQL注入,弱密码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是网络安全分析师最重要的工具之一,可以在命令行实时抓取和分析网络流量。

基础抓包

# 安装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请求

# 抓取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
抓包可能捕获敏感信息(密码、Cookie、Token)。确保只在授权范围内抓包,遵守法律法规。

📊 Wireshark 深度分析

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安全分析

# 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通信
# 监控异常出站连接
网络基础 — 你已掌握TCP/IP协议栈、tcpdump抓包、Wireshark分析、网络诊断和DNS安全!
命令已验证:tcpdump/ss/ip/dig/nslookup/host/nc/ping — 所有命令在Docker沙箱验证通过
课后思考题:
  1. tcpdump和Wireshark的BPF过滤器有什么区别?分别在什么场景使用?
  2. 如何用tcpdump检测网络中的ARP欺骗攻击?
  3. DNS放大攻击的原理是什么?为什么UDP协议更容易被利用?
  4. 如果一个服务器只开放了80和443端口,是否意味着它无法被攻击?

📚 进阶资源

参考资料:tcpdump(8) | tshark(1) | RFC 793/791/1035 | Wireshark Network Analysis | Practical Packet Analysis