Esempio n. 1
0
    def get_intermediate_timestamp(self,
                                   divisor: int = 2,
                                   factor: int = 1) -> int:
        """
        Calculates a timestamp, which lays within the input pcap.
        By default a timestamp in the middle of the pcap.

        :param divisor: The number of intervals in which the pcap length should be divided.
        :param factor: The number of the interval from which to get the timestamp.
        :return: The calculated timestamp.
        """
        start = Util.get_timestamp_from_datetime_str(
            self.statistics.get_pcap_timestamp_start())
        end = Util.get_timestamp_from_datetime_str(
            self.statistics.get_pcap_timestamp_end())
        if factor > divisor:
            print("Error: timestamp out of range (factor > divisor)")
            return start
        return start + ((end - start) / divisor) * factor
Esempio n. 2
0
    def process_attack(self, attack: str, params: str, time=False):
        """
        Takes as input the name of an attack (classname) and the attack parameters as string. Parses the string of
        attack parameters, creates the attack by writing the attack packets and returns the path of the written pcap.

        :param attack: The classname of the attack to inject.
        :param params: The parameters for attack customization, see attack class for supported params.
        :param time: Measure packet generation time or not.
        :return: The file path to the created pcap file.
        """
        attack = self.choose_attack(attack)

        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:
                if entry.count('=') != 1:
                    print(
                        'ERROR: Incorrect attack parameter syntax (\'{}\'). Ignoring.'
                        .format(entry))
                    continue
                params_dict.append(entry.split('='))
            params_dict = dict(params_dict)
            # Check if Parameter.INJECT_AT_TIMESTAMP and Parameter.INJECT_AFTER_PACKET are provided at the same time
            # if TRUE: delete Parameter.INJECT_AT_TIMESTAMP (lower priority) and use Parameter.INJECT_AFTER_PACKET
            if (self.current_attack.INJECT_AFTER_PACKET in params_dict) and (
                    self.current_attack.INJECT_AT_TIMESTAMP in params_dict):
                print("CONFLICT: Parameters", attack.INJECT_AT_TIMESTAMP,
                      "and", self.current_attack.INJECT_AFTER_PACKET,
                      "given at the same time. Ignoring",
                      self.current_attack.INJECT_AT_TIMESTAMP, "and using",
                      self.current_attack.INJECT_AFTER_PACKET,
                      "instead to derive the timestamp.")
                del params_dict[self.current_attack.INJECT_AT_TIMESTAMP]

            # Extract attack_note parameter, if not provided returns an empty string
            key_attack_note = "attack.note"
            attack_note = params_dict.get(key_attack_note, "")
            params_dict.pop(
                key_attack_note, None
            )  # delete entry if found, otherwise return an empty string

            # Pass paramters to attack controller
            self.set_params(params_dict)
        else:
            attack_note = "This attack used only default parameters."

        self.current_attack.init_mutual_params()
        self.current_attack.init_params()

        self.current_attack.init_objects()

        print("Generating attack packets...", end=" ")
        sys.stdout.flush()  # force python to print text immediately
        if time:
            self.current_attack.set_start_time()
        # Generate attack packets
        self.current_attack.generate_attack_packets()
        if time:
            self.current_attack.set_finish_time()
        duration = self.current_attack.get_packet_generation_time()
        # Write attack into pcap file
        attack_result = self.current_attack.generate_attack_pcap()

        self.total_packets = attack_result[0]
        temp_attack_pcap_path = attack_result[1]
        if len(attack_result) == 3:
            # Extract the list of additional files, if available
            self.additional_files += attack_result[2]
        print("done. (total: " + str(self.total_packets) + " pkts", end="")
        if time:
            print(" in ", duration, " seconds", end="")
        print(".)")

        # Warning if attack duration gets exceeded by more than 1 second
        if self.current_attack.ATTACK_DURATION in [x.name for x in self.current_attack.params] and \
                self.current_attack.get_param_value(self.current_attack.ATTACK_DURATION) != 0:
            attack_duration = self.current_attack.get_param_value(
                self.current_attack.ATTACK_DURATION)
            packet_duration = abs(self.current_attack.attack_end_utime -
                                  self.current_attack.attack_start_utime)
            time_diff = abs(attack_duration - packet_duration)
            if self.current_attack.exceeding_packets > 0 and time_diff > 1:
                exceeding_packets = ""
                if self.current_attack.exceeding_packets:
                    exceeding_packets = " ({} attack pkts)".format(
                        self.current_attack.exceeding_packets)
                print(
                    "Warning: attack duration was exceeded by {0} seconds{1}.".
                    format(time_diff, exceeding_packets))
            elif time_diff > 1:
                print(
                    "Warning: attack duration was not reached by generated pkts by {} seconds."
                    .format(time_diff))

        # Warning if pcap length gets exceeded
        pcap_end = Util.get_timestamp_from_datetime_str(
            self.statistics.get_pcap_timestamp_end())
        time_diff = pcap_end - self.current_attack.attack_end_utime
        if time_diff < 0:
            print("Warning: end of pcap exceeded by " +
                  str(round(abs(time_diff), 2)) + " seconds.")

        # Store label into LabelManager
        label = Label.Label(attack, self.get_attack_start_utime(),
                            self.get_attack_end_utime(), self.total_packets,
                            self.seed, self.current_attack.params, attack_note)
        self.label_mgr.add_labels(label)

        return temp_attack_pcap_path, duration