Skip to content
Open
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
27 changes: 26 additions & 1 deletion scapy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1749,7 +1749,7 @@ def _read_block_shb(self):
raise Scapy_Exception("PcapNg: Invalid Section Header Block "
" options (too short)")
self._read_block_tail(blocklen)
self._read_options(options)
self.shb_options = self._read_options(options)

def _read_packet(self, size=MTU): # type: ignore
# type: (int) -> Tuple[bytes, RawPcapNgReader.PacketMetadata]
Expand Down Expand Up @@ -2403,6 +2403,9 @@ class RawPcapNgWriter(GenericRawPcapWriter):

def __init__(self,
filename, # type: str
shb_hardware=None, # type: Optional[str]
shb_os=None, # type: Optional[str]
shb_userapp=None, # type: Optional[str]
):
# type: (...) -> None

Expand All @@ -2416,6 +2419,10 @@ def __init__(self,
self.endian = "<"
self.endian_magic = b"\x4d\x3c\x2b\x1a"

self.shb_hardware = shb_hardware
self.shb_os = shb_os
self.shb_userapp = shb_userapp

self.filename = filename
self.f = open(filename, "wb", 4096)

Expand Down Expand Up @@ -2483,6 +2490,24 @@ def _write_block_shb(self):
# Section Length
block_shb += struct.pack(self.endian + "q", -1)

# Add Hardware Name Option (2), if exists
if self.shb_hardware is not None:
block_shb += struct.pack(self.endian + "HH", 0x0002, len(self.shb_hardware))
block_shb += self.shb_hardware.encode()
block_shb = self._add_padding(block_shb)

# Add OS Name Option (3), if exists
if self.shb_os is not None:
block_shb += struct.pack(self.endian + "HH", 0x0003, len(self.shb_os))
block_shb += self.shb_os.encode()
block_shb = self._add_padding(block_shb)

# Add User Application Name Option (4), if exists
if self.shb_userapp is not None:
block_shb += struct.pack(self.endian + "HH", 0x0004, len(self.shb_userapp))
block_shb += self.shb_userapp.encode()
block_shb = self._add_padding(block_shb)

self.f.write(self.build_block(block_type, block_shb))

def _write_block_idb(self,
Expand Down
13 changes: 13 additions & 0 deletions test/regression.uts
Original file line number Diff line number Diff line change
Expand Up @@ -2158,6 +2158,19 @@ l = rdpcap(tmpfile)
assert b"Scapy" in l[0][Raw].load
assert l[0].time == ts

= Write and read back a pcapng with SHB options

tmpfile = get_temp_file(autoext=".pcapng")
r = RawPcapNgWriter(tmpfile, shb_hardware="hardware", shb_os="os", shb_userapp="userapp")
r._write_block_shb()
r.f.close()

reader = RawPcapNgReader(tmpfile)
assert reader.shb_options.get(2) == b"hardware"
assert reader.shb_options.get(3) == b"os"
assert reader.shb_options.get(4) == b"userapp"
reader.close()

= Check wrpcapng()

tmpfile = get_temp_file(autoext=".pcapng")
Expand Down