Scapy sniff() function not working for no apparent reason

965 views Asked by At

I'm trying to use the sniff() function that scapy provides but it raises the following error:

Traceback (most recent call last):

  File "TestCode.py", line 54, in <module>
    packets = getMessege()

  File "TestCode.py", line 45, in getMessege
    return sniff(count=getLen(), lfilter=filterFrom)

  File "C:\Heights\PortableApps\PortablePython2.7.6.1\App\lib\site-packages\scapy\sendrecv.py", line 575, in sniff

    sel = select([s],[],[],remain)

select.error: (10038, 'An operation was attempted on something that is not a socket')

Here is the code (FromGlobal is a tuple that contains the IP and Port of the sender):

def getLen():
    while True:
        length, LenFrom = sock.recvfrom(1024)
        try:
            IntLen = int(length)
        except:
            pass
        else:
            if LenFrom == FromGlobal:
                return IntLen

def filterFrom(pck):
    try:
        return pck[IP].src == FromGlobal[0] and pck[UDP].sport == FromGlobal[1]
    except:
        return False

def getMessege():  # TODO This needs to return only the messege and port
    return sniff(count=getLen(), lfilter=filterFrom)

packets = getMessege()
print packets.show()

The weird part is that if I try to do it like so:

def func1():
    return int('1')

def lfilter(pack):
    return TCP in pack and pack[IP].src != '8.8.8.8'

def func():
    return sniff(count=func1(), lfilter=lfilter)

var = func()
print var.show()

it works perfectly well. If someone could point out the difference between the two it would help a lot.

I'm use WinPcap 4.1.3 and scapy 2.x.

1

There are 1 answers

0
Saviour On BEST ANSWER

Well, Resolved it myself. apparently if you do:

from scapy.all import *
from scapy.layers.inet import *

the sniff function won't works so do only

from scapy.all import *