Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scapy sniff in non blocking way

In the blocking way I can do this:

from scapy.all import *

sniff(filter"tcp and port 80", count=10, prn = labmda x:x.summary())
# Below code will be executed only after 10 packets have been received
do_stuff()
do_stuff2()
do_stuff3()

I want to be able to sniff packets with scapy in a non blocking way, something like this:

def packet_recevied_event(p):
   print "Packet received event!"
   print p.summary()

# The "event_handler" parameter is my wishful thinking
sniff(filter"tcp and port 80", count=10, prn=labmda x:x.summary(), 
                                  event_handler=packet_received_event)

#I want this to be executed immediately
do_stuff()
do_stuff2()
do_stuff3()

To sum-up: My question is pretty clear, I want to be able to continue executing code without the sniff function blocking it. One option is to open a separate thread for this, but I would like to avoid it and use scapy native tools if possible.

Environment details:

python: 2.7

scapy: 2.1.0

os: ubuntu 12.04 64bit

like image 719
Michael Avatar asked Nov 12 '13 12:11

Michael


1 Answers

This functionality was added in https://github.com/secdev/scapy/pull/1999. I'll be available with Scapy 2.4.3+ (or the github branch). Have a look at the doc over: https://scapy.readthedocs.io/en/latest/usage.html#asynchronous-sniffing

>>> t = AsyncSniffer(prn=lambda x: x.summary(), store=False, filter="tcp")
>>> t.start()
>>> time.sleep(20)
>>> t.stop()
like image 106
Cukic0d Avatar answered Sep 18 '22 14:09

Cukic0d