瀏覽代碼

Code cleanup and moving files into fitting directory

dustin.born 7 年之前
父節點
當前提交
70edad3d28

+ 4 - 1
code/Attack/AttackParameters.py

@@ -36,7 +36,9 @@ class Parameter(Enum):
     PORT_DEST_ORDER_DESC = 'port.dst.order-desc'  # uses a descending port order instead of a ascending order
     IP_SOURCE_RANDOMIZE = 'ip.src.shuffle'  # randomizes the sources IP address if a list of IP addresses is given
     PORT_SOURCE_RANDOMIZE = 'port.src.shuffle'  # randomizes the source port if a list of sources ports is given
-
+    # recommended type: Filepath ------------------------------------
+    FILE_CSV = 'file.csv'
+    FILE_XML = 'file.xml'
 
 class ParameterTypes(Enum):
     """
@@ -52,3 +54,4 @@ class ParameterTypes(Enum):
     TYPE_FLOAT = 6
     TYPE_PACKET_POSITION = 7  # used to derive timestamp from parameter INJECT_AFTER_PACKET
     TYPE_DOMAIN = 8
+    TYPE_FILEPATH = 9

+ 11 - 0
code/Attack/BaseAttack.py

@@ -241,6 +241,15 @@ class BaseAttack(metaclass=ABCMeta):
         domain = re.match('^(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+$', val)
         return (domain is not None)
 
+    @staticmethod
+    def _is_filepath(val: str):
+        '''
+        Verifies that the given string points to an existing file
+
+        :param filepath: The filepath as string
+        :return: True if the file at the given location exists, otherwise False
+        '''
+        return os.path.isfile(val)
 
     #########################################
     # HELPER METHODS
@@ -321,6 +330,8 @@ class BaseAttack(metaclass=ABCMeta):
                 value = (ts / 1000000)  # convert microseconds from getTimestampMuSec into seconds
         elif param_type == ParameterTypes.TYPE_DOMAIN:
             is_valid = self._is_domain(value)
+        elif param_type == ParameterTypes.TYPE_FILEPATH:
+            is_valid = self._is_filepath(value)
 
         # add value iff validation was successful
         if is_valid:

+ 51 - 0
code/ID2TLib/FileUtils.py

@@ -0,0 +1,51 @@
+import xml.etree.ElementTree as ElementTree
+import csv
+
+
+def parse_xml(filepath: str):
+	'''
+	Parses an XML File
+	It is assumed, that packets are placed on the second hierarchical level and packetinformation is encoded as attributes
+	
+	:param filepath: the path to the XML file to be parsed
+ 	:return: a List of Dictionaries, each Dictionary contains the information of one packet
+	'''
+
+	tree = ElementTree.parse(filepath)
+	root = tree.getroot()
+
+	#Convert Tree to List of Dictionaries
+	packets = []
+	for child in root:
+		packets.append(child.attrib)
+
+	return packets
+
+def parse_csv_to_xml(filepath: str):
+	'''
+	Converts a CSV file into an XML file. Every entry is converted to a child with respective attributes of the root node
+	
+	:param filepath: the path to the CSV file to be parsed
+ 	:return: a path to the newly created XML file 
+	'''
+
+	filename = filepath[:filepath.rfind(".")]
+	# build a tree structure
+	root = ElementTree.Element(filename)
+
+	# parse the csvFile into reader
+	with open(filepath, "rt") as csvFile:
+	    reader = csv.reader(csvFile, delimiter=",")
+		# loop through the parsed file, creating packet-elements with the structure of the csvFile as attributes
+	    for line in reader:
+	        packet = ElementTree.SubElement(root, "packet")
+	        for element in line:
+	        	element = element.replace(" ", "")
+	        	key, value = element.split(":")
+		        packet.attrib[key] = value
+
+	# writing the ElementTree into the .xml file
+	tree = ElementTree.ElementTree(root)
+	filepath = filename + ".xml"
+	tree.write(filepath)
+	return filepath

+ 1 - 0
code/Iteration1/IPPacketGenerator.py → code/ID2TLib/IPPacketGenerator.py

@@ -6,6 +6,7 @@ def generate_ip_packet(ip_src:str="192.168.64.32", ip_dst:str="192.168.64.48", m
 	"""
     Builds an IP packet with the values specified by the caller. If a parameter was not specified by the caller, 
     it is set to a default value.
+    
     :param ip_src: the source IP address of the IP header
     :param ip_dst the destination IP address of the IP header
     :param mac_src: the source MAC address of the MAC header

+ 0 - 27
code/Iteration1/CSVParser.py

@@ -1,27 +0,0 @@
-import xml.etree.ElementTree as ET
-import csv
-
-# def parseCSV:
-
-def parse_csv(file: str):
-	# build a tree structure
-	root = ET.Element("mmcommunication")
-
-	# parse the csvFile into reader
-	with open(file) as csvFile:
-	    reader = csv.reader(csvFile, delimiter=",")
-		# loop through the parsed file, creating packet-elements with the structure of the csvFile as attributes
-	    for line in reader:
-	        packet = ET.SubElement(root, "packet")
-	        for element in line:
-	        	element = element.replace(" ", "")
-	        	splitel = element.split(":")
-	        	key, value = splitel[0], splitel[1]
-		        packet.attrib[key] = value
-
-	# writing the ElementTree into the .xml file
-	tree = ET.ElementTree(root)
-	tree.write("ExampleCSVBotnetCommunication.xml")
-
-
-parse_csv("ExampleCSVBotnetCommunication.txt")

+ 0 - 15
code/Iteration1/ParseXML.py

@@ -1,15 +0,0 @@
-import xml.etree.ElementTree as ET
-
-# Parses an XML File at the given location
-# It is assumed, that packets are placed on the second hierarchical level and packetinformation encoded as attributes
-# Returns a List of Dictionaries, each Dictionary contains the information for one packet
-def ParseXML(path_xml):
-	tree = ET.parse(path_xml)
-	root = tree.getroot()
-
-	#Convert Tree to List of Dictionaries
-	packets = []
-	for child in root:
-		packets.append(child.attrib)
-
-	return packets