Search code examples
linuxsocketstcppacket

Send TCP SYN packet with payload


Is it possible to send a SYN packet with self-defined payload when initiating TCP connections? My gut feeling is that it is doable theoretically. I'm looking for a easy way to achieve this goal in Linux (with C or perhaps Go language) but because it is not a standard behavior, I didn't find helpful information yet. (This post is quite similar while it is not very helpful.)

Please help me, thanks!

EDIT: Sorry for the ambiguity. Not only the possibility for such task, I'm also looking for a way, or even sample codes to achieve it.


Solution

  • Obviously if you write your own software on both sides, it is possible to make it work however you want. But if you are relying on standard software on either end (such as, for example, a standard linux or Windows kernel), then no, it isn't possible, because according to TCP, you cannot send data until the session is established, and the session isn't established until you get an acknowledgment to your SYN from the other peer.

    So, for example, if you send a SYN packet that also includes additional payload to a linux kernel (caveat: this is speculation to some extent since I haven't actually tried it), it will simply ignore the payload and proceed to acknowledge (SYN/ACK) or reject (with RST) the SYN depending on whether there's a listener.

    In any case, you could try this, but since you're going "off the reservation" so to speak, you would need to craft your own raw packets; you won't be able to convince your local OS to create them for you.

    The python scapy package could construct it:

    #!/usr/bin/env python2
    from scapy.all import *
    sport = 3377
    dport = 2222
    src = "192.168.40.2"
    dst = "192.168.40.135"
    ether = Ether(type=0x800, dst="00:0c:29:60:57:04", src="00:0c:29:78:b0:ff")
    ip = IP(src=src, dst=dst)
    SYN = TCP(sport=sport, dport=dport, flags='S', seq=1000)
    xsyn = ether / ip / SYN / "Some Data"
    packet = xsyn.build()
    print(repr(packet))