开发者

Sending a reset in TCP/IP Socket connection

开发者 https://www.devze.com 2023-03-14 11:56 出处:网络
I am using python’s socket.py to create a connection to an ftp-server. Now I want to reset the connection (send a RST Flag开发者_JAVA技巧) and listen to the response of the ftp-server. (FYI using soc

I am using python’s socket.py to create a connection to an ftp-server. Now I want to reset the connection (send a RST Flag开发者_JAVA技巧) and listen to the response of the ftp-server. (FYI using socket.send('','R') does not work as the OS sends FIN flag instead of RST.)


Turn the SO_LINGER socket option on and set the linger time to 0 seconds. This will cause TCP to abort the connection when it is closed, flush the data and send a RST. See section 7.5 and example 15.21 in UNP.

In python:

def client(host, port):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
    s.connect((host, port))
    l_onoff = 1                                                                                                                                                           
    l_linger = 0                                                                                                                                                          
    s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER,                                                                                                                     
                 struct.pack('ii', l_onoff, l_linger))
    # send data here
    s.close()


If you want to implement your own behavior over connections I think you should try using Scapy. It is a really useful library/tool. It lets you play with IP/TCP/UDP/ICMP packages.


To send an RST on a TCP connection, set the SO_LINGER option to true with a zero timeout, then close the socket. This resets the connection. I have no idea how to do that in Python, or indeed whether you can even do it.

0

精彩评论

暂无评论...
验证码 换一张
取 消