11

I am trying to open a raw socket with Python under linux.

My simple code:

s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)
s.bind((HOST, 5454))

And I got this error:

[ERROR] Protocol not supported

By the way, I am using python 2.7.3 under linux 12.04, and I used root to run the code.

Does anyone have a clue?

Update: The solution given by dstromberg is correct. If you want the whole packet, then use his solution. However, there is another combination:

s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)

that also works.

In this case, you will receive a whole TCP packet with IP and TCP headers on it. If your use dstromberg's solution, you will also see the ethernet header. So it depends on how 'raw' you want your packet to be.

Dev Null
  • 4,731
  • 1
  • 30
  • 46
Jerry Meng
  • 1,466
  • 3
  • 21
  • 40
  • FYI I get the same error when running it as root. – pts Nov 01 '13 at 17:39
  • Try `AF_UNIX`, instead of `AF_INET`. – Anthony Nov 01 '13 at 17:58
  • @Anthony, It seems work. I got new error, though. Let me do some test and see whether it works. Thanks for advice~~~ – Jerry Meng Nov 01 '13 at 18:03
  • Are you trying to receive all IP packets? All packets? Or all packets of a specific IP protocol? – Robᵩ Nov 01 '13 at 18:08
  • @Robᵩ,All Packets. We have conversation in another post. And this is the way for me to bypass the TCP part. If I can receive all the packet then I can acheive the goal: one socket send, one socket receive. – Jerry Meng Nov 01 '13 at 18:10
  • One reason you may receive this error is that [Raw TCP packets are prohibited by Windows Desktop machines](https://stackoverflow.com/questions/62281097/why-am-i-getting-an-exception-on-windows-8-using-raw-sockets-with-ipproto-tcp). Apparently Windows 10 for the cloud may allow them... – Josiah Yoder Oct 28 '22 at 16:58

3 Answers3

10

Try socket.AF_PACKET instead of socket.AF_INET.

dstromberg
  • 6,954
  • 1
  • 26
  • 27
  • I tried, and I got "[ERROR] No such device". It confused me, actually. – Jerry Meng Nov 01 '13 at 17:46
  • 1
    @JerryMeng - What OS are you running on? On Ubuntu 12.04, with root privileges, `socket.socket(socket.AF_PACKET, socket.SOCK_RAW)` worked for me. – Robᵩ Nov 01 '13 at 18:12
  • @Robᵩ then how do you bind your socket to a host? like s.bind((HOST, 5454))? I got "[ERROR] No such device". And by the way, If I use AF_PACKET, why I don't need to specify the proto argument? – Jerry Meng Nov 01 '13 at 18:16
  • @JerryMeng 1) You don't bind raw sockets to hosts. You are bypassing IP, so you don't get any of its features. 2) I didn't specify a protocol because you said "All Packets". – Robᵩ Nov 01 '13 at 18:29
  • 2
    @JerryMeng - You can bind raw sockets to a specific device & protocol. So, you might try `s.bind(("eth0", 0x0800))`. – Robᵩ Nov 01 '13 at 18:33
  • @Robᵩ That is what I was typing...Yes, I think I still need to bind to something. – Jerry Meng Nov 01 '13 at 18:35
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/40392/discussion-between-jerry-meng-and-rob) – Jerry Meng Nov 01 '13 at 18:35
  • @Robᵩ I think I realize the point you were talking about. Let me do some test. thanks man~~~ – Jerry Meng Nov 01 '13 at 18:45
  • Why does this work? It's not even listed in [the py2 documentation](https://docs.python.org/2/library/socket.html). – Luc Jul 09 '17 at 18:57
0

This runs without error as root:

#!/usr/local/cpython-3.3/bin/python

import socket as socket_mod

#s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)
socket = socket_mod.socket(socket_mod.AF_PACKET, socket_mod.SOCK_RAW, socket_mod.IPPROTO_IP)
#socket.bind(('localhost', 5454))
socket.bind(('lo', 5454))
dstromberg
  • 6,954
  • 1
  • 26
  • 27
  • 1
    In fact, I have figured it out. And the second line is not right. 5454 won't give your error, neither any packet. 0x0800 is the correct port to use. Thanks for answering~~ – Jerry Meng Nov 01 '13 at 19:29
  • Why two answers? Wouldn't it be clearer to merge this into your first answer? – Josiah Yoder Oct 28 '22 at 17:00
-1

Try socket.AF_UNIX, it can solve your problem, good luck.

ZZB
  • 277
  • 4
  • 21