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

Fixed HTTP decode bug. Added logging.

parent 155e92e6
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Pipeline #61616 réussi
...@@ -37,5 +37,5 @@ def anonymize_app_layer(packet: Packet) -> None: ...@@ -37,5 +37,5 @@ def anonymize_app_layer(packet: Packet) -> None:
dport = tcp.getfieldval("dport") dport = tcp.getfieldval("dport")
if sport == 9999 or dport == 9999: if sport == 9999 or dport == 9999:
anonymize_tplink(tcp) anonymize_tplink(tcp)
except: except AttributeError:
pass pass
...@@ -3,11 +3,14 @@ Anonymize HTTP packets. ...@@ -3,11 +3,14 @@ Anonymize HTTP packets.
""" """
from enum import Enum from enum import Enum
import logging
from scapy.all import Packet, Raw from scapy.all import Packet, Raw
from scapy.layers.http import HTTP, HTTPRequest, HTTPResponse from scapy.layers.http import HTTP, HTTPRequest, HTTPResponse
ENCODING = "utf-8" ENCODING = "utf-8"
logger = logging.getLogger("pcap_anonymize")
class HttpFields(Enum): class HttpFields(Enum):
""" """
...@@ -72,6 +75,11 @@ def anonymize_http(http: HTTP) -> None: ...@@ -72,6 +75,11 @@ def anonymize_http(http: HTTP) -> None:
http.setfieldval(HttpFields.PATH.value, path.split("?")[0].encode(ENCODING)) http.setfieldval(HttpFields.PATH.value, path.split("?")[0].encode(ENCODING))
except AttributeError: except AttributeError:
# HTTP packet does not contain the `Path` field # HTTP packet does not contain the `Path` field
logger.warning(f"Field {HttpFields.PATH.value} not found in HTTP layer {http.summary()}")
pass
except UnicodeDecodeError:
# `Path` field is not encoded in UTF-8
logger.warning(f"Field {HttpFields.PATH.value} not UTF-8 encoded in HTTP layer {http.summary()}")
pass pass
# Remove all fields other than Method and Path # Remove all fields other than Method and Path
......
...@@ -5,6 +5,7 @@ Anonymize all packets in a PCAP file. ...@@ -5,6 +5,7 @@ Anonymize all packets in a PCAP file.
import os import os
import glob import glob
from pathlib import Path from pathlib import Path
import logging
from scapy.all import Packet, sniff, wrpcap from scapy.all import Packet, sniff, wrpcap
# Packet layers # Packet layers
from .mac import anonymize_pkt_macs from .mac import anonymize_pkt_macs
...@@ -13,8 +14,12 @@ from .app_layer import anonymize_app_layer ...@@ -13,8 +14,12 @@ from .app_layer import anonymize_app_layer
### GLOBAL VARIABLES ### ### GLOBAL VARIABLES ###
i = 1
packets = [] packets = []
# Logging configuration
logger = logging.getLogger("pcap_anonymize")
### FUNCTIONS ### ### FUNCTIONS ###
...@@ -49,7 +54,9 @@ def anonymize_packet(packet: Packet) -> None: ...@@ -49,7 +54,9 @@ def anonymize_packet(packet: Packet) -> None:
Args: Args:
packet: scapy packet to anonymize packet: scapy packet to anonymize
""" """
global packets global i, packets
logger.debug(f"Packet #{i}: {packet.summary()}")
# Anonymize MAC addresses # Anonymize MAC addresses
anonymize_pkt_macs(packet) anonymize_pkt_macs(packet)
...@@ -61,6 +68,7 @@ def anonymize_packet(packet: Packet) -> None: ...@@ -61,6 +68,7 @@ def anonymize_packet(packet: Packet) -> None:
packet = rebuild_packet(packet) packet = rebuild_packet(packet)
packets.append(packet) packets.append(packet)
i += 1
def anonymize_pcap(input: os.PathLike, output: os.PathLike = None) -> None: def anonymize_pcap(input: os.PathLike, output: os.PathLike = None) -> None:
...@@ -72,7 +80,7 @@ def anonymize_pcap(input: os.PathLike, output: os.PathLike = None) -> None: ...@@ -72,7 +80,7 @@ def anonymize_pcap(input: os.PathLike, output: os.PathLike = None) -> None:
output: path to the output PCAP file. output: path to the output PCAP file.
If None, create a new file having the same name as the input file with the suffix '.anon.pcap'. If None, create a new file having the same name as the input file with the suffix '.anon.pcap'.
""" """
global packets global i, packets
if output is None: if output is None:
output = str(Path(input).with_suffix(".anon.pcap")) output = str(Path(input).with_suffix(".anon.pcap"))
...@@ -83,7 +91,8 @@ def anonymize_pcap(input: os.PathLike, output: os.PathLike = None) -> None: ...@@ -83,7 +91,8 @@ def anonymize_pcap(input: os.PathLike, output: os.PathLike = None) -> None:
# Write anonymized packets to the output file # Write anonymized packets to the output file
wrpcap(output, packets) wrpcap(output, packets)
# Reset global packets list # Reset global variables
i = 1
packets = [] packets = []
......
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