Skip to content

Commit 3f5e7ca

Browse files
guedougpotter2
authored andcommitted
Check if tcpdump binary exists
1 parent f3e936c commit 3f5e7ca

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

scapy/sendrecv.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
import time
1818
import types
1919

20+
from scapy.arch.common import TCPDUMP
2021
from scapy.compat import plain_str
2122
from scapy.data import ETH_P_ALL
2223
from scapy.config import conf
23-
from scapy.error import warning
24+
from scapy.error import Scapy_Exception, warning
2425
from scapy.packet import Packet, Gen
2526
from scapy.utils import get_temp_file, tcpdump, wrpcap, \
2627
ContextManagerSubprocess, PcapReader
@@ -802,6 +803,11 @@ def sniff(count=0, store=True, offline=None, prn=None, lfilter=None,
802803
sniff_sockets[opened_socket] = "socket0"
803804
if offline is not None:
804805
flt = karg.get('filter')
806+
807+
if not TCPDUMP and flt is not None:
808+
message = "tcpdump is not available. Cannot use filter!"
809+
raise Scapy_Exception(message)
810+
805811
if isinstance(offline, list):
806812
sniff_sockets.update((PcapReader(
807813
fname if flt is None else

scapy/utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
base64_bytes, hex_bytes, lambda_tuple_converter, bytes_encode
3333
from scapy.error import log_runtime, Scapy_Exception, warning
3434
from scapy.pton_ntop import inet_pton
35+
from scapy.arch.common import TCPDUMP
3536

3637
###########
3738
# Tools #
@@ -1581,6 +1582,9 @@ def tcpdump(pktlist, dump=False, getfd=False, args=None,
15811582
prog = [prog]
15821583
else:
15831584
raise ValueError("prog must be a string")
1585+
if prog[0] == conf.prog.tcpdump and not TCPDUMP:
1586+
message = "tcpdump is not available. Cannot use tcpdump() !"
1587+
raise Scapy_Exception(message)
15841588

15851589
if linktype is not None:
15861590
# Tcpdump does not support integers in -y (yet)

test/regression.uts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6688,6 +6688,26 @@ fdesc.close()
66886688
assert list(pktpcap[TCP]) == list(pktpcap_tcp)
66896689
os.unlink(filename)
66906690

6691+
= Check offline sniff() without a tcpdump binary
6692+
~ tcpdump
6693+
import mock
6694+
6695+
conf_prog_tcpdump = conf.prog.tcpdump
6696+
conf.prog.tcpdump = "tcpdump_fake"
6697+
6698+
from scapy.arch.common import _check_tcpdump
6699+
6700+
@mock.patch("scapy.sendrecv.TCPDUMP", _check_tcpdump())
6701+
def _test_sniff_notcpdump():
6702+
try:
6703+
sniff(offline="fake.pcap", filter="tcp")
6704+
assert False
6705+
except Scapy_Exception:
6706+
assert True
6707+
6708+
_test_sniff_notcpdump()
6709+
conf.prog.tcpdump = conf_prog_tcpdump
6710+
66916711
= Check wrpcap(nano=True)
66926712
fdesc, filename = tempfile.mkstemp()
66936713
fdesc = os.fdopen(fdesc, "wb")
@@ -6774,6 +6794,7 @@ assert r.linktype == DLT_EN10MB
67746794

67756795
= Check tcpdump()
67766796
~ tcpdump
6797+
from io import BytesIO
67776798
* No very specific tests because we do not want to depend on tcpdump output
67786799
pcapfile = BytesIO(b'\xd4\xc3\xb2\xa1\x02\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\x00\x00e\x00\x00\x00\xcf\xc5\xacVo*\n\x00(\x00\x00\x00(\x00\x00\x00E\x00\x00(\x00\x01\x00\x00@\x06|\xcd\x7f\x00\x00\x01\x7f\x00\x00\x01\x00\x14\x00P\x00\x00\x00\x00\x00\x00\x00\x00P\x02 \x00\x91|\x00\x00\xcf\xc5\xacV_-\n\x00\x1c\x00\x00\x00\x1c\x00\x00\x00E\x00\x00\x1c\x00\x01\x00\x00@\x11|\xce\x7f\x00\x00\x01\x7f\x00\x00\x01\x005\x005\x00\x08\x01r\xcf\xc5\xacV\xf90\n\x00\x1c\x00\x00\x00\x1c\x00\x00\x00E\x00\x00\x1c\x00\x01\x00\x00@\x01|\xde\x7f\x00\x00\x01\x7f\x00\x00\x01\x08\x00\xf7\xff\x00\x00\x00\x00')
67796800
data = tcpdump(pcapfile, dump=True, args=['-nn']).split(b'\n')
@@ -6782,6 +6803,26 @@ assert b'IP 127.0.0.1.20 > 127.0.0.1.80:' in data[0]
67826803
assert b'IP 127.0.0.1.53 > 127.0.0.1.53:' in data[1]
67836804
assert b'IP 127.0.0.1 > 127.0.0.1:' in data[2]
67846805

6806+
* Non existing tcpdump binary
6807+
6808+
import mock
6809+
6810+
conf_prog_tcpdump = conf.prog.tcpdump
6811+
conf.prog.tcpdump = "tcpdump_fake"
6812+
6813+
from scapy.arch.common import _check_tcpdump
6814+
6815+
@mock.patch("scapy.utils.TCPDUMP", _check_tcpdump())
6816+
def _test_tcpdump_notcpdump():
6817+
try:
6818+
tcpdump(IP()/TCP())
6819+
assert False
6820+
except Scapy_Exception:
6821+
assert True
6822+
6823+
_test_tcpdump_notcpdump()
6824+
conf.prog.tcpdump = conf_prog_tcpdump
6825+
67856826
# Also check with use_tempfile=True (for non-OSX platforms)
67866827
pcapfile.seek(0) or None
67876828
tempfile_count = len(conf.temp_files)

0 commit comments

Comments
 (0)