Ver código fonte

fixed equal length for packets [52]

Joshua 6 anos atrás
pai
commit
5974095bc6
2 arquivos alterados com 46 adições e 27 exclusões
  1. 34 18
      code/Attack/MembersMgmtCommAttack.py
  2. 12 9
      code/ID2TLib/Generator.py

+ 34 - 18
code/Attack/MembersMgmtCommAttack.py

@@ -180,6 +180,7 @@ class MembersMgmtCommAttack(BaseAttack.BaseAttack):
         limit_packetcount = self.get_param_value(Param.PACKETS_LIMIT)
         limit_duration = self.get_param_value(Param.ATTACK_DURATION)
         path_attack_pcap = None
+        overThousand = False
 
         msg_packet_mapping = MessageMapping(messages)
 
@@ -222,18 +223,33 @@ class MembersMgmtCommAttack(BaseAttack.BaseAttack):
             if total_pkts <= 1:
                 self.attack_start_utime = packets[0].time
             elif total_pkts % BUFFER_SIZE == 0: # every 1000 packets write them to the PCAP file (append)
-                packets = list(packets)
-                Generator.equal_length(packets, padding = padding)
-                last_packet = packets[-1]
-                path_attack_pcap = self.write_attack_pcap(packets, True, path_attack_pcap)
-                packets = deque(maxlen=BUFFER_SIZE)
+                if overThousand: # if over 1000 packets written, there may be a different packet-length for the last few packets 
+                    packets = list(packets)
+                    Generator.equal_length(packets, length = max_len, padding = padding, force_len = True)
+                    last_packet = packets[-1]
+                    path_attack_pcap = self.write_attack_pcap(packets, True, path_attack_pcap)
+                    packets = deque(maxlen=BUFFER_SIZE)
+                else:
+                    packets = list(packets)
+                    Generator.equal_length(packets, padding = padding)
+                    last_packet = packets[-1]
+                    max_len = len(last_packet)
+                    overThousand = True
+                    path_attack_pcap = self.write_attack_pcap(packets, True, path_attack_pcap)
+                    packets = deque(maxlen=BUFFER_SIZE)
 
         # if there are unwritten packets remaining, write them to the PCAP file
         if len(packets) > 0:
-            packets = list(packets)
-            Generator.equal_length(packets, padding = padding)
-            path_attack_pcap = self.write_attack_pcap(packets, True, path_attack_pcap)
-            last_packet = packets[-1]
+            if overThousand:
+                packets = list(packets)
+                Generator.equal_length(packets, length = max_len, padding = padding, force_len = True)
+                path_attack_pcap = self.write_attack_pcap(packets, True, path_attack_pcap)
+                last_packet = packets[-1]
+            else:
+                packets = list(packets)
+                Generator.equal_length(packets, padding = padding)
+                path_attack_pcap = self.write_attack_pcap(packets, True, path_attack_pcap)
+                last_packet = packets[-1]
 
         # write the mapping to a file
         msg_packet_mapping.write_to(context.allocate_file("_mapping.xml"))
@@ -319,7 +335,7 @@ class MembersMgmtCommAttack(BaseAttack.BaseAttack):
         def assign_realistic_timestamps(messages: list, external_ids: set, local_ids: set, avg_delay_local:float, avg_delay_external: float, zero_reference:float):
             """
             Assigns realistic timestamps to a set of messages
-            
+
             :param messages: the set of messages to be updated
             :param external_ids: the set of bot ids, that are outside the network, i.e. external
             :param local_ids: the set of bot ids, that are inside the network, i.e. local
@@ -331,7 +347,7 @@ class MembersMgmtCommAttack(BaseAttack.BaseAttack):
             last_response = {}      # Dict, takes a tuple of 2 Bot_IDs as a key (requester, responder), returns the time of the last response, the requester received
                                     # necessary in order to make sure, that additional requests are sent only after the response to the last one was received
             for msg in messages:    # init
-                last_response[(msg.src, msg.dst)] = -1  
+                last_response[(msg.src, msg.dst)] = -1
 
             # update all timestamps
             for req_msg in messages:
@@ -357,7 +373,7 @@ class MembersMgmtCommAttack(BaseAttack.BaseAttack):
                     if req_msg.src in external_ids or req_msg.dst in external_ids:
                         #external communication
                         respns_msg.time = req_msg.time + avg_delay_external + uniform(-0.1*avg_delay_external, 0.1*avg_delay_external)
-                    
+
                     else:
                         #local communication
                         respns_msg.time = req_msg.time + avg_delay_local + uniform(-0.1*avg_delay_local, 0.1*avg_delay_local)
@@ -385,12 +401,12 @@ class MembersMgmtCommAttack(BaseAttack.BaseAttack):
                 with open("resources/CaidaTTL_perIP.csv", "r") as file:
                     # every line consists of: IP, TTL, Frequency
                     next(file)  # skip CSV header line
-                    for line in file: 
+                    for line in file:
                         ip_addr, ttl, freq = line.split(",")
                         if ip_addr not in ip_based_distrib:
                             ip_based_distrib[ip_addr] = {}  # the values for ip_based_distrib are dicts with key=TTL, value=Frequency
                         ip_based_distrib[ip_addr][ttl] = int(freq)
-                        
+
                 return ip_based_distrib
 
             def get_total_ttl_distrib():
@@ -403,12 +419,12 @@ class MembersMgmtCommAttack(BaseAttack.BaseAttack):
                 with open("resources/CaidaTTL_total.csv", "r") as file:
                     # every line consists of: TTL, Frequency, Fraction
                     next(file)  # skip CSV header line
-                    for line in file: 
+                    for line in file:
                         ttl, freq, _ = line.split(",")
                         total_ttl_distrib[ttl] = int(freq)
-                        
+
                 return total_ttl_distrib
-            
+
             # get the TTL distribution for every IP that is available in "resources/CaidaTTL_perIP.csv"
             ip_ttl_distrib = get_ip_ttl_distrib()
             # build a probability dict for the total TTL distribution
@@ -423,7 +439,7 @@ class MembersMgmtCommAttack(BaseAttack.BaseAttack):
                     bot_configs[bot_id]["TTL"] = 128
 
                 # if there exists detailed information about the TTL distribution of this IP
-                elif bot_ip in ip_ttl_distrib:  
+                elif bot_ip in ip_ttl_distrib:
                     ip_ttl_freqs = ip_ttl_distrib[bot_ip]
                     source_ttl_prob_dict = Lea.fromValFreqsDict(ip_ttl_freqs)  # build a probability dict from this IP's TTL distribution
                     bot_configs[bot_id]["TTL"] = source_ttl_prob_dict.random()

+ 12 - 9
code/ID2TLib/Generator.py

@@ -38,22 +38,25 @@ def add_padding(packet, bytes_padding:int = 0, user_padding:bool=True, rnd:bool
     packet[Raw].load += Raw(load=payload).load
     return packet
 
-def equal_length(list_of_packets:list, length:int = 0, padding:int = 0):
+def equal_length(list_of_packets:list, length:int = 0, padding:int = 0, force_len:bool = False):
     '''
-    Equals the length of a given list of packets on the given length. If the given length is smaller than the largest
+    Equals the length of all packets of a given list of packets to the given length. If the given length is smaller than the largest
     packet, all the other packets are extended to the largest packet's length. Add additional padding
     afterwards to create realism.
     :param list_of_packets: The given set of packet.
     :param length: The length each packet should have. Can be redundant, if the largest packet has more bytes
+    :param force_len: if true, all packets are forced to take on the length of param length
     than length.
     :return: The list of extended packets.
     '''
-
-    largest_packet = length
-    for packet in list_of_packets:
-        packet_length = len(packet)
-        if(packet_length > largest_packet):
-            largest_packet = packet_length
+    if not force_len:
+        largest_packet = length
+        for packet in list_of_packets:
+            packet_length = len(packet)
+            if(packet_length > largest_packet):
+                largest_packet = packet_length
+    else:
+        largest_packet = length
 
     for packet in list_of_packets:
         bytes_padding = largest_packet - len(packet)
@@ -392,4 +395,4 @@ class MappingIPGenerator(IPGenerator):
         return self.mapping[key]
 
     def __getitem__(self, item):
-        return self.get_mapped_ip(item)
+        return self.get_mapped_ip(item)