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:
dport = tcp.getfieldval("dport")
if sport == 9999 or dport == 9999:
anonymize_tplink(tcp)
except:
except AttributeError:
pass
......@@ -3,11 +3,14 @@ Anonymize HTTP packets.
"""
from enum import Enum
import logging
from scapy.all import Packet, Raw
from scapy.layers.http import HTTP, HTTPRequest, HTTPResponse
ENCODING = "utf-8"
logger = logging.getLogger("pcap_anonymize")
class HttpFields(Enum):
"""
......@@ -72,6 +75,11 @@ def anonymize_http(http: HTTP) -> None:
http.setfieldval(HttpFields.PATH.value, path.split("?")[0].encode(ENCODING))
except AttributeError:
# 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
# Remove all fields other than Method and Path
......
......@@ -5,6 +5,7 @@ Anonymize all packets in a PCAP file.
import os
import glob
from pathlib import Path
import logging
from scapy.all import Packet, sniff, wrpcap
# Packet layers
from .mac import anonymize_pkt_macs
......@@ -13,8 +14,12 @@ from .app_layer import anonymize_app_layer
### GLOBAL VARIABLES ###
i = 1
packets = []
# Logging configuration
logger = logging.getLogger("pcap_anonymize")
### FUNCTIONS ###
......@@ -49,7 +54,9 @@ def anonymize_packet(packet: Packet) -> None:
Args:
packet: scapy packet to anonymize
"""
global packets
global i, packets
logger.debug(f"Packet #{i}: {packet.summary()}")
# Anonymize MAC addresses
anonymize_pkt_macs(packet)
......@@ -61,6 +68,7 @@ def anonymize_packet(packet: Packet) -> None:
packet = rebuild_packet(packet)
packets.append(packet)
i += 1
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.
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:
output = str(Path(input).with_suffix(".anon.pcap"))
......@@ -83,7 +91,8 @@ def anonymize_pcap(input: os.PathLike, output: os.PathLike = None) -> None:
# Write anonymized packets to the output file
wrpcap(output, packets)
# Reset global packets list
# Reset global variables
i = 1
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