Skip to content
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
15 changes: 12 additions & 3 deletions scapy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1453,7 +1453,11 @@ def __init__(self, filename, fdesc=None, magic=None): # type: ignore
raise Scapy_Exception(
"Not a pcap capture file (bad magic: %r)" % magic
)
hdr = self.f.read(20)
try:
hdr = self.f.read(20)
except (OSError, OverflowError, zlib.error) as e:
warning(f"Pcap: {e}")
raise Scapy_Exception("Invalid pcap file (corrupted stream)")
if len(hdr) < 20:
raise Scapy_Exception("Invalid pcap file (too short)")
vermaj, vermin, tz, sig, snaplen, linktype = struct.unpack(
Expand Down Expand Up @@ -1697,7 +1701,8 @@ def __init__(self, filename, fdesc=None, magic=None): # type: ignore

try:
self._read_block_shb()
except EOFError:
except (EOFError, OSError, OverflowError, zlib.error) as e:
warning(f"PcapNg: {e}")
raise Scapy_Exception(
"The first SHB of the pcapng file is malformed !"
)
Expand Down Expand Up @@ -1801,7 +1806,11 @@ def _read_packet(self, size=MTU): # type: ignore

"""
while True:
res = self._read_block(size=size)
try:
res = self._read_block(size=size)
except (OSError, OverflowError, zlib.error) as e:
warning(f"PcapNg: {e}")
raise EOFError
if res is not None:
return res

Expand Down
24 changes: 24 additions & 0 deletions test/regression.uts
Original file line number Diff line number Diff line change
Expand Up @@ -2548,6 +2548,30 @@ with mock.patch("scapy.utils.warning") as warning:
for call in warning.call_args_list
) == 3

# Issue 568035799
import zlib

from scapy.error import Scapy_Exception

class CorruptedStream(BytesIO):
def read(self, *args, **kwargs):
if self.tell() >= self.getbuffer().nbytes:
raise zlib.error(
"Error -3 while decompressing data: "
"invalid distance too far back"
)
return BytesIO.read(self, *args, **kwargs)

with mock.patch("scapy.utils.warning") as warning:
tmpfile = get_temp_file(autoext=".pcapng")
writer = RawPcapNgWriter(tmpfile)
writer._write_block_shb()
writer.f.close()
with open(tmpfile, "rb") as fd:
capture = fd.read()
rdpcap(CorruptedStream(capture))
any("PcapNg" in call.args[0] for call in warning.call_args_list)

# Issue #69628
file = BytesIO(b"\xd4\xc3\xb2\xa1\x02\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\x00\x00\x01\x00\x00\x00\x04{\xdcf\xc2\xa5\x07\x008\x00\x00\x008\x00\x00\x00A]+\xdb]\x04\x8e(6\n\x99\xcb\x08\x00E\x00\x00*\x00\x01\x00\x00@\x06\xe3V\x07\x87\xa5m\x17\x15\xd3m\x01\x85\x01\x85\x00\x00\x00\x00\x00\x00\x00\x00P\x02 \x00\xc5_\x00\x000\x00")
l = rdpcap(file)
Expand Down
Loading