i m developing a program for packet sniffing in PYTHON but i am not getting what does the (0x0003)parameter means in 'ntohs' function in python..
s = socket.socket( socket.AF_PACKET , socket.SOCK_RAW , socket.ntohs(0x0003))
i m developing a program for packet sniffing in PYTHON but i am not getting what does the (0x0003)parameter means in 'ntohs' function in python..
s = socket.socket( socket.AF_PACKET , socket.SOCK_RAW , socket.ntohs(0x0003))
In linux socket.ntohs(0x0003)
tells capture everything including ethernet frames.
To capture TCP, UDP, or ICMP only, instead of socket.ntohs(0x0003)
you will write socket.IPPROTO_TCP
, socket.IPPROTO_UDP
and socket.IPPROTO_ICMP
respectively.
In windows, the socket can only capture IP and above and cannot capture ethernet. To do that you can use pypcap
library in the windows.
socket.ntohs(0x0003)
to capture UDP. socket.IPPROTO_UDP
isn't working for me. Linux. Python 3.6.8 –
Linnell It is the protocol to use. From documentation -
socket.socket([family[, type[, proto]]])
Create a new socket using the given address family, socket type and protocol number. The address family should be AF_INET (the default), AF_INET6 or AF_UNIX. The socket type should be SOCK_STREAM (the default), SOCK_DGRAM or perhaps one of the other SOCK_ constants. The protocol number is usually zero and may be omitted in that case.
If protocol is ommitted it defaults to 0 , which causes the protocol to be taken based on the family
(first argument).
For linux, You can find the list of protocol numbers in /etc/protocols
. I believe when you give protocol as 0x0003
it is using GGP (Gateway-Gateway protocol) .
The last parameter is the protocol
and is normally left as 0, which is documented here.
The IANA list the Assigned Internet Protocol Numbers, '0x0003` is defined as:
3 GGP Gateway-to-Gateway
The ntohs()
function though is used to do an endian swap on the number if required (based on the architecture you are running on). For your example this could be 0x0300
or 768
, which does not make sense to me. Perhaps someone else knows of a special use case for which I am not aware.
If you check linux/if_ether_h you will aww
#define ETH_P_ALL 0x0003 /* Every packet (be careful!!!) */
so 0x0003 means capture all packets
from the python official socket doc
socket.ntohs(0x0003)
captures all the send & receive traffic from the network interface.
© 2022 - 2024 — McMap. All rights reserved.