Browse Source

split summary into post_injection and new_db, moved them to Statistics.py, added support for multiple attacks

Jonathan Speth 6 years ago
parent
commit
d07672d77c
3 changed files with 57 additions and 30 deletions
  1. 0 29
      code/Core/AttackController.py
  2. 4 1
      code/Core/Controller.py
  3. 53 0
      code/Core/Statistics.py

+ 0 - 29
code/Core/AttackController.py

@@ -195,32 +195,3 @@ class AttackController:
         """
         for param_key, param_value in params.items():
             self.current_attack.add_param_value(param_key, param_value)
-
-    def stats_summary(self, pcap):
-        """
-        Prints a summary of important statistics
-        :param pcap: path of the pcap file whose statistics are to be printed
-        :return: None
-        """
-        #self.statistics.pcap_filepath = pcap
-        #self.statistics.load_pcap_statistics(False, True, False)
-
-        total_packet_count = self.statistics.get_packet_count() + self.total_packets
-        added_packets_share = self.total_packets / total_packet_count * 100
-        pdu_count = self.statistics.process_db_query("SELECT SUM(pktCount) FROM unrecognized_pdus")
-        pdu_share = pdu_count / total_packet_count * 100
-        last_pdu_timestamp = self.statistics.process_db_query(
-            "SELECT MAX(timestampLastOccurrence) FROM unrecognized_pdus")
-        timespan = self.statistics.get_capture_duration()
-
-        summary = [("Total packet count", total_packet_count, "packets"),
-                   ("Added packet count", self.total_packets, "packets"),
-                   ("Share of added packets", added_packets_share, "%"),
-                   ("Unknown pdu count", pdu_count, "pdus"),
-                   ("Share of unknown pdus", pdu_share, "%"),
-                   ("Last unknown pdu", last_pdu_timestamp, ""),
-                   ("Capture duration", timespan, "seconds")]
-
-        print("\nPCAP FILE STATISTICS SUMMARY  ------------------------------")
-        self.statistics.write_list(summary, print, "")
-        print("------------------------------------------------------------")

+ 4 - 1
code/Core/Controller.py

@@ -23,6 +23,7 @@ class Controller:
         self.do_extra_tests = do_extra_tests
         self.seed = None
         self.durations = []
+        self.added_packets = 0
 
         # Initialize class instances
         print("Input file: %s" % self.pcap_src_path)
@@ -65,6 +66,8 @@ class Controller:
                 self.attack_controller.set_seed(seed=seeds[i][0])
             temp_attack_pcap, duration = self.attack_controller.process_attack(attack[0], attack[1:], time)
             self.durations.append(duration)
+            self.added_packets += self.attack_controller.total_packets
+            self.statistics.stats_summary_post_attack(self.added_packets)
             self.written_pcaps.append(temp_attack_pcap)
             i += 1
 
@@ -111,7 +114,7 @@ class Controller:
         print('\nOutput files created: \n', self.pcap_dest_path, '\n', self.label_manager.label_file_path)
 
         # print summary statistics
-        self.attack_controller.stats_summary(self.pcap_dest_path)
+        self.statistics.stats_summary_post_attack(self.added_packets)
 
     def process_db_queries(self, query, print_results=False):
         """

+ 53 - 0
code/Core/Statistics.py

@@ -63,6 +63,10 @@ class Statistics:
             self.pcap_proc.collect_statistics()
             self.pcap_proc.write_to_database(self.path_db)
             outstring_datasource = "by PCAP file processor."
+
+            # only print summary of new db if -s flag not set
+            if not flag_print_statistics:
+                self.stats_summary_new_db()
         else:
             outstring_datasource = "from statistics database."
 
@@ -1011,3 +1015,52 @@ class Statistics:
         # ip_dst_out_path = plot_ip_dst('.' + format)
 
         print("Saved plots in the input PCAP directory.")
+
+    def stats_summary_post_attack(self, added_packets):
+        """
+        Prints a summary of relevant statistics after an attack is injected
+
+        :param added_packets: sum of packets added by attacks, gets updated if more than one attack
+        :return: None
+        """
+
+        total_packet_count = self.get_packet_count() + added_packets
+        added_packets_share = added_packets / total_packet_count * 100
+        timespan = self.get_capture_duration()
+
+        summary = [("Total packet count", total_packet_count, "packets"),
+                   ("Added packet count", added_packets, "packets"),
+                   ("Share of added packets", added_packets_share, "%"),
+                   ("Capture duration", timespan, "seconds")]
+
+        print("\nPOST INJECTION STATISTICS SUMMARY  ----------------------------")
+        self.write_list(summary, print, "")
+        print("------------------------------------------------------------")
+
+    def stats_summary_new_db(self):
+        """
+        Prints a summary of relevant statistics when a new db is created
+
+        :return: None
+        """
+
+        self.file_info = self.stats_db.get_file_info()
+        print("\nNew database has been generated, printing statistics summary... ")
+        total_packet_count = self.get_packet_count()
+        pdu_count = self.process_db_query("SELECT SUM(pktCount) FROM unrecognized_pdus")
+        pdu_share = pdu_count / total_packet_count * 100
+        last_pdu_timestamp = self.process_db_query(
+            "SELECT MAX(timestampLastOccurrence) FROM unrecognized_pdus")
+        timespan = self.get_capture_duration()
+
+        summary = [("Total packet count", total_packet_count, "packets"),
+                   ("Recognized packet count", total_packet_count - pdu_count, "packets"),
+                   ("Unrecognized packet count", pdu_count, "PDUs"),
+                   ("Share of recognized packets", 100 - pdu_share, "%"),
+                   ("Share of unrecognized packets", pdu_share, "%"),
+                   ("Last unknown pdu", last_pdu_timestamp, ""),
+                   ("Capture duration", timespan, "seconds")]
+
+        print("\nPCAP FILE STATISTICS SUMMARY  ------------------------------")
+        self.write_list(summary, print, "")
+        print("------------------------------------------------------------")