简体   繁体   English

如何通过Scapy发送FIN数据包关闭连接?

[英]How can I close a connection via scapy sending a FIN packet?

I need to send a FIN packet to close a connection having IP and ports information (plus other informations from previus packets). 我需要发送FIN数据包以关闭具有IP和端口信息(以及之前数据包中的其他信息)的连接。

I've seen that is possbile to craft a new connection through a 3-way-handshake (see 3 way handshake in Scapy ), but nothing on connection closure. 我已经看到可以通过三向握手(请参阅Scapy中的三向握手 )来建立新的连接,但是在关闭连接时什么也没有。

As stated by the RFC you need to send a FIN segment then wait for the endpoint's acknowledgment (ACK) + FIN segment and then send a last ACK segment for it. RFC所述,您需要发送FIN段,然后等待端点的确认(ACK)+ FIN段,然后为其发送最后一个ACK段。

Here is a simple example using Scapy: 这是一个使用Scapy的简单示例:

from scapy.all import *

conf.L3socket=L3RawSocket

sport=10000
dport=45000
pkt=IP(src="1.2.3.4", dst="4.5.6.7")

SYN=pkt/TCP(sport=sport, dport=dport, flags="S")
SYNACK=sr1(SYN)
ACK=pkt/TCP(sport=sport, dport=dport, flags="A", seq=SYNACK.ack, ack=SYNACK.seq + 1)
send(ACK)

# ...

FIN=pkt/TCP(sport=sport, dport=dport, flags="FA", seq=SYNACK.ack, ack=SYNACK.seq + 1)
FINACK=sr1(FIN)
LASTACK=pkt/TCP(sport=sport, dport=dport, flags="A", seq=FINACK.ack, ack=FINACK.seq + 1)
send(LASTACK)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM