From 1d6ba15681f8cf173ec87288fc7846e08ed3ad09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20De=20Keersmaeker?= <francois.dekeersmaeker@uclouvain.be> Date: Thu, 26 Dec 2024 17:46:23 +0100 Subject: [PATCH] Fixed HTTP decode bug. Added logging. --- pcap_anonymize/app_layer/__init__.py | 2 +- pcap_anonymize/app_layer/http.py | 8 ++++++++ pcap_anonymize/pcap_anonymize.py | 15 ++++++++++++--- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/pcap_anonymize/app_layer/__init__.py b/pcap_anonymize/app_layer/__init__.py index 667ee0f..3bb84fd 100644 --- a/pcap_anonymize/app_layer/__init__.py +++ b/pcap_anonymize/app_layer/__init__.py @@ -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 diff --git a/pcap_anonymize/app_layer/http.py b/pcap_anonymize/app_layer/http.py index 7a418c3..cb4babc 100644 --- a/pcap_anonymize/app_layer/http.py +++ b/pcap_anonymize/app_layer/http.py @@ -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 diff --git a/pcap_anonymize/pcap_anonymize.py b/pcap_anonymize/pcap_anonymize.py index 571fdf4..b88edf8 100644 --- a/pcap_anonymize/pcap_anonymize.py +++ b/pcap_anonymize/pcap_anonymize.py @@ -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 = [] -- GitLab