未分类 · 2025-07-27 0

抓包 TCP 重传与拒绝

一、TCP Flags 标志位

Flag 缩写 含义
SYN [S] 同步序号,用于发起一个连接。
ACK [.] 确认序号有效。通常单独显示为点(.),但在其他标志存在时会明确写出。
FIN [F] 发送端已经完成数据发送,请求关闭连接。
RST [R] 重置连接。用于异常终止连接。
PSH [P] 推送功能,提示接收方尽快将数据提交给应用程序,而不是等待更多的数据到来。
URG [U] 紧急指针字段有效,表示该数据段中有紧急数据。
ECE [E] ECN-Echo,用于显式拥塞通知机制。
CWR [C] 拥塞窗口减少,用于显式拥塞通知机制。

二、服务端端口未开

服务端端口未开

tcpdump 在服务端上抓包

1.服务端运行 tcpdump

root@fdc276a87cec:~# tcpdump -n -v -X

2.客户端 TCP 连接

客户端 tcp 连接服务端未开放的端口

zxm@zxm-pc:~$ nc -v 172.17.0.2 1231
nc: connect to 172.17.0.2 port 1231 (tcp) failed: Connection refused

3.抓包结果

root@fdc276a87cec:~# tcpdump -n -v -X
tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
18:17:41.991909 IP (tos 0x0, ttl 64, id 12065, offset 0, flags [DF], proto TCP (6), length 60)
    172.17.0.1.40268 > 172.17.0.2.1231: Flags [S], cksum 0x5854 (incorrect -> 0xb1bb), seq 2065754012, win 64240, options [mss 1460,sackOK,TS val 3598870482 ecr 0,nop,wscale 7], length 0
    0x0000:  4500 003c 2f21 4000 4006 b375 ac11 0001  E..</!@.@..u....
    0x0010:  ac11 0002 9d4c 04cf 7b20 e79c 0000 0000  .....L..{.......
    0x0020:  a002 faf0 5854 0000 0204 05b4 0402 080a  ....XT..........
    0x0030:  d682 67d2 0000 0000 0103 0307            ..g.........
18:17:41.991973 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 40)
    172.17.0.2.1231 > 172.17.0.1.40268: Flags [R.], cksum 0x52d1 (correct), seq 0, ack 2065754013, win 0, length 0
    0x0000:  4500 0028 0000 4000 4006 e2aa ac11 0002  E..(..@.@.......
    0x0010:  ac11 0001 04cf 9d4c 0000 0000 7b20 e79d  .......L....{...
    0x0020:  5014 0000 52d1 0000                      P...R...

4.结论

172.17.0.1.40268 > 172.17.0.2.1231: Flags [S], cksum 0x5854 (incorrect -> 0xb1bb), seq 2065754012, win 64240, options [mss 1460,sackOK,TS val 3598870482 ecr 0,nop,wscale 7], length 0
172.17.0.2.1231 > 172.17.0.1.40268: Flags [R.], cksum 0x52d1 (correct), seq 0, ack 2065754013, win 0, length 0

1) 客户端发送 TCP 的 SYN 包
2) 服务端返回了 TCP 的 RST 包拒绝

三、防火墙设置端口 REJECT

1.服务端运行 tcpdump

root@fdc276a87cec:~# tcpdump -n -v -X

2.服务端开启端口

root@fdc276a87cec:~# nc -l 1232

3.服务端防火墙设置端口 REJECT

root@fdc276a87cec:~# iptables -A INPUT -p tcp --dport 1232 -j REJECT
# root@fdc276a87cec:~# iptables -A INPUT -p tcp --dport 1232 -j REJECT --reject-with icmp-port-unreachable
# root@fdc276a87cec:~# iptables -A INPUT -p tcp --dport 1232 -j REJECT --reject-with tcp-reset

4.客户端 TCP 连接

zxm@zxm-pc:~$ nc -v 172.17.0.2 1232
nc: connect to 172.17.0.2 port 1232 (tcp) failed: Connection refused

5.抓包结果

root@fdc276a87cec:~# tcpdump -n -v -X
tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
18:38:13.247974 IP (tos 0x0, ttl 64, id 3624, offset 0, flags [DF], proto TCP (6), length 60)
    172.17.0.1.41922 > 172.17.0.2.1232: Flags [S], cksum 0x5854 (incorrect -> 0x18bd), seq 1124919437, win 64240, options [mss 1460,sackOK,TS val 3600101738 ecr 0,nop,wscale 7], length 0
    0x0000:  4500 003c 0e28 4000 4006 d46e ac11 0001  E..<.(@.@..n....
    0x0010:  ac11 0002 a3c2 04d0 430c e88d 0000 0000  ........C.......
    0x0020:  a002 faf0 5854 0000 0204 05b4 0402 080a  ....XT..........
    0x0030:  d695 316a 0000 0000 0103 0307            ..1j........
18:38:13.248037 IP (tos 0xc0, ttl 64, id 31814, offset 0, flags [none], proto ICMP (1), length 88)
    172.17.0.2 > 172.17.0.1: ICMP 172.17.0.2 tcp port 1232 unreachable, length 68
    IP (tos 0x0, ttl 64, id 3624, offset 0, flags [DF], proto TCP (6), length 60)
    172.17.0.1.41922 > 172.17.0.2.1232: Flags [S], cksum 0x5854 (incorrect -> 0x18bd), seq 1124919437, win 64240, options [mss 1460,sackOK,TS val 3600101738 ecr 0,nop,wscale 7], length 0
    0x0000:  45c0 0058 7c46 0000 4001 a579 ac11 0002  E..X|F..@..y....
    0x0010:  ac11 0001 0303 15ba 0000 0000 4500 003c  ............E..<
    0x0020:  0e28 4000 4006 d46e ac11 0001 ac11 0002  .(@.@..n........
    0x0030:  a3c2 04d0 430c e88d 0000 0000 a002 faf0  ....C...........
    0x0040:  5854 0000 0204 05b4 0402 080a d695 316a  XT............1j
    0x0050:  0000 0000 0103 0307                      ........

6.结论

172.17.0.1.41922 > 172.17.0.2.1232: Flags [S], cksum 0x5854 (incorrect -> 0x18bd), seq 1124919437, win 64240, options [mss 1460,sackOK,TS val 3600101738 ecr 0,nop,wscale 7], length 0
172.17.0.2 > 172.17.0.1: ICMP 172.17.0.2 tcp port 1232 unreachable, length 68

总共抓到的数据包数量:2 个。

虽然 ICMP 包中“嵌入”了原始 TCP 包的头部信息(看起来像又一个 TCP 包),但它不是独立的数据包,而是 ICMP 错误消息的载荷部分,用于说明错误的来源。

1) 客户端发送 TCP 的 SYN 包
2) 服务端返回的 ICMP 端口不可达消息 (如果是 iptables --reject-with tcp-reset,则服务端返回了 TCP 的 RST 包拒绝)

四、防火墙设置端口 DROP

1.服务端运行 tcpdump

root@fdc276a87cec:~# tcpdump -n -v -X

2.服务端开启端口

root@fdc276a87cec:~# nc -l 1233

3.服务端防火墙设置端口 DROP

root@fdc276a87cec:~# iptables -A INPUT -p tcp --dport 1233 -j DROP

4.客户端 TCP 连接

客户端一直处于阻塞状态

zxm@zxm-pc:~$ nc -v 172.17.0.2 1233

5.抓包结果

root@fdc276a87cec:~# tcpdump -n -v -X
tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
18:53:53.442707 IP (tos 0x0, ttl 64, id 22858, offset 0, flags [DF], proto TCP (6), length 60)
    172.17.0.1.36330 > 172.17.0.2.1233: Flags [S], cksum 0x5854 (incorrect -> 0xf6a9), seq 4072282136, win 64240, options [mss 1460,sackOK,TS val 3601041933 ecr 0,nop,wscale 7], length 0
    0x0000:  4500 003c 594a 4000 4006 894c ac11 0001  E..<YJ@.@..L....
    0x0010:  ac11 0002 8dea 04d1 f2ba 1818 0000 0000  ................
    0x0020:  a002 faf0 5854 0000 0204 05b4 0402 080a  ....XT..........
    0x0030:  d6a3 8a0d 0000 0000 0103 0307            ............
18:53:54.443135 IP (tos 0x0, ttl 64, id 22859, offset 0, flags [DF], proto TCP (6), length 60)
    172.17.0.1.36330 > 172.17.0.2.1233: Flags [S], cksum 0x5854 (incorrect -> 0xf2c0), seq 4072282136, win 64240, options [mss 1460,sackOK,TS val 3601042934 ecr 0,nop,wscale 7], length 0
    0x0000:  4500 003c 594b 4000 4006 894b ac11 0001  E..<YK@.@..K....
    0x0010:  ac11 0002 8dea 04d1 f2ba 1818 0000 0000  ................
    0x0020:  a002 faf0 5854 0000 0204 05b4 0402 080a  ....XT..........
    0x0030:  d6a3 8df6 0000 0000 0103 0307            ............
18:53:55.467119 IP (tos 0x0, ttl 64, id 22860, offset 0, flags [DF], proto TCP (6), length 60)
    172.17.0.1.36330 > 172.17.0.2.1233: Flags [S], cksum 0x5854 (incorrect -> 0xeec0), seq 4072282136, win 64240, options [mss 1460,sackOK,TS val 3601043958 ecr 0,nop,wscale 7], length 0
    0x0000:  4500 003c 594c 4000 4006 894a ac11 0001  E..<YL@.@..J....
    0x0010:  ac11 0002 8dea 04d1 f2ba 1818 0000 0000  ................
    0x0020:  a002 faf0 5854 0000 0204 05b4 0402 080a  ....XT..........
    0x0030:  d6a3 91f6 0000 0000 0103 0307            ............

6.结论

172.17.0.1.36330 > 172.17.0.2.1233: Flags [S], cksum 0x5854 (incorrect -> 0xf6a9), seq 4072282136, win 64240, options [mss 1460,sackOK,TS val 3601041933 ecr 0,nop,wscale 7], length 0
172.17.0.1.36330 > 172.17.0.2.1233: Flags [S], cksum 0x5854 (incorrect -> 0xf2c0), seq 4072282136, win 64240, options [mss 1460,sackOK,TS val 3601042934 ecr 0,nop,wscale 7], length 0
172.17.0.1.36330 > 172.17.0.2.1233: Flags [S], cksum 0x5854 (incorrect -> 0xeec0), seq 4072282136, win 64240, options [mss 1460,sackOK,TS val 3601043958 ecr 0,nop,wscale 7], length 0

1) 客户端发送 TCP 的 SYN 包
2) 客户端重发送 TCP 的 SYN 包
3) 客户端重发送 TCP 的 SYN 包