BaseAttack.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. # Aidmar
  2. from scapy.layers.inet import Ether
  3. import socket
  4. import sys
  5. from math import sqrt
  6. import ipaddress
  7. import os
  8. import random
  9. import re
  10. import tempfile
  11. from abc import abstractmethod, ABCMeta
  12. import numpy as np # TO-DO: it needs to be added to required packages
  13. import ID2TLib.libpcapreader as pr
  14. from scapy.utils import PcapWriter
  15. from Attack import AttackParameters
  16. from Attack.AttackParameters import Parameter
  17. from Attack.AttackParameters import ParameterTypes
  18. class BaseAttack(metaclass=ABCMeta):
  19. """
  20. Abstract base class for all attack classes. Provides basic functionalities, like parameter validation.
  21. """
  22. def __init__(self, statistics, name, description, attack_type):
  23. """
  24. To be called within the individual attack class to initialize the required parameters.
  25. :param statistics: A reference to the Statistics class.
  26. :param name: The name of the attack class.
  27. :param description: A short description of the attack.
  28. :param attack_type: The type the attack belongs to, like probing/scanning, malware.
  29. """
  30. # Reference to statistics class
  31. self.statistics = statistics
  32. # Class fields
  33. self.attack_name = name
  34. self.attack_description = description
  35. self.attack_type = attack_type
  36. self.params = {}
  37. self.supported_params = {}
  38. self.attack_start_utime = 0
  39. self.attack_end_utime = 0
  40. @abstractmethod
  41. def generate_attack_pcap(self):
  42. """
  43. Creates a pcap containing the attack packets.
  44. :return: The location of the generated pcap file.
  45. """
  46. pass
  47. ################################################
  48. # HELPER VALIDATION METHODS
  49. # Used to validate the given parameter values
  50. ################################################
  51. @staticmethod
  52. def _is_mac_address(mac_address: str):
  53. """
  54. Verifies if the given string is a valid MAC address. Accepts the formats 00:80:41:ae:fd:7e and 00-80-41-ae-fd-7e.
  55. :param mac_address: The MAC address as string.
  56. :return: True if the MAC address is valid, otherwise False.
  57. """
  58. pattern = re.compile('^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$', re.MULTILINE)
  59. if isinstance(mac_address, list):
  60. for mac in mac_address:
  61. if re.match(pattern, mac) is None:
  62. return False
  63. else:
  64. if re.match(pattern, mac_address) is None:
  65. return False
  66. return True
  67. @staticmethod
  68. def _is_ip_address(ip_address: str):
  69. """
  70. Verifies that the given string or list of IP addresses (strings) is a valid IPv4/IPv6 address.
  71. Accepts comma-separated lists of IP addresses, like "192.169.178.1, 192.168.178.2"
  72. :param ip_address: The IP address(es) as list of strings or comma-separated string.
  73. :return: True if all IP addresses are valid, otherwise False. And a list of IP addresses as string.
  74. """
  75. ip_address_output = []
  76. # a comma-separated list of IP addresses must be splitted first
  77. if isinstance(ip_address, str):
  78. ip_address = ip_address.split(',')
  79. for ip in ip_address:
  80. try:
  81. ipaddress.ip_address(ip)
  82. ip_address_output.append(ip)
  83. except ValueError:
  84. return False, ip_address_output
  85. if len(ip_address_output) == 1:
  86. return True, ip_address_output[0]
  87. else:
  88. return True, ip_address_output
  89. @staticmethod
  90. def _is_port(ports_input: str):
  91. """
  92. Verifies if the given value is a valid port. Accepts port ranges, like 80-90, 80..99, 80...99.
  93. :param ports_input: The port number as int or string.
  94. :return: True if the port number is valid, otherwise False. If a single port or a comma-separated list of ports
  95. was given, a list of int is returned. If a port range was given, the range is resolved
  96. and a list of int is returned.
  97. """
  98. def _is_invalid_port(num):
  99. """
  100. Checks whether the port number is invalid.
  101. :param num: The port number as int.
  102. :return: True if the port number is invalid, otherwise False.
  103. """
  104. return num < 1 or num > 65535
  105. if isinstance(ports_input, str):
  106. ports_input = ports_input.replace(' ', '').split(',')
  107. elif isinstance(ports_input, int):
  108. ports_input = [ports_input]
  109. ports_output = []
  110. for port_entry in ports_input:
  111. if isinstance(port_entry, int):
  112. if _is_invalid_port(port_entry):
  113. return False
  114. ports_output.append(port_entry)
  115. elif isinstance(port_entry, str) and port_entry.isdigit():
  116. # port_entry describes a single port
  117. port_entry = int(port_entry)
  118. if _is_invalid_port(port_entry):
  119. return False
  120. ports_output.append(port_entry)
  121. elif '-' in port_entry or '..' in port_entry:
  122. # port_entry describes a port range
  123. # allowed format: '1-49151', '1..49151', '1...49151'
  124. match = re.match('^([0-9]{1,5})(?:-|\.{2,3})([0-9]{1,5})$', port_entry)
  125. # check validity of port range
  126. # and create list of ports derived from given start and end port
  127. (port_start, port_end) = int(match.group(1)), int(match.group(2))
  128. if _is_invalid_port(port_start) or _is_invalid_port(port_end):
  129. return False
  130. else:
  131. ports_list = [i for i in range(port_start, port_end + 1)]
  132. # append ports at ports_output list
  133. ports_output += ports_list
  134. if len(ports_output) == 1:
  135. return True, ports_output[0]
  136. else:
  137. return True, ports_output
  138. @staticmethod
  139. def _is_timestamp(timestamp: str):
  140. """
  141. Checks whether the given value is in a valid timestamp format. The accepted format is:
  142. YYYY-MM-DD h:m:s, whereas h, m, s may be one or two digits.
  143. :param timestamp: The timestamp to be checked.
  144. :return: True if the timestamp is valid, otherwise False.
  145. """
  146. is_valid = re.match('[0-9]{4}(?:-[0-9]{1,2}){2} (?:[0-9]{1,2}:){2}[0-9]{1,2}', timestamp)
  147. return is_valid is not None
  148. @staticmethod
  149. def _is_boolean(value):
  150. """
  151. Checks whether the given value (string or bool) is a boolean. Strings are valid booleans if they are in:
  152. {y, yes, t, true, on, 1, n, no, f, false, off, 0}.
  153. :param value: The value to be checked.
  154. :return: True if the value is a boolean, otherwise false. And the casted boolean.
  155. """
  156. # If value is already a boolean
  157. if isinstance(value, bool):
  158. return True, value
  159. # If value is a string
  160. # True values are y, yes, t, true, on and 1;
  161. # False values are n, no, f, false, off and 0.
  162. # Raises ValueError if value is anything else.
  163. try:
  164. import distutils.core
  165. value = distutils.util.strtobool(value.lower())
  166. is_bool = True
  167. except ValueError:
  168. is_bool = False
  169. return is_bool, value
  170. @staticmethod
  171. def _is_float(value):
  172. """
  173. Checks whether the given value is a float.
  174. :param value: The value to be checked.
  175. :return: True if the value is a float, otherwise False. And the casted float.
  176. """
  177. try:
  178. value = float(value)
  179. return True, value
  180. except ValueError:
  181. return False, value
  182. # Aidmar
  183. @staticmethod
  184. def _is_domain(val: str):
  185. """
  186. Verifies that the given string is a valid URI.
  187. :param uri: The URI as string.
  188. :return: True if URI is valid, otherwise False.
  189. """
  190. domain = re.match('^(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+$', val)
  191. return (domain is not None)
  192. #########################################
  193. # HELPER METHODS
  194. #########################################
  195. def add_param_value(self, param, value):
  196. """
  197. Adds the pair param : value to the dictionary of attack parameters. Prints and error message and skips the
  198. parameter if the validation fails.
  199. :param param: The parameter name.
  200. :param value: The parameter's value.
  201. :return: None.
  202. """
  203. # by default no param is valid
  204. is_valid = False
  205. # get AttackParameters instance associated with param
  206. # for default values assigned in attack classes, like Parameter.PORT_OPEN
  207. if isinstance(param, AttackParameters.Parameter):
  208. param_name = param
  209. # for values given by user input, like port.open
  210. else:
  211. # Get Enum key of given string identifier
  212. param_name = AttackParameters.Parameter(param)
  213. # Get parameter type of attack's required_params
  214. param_type = self.supported_params.get(param_name)
  215. # Verify validity of given value with respect to parameter type
  216. if param_type is None:
  217. print('Parameter ' + str(param_name) + ' not available for chosen attack. Skipping parameter.')
  218. # If value is query -> get value from database
  219. elif self.statistics.is_query(value):
  220. value = self.statistics.process_db_query(value, False)
  221. if value is not None and value is not "":
  222. is_valid = True
  223. else:
  224. print('Error in given parameter value: ' + value + '. Data could not be retrieved.')
  225. # Validate parameter depending on parameter's type
  226. elif param_type == ParameterTypes.TYPE_IP_ADDRESS:
  227. is_valid, value = self._is_ip_address(value)
  228. elif param_type == ParameterTypes.TYPE_PORT:
  229. is_valid, value = self._is_port(value)
  230. elif param_type == ParameterTypes.TYPE_MAC_ADDRESS:
  231. is_valid = self._is_mac_address(value)
  232. elif param_type == ParameterTypes.TYPE_INTEGER_POSITIVE:
  233. if isinstance(value, int) and int(value) >= 0:
  234. is_valid = True
  235. elif isinstance(value, str) and value.isdigit() and int(value) >= 0:
  236. is_valid = True
  237. value = int(value)
  238. elif param_type == ParameterTypes.TYPE_FLOAT:
  239. is_valid, value = self._is_float(value)
  240. # this is required to avoid that the timestamp's microseconds of the first attack packet is '000000'
  241. # but microseconds are only chosen randomly if the given parameter does not already specify it
  242. # e.g. inject.at-timestamp=123456.987654 -> is not changed
  243. # e.g. inject.at-timestamp=123456 -> is changed to: 123456.[random digits]
  244. if param_name == Parameter.INJECT_AT_TIMESTAMP and is_valid and ((value - int(value)) == 0):
  245. value = value + random.uniform(0, 0.999999)
  246. elif param_type == ParameterTypes.TYPE_TIMESTAMP:
  247. is_valid = self._is_timestamp(value)
  248. elif param_type == ParameterTypes.TYPE_BOOLEAN:
  249. is_valid, value = self._is_boolean(value)
  250. elif param_type == ParameterTypes.TYPE_PACKET_POSITION:
  251. ts = pr.pcap_processor(self.statistics.pcap_filepath, "False").get_timestamp_mu_sec(int(value))
  252. if 0 <= int(value) <= self.statistics.get_packet_count() and ts >= 0:
  253. is_valid = True
  254. param_name = Parameter.INJECT_AT_TIMESTAMP
  255. value = (ts / 1000000) # convert microseconds from getTimestampMuSec into seconds
  256. # Aidmar
  257. elif param_type == ParameterTypes.TYPE_DOMAIN:
  258. is_valid = self._is_domain(value)
  259. # add value iff validation was successful
  260. if is_valid:
  261. self.params[param_name] = value
  262. else:
  263. print("ERROR: Parameter " + str(param) + " or parameter value " + str(value) +
  264. " not valid. Skipping parameter.")
  265. def get_param_value(self, param: Parameter):
  266. """
  267. Returns the parameter value for a given parameter.
  268. :param param: The parameter whose value is wanted.
  269. :return: The parameter's value.
  270. """
  271. return self.params.get(param)
  272. def check_parameters(self):
  273. """
  274. Checks whether all parameter values are defined. If a value is not defined, the application is terminated.
  275. However, this should not happen as all attack should define default parameter values.
  276. """
  277. # parameters which do not require default values
  278. non_obligatory_params = [Parameter.INJECT_AFTER_PACKET, Parameter.NUMBER_ATTACKERS]
  279. for param, type in self.supported_params.items():
  280. # checks whether all params have assigned values, INJECT_AFTER_PACKET must not be considered because the
  281. # timestamp derived from it is set to Parameter.INJECT_AT_TIMESTAMP
  282. if param not in self.params.keys() and param not in non_obligatory_params:
  283. print("\033[91mCRITICAL ERROR: Attack '" + self.attack_name + "' does not define the parameter '" +
  284. str(param) + "'.\n The attack must define default values for all parameters."
  285. + "\n Cannot continue attack generation.\033[0m")
  286. import sys
  287. sys.exit(0)
  288. def write_attack_pcap(self, packets: list, append_flag: bool = False, destination_path: str = None):
  289. """
  290. Writes the attack's packets into a PCAP file with a temporary filename.
  291. :return: The path of the written PCAP file.
  292. """
  293. # Only check params initially when attack generation starts
  294. if append_flag is False and destination_path is None:
  295. # Check if all req. parameters are set
  296. self.check_parameters()
  297. # Determine destination path
  298. if destination_path is not None and os.path.exists(destination_path):
  299. destination = destination_path
  300. else:
  301. temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.pcap')
  302. destination = temp_file.name
  303. # Write packets into pcap file
  304. pktdump = PcapWriter(destination, append=append_flag)
  305. pktdump.write(packets)
  306. # Store pcap path and close file objects
  307. pktdump.close()
  308. return destination
  309. #########################################
  310. # RANDOM IP/MAC ADDRESS GENERATORS
  311. #########################################
  312. @staticmethod
  313. def generate_random_ipv4_address(ipClass, n: int = 1):
  314. """
  315. Generates n random IPv4 addresses.
  316. :param n: The number of IP addresses to be generated
  317. :return: A single IP address, or if n>1, a list of IP addresses
  318. """
  319. def is_invalid(ipAddress: ipaddress.IPv4Address):
  320. return ipAddress.is_multicast or ipAddress.is_unspecified or ipAddress.is_loopback or \
  321. ipAddress.is_link_local or ipAddress.is_reserved or ipAddress.is_private
  322. # Aidmar - generate a random IP from specific class
  323. def generate_address(ipClass):
  324. if ipClass == "Unknown":
  325. return ipaddress.IPv4Address(random.randint(0, 2 ** 32 - 1))
  326. else:
  327. # For DDoS attack, we do not generate private IPs
  328. if "private" in ipClass:
  329. ipClass = ipClass[0] # convert A-private to A
  330. ipClassesByte1 = {"A": {1,126}, "B": {128,191}, "C":{192, 223}, "D":{224, 239}, "E":{240, 254}}
  331. temp = list(ipClassesByte1[ipClass])
  332. minB1 = temp[0]
  333. maxB1 = temp[1]
  334. b1 = random.randint(minB1, maxB1)
  335. b2 = random.randint(1, 255)
  336. b3 = random.randint(1, 255)
  337. b4 = random.randint(1, 255)
  338. ipAddress = ipaddress.IPv4Address(str(b1) +"."+ str(b2) + "." + str(b3) + "." + str(b4))
  339. return ipAddress
  340. ip_addresses = []
  341. for i in range(0, n):
  342. address = generate_address(ipClass)
  343. while is_invalid(address):
  344. address = generate_address(ipClass)
  345. ip_addresses.append(str(address))
  346. if n == 1:
  347. return ip_addresses[0]
  348. else:
  349. return ip_addresses
  350. @staticmethod
  351. def generate_random_ipv6_address(n: int = 1):
  352. """
  353. Generates n random IPv6 addresses.
  354. :param n: The number of IP addresses to be generated
  355. :return: A single IP address, or if n>1, a list of IP addresses
  356. """
  357. def is_invalid(ipAddress: ipaddress.IPv6Address):
  358. return ipAddress.is_multicast or ipAddress.is_unspecified or ipAddress.is_loopback or \
  359. ipAddress.is_link_local or ipAddress.is_private or ipAddress.is_reserved
  360. def generate_address():
  361. return ipaddress.IPv6Address(random.randint(0, 2 ** 128 - 1))
  362. ip_addresses = []
  363. for i in range(0, n):
  364. address = generate_address()
  365. while is_invalid(address):
  366. address = generate_address()
  367. ip_addresses.append(str(address))
  368. if n == 1:
  369. return ip_addresses[0]
  370. else:
  371. return ip_addresses
  372. @staticmethod
  373. def generate_random_mac_address(n: int = 1):
  374. """
  375. Generates n random MAC addresses.
  376. :param n: The number of MAC addresses to be generated.
  377. :return: A single MAC addres, or if n>1, a list of MAC addresses
  378. """
  379. def is_invalid(address: str):
  380. first_octet = int(address[0:2], 16)
  381. is_multicast_address = bool(first_octet & 0b01)
  382. is_locally_administered = bool(first_octet & 0b10)
  383. return is_multicast_address or is_locally_administered
  384. def generate_address():
  385. mac = [random.randint(0x00, 0xff) for i in range(0, 6)]
  386. return ':'.join(map(lambda x: "%02x" % x, mac))
  387. mac_addresses = []
  388. for i in range(0, n):
  389. address = generate_address()
  390. while is_invalid(address):
  391. address = generate_address()
  392. mac_addresses.append(address)
  393. if n == 1:
  394. return mac_addresses[0]
  395. else:
  396. return mac_addresses
  397. # Aidmar
  398. def get_reply_delay(self, ip_dst):
  399. """
  400. Gets the minimum and the maximum reply delay for all the connections of a specific IP.
  401. :param ip_dst: The IP to reterive its reply delay.
  402. :return minDelay: minimum delay
  403. :return maxDelay: maximum delay
  404. """
  405. result = self.statistics.process_db_query(
  406. "SELECT standardDeviationDelay, minDelay, maxDelay FROM conv_statistics WHERE ipAddressB='" + ip_dst + "' LIMIT 1;")
  407. if result:
  408. standardDeviationDelay = result[0][0]
  409. minDelay = result[0][1]
  410. maxDelay = result[0][2]
  411. else:
  412. allMinDelays = self.statistics.process_db_query("SELECT minDelay FROM conv_statistics LIMIT 500;")
  413. minDelay = np.median(allMinDelays)
  414. allMaxDelays = self.statistics.process_db_query("SELECT maxDelay FROM conv_statistics LIMIT 500;")
  415. maxDelay = np.median(allMaxDelays)
  416. allStandardDeviationDelay = self.statistics.process_db_query("SELECT standardDeviationDelay FROM conv_statistics LIMIT 500;")
  417. standardDeviationDelay = np.median(allStandardDeviationDelay)
  418. minDelay = int(minDelay) * 10 ** -6 # convert from micro to seconds
  419. maxDelay = int(maxDelay) * 10 ** -6
  420. standardDeviationDelay = int(standardDeviationDelay) * 10 ** -6
  421. return minDelay, maxDelay, standardDeviationDelay
  422. # Group the packets in conversations
  423. def packetsToConvs(self,exploit_raw_packets):
  424. """
  425. Classifies a bunch of packets to conversations groups. A conversation is a set of packets go between host A (IP,port)
  426. to host B (IP,port)
  427. :param exploit_raw_packets: A set of packets contains several conversations.
  428. :return conversations: A set of arrays, each array contains the packet of specifc conversation
  429. :return orderList_conversations: An array contains the conversations ids (IP_A,port_A, IP_b,port_B) in the order
  430. they appeared in the original packets.
  431. """
  432. conversations = {}
  433. orderList_conversations = []
  434. for pkt_num, pkt in enumerate(exploit_raw_packets):
  435. eth_frame = Ether(pkt[0])
  436. ip_pkt = eth_frame.payload
  437. ip_dst = ip_pkt.getfieldval("dst")
  438. ip_src = ip_pkt.getfieldval("src")
  439. tcp_pkt = ip_pkt.payload
  440. port_dst = tcp_pkt.getfieldval("dport")
  441. port_src = tcp_pkt.getfieldval("sport")
  442. conv_req = (ip_src, port_src, ip_dst, port_dst)
  443. conv_rep = (ip_dst, port_dst, ip_src, port_src)
  444. if conv_req not in conversations and conv_rep not in conversations:
  445. pktList = [pkt]
  446. conversations[conv_req] = pktList
  447. # Order list of conv
  448. orderList_conversations.append(conv_req)
  449. else:
  450. if conv_req in conversations:
  451. pktList = conversations[conv_req]
  452. pktList.append(pkt)
  453. conversations[conv_req] = pktList
  454. else:
  455. pktList = conversations[conv_rep]
  456. pktList.append(pkt)
  457. conversations[conv_rep] = pktList
  458. return (conversations, orderList_conversations)
  459. def is_valid_ip_address(self,addr):
  460. """
  461. Checks if the IP address family is supported.
  462. :param addr: IP address to be checked.
  463. :return: Boolean
  464. """
  465. try:
  466. socket.inet_aton(addr)
  467. return True
  468. except socket.error:
  469. return False
  470. def ip_src_dst_equal_check(self, ip_source, ip_destination):
  471. """
  472. Checks if the source IP and destination IP are equal.
  473. :param ip_source: source IP address.
  474. :param ip_destination: destination IP address.
  475. """
  476. equal = False
  477. if isinstance(ip_source, list):
  478. if ip_destination in ip_source:
  479. equal = True
  480. else:
  481. if ip_source == ip_destination:
  482. equal = True
  483. if equal:
  484. print("\nERROR: Invalid IP addresses; source IP is the same as destination IP: " + ip_source + ".")
  485. sys.exit(0)
  486. def clean_white_spaces(self, str):
  487. str = str.replace("\\n", "\n")
  488. str = str.replace("\\r", "\r")
  489. str = str.replace("\\t", "\t")
  490. str = str.replace("\\\'", "\'")
  491. return str