PcapAddressOperations.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. from random import choice
  2. from ID2TLib import Statistics
  3. from ID2TLib.IPv4 import IPAddress
  4. is_ipv4 = IPAddress.is_ipv4
  5. class PcapAddressOperations():
  6. def __init__(self, statistics: Statistics, uncertain_ip_mult: int=3):
  7. """
  8. Initializes a pcap information extractor that uses the provided statistics for its operations.
  9. :param statistics: The statistics of the pcap file
  10. :param uncertain_ip_mult: the mutliplier to create new address space when the remaining observed space has been drained
  11. """
  12. self.statistics = statistics
  13. self.UNCERTAIN_IPSPACE_MULTIPLIER = uncertain_ip_mult
  14. self._init_ipaddress_ops()
  15. def get_probable_router_mac(self):
  16. """
  17. Returns the most probable router MAC address based on the most used MAC address in the statistics.
  18. :return: the MAC address
  19. """
  20. self.probable_router_mac, count = self.statistics.process_db_query("most_used(macAddress)", print_results=False)[0]
  21. return self.probable_router_mac # and count as a measure of certainty?
  22. def pcap_contains_priv_ips(self):
  23. """
  24. Returns if the provided traffic contains private IPs.
  25. :return: True if the provided traffic contains private IPs, otherwise False
  26. """
  27. return self.contains_priv_ips
  28. def get_local_address_range(self):
  29. """
  30. Returns a tuple with the start and end of the observed local IP range.
  31. :return: The IP range as tuple
  32. """
  33. return str(self.min_local_ip), str(self.max_local_ip)
  34. def get_count_rem_local_ips(self):
  35. """
  36. Returns the number of local IPs in the pcap file that have not aldready been returned by get_existing_local_ips.
  37. :return: the not yet assigned local IPs
  38. """
  39. return len(self.remaining_local_ips)
  40. def get_existing_local_ips(self, count: int=1):
  41. """
  42. Returns the given number of local IPs that are existent in the pcap file.
  43. :param count: the number of local IPs to return
  44. :return: the chosen local IPs
  45. """
  46. if count > len(self.remaining_local_ips):
  47. print("Warning: There are no more {} local IPs in the .pcap file. Returning all remaining local IPs.".format(count))
  48. total = min(len(self.remaining_local_ips), count)
  49. retr_local_ips = []
  50. local_ips = self.remaining_local_ips
  51. for _ in range(0, total):
  52. random_local_ip = choice(sorted(local_ips))
  53. retr_local_ips.append(str(random_local_ip))
  54. local_ips.remove(random_local_ip)
  55. return retr_local_ips
  56. def get_new_local_ips(self, count: int=1):
  57. """
  58. Returns in the pcap not existent local IPs that are in proximity of the observed local IPs. IPs can be returned
  59. that are either between the minimum and maximum observed IP and are therefore considered certain
  60. or that are above the observed maximum address, are more likely to not belong to the local network
  61. and are therefore considered uncertain.
  62. :param count: the number of new local IPs to return
  63. :return: the newly created local IP addresses
  64. """
  65. unused_local_ips = self.unused_local_ips
  66. uncertain_local_ips = self.uncertain_local_ips
  67. count_certain = min(count, len(unused_local_ips))
  68. retr_local_ips = []
  69. for _ in range(0, count_certain):
  70. random_local_ip = choice(sorted(unused_local_ips))
  71. retr_local_ips.append(str(random_local_ip))
  72. unused_local_ips.remove(random_local_ip)
  73. # retrieve uncertain local ips
  74. if count_certain < count:
  75. count_uncertain = count - count_certain
  76. # check if new uncertain IPs have to be created
  77. if len(uncertain_local_ips) < count_uncertain:
  78. ipspace_multiplier = self.UNCERTAIN_IPSPACE_MULTIPLIER
  79. max_new_ip = self.max_uncertain_local_ip.to_int() + ipspace_multiplier * count_uncertain
  80. count_new_ips = max_new_ip - self.max_uncertain_local_ip.to_int()
  81. # create ipspace_multiplier * count_uncertain new uncertain local IP addresses
  82. last_gen_ip = None
  83. for i in range(1, count_new_ips + 1):
  84. ip = IPAddress.from_int(self.max_uncertain_local_ip.to_int() + i)
  85. # exclude the definite broadcast address
  86. if self.priv_ip_segment:
  87. if ip.to_int() >= self.priv_ip_segment.last_address().to_int():
  88. break
  89. uncertain_local_ips.add(ip)
  90. last_gen_ip = ip
  91. self.max_uncertain_local_ip = last_gen_ip
  92. # choose the uncertain IPs to return
  93. total_uncertain = min(count_uncertain, len(uncertain_local_ips))
  94. for _ in range(0, total_uncertain):
  95. random_local_ip = choice(sorted(uncertain_local_ips))
  96. retr_local_ips.append(str(random_local_ip))
  97. uncertain_local_ips.remove(random_local_ip)
  98. return retr_local_ips
  99. def get_existing_external_ips(self, count: int=1):
  100. """
  101. Returns the given number of external IPs that are existent in the pcap file.
  102. :param count: the number of external IPs to return
  103. :return: the chosen external IPs
  104. """
  105. if not (len(self.external_ips) > 0):
  106. print("Warning: .pcap does not contain any external ips.")
  107. return []
  108. total = min(len(self.remaining_external_ips), count)
  109. retr_external_ips = []
  110. external_ips = self.remaining_external_ips
  111. for _ in range(0, total):
  112. random_external_ip = choice(sorted(external_ips))
  113. retr_external_ips.append(str(random_external_ip))
  114. external_ips.remove(random_external_ip)
  115. return retr_external_ips
  116. def _init_ipaddress_ops(self):
  117. """
  118. Load and process data needed to perform functions on the IP addresses contained in the statistics
  119. """
  120. # retrieve local and external IPs
  121. all_ips_str = set(self.statistics.process_db_query("all(ipAddress)", print_results=False))
  122. external_ips_str = set(self.statistics.process_db_query("ipAddress(macAddress=%s)" % self.get_probable_router_mac(), print_results=False)) # including router
  123. local_ips_str = all_ips_str - external_ips_str
  124. external_ips = set()
  125. local_ips = set()
  126. self.contains_priv_ips = False
  127. self.priv_ip_segment = None
  128. # convert local IP strings to IPv4.IPAddress representation
  129. for ip in local_ips_str:
  130. if is_ipv4(ip):
  131. ip = IPAddress.parse(ip)
  132. if ip.is_private() and not self.contains_priv_ips:
  133. self.contains_priv_ips = True
  134. self.priv_ip_segment = ip.get_private_segment()
  135. # exclude local broadcast address and other special addresses
  136. if (not str(ip) == "255.255.255.255") and (not ip.is_localhost()) and (not ip.is_multicast()) and (not ip.is_reserved()) and (not ip.is_zero_conf()):
  137. local_ips.add(ip)
  138. # convert external IP strings to IPv4.IPAddress representation
  139. for ip in external_ips_str:
  140. if is_ipv4(ip):
  141. ip = IPAddress.parse(ip)
  142. # if router MAC can definitely be mapped to local/private IP, add it to local_ips (because at first it is stored in external_ips, see above)
  143. # this depends on whether the local network is identified by a private IP address range or not.
  144. if ip.is_private():
  145. local_ips.add(ip)
  146. # exclude local broadcast address and other special addresses
  147. elif (not str(ip) == "255.255.255.255") and (not ip.is_localhost()) and (not ip.is_multicast()) and (not ip.is_reserved()) and (not ip.is_zero_conf()):
  148. external_ips.add(ip)
  149. min_local_ip, max_local_ip = min(local_ips), max(local_ips)
  150. # save the certain unused local IPs of the network
  151. unused_local_ips = set()
  152. for i in range(min_local_ip.to_int() + 1, max_local_ip.to_int()):
  153. ip = IPAddress.from_int(i)
  154. if not ip in local_ips:
  155. unused_local_ips.add(ip)
  156. # save the gathered information for efficient later use
  157. self.external_ips = frozenset(external_ips)
  158. self.remaining_external_ips = external_ips
  159. self.min_local_ip, self.max_local_ip = min_local_ip, max_local_ip
  160. self.max_uncertain_local_ip = max_local_ip
  161. self.local_ips = frozenset(local_ips)
  162. self.remaining_local_ips = local_ips
  163. self.unused_local_ips = unused_local_ips
  164. self.uncertain_local_ips = set()