Skip to content

Commit d184fc9

Browse files
committed
PCAPNG: Adding SHB Options
This patch allows to set Hardware, OS, and User Application, when creating a PcapNgWriter. These values will be written into the SHB.
1 parent ef72e1d commit d184fc9

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

scapy/utils.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2403,6 +2403,9 @@ class RawPcapNgWriter(GenericRawPcapWriter):
24032403

24042404
def __init__(self,
24052405
filename, # type: str
2406+
shb_hardware=None, # type: Optional[str]
2407+
shb_os=None, # type: Optional[str]
2408+
shb_userapp=None, # type: Optional[str]
24062409
):
24072410
# type: (...) -> None
24082411

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

2422+
self.shb_hardware = shb_hardware
2423+
self.shb_os = shb_os
2424+
self.shb_userapp = shb_userapp
2425+
24192426
self.filename = filename
24202427
self.f = open(filename, "wb", 4096)
24212428

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

2493+
# Add Hardware Name Option (2), if exists
2494+
if self.shb_hardware is not None:
2495+
block_shb += struct.pack(self.endian + "HH", 0x0002, len(self.shb_hardware))
2496+
block_shb += self.shb_hardware.encode()
2497+
block_shb = self._add_padding(block_shb)
2498+
2499+
# Add OS Name Option (3), if exists
2500+
if self.shb_os is not None:
2501+
block_shb += struct.pack(self.endian + "HH", 0x0003, len(self.shb_os))
2502+
block_shb += self.shb_os.encode()
2503+
block_shb = self._add_padding(block_shb)
2504+
2505+
# Add User Application Name Option (4), if exists
2506+
if self.shb_userapp is not None:
2507+
block_shb += struct.pack(self.endian + "HH", 0x0004, len(self.shb_userapp))
2508+
block_shb += self.shb_userapp.encode()
2509+
#block_shb = self._add_padding(block_shb)
2510+
24862511
self.f.write(self.build_block(block_type, block_shb))
24872512

24882513
def _write_block_idb(self,

0 commit comments

Comments
 (0)