Sending a reset in TCP/IP Socket connection
Asked Answered
G

4

25

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) 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.)

Germaun answered 22/6, 2011 at 12:26 Comment(3)
Why do you want to reset the connection?Tronna
replicating a malicious ftpclientGermaun
Related: resetting a TCP connection from the server sideHudis
E
48

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 UNIX Network Programming (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()
Effloresce answered 22/6, 2011 at 13:7 Comment(3)
It will cause TCP to abort the connection when closed, throw away any pending data, and send an RST.Tronna
Just some more info on this for those curious: SO_LINGER with a time of 0 seconds means the TCP stack won't "linger around" on close(): it will discard any unsent data and send a RST instead of a FIN. For more info read this and this.Cuprous
Note that if you are using this code in the Microsoft Windows environment then you need to replace the 'ii' in the call to struct.pack() with 'hh'. Windows uses shorts in struct linger, where Linux uses ints.Confiteor
T
3

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.

Testify answered 22/6, 2011 at 12:36 Comment(0)
T
3

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.

Tronna answered 22/6, 2011 at 12:43 Comment(0)
O
0

Connect to a server, send data and then RST (instead of FIN): (I tested it on Windows)

import struct
from socket import socket, AF_INET, SOCK_STREAM
from _socket import SOL_SOCKET, SO_LINGER

ip="192.168.1.10" #change it to real IP address.
port=8084 #change it to real port number.
s = socket(AF_INET, SOCK_STREAM)
s.setsockopt(SOL_SOCKET, SO_LINGER, struct.pack('ii', 1, 0))
s.connect((ip, port))
#send data to server (optional)
s.close()
Oneself answered 11/12, 2023 at 12:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.