34

In order to understand how TCP works, I tried to forge my own TCP SYN/SYN-ACK/ACK (based on the tutorial: http://www.thice.nl/creating-ack-get-packets-with-scapy/ ).

The problem is that whenever my computer recieve the SYN-ACK from the server, it generates a RST packet that stops the connection process.

I tried on a OS X Lion and on a Ubuntu 10.10 Maverick Meerkat, both reset the connection. I found this: http://lkml.indiana.edu/hypermail/linux/net/0404.2/0021.html, I don't know if it is the reason.

Does anyone could tell me what could be the reason? And how to avoid this problem?

Thank you.

2
  • I think this code snippet make this problem more apparently:ans = scapy.all.sr1(generate_tcp_syn_pkt()); ack_pkt = generate_tcp_ack_pkt(ans); scapy.all.send(ack_pkt)
    – diabloneo
    Commented Sep 26, 2013 at 17:12
  • How did you solve this issue for OS X? Commented Nov 8, 2015 at 11:10

4 Answers 4

35

The article you cited makes this pretty clear...

Since you are not completing the full TCP handshake your operating system might try to take control and can start sending RST (reset) packets, to avoid this we can use iptables:

iptables -A OUTPUT -p tcp --tcp-flags RST RST -s 192.168.1.20 -j DROP

Essentially, the problem is that scapy runs in user space, and the linux kernel will receive the SYN-ACK first. The kernel will send a RST because it won't have a socket open on the port number in question, before you have a chance to do anything with scapy.

The solution (as the blog mentions) is to firewall your kernel from sending a RST packet.

1
  • 6
    Is there a solution without IPTables? I really can't change the iptables config on my machine. Besides that, I'd like to send out RST's from my implementation also (creating own TCP flow for testing purposes)
    – KillianDS
    Commented Dec 10, 2012 at 11:21
6

I don't have a non-iptables answer, but one can fix the reset issue. Instead of trying to filter the outgoing reset in the filter table, filter all of the incoming packets from the target in the raw table instead. This prevents the return packets from the target from even being processed by the kernel, though scapy still sees them. I used the following syntax:

iptables -t raw -A PREROUTING -p tcp --dport <source port I use for scapy traffic> -j DROP

This solution does force me to use the same source port for my traffic; feel free to use your own iptables-fu to identify your target's return packets.

6

The blog article cited in other answers is not entirely correct. It's not only that you aren't completing the three way handshake, it's that the kernel's IP stack has no idea that there's a connection happening. When it receives the SYN-ACK, it sends a RST-ACK because it's unexpected. Receiving first or last really doesn't enter into it. The stack receiving the SYN-ACK is the issue.

Using IPTables to drop outbound RST packets is a common and valid approach, but sometimes you need to send a RST from Scapy. A more involved but very workable approach is to go lower, generating and responding to ARP with a MAC that is different from the host's. This allows you to have the ability to send and receive anything without any interference from the host.

Clearly this is more effort. Personally, I only take this approach (as opposed to the RST dropping approach) when I actually need to send a RST myself.

3

I found a solution without IPTables in https://widu.tumblr.com/post/43624355124/suppressing-tcp-rst-on-raw-sockets .

To bypass this problem, simply create a standard TCP socket as a server socket and bind to the requested port. Don’t do accept(). Just socket(), bind() on the port and listen(). This relaxes the kernel and let you do the 3-way handshake.

2
  • Not worked in my case. iptables does.
    – Maxim
    Commented Oct 26, 2023 at 16:06
  • What is written in the answer does not make much sense. The socket I want to send the packets to (server socket) is already created in my case and I would suspect for most people. Still RST is sent. Commented Oct 27, 2023 at 14:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.