Scapy: operation not allowed when sending packets

I am trying to learn a bit of packet generation using scapy. It looks pretty cool. Following some documentation, I do this:

l3=IP(dst="192.168.0.1", src="192.168.0.2", tos=(46 << 2)) 

But only to receive the error message:

 Traceback (most recent call last): File "<console>", line 1, in <module> File "/usr/lib/python2.7/dist-packages/scapy/sendrecv.py", line 251, in send __gen_send(conf.L3socket(*args, **kargs), x, inter=inter, loop=loop, count=count,verbose=verbose, realtime=realtime) File "/usr/lib/python2.7/dist-packages/scapy/arch/linux.py", line 307, in __init__ self.ins = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(type)) File "/usr/lib/python2.7/socket.py", line 187, in __init__ _sock = _realsocket(family, type, proto) error: [Errno 1] Operation not permitted 

Running scapy as root solved the problem. But this is not what I wanted. Is this because a regular user cannot create a RAW socket? If so, is there a solution?

+8
scapy
source share
2 answers

Scapy needs root privileges to create raw sockets because it uses the Python socket library. Source sockets are only allowed for use with "effective user ID 0 or CAP_NET_RAW capabilities" according to the Linux raw man pages.

I cannot find reliable documentation on setting the CAP_NET_RAW , but if you are looking for work with Scapy scripts in which user sockets without root are what you need to do.

+7
source share

To run Scapy only with the cap_net_raw privilege ...

The safest and less complicated way I know is in order:

  • Make a personal copy of the python binary:

    $ sudo cp / usr / bin / python2.7 ~ / python_netraw

  • Observe this:

    $ sudo chown your user name ~ / python_netraw

  • Do not let anyone else run it:

    $ chmod -x, u + x ~ / python_netraw

  • Give it the option cap_net_raw:

    $ sudo setcap cap_net_raw = eip / usr / bin / python_netraw

  • Run scapy with it:

    $ ~ / python_netraw -O / usr / bin / scapy

(Or use sudo every time you need to run Scapy with raw privileges.)

+6
source share

All Articles