@@ -1603,9 +1603,14 @@ def _read_block(self, size=MTU):
1603
1603
warning ("PcapNg: Error reading blocklen before block body" )
1604
1604
raise EOFError
1605
1605
if blocklen < 12 :
1606
- warning ("Invalid block length !" )
1606
+ warning ("PcapNg: Invalid block length !" )
1607
1607
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)" )
1609
1614
self ._read_block_tail (blocklen )
1610
1615
if blocktype in self .blocktypes :
1611
1616
return self .blocktypes [blocktype ](block , size )
@@ -1635,25 +1640,42 @@ def _read_block_shb(self):
1635
1640
elif endian == b"\x4d \x3c \x2b \x1a " :
1636
1641
self .endian = "<"
1637
1642
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?)" )
1639
1645
raise EOFError
1640
1646
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
1642
1652
if blocklen < 28 :
1643
- warning (f"Invalid SHB block length ({ blocklen } )!" )
1653
+ warning (f"PcapNg: Invalid Section Header Block length ({ blocklen } )!" ) # noaq: E501
1644
1654
raise EOFError
1645
1655
1656
+
1646
1657
# Major version must be 1
1647
1658
_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" )
1651
1663
raise EOFError
1664
+ if major != 1 :
1665
+ warning (f"PcapNg: SHB Major version { major } unsupported !" )
1666
+ raise EOFErro
1652
1667
1653
1668
# 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
1655
1673
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)" )
1657
1679
self ._read_block_tail (blocklen )
1658
1680
self ._read_options (options )
1659
1681
@@ -1673,7 +1695,12 @@ def _read_options(self, options):
1673
1695
# type: (bytes) -> Dict[int, bytes]
1674
1696
opts = dict ()
1675
1697
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
1677
1704
if code != 0 and 4 + length < len (options ):
1678
1705
opts [code ] = options [4 :4 + length ]
1679
1706
if code == 0 :
@@ -1910,7 +1937,7 @@ def read_packet(self, size=MTU, **kwargs):
1910
1937
p .comment = comment
1911
1938
p .direction = direction
1912
1939
if ifname is not None :
1913
- p .sniffed_on = ifname .decode ('utf-8' )
1940
+ p .sniffed_on = ifname .decode ('utf-8' , 'backslashreplace' )
1914
1941
return p
1915
1942
1916
1943
def recv (self , size : int = MTU , ** kwargs : Any ) -> 'Packet' : # type: ignore
0 commit comments