Skip to content
Extraits de code Groupes Projets
Valider a2a17dd3 rédigé par François De Keersmaeker's avatar François De Keersmaeker
Parcourir les fichiers

Payload hash: first pad payload to min Ethernet payload length (46 bytes)

parent c3b6071b
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -8,14 +8,19 @@ from ipaddress import IPv4Address, IPv6Address ...@@ -8,14 +8,19 @@ from ipaddress import IPv4Address, IPv6Address
import scapy.all as scapy import scapy.all as scapy
import hashlib import hashlib
class Packet: class Packet:
""" """
Wrapper around the Scapy `Packet` class. Wrapper around the Scapy `Packet` class.
""" """
##### CLASS VARIABLES #####
# List of all alphanumerical characters # List of all alphanumerical characters
ALPHANUM_CHARS = list(string.ascii_letters + string.digits) ALPHANUM_CHARS = list(string.ascii_letters + string.digits)
ALPHANUM_BYTES = list(bytes(string.ascii_letters + string.digits, "utf-8")) ALPHANUM_BYTES = list(bytes(string.ascii_letters + string.digits, "utf-8"))
# Minimun payload length (in bytes)
MIN_PAYLOAD_LENGTH = 46
# Protocol name correspondences # Protocol name correspondences
protocols = { protocols = {
...@@ -26,6 +31,10 @@ class Packet: ...@@ -26,6 +31,10 @@ class Packet:
fields = {} fields = {}
##### STATIC METHODS #####
@staticmethod @staticmethod
def string_edit_char(s: str) -> str: def string_edit_char(s: str) -> str:
""" """
...@@ -97,6 +106,21 @@ class Packet: ...@@ -97,6 +106,21 @@ class Packet:
return i - 1 return i - 1
@staticmethod
def rebuild_packet(packet: scapy.Packet) -> scapy.Packet:
"""
Rebuild a Scapy packet from its bytes representation,
but keep its old timestamp.
:param packet: Scapy packet
:return: Rebuilt Scapy packet, with old timestamp
"""
timestamp = packet.time
new_packet = packet.__class__(bytes(packet))
new_packet.time = timestamp
return new_packet
@classmethod @classmethod
def init_packet(c, packet: scapy.Packet, id: int = 0, last_layer_index: int = -1) -> Packet: def init_packet(c, packet: scapy.Packet, id: int = 0, last_layer_index: int = -1) -> Packet:
""" """
...@@ -135,6 +159,10 @@ class Packet: ...@@ -135,6 +159,10 @@ class Packet:
continue continue
# No supported protocol found, raise ValueError # No supported protocol found, raise ValueError
raise ValueError(f"No supported protocol found for packet: {packet.summary()}") raise ValueError(f"No supported protocol found for packet: {packet.summary()}")
##### INSTANCE METHODS #####
def __init__(self, packet: scapy.Packet, id: int = 0, last_layer_index: int = -1) -> None: def __init__(self, packet: scapy.Packet, id: int = 0, last_layer_index: int = -1) -> None:
...@@ -193,11 +221,14 @@ class Packet: ...@@ -193,11 +221,14 @@ class Packet:
def get_hash(self) -> str: def get_hash(self) -> str:
""" """
Get packet hash. Get packet payload SHA256 hash.
The payload is first padded with null bytes to reach the minimum Ethernet payload length of 46 bytes.
:return: Packet hash. :return: Packet payload SHA256 hash.
""" """
return hashlib.sha256(bytes(self.packet)).hexdigest() pad_bytes_to_add = Packet.MIN_PAYLOAD_LENGTH - len(self.packet.payload)
payload = bytes(self.packet.payload) + bytes(pad_bytes_to_add) if pad_bytes_to_add > 0 else bytes(self.packet.payload)
return hashlib.sha256(payload).hexdigest()
def rebuild(self) -> None: def rebuild(self) -> None:
......
...@@ -10,7 +10,6 @@ import csv ...@@ -10,7 +10,6 @@ import csv
import scapy.all as scapy import scapy.all as scapy
from scapy.layers import dhcp, dns, http from scapy.layers import dhcp, dns, http
from scapy.contrib import coap, igmp, igmpv3 from scapy.contrib import coap, igmp, igmpv3
import hashlib
from packet.Packet import Packet from packet.Packet import Packet
...@@ -46,30 +45,6 @@ def must_edit_packet(i: int, packet_numbers: list, random_range: int) -> bool: ...@@ -46,30 +45,6 @@ def must_edit_packet(i: int, packet_numbers: list, random_range: int) -> bool:
return is_specified or is_random return is_specified or is_random
def rebuild_packet(packet: scapy.Packet) -> scapy.Packet:
"""
Rebuild a Scapy packet from its bytes representation,
but keep its old timestamp.
:param packet: Scapy packet
:return: Rebuilt Scapy packet, with old timestamp
"""
timestamp = packet.time
new_packet = packet.__class__(bytes(packet))
new_packet.time = timestamp
return new_packet
def get_packet_hash(packet: scapy.Packet) -> str:
"""
Get the SHA256 hash of a Scapy packet.
:param packet: Scapy packet
:return: SHA256 hash of Scapy packet
"""
return hashlib.sha256(bytes(packet)).hexdigest()
def tweak_pcaps(pcaps: list, output: str, random_range: int = 1, packet_numbers: list = None, dry_run: bool = False) -> None: def tweak_pcaps(pcaps: list, output: str, random_range: int = 1, packet_numbers: list = None, dry_run: bool = False) -> None:
""" """
Main functionality of the program: Main functionality of the program:
...@@ -117,7 +92,7 @@ def tweak_pcaps(pcaps: list, output: str, random_range: int = 1, packet_numbers: ...@@ -117,7 +92,7 @@ def tweak_pcaps(pcaps: list, output: str, random_range: int = 1, packet_numbers:
my_packet = Packet.init_packet(packet, i, last_layer_index) my_packet = Packet.init_packet(packet, i, last_layer_index)
except ValueError: except ValueError:
# No supported protocol found in packet, skip it # No supported protocol found in packet, skip it
new_packets.append(rebuild_packet(packet)) new_packets.append(Packet.rebuild_packet(packet))
break break
else: else:
d = my_packet.tweak() d = my_packet.tweak()
...@@ -131,7 +106,7 @@ def tweak_pcaps(pcaps: list, output: str, random_range: int = 1, packet_numbers: ...@@ -131,7 +106,7 @@ def tweak_pcaps(pcaps: list, output: str, random_range: int = 1, packet_numbers:
break break
else: else:
# Packet won't be edited # Packet won't be edited
new_packets.append(rebuild_packet(packet)) new_packets.append(Packet.rebuild_packet(packet))
i += 1 i += 1
......
Fichier ajouté
Aucun aperçu pour ce type de fichier
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter