From 7f558ddf2dab7e038ab24ad9c3b4ec4d118a3ed8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20De=20Keersmaeker?=
 <francois.dekeersmaeker@uclouvain.be>
Date: Thu, 16 May 2024 15:23:12 +0200
Subject: [PATCH] Replaced 'tweak' -> 'fuzz'. Removed unused imports.

---
 .ci_scripts/run-all-pcaps.sh            |  4 ++--
 README.md                               |  4 ++--
 src/packet/ARP.py                       |  1 -
 src/packet/BOOTP.py                     |  8 +++-----
 src/packet/CoAP.py                      |  9 +++------
 src/packet/DNS.py                       |  7 +++----
 src/packet/HTTP_Request.py              |  2 --
 src/packet/ICMP.py                      |  1 -
 src/packet/IGMP.py                      |  2 --
 src/packet/IGMPv3mr.py                  | 10 ++++------
 src/packet/IPv4.py                      |  1 -
 src/packet/IPv6.py                      |  1 -
 src/packet/Packet.py                    | 14 +++++++-------
 src/packet/TCP.py                       |  1 -
 src/packet/Transport.py                 | 12 +++++-------
 src/packet/UDP.py                       |  1 -
 src/packet/mDNS.py                      |  1 -
 src/{pcap_tweaker.py => pcap_fuzzer.py} |  6 +++---
 18 files changed, 32 insertions(+), 53 deletions(-)
 rename src/{pcap_tweaker.py => pcap_fuzzer.py} (97%)

diff --git a/.ci_scripts/run-all-pcaps.sh b/.ci_scripts/run-all-pcaps.sh
index 8f75b00..53446e7 100755
--- a/.ci_scripts/run-all-pcaps.sh
+++ b/.ci_scripts/run-all-pcaps.sh
@@ -4,8 +4,8 @@ EXITCODE=0
 
 for pcap in $GITHUB_WORKSPACE/traces/*.pcap
 do
-    # Run pcap_tweaker script on pcap file
-    python3 $GITHUB_WORKSPACE/src/pcap_tweaker.py $pcap
+    # Run pcap_fuzzer script on pcap file
+    python3 $GITHUB_WORKSPACE/src/pcap_fuzzer.py $pcap
     # If the exit code is not 0, set EXITCODE to 1
     if [[ $? -ne 0 ]]
     then
diff --git a/README.md b/README.md
index ec46c0b..e88d766 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,4 @@
-# pcap-tweaker
+# pcap-fuzzer
 This program randomly edits packets from a PCAP file,
 one field per edited packet.
 
@@ -22,7 +22,7 @@ pip install -r requirements.txt
 ## Usage
 
 ```bash
-python3 pcap_tweaker.py [-h] [-o OUTPUT] [-r RANDOM_RANGE] [-n PACKET_NUMBER] [-d] pcap [pcap ...]
+python3 pcap_fuzzer.py [-h] [-o OUTPUT] [-r RANDOM_RANGE] [-n PACKET_NUMBER] [-d] pcap [pcap ...]
 ```
 
 The program produces new PCAP file with the same name as the input files,
diff --git a/src/packet/ARP.py b/src/packet/ARP.py
index d3a9554..e037887 100644
--- a/src/packet/ARP.py
+++ b/src/packet/ARP.py
@@ -1,4 +1,3 @@
-import scapy.all as scapy
 from packet.Packet import Packet
 
 class ARP(Packet):
diff --git a/src/packet/BOOTP.py b/src/packet/BOOTP.py
index c796c1b..a88730e 100644
--- a/src/packet/BOOTP.py
+++ b/src/packet/BOOTP.py
@@ -1,8 +1,6 @@
-import logging
 from typing import Tuple
 import random
 import scapy.all as scapy
-from scapy.layers import dhcp
 from packet.Packet import Packet
 
 class BOOTP(Packet):
@@ -61,13 +59,13 @@ class BOOTP(Packet):
         self.dhcp_options.setfieldval("options", dhcp_options)
 
 
-    def tweak(self) -> dict:
+    def fuzz(self) -> dict:
         """
         Randomly edit a BOOTP/DHCP field, among the following:
             - chaddr (client hardware address)
             - message-type (DHCP message type)
 
-        :return: Dictionary containing tweak information.
+        :return: Dictionary containing fuzz information.
         """
         # Store old hash value
         old_hash = self.get_hash()
@@ -95,5 +93,5 @@ class BOOTP(Packet):
         # Update checksums
         self.update_fields()
 
-        # Return value: dictionary containing tweak information
+        # Return value: dictionary containing fuzz information
         return self.get_dict_log(field, old_value, new_value, old_hash)
diff --git a/src/packet/CoAP.py b/src/packet/CoAP.py
index 1855d93..b274a7d 100644
--- a/src/packet/CoAP.py
+++ b/src/packet/CoAP.py
@@ -1,7 +1,4 @@
-import logging
 import random
-import scapy.all as scapy
-from scapy.contrib import coap
 from packet.Packet import Packet
 
 class CoAP(Packet):
@@ -68,14 +65,14 @@ class CoAP(Packet):
         return result
 
 
-    def tweak(self) -> dict:
+    def fuzz(self) -> dict:
         """
         Randomly edit one field of the CoAP packet, among the following:
             - type
             - code
             - uri
 
-        :return: Dictionary containing tweak information.
+        :return: Dictionary containing fuzz information.
         """
         # Store old hash value
         old_hash = self.get_hash()
@@ -105,5 +102,5 @@ class CoAP(Packet):
         # Update checksums
         self.update_fields()
 
-        # Return value: dictionary containing tweak information
+        # Return value: dictionary containing fuzz information
         return self.get_dict_log(field, old_value, new_value, old_hash)
diff --git a/src/packet/DNS.py b/src/packet/DNS.py
index cfb79b9..2139740 100644
--- a/src/packet/DNS.py
+++ b/src/packet/DNS.py
@@ -1,5 +1,4 @@
 import random
-import scapy.all as scapy
 from scapy.layers import dns
 from packet.Packet import Packet
 
@@ -62,14 +61,14 @@ class DNS(Packet):
         return random.choice(self.fields)
 
 
-    def tweak(self) -> dict:
+    def fuzz(self) -> dict:
         """
         Randomly edit one DNS field, among the following:
             - QR flag
             - Query type
             - Query name
 
-        :return: Dictionary containing tweak information.
+        :return: Dictionary containing fuzz information.
         """
         # Store old hash value
         old_hash = self.get_hash()
@@ -124,5 +123,5 @@ class DNS(Packet):
         # Update checksums
         self.update_fields()
 
-        # Return value: dictionary containing tweak information
+        # Return value: dictionary containing fuzz information
         return self.get_dict_log(field, old_value, new_value, old_hash)
diff --git a/src/packet/HTTP_Request.py b/src/packet/HTTP_Request.py
index 83ca151..6d5665f 100644
--- a/src/packet/HTTP_Request.py
+++ b/src/packet/HTTP_Request.py
@@ -1,5 +1,3 @@
-import scapy.all as scapy
-from scapy.layers import http
 from packet.Packet import Packet
 
 class HTTP_Request(Packet):
diff --git a/src/packet/ICMP.py b/src/packet/ICMP.py
index 4cdb3c4..8d4879f 100644
--- a/src/packet/ICMP.py
+++ b/src/packet/ICMP.py
@@ -1,4 +1,3 @@
-import scapy.all as scapy
 from packet.Packet import Packet
 
 class ICMP(Packet):
diff --git a/src/packet/IGMP.py b/src/packet/IGMP.py
index b413787..0afe978 100644
--- a/src/packet/IGMP.py
+++ b/src/packet/IGMP.py
@@ -1,5 +1,3 @@
-import scapy.all as scapy
-from scapy.contrib import igmp
 from packet.Packet import Packet
 
 class IGMP(Packet):
diff --git a/src/packet/IGMPv3mr.py b/src/packet/IGMPv3mr.py
index 40d3b81..874b0d8 100644
--- a/src/packet/IGMPv3mr.py
+++ b/src/packet/IGMPv3mr.py
@@ -1,5 +1,3 @@
-import logging
-import scapy.all as scapy
 from scapy.contrib import igmpv3
 from packet.Packet import Packet
 
@@ -12,12 +10,12 @@ class IGMPv3mr(Packet):
     name = "IGMPv3mr"
 
 
-    def tweak(self) -> dict:
+    def fuzz(self) -> dict:
         """
-        Tweak the IGMPv3 Membership Report packet,
+        fuzz the IGMPv3 Membership Report packet,
         by randomizing all group addresses.
 
-        :return: Dictionary containing tweak information.
+        :return: Dictionary containing fuzz information.
         """
         # Store old hash value
         old_hash = self.get_hash()
@@ -39,5 +37,5 @@ class IGMPv3mr(Packet):
         # Update checksums
         self.update_fields()
 
-        # Return value: dictionary containing tweak information
+        # Return value: dictionary containing fuzz information
         return self.get_dict_log("maddr", old_value, new_value, old_hash)
diff --git a/src/packet/IPv4.py b/src/packet/IPv4.py
index 47be767..0636335 100644
--- a/src/packet/IPv4.py
+++ b/src/packet/IPv4.py
@@ -1,4 +1,3 @@
-import scapy.all as scapy
 from packet.Packet import Packet
 
 class IPv4(Packet):
diff --git a/src/packet/IPv6.py b/src/packet/IPv6.py
index acc1aa1..0d8703a 100644
--- a/src/packet/IPv6.py
+++ b/src/packet/IPv6.py
@@ -1,4 +1,3 @@
-import scapy.all as scapy
 from packet.Packet import Packet
 
 class IPv6(Packet):
diff --git a/src/packet/Packet.py b/src/packet/Packet.py
index 43f9064..11fadcb 100644
--- a/src/packet/Packet.py
+++ b/src/packet/Packet.py
@@ -269,13 +269,13 @@ class Packet:
     def get_dict_log(self, field: str, old_value: str, new_value: str, old_hash: str) -> dict:
         """
         Log packet field modification,
-        and return a dictionary containing tweak information.
+        and return a dictionary containing fuzz information.
 
         :param field: Field name.
         :param old_value: Old field value.
         :param new_value: New field value.
-        :param old_hash: Old packet hash (before tweak).
-        :return: Dictionary containing tweak information.
+        :param old_hash: Old packet hash (before fuzz).
+        :return: Dictionary containing fuzz information.
         """
         timestamp = self.packet.time
         logging.info(f"Packet {self.id}, timestamp {timestamp}: {self.name}.{field} = {old_value} -> {new_value}")
@@ -292,12 +292,12 @@ class Packet:
         return d
 
 
-    def tweak(self) -> dict:
+    def fuzz(self) -> dict:
         """
         Randomly edit one packet field.
 
-        :return: Dictionary containing tweak information,
-                 or None if no tweak was performed.
+        :return: Dictionary containing fuzz information,
+                 or None if no fuzz was performed.
         """
         # Store old hash value
         old_hash = self.get_hash()
@@ -368,5 +368,5 @@ class Packet:
         # Update checksums
         self.update_fields()
 
-        # Return value: dictionary containing tweak information
+        # Return value: dictionary containing fuzz information
         return self.get_dict_log(field, old_value, new_value, old_hash)
diff --git a/src/packet/TCP.py b/src/packet/TCP.py
index 590b729..0a19fed 100644
--- a/src/packet/TCP.py
+++ b/src/packet/TCP.py
@@ -1,4 +1,3 @@
-import scapy.all as scapy
 from packet.Transport import Transport
 
 class TCP(Transport):
diff --git a/src/packet/Transport.py b/src/packet/Transport.py
index da73a5e..d6e1473 100644
--- a/src/packet/Transport.py
+++ b/src/packet/Transport.py
@@ -1,6 +1,4 @@
 import random
-import scapy.all as scapy
-from scapy.layers import http
 from packet.Packet import Packet
 
 class Transport(Packet):
@@ -18,14 +16,14 @@ class Transport(Packet):
     ports = []
 
 
-    def tweak(self) -> dict:
+    def fuzz(self) -> dict:
         """
         If one of the ports is a well-known port,
         randomly edit destination or source port,
         in this respective order of priority.
 
-        :return: Dictionary containing tweak information,
-                 or None if no tweak was performed.
+        :return: Dictionary containing fuzz information,
+                 or None if no fuzz was performed.
         """
         # Store old hash value
         old_hash = self.get_hash()
@@ -36,7 +34,7 @@ class Transport(Packet):
         elif self.layer.getfieldval("sport") in self.ports:
             field = "sport"
         else:
-            # No well-known port, do not tweak
+            # No well-known port, do not fuzz
             return None
         
         # Store old value of field
@@ -54,5 +52,5 @@ class Transport(Packet):
         # Update checksums, if needed
         self.update_fields()
 
-        # Return value: dictionary containing tweak information
+        # Return value: dictionary containing fuzz information
         return self.get_dict_log(field, old_value, new_value, old_hash)
diff --git a/src/packet/UDP.py b/src/packet/UDP.py
index 5c6f444..c2e586b 100644
--- a/src/packet/UDP.py
+++ b/src/packet/UDP.py
@@ -1,4 +1,3 @@
-import scapy.all as scapy
 from packet.Transport import Transport
 
 class UDP(Transport):
diff --git a/src/packet/mDNS.py b/src/packet/mDNS.py
index 1c566b4..55abe51 100644
--- a/src/packet/mDNS.py
+++ b/src/packet/mDNS.py
@@ -1,6 +1,5 @@
 import random
 import scapy.all as scapy
-from scapy.layers import dns
 from packet.DNS import DNS
 
 class mDNS(DNS):
diff --git a/src/pcap_tweaker.py b/src/pcap_fuzzer.py
similarity index 97%
rename from src/pcap_tweaker.py
rename to src/pcap_fuzzer.py
index 521d9f0..f8ddfdd 100644
--- a/src/pcap_tweaker.py
+++ b/src/pcap_fuzzer.py
@@ -45,7 +45,7 @@ def must_edit_packet(i: int, packet_numbers: list, random_range: int) -> bool:
     return is_specified or is_random
 
 
-def tweak_pcaps(pcaps: list, output: str, random_range: int = 1, packet_numbers: list = None, dry_run: bool = False) -> None:
+def fuzz_pcaps(pcaps: list, output: str, random_range: int = 1, packet_numbers: list = None, dry_run: bool = False) -> None:
     """
     Main functionality of the program:
     (Randomly) edit packet fields in a (list of) PCAP file(s).
@@ -95,7 +95,7 @@ def tweak_pcaps(pcaps: list, output: str, random_range: int = 1, packet_numbers:
                             new_packets.append(Packet.rebuild_packet(packet))
                             break
                         else:
-                            d = my_packet.tweak()
+                            d = my_packet.fuzz()
                             if d is None:
                                 # Packet was not edited, try editing one layer lower
                                 last_layer_index = my_packet.get_layer_index() - 1
@@ -162,7 +162,7 @@ if __name__ == "__main__":
 
 
     ### MAIN PROGRAM ###
-    tweak_pcaps(
+    fuzz_pcaps(
         pcaps=args.input_pcaps,
         output=args.output,
         random_range=args.random_range,
-- 
GitLab