前面聊到四向揮手的過程,接下來是demo的部分,和三向交握一樣,需要nc指令在HostA啟動TCP的servernc -l 172.17.0.5 80
在HostB的部分,一樣是如下
iptables -t filter -I OUTPUT -p tcp --sport 20 --tcp-flags RST RST -j DROP
python3 tcp.py
iptables負責把系統自動發出的RST flag封包給擋掉
用nc啟動的伺服器應該只有三向而已,因為從client關閉連線的時候,server沒有資料要傳給client,所以會直接發出FIN
下面的log是client關閉的時候,server還有數據要傳給client的情況,可以看見在client發出fin之後,server發回了ACK和PSH
2020-09-18 03:36:07 PM INFO Source Mac: 02:42:ac:11:00:02 Destination Mac: 02:42:ac:11:00:05 Protocol: IPv4 Version: 4 Type of Service: 0 Packet ID: 123 TTL: 64 Protocol: TCP Source IP: 172.17.0.2 Destination IP: 172.17.0.5
{'SRC_PORT': 20, 'DEST_PORT': 79, 'SEQ': 1, 'ACK_NUM': 1757834165, 'Flags': '_,_,_,ACK,_,_,_,FIN', 'RESERVE': 0, 'TCP_LENGTH': 5, 'WINDOW': 29200, 'CHECKSUM': 4282, 'PTR': 0, 'data': b''}
------------------------
2020-09-18 03:36:07 PM INFO Source Mac: 02:42:ac:11:00:05 Destination Mac: 02:42:ac:11:00:02 Protocol: IPv4 Version: 4 Type of Service: 0 Packet ID: 39355 TTL: 64 Protocol: TCP Source IP: 172.17.0.5 Destination IP: 172.17.0.2
{'SRC_PORT': 79, 'DEST_PORT': 20, 'SEQ': 1757834165, 'ACK_NUM': 2, 'Flags': '_,_,_,ACK,PSH,_,_,_', 'RESERVE': 0, 'TCP_LENGTH': 5, 'WINDOW': 29200, 'CHECKSUM': 22600, 'PTR': 0, 'data': b''}
------------------------
2020-09-18 03:36:07 PM INFO Source Mac: 02:42:ac:11:00:05 Destination Mac: 02:42:ac:11:00:02 Protocol: IPv4 Version: 4 Type of Service: 0 Packet ID: 39356 TTL: 64 Protocol: TCP Source IP: 172.17.0.5 Destination IP: 172.17.0.2
{'SRC_PORT': 79, 'DEST_PORT': 20, 'SEQ': 1757834169, 'ACK_NUM': 2, 'Flags': '_,_,_,ACK,_,_,_,FIN', 'RESERVE': 0, 'TCP_LENGTH': 5, 'WINDOW': 29200, 'CHECKSUM': 22596, 'PTR': 0, 'data': b''}
------------------------
2020-09-18 03:36:07 PM INFO Source Mac: 02:42:ac:11:00:02 Destination Mac: 02:42:ac:11:00:05 Protocol: IPv4 Version: 4 Type of Service: 0 Packet ID: 123 TTL: 64 Protocol: TCP Source IP: 172.17.0.2 Destination IP: 172.17.0.5
{'SRC_PORT': 20, 'DEST_PORT': 79, 'SEQ': 2, 'ACK_NUM': 1757834170, 'Flags': '_,_,_,ACK,_,_,_,_', 'RESERVE': 0, 'TCP_LENGTH': 5, 'WINDOW': 29200, 'CHECKSUM': 4277, 'PTR': 0, 'data': b''}