Browse Source

Refactored and removed MISC_OUT_FILES

Stefan Schmidt 6 years ago
parent
commit
9bcfada5f6

+ 4 - 5
code/Attack/MembersMgmtCommAttack.py

@@ -130,7 +130,8 @@ class MembersMgmtCommAttack(BaseAttack.BaseAttack):
     def generate_attack_pcap(self):
     def generate_attack_pcap(self):
         """
         """
         Injects the packets of this attack into a PCAP and stores it as a temporary file.
         Injects the packets of this attack into a PCAP and stores it as a temporary file.
-        :return: a tuple of the number packets injected and the path to the temporary attack PCAP
+        :return: a tuple of the number packets injected, the path to the temporary attack PCAP
+        and a list of additionally created files
         """
         """
 
 
         # create the final messages that have to be sent, including all bot configurations
         # create the final messages that have to be sent, including all bot configurations
@@ -231,15 +232,14 @@ class MembersMgmtCommAttack(BaseAttack.BaseAttack):
 
 
         # write the mapping to a file
         # write the mapping to a file
         current_ts = datetime.now().strftime("%Y%m%d-%H%M%S")
         current_ts = datetime.now().strftime("%Y%m%d-%H%M%S")
-        mapping_filename = "mapping_" + current_ts 
+        mapping_filename = "mapping_" + current_ts + ".xml"
         msg_packet_mapping.write_to_file(mapping_filename)
         msg_packet_mapping.write_to_file(mapping_filename)
-        Util.MISC_OUT_FILES["mapping.xml"] = mapping_filename
 
 
         # Store timestamp of last packet
         # Store timestamp of last packet
         self.attack_end_utime = last_packet.time
         self.attack_end_utime = last_packet.time
 
 
         # Return packets sorted by packet by timestamp and total number of packets (sent)
         # Return packets sorted by packet by timestamp and total number of packets (sent)
-        return total_pkts , path_attack_pcap
+        return total_pkts , path_attack_pcap, [mapping_filename]
 
 
 
 
     def generate_attack_packets(self):
     def generate_attack_packets(self):
@@ -447,7 +447,6 @@ class MembersMgmtCommAttack(BaseAttack.BaseAttack):
                 print("Writing corresponding XML file...", end=" ")
                 print("Writing corresponding XML file...", end=" ")
                 sys.stdout.flush()
                 sys.stdout.flush()
             filepath_xml = cpp_comm_proc.write_xml(Util.OUT_DIR, filename)
             filepath_xml = cpp_comm_proc.write_xml(Util.OUT_DIR, filename)
-            Util.MISC_OUT_FILES[filepath_xml] = None
             if print_updates: print("done.")
             if print_updates: print("done.")
         else:
         else:
             filesize = os.path.getsize(filepath_xml) / 2**20  # get filesize in MB
             filesize = os.path.getsize(filepath_xml) / 2**20  # get filesize in MB

+ 7 - 1
code/Core/AttackController.py

@@ -29,6 +29,7 @@ class AttackController:
         self.added_attacks = []
         self.added_attacks = []
         self.seed = None
         self.seed = None
         self.total_packets = 0
         self.total_packets = 0
+        self.additional_files = []
 
 
     def set_seed(self, seed: int) -> None:
     def set_seed(self, seed: int) -> None:
         """
         """
@@ -176,7 +177,12 @@ class AttackController:
         if time:
         if time:
             self.current_attack.set_finish_time()
             self.current_attack.set_finish_time()
         duration = self.current_attack.get_packet_generation_time()
         duration = self.current_attack.get_packet_generation_time()
-        self.total_packets, temp_attack_pcap_path = self.current_attack.generate_attack_pcap()
+        attack_result = self.current_attack.generate_attack_pcap()
+        self.total_packets = attack_result[0]
+        temp_attack_pcap_path = attack_result[1]
+        if len(attack_result) == 3:
+            # Extract the list of additional files, if available
+            self.additional_files += attack_result[2]
         print("done. (total: " + str(self.total_packets) + " pkts", end="")
         print("done. (total: " + str(self.total_packets) + " pkts", end="")
         if time:
         if time:
             print(" in ", duration, " seconds", end="")
             print(" in ", duration, " seconds", end="")

+ 9 - 11
code/Core/Controller.py

@@ -32,6 +32,7 @@ class Controller:
         self.seed = None
         self.seed = None
         self.durations = []
         self.durations = []
         self.added_packets = 0
         self.added_packets = 0
+        self.created_files = []
         self.debug = debug
         self.debug = debug
 
 
         # Initialize class instances
         # Initialize class instances
@@ -136,17 +137,14 @@ class Controller:
 
 
             os.rename(self.pcap_dest_path, result_path)
             os.rename(self.pcap_dest_path, result_path)
             self.pcap_dest_path = result_path
             self.pcap_dest_path = result_path
-            created_files = [self.pcap_dest_path]
+            self.created_files = [self.pcap_dest_path]
 
 
             # process/move other created files
             # process/move other created files
-            pcap_root = os.path.splitext(self.pcap_dest_path)[0]
-            for k, v in Util.MISC_OUT_FILES.items():
-                if v is None:
-                    created_files.append(k)
-                else:
-                    outpath = pcap_root + "_" + k
-                    os.rename(v, outpath)
-                    created_files.append(outpath)
+            pcap_basename = os.path.splitext(self.pcap_dest_path)[0]
+            for x in self.attack_controller.additional_files:
+                outpath = pcap_basename + "_" + x
+                os.rename(x, outpath)
+                self.created_files.append(outpath)
 
 
             print("done.")
             print("done.")
 
 
@@ -161,11 +159,11 @@ class Controller:
 
 
             # write label file with attacks
             # write label file with attacks
             self.label_manager.write_label_file(self.pcap_dest_path)
             self.label_manager.write_label_file(self.pcap_dest_path)
-            created_files.insert(1, self.label_manager.label_file_path)
+            self.created_files.insert(1, self.label_manager.label_file_path)
 
 
             # print status message
             # print status message
             print('\nOutput files created:')
             print('\nOutput files created:')
-            for filepath in created_files:
+            for filepath in self.created_files:
                 print(filepath)
                 print(filepath)
         else:
         else:
             print("done.")
             print("done.")

+ 2 - 2
code/ID2TLib/TestLibrary.py

@@ -41,8 +41,8 @@ def clean_up(controller):
 
 
     :param controller: controller which created output files
     :param controller: controller which created output files
     """
     """
-    os.remove(controller.pcap_dest_path)
-    os.remove(controller.label_manager.label_file_path)
+    for file in controller.created_files:
+        os.remove(file)
 
 
 
 
 def rename_test_result_files(controller, caller_function: str, attack_sub_dir=False, test_sub_dir=False):
 def rename_test_result_files(controller, caller_function: str, attack_sub_dir=False, test_sub_dir=False):

+ 0 - 1
code/ID2TLib/Utility.py

@@ -18,7 +18,6 @@ ROOT_DIR = CODE_DIR + "../"
 RESOURCE_DIR = ROOT_DIR + "resources/"
 RESOURCE_DIR = ROOT_DIR + "resources/"
 TEST_DIR = RESOURCE_DIR + "test/"
 TEST_DIR = RESOURCE_DIR + "test/"
 OUT_DIR = None
 OUT_DIR = None
-MISC_OUT_FILES = {}
 
 
 # List of common operation systems
 # List of common operation systems
 platforms = {"win7", "win10", "winxp", "win8.1", "macos", "linux", "win8", "winvista", "winnt", "win2000"}
 platforms = {"win7", "win10", "winxp", "win8.1", "macos", "linux", "win8", "winvista", "winnt", "win2000"}