Browse Source

Improve message selection without NAT.
Put created trace XML from input CSV in the directory of the input PCAP.

dustin.born 7 years ago
parent
commit
95f5ada453

+ 2 - 5
code/Attack/MembersMgmtCommAttack.py

@@ -148,9 +148,6 @@ class MembersMgmtCommAttack(BaseAttack.BaseAttack):
         if messages == []:
             return 0, []
 
-        # for msg in messages:
-        #     print(msg)
-
         # Setup (initial) parameters for packet creation loop
         BUFFER_SIZE = 1000
         pkt_gen = PacketGenerator()
@@ -163,7 +160,6 @@ class MembersMgmtCommAttack(BaseAttack.BaseAttack):
         limit_duration = self.get_param_value(Param.ATTACK_DURATION)
         duration = 0
         path_attack_pcap = None
-
         # create packets to write to PCAP file
         for msg in messages:
             # retrieve the source and destination configurations
@@ -454,7 +450,7 @@ class MembersMgmtCommAttack(BaseAttack.BaseAttack):
                     Hello_times[(req_msg.src, req_msg.dst)] = respns_msg.time
                 else:
                     Hello_times[(req_msg.dst, req_msg.src)] = respns_msg.time
-        
+                    
         # create port configurations for the bots
         for bot in bot_configs:
             bot_configs[bot]["Port"] = gen_random_server_port()    
@@ -469,6 +465,7 @@ class MembersMgmtCommAttack(BaseAttack.BaseAttack):
         final_messages = []
         messages = sorted(messages, key=lambda msg: msg.time)
         new_id = 0
+
         for msg in messages:
             type_src, type_dst = bot_configs[msg.src]["Type"], bot_configs[msg.dst]["Type"]
             id_src, id_dst = msg.src, msg.dst

+ 16 - 0
code/ID2TLib/CommunicationProcessor.py

@@ -232,6 +232,22 @@ class CommunicationProcessor():
                 del(prev_reqs[msg_str])
                 msg_id += 1
 
+            elif msg_type == MessageType.TIMEOUT and id_src in local_init_ids and not self.nat:
+                # convert the abstract message into a message object to handle it better
+                msg_str = "{0}-{1}".format(id_dst, id_src)
+                # find the request message ID for this response and set its reference index
+                refer_idx = prev_reqs.get(msg_str)
+                if refer_idx is not None:
+                    msgs[refer_idx].refer_msg_id = msg_id
+                    if msgs[refer_idx].type == MessageType.SALITY_NL_REQUEST:
+                        msg = Message(msg_id, id_src, id_dst, MessageType.SALITY_NL_REPLY, time, refer_idx)
+                    else:
+                        msg = Message(msg_id, id_src, id_dst, MessageType.SALITY_HELLO_REPLY, time, refer_idx)
+                    msgs.append(msg)
+                    # remove the request to this response from storage
+                    del(prev_reqs[msg_str])
+                    msg_id += 1
+
         # store the retrieved information in this object for later use
         self.respnd_ids = sorted(respnd_ids)
         self.external_init_ids = sorted(external_init_ids)

+ 25 - 1
code/ID2TLib/Controller.py

@@ -52,11 +52,23 @@ class Controller:
         input dataset.
         :param attacks_config: A list of attacks with their attack parameters.
         """
+        # note if new xml file has been created by MembersMgmtCommAttack
+        created_xml = None
         # load attacks sequentially
         for attack in attacks_config:
+            # check if new xml file has been created by MembersMgmtCommAttack
+            if attack[0] == "MembersMgmtCommAttack":
+                for param in attack[1:]:
+                    key, value = param.split("=")
+                    if key == "file.csv":
+                        if os.path.isfile(value):
+                            created_xml, _ = os.path.splitext(value)
+                            created_xml += ".xml"
+                            break
             temp_attack_pcap = self.attack_controller.process_attack(attack[0], attack[1:])
             self.written_pcaps.append(temp_attack_pcap)
 
+
         # merge attack pcaps to get single attack pcap
         if len(self.written_pcaps) > 1:
             print("\nMerging temporary attack pcaps into single pcap file...", end=" ")
@@ -89,9 +101,21 @@ class Controller:
 
         # write label file with attacks
         self.label_manager.write_label_file(self.pcap_dest_path)
+        if created_xml:
+            pcap_dir = os.path.splitext(self.pcap_dest_path)[0]
+            if "/" in pcap_dir:
+                pcap_dir = "/".join(pcap_dir.split("/")[:-1])
+            xml_name = os.path.splitext(created_xml)[0] + ".xml"
+            if "/" in xml_name:
+                xml_name = xml_name.split("/")[-1]
+            new_xml_path = pcap_dir + "/" + xml_name
+            os.rename(created_xml, new_xml_path)
 
         # print status message
-        print('\nOutput files created: \n', self.pcap_dest_path, '\n', self.label_manager.label_file_path)
+        if created_xml:
+            print('\nOutput files created: \n', self.pcap_dest_path, '\n', self.label_manager.label_file_path, '\n', new_xml_path)
+        else:
+            print('\nOutput files created: \n', self.pcap_dest_path, '\n', self.label_manager.label_file_path)
 
     def process_db_queries(self, query, print_results=False):
         """

+ 4 - 3
code/ID2TLib/FileUtils.py

@@ -1,6 +1,6 @@
 import xml.etree.ElementTree as ElementTree
 import csv
-
+import os
 
 def parse_xml(filepath: str):
 	'''
@@ -29,9 +29,10 @@ def parse_csv_to_xml(filepath: str):
  	:return: a path to the newly created XML file 
 	'''
 
-	filename = filepath[:filepath.rfind(".")]
+	filename = os.path.splitext(filepath)[0]
 	# build a tree structure
-	root = ElementTree.Element(filename)
+	root = ElementTree.Element("trace")
+	root.attrib["path"] = filename
 
 	# parse the csvFile into reader
 	with open(filepath, "rt") as csvFile: