Browse Source

reworked result paths
results are now stored in ID2T_result directory (still next to input)
renamed testresult files to include the testname
add option to create sub-directory for testresults for each attack-class
add option to create sub-directory for testresults for each test-function/case
refactored some testnames
add new directory to gitignore

Stefano Acquaviti 6 years ago
parent
commit
fadb3a8f91

+ 1 - 0
.gitignore

@@ -30,5 +30,6 @@ code_boost/src/cxx/CMakeLists.txt
 code_boost/src/cxx/cmake-build-debug/
 code_boost/src/SQLiteCpp/
 code_boost/src/cmake-build-debug/
+resources/test/ID2T_results
 
 run_tests

+ 9 - 0
code/ID2TLib/Controller.py

@@ -70,6 +70,15 @@ class Controller:
         print("Merging base pcap with single attack pcap...", end=" ")
         sys.stdout.flush()  # force python to print text immediately
         self.pcap_dest_path = self.pcap_file.merge_attack(attacks_pcap_path)
+
+        tmp_path_tuple = self.pcap_dest_path.rpartition("/")
+        result_dir = tmp_path_tuple[0] + tmp_path_tuple[1] + "ID2T_results/"
+        result_path = result_dir + tmp_path_tuple[2]
+
+        os.makedirs(result_dir, exist_ok=True)
+        os.rename(self.pcap_dest_path, result_path)
+        self.pcap_dest_path = result_path
+
         print("done.")
 
         # delete intermediate PCAP files

+ 41 - 0
code/ID2TLib/TestLibrary.py

@@ -67,3 +67,44 @@ def get_x86_nop(count, side_effect_free, char_filter):
     :return: a count of \x90
     """
     return b'\x90' * count
+
+
+def rename_test_result_files(controller, caller_function: str, attack_sub_dir=False, test_sub_dir=False):
+    """
+    :param controller: controller which created output files
+    :param caller_function: the name of the function which called the generic test
+    :param attack_sub_dir: create sub-directory for each attack-class if True
+    :param test_sub_dir: create sub-directory for each test-function/case if True
+    """
+    tmp_path_tuple = controller.pcap_dest_path.rpartition("_")
+    result_pcap_path = tmp_path_tuple[0] + tmp_path_tuple[1] + caller_function + "_" + tmp_path_tuple[2]
+
+    tmp_label_path_tuple = controller.label_manager.label_file_path.rpartition("_")
+    tmp_path_tuple = tmp_label_path_tuple[0].rpartition("_")
+    result_labels_path = tmp_path_tuple[0] + tmp_path_tuple[1] + caller_function + "_" + tmp_path_tuple[2]
+    result_labels_path = result_labels_path + tmp_label_path_tuple[1] + tmp_label_path_tuple[2]
+
+    if attack_sub_dir:
+        caller_attack = caller_function.replace("test_", "").partition("_")[0]
+        tmp_dir_tuple = result_pcap_path.rpartition("/")
+        result_dir = tmp_dir_tuple[0] + tmp_dir_tuple[1] + caller_attack + "/"
+        result_pcap_path = result_dir + tmp_dir_tuple[2]
+        os.makedirs(result_dir, exist_ok=True)
+
+        tmp_dir_tuple = result_labels_path.rpartition("/")
+        result_labels_path = result_dir + tmp_dir_tuple[2]
+
+    if test_sub_dir:
+        tmp_dir_tuple = result_pcap_path.rpartition("/")
+        result_dir = tmp_dir_tuple[0] + tmp_dir_tuple[1] + (caller_function.replace("test_", "")) + "/"
+        result_pcap_path = result_dir + tmp_dir_tuple[2]
+        os.makedirs(result_dir, exist_ok=True)
+
+        tmp_dir_tuple = result_labels_path.rpartition("/")
+        result_labels_path = result_dir + tmp_dir_tuple[2]
+
+    os.rename(controller.pcap_dest_path, result_pcap_path)
+    controller.pcap_dest_path = result_pcap_path
+
+    os.rename(controller.label_manager.label_file_path, result_labels_path)
+    controller.label_manager.label_file_path = result_labels_path

+ 14 - 3
code/Test/GenericTest.py

@@ -1,5 +1,6 @@
 import random
 import unittest
+import inspect
 
 import ID2TLib.Controller as Ctrl
 import ID2TLib.TestLibrary as Lib
@@ -7,13 +8,23 @@ import ID2TLib.TestLibrary as Lib
 
 class GenericTest(unittest.TestCase):
 
-    def generic_test(self, attack_args, sha_checksum, seed=5, cleanup=True, pcap=Lib.test_pcap, flag_write_file=False,
-                     flag_recalculate_stats=False, flag_print_statistics=False):
+    def generic_test(self, attack_args, sha_checksum, seed=5, cleanup=False, pcap=Lib.test_pcap, flag_write_file=False,
+                     flag_recalculate_stats=False, flag_print_statistics=False, attack_sub_dir=True, test_sub_dir=True):
         # TODO: move seed to attacks
         random.seed(seed)
         controller = Ctrl.Controller(pcap_file_path=pcap, do_extra_tests=False)
         controller.load_pcap_statistics(flag_write_file, flag_recalculate_stats, flag_print_statistics)
         controller.process_attacks(attack_args)
-        self.assertEqual(Lib.get_sha256(controller.pcap_dest_path), sha_checksum)
+
+        caller_function = inspect.stack()[1].function
+
+        try:
+            self.assertEqual(Lib.get_sha256(controller.pcap_dest_path), sha_checksum)
+        except self.failureException:
+            Lib.rename_test_result_files(controller, caller_function, attack_sub_dir, test_sub_dir)
+            raise
+
         if cleanup:
             Lib.clean_up(controller)
+        else:
+            Lib.rename_test_result_files(controller, caller_function, attack_sub_dir, test_sub_dir)

+ 1 - 1
code/Test/test_EternalBlue.py

@@ -15,7 +15,7 @@ Attack/EternalBlueExploit.py       246     10    96%   62, 72, 112, 119, 126-127
 
 class UnitTestEternalBlue(GenTest.GenericTest):
 
-    def test_default(self):
+    def test_eternalblue_default(self):
         self.generic_test([['EternalBlueExploit']], sha_default)
 
 

+ 1 - 1
code/Test/test_Joomla.py

@@ -15,7 +15,7 @@ Attack/JoomlaRegPrivExploit.py     127      4    97%   62, 71, 116, 123
 
 class UnitTestJoomla(GenericTest.GenericTest):
 
-    def test_default(self):
+    def test_joomla_default(self):
         self.generic_test([['JoomlaRegPrivExploit']], sha_default)
 
 

+ 6 - 6
code/Test/test_SMBLoris.py

@@ -20,26 +20,26 @@ Attack/SMBLorisAttack.py           128      4    97%   67, 72, 149, 182
 
 class UnitTestSMBLoris(GenericTest):
 
-    def test_default(self):
+    def test_smbloris_default(self):
         self.generic_test([['SMBLorisAttack']], sha_default)
 
-    def test_one_attacker(self):
+    def test_smbloris_one_attacker(self):
         self.generic_test([['SMBLorisAttack', 'ip.src=192.168.1.240', 'ip.dst=192.168.1.210']], sha_one_attacker)
 
-    def test_ips_in_pcap(self):
+    def test_smbloris_ips_in_pcap(self):
         ip_src = 'ip.src='+Lib.test_pcap_ips[0]
         ip_dst = 'ip.dst='+Lib.test_pcap_ips[1]
         self.generic_test([['SMBLorisAttack', ip_src, ip_dst]], sha_ips_in_pcap)
 
-    def test_sixteen_attackers(self):
+    def test_smbloris_sixteen_attackers(self):
         self.generic_test([['SMBLorisAttack', 'ip.dst=192.168.1.210', 'attackers.count=16']], sha_sixteen_attackers)
 
     @mock.patch('ID2TLib.Statistics.Statistics.get_most_used_ip_address')
-    def test_two_most_used_ips(self, mock_most_used_ip_address):
+    def test_smbloris_two_most_used_ips(self, mock_most_used_ip_address):
         mock_most_used_ip_address.return_value = Lib.test_pcap_ips
         self.generic_test([['SMBLorisAttack']], sha_default)
 
-    def test_same_ip_src_dst(self):
+    def test_smbloris_same_ip_src_dst(self):
         with self.assertRaises(SystemExit):
             self.generic_test([['SMBLorisAttack', 'ip.src=192.168.1.240', 'ip.dst=192.168.1.240']], sha_default)
 

+ 10 - 10
code/Test/test_SMBScan.py

@@ -23,53 +23,53 @@ Attack/SMBScanAttack.py            239      9    96%   65, 73-74, 82, 193, 210-2
 
 class UnitTestSMBScan(GenericTest.GenericTest):
 
-    def test_default(self):
+    def test_smbscan_default(self):
         with mock.patch("ID2TLib.Utility.get_rnd_os", return_value="win7"):
             self.generic_test([['SMBScanAttack']], sha_default)
 
-    def test_one_victim_linux(self):
+    def test_smbscan_one_victim_linux(self):
         with mock.patch("ID2TLib.Utility.get_rnd_os", return_value="linux"):
             self.generic_test([['SMBScanAttack', 'ip.src=192.168.178.1', 'ip.dst=192.168.178.10']],
                               sha_one_victim_linux)
 
-    def test_victim_range_winxp_hosting(self):
+    def test_smbscan_victim_range_winxp_hosting(self):
         with mock.patch("ID2TLib.Utility.get_rnd_os", return_value="winxp"):
             self.generic_test([['SMBScanAttack', 'ip.src=192.168.178.1', 'ip.dst=192.168.178.5',
                                 'ip.dst.end=192.168.178.10', 'hosting.ip=192.168.178.5']],
                               sha_victim_range_winxp_hosting)
 
-    def test_multiple_victims_macos(self):
+    def test_smbscan_multiple_victims_macos(self):
         with mock.patch("ID2TLib.Utility.get_rnd_os", return_value="macos"):
             self.generic_test([['SMBScanAttack', 'ip.src=192.168.178.1',
                                 'ip.dst=192.168.178.10,192.168.178.15,192.168.178.20',
                                 'hosting.ip=192.168.178.15,192.168.178.20']], sha_multiple_victims_macos)
 
-    def test_invalid_smb_version(self):
+    def test_smbscan_invalid_smb_version(self):
         with self.assertRaises(SystemExit):
             self.generic_test([['SMBScanAttack', 'protocol.version=42']], 'somehash')
 
-    def test_invalid_smb_platform(self):
+    def test_smbscan_invalid_smb_platform(self):
         with self.assertRaises(SystemExit):
             self.generic_test([['SMBScanAttack', 'hosting.version=1337']], 'somehash')
 
-    def test_port_shuffle(self):
+    def test_smbscan_port_shuffle(self):
         with mock.patch("ID2TLib.Utility.get_rnd_os", return_value="win7"):
             self.generic_test([['SMBScanAttack', 'ip.src=192.168.178.1', 'ip.dst=192.168.178.5',
                                 'ip.dst.end=192.168.178.10', 'hosting.ip=192.168.178.5', 'port.src.shuffle=false']],
                               sha_port_shuffle)
 
-    def test_dest_mac_only(self):
+    def test_smbscan_dest_mac_only(self):
         with mock.patch("ID2TLib.Utility.get_rnd_os", return_value="win7"):
             self.generic_test([['SMBScanAttack', 'ip.src=192.168.178.1',
                                 'mac.dst=00:0C:29:9C:70:64']], sha_dest_mac_only)
 
-    def test_src_ip_shuffle(self):
+    def test_smbscan_src_ip_shuffle(self):
         with mock.patch("ID2TLib.Utility.get_rnd_os", return_value="win7"):
             self.generic_test([['SMBScanAttack', 'ip.src=192.168.178.1', 'ip.dst=192.168.178.5',
                                 'ip.dst.end=192.168.178.10', 'hosting.ip=192.168.178.5', 'ip.src.shuffle=True']],
                               sha_ip_src_shuffle)
 
-    def test_smb2(self):
+    def test_smbscan_smb2(self):
 
         with mock.patch("ID2TLib.Utility.get_rnd_os", return_value="linux"):
             self.generic_test([['SMBScanAttack', 'ip.src=192.168.178.1', 'ip.dst=192.168.178.5',

+ 1 - 1
code/Test/test_SQLi.py

@@ -15,7 +15,7 @@ Attack/SQLiAttack.py               159      5    97%   62, 71, 113, 120, 245
 
 class UnitTestSQLi(GenericTest.GenericTest):
 
-    def test_default(self):
+    def test_sqli_default(self):
         self.generic_test([['SQLiAttack']], sha_default)