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

If packet not tweaked, loop on lower layers until edit or not supported

parent 470023d5
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -97,17 +97,20 @@ class Packet: ...@@ -97,17 +97,20 @@ class Packet:
@classmethod @classmethod
def init_packet(c, packet: scapy.Packet, id: int = 0) -> Packet: def init_packet(c, packet: scapy.Packet, id: int = 0, last_layer_index: int = -1) -> Packet:
""" """
Factory method to create a packet of a given protocol. Factory method to create a packet of a given protocol.
:param packet: Scapy Packet to be edited. :param packet: Scapy Packet to be edited.
:param id: Packet integer identifier. :param id: [Optional] Packet integer identifier. Default is 0.
:param last_layer_index: [Optional] Index of the last layer of the packet.
If not specified, it will be calculated.
:return: Packet of given protocol, :return: Packet of given protocol,
or generic Packet if protocol is not supported. or generic Packet if protocol is not supported.
""" """
# Try creating specific packet if possible # Try creating specific packet if possible
last_layer_index = Packet.get_last_layer_index(packet) if last_layer_index == -1:
last_layer_index = Packet.get_last_layer_index(packet)
for i in range(last_layer_index, -1, -1): for i in range(last_layer_index, -1, -1):
layer = packet.getlayer(i) layer = packet.getlayer(i)
try: try:
...@@ -120,7 +123,7 @@ class Packet: ...@@ -120,7 +123,7 @@ class Packet:
protocol = Packet.protocols.get(protocol, protocol) protocol = Packet.protocols.get(protocol, protocol)
module = importlib.import_module(f"packet.{protocol}") module = importlib.import_module(f"packet.{protocol}")
cls = getattr(module, protocol) cls = getattr(module, protocol)
return cls(packet, id) return cls(packet, id, i)
except ModuleNotFoundError: except ModuleNotFoundError:
# Layer protocol not supported # Layer protocol not supported
continue continue
...@@ -128,19 +131,21 @@ class Packet: ...@@ -128,19 +131,21 @@ class Packet:
raise ValueError(f"No supported protocol found for packet: {packet.summary()}") raise ValueError(f"No supported protocol found for packet: {packet.summary()}")
def __init__(self, packet: scapy.Packet, id: int = 0) -> None: def __init__(self, packet: scapy.Packet, id: int = 0, last_layer_index: int = -1) -> None:
""" """
Generic packet constructor. Generic packet constructor.
:param packet: Scapy Packet to be edited. :param packet: Scapy Packet to be edited.
:param id: Packet integer identifier. :param id: Packet integer identifier.
:param last_layer_index: [Optional] Index of the last layer of the packet.
If not specified, it will be calculated.
""" """
self.id = id self.id = id
self.packet = packet self.packet = packet
try: self.layer_index = last_layer_index if last_layer_index != -1 else Packet.get_last_layer_index(packet)
self.layer = packet.getlayer(self.name) self.layer = packet.getlayer(self.name)
except AttributeError: if self.layer is None:
self.layer = packet.lastlayer() self.layer = packet.getlayer(self.layer_index)
def get_packet(self) -> scapy.Packet: def get_packet(self) -> scapy.Packet:
...@@ -171,6 +176,15 @@ class Packet: ...@@ -171,6 +176,15 @@ class Packet:
return len(self.packet.getlayer(layer)) return len(self.packet.getlayer(layer))
def get_layer_index(self) -> int:
"""
Get packet layer index.
:return: Packet layer index.
"""
return self.layer_index
def rebuild(self) -> None: def rebuild(self) -> None:
""" """
Rebuild packet. Rebuild packet.
......
...@@ -20,8 +20,9 @@ class Transport(Packet): ...@@ -20,8 +20,9 @@ class Transport(Packet):
def tweak(self) -> dict: def tweak(self) -> dict:
""" """
Randomly edit destination or source port, If one of the ports is a well-known port,
in this order of priority. randomly edit destination or source port,
in this respective order of priority.
:return: Dictionary containing tweak information, :return: Dictionary containing tweak information,
or None if no tweak was performed. or None if no tweak was performed.
......
...@@ -96,25 +96,30 @@ def tweak_pcaps(pcaps: list, output: str, random_range: int = 1, packet_numbers: ...@@ -96,25 +96,30 @@ def tweak_pcaps(pcaps: list, output: str, random_range: int = 1, packet_numbers:
if must_edit_packet(i, packet_numbers, random_range): if must_edit_packet(i, packet_numbers, random_range):
# Edit packet, if possible # Edit packet, if possible
try: last_layer_index = Packet.get_last_layer_index(packet)
my_packet = Packet.init_packet(packet, i) while True:
except ValueError: try:
# No supported protocol found in packet, skip it my_packet = Packet.init_packet(packet, i, last_layer_index)
new_packets.append(rebuild_packet(packet)) except ValueError:
pass # No supported protocol found in packet, skip it
else: new_packets.append(rebuild_packet(packet))
d = my_packet.tweak() break
new_packets.append(my_packet.get_packet()) else:
if d is not None: d = my_packet.tweak()
writer.writerow(d) if d is None:
finally: # Packet was not edited, try editing one layer lower
i += 1 last_layer_index = my_packet.get_layer_index() - 1
else:
# Packet was edited
new_packets.append(my_packet.get_packet())
writer.writerow(d)
break
else: else:
# Packet won't be edited # Packet won't be edited
# Go to next packet
i += 1
new_packets.append(rebuild_packet(packet)) new_packets.append(rebuild_packet(packet))
i += 1
# Write output PCAP file # Write output PCAP file
output_pcap = "" output_pcap = ""
if output is not None and len(pcaps) == 1: if output is not None and len(pcaps) == 1:
......
Fichier ajouté
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