BaseAttack.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. import abc
  2. import csv
  3. import hashlib
  4. import ipaddress
  5. import math
  6. import os
  7. import random
  8. import random as rnd
  9. import re
  10. import socket
  11. import sys
  12. import tempfile
  13. import time
  14. import collections
  15. import typing as t
  16. # TODO: double check this import
  17. # does it complain because libpcapreader is not a .py?
  18. import ID2TLib.libpcapreader as pr
  19. import lea
  20. import numpy as np
  21. import scapy.layers.inet as inet
  22. import scapy.utils
  23. import Attack.AttackParameters as atkParam
  24. import ID2TLib.Utility as Util
  25. class BaseAttack(metaclass=abc.ABCMeta):
  26. """
  27. Abstract base class for all attack classes. Provides basic functionalities, like parameter validation.
  28. """
  29. ValuePair = collections.namedtuple('ValuePair', ['value', 'user_specified'])
  30. def __init__(self, name, description, attack_type):
  31. """
  32. To be called within the individual attack class to initialize the required parameters.
  33. :param name: The name of the attack class.
  34. :param description: A short description of the attack.
  35. :param attack_type: The type the attack belongs to, like probing/scanning, malware.
  36. """
  37. # Reference to statistics class
  38. self.statistics = None
  39. # Class fields
  40. self.attack_name = name
  41. self.attack_description = description
  42. self.attack_type = attack_type
  43. self.params = {}
  44. self.supported_params = {}
  45. self.attack_start_utime = 0
  46. self.attack_end_utime = 0
  47. self.start_time = 0
  48. self.finish_time = 0
  49. self.packets = []
  50. self.path_attack_pcap = ""
  51. def set_statistics(self, statistics):
  52. """
  53. Specify the statistics object that will be used to calculate the parameters of this attack.
  54. The statistics are used to calculate default parameters and to process user supplied
  55. queries.
  56. :param statistics: Reference to a statistics object.
  57. """
  58. self.statistics = statistics
  59. @abc.abstractmethod
  60. def init_params(self):
  61. """
  62. Initialize all required parameters taking into account user supplied values. If no value is supplied,
  63. or if a user defined query is supplied, use a statistics object to do the calculations.
  64. A call to this function requires a call to 'set_statistics' first.
  65. """
  66. pass
  67. @abc.abstractmethod
  68. def generate_attack_packets(self):
  69. """
  70. Creates the attack packets.
  71. """
  72. pass
  73. @abc.abstractmethod
  74. def generate_attack_pcap(self):
  75. """
  76. Creates a pcap containing the attack packets.
  77. :return: The location of the generated pcap file.
  78. """
  79. pass
  80. ################################################
  81. # HELPER VALIDATION METHODS
  82. # Used to validate the given parameter values
  83. ################################################
  84. @staticmethod
  85. def _is_mac_address(mac_address: t.Union[str, t.List[str]]) -> bool:
  86. """
  87. Verifies if the given string is a valid MAC address.
  88. Accepts the formats 00:80:41:ae:fd:7e and 00-80-41-ae-fd-7e.
  89. :param mac_address: The MAC address as string.
  90. :return: True if the MAC address is valid, otherwise False.
  91. """
  92. pattern = re.compile('^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$', re.MULTILINE)
  93. if isinstance(mac_address, list):
  94. for mac in mac_address:
  95. if re.match(pattern, mac) is None:
  96. return False
  97. else:
  98. if re.match(pattern, mac_address) is None:
  99. return False
  100. return True
  101. @staticmethod
  102. def _is_ip_address(ip_address: t.Union[str, t.List[str]]) -> t.Tuple[bool, t.Union[str, t.List[str]]]:
  103. """
  104. Verifies that the given string or list of IP addresses (strings) is a valid IPv4/IPv6 address.
  105. Accepts comma-separated lists of IP addresses, like "192.169.178.1, 192.168.178.2"
  106. :param ip_address: The IP address(es) as list of strings, comma-separated or dash-separated string.
  107. :return: True if all IP addresses are valid, otherwise False. And a list of IP addresses as string.
  108. """
  109. def append_ips(ip_address_input: t.List[str]) -> t.Tuple[bool, t.List[str]]:
  110. """
  111. Recursive appending function to handle lists and ranges of IP addresses.
  112. :param ip_address_input: The IP address(es) as list of strings, comma-separated or dash-separated string.
  113. :return: List of all given IP addresses.
  114. """
  115. ip_list = []
  116. is_valid = True
  117. for ip in ip_address_input:
  118. if '-' in ip:
  119. ip_range = ip.split('-')
  120. ip_range = Util.get_ip_range(ip_range[0], ip_range[1])
  121. is_valid, ips = append_ips(ip_range)
  122. ip_list.extend(ips)
  123. else:
  124. try:
  125. ipaddress.ip_address(ip)
  126. ip_list.append(ip)
  127. except ValueError:
  128. return False, ip_list
  129. return is_valid, ip_list
  130. # a comma-separated list of IP addresses must be split first
  131. if isinstance(ip_address, str):
  132. ip_address = ip_address.split(',')
  133. result, ip_address_output = append_ips(ip_address)
  134. if len(ip_address_output) == 1:
  135. return result, ip_address_output[0]
  136. else:
  137. return result, ip_address_output
  138. @staticmethod
  139. def _is_port(ports_input: t.Union[t.List[str], t.List[int], str, int])\
  140. -> t.Union[bool, t.Tuple[bool, t.List[t.Union[int, str]]]]:
  141. """
  142. Verifies if the given value is a valid port. Accepts port ranges, like 80-90, 80..99, 80...99.
  143. :param ports_input: The port number as int or string.
  144. :return: True if the port number is valid, otherwise False. If a single port or a comma-separated list of ports
  145. was given, a list of int is returned. If a port range was given, the range is resolved
  146. and a list of int is returned.
  147. """
  148. def _is_invalid_port(num: int) -> bool:
  149. """
  150. Checks whether the port number is invalid.
  151. :param num: The port number as int.
  152. :return: True if the port number is invalid, otherwise False.
  153. """
  154. return num < 1 or num > 65535
  155. if ports_input is None or ports_input is "":
  156. return False
  157. if isinstance(ports_input, str):
  158. ports_input = ports_input.replace(' ', '').split(',')
  159. elif isinstance(ports_input, int):
  160. ports_input = [ports_input]
  161. elif len(ports_input) is 0:
  162. return False
  163. ports_output = []
  164. for port_entry in ports_input:
  165. if isinstance(port_entry, int):
  166. if _is_invalid_port(port_entry):
  167. return False
  168. ports_output.append(port_entry)
  169. # TODO: validate last condition
  170. elif isinstance(port_entry, str) and port_entry.isdigit():
  171. # port_entry describes a single port
  172. port_entry = int(port_entry)
  173. if _is_invalid_port(port_entry):
  174. return False
  175. ports_output.append(port_entry)
  176. elif '-' in port_entry or '..' in port_entry:
  177. # port_entry describes a port range
  178. # allowed format: '1-49151', '1..49151', '1...49151'
  179. match = re.match(r'^([0-9]{1,5})(?:-|\.{2,3})([0-9]{1,5})$', str(port_entry))
  180. # check validity of port range
  181. # and create list of ports derived from given start and end port
  182. (port_start, port_end) = int(match.group(1)), int(match.group(2))
  183. if _is_invalid_port(port_start) or _is_invalid_port(port_end):
  184. return False
  185. else:
  186. ports_list = [i for i in range(port_start, port_end + 1)]
  187. # append ports at ports_output list
  188. ports_output += ports_list
  189. if len(ports_output) == 1:
  190. return True, ports_output[0]
  191. else:
  192. return True, ports_output
  193. @staticmethod
  194. def _is_timestamp(timestamp: str) -> bool:
  195. """
  196. Checks whether the given value is in a valid timestamp format. The accepted format is:
  197. YYYY-MM-DD h:m:s, whereas h, m, s may be one or two digits.
  198. :param timestamp: The timestamp to be checked.
  199. :return: True if the timestamp is valid, otherwise False.
  200. """
  201. is_valid = re.match(r'[0-9]{4}(?:-[0-9]{1,2}){2} (?:[0-9]{1,2}:){2}[0-9]{1,2}', timestamp)
  202. return is_valid is not None
  203. @staticmethod
  204. def _is_boolean(value):
  205. """
  206. Checks whether the given value (string or bool) is a boolean. Strings are valid booleans if they are in:
  207. {y, yes, t, true, on, 1, n, no, f, false, off, 0}.
  208. :param value: The value to be checked.
  209. :return: True if the value is a boolean, otherwise false. And the casted boolean.
  210. """
  211. # If value is already a boolean
  212. if isinstance(value, bool):
  213. return True, value
  214. # If value is a string
  215. # True values are y, yes, t, true, on and 1;
  216. # False values are n, no, f, false, off and 0.
  217. # Raises ValueError if value is anything else.
  218. try:
  219. import distutils.core
  220. import distutils.util
  221. value = bool(distutils.util.strtobool(value.lower()))
  222. is_bool = True
  223. except ValueError:
  224. is_bool = False
  225. return is_bool, value
  226. @staticmethod
  227. def _is_float(value):
  228. """
  229. Checks whether the given value is a float.
  230. :param value: The value to be checked.
  231. :return: True if the value is a float, otherwise False. And the casted float.
  232. """
  233. try:
  234. value = float(value)
  235. return True, value
  236. except ValueError:
  237. return False, value
  238. @staticmethod
  239. def _is_domain(val: str) -> bool:
  240. """
  241. Verifies that the given string is a valid URI.
  242. :param val: The URI as string.
  243. :return: True if URI is valid, otherwise False.
  244. """
  245. domain = re.match(r'^(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+$', val)
  246. return domain is not None
  247. #########################################
  248. # HELPER METHODS
  249. #########################################
  250. @staticmethod
  251. def set_seed(seed):
  252. """
  253. :param seed: The random seed to be set.
  254. """
  255. seed_final = None
  256. if isinstance(seed, int):
  257. seed_final = seed
  258. elif isinstance(seed, str):
  259. if seed.isdigit():
  260. seed_final = int(seed)
  261. else:
  262. hashed_seed = hashlib.sha1(seed.encode()).digest()
  263. seed_final = int.from_bytes(hashed_seed, byteorder="little")
  264. if seed_final:
  265. random.seed(seed_final)
  266. np.random.seed(seed_final & 0xFFFFFFFF)
  267. def set_start_time(self) -> None:
  268. """
  269. Set the current time as global starting time.
  270. """
  271. self.start_time = time.time()
  272. def set_finish_time(self) -> None:
  273. """
  274. Set the current time as global finishing time.
  275. """
  276. self.finish_time = time.time()
  277. def get_packet_generation_time(self) -> float:
  278. """
  279. :return difference between starting and finishing time.
  280. """
  281. return self.finish_time - self.start_time
  282. def add_param_value(self, param, value, user_specified: bool = True) -> None:
  283. """
  284. Adds the pair param : value to the dictionary of attack parameters. Prints and error message and skips the
  285. parameter if the validation fails.
  286. :param param: Name of the parameter that we wish to modify.
  287. :param value: The value we wish to assign to the specified parameter.
  288. :param user_specified: Whether the value was specified by the user (or left default)
  289. :return: None.
  290. """
  291. # by default no param is valid
  292. is_valid = False
  293. # get AttackParameters instance associated with param
  294. # for default values assigned in attack classes, like Parameter.PORT_OPEN
  295. if isinstance(param, atkParam.Parameter):
  296. param_name = param
  297. # for values given by user input, like port.open
  298. else:
  299. # Get Enum key of given string identifier
  300. param_name = atkParam.Parameter(param)
  301. # Get parameter type of attack's required_params
  302. param_type = self.supported_params.get(param_name)
  303. # Verify validity of given value with respect to parameter type
  304. if param_type is None:
  305. print('Parameter ' + str(param_name) + ' not available for chosen attack. Skipping parameter.')
  306. # If value is query -> get value from database
  307. elif param_name != atkParam.Parameter.INTERVAL_SELECT_STRATEGY and self.statistics.is_query(value):
  308. value = self.statistics.process_db_query(value, False)
  309. if value is not None and value is not "":
  310. is_valid = True
  311. else:
  312. print('Error in given parameter value: ' + str(value) + '. Data could not be retrieved.')
  313. # Validate parameter depending on parameter's type
  314. elif param_type == atkParam.ParameterTypes.TYPE_IP_ADDRESS:
  315. is_valid, value = self._is_ip_address(value)
  316. elif param_type == atkParam.ParameterTypes.TYPE_PORT:
  317. is_valid, value = self._is_port(value)
  318. elif param_type == atkParam.ParameterTypes.TYPE_MAC_ADDRESS:
  319. is_valid = self._is_mac_address(value)
  320. elif param_type == atkParam.ParameterTypes.TYPE_INTEGER_POSITIVE:
  321. if isinstance(value, int) and int(value) >= 0:
  322. is_valid = True
  323. elif isinstance(value, str) and value.isdigit() and int(value) >= 0:
  324. is_valid = True
  325. value = int(value)
  326. elif param_type == atkParam.ParameterTypes.TYPE_STRING:
  327. if isinstance(value, str):
  328. is_valid = True
  329. elif param_type == atkParam.ParameterTypes.TYPE_FLOAT:
  330. is_valid, value = self._is_float(value)
  331. # this is required to avoid that the timestamp's microseconds of the first attack packet is '000000'
  332. # but microseconds are only chosen randomly if the given parameter does not already specify it
  333. # e.g. inject.at-timestamp=123456.987654 -> is not changed
  334. # e.g. inject.at-timestamp=123456 -> is changed to: 123456.[random digits]
  335. if param_name == atkParam.Parameter.INJECT_AT_TIMESTAMP and is_valid and ((value - int(value)) == 0):
  336. value = value + random.uniform(0, 0.999999)
  337. elif param_type == atkParam.ParameterTypes.TYPE_TIMESTAMP:
  338. is_valid = self._is_timestamp(value)
  339. elif param_type == atkParam.ParameterTypes.TYPE_BOOLEAN:
  340. is_valid, value = self._is_boolean(value)
  341. elif param_type == atkParam.ParameterTypes.TYPE_PACKET_POSITION:
  342. # This function call is valid only if there is a statistics object available.
  343. if self.statistics is None:
  344. print('Error: Statistics-dependent attack parameter added without setting a statistics object first.')
  345. exit(1)
  346. ts = pr.pcap_processor(self.statistics.pcap_filepath, "False").get_timestamp_mu_sec(int(value))
  347. if 0 <= int(value) <= self.statistics.get_packet_count() and ts >= 0:
  348. is_valid = True
  349. param_name = atkParam.Parameter.INJECT_AT_TIMESTAMP
  350. value = (ts / 1000000) # convert microseconds from getTimestampMuSec into seconds
  351. elif param_type == atkParam.ParameterTypes.TYPE_DOMAIN:
  352. is_valid = self._is_domain(value)
  353. elif param_type == atkParam.ParameterTypes.TYPE_FILEPATH:
  354. is_valid = os.path.isfile(value)
  355. elif param_type == atkParam.ParameterTypes.TYPE_PERCENTAGE:
  356. is_valid_float, value = self._is_float(value)
  357. if is_valid_float:
  358. is_valid = value >= 0 and value <= 1
  359. else:
  360. is_valid = False
  361. elif param_type == atkParam.ParameterTypes.TYPE_PADDING:
  362. if isinstance(value, int):
  363. is_valid = value >= 0 and value <= 100
  364. elif isinstance(value, str) and value.isdigit():
  365. value = int(value)
  366. is_valid = value >= 0 and value <= 100
  367. elif param_type == atkParam.ParameterTypes.TYPE_INTERVAL_SELECT_STRAT:
  368. is_valid = value in {"random", "optimal", "custom"}
  369. # add value iff validation was successful
  370. if is_valid:
  371. self.params[param_name] = self.ValuePair(value, user_specified)
  372. else:
  373. print("ERROR: Parameter " + str(param) + " or parameter value " + str(value) +
  374. " not valid. Skipping parameter.")
  375. def get_param_value(self, param: atkParam.Parameter):
  376. """
  377. Returns the parameter value for a given parameter.
  378. :param param: The parameter whose value is wanted.
  379. :return: The parameter's value.
  380. """
  381. parameter = self.params.get(param)
  382. if parameter is not None:
  383. return parameter.value
  384. else:
  385. return None
  386. def get_param_user_specified(self, param: atkParam.Parameter) -> bool:
  387. """
  388. Returns whether the parameter value was specified by the user for a given parameter.
  389. :param param: The parameter whose user-specified flag is wanted.
  390. :return: The parameter's user-specified flag.
  391. """
  392. parameter = self.params.get(param)
  393. if parameter is not None:
  394. return parameter.user_specified
  395. else:
  396. return False
  397. def check_parameters(self):
  398. """
  399. Checks whether all parameter values are defined. If a value is not defined, the application is terminated.
  400. However, this should not happen as all attack should define default parameter values.
  401. """
  402. # parameters which do not require default values
  403. non_obligatory_params = [atkParam.Parameter.INJECT_AFTER_PACKET, atkParam.Parameter.NUMBER_ATTACKERS]
  404. for param, param_type in self.supported_params.items():
  405. # checks whether all params have assigned values, INJECT_AFTER_PACKET must not be considered because the
  406. # timestamp derived from it is set to Parameter.INJECT_AT_TIMESTAMP
  407. if param not in self.params.keys() and param not in non_obligatory_params:
  408. print("\033[91mCRITICAL ERROR: Attack '" + self.attack_name + "' does not define the parameter '" +
  409. str(param) + "'.\n The attack must define default values for all parameters."
  410. + "\n Cannot continue attack generation.\033[0m")
  411. import sys
  412. sys.exit(0)
  413. def write_attack_pcap(self, packets: list, append_flag: bool = False, destination_path: str = None):
  414. """
  415. Writes the attack's packets into a PCAP file with a temporary filename.
  416. :return: The path of the written PCAP file.
  417. """
  418. # Only check params initially when attack generation starts
  419. if append_flag is False and destination_path is None:
  420. # Check if all req. parameters are set
  421. self.check_parameters()
  422. # Determine destination path
  423. if destination_path is not None and os.path.exists(destination_path):
  424. destination = destination_path
  425. else:
  426. temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.pcap')
  427. destination = temp_file.name
  428. # Write packets into pcap file
  429. pktdump = scapy.utils.PcapWriter(destination, append=append_flag)
  430. pktdump.write(packets)
  431. # Store pcap path and close file objects
  432. pktdump.close()
  433. return destination
  434. def get_reply_delay(self, ip_dst, default = 2000):
  435. """
  436. Gets the minimum and the maximum reply delay for all the connections of a specific IP.
  437. :param ip_dst: The IP to reterive its reply delay.
  438. :param default: The default value to return if no delay could be fount. If < 0 raise an exception instead
  439. :return minDelay: minimum delay
  440. :return maxDelay: maximum delay
  441. """
  442. result = self.statistics.process_db_query(
  443. "SELECT AVG(minDelay), AVG(maxDelay) FROM conv_statistics WHERE ipAddressB='" + ip_dst + "';")
  444. if result[0][0] and result[0][1]:
  445. min_delay = result[0][0]
  446. max_delay = result[0][1]
  447. else:
  448. all_min_delays = self.statistics.process_db_query("SELECT minDelay FROM conv_statistics LIMIT 500;")
  449. min_delay = np.median(all_min_delays)
  450. all_max_delays = self.statistics.process_db_query("SELECT maxDelay FROM conv_statistics LIMIT 500;")
  451. max_delay = np.median(all_max_delays)
  452. if math.isnan(min_delay): # max_delay is nan too then
  453. if default < 0:
  454. raise ValueError("Could not calculate min/max_delay")
  455. min_delay = default
  456. max_delay = default
  457. min_delay = int(min_delay) * 10 ** -6 # convert from micro to seconds
  458. max_delay = int(max_delay) * 10 ** -6
  459. return min_delay, max_delay
  460. @staticmethod
  461. def packets_to_convs(exploit_raw_packets):
  462. """
  463. Classifies a bunch of packets to conversations groups. A conversation is a set of packets go between host A
  464. (IP,port) to host B (IP,port)
  465. :param exploit_raw_packets: A set of packets contains several conversations.
  466. :return conversations: A set of arrays, each array contains the packet of specific conversation
  467. :return orderList_conversations: An array contains the conversations ids (IP_A,port_A, IP_b,port_B) in the
  468. order they appeared in the original packets.
  469. """
  470. conversations = {}
  471. order_list_conversations = []
  472. for pkt_num, pkt in enumerate(exploit_raw_packets):
  473. eth_frame = inet.Ether(pkt[0])
  474. ip_pkt = eth_frame.payload
  475. ip_dst = ip_pkt.getfieldval("dst")
  476. ip_src = ip_pkt.getfieldval("src")
  477. tcp_pkt = ip_pkt.payload
  478. port_dst = tcp_pkt.getfieldval("dport")
  479. port_src = tcp_pkt.getfieldval("sport")
  480. conv_req = (ip_src, port_src, ip_dst, port_dst)
  481. conv_rep = (ip_dst, port_dst, ip_src, port_src)
  482. if conv_req not in conversations and conv_rep not in conversations:
  483. pkt_list = [pkt]
  484. conversations[conv_req] = pkt_list
  485. # Order list of conv
  486. order_list_conversations.append(conv_req)
  487. else:
  488. if conv_req in conversations:
  489. pkt_list = conversations[conv_req]
  490. pkt_list.append(pkt)
  491. conversations[conv_req] = pkt_list
  492. else:
  493. pkt_list = conversations[conv_rep]
  494. pkt_list.append(pkt)
  495. conversations[conv_rep] = pkt_list
  496. return conversations, order_list_conversations
  497. @staticmethod
  498. def is_valid_ip_address(addr):
  499. """
  500. Checks if the IP address family is supported.
  501. :param addr: IP address to be checked.
  502. :return: Boolean
  503. """
  504. try:
  505. socket.inet_aton(addr)
  506. return True
  507. except socket.error:
  508. return False
  509. @staticmethod
  510. def ip_src_dst_equal_check(ip_source, ip_destination):
  511. """
  512. Checks if the source IP and destination IP are equal.
  513. :param ip_source: source IP address.
  514. :param ip_destination: destination IP address.
  515. """
  516. equal = False
  517. if isinstance(ip_source, list):
  518. if ip_destination in ip_source:
  519. equal = True
  520. else:
  521. if ip_source == ip_destination:
  522. equal = True
  523. if equal:
  524. print("\nERROR: Invalid IP addresses; source IP is the same as destination IP: " + ip_destination + ".")
  525. sys.exit(0)
  526. @staticmethod
  527. def get_inter_arrival_time(packets, distribution: bool = False):
  528. """
  529. Gets the inter-arrival times array and its distribution of a set of packets.
  530. :param packets: the packets to extract their inter-arrival time.
  531. :param distribution: build distribution dictionary or not
  532. :return inter_arrival_times: array of the inter-arrival times
  533. :return dict: the inter-arrival time distribution as a histogram {inter-arrival time:frequency}
  534. """
  535. inter_arrival_times = []
  536. prvs_pkt_time = 0
  537. for index, pkt in enumerate(packets):
  538. timestamp = pkt[2][0] + pkt[2][1] / 10 ** 6
  539. if index == 0:
  540. prvs_pkt_time = timestamp
  541. inter_arrival_times.append(0)
  542. else:
  543. inter_arrival_times.append(timestamp - prvs_pkt_time)
  544. prvs_pkt_time = timestamp
  545. if distribution:
  546. # Build a distribution dictionary
  547. freq, values = np.histogram(inter_arrival_times, bins=20)
  548. dist_dict = {}
  549. for i, val in enumerate(values):
  550. if i < len(freq):
  551. dist_dict[str(val)] = freq[i]
  552. return inter_arrival_times, dist_dict
  553. else:
  554. return inter_arrival_times
  555. @staticmethod
  556. def clean_white_spaces(str_param):
  557. """
  558. Delete extra backslash from white spaces. This function is used to process the payload of packets.
  559. :param str_param: the payload to be processed.
  560. """
  561. str_param = str_param.replace("\\n", "\n")
  562. str_param = str_param.replace("\\r", "\r")
  563. str_param = str_param.replace("\\t", "\t")
  564. str_param = str_param.replace("\\\'", "\'")
  565. return str_param
  566. def modify_http_header(self, str_tcp_seg, orig_target_uri, target_uri, orig_ip_dst, target_host):
  567. """
  568. Substitute the URI and HOST in a HTTP header with new values.
  569. :param str_tcp_seg: the payload to be processed.
  570. :param orig_target_uri: old URI
  571. :param target_uri: new URI
  572. :param orig_ip_dst: old host
  573. :param target_host: new host
  574. """
  575. if len(str_tcp_seg) > 0:
  576. # convert payload bytes to str => str = "b'..\\r\\n..'"
  577. str_tcp_seg = str_tcp_seg[2:-1]
  578. str_tcp_seg = str_tcp_seg.replace(orig_target_uri, target_uri)
  579. str_tcp_seg = str_tcp_seg.replace(orig_ip_dst, target_host)
  580. str_tcp_seg = self.clean_white_spaces(str_tcp_seg)
  581. return str_tcp_seg
  582. def get_ip_data(self, ip_address: str):
  583. """
  584. :param ip_address: the ip of which (packet-)data shall be returned
  585. :return: MSS, TTL and Window Size values of the given IP
  586. """
  587. # Set MSS (Maximum Segment Size) based on MSS distribution of IP address
  588. mss_dist = self.statistics.get_mss_distribution(ip_address)
  589. if len(mss_dist) > 0:
  590. mss_prob_dict = lea.Lea.fromValFreqsDict(mss_dist)
  591. mss_value = mss_prob_dict.random()
  592. else:
  593. mss_value = Util.handle_most_used_outputs(self.statistics.process_db_query("most_used(mssValue)"))
  594. # Set TTL based on TTL distribution of IP address
  595. ttl_dist = self.statistics.get_ttl_distribution(ip_address)
  596. if len(ttl_dist) > 0:
  597. ttl_prob_dict = lea.Lea.fromValFreqsDict(ttl_dist)
  598. ttl_value = ttl_prob_dict.random()
  599. else:
  600. ttl_value = Util.handle_most_used_outputs(self.statistics.process_db_query("most_used(ttlValue)"))
  601. # Set Window Size based on Window Size distribution of IP address
  602. win_dist = self.statistics.get_win_distribution(ip_address)
  603. if len(win_dist) > 0:
  604. win_prob_dict = lea.Lea.fromValFreqsDict(win_dist)
  605. win_value = win_prob_dict.random()
  606. else:
  607. win_value = Util.handle_most_used_outputs(self.statistics.process_db_query("most_used(winSize)"))
  608. return mss_value, ttl_value, win_value
  609. #########################################
  610. # RANDOM IP/MAC ADDRESS GENERATORS
  611. #########################################
  612. @staticmethod
  613. def generate_random_ipv4_address(ip_class, n: int = 1):
  614. # TODO: document ip_class
  615. """
  616. Generates n random IPv4 addresses.
  617. :param ip_class:
  618. :param n: The number of IP addresses to be generated
  619. :return: A single IP address, or if n>1, a list of IP addresses
  620. """
  621. def is_invalid(ip_address_param: ipaddress.IPv4Address):
  622. """
  623. TODO FILL ME
  624. :param ip_address_param:
  625. :return:
  626. """
  627. return ip_address_param.is_multicast or ip_address_param.is_unspecified or ip_address_param.is_loopback or \
  628. ip_address_param.is_link_local or ip_address_param.is_reserved or ip_address_param.is_private
  629. # Generate a random IP from specific class
  630. def generate_address(ip_class_param):
  631. """
  632. TODO FILL ME
  633. :param ip_class_param:
  634. :return:
  635. """
  636. if ip_class_param == "Unknown":
  637. return ipaddress.IPv4Address(random.randint(0, 2 ** 32 - 1))
  638. else:
  639. # For DDoS attack, we do not generate private IPs
  640. if "private" in ip_class_param:
  641. ip_class_param = ip_class_param[0] # convert A-private to A
  642. ip_classes_byte1 = {"A": {1, 126}, "B": {128, 191}, "C": {192, 223}, "D": {224, 239}, "E": {240, 254}}
  643. temp = list(ip_classes_byte1[ip_class_param])
  644. min_b1 = temp[0]
  645. max_b1 = temp[1]
  646. b1 = random.randint(min_b1, max_b1)
  647. b2 = random.randint(1, 255)
  648. b3 = random.randint(1, 255)
  649. b4 = random.randint(1, 255)
  650. ip_address = ipaddress.IPv4Address(str(b1) + "." + str(b2) + "." + str(b3) + "." + str(b4))
  651. return ip_address
  652. ip_addresses = []
  653. for i in range(0, n):
  654. address = generate_address(ip_class)
  655. while is_invalid(address):
  656. address = generate_address(ip_class)
  657. ip_addresses.append(str(address))
  658. if n == 1:
  659. return ip_addresses[0]
  660. else:
  661. return ip_addresses
  662. @staticmethod
  663. def generate_random_ipv6_address(n: int = 1):
  664. """
  665. Generates n random IPv6 addresses.
  666. :param n: The number of IP addresses to be generated
  667. :return: A single IP address, or if n>1, a list of IP addresses
  668. """
  669. def is_invalid(ip_address: ipaddress.IPv6Address):
  670. """
  671. TODO FILL ME
  672. :param ip_address:
  673. :return:
  674. """
  675. return ip_address.is_multicast or ip_address.is_unspecified or ip_address.is_loopback or \
  676. ip_address.is_link_local or ip_address.is_private or ip_address.is_reserved
  677. def generate_address():
  678. """
  679. TODO FILL ME
  680. :return:
  681. """
  682. return ipaddress.IPv6Address(random.randint(0, 2 ** 128 - 1))
  683. ip_addresses = []
  684. for i in range(0, n):
  685. address = generate_address()
  686. while is_invalid(address):
  687. address = generate_address()
  688. ip_addresses.append(str(address))
  689. if n == 1:
  690. return ip_addresses[0]
  691. else:
  692. return ip_addresses
  693. @staticmethod
  694. def generate_random_mac_address(n: int = 1):
  695. """
  696. Generates n random MAC addresses.
  697. :param n: The number of MAC addresses to be generated.
  698. :return: A single MAC address, or if n>1, a list of MAC addresses
  699. """
  700. def is_invalid(address_param: str):
  701. first_octet = int(address_param[0:2], 16)
  702. is_multicast_address = bool(first_octet & 0b01)
  703. is_locally_administered = bool(first_octet & 0b10)
  704. return is_multicast_address or is_locally_administered
  705. def generate_address():
  706. # FIXME: cleanup
  707. mac = [random.randint(0x00, 0xff) for i in range(0, 6)]
  708. return ':'.join(map(lambda x: "%02x" % x, mac))
  709. mac_addresses = []
  710. for i in range(0, n):
  711. address = generate_address()
  712. while is_invalid(address):
  713. address = generate_address()
  714. mac_addresses.append(address)
  715. if n == 1:
  716. return mac_addresses[0]
  717. else:
  718. return mac_addresses
  719. @staticmethod
  720. def get_ports_from_nmap_service_dst(ports_num):
  721. """
  722. Read the most ports_num frequently open ports from nmap-service-tcp file to be used in the port scan.
  723. :return: Ports numbers to be used as default destination ports or default open ports in the port scan.
  724. """
  725. ports_dst = []
  726. file = open(Util.RESOURCE_DIR + 'nmap-services-tcp.csv', 'rt')
  727. spamreader = csv.reader(file, delimiter=',')
  728. for count in range(ports_num):
  729. # escape first row (header)
  730. next(spamreader)
  731. # save ports numbers
  732. ports_dst.append(next(spamreader)[0])
  733. file.close()
  734. # rnd.shuffle ports numbers partially
  735. if ports_num == 1000: # used for port.dst
  736. # FIXME: cleanup
  737. temp_array = [[0 for i in range(10)] for i in range(100)]
  738. port_dst_shuffled = []
  739. for count in range(0, 10):
  740. temp_array[count] = ports_dst[count * 100:(count + 1) * 100]
  741. rnd.shuffle(temp_array[count])
  742. port_dst_shuffled += temp_array[count]
  743. else: # used for port.open
  744. rnd.shuffle(ports_dst)
  745. port_dst_shuffled = ports_dst
  746. return port_dst_shuffled