Browse Source

fix seed param

move seed from attack to id2t params
fix "seed required to run any attack"
refactoring
Jens Keim 6 years ago
parent
commit
e22234e1e8

+ 0 - 3
code/Attack/AttackParameters.py

@@ -45,9 +45,6 @@ class Parameter(Enum):
     CUSTOM_PAYLOAD = 'custom.payload'  # custom payload for ftp exploits
     CUSTOM_PAYLOAD_FILE = 'custom.payload.file'  # file that contains custom payload for ftp exploits
 
-    # benchmark
-    SEED = 'seed'
-
 
 class ParameterTypes(Enum):
     """

+ 3 - 4
code/Attack/BaseAttack.py

@@ -41,7 +41,7 @@ class BaseAttack(metaclass=ABCMeta):
         self.attack_description = description
         self.attack_type = attack_type
         self.params = {}
-        self.supported_params = {Parameter.SEED: ParameterTypes.TYPE_INTEGER_POSITIVE}
+        self.supported_params = {}
         self.attack_start_utime = 0
         self.attack_end_utime = 0
 
@@ -249,9 +249,8 @@ class BaseAttack(metaclass=ABCMeta):
     #########################################
 
     def set_seed(self, seed: int):
-        if seed:
-            self.add_param_value(Parameter.SEED, seed)
-            random.seed(self.get_param_value(Parameter.SEED))
+        if isinstance(seed, int):
+            random.seed(seed)
 
     def add_param_value(self, param, value):
         """

+ 9 - 2
code/CLI.py

@@ -61,7 +61,8 @@ class CLI(object):
                             help='query the statistics database. If no query is provided, the application enters query mode.')
         parser.add_argument('-t', '--extraTests', help='perform extra tests on the input pcap file, including calculating IP entropy'
                                                        'in interval-wise, TCP checksum, and checking payload availability.', action='store_true')
-
+        parser.add_argument('-S', '--randomSeed', action='append', help='sets random seed for testing or benchmarking',
+                            nargs='+', default=[])
 
         # Attack arguments
         parser.add_argument('-a', '--attack', metavar="ATTACK", action='append',
@@ -141,10 +142,14 @@ class CLI(object):
         if self.args.plot is not None:
             controller.create_statistics_plot(self.args.plot)
 
+        # Check random seed
+        if not isinstance(self.args.randomSeed, list):
+            self.args.randomSeed = [self.args.randomSeed]
+
         # Process attack(s) with given attack params
         if self.args.attack is not None:
             # If attack is present, load attack with params
-            controller.process_attacks(self.args.attack)
+            controller.process_attacks(self.args.attack, self.args.randomSeed)
 
         # Parameter -q without arguments was given -> go into query loop
         if self.args.query == [None]:
@@ -153,6 +158,7 @@ class CLI(object):
         elif self.args.query is not None:
             controller.process_db_queries(self.args.query, True)
 
+
 def main(args):
     """
     Creates a new CLI object and invokes the arguments parsing.
@@ -163,6 +169,7 @@ def main(args):
     # Check arguments
     cli.parse_arguments(args)
 
+
 # Uncomment to enable calling by terminal
 if __name__ == '__main__':
     main(sys.argv[1:])

+ 11 - 9
code/ID2TLib/AttackController.py

@@ -20,8 +20,12 @@ class AttackController:
 
         self.current_attack = None
         self.added_attacks = []
+        self.seed = None
 
-    def create_attack(self, attack_name: str, seed: int):
+    def set_seed(self, seed: int):
+        self.seed = seed
+
+    def create_attack(self, attack_name: str, seed=None):
         """
         Creates dynamically a new class instance based on the given attack_name.
         :param attack_name: The name of the attack, must correspond to the attack's class name.
@@ -37,7 +41,8 @@ class AttackController:
         self.current_attack = attack_class()
         # Initialize the parameters of the attack with defaults or user supplied values.
         self.current_attack.set_statistics(self.statistics)
-        self.current_attack.set_seed(seed=seed)
+        if seed is not None:
+            self.current_attack.set_seed(seed=seed)
         self.current_attack.init_params()
         # Record the attack
         self.added_attacks.append(self.current_attack)
@@ -50,20 +55,17 @@ class AttackController:
         :param params: The parameters for attack customization, see attack class for supported params.
         :return: The file path to the created pcap file.
         """
+        self.create_attack(attack, self.seed)
+
         print("Validating and adding attack parameters.")
+
+        # Add attack parameters if provided
         params_dict = []
         if isinstance(params, list) and params:
             # Convert attack param list into dictionary
             for entry in params:
                 params_dict.append(entry.split('='))
             params_dict = dict(params_dict)
-
-        seed = params_dict['seed']
-
-        self.create_attack(attack, seed)
-
-        # Add attack parameters if provided
-        if params_dict is not []:
             # Check if Parameter.INJECT_AT_TIMESTAMP and Parameter.INJECT_AFTER_PACKET are provided at the same time
             # if TRUE: delete Paramter.INJECT_AT_TIMESTAMP (lower priority) and use Parameter.INJECT_AFTER_PACKET
             if (Parameter.INJECT_AFTER_PACKET.value in params_dict) and (

+ 6 - 1
code/ID2TLib/Controller.py

@@ -18,6 +18,7 @@ class Controller:
         self.pcap_dest_path = ''
         self.written_pcaps = []
         self.do_extra_tests = do_extra_tests
+        self.seed = None
 
         # Initialize class instances
         print("Input file: %s" % self.pcap_src_path)
@@ -39,7 +40,7 @@ class Controller:
         """
         self.statistics.load_pcap_statistics(flag_write_file, flag_recalculate_stats, flag_print_statistics)
 
-    def process_attacks(self, attacks_config: list):
+    def process_attacks(self, attacks_config: list, seeds=[]):
         """
         Creates the attack based on the attack name and the attack parameters given in the attacks_config. The
         attacks_config is a list of attacks, e.g.
@@ -49,9 +50,13 @@ class Controller:
         :param attacks_config: A list of attacks with their attack parameters.
         """
         # load attacks sequentially
+        i = 0
         for attack in attacks_config:
+            if len(seeds) > i:
+                self.attack_controller.set_seed(seed=seeds[i][0])
             temp_attack_pcap = self.attack_controller.process_attack(attack[0], attack[1:])
             self.written_pcaps.append(temp_attack_pcap)
+            i += 1
 
         # merge attack pcaps to get single attack pcap
         if len(self.written_pcaps) > 1:

+ 1 - 2
code/Test/GenericTest.py

@@ -12,8 +12,7 @@ class GenericTest(unittest.TestCase):
         # TODO: move seed to attacks
         controller = Ctrl.Controller(pcap_file_path=pcap, do_extra_tests=False)
         controller.load_pcap_statistics(flag_write_file, flag_recalculate_stats, flag_print_statistics)
-        attack_args[0].append("seed=" + str(seed))
-        controller.process_attacks(attack_args)
+        controller.process_attacks(attack_args, [[seed]])
 
         caller_function = inspect.stack()[1].function