Skip to content

Commit 309dc52

Browse files
committed
Check added to PcapNg processing
1 parent ac3d5bb commit 309dc52

File tree

1 file changed

+39
-12
lines changed

1 file changed

+39
-12
lines changed

scapy/utils.py

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,9 +1603,14 @@ def _read_block(self, size=MTU):
16031603
warning("PcapNg: Error reading blocklen before block body")
16041604
raise EOFError
16051605
if blocklen < 12:
1606-
warning("Invalid block length !")
1606+
warning("PcapNg: Invalid block length !")
16071607
raise EOFError
1608-
block = self.f.read(blocklen - 12)
1608+
1609+
_block_body_length = blocklen - 12
1610+
block = self.f.read(_block_body_length)
1611+
if len(block) != _block_body_length:
1612+
raise Scapy_Exception("PcapNg: Invalid Block body length "
1613+
"(too short)")
16091614
self._read_block_tail(blocklen)
16101615
if blocktype in self.blocktypes:
16111616
return self.blocktypes[blocktype](block, size)
@@ -1635,25 +1640,42 @@ def _read_block_shb(self):
16351640
elif endian == b"\x4d\x3c\x2b\x1a":
16361641
self.endian = "<"
16371642
else:
1638-
warning("Bad magic in Section Header block (not a pcapng file?)")
1643+
warning("PcapNg: Bad magic in Section Header Block"
1644+
" (not a pcapng file?)")
16391645
raise EOFError
16401646

1641-
blocklen = struct.unpack(self.endian + "I", _blocklen)[0]
1647+
try:
1648+
blocklen = struct.unpack(self.endian + "I", _blocklen)[0]
1649+
except struct.error:
1650+
warning("PcapNg: Could not read blocklen")
1651+
raise EOFError
16421652
if blocklen < 28:
1643-
warning(f"Invalid SHB block length ({blocklen})!")
1653+
warning(f"PcapNg: Invalid Section Header Block length ({blocklen})!") # noaq: E501
16441654
raise EOFError
16451655

1656+
16461657
# Major version must be 1
16471658
_major = self.f.read(2)
1648-
major = struct.unpack(self.endian + "H", _major)[0]
1649-
if major != 1:
1650-
warning(f"SHB Major version {major} unsupported !")
1659+
try:
1660+
major = struct.unpack(self.endian + "H", _major)[0]
1661+
except struct.error:
1662+
warning("PcapNg: Could not read major value")
16511663
raise EOFError
1664+
if major != 1:
1665+
warning(f"PcapNg: SHB Major version {major} unsupported !")
1666+
raise EOFErro
16521667

16531668
# Skip minor version & section length
1654-
self.f.read(10)
1669+
skipped = self.f.read(10)
1670+
if len(skipped) != 10:
1671+
warning("PcapNg: Could not read minor value & section length")
1672+
raise EOFError
16551673

1656-
options = self.f.read(blocklen - 28)
1674+
_options_len = blocklen - 28
1675+
options = self.f.read(_options_len)
1676+
if len(options) != _options_len:
1677+
raise Scapy_Exception("PcapNg: Invalid Section Header Block "
1678+
" options (too short)")
16571679
self._read_block_tail(blocklen)
16581680
self._read_options(options)
16591681

@@ -1673,7 +1695,12 @@ def _read_options(self, options):
16731695
# type: (bytes) -> Dict[int, bytes]
16741696
opts = dict()
16751697
while len(options) >= 4:
1676-
code, length = struct.unpack(self.endian + "HH", options[:4])
1698+
try:
1699+
code, length = struct.unpack(self.endian + "HH", options[:4])
1700+
except struct.error:
1701+
warning("PcapNg: options header is too small "
1702+
"%d !" % len(options))
1703+
raise EOFError
16771704
if code != 0 and 4 + length < len(options):
16781705
opts[code] = options[4:4 + length]
16791706
if code == 0:
@@ -1910,7 +1937,7 @@ def read_packet(self, size=MTU, **kwargs):
19101937
p.comment = comment
19111938
p.direction = direction
19121939
if ifname is not None:
1913-
p.sniffed_on = ifname.decode('utf-8')
1940+
p.sniffed_on = ifname.decode('utf-8', 'backslashreplace')
19141941
return p
19151942

19161943
def recv(self, size: int = MTU, **kwargs: Any) -> 'Packet': # type: ignore

0 commit comments

Comments
 (0)