MembersMgmtCommAttack.py 29 KB

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