Utility.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. import calendar as cal
  2. import datetime as dt
  3. import ipaddress
  4. import os
  5. import random as rnd
  6. import matplotlib
  7. import scipy.stats as stats
  8. matplotlib.use('Agg', force=True)
  9. import lea
  10. import xdg.BaseDirectory as BaseDir
  11. import scapy.layers.inet as inet
  12. CACHE_DIR = os.path.join(BaseDir.xdg_cache_home, 'id2t')
  13. CODE_DIR = os.path.dirname(os.path.abspath(__file__)) + "/../"
  14. ROOT_DIR = CODE_DIR + "../"
  15. RESOURCE_DIR = ROOT_DIR + "resources/"
  16. TEST_DIR = RESOURCE_DIR + "test/"
  17. OUT_DIR = None
  18. MISC_OUT_FILES = {}
  19. # List of common operation systems
  20. platforms = {"win7", "win10", "winxp", "win8.1", "macos", "linux", "win8", "winvista", "winnt", "win2000"}
  21. # Distribution of common operation systems
  22. platform_probability = {"win7": 48.43, "win10": 27.99, "winxp": 6.07, "win8.1": 6.07, "macos": 5.94, "linux": 3.38,
  23. "win8": 1.35, "winvista": 0.46, "winnt": 0.31}
  24. # List of no-ops
  25. x86_nops = {b'\x90', b'\xfc', b'\xfd', b'\xf8', b'\xf9', b'\xf5', b'\x9b'}
  26. # List of pseudo no-ops (includes ops which won't change the state e.g. read access)
  27. x86_pseudo_nops = {b'\x97', b'\x96', b'\x95', b'\x93', b'\x92', b'\x91', b'\x99', b'\x4d', b'\x48', b'\x47', b'\x4f',
  28. b'\x40', b'\x41', b'\x37', b'\x3f', b'\x27', b'\x2f', b'\x46', b'\x4e', b'\x98', b'\x9f', b'\x4a',
  29. b'\x44', b'\x42', b'\x43', b'\x49', b'\x4b', b'\x45', b'\x4c', b'\x60', b'\x0e', b'\x1e', b'\x50',
  30. b'\x55', b'\x53', b'\x51', b'\x57', b'\x52', b'\x06', b'\x56', b'\x54', b'\x16', b'\x58', b'\x5d',
  31. b'\x5b', b'\x59', b'\x5f', b'\x5a', b'\x5e', b'\xd6'}
  32. # Characters which result in operational behaviour (e.g. FTPWinaXeExploit.py)
  33. forbidden_chars = [b'\x00', b'\x0a', b'\x0d']
  34. # Used in get_attacker_config
  35. attacker_port_mapping = {}
  36. # Used in get_attacker_config
  37. attacker_ttl_mapping = {}
  38. # Identifier for attacks
  39. generic_attack_names = {"attack", "exploit"}
  40. def update_timestamp(timestamp: float, pps: float, delay: float=0, inj_pps: float=0, inj_timestamp: float=0):
  41. """
  42. Calculates the next timestamp to be used based on the packet per second rate (pps) and the maximum delay.
  43. :return: Timestamp to be used for the next packet.
  44. """
  45. # FIXME: throw Exception if pps==0
  46. second = 0
  47. packets_this_second = 0
  48. if inj_pps != 0 and inj_timestamp != 0:
  49. time = timestamp - inj_timestamp
  50. packets_so_far = time / inj_pps
  51. packets_this_second = packets_so_far % inj_pps
  52. else:
  53. inj_pps = 0
  54. if delay == 0:
  55. # Calculate request timestamp
  56. # To imitate the bursty behavior of traffic
  57. random_delay = lea.Lea.fromValFreqsDict({1 / pps: 70, 2 / pps: 20, 5 / pps: 7, 10 / pps: 3})
  58. result_delay = rnd.uniform(1 / pps, random_delay.random())
  59. else:
  60. # Calculate reply timestamp
  61. random_delay = lea.Lea.fromValFreqsDict({delay / 2: 70, delay / 3: 20, delay / 5: 7, delay / 10: 3})
  62. result_delay = rnd.uniform(1 / pps + delay, 1 / pps + random_delay.random())
  63. result = timestamp + result_delay
  64. if inj_pps > packets_this_second and int(result) - int(timestamp) != 1:
  65. result = result + 1
  66. return result
  67. def get_timestamp_from_datetime_str(time: str):
  68. return dt.datetime.strptime(time, "%Y-%m-%d %H:%M:%S.%f").timestamp()
  69. def get_interval_pps(complement_interval_pps, timestamp):
  70. """
  71. Gets the packet rate (pps) for a specific time interval.
  72. :param complement_interval_pps: an array of tuples (the last timestamp in the interval, the packet rate in the
  73. corresponding interval).
  74. :param timestamp: the timestamp at which the packet rate is required.
  75. :return: the corresponding packet rate (pps) .
  76. """
  77. for row in complement_interval_pps:
  78. if timestamp <= row[0]:
  79. return row[1]
  80. return complement_interval_pps[-1][1] # in case the timestamp > capture max timestamp
  81. def get_nth_random_element(*element_list):
  82. """
  83. Returns the n-th element of every list from an arbitrary number of given lists.
  84. For example, list1 contains IP addresses, list 2 contains MAC addresses. Use of this function ensures that
  85. the n-th IP address uses always the n-th MAC address.
  86. :param element_list: An arbitrary number of lists.
  87. :return: A tuple of the n-th element of every list.
  88. """
  89. if len(element_list) <= 0:
  90. return None
  91. elif len(element_list) == 1 and len(element_list[0]) > 0:
  92. return rnd.choice(element_list[0])
  93. else:
  94. range_max = min([len(x) for x in element_list])
  95. if range_max > 0:
  96. range_max -= 1
  97. n = rnd.randint(0, range_max)
  98. return tuple(x[n] for x in element_list)
  99. else:
  100. return None
  101. def get_rnd_os():
  102. """
  103. Chooses random platform over an operating system probability distribution
  104. :return: random platform as string
  105. """
  106. os_dist = lea.Lea.fromValFreqsDict(platform_probability)
  107. return os_dist.random()
  108. def check_platform(platform: str) -> None:
  109. """
  110. Checks if the given platform is currently supported
  111. if not exits with error
  112. :param platform: the platform, which should be validated
  113. """
  114. if platform not in platforms:
  115. raise ValueError("ERROR: Invalid platform: " + platform + "." +
  116. "\n Please select one of the following platforms: " + ",".join(platforms))
  117. def get_ip_range(start_ip: str, end_ip: str):
  118. """
  119. Generates a list of IPs of a given range. If the start_ip is greater than the end_ip, the reverse range is generated
  120. :param start_ip: the start_ip of the desired IP-range
  121. :param end_ip: the end_ip of the desired IP-range
  122. :return: a list of all IPs in the desired IP-range, including start-/end_ip
  123. """
  124. start = ipaddress.ip_address(start_ip)
  125. end = ipaddress.ip_address(end_ip)
  126. ips = []
  127. if start < end:
  128. while start <= end:
  129. ips.append(start.exploded)
  130. start = start + 1
  131. elif start > end:
  132. while start >= end:
  133. ips.append(start.exploded)
  134. start = start - 1
  135. else:
  136. ips.append(start_ip)
  137. return ips
  138. def generate_source_port_from_platform(platform: str, previous_port=0):
  139. """
  140. Generates the next source port according to the TCP-port-selection strategy of the given platform
  141. :param platform: the platform for which to generate source ports
  142. :param previous_port: the previously used/generated source port. Must be 0 if no port was generated before
  143. :return: the next source port for the given platform
  144. """
  145. check_platform(platform)
  146. if platform in {"winnt", "winxp", "win2000"}:
  147. if (previous_port == 0) or (previous_port + 1 > 5000):
  148. return rnd.randint(1024, 5000)
  149. else:
  150. return previous_port + 1
  151. elif platform == "linux":
  152. return rnd.randint(32768, 61000)
  153. else:
  154. if (previous_port == 0) or (previous_port + 1 > 65535):
  155. return rnd.randint(49152, 65535)
  156. else:
  157. return previous_port + 1
  158. def get_filetime_format(timestamp):
  159. """
  160. Converts a timestamp into MS FILETIME format
  161. :param timestamp: a timestamp in seconds
  162. :return: MS FILETIME timestamp
  163. """
  164. boot_datetime = dt.datetime.fromtimestamp(timestamp)
  165. if boot_datetime.tzinfo is None or boot_datetime.tzinfo.utcoffset(boot_datetime) is None:
  166. boot_datetime = boot_datetime.replace(tzinfo=boot_datetime.tzname())
  167. boot_filetime = 116444736000000000 + (cal.timegm(boot_datetime.timetuple()) * 10000000)
  168. return boot_filetime + (boot_datetime.microsecond * 10)
  169. def get_rnd_boot_time(timestamp, platform="winxp"):
  170. """
  171. Generates a random boot time based on a given timestamp and operating system
  172. :param timestamp: a timestamp in seconds
  173. :param platform: a platform as string as specified in check_platform above. default is winxp. this param is optional
  174. :return: timestamp of random boot time in seconds since EPOCH
  175. """
  176. check_platform(platform)
  177. if platform is "linux":
  178. uptime_in_days = lea.Lea.fromValFreqsDict({3: 50, 7: 25, 14: 12.5, 31: 6.25, 92: 3.125, 183: 1.5625,
  179. 365: 0.78125, 1461: 0.390625, 2922: 0.390625})
  180. elif platform is "macos":
  181. uptime_in_days = lea.Lea.fromValFreqsDict({7: 50, 14: 25, 31: 12.5, 92: 6.25, 183: 3.125, 365: 3.076171875,
  182. 1461: 0.048828125})
  183. else:
  184. uptime_in_days = lea.Lea.fromValFreqsDict({3: 50, 7: 25, 14: 12.5, 31: 6.25, 92: 3.125, 183: 1.5625,
  185. 365: 0.78125, 1461: 0.78125})
  186. timestamp -= rnd.randint(0, uptime_in_days.random() * 86400)
  187. return timestamp
  188. def get_rnd_x86_nop(count=1, side_effect_free=False, char_filter=set()):
  189. """
  190. Generates a specified number of x86 single-byte (pseudo-)NOPs
  191. :param count: The number of bytes to generate
  192. :param side_effect_free: Determines whether NOPs with side-effects (to registers or the stack) are allowed
  193. :param char_filter: A set of bytes which are forbidden to generate
  194. :return: Random x86 NOP bytestring
  195. """
  196. result = b''
  197. nops = x86_nops.copy()
  198. if not side_effect_free:
  199. nops |= x86_pseudo_nops.copy()
  200. if not isinstance(char_filter, set):
  201. char_filter = set(char_filter)
  202. nops = list(nops - char_filter)
  203. for i in range(0, count):
  204. result += nops[rnd.randint(0, len(nops) - 1)]
  205. return result
  206. def get_rnd_bytes(count=1, ignore=None):
  207. """
  208. Generates a specified number of random bytes while excluding unwanted bytes
  209. :param count: Number of wanted bytes
  210. :param ignore: The bytes, which should be ignored, as an array
  211. :return: Random bytestring
  212. """
  213. if ignore is None:
  214. ignore = []
  215. result = b''
  216. for i in range(0, count):
  217. char = os.urandom(1)
  218. while char in ignore:
  219. char = os.urandom(1)
  220. result += char
  221. return result
  222. def check_payload_len(payload_len: int, limit: int) -> None:
  223. """
  224. Checks if the len of the payload exceeds a given limit
  225. :param payload_len: The length of the payload
  226. :param limit: The limit of the length of the payload which is allowed
  227. """
  228. if payload_len > limit:
  229. raise ValueError("Custom payload too long: " + str(payload_len) +
  230. " bytes. Should be a maximum of " + str(limit) + " bytes.")
  231. def get_bytes_from_file(filepath):
  232. """
  233. Converts the content of a file into its byte representation
  234. The content of the file can either be a string or hexadecimal numbers/bytes (e.g. shellcode)
  235. The file must have the keyword "str" or "hex" in its first line to specify the rest of the content
  236. If the content is hex, whitespaces, backslashes, "x", quotation marks and "+" are removed
  237. Example for a hexadecimal input file:
  238. hex
  239. "abcd ef \xff10\ff 'xaa' x \ ab"
  240. Output: b'\xab\xcd\xef\xff\x10\xff\xaa\xab'
  241. :param filepath: The path of the file from which to get the bytes
  242. :return: The bytes of the file (either a byte representation of a string or the bytes contained in the file)
  243. """
  244. try:
  245. file = open(filepath)
  246. result_bytes = b''
  247. header = file.readline().strip()
  248. content = file.read()
  249. if header == "hex":
  250. content = content.replace(" ", "").replace("\n", "").replace("\\", "").replace("x", "").replace("\"", "") \
  251. .replace("'", "").replace("+", "").replace("\r", "")
  252. try:
  253. result_bytes = bytes.fromhex(content)
  254. except ValueError:
  255. print("\nERROR: Content of file is not all hexadecimal.")
  256. file.close()
  257. exit(1)
  258. elif header == "str":
  259. result_bytes = content.strip().encode()
  260. else:
  261. print("\nERROR: Invalid header found: " + header + ". Try 'hex' or 'str' followed by endline instead.")
  262. file.close()
  263. exit(1)
  264. for forbidden_char in forbidden_chars:
  265. if forbidden_char in result_bytes:
  266. print("\nERROR: Forbidden character found in payload: ", forbidden_char)
  267. file.close()
  268. exit(1)
  269. file.close()
  270. return result_bytes
  271. except FileNotFoundError:
  272. print("\nERROR: File not found: ", filepath)
  273. exit(1)
  274. def handle_most_used_outputs(most_used_x):
  275. """
  276. :param most_used_x: Element or list (e.g. from SQL-query output) which should only be one element
  277. :return: most_used_x if it's not a list. The first element of most_used_x after being sorted if it's a list.
  278. None if that list is empty.
  279. """
  280. if isinstance(most_used_x, list):
  281. if len(most_used_x) == 0:
  282. return None
  283. most_used_x.sort()
  284. return most_used_x[0]
  285. else:
  286. return most_used_x
  287. def get_attacker_config(ip_source_list, ip_address: str):
  288. """
  289. Returns the attacker configuration depending on the IP address, this includes the port for the next
  290. attacking packet and the previously used (fixed) TTL value.
  291. :param ip_source_list: List of source IPs
  292. :param ip_address: The IP address of the attacker
  293. :return: A tuple consisting of (port, ttlValue)
  294. """
  295. # Gamma distribution parameters derived from MAWI 13.8G dataset
  296. alpha, loc, beta = (2.3261710235, -0.188306914406, 44.4853123884)
  297. gd = stats.gamma.rvs(alpha, loc=loc, scale=beta, size=len(ip_source_list))
  298. # Determine port
  299. port = attacker_port_mapping.get(ip_address)
  300. if port is not None: # use next port
  301. next_port = attacker_port_mapping.get(ip_address) + 1
  302. if next_port > (2 ** 16 - 1):
  303. next_port = 1
  304. else: # generate starting port
  305. next_port = inet.RandShort()
  306. attacker_port_mapping[ip_address] = next_port
  307. # Determine TTL value
  308. ttl = attacker_ttl_mapping.get(ip_address)
  309. if ttl is None: # determine TTL value
  310. is_invalid = True
  311. pos = ip_source_list.index(ip_address)
  312. pos_max = len(gd)
  313. while is_invalid:
  314. ttl = int(round(gd[pos]))
  315. if 0 < ttl < 256: # validity check
  316. is_invalid = False
  317. else:
  318. pos = (pos + 1) % pos_max
  319. attacker_ttl_mapping[ip_address] = ttl
  320. # return port and TTL
  321. return next_port, ttl
  322. def remove_generic_ending(string):
  323. """"
  324. Returns the input string with it's ending cut off, in case it was a generic one
  325. :param string: Input string
  326. :return: Input string with ending cut off
  327. """
  328. for end in generic_attack_names:
  329. if string.endswith(end):
  330. return string[:-len(end)]
  331. return string