"[errno 1] Operation Not Permitted" When Creating Socket
Solution 1:
Since you wish to receive and parse ARP packets (which are on a link layer, OSI layer 2, below IP level you receive with AF_INET
), you'll have to use the low-level packet interface, AF_PACKET
.
From man packet
(for AF_PACKET
sockets):
The socket_type is either
SOCK_RAW
for raw packets including the link-level header orSOCK_DGRAM
for cooked packets with the link-level header removed. The link-level header information is available in a common format in asockaddr_ll
structure. protocol is the IEEE 802.3 protocol number in network byte order. See the<linux/if_ether.h>
include file for a list of allowed protocols. When protocol is set tohtons(ETH_P_ALL)
, then all protocols are received. All incoming packets of that protocol type will be passed to the packet socket before they are passed to the protocols implemented in the kernel.
So, for sniffing ARP packets, you must use SOCK_RAW
socket type. However, to use it, from man 7 raw
:
Only processes with an effective user ID of 0 or the CAP_NET_RAW capability are allowed to open raw sockets.
therefore, you'll have to run your program with sudo
.
For socket protocol (third parameter) you might choose 0x0003
as you already have, which means ETH_P_ALL
, receiving all packages, or probably better, ETH_P_ARP
which has a value of 0x0806
(see your /usr/include/linux/if_ether.h
) to receive only ARP packages.
All taken together, this looks like this:
rawSocket = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(0x0806))
whileTrue:
packet = rawSocket.recvfrom(2048)
# no need to filter-out ARP# less load on user program
Post a Comment for ""[errno 1] Operation Not Permitted" When Creating Socket"