Browse Source

Slightly change or add some things to make them work better as a whole (or correctly to some extent)

dustin.born 7 years ago
parent
commit
2dd481159d

+ 1 - 0
code/Attack/AttackParameters.py

@@ -23,6 +23,7 @@ class Parameter(Enum):
     ATTACK_DURATION = 'attack.duration' # in seconds
     ATTACK_DURATION = 'attack.duration' # in seconds
     VICTIM_BUFFER = 'victim.buffer' # in packets
     VICTIM_BUFFER = 'victim.buffer' # in packets
     TARGET_URI = 'target.uri'
     TARGET_URI = 'target.uri'
+    NUMBER_BOTS = 'bots.count'
     # recommended type: domain -----------------------------------
     # recommended type: domain -----------------------------------
     TARGET_HOST = 'target.host'
     TARGET_HOST = 'target.host'
 
 

+ 26 - 4
code/ID2TLib/MapInputCSVToIDs.py

@@ -100,6 +100,20 @@ def find_interval_with_most_comm(packets, number_ids: int, max_int_time: float):
 
 
         return picked_ids, total_msg_count
         return picked_ids, total_msg_count
 
 
+    def get_indv_id_counts(picked_ids: dict, msg_counts: dict):
+        indv_id_counts = {}
+        for msg in msg_counts:
+            ids = msg.split("-")
+            if ids[0] in picked_ids and ids[1] in picked_ids:
+                for id_ in ids:
+                    if id_ in indv_id_counts:
+                        indv_id_counts[id_] += msg_counts[msg]
+                    else:
+                        indv_id_counts[id_] = msg_counts[msg]
+
+        return indv_id_counts
+
+
     # first find all possible intervals that contain enough ids that communicate among themselves
     # first find all possible intervals that contain enough ids that communicate among themselves
     idx_low, idx_high = 0, 0
     idx_low, idx_high = 0, 0
     msg_counts = dict()
     msg_counts = dict()
@@ -119,7 +133,7 @@ def find_interval_with_most_comm(packets, number_ids: int, max_int_time: float):
 
 
             # if we have enough ids as specified by the caller, mark as possible interval
             # if we have enough ids as specified by the caller, mark as possible interval
             if count_ids_in_msg_counts(nez_msg_counts) >= number_ids:
             if count_ids_in_msg_counts(nez_msg_counts) >= number_ids:
-                #possible_intervals.append((nez_msg_counts, packets[idx_low]["Time"], packets[idx_high-1]["Time"]))
+                # possible_intervals.append((nez_msg_counts, packets[idx_low]["Time"], packets[idx_high-1]["Time"]))
                 possible_intervals.append((nez_msg_counts, idx_low, idx_high - 1))
                 possible_intervals.append((nez_msg_counts, idx_low, idx_high - 1))
 
 
             if idx_high >= len(packets):
             if idx_high >= len(packets):
@@ -138,21 +152,29 @@ def find_interval_with_most_comm(packets, number_ids: int, max_int_time: float):
 
 
     # now find the interval in which as many ids as specified communicate the most in the given time interval
     # now find the interval in which as many ids as specified communicate the most in the given time interval
     summed_intervals = []
     summed_intervals = []
+    sum_intervals_idxs = []
     cur_highest_sum = 0
     cur_highest_sum = 0
-    
+
     # for every interval compute the sum of msg_counts of the first most communicative ids and eventually find
     # for every interval compute the sum of msg_counts of the first most communicative ids and eventually find
     # the interval(s) with most communication and its ids
     # the interval(s) with most communication and its ids
-    for interval in possible_intervals:
+    for j, interval in enumerate(possible_intervals):
         msg_counts = interval[0].items()
         msg_counts = interval[0].items()
         sorted_msg_counts = sorted(msg_counts, key=lambda x: x[1], reverse=True)
         sorted_msg_counts = sorted(msg_counts, key=lambda x: x[1], reverse=True)
-
         picked_ids, msg_sum = get_msg_count_first_ids(sorted_msg_counts)
         picked_ids, msg_sum = get_msg_count_first_ids(sorted_msg_counts)
 
 
         if msg_sum == cur_highest_sum:
         if msg_sum == cur_highest_sum:
             summed_intervals.append({"IDs": picked_ids, "MsgSum": msg_sum, "Start": interval[1], "End": interval[2]})
             summed_intervals.append({"IDs": picked_ids, "MsgSum": msg_sum, "Start": interval[1], "End": interval[2]})
+            sum_intervals_idxs.append(j)
         elif msg_sum > cur_highest_sum:
         elif msg_sum > cur_highest_sum:
             summed_intervals = []
             summed_intervals = []
+            sum_intervals_idxs = [j]
             summed_intervals.append({"IDs": picked_ids, "MsgSum": msg_sum, "Start": interval[1], "End": interval[2]})
             summed_intervals.append({"IDs": picked_ids, "MsgSum": msg_sum, "Start": interval[1], "End": interval[2]})
             cur_highest_sum = msg_sum
             cur_highest_sum = msg_sum
 
 
+    for j, interval in enumerate(summed_intervals):
+        idx = sum_intervals_idxs[j]
+        msg_counts_picked = possible_intervals[idx][0]
+        indv_id_counts = get_indv_id_counts(interval["IDs"], msg_counts_picked)
+        interval["IDs"] = indv_id_counts
+
     return summed_intervals
     return summed_intervals

+ 5 - 4
code/ID2TLib/MessageType.py

@@ -5,7 +5,8 @@ class MessageType(Enum):
 	Defines possible Message types
 	Defines possible Message types
 	"""
 	"""
 
 
-	HELLO=0
-	HELLO_REPLY=1
-	NEIGHBORLIST_REQUEST=2
-	NEIGHBORLIST_REPLY=3
+	TIMEOUT = 3
+	SALITY_NL_REQUEST = 101
+	SALITY_NL_REPLY = 102
+	SALITY_HELLO = 103
+	SALITY_HELLO_REPLY = 104

+ 9 - 9
code/ID2TLib/PacketGenerator.py

@@ -1,14 +1,14 @@
-from MessageType import MessageType
+from ID2TLib.MessageType import MessageType
 
 
-import PayloadGenerator
+import ID2TLib.PayloadGenerator as PayloadGenerator
 
 
 from scapy.layers.inet import IP, Ether, UDP, TCP
 from scapy.layers.inet import IP, Ether, UDP, TCP
 from scapy.packet import Raw
 from scapy.packet import Raw
 
 
-class Packet_Generator():
+class PacketGenerator():
 	"""docstring for Packet_Generator"""
 	"""docstring for Packet_Generator"""
 	def __init__(self, protocol="udp"):
 	def __init__(self, protocol="udp"):
-		super(Packet_Generator, self).__init__()
+		super(PacketGenerator, self).__init__()
 		self.protocol = protocol
 		self.protocol = protocol
 
 
 	def generate_Packet(self, ip_src: str="192.168.64.32", ip_dst: str="192.168.64.48", mac_src: str="56:6D:D9:BC:70:1C",
 	def generate_Packet(self, ip_src: str="192.168.64.32", ip_dst: str="192.168.64.48", mac_src: str="56:6D:D9:BC:70:1C",
@@ -41,7 +41,7 @@ class Packet_Generator():
 
 
 	def generate_MMCOM_Packet(self, ip_src: str="192.168.64.32", ip_dst: str="192.168.64.48", mac_src: str="56:6D:D9:BC:70:1C",
 	def generate_MMCOM_Packet(self, ip_src: str="192.168.64.32", ip_dst: str="192.168.64.48", mac_src: str="56:6D:D9:BC:70:1C",
 			mac_dst: str="F4:2B:95:B3:0E:1A", port_src: int=1337, port_dst: int=6442, tcpflags: str="S",
 			mac_dst: str="F4:2B:95:B3:0E:1A", port_src: int=1337, port_dst: int=6442, tcpflags: str="S",
-			message_type: MessageType=MessageType.HELLO, neighborlist_entries: int=1):
+			message_type: MessageType=103, neighborlist_entries: int=1):
 		"""
 		"""
 		Creates a Packet for Members-Management-Communication with the specified Values and the current protocol
 		Creates a Packet for Members-Management-Communication with the specified Values and the current protocol
 
 
@@ -58,13 +58,13 @@ class Packet_Generator():
 	    """
 	    """
 
 
 	    #Determine length of the payload that has to be generated
 	    #Determine length of the payload that has to be generated
-		if(message_type == MessageType.HELLO):
+		if(message_type == MessageType.SALITY_HELLO.value):
 			payload_len = 0
 			payload_len = 0
-		elif(message_type == MessageType.HELLO_REPLY):
+		elif(message_type == MessageType.SALITY_HELLO_REPLY.value):
 			payload_len = 22
 			payload_len = 22
-		elif(message_type == MessageType.NEIGHBORLIST_REQUEST):
+		elif(message_type == MessageType.SALITY_NL_REQUEST.value):
 			payload_len = 28
 			payload_len = 28
-		elif(message_type == MessageType.NEIGHBORLIST_REPLY):
+		elif(message_type == MessageType.SALITY_NL_REPLY.value):
 			payload_len = 24 + 6 * neighborlist_entries
 			payload_len = 24 + 6 * neighborlist_entries
 		else:
 		else:
 			payload_len = 0
 			payload_len = 0

+ 12 - 9
code/ID2TLib/PaddingGenerator.py

@@ -1,20 +1,22 @@
 from scapy.packet import Raw
 from scapy.packet import Raw
-import sys
 
 
-import PayloadGenerator
+import ID2TLib.PayloadGenerator as PayloadGenerator
 
 
 
 
-def add_padding(packet, bytes_padding = 0):
+def add_padding(packet, bytes_padding = 0, user_padding=True):
     '''
     '''
     Adds padding to a packet with the given amount of bytes, but a maximum of 100 bytes.
     Adds padding to a packet with the given amount of bytes, but a maximum of 100 bytes.
     :param packet: the packet that will be extended with the additional payload
     :param packet: the packet that will be extended with the additional payload
     :param bytes_padding: the amount of bytes that will be appended to the packet
     :param bytes_padding: the amount of bytes that will be appended to the packet
     :return: the initial packet, extended with the wanted amount of bytes of padding
     :return: the initial packet, extended with the wanted amount of bytes of padding
     '''
     '''
-    if(bytes_padding > 100):
+
+    # should equal_length be limited by this?
+    if(user_padding and bytes_padding > 100):
         bytes_padding = 100
         bytes_padding = 100
+
     payload = PayloadGenerator.generate_Payload(bytes_padding)
     payload = PayloadGenerator.generate_Payload(bytes_padding)
-    packet = packet / Raw(load = payload)
+    packet[Raw].load += Raw(load=payload).load
     return packet
     return packet
 
 
 def equal_length(list_of_packets, length = 0):
 def equal_length(list_of_packets, length = 0):
@@ -25,15 +27,16 @@ def equal_length(list_of_packets, length = 0):
     :param length: The length each packet should have.
     :param length: The length each packet should have.
     :return: The set of extended packets.
     :return: The set of extended packets.
     '''
     '''
+
     largest_packet = length
     largest_packet = length
     for packet in list_of_packets:
     for packet in list_of_packets:
-        packet_length = sys.getsizeof(packet)
-        if(packet_length > length):
+        packet_length = len(packet)
+        if(packet_length > largest_packet):
             largest_packet = packet_length
             largest_packet = packet_length
 
 
     for packet in list_of_packets:
     for packet in list_of_packets:
-        bytes_padding = largest_packet - sys.getsizeof(packet)
+        bytes_padding = largest_packet - len(packet)
         if(bytes_padding > 0):
         if(bytes_padding > 0):
-            add_padding(packet, bytes_padding)
+            add_padding(packet, bytes_padding, False)
 
 
     return list_of_packets
     return list_of_packets

+ 49 - 5
code/ID2TLib/PcapAddressOperations.py

@@ -60,10 +60,10 @@ class PcapAddressOperations():
             print("Warning: .pcap does not contain any private ips.")
             print("Warning: .pcap does not contain any private ips.")
             return []
             return []
 
 
-        if count > len(self.priv_ips):
-            print("Warning: There are no {} priv IPs in the .pcap file. Returning all existing priv IPs.".format(count))
+        if count > len(self.remaining_priv_ips):
+            print("Warning: There are no more {} private IPs in the .pcap file. Returning all remaining private IPs.".format(count))
 
 
-        total = min(len(self.priv_ips), count)
+        total = min(len(self.remaining_priv_ips), count)
 
 
         retr_priv_ips = []
         retr_priv_ips = []
         priv_ips = self.remaining_priv_ips
         priv_ips = self.remaining_priv_ips
@@ -72,6 +72,9 @@ class PcapAddressOperations():
             retr_priv_ips.append(str(random_priv_ip))
             retr_priv_ips.append(str(random_priv_ip))
             priv_ips.remove(random_priv_ip)
             priv_ips.remove(random_priv_ip)
 
 
+        if count == 1:
+            return retr_priv_ips[0]
+
         return retr_priv_ips
         return retr_priv_ips
 
 
     # also use IPs below minimum observed IP?
     # also use IPs below minimum observed IP?
@@ -143,8 +146,41 @@ class PcapAddressOperations():
                 retr_priv_ips.append(str(random_priv_ip))
                 retr_priv_ips.append(str(random_priv_ip))
                 uncertain_priv_ips.remove(random_priv_ip)
                 uncertain_priv_ips.remove(random_priv_ip)
 
 
+        if count == 1:
+            return retr_public_ips[0]
+            
         return retr_priv_ips
         return retr_priv_ips
 
 
+    def get_existing_public_ips(self, count: int=1):
+        """
+        Returns the given number of private IPs that are existent in the pcap file.
+
+        :param count: the number of IPs to return
+        :return: the chosen private IPs
+        """
+
+        # reasonable to include this?
+        if not (len(self.public_ips) > 0):
+            print("Warning: .pcap does not contain any public ips.")
+            return []
+
+        if count > len(self.remaining_public_ips):
+            print("Warning: There are no {} public IPs in the .pcap file. Returning all existing public IPs.".format(count))
+
+        total = min(len(self.remaining_public_ips), count)
+
+        retr_public_ips = []
+        public_ips = self.remaining_public_ips
+        for _ in range(0, total):
+            random_public_ip = choice(tuple(public_ips))
+            retr_public_ips.append(str(random_public_ip))
+            public_ips.remove(random_public_ip)
+
+        if count == 1:
+            return retr_public_ips[0]
+
+        return retr_public_ips
+
     def _init_ipaddress_ops(self):
     def _init_ipaddress_ops(self):
         """
         """
         Load and process data needed to perform functions on the IP addresses contained in the statistics
         Load and process data needed to perform functions on the IP addresses contained in the statistics
@@ -152,7 +188,9 @@ class PcapAddressOperations():
 
 
         all_ips = self.statistics.process_db_query("all(ipAddress)", print_results=False)
         all_ips = self.statistics.process_db_query("all(ipAddress)", print_results=False)
 
 
+        # Prepare private address operations and on the side save all external IPs
         # find the private IP segment in use
         # find the private IP segment in use
+        public_ips = set()
         priv_ip_segment = None
         priv_ip_segment = None
         self.contains_priv_ips = False
         self.contains_priv_ips = False
         first_priv_ip = None
         first_priv_ip = None
@@ -170,6 +208,9 @@ class PcapAddressOperations():
                 first_priv_ip = ip
                 first_priv_ip = ip
                 self.contains_priv_ips = True
                 self.contains_priv_ips = True
                 break
                 break
+            # new function in IPv4 to shorten this?
+            elif (not ip.is_localhost()) and (not ip.is_localhost()) and (not ip.is_multicast()) and (not ip.is_reserved()) and (not ip.is_zero_conf()):
+                public_ips.add(ip)
 
 
         if not self.contains_priv_ips:
         if not self.contains_priv_ips:
             #print("The Pcap File does not contain any private IPs")
             #print("The Pcap File does not contain any private IPs")
@@ -190,6 +231,9 @@ class PcapAddressOperations():
                     max_priv_ip = ip
                     max_priv_ip = ip
                 elif ip < min_priv_ip:
                 elif ip < min_priv_ip:
                     min_priv_ip = ip
                     min_priv_ip = ip
+            # new function in IPv4 to shorten this?
+            elif (not ip.is_private()) and (not ip.is_localhost()) and (not ip.is_localhost()) and (not ip.is_multicast()) and (not ip.is_reserved()) and (not ip.is_zero_conf()):
+                public_ips.add(ip)
 
 
         # save the certain unused priv IPs of the network
         # save the certain unused priv IPs of the network
         unused_priv_ips = set()
         unused_priv_ips = set()
@@ -199,6 +243,8 @@ class PcapAddressOperations():
                 unused_priv_ips.add(ip)
                 unused_priv_ips.add(ip)
 
 
         # save the gathered information for efficient later use
         # save the gathered information for efficient later use
+        self.public_ips = frozenset(public_ips)
+        self.remaining_public_ips = public_ips
         self.min_priv_ip, self.max_priv_ip = min_priv_ip, max_priv_ip
         self.min_priv_ip, self.max_priv_ip = min_priv_ip, max_priv_ip
         self.max_uncertain_priv_ip = max_priv_ip
         self.max_uncertain_priv_ip = max_priv_ip
         self.priv_ips = frozenset(priv_ips)
         self.priv_ips = frozenset(priv_ips)
@@ -207,5 +253,3 @@ class PcapAddressOperations():
         self.generated_uncertain_ips = set()
         self.generated_uncertain_ips = set()
         self.uncertain_priv_ips = set()
         self.uncertain_priv_ips = set()
         self.priv_ip_segment = priv_ip_segment
         self.priv_ip_segment = priv_ip_segment
-
-

+ 1 - 1
code/ID2TLib/genRandPort.py

@@ -1,7 +1,7 @@
 import random
 import random
 import string
 import string
 
 
-def generateRandomPort(default):
+def generateRandomPort(default: int=0):
 
 
     firstLetter = random.choice(string.ascii_letters);
     firstLetter = random.choice(string.ascii_letters);
     lastLetter = random.choice(string.ascii_letters + string.digits);
     lastLetter = random.choice(string.ascii_letters + string.digits);

File diff suppressed because it is too large
+ 0 - 0
resources/MembersMgmtComm_example.xml


Some files were not shown because too many files changed in this diff