CommunicationProcessor.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. from lea import Lea
  2. from Attack.MembersMgmtCommAttack import MessageType
  3. from Attack.MembersMgmtCommAttack import Message
  4. import ID2TLib.libbotnet as bcp
  5. # needed because of machine inprecision. E.g A time difference of 0.1s is stored as >0.1s
  6. EPS_TOLERANCE = 1e-13 # works for a difference of 0.1, no less
  7. def greater_than(a: float, b: float):
  8. """
  9. A greater than operator desgined to handle slight machine inprecision up to EPS_TOLERANCE.
  10. :return: True if a > b, otherwise False
  11. """
  12. return b - a < -EPS_TOLERANCE
  13. class CommunicationProcessor():
  14. """
  15. Class to process parsed input CSV/XML data and retrieve a mapping or other information.
  16. """
  17. def __init__(self, packets:list, mtypes:dict, nat:bool):
  18. """
  19. Creates an instance of CommunicationProcessor.
  20. :param packets: the list of abstract packets
  21. :param mtypes: a dict containing an int to EnumType mapping of MessageTypes
  22. :param nat: whether NAT is present in this network
  23. """
  24. self.packets = packets
  25. self.mtypes = mtypes
  26. self.nat = nat
  27. def set_mapping(self, packets: list, mapped_ids: dict):
  28. """
  29. Set the selected mapping for this communication processor.
  30. :param packets: all packets contained in the mapped time frame
  31. :param mapped_ids: the chosen IDs
  32. """
  33. self.packets = packets
  34. self.local_init_ids = set(mapped_ids)
  35. def find_interval_most_comm(self, number_ids: int, max_int_time: float):
  36. botproc = bcp.botnet_comm_processor(self.packets)
  37. cpp_intervals = botproc.find_interval(number_ids, max_int_time)
  38. intervals = []
  39. for cpp_interval in cpp_intervals:
  40. ids = []
  41. for id_ in cpp_interval[0]:
  42. ids.append(str(id_))
  43. interval = {"IDs": ids, "Start": cpp_interval[1], "End": cpp_interval[2]}
  44. intervals.append(interval)
  45. return intervals
  46. def det_id_roles_and_msgs(self):
  47. """
  48. Determine the role of every mapped ID. The role can be initiator, responder or both.
  49. On the side also connect corresponding messages together to quickly find out
  50. which reply belongs to which request and vice versa.
  51. :return: a triple as (initiator IDs, responder IDs, messages)
  52. """
  53. mtypes = self.mtypes
  54. # setup initial variables and their values
  55. respnd_ids = set()
  56. # msgs --> the filtered messages, msg_id --> an increasing ID to give every message an artificial primary key
  57. msgs, msg_id = [], 0
  58. # keep track of previous request to find connections
  59. prev_reqs = {}
  60. # used to determine whether a request has been seen yet, so that replies before the first request are skipped and do not throw an error by
  61. # accessing the empty dict prev_reqs (this is not a perfect solution, but it works most of the time)
  62. req_seen = False
  63. local_init_ids = self.local_init_ids
  64. external_init_ids = set()
  65. # process every packet individually
  66. for packet in self.packets:
  67. id_src, id_dst, msg_type, time = packet["Src"], packet["Dst"], int(packet["Type"]), float(packet["Time"])
  68. lineno = packet.get("LineNumber", -1)
  69. # if if either one of the IDs is not mapped, continue
  70. if (id_src not in local_init_ids) and (id_dst not in local_init_ids):
  71. continue
  72. # convert message type number to enum type
  73. msg_type = mtypes[msg_type]
  74. # process a request
  75. if msg_type in {MessageType.SALITY_HELLO, MessageType.SALITY_NL_REQUEST}:
  76. if not self.nat and id_dst in local_init_ids and id_src not in local_init_ids:
  77. external_init_ids.add(id_src)
  78. elif id_src not in local_init_ids:
  79. continue
  80. else:
  81. # process ID's role
  82. respnd_ids.add(id_dst)
  83. # convert the abstract message into a message object to handle it better
  84. msg_str = "{0}-{1}".format(id_src, id_dst)
  85. msg = Message(msg_id, id_src, id_dst, msg_type, time, line_no = lineno)
  86. msgs.append(msg)
  87. prev_reqs[msg_str] = msg_id
  88. msg_id += 1
  89. req_seen = True
  90. # process a reply
  91. elif msg_type in {MessageType.SALITY_HELLO_REPLY, MessageType.SALITY_NL_REPLY} and req_seen:
  92. if not self.nat and id_src in local_init_ids and id_dst not in local_init_ids:
  93. # process ID's role
  94. external_init_ids.add(id_dst)
  95. elif id_dst not in local_init_ids:
  96. continue
  97. else:
  98. # process ID's role
  99. respnd_ids.add(id_src)
  100. # convert the abstract message into a message object to handle it better
  101. msg_str = "{0}-{1}".format(id_dst, id_src)
  102. # find the request message ID for this response and set its reference index
  103. refer_idx = prev_reqs[msg_str]
  104. msgs[refer_idx].refer_msg_id = msg_id
  105. msg = Message(msg_id, id_src, id_dst, msg_type, time, refer_idx, lineno)
  106. msgs.append(msg)
  107. # remove the request to this response from storage
  108. del(prev_reqs[msg_str])
  109. msg_id += 1
  110. elif msg_type == MessageType.TIMEOUT and id_src in local_init_ids and not self.nat:
  111. # convert the abstract message into a message object to handle it better
  112. msg_str = "{0}-{1}".format(id_dst, id_src)
  113. # find the request message ID for this response and set its reference index
  114. refer_idx = prev_reqs.get(msg_str)
  115. if refer_idx is not None:
  116. msgs[refer_idx].refer_msg_id = msg_id
  117. if msgs[refer_idx].type == MessageType.SALITY_NL_REQUEST:
  118. msg = Message(msg_id, id_src, id_dst, MessageType.SALITY_NL_REPLY, time, refer_idx, lineno)
  119. else:
  120. msg = Message(msg_id, id_src, id_dst, MessageType.SALITY_HELLO_REPLY, time, refer_idx, lineno)
  121. msgs.append(msg)
  122. # remove the request to this response from storage
  123. del(prev_reqs[msg_str])
  124. msg_id += 1
  125. # store the retrieved information in this object for later use
  126. self.respnd_ids = sorted(respnd_ids)
  127. self.external_init_ids = sorted(external_init_ids)
  128. self.messages = msgs
  129. # return the retrieved information
  130. return self.local_init_ids, self.external_init_ids, self.respnd_ids, self.messages
  131. def det_ext_and_local_ids(self, prob_rspnd_local: int=0):
  132. """
  133. Map the given IDs to a locality (i.e. local or external} considering the given probabilities.
  134. :param comm_type: the type of communication (i.e. local, external or mixed)
  135. :param prob_rspnd_local: the probabilty that a responder is local
  136. """
  137. external_ids = set()
  138. local_ids = self.local_init_ids.copy()
  139. # set up probabilistic chooser
  140. rspnd_locality = Lea.fromValFreqsDict({"local": prob_rspnd_local*100, "external": (1-prob_rspnd_local)*100})
  141. for id_ in self.external_init_ids:
  142. external_ids.add(id_)
  143. # determine responder localities
  144. for id_ in self.respnd_ids:
  145. if id_ in local_ids or id_ in external_ids:
  146. continue
  147. pos = rspnd_locality.random()
  148. if pos == "local":
  149. local_ids.add(id_)
  150. elif pos == "external":
  151. external_ids.add(id_)
  152. self.local_ids, self.external_ids = local_ids, external_ids
  153. return self.local_ids, self.external_ids