-
Notifications
You must be signed in to change notification settings - Fork 873
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Save As pcapng for ProtocolAnalyzer
- Loading branch information
Showing
6 changed files
with
172 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import os | ||
import struct | ||
import time | ||
import math | ||
|
||
from urh.util.Logger import logger | ||
|
||
from urh.signalprocessing.Message import Message | ||
|
||
# Refer to PCAPNG spec | ||
# https://www.ietf.org/staging/draft-tuexen-opsawg-pcapng-02.html | ||
|
||
class PCAPNG(object): | ||
def __init__(self): | ||
self.__messages = [] | ||
self.__messages_timestamps = [] | ||
self.__filename = "" | ||
|
||
def __build_pcapng_shb(self, shb_userappl : str = "", shb_hardware : str = "") -> bytes: | ||
BLOCKTYPE = 0x0A0D0D0A | ||
HEADERS_BLOCK_LENGTH = 28 | ||
MAGIC_NUMBER = 0x1A2B3C4D | ||
VERSION_MAJOR, VERSION_MINOR = 1,0 | ||
SECTIONLENGTH = 0xFFFFFFFFFFFFFFFF # -1 => Not specified | ||
|
||
shb_userappl_padded_len = math.ceil(len(shb_userappl)/4) * 4 | ||
shb_hardware_padded_len = math.ceil(len(shb_hardware)/4) * 4 | ||
|
||
total_block_len = HEADERS_BLOCK_LENGTH | ||
if shb_userappl_padded_len > 0: | ||
total_block_len += shb_userappl_padded_len + 4 | ||
|
||
if shb_hardware_padded_len > 0: | ||
total_block_len += shb_hardware_padded_len + 4 | ||
|
||
shb = struct.pack(">IIIHHQ", | ||
BLOCKTYPE, | ||
total_block_len, | ||
MAGIC_NUMBER, | ||
VERSION_MAJOR, VERSION_MINOR, | ||
SECTIONLENGTH) | ||
|
||
if shb_userappl != "": | ||
SHB_USERAPPL = 4 | ||
strpad = shb_userappl.ljust(shb_userappl_padded_len, "\0") | ||
shb += struct.pack(">HH", SHB_USERAPPL, shb_userappl_padded_len) | ||
shb += bytes(strpad, 'ascii') | ||
|
||
if shb_hardware != "": | ||
SHB_HARDWARE = 2 | ||
strpad = shb_hardware.ljust(shb_hardware_padded_len, "\0") | ||
shb += struct.pack(">HH", SHB_HARDWARE, shb_hardware_padded_len) | ||
shb += bytes(strpad, 'ascii') | ||
|
||
shb += struct.pack(">I",total_block_len) | ||
return shb | ||
|
||
def __build_pcapng_idb(self, link_type) -> bytes: | ||
BLOCKTYPE = 0x00000001 | ||
BLOCKLENGTH = 20 | ||
SNAP_LEN = 0 | ||
|
||
return struct.pack(">IIHHII", | ||
BLOCKTYPE, | ||
BLOCKLENGTH, | ||
link_type, 0, | ||
SNAP_LEN, | ||
BLOCKLENGTH) | ||
|
||
def __build_pcapng_epb(self, packet : bytes, timestamp : float) -> bytes: | ||
BLOCKTYPE = 0x00000006 | ||
BLOCKHEADERLEN = 32 | ||
INTERFACE_ID = 0 | ||
|
||
captured_packet_len = len(packet) | ||
original_packet_len = captured_packet_len | ||
padded_packet_len = math.ceil(captured_packet_len/4) * 4 | ||
padding_len = padded_packet_len - original_packet_len | ||
padded_packet = packet + bytearray(padding_len) | ||
block_total_length = BLOCKHEADERLEN + padded_packet_len | ||
timestamp_int = int(timestamp *1e6) # Set the proper resolution | ||
timestamp_high = timestamp_int >> 32 | ||
timestamp_low = timestamp_int & 0x00000000FFFFFFFF | ||
|
||
epb = struct.pack(">IIIIIII", | ||
BLOCKTYPE, | ||
block_total_length, | ||
INTERFACE_ID, | ||
timestamp_high, | ||
timestamp_low, | ||
captured_packet_len, | ||
original_packet_len) | ||
epb += padded_packet | ||
epb += struct.pack(">I", block_total_length) | ||
return epb | ||
|
||
############################################################################################################ | ||
# PUBLIC INTERFACES | ||
def create_pcapng_file(self, filename : str, shb_userappl : str = "", shb_hardware : str = "", link_type : int =147) -> bytes: | ||
if (filename == ""): | ||
return | ||
self.__filename = filename | ||
|
||
shb_bytes = self.__build_pcapng_shb(shb_userappl,shb_hardware) | ||
idb_bytes = self.__build_pcapng_idb(link_type) | ||
|
||
if os.path.isfile(self.__filename): | ||
logger.warning("{0} already exists. Overwriting it".format(filename)) | ||
|
||
with open(self.__filename, "wb") as f: | ||
f.write(shb_bytes) | ||
f.write(idb_bytes) | ||
|
||
def pcapng_file_append_packet(self, packet : bytes, timestamp: float): | ||
with open(self.__filename, "ab") as f: | ||
f.write(self.__build_pcapng_epb(packet, timestamp)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters