Example #1
0
    def generate_attack_pcap(self):
        """
        Creates a pcap containing the attack packets.

        :return: The location of the generated pcap file.
        """

        # write attack packets to pcap
        if self.readwrite != 'sequence': # sequence mode automatically writes to pcap
            self.attack_start_utime = self.packets[0].time
            self.attack_end_utime = self.packets[-1].time

            self.path_attack_pcap = self.write_attack_pcap(self.packets)

        # export statistics for generated attack pcap
        generated_attack_statistics = Statistics.Statistics(PcapFile.PcapFile(self.path_attack_pcap))
        generated_attack_statistics.load_pcap_statistics(False, True, False, False, [], False, None)
        if self.export_filetype == 'xlsx':
            Testing.exportSQLite3_toXLSX(Testing.connection_SQLite3_fromStatistics
                , generated_attack_statistics
                , 'semi_labeled_e_' + self.output_name
                , self.output_path)
        elif self.export_filetype == 'csv':
            output_path = os.path.join(self.output_path, 'CSV_generated_attack')
            if not os.path.exists(output_path):
                os.makedirs(output_path)
            Testing.exportSQLite3_toCSV(Testing.connection_SQLite3_fromStatistics
                , self.attack_statistics
                , self.output_path)

        # return packets sorted by packet time_sec_start
        return self.pkt_num, self.path_attack_pcap
Example #2
0
def build_rewrapper(attack, param_dict):
    """
    Fill dictinaries with data from config.

    :param attack: Mix attack.
    :param param_dict: parsed config
    :return: rewrapper
    """
    if param_dict['atk.file'] != 'default':  ## default attack is hydra-1_tasks
        attack.attack_file = param_dict['atk.file']

    ## generate statistics for attack pcap
    attack.attack_statistics = Statistics.Statistics(
        PcapFile.PcapFile(attack.attack_file))
    attack.attack_statistics.load_pcap_statistics(False, True, False, False,
                                                  [], False, False)

    ## statistics stored in global dict under the keys
    global_dict = TMdict.GlobalRWdict(
        statistics=attack.statistics,
        attack_statistics=attack.attack_statistics)
    packet_dict = TMdict.PacketDataRWdict()
    conversation_dict = TMdict.ConversationRWdict()

    global_dict.add_recalculation_function(RecalTMdict.recalculate_mss)
    global_dict.add_recalculation_function(RecalTMdict.recalculate_ttl)
    global_dict.add_recalculation_function(RecalTMdict.recalculate_win_size)
    ## dicts stored in a dict under param data_dict under keys from TMdef
    rewrap = ReWrapper.ReWrapper(attack.statistics, global_dict,
                                 conversation_dict, packet_dict,
                                 scapy.NoPayload)

    return rewrap
Example #3
0
    def __init__(self, name, description, attack_type):
        """
        To be called within the individual attack class to initialize the required parameters.

        :param name: The name of the attack class.
        :param description: A short description of the attack.
        :param attack_type: The type the attack belongs to, like probing/scanning, malware.
        """
        # Reference to statistics class
        self.statistics = Statistics.Statistics(None)

        try:
            # get_reply_delay
            self.all_min_latencies = self.statistics.process_db_query(
                "SELECT minDelay FROM conv_statistics LIMIT 500;")
            self.all_max_latencies = self.statistics.process_db_query(
                "SELECT maxDelay FROM conv_statistics LIMIT 500;")
            self.most_used_mss_value = self.statistics.get_most_used_mss_value(
            )
            self.most_used_ttl_value = self.statistics.get_most_used_ttl_value(
            )
            self.most_used_win_size = self.statistics.get_most_used_win_size()
            pkt_count = self.statistics.get_packet_count()
        except AttributeError:
            self.all_min_latencies = 0
            self.all_max_latencies = 0
            self.most_used_mss_value = 0
            self.most_used_ttl_value = 0
            self.most_used_win_size = 0
            pkt_count = 0

            # Class fields
        self.attack_name = name
        self.attack_description = description
        self.attack_type = attack_type
        self.params = [
            Parameter(self.INJECT_AT_TIMESTAMP, Float()),
            Parameter(self.INJECT_AFTER_PACKET, IntegerLimited([0,
                                                                pkt_count])),
            Parameter(self.BANDWIDTH_MAX, Float()),
            Parameter(self.BANDWIDTH_MIN_LOCAL, Float()),
            Parameter(self.BANDWIDTH_MIN_PUBLIC, Float())
        ]
        self.attack_start_utime = 0
        self.attack_end_utime = 0
        self.start_time = 0
        self.finish_time = 0
        self.packets = []
        self.total_pkt_num = 0
        self.exceeding_packets = 0
        self.path_attack_pcap = ""
        self.timestamp_controller = None
        self.bandwidth_controller = None
        self.last_packet = None
        self.full_interval = None
        self.previous_interval = 0
        self.sent_bytes = 0
        self.interval_count = 0
        self.buffer_size = 1000
Example #4
0
    def __init__(self,
                 pcap_file_path: str,
                 do_extra_tests: bool,
                 non_verbose: bool = True,
                 pcap_out_path: str = None,
                 debug: bool = False):
        """
        Creates a new Controller, acting as a central coordinator for the whole application.

        :param pcap_file_path:
        """
        # Fields
        self.pcap_src_path = pcap_file_path.strip()
        self.pcap_dest_path = ''
        self.pcap_out_path = pcap_out_path
        self.written_pcaps = []
        self.do_extra_tests = do_extra_tests
        self.non_verbose = non_verbose
        self.seed = None
        self.durations = []
        self.added_packets = 0
        self.created_files = []
        self.debug = debug

        # Initialize class instances
        print("Input file: %s" % self.pcap_src_path)
        self.pcap_file = PcapFile.PcapFile(self.pcap_src_path)
        self.label_manager = LabelManager.LabelManager(self.pcap_src_path)
        self.statistics = Statistics.Statistics(self.pcap_file)
        self.statistics.do_extra_tests = self.do_extra_tests
        self.statisticsDB = self.statistics.get_statistics_database()
        self.attack_controller = atkCtrl.AttackController(
            self.pcap_file, self.statistics, self.label_manager)

        # Set output directory and create it (if necessary)
        if pcap_out_path is not None:
            out_dir = os.path.dirname(pcap_out_path)
            if not out_dir:  # if out_dir is cwd
                out_dir = "."
            Util.OUT_DIR = out_dir + os.sep
        else:
            Util.OUT_DIR = os.path.join(os.path.dirname(pcap_file_path),
                                        "id2t_results") + os.sep
        os.makedirs(Util.OUT_DIR, exist_ok=True)