3
0

MembersMgmtCommAttack.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. from enum import Enum
  2. from random import randint, randrange, choice, uniform
  3. from collections import deque
  4. from scipy.stats import gamma
  5. from lea import Lea
  6. from datetime import datetime
  7. import os
  8. import sys
  9. import ID2TLib.Botnet.libbotnetcomm as lb
  10. import ID2TLib.Botnet.Message as Bmsg
  11. from Attack import BaseAttack
  12. from Attack.AttackParameters import Parameter as Param
  13. from Attack.AttackParameters import ParameterTypes
  14. from ID2TLib.Ports import PortSelectors
  15. import ID2TLib.Utility as Util
  16. from ID2TLib import FileUtils, Generator
  17. from ID2TLib.IPv4 import IPAddress
  18. from ID2TLib.PcapAddressOperations import PcapAddressOperations
  19. from ID2TLib.Botnet.CommunicationProcessor import CommunicationProcessor
  20. from ID2TLib.Botnet.MessageMapping import MessageMapping
  21. from ID2TLib.PcapFile import PcapFile
  22. from Core.Statistics import Statistics
  23. from scapy.layers.inet import IP, IPOption_Security
  24. class MembersMgmtCommAttack(BaseAttack.BaseAttack):
  25. def __init__(self):
  26. """
  27. Creates a new instance of the Membership Management Communication.
  28. """
  29. # Initialize communication
  30. super(MembersMgmtCommAttack, self).__init__("Membership Management Communication Attack (MembersMgmtCommAttack)",
  31. "Injects Membership Management Communication", "Botnet communication")
  32. # Define allowed parameters and their type
  33. self.supported_params = {
  34. # parameters regarding attack
  35. Param.INJECT_AT_TIMESTAMP: ParameterTypes.TYPE_FLOAT,
  36. Param.INJECT_AFTER_PACKET: ParameterTypes.TYPE_PACKET_POSITION,
  37. Param.PACKETS_LIMIT: ParameterTypes.TYPE_INTEGER_POSITIVE,
  38. Param.ATTACK_DURATION: ParameterTypes.TYPE_INTEGER_POSITIVE,
  39. # use num_attackers to specify number of communicating devices?
  40. Param.NUMBER_INITIATOR_BOTS: ParameterTypes.TYPE_INTEGER_POSITIVE,
  41. # input file containing botnet communication
  42. Param.FILE_CSV: ParameterTypes.TYPE_FILEPATH,
  43. Param.FILE_XML: ParameterTypes.TYPE_FILEPATH,
  44. # the percentage of IP reuse (if total and other is specified, percentages are multiplied)
  45. Param.IP_REUSE_TOTAL: ParameterTypes.TYPE_PERCENTAGE,
  46. Param.IP_REUSE_LOCAL: ParameterTypes.TYPE_PERCENTAGE,
  47. Param.IP_REUSE_EXTERNAL: ParameterTypes.TYPE_PERCENTAGE,
  48. # the user-selected padding to add to every packet
  49. Param.PACKET_PADDING: ParameterTypes.TYPE_PADDING,
  50. # presence of NAT at the gateway of the network
  51. Param.NAT_PRESENT: ParameterTypes.TYPE_BOOLEAN,
  52. # whether the TTL distribution should be based on the input PCAP
  53. # or the CAIDA dataset
  54. Param.TTL_FROM_CAIDA: ParameterTypes.TYPE_BOOLEAN,
  55. # whether the destination port of a response should be the ephemeral port
  56. # its request came from or a static (server)port based on a hostname
  57. Param.MULTIPORT: ParameterTypes.TYPE_BOOLEAN,
  58. # information about the interval selection strategy
  59. Param.INTERVAL_SELECT_STRATEGY: ParameterTypes.TYPE_INTERVAL_SELECT_STRAT,
  60. Param.INTERVAL_SELECT_START: ParameterTypes.TYPE_INTEGER_POSITIVE,
  61. Param.INTERVAL_SELECT_END: ParameterTypes.TYPE_INTEGER_POSITIVE,
  62. # determines whether injected packets are marked with an unused IP option
  63. # to easily filter them in e.g. wireshark
  64. Param.HIDDEN_MARK: ParameterTypes.TYPE_BOOLEAN
  65. }
  66. # create dict with MessageType values for fast name lookup
  67. self.msg_types = {}
  68. for msg_type in Bmsg.MessageType:
  69. self.msg_types[msg_type.value] = msg_type
  70. def init_params(self):
  71. """
  72. Initialize some parameters of this communication-attack using the user supplied command line parameters.
  73. The remaining parameters are implicitly set in the provided data file. Note: the timestamps in the file
  74. have to be sorted in ascending order
  75. :param statistics: Reference to a statistics object.
  76. """
  77. # set class constants
  78. self.DEFAULT_XML_PATH = Util.RESOURCE_DIR + "Botnet/MembersMgmtComm_example.xml"
  79. # PARAMETERS: initialize with default values
  80. # (values are overwritten if user specifies them)
  81. self.add_param_value(Param.INJECT_AFTER_PACKET, 1 + randint(0, self.statistics.get_packet_count() // 5))
  82. self.add_param_value(Param.FILE_XML, self.DEFAULT_XML_PATH)
  83. # Alternatively new attack parameter?
  84. duration = int(float(self.statistics.get_capture_duration()))
  85. self.add_param_value(Param.ATTACK_DURATION, duration)
  86. self.add_param_value(Param.NUMBER_INITIATOR_BOTS, 1)
  87. # NAT on by default
  88. self.add_param_value(Param.NAT_PRESENT, True)
  89. # TODO: change 1 to something better
  90. self.add_param_value(Param.IP_REUSE_TOTAL, 1)
  91. self.add_param_value(Param.IP_REUSE_LOCAL, 0.5)
  92. self.add_param_value(Param.IP_REUSE_EXTERNAL, 0.5)
  93. # add default additional padding
  94. self.add_param_value(Param.PACKET_PADDING, 20)
  95. # choose the input PCAP as default base for the TTL distribution
  96. self.add_param_value(Param.TTL_FROM_CAIDA, False)
  97. # do not use multiple ports for requests and responses
  98. self.add_param_value(Param.MULTIPORT, False)
  99. # interval selection strategy
  100. self.add_param_value(Param.INTERVAL_SELECT_STRATEGY, "optimal")
  101. self.add_param_value(Param.HIDDEN_MARK, False)
  102. def generate_attack_pcap(self):
  103. """
  104. Injects the packets of this attack into a PCAP and stores it as a temporary file.
  105. :return: a tuple of the number packets injected, the path to the temporary attack PCAP
  106. and a list of additionally created files
  107. """
  108. # create the final messages that have to be sent, including all bot configurations
  109. messages = self._create_messages()
  110. if messages == []:
  111. return 0, None
  112. # Setup (initial) parameters for packet creation loop
  113. BUFFER_SIZE = 1000
  114. pkt_gen = Generator.PacketGenerator()
  115. padding = self.get_param_value(Param.PACKET_PADDING)
  116. packets = deque(maxlen=BUFFER_SIZE)
  117. total_pkts = 0
  118. limit_packetcount = self.get_param_value(Param.PACKETS_LIMIT)
  119. limit_duration = self.get_param_value(Param.ATTACK_DURATION)
  120. path_attack_pcap = None
  121. overThousand = False
  122. msg_packet_mapping = MessageMapping(messages, self.statistics.get_pcap_timestamp_start())
  123. mark_packets = self.get_param_value(Param.HIDDEN_MARK)
  124. # create packets to write to PCAP file
  125. for msg in messages:
  126. # retrieve the source and destination configurations
  127. id_src, id_dst = msg.src["ID"], msg.dst["ID"]
  128. ip_src, ip_dst = msg.src["IP"], msg.dst["IP"]
  129. mac_src, mac_dst = msg.src["MAC"], msg.dst["MAC"]
  130. if msg.type.is_request():
  131. port_src, port_dst = int(msg.src["SrcPort"]), int(msg.dst["DstPort"])
  132. else:
  133. port_src, port_dst = int(msg.src["DstPort"]), int(msg.dst["SrcPort"])
  134. ttl = int(msg.src["TTL"])
  135. # update duration
  136. duration = msg.time - messages[0].time
  137. # if total number of packets has been sent or the attack duration has been exceeded, stop
  138. if ((limit_packetcount is not None and total_pkts >= limit_packetcount) or
  139. (limit_duration is not None and duration >= limit_duration)):
  140. break
  141. # if the type of the message is a NL reply, determine the number of entries
  142. nl_size = 0
  143. if msg.type == Bmsg.MessageType.SALITY_NL_REPLY:
  144. nl_size = randint(1, 25) # what is max NL entries?
  145. # create suitable IP/UDP packet and add to packets list
  146. packet = pkt_gen.generate_mmcom_packet(ip_src=ip_src, ip_dst=ip_dst, ttl=ttl, mac_src=mac_src, mac_dst=mac_dst,
  147. port_src=port_src, port_dst=port_dst, message_type=msg.type, neighborlist_entries=nl_size)
  148. Generator.add_padding(packet, padding,True, True)
  149. packet.time = msg.time
  150. if mark_packets and isinstance(packet.payload, IP): # do this only for ip-packets
  151. ip_data = packet.payload
  152. hidden_opt = IPOption_Security()
  153. hidden_opt.option = 2 # "normal" security opt
  154. hidden_opt.security = 16 # magic value indicating NSA
  155. ip_data.options = hidden_opt
  156. packets.append(packet)
  157. msg_packet_mapping.map_message(msg, packet)
  158. total_pkts += 1
  159. # Store timestamp of first packet (for attack label)
  160. if total_pkts <= 1:
  161. self.attack_start_utime = packets[0].time
  162. elif total_pkts % BUFFER_SIZE == 0: # every 1000 packets write them to the PCAP file (append)
  163. if overThousand: # if over 1000 packets written, there may be a different packet-length for the last few packets
  164. packets = list(packets)
  165. Generator.equal_length(packets, length = max_len, padding = padding, force_len = True)
  166. last_packet = packets[-1]
  167. path_attack_pcap = self.write_attack_pcap(packets, True, path_attack_pcap)
  168. packets = deque(maxlen=BUFFER_SIZE)
  169. else:
  170. packets = list(packets)
  171. Generator.equal_length(packets, padding = padding)
  172. last_packet = packets[-1]
  173. max_len = len(last_packet)
  174. overThousand = True
  175. path_attack_pcap = self.write_attack_pcap(packets, True, path_attack_pcap)
  176. packets = deque(maxlen=BUFFER_SIZE)
  177. # if there are unwritten packets remaining, write them to the PCAP file
  178. if len(packets) > 0:
  179. if overThousand:
  180. packets = list(packets)
  181. Generator.equal_length(packets, length = max_len, padding = padding, force_len = True)
  182. path_attack_pcap = self.write_attack_pcap(packets, True, path_attack_pcap)
  183. last_packet = packets[-1]
  184. else:
  185. packets = list(packets)
  186. Generator.equal_length(packets, padding = padding)
  187. path_attack_pcap = self.write_attack_pcap(packets, True, path_attack_pcap)
  188. last_packet = packets[-1]
  189. # write the mapping to a file
  190. current_ts = datetime.now().strftime("%Y%m%d-%H%M%S")
  191. mapping_filename = "mapping_" + current_ts + ".xml"
  192. msg_packet_mapping.write_to_file(mapping_filename)
  193. # Store timestamp of last packet
  194. self.attack_end_utime = last_packet.time
  195. # Return packets sorted by packet by timestamp and total number of packets (sent)
  196. return total_pkts , path_attack_pcap, [mapping_filename]
  197. def generate_attack_packets(self):
  198. pass
  199. def _create_messages(self):
  200. """
  201. Creates the messages that are to be injected into the PCAP.
  202. :return: the final messages as a list
  203. """
  204. def add_ids_to_config(ids_to_add: list, existing_ips: list, new_ips: list, bot_configs: dict, idtype:str="local", router_mac:str=""):
  205. """
  206. Creates IP and MAC configurations for the given IDs and adds them to the existing configurations object.
  207. :param ids_to_add: all sorted IDs that have to be configured and added
  208. :param existing_ips: the existing IPs in the PCAP file that should be assigned to some, or all, IDs
  209. :param new_ips: the newly generated IPs that should be assigned to some, or all, IDs
  210. :param bot_configs: the existing configurations for the bots
  211. :param idtype: the locality type of the IDs
  212. :param router_mac: the MAC address of the router in the PCAP
  213. """
  214. ids = ids_to_add.copy()
  215. # macgen only needed, when IPs are new local IPs (therefore creating the object here suffices for the current callers
  216. # to not end up with the same MAC paired with different IPs)
  217. macgen = Generator.MacAddressGenerator()
  218. # assign existing IPs and the corresponding MAC addresses in the PCAP to the IDs
  219. for ip in existing_ips:
  220. random_id = choice(ids)
  221. mac = self.statistics.process_db_query("macAddress(IPAddress=%s)" % ip)
  222. bot_configs[random_id] = {"Type": idtype, "IP": ip, "MAC": mac}
  223. ids.remove(random_id)
  224. # assign new IPs and for local IPs new MACs or for external IPs the router MAC to the IDs
  225. for ip in new_ips:
  226. random_id = choice(ids)
  227. if idtype == "local":
  228. mac = macgen.random_mac()
  229. elif idtype == "external":
  230. mac = router_mac
  231. bot_configs[random_id] = {"Type": idtype, "IP": ip, "MAC": mac}
  232. ids.remove(random_id)
  233. def assign_realistic_ttls(bot_configs:list):
  234. '''
  235. Assigns a realisitic ttl to each bot from @param: bot_configs. Uses statistics and distribution to be able
  236. to calculate a realisitc ttl.
  237. :param bot_configs: List that contains all bots that should be assigned with realistic ttls.
  238. '''
  239. ids = sorted(bot_configs.keys())
  240. for pos,bot in enumerate(ids):
  241. bot_type = bot_configs[bot]["Type"]
  242. if(bot_type == "local"): # Set fix TTL for local Bots
  243. bot_configs[bot]["TTL"] = 128
  244. # Set TTL based on TTL distribution of IP address
  245. else: # Set varying TTl for external Bots
  246. bot_ttl_dist = self.statistics.get_ttl_distribution(bot_configs[bot]["IP"])
  247. if len(bot_ttl_dist) > 0:
  248. source_ttl_prob_dict = Lea.fromValFreqsDict(bot_ttl_dist)
  249. bot_configs[bot]["TTL"] = source_ttl_prob_dict.random()
  250. else:
  251. most_used_ttl = self.statistics.process_db_query("most_used(ttlValue)")
  252. if isinstance(most_used_ttl, list):
  253. bot_configs[bot]["TTL"] = choice(self.statistics.process_db_query("most_used(ttlValue)"))
  254. else:
  255. bot_configs[bot]["TTL"] = self.statistics.process_db_query("most_used(ttlValue)")
  256. def assign_realistic_timestamps(messages: list, external_ids: set, local_ids: set, avg_delay_local:float, avg_delay_external: float, zero_reference:float):
  257. """
  258. Assigns realistic timestamps to a set of messages
  259. :param messages: the set of messages to be updated
  260. :param external_ids: the set of bot ids, that are outside the network, i.e. external
  261. :param local_ids: the set of bot ids, that are inside the network, i.e. local
  262. :avg_delay_local: the avg_delay between the dispatch and the reception of a packet between local computers
  263. :avg_delay_external: the avg_delay between the dispatch and the reception of a packet between a local and an external computer
  264. :zero_reference: the timestamp which is regarded as the beginning of the pcap_file and therefore handled like a timestamp that resembles 0
  265. """
  266. updated_msgs = []
  267. last_response = {} # Dict, takes a tuple of 2 Bot_IDs as a key (requester, responder), returns the time of the last response, the requester received
  268. # necessary in order to make sure, that additional requests are sent only after the response to the last one was received
  269. for msg in messages: # init
  270. last_response[(msg.src, msg.dst)] = -1
  271. # update all timestamps
  272. for req_msg in messages:
  273. if(req_msg in updated_msgs):
  274. # message already updated
  275. continue
  276. # if req_msg.timestamp would be before the timestamp of the response to the last request, req_msg needs to be sent later (else branch)
  277. if last_response[(req_msg.src, req_msg.dst)] == -1 or last_response[(req_msg.src, req_msg.dst)] < (zero_reference + req_msg.time - 0.05):
  278. ## update req_msg timestamp with a variation of up to 50ms
  279. req_msg.time = zero_reference + req_msg.time + uniform(-0.05, 0.05)
  280. updated_msgs.append(req_msg)
  281. else:
  282. req_msg.time = last_response[(req_msg.src, req_msg.dst)] + 0.06 + uniform(-0.05, 0.05)
  283. # update response if necessary
  284. if req_msg.refer_msg_id != -1:
  285. respns_msg = messages[req_msg.refer_msg_id]
  286. # check for local or external communication and update response timestamp with the respective avg delay
  287. if req_msg.src in external_ids or req_msg.dst in external_ids:
  288. #external communication
  289. respns_msg.time = req_msg.time + avg_delay_external + uniform(-0.1*avg_delay_external, 0.1*avg_delay_external)
  290. else:
  291. #local communication
  292. respns_msg.time = req_msg.time + avg_delay_local + uniform(-0.1*avg_delay_local, 0.1*avg_delay_local)
  293. updated_msgs.append(respns_msg)
  294. last_response[(req_msg.src, req_msg.dst)] = respns_msg.time
  295. def assign_ttls_from_caida(bot_configs):
  296. """
  297. Assign realistic TTL values to bots with respect to their IP, based on the CAIDA dataset.
  298. If there exists an entry for a bot's IP, the TTL is chosen based on a distribution over all used TTLs by this IP.
  299. If there is no such entry, the TTL is chosen based on a distribution over all used TTLs and their respective frequency.
  300. :param bot_configs: the existing bot configurations
  301. """
  302. def get_ip_ttl_distrib():
  303. """
  304. Parses the CSV file containing a mapping between IP and their used TTLs.
  305. :return: returns a dict with the IPs as keys and dicts for their TTL disribution as values
  306. """
  307. ip_based_distrib = {}
  308. with open("resources/CaidaTTL_perIP.csv", "r") as file:
  309. # every line consists of: IP, TTL, Frequency
  310. next(file) # skip CSV header line
  311. for line in file:
  312. ip_addr, ttl, freq = line.split(",")
  313. if ip_addr not in ip_based_distrib:
  314. ip_based_distrib[ip_addr] = {} # the values for ip_based_distrib are dicts with key=TTL, value=Frequency
  315. ip_based_distrib[ip_addr][ttl] = int(freq)
  316. return ip_based_distrib
  317. def get_total_ttl_distrib():
  318. """
  319. Parses the CSV file containing an overview of all used TTLs and their respective frequency.
  320. :return: returns a dict with the TTLs as keys and their frequencies as keys
  321. """
  322. total_ttl_distrib = {}
  323. with open("resources/CaidaTTL_total.csv", "r") as file:
  324. # every line consists of: TTL, Frequency, Fraction
  325. next(file) # skip CSV header line
  326. for line in file:
  327. ttl, freq, _ = line.split(",")
  328. total_ttl_distrib[ttl] = int(freq)
  329. return total_ttl_distrib
  330. # get the TTL distribution for every IP that is available in "resources/CaidaTTL_perIP.csv"
  331. ip_ttl_distrib = get_ip_ttl_distrib()
  332. # build a probability dict for the total TTL distribution
  333. total_ttl_prob_dict = Lea.fromValFreqsDict(get_total_ttl_distrib())
  334. # loop over every bot id and assign a TTL to the respective bot
  335. for bot_id in sorted(bot_configs):
  336. bot_type = bot_configs[bot_id]["Type"]
  337. bot_ip = bot_configs[bot_id]["IP"]
  338. if bot_type == "local":
  339. bot_configs[bot_id]["TTL"] = 128
  340. # if there exists detailed information about the TTL distribution of this IP
  341. elif bot_ip in ip_ttl_distrib:
  342. ip_ttl_freqs = ip_ttl_distrib[bot_ip]
  343. source_ttl_prob_dict = Lea.fromValFreqsDict(ip_ttl_freqs) # build a probability dict from this IP's TTL distribution
  344. bot_configs[bot_id]["TTL"] = source_ttl_prob_dict.random()
  345. # otherwise assign a random TTL based on the total TTL distribution
  346. else:
  347. bot_configs[bot_id]["TTL"] = total_ttl_prob_dict.random()
  348. # parse input CSV or XML
  349. filepath_xml = self.get_param_value(Param.FILE_XML)
  350. filepath_csv = self.get_param_value(Param.FILE_CSV)
  351. # use C++ communication processor for faster interval finding
  352. cpp_comm_proc = lb.botnet_comm_processor();
  353. # only use CSV input if the XML path is the default one
  354. # --> prefer XML input over CSV input (in case both are given)
  355. print_updates = False
  356. if filepath_csv and filepath_xml == self.DEFAULT_XML_PATH:
  357. filename = os.path.splitext(os.path.basename(filepath_csv))[0]
  358. filesize = os.path.getsize(filepath_csv) / 2**20 # get filesize in MB
  359. if filesize > 10:
  360. print("\nParsing input CSV file...", end=" ")
  361. sys.stdout.flush()
  362. print_updates = True
  363. cpp_comm_proc.parse_csv(filepath_csv)
  364. if print_updates:
  365. print("done.")
  366. print("Writing corresponding XML file...", end=" ")
  367. sys.stdout.flush()
  368. filepath_xml = cpp_comm_proc.write_xml(Util.OUT_DIR, filename)
  369. if print_updates: print("done.")
  370. else:
  371. filesize = os.path.getsize(filepath_xml) / 2**20 # get filesize in MB
  372. if filesize > 10:
  373. print("Parsing input XML file...", end=" ")
  374. sys.stdout.flush()
  375. print_updates = True
  376. cpp_comm_proc.parse_xml(filepath_xml)
  377. if print_updates: print("done.")
  378. # find a good communication mapping in the input file that matches the users parameters
  379. nat = self.get_param_value(Param.NAT_PRESENT)
  380. comm_proc = CommunicationProcessor(self.msg_types, nat)
  381. duration = self.get_param_value(Param.ATTACK_DURATION)
  382. number_init_bots = self.get_param_value(Param.NUMBER_INITIATOR_BOTS)
  383. strategy = self.get_param_value(Param.INTERVAL_SELECT_STRATEGY)
  384. start_idx = self.get_param_value(Param.INTERVAL_SELECT_START)
  385. end_idx = self.get_param_value(Param.INTERVAL_SELECT_END)
  386. potential_long_find_time = (strategy == "optimal" and (filesize > 4 and self.statistics.get_packet_count() > 1000))
  387. if print_updates or potential_long_find_time:
  388. if not print_updates: print()
  389. print("Selecting communication interval from input CSV/XML file...", end=" ")
  390. sys.stdout.flush()
  391. if potential_long_find_time:
  392. print("\nWarning: Because of the large input files and the (chosen) interval selection strategy 'optimal',")
  393. print("this may take a while. Consider using selection strategy 'random' or 'custom'...", end=" ")
  394. sys.stdout.flush()
  395. print_updates = True
  396. comm_interval = comm_proc.get_comm_interval(cpp_comm_proc, strategy, number_init_bots, duration, start_idx, end_idx)
  397. if not comm_interval:
  398. print("Error: An interval that satisfies the input cannot be found.")
  399. return []
  400. if print_updates: print("done.") # print corresponding message to interval finding message
  401. # retrieve the mapping information
  402. mapped_ids, packet_start_idx, packet_end_idx = comm_interval["IDs"], comm_interval["Start"], comm_interval["End"]
  403. while len(mapped_ids) > number_init_bots:
  404. rm_idx = randrange(0, len(mapped_ids))
  405. del mapped_ids[rm_idx]
  406. if print_updates: print("Generating attack packets...", end=" ")
  407. sys.stdout.flush()
  408. # get the messages contained in the chosen interval
  409. abstract_packets = cpp_comm_proc.get_messages(packet_start_idx, packet_end_idx);
  410. comm_proc.set_mapping(abstract_packets, mapped_ids)
  411. # determine ID roles and select the messages that are to be mapped into the PCAP
  412. messages = comm_proc.det_id_roles_and_msgs()
  413. # use the previously detetermined roles to assign the locality of all IDs
  414. local_ids, external_ids = comm_proc.det_ext_and_local_ids()
  415. # determine number of reused local and external IPs
  416. reuse_percent_total = self.get_param_value(Param.IP_REUSE_TOTAL)
  417. reuse_percent_external = self.get_param_value(Param.IP_REUSE_EXTERNAL)
  418. reuse_percent_local = self.get_param_value(Param.IP_REUSE_LOCAL)
  419. reuse_count_external = int(reuse_percent_total * reuse_percent_external * len(mapped_ids))
  420. reuse_count_local = int(reuse_percent_total * reuse_percent_local * len(mapped_ids))
  421. # create IP and MAC configurations for the IDs/Bots
  422. ipgen = Generator.IPGenerator()
  423. pcapops = PcapAddressOperations(self.statistics)
  424. router_mac = pcapops.get_probable_router_mac()
  425. bot_configs = {}
  426. # retrieve and assign the IPs and MACs for the bots with respect to the given parameters
  427. # (IDs are always added to bot_configs in the same order under a given seed)
  428. number_local_ids, number_external_ids = len(local_ids), len(external_ids)
  429. # assign addresses for local IDs
  430. if number_local_ids > 0:
  431. reuse_count_local = int(reuse_percent_total * reuse_percent_local * number_local_ids)
  432. existing_local_ips = sorted(pcapops.get_existing_local_ips(reuse_count_local))
  433. new_local_ips = sorted(pcapops.get_new_local_ips(number_local_ids - len(existing_local_ips)))
  434. add_ids_to_config(sorted(local_ids), existing_local_ips, new_local_ips, bot_configs)
  435. # assign addresses for external IDs
  436. if number_external_ids > 0:
  437. reuse_count_external = int(reuse_percent_total * reuse_percent_external * number_external_ids)
  438. existing_external_ips = sorted(pcapops.get_existing_external_ips(reuse_count_external))
  439. remaining = len(external_ids) - len(existing_external_ips)
  440. for external_ip in existing_external_ips: ipgen.add_to_blacklist(external_ip)
  441. new_external_ips = sorted([ipgen.random_ip() for _ in range(remaining)])
  442. add_ids_to_config(sorted(external_ids), existing_external_ips, new_external_ips, bot_configs, idtype="external", router_mac=router_mac)
  443. # this is the timestamp at which the first packet should be injected, the packets have to be shifted to the beginning of the
  444. # pcap file (INJECT_AT_TIMESTAMP) and then the offset of the packets have to be compensated to start at the given point in time
  445. zero_reference = self.get_param_value(Param.INJECT_AT_TIMESTAMP) - messages[0].time
  446. # calculate the average delay values for local and external responses
  447. avg_delay_local, avg_delay_external = self.statistics.get_avg_delay_local_ext()
  448. #set timestamps
  449. assign_realistic_timestamps(messages, external_ids, local_ids, avg_delay_local, avg_delay_external, zero_reference)
  450. portSelector = PortSelectors.LINUX
  451. reserved_ports = set(int(line.strip()) for line in open(Util.RESOURCE_DIR + "reserved_ports.txt").readlines())
  452. def filter_reserved(get_port):
  453. port = get_port()
  454. while port in reserved_ports:
  455. port = get_port()
  456. return port
  457. # create port configurations for the bots
  458. use_multiple_ports = self.get_param_value(Param.MULTIPORT)
  459. for bot in sorted(bot_configs):
  460. bot_configs[bot]["SrcPort"] = filter_reserved(portSelector.select_port_udp)
  461. if not use_multiple_ports:
  462. bot_configs[bot]["DstPort"] = filter_reserved(Generator.gen_random_server_port)
  463. else:
  464. bot_configs[bot]["DstPort"] = filter_reserved(portSelector.select_port_udp)
  465. # assign realistic TTL for every bot
  466. if self.get_param_value(Param.TTL_FROM_CAIDA):
  467. assign_ttls_from_caida(bot_configs)
  468. else:
  469. assign_realistic_ttls(bot_configs)
  470. # put together the final messages including the full sender and receiver
  471. # configurations (i.e. IP, MAC, port, ...) for easier later use
  472. final_messages = []
  473. messages = sorted(messages, key=lambda msg: msg.time)
  474. new_id = 0
  475. for msg in messages:
  476. type_src, type_dst = bot_configs[msg.src]["Type"], bot_configs[msg.dst]["Type"]
  477. id_src, id_dst = msg.src, msg.dst
  478. # sort out messages that do not have a suitable locality setting
  479. if type_src == "external" and type_dst == "external":
  480. continue
  481. msg.src, msg.dst = bot_configs[id_src], bot_configs[id_dst]
  482. msg.src["ID"], msg.dst["ID"] = id_src, id_dst
  483. msg.msg_id = new_id
  484. new_id += 1
  485. ### Important here to update refers, if needed later?
  486. final_messages.append(msg)
  487. return final_messages