1

I'm currently having a lot of trouble while writing a raw socket. While my implementation is pretty big by now, i think solving the problem for the following code, would also solve it for my whole implementation:

from scapy.all import *
import socket


sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)
src_port = 12345 # any random port > 1024 i guess ...
dst_port = 80 # should be tcp, as http runs over it in the end
dst_addr = socket.gethostbyname("www.google.de") 

# ensuring that kernel will not create its own ip header
sock.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, True)

# create a packet to send using scapy
packet = IP(dst = dst_addr)/TCP(dport = dst_port)

sock.sendto(bytes(packet), (dst_addr, 80))

I get the following error :

OS error: [Errno 22] Invalid argument

which is raised by:

sock.sendto(bytes(packet), (dst_addr, 80))

Does someone have a clue, why this is not working?

-- EDIT --

I'm working on MacOSX. I already noticed this question: Raw sockets and sendto in python However, I'm hoping that someone has an answer anyways, as the old question lacks a good answer and is also around 4 years old.

Community
  • 1
  • 1
Lars Prehn
  • 19
  • 9
  • Have you tried using `sock.send(...)` instead? Or `socket.IPPROTO_IP` as the last argument to the `socket.socket` call? Also this is basically a straight duplicate [this old question](http://stackoverflow.com/questions/11004249/raw-sockets-and-sendto-in-python) where it is said that it's a major hassle on macOS. Are you trying this on macOS? – Some programmer dude Mar 07 '17 at 15:28
  • @Someprogrammerdude using sock.send() requires an earlier connect() call. However, I tried this with the same result. Using IPPROTO_IP with any of the before mentioned sending possibilities does also not change anything. I already read the old question, but as it lacks a real answer, I hoped to ask again might help me solve my problem, due to the huge amount of time since the old question. And yep, I'm trying on MacOS. Thanks for your reply anyways :) – Lars Prehn Mar 07 '17 at 16:29
  • Possible duplicate of [Raw sockets and sendto in python](http://stackoverflow.com/questions/11004249/raw-sockets-and-sendto-in-python) – President James K. Polk Mar 07 '17 at 16:32
  • @JamesKPolk check my answer to Someprogrammerdude, as well as my edit on the topic. – Lars Prehn Mar 07 '17 at 16:33

0 Answers0