-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPacketSniffer.py
More file actions
40 lines (24 loc) · 784 Bytes
/
PacketSniffer.py
File metadata and controls
40 lines (24 loc) · 784 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import socket
import os
#host to listen on
host = "10.10.40.65"
port = 0
#create a raw socket and bind it to the public interface
if os.name == "nt":
socket_protocol = socket.IPPROTO_IP
else:
socket_protocol = socket.IPPROTO_ICMP
sniffer = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket_protocol)
sniffer.bind((host, port))
#include the IP headers in the capture
sniffer.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
#if using Windows, we need to send an IOCTL
#to set up promiscuous mode
if os.name == "nt":
sniffer.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)
#read the packets in a loop
while True:
print (sniffer.recvfrom(65565))
#if using windows, turn off premicuous mode
if os.name == "nt":
sniffer.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)