1

According to python documentation we can build simple sniffer like :

import socket
# the public network interface
HOST = socket.gethostbyname(socket.gethostname())
# create a raw socket and bind it to the public interface
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)
s.bind((HOST, 0))
# Include IP headers
s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
# receive all packages
s.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)
# receive a package
print s.recvfrom(65565)
# disabled promiscuous mode
s.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)

for the windows platform but in linux socket.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF) doesn't work.

How that example will looks for linux platform ? How I could set promiscuous mode in Linux?

EDIT

I've got a message :

Traceback (most recent call last):
  File "b.py", line 46, in <module>
    sniffer(count=10,showPort=True,showRawData=True)
  File "b.py", line 12, in sniffer
    s.bind((HOST, 0))
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 19] No such device

When I did a code of @Christian-James-Bell with some changes :

import socket

def sniffer(count, bufferSize=65565, showPort=False, showRawData=False):
    # the public network interface
    HOST = socket.gethostbyname(socket.gethostname())

    # create a raw socket and bind it to the public interface
    s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.IPPROTO_IP)

    # prevent socket from being left in TIME_WAIT state, enabling reuse
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.bind((HOST, 0))

    # Include IP headers
    s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)

    # receive all packages
    s.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)

    for i in range(count):

        # receive a package
        package = s.recvfrom(bufferSize)
        printPacket(package, showPort, showRawData)

    # disabled promiscuous mode
    s.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)

def printPacket(package, showPort, showRawData):

    # index values for (data, header) tuple
    dataIndex = 0
    headerIndex = 1

    # index values for (ipAddress, port) tuple
    ipAddressIndex = 0
    portIndex = 1

    print('IP:', package[headerIndex][ipAddressIndex])
    if(showPort):
        print('Port:', package[headerIndex][portIndex])           
        print ('') #newline
    if(showRawData):
        print ('Data:', package[dataIndex])

sniffer(count=10,showPort=True,showRawData=True)

Anybody have any idea whats wrong ?

przelacz
  • 33
  • 2
  • 7
  • About your update: looks like OS doesn't let you access. I think you should check if you are in admin mode first: check if os.geteuid() == True. Take a look here: https://stackoverflow.com/questions/14950378/what-is-difference-between-os-getuid-and-os-geteuid – Rustam A. Jan 14 '22 at 16:51

1 Answers1

-2
 import socket

def sniffer(count, bufferSize=65565, showPort=False, showRawData=False):
    # the public network interface
    HOST = socket.gethostbyname(socket.gethostname())

    # create a raw socket and bind it to the public interface
    s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)

    # prevent socket from being left in TIME_WAIT state, enabling reuse
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.bind((HOST, 0))

    # Include IP headers
    s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)

    # receive all packages
    s.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)

    for i in range(count):

        # receive a package
        package = s.recvfrom(bufferSize)
        printPacket(package, showPort, showRawData)

    # disabled promiscuous mode
    s.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)

 def printPacket(package, showPort, showRawData):

    # index values for (data, header) tuple
    dataIndex = 0
    headerIndex = 1

    # index values for (ipAddress, port) tuple
    ipAddressIndex = 0
    portIndex = 1

    print('IP:', package[headerIndex][ipAddressIndex], end=' ')
    if(showPort):
    print('Port:', package[headerIndex][portIndex], end=' ')            
    print('') #newline
    if(showRawData):
        print('Data:', package[dataIndex])

 sniffer(count=10,showPort=True,showRawData=True)    
  • 1
    "in linux `socket.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)` doesn't work" but you still included it in your answer. – Kara Dec 03 '15 at 01:15