MembersMgmtCommAttack.py 27 KB

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