Search code examples
pythonsocketstcp

python socket setsockopt with raw socket


Just send tcp_syn with python socket raw. I follow the code here, but when I run the code I got below error:

Traceback (most recent call last):
  File "findPort.py", line 122, in <module>
    s.sendto(packet, (dest_ip , 27015 ))    # put this in a loop if you want to flood the target
socket.error: [Errno 22] Invalid argument

However, when I delete setsockopt:

#s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)

the code run smoothly. Unfortunately, as you can image, the wireshark sniff two tcp header data. one is the kernel add the other is myself add.

So the question is :

  1. what does the error mean as I show above?
  2. why I delete setsockopt the error disappear?
  3. how can I send tcp_syn with python 2.7?

thanks so much in advance~~~~~~


Solution

  • Let me answer my own question. The main reason why python can not send raw socket because Mac/Windows not support customize TCP/UDP header. If you want to write your own TCP/UDP header and send it to net, you need to use LINUX operation system.