Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly raise on bad filter #4241

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions scapy/arch/bpf/supersocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ def __init__(self,
try:
attach_filter(self.bpf_fd, filter, self.iface)
filter_attached = True
except ImportError as ex:
warning("Cannot set filter: %s" % ex)
except (ImportError, Scapy_Exception) as ex:
raise Scapy_Exception("Cannot set filter: %s" % ex)
if NETBSD and filter_attached is False:
# On NetBSD, a filter must be attached to an interface, otherwise
# no frame will be received by os.read(). When no filter has been
Expand Down
19 changes: 10 additions & 9 deletions scapy/arch/libpcap.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ def close(self):
pcap_datalink,
pcap_findalldevs,
pcap_freealldevs,
pcap_geterr,
pcap_if_t,
pcap_lib_version,
pcap_next_ex,
Expand Down Expand Up @@ -386,16 +387,16 @@ def fileno(self):
return cast(int, pcap_get_selectable_fd(self.pcap))

def setfilter(self, f):
# type: (str) -> bool
# type: (str) -> None
filter_exp = create_string_buffer(f.encode("utf8"))
if pcap_compile(self.pcap, byref(self.bpf_program), filter_exp, 1, -1) == -1: # noqa: E501
log_runtime.error("Could not compile filter expression %s", f)
return False
else:
if pcap_setfilter(self.pcap, byref(self.bpf_program)) == -1:
log_runtime.error("Could not set filter %s", f)
return False
return True
if pcap_compile(self.pcap, byref(self.bpf_program), filter_exp, 1, -1) >= 0: # noqa: E501
if pcap_setfilter(self.pcap, byref(self.bpf_program)) >= 0:
# Success
return
errstr = decode_locale_str(
bytearray(pcap_geterr(self.pcap)).strip(b"\x00")
)
raise Scapy_Exception("Cannot set filter: %s" % errstr)

def setnonblock(self, i):
# type: (bool) -> None
Expand Down
2 changes: 1 addition & 1 deletion scapy/arch/linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ def __init__(self,
try:
attach_filter(self.ins, filter, self.iface)
except (ImportError, Scapy_Exception) as ex:
log_runtime.error("Cannot set filter: %s", ex)
raise Scapy_Exception("Cannot set filter: %s" % ex)
if self.promisc:
set_promisc(self.ins, self.iface)
self.ins.bind((self.iface, type))
Expand Down
9 changes: 9 additions & 0 deletions test/regression.uts
Original file line number Diff line number Diff line change
Expand Up @@ -2261,6 +2261,15 @@ except Scapy_Exception:
else:
assert False

= Check online sniff() with a bad filter
~ needs_root tcpdump libpcap
try:
sniff(timeout=0, filter="bad filter")
except Scapy_Exception:
pass
else:
assert False

= Check offline sniff with lfilter
assert len(sniff(offline=[IP()/UDP(), IP()/TCP()], lfilter=lambda x: TCP in x)) == 1

Expand Down
Loading