Filter options for sniff function in scapy
sniff()
uses Berkeley Packet Filter (BPF) syntax (the same one as tcpdump
), here are some examples:
Packets from or to host:
host x.x.x.x
Only TCP SYN segments:
tcp[tcpflags] & tcp-syn != 0
Everything ICMP but echo requests/replies:
icmp[icmptype] != icmp-echo and icmp[icmptype] != icmp-echoreply
Advantages of Scapy sniff's filter, versus filtering in packet handling method?
If you're using the filter=
keyword argument from sniff, you're passing a BPF filter. This string filter is compiled into a C object by libpcap, then passed to the socket. It is then used by the kernel directly, i.e. it is much, much more performant than filtering in the callback.
This actually matters a lot when you're on heavy-loads: if you receive for instance 1 Go/s of packets, Scapy can't dissect that fast enough, so the socket it is using to receive those packets will have its buffer filled, and tons of packets will be dropped. On the other hand, if you're using a BPF "kernel-level" filter, only the filtered packets reach Scapy: that is a much more manageable packet stream.
If you are not experiencing issues with packet drops though (low rates... etc), it comes down to preference.
Scapy multiple sniff filters not working?
The filter you are using is simply not a valid PCAP filter (hence the syntax error message). On a Unix system, you can have a look at the pcap-filter(7)
and the tcpdump(1)
manpages for more information about the syntax you can use.
Something like this should work:
myfilter="tcp[tcpflags] & (tcp-syn|tcp-ack|tcp-push) == tcp-syn"
sniff(iface="myinter" , filter=myfilter, prn=mitm , count=1)
Related Topics
Redirecting Python's Stdout to the File Fails with Unicodeencodeerror
How to Make the Python Program to Check Linux Services
Opening a Python Thread in a New Console Window
Installing Python 2.7 Without Root
Command 'X86_64-Linux-Gnu-Gcc' Failed with Exit Status 1
How to Control the Keyboard and Mouse with Python
Pip Is Not Working for Python 3.10 on Ubuntu
How to Get the Owner and Group of a Folder with Python on a Linux MAChine
Multiple Instances of Python Running Simultaneously Limited to 35
What Conditions Result in an Opened, Nonblocking Named Pipe (Fifo) Being "Unavailable" for Reads
Python Convert Microsoft Office Docs to Plain Text on Linux
Python: Interplay Between Lib/Site-Packages/Site.Py and Lib/Site.Py
Python Memory Debugging with Gdb
Python's Equivalent for R's Dput() Function
Faster Way to Find Large Files with Python
How to Get an Windows Executable from My Kivy App (Pyinstaller)
Loading a Config File from Operation System Independent Place in Python