How to prohibit the transmission of intercepted packets?

28 Views Asked by At

I use the socket module in python to intercept packets at the L2 level. How do I configure socket to not allow captured packets to enter the network. Linux operating system.

sniffer = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(3))
while True:
            raw_data = sniffer.recvfrom(65535)[0]
            ethernet = EthernetFrame(raw_data)
            # Ethernet packet handling

The received packet is still transmitted to the network

I tried to turn on promiscuous mode, but it had no effect

import ctypes

IFF_PROMISC = 0x100
SIOCGIFFLAGS = 0x8913
SIOCSIFFLAGS = 0x8914

class ifreq(ctypes.Structure):
    _fields_ = [("ifr_ifrn", ctypes.c_char * 16),
                ("ifr_flags", ctypes.c_short)]
...
ifr = ifreq()
    ifr.ifr_ifrn = b"eth0"
    fcntl.ioctl(sniffer.fileno(), SIOCGIFFLAGS, ifr)
    ifr.ifr_flags |= IFF_PROMISC
    fcntl.ioctl(sniffer.fileno(), SIOCSIFFLAGS, ifr)
0

There are 0 best solutions below