Browse Source

- Fixes issue #16: After loading a label's file, the application crashes

Patrick Jattke 7 years ago
parent
commit
5f4ad34c2b
2 changed files with 56 additions and 25 deletions
  1. 18 16
      code/CLI.py
  2. 38 9
      code/ID2TLib/LabelManager.py

+ 18 - 16
code/CLI.py

@@ -109,22 +109,24 @@ def main(args):
 
 
 # Uncomment to enable calling by terminal
-#if __name__ == '__main__':
-#    main(sys.argv[1:])
-
 if __name__ == '__main__':
-    FILE = ['-i', '/mnt/hgfs/datasets/95M.pcap']
-
-    ATTACK = ['-a', 'PortscanAttack', 'ip.src=10.2.2.4', 'mac.dst=05:AB:47:B5:19:11',
-              'inject.at-timestamp=1449038705.316721', 'attack.note=First portscan Attack']
-    ATTACK2 = ['-a', 'PortscanAttack', 'ip.dst=193.133.122.23, ip.src=192.124.34.12', 'inject.after-pkt=34']
-
-    STATS_RECALC = ['-r']
-    STATS_PRINT = ['-s']
-
-    QUERY_MODE_LOOP = ['-q']
-    QUERY_DB = ['-q', 'most_used(ttlValue)']
-
-    main(FILE + ATTACK)
+    main(sys.argv[1:])
+
+# if __name__ == '__main__':
+#     FILE = ['-i', '/mnt/hgfs/datasets/95M.pcap']
+#     FILE2 = ['-i', '/mnt/hgfs/datasets/95M_20161103-185151.pcap']
+#
+#
+#     ATTACK = ['-a', 'PortscanAttack', 'ip.src=10.2.2.4', 'mac.dst=05:AB:47:B5:19:11',
+#               'inject.at-timestamp=1449038705.316721', 'attack.note=Portscan2']
+#     ATTACK2 = ['-a', 'PortscanAttack', 'ip.dst=193.133.122.23, ip.src=192.124.34.12', 'inject.after-pkt=34']
+#
+#     STATS_RECALC = ['-r']
+#     STATS_PRINT = ['-s']
+#
+#     QUERY_MODE_LOOP = ['-q']
+#     QUERY_DB = ['-q', 'most_used(ttlValue)']
+#
+#     main(FILE2 + ATTACK)
 
     # main(['-c', '/home/pjattke/Thesis/development/code/config'])

+ 38 - 9
code/ID2TLib/LabelManager.py

@@ -120,21 +120,50 @@ class LabelManager:
         Loads the labels from an already existing label XML file located at label_file_path (set by constructor).
 
         """
+
+        def get_value_from_node(node, tag_name, *child_number):
+            """
+            Returns the value located in the tag specified by tag_name from a given node. Walks therefor the
+            node's children along as indicated by child_number, e.g., childNumber = (1,2,) first goes to the 1st child, and
+            then to the 2nd child of the first child -> elem.childNodes[1].childNodes[2].
+            """
+            elem = node.getElementsByTagName(tag_name)
+            if len(elem) == 1:
+                elem = elem[0]
+                for c in child_number:
+                    if len(elem.childNodes) > 0:
+                        elem = elem.childNodes[c]
+                    else:
+                        return ""
+                return elem.data
+            else:
+                return ""
+
         print("Label file found. Loading labels...")
-        dom = parse(self.label_file_path)
+        try:
+            dom = parse(self.label_file_path)
+        except Exception:
+            print('ERROR: Provided label file could not be parsed. Ignoring label file')
+            return
 
         # Check if version of parser and version of file match
-        version = dom.getElementsByTagName(self.TAG_ROOT)[0].getAttribute(self.ATTR_VERSION)
-        if not version == self.ATTR_VERSION_VALUE:
-            raise ValueError(
-                "The file " + self.label_file_path + " was created by another version of ID2TLib.LabelManager")
+        version = dom.getElementsByTagName(self.TAG_ROOT)
+        if len(version) > 0:
+            version = version[0].getAttribute(self.ATTR_VERSION)
+            if version == [] or not version == self.ATTR_VERSION_VALUE:
+                print(
+                    "The file " + self.label_file_path + " was created by another version of ID2TLib.LabelManager. Ignoring label file.")
 
         # Parse attacks from XML file
         attacks = dom.getElementsByTagName(self.TAG_ATTACK)
+        count_labels = 0
         for a in attacks:
-            attack_name = a.childNodes[1].firstChild.data
-            attack_note = a.childNodes[3].firstChild.data
-            timestamp_start = a.childNodes[5].childNodes[1].firstChild.data
-            timestamp_end = a.childNodes[7].childNodes[1].firstChild.data
+            attack_name = get_value_from_node(a, self.TAG_ATTACK_NAME, 0)
+            attack_note = get_value_from_node(a, self.TAG_ATTACK_NOTE, 0)
+            timestamp_start = get_value_from_node(a, self.TAG_TIMESTAMP_START, 1, 0)
+            timestamp_end = get_value_from_node(a, self.TAG_TIMESTAMP_END, 1, 0)
             label = Label.Label(attack_name, float(timestamp_start), float(timestamp_end), attack_note)
             self.labels.append(label)
+            count_labels += 1
+
+        print("Read " + str(count_labels) + " label(s) successfully.")