AttackController.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import importlib
  2. import sys
  3. from Attack.AttackParameters import Parameter
  4. from ID2TLib import LabelManager
  5. from ID2TLib import Statistics
  6. from ID2TLib.Label import Label
  7. from ID2TLib.PcapFile import PcapFile
  8. class AttackController:
  9. def __init__(self, pcap_file: PcapFile, statistics_class: Statistics, label_manager: LabelManager):
  10. """
  11. Creates a new AttackController. The controller manages the attack injection, including the PCAP writing.
  12. :param statistics_class:
  13. """
  14. self.statistics = statistics_class
  15. self.pcap_file = pcap_file
  16. self.label_mgr = label_manager
  17. self.current_attack = None
  18. self.added_attacks = []
  19. # The PCAP where the attack should be injected into
  20. self.base_pcap = self.statistics.pcap_filepath
  21. def create_attack(self, attack_name: str):
  22. """
  23. Creates dynamically a new class instance based on the given attack_name.
  24. :param attack_name: The name of the attack, must correspond to the attack's class name.
  25. :return: None
  26. """
  27. print("\nCreating attack instance of \033[1m" + attack_name + "\033[0m")
  28. # Load attack class
  29. attack_module = importlib.import_module("Attack." + attack_name)
  30. attack_class = getattr(attack_module, attack_name)
  31. # Set current attack
  32. self.current_attack = attack_class(self.statistics, self.base_pcap)
  33. self.added_attacks.append(self.current_attack)
  34. def process_attack(self, attack: str, params: str):
  35. """
  36. Takes as input the name of an attack (classname) and the attack parameters as string. Parses the string of
  37. attack parameters, creates the attack by writing the attack packets and returns the path of the written pcap.
  38. :param attack: The classname of the attack to injecect.
  39. :param params: The parameters for attack customization, see attack class for supported params.
  40. :return: The file path to the created pcap file.
  41. """
  42. self.create_attack(attack)
  43. # Add attack parameters if provided
  44. print("Validating and adding attack parameters.")
  45. params_dict = []
  46. if params is not None:
  47. # Convert attack param list into dictionary
  48. for entry in params:
  49. params_dict.append(entry.split('='))
  50. params_dict = dict(params_dict)
  51. # Check if Parameter.INJECT_AT_TIMESTAMP and Parameter.INJECT_AFTER_PACKET are provided at the same time
  52. # if TRUE: delete Paramter.INJECT_AT_TIMESTAMP (lower priority) and use Parameter.INJECT_AFTER_PACKET
  53. if (Parameter.INJECT_AFTER_PACKET.value in params_dict) and (
  54. Parameter.INJECT_AT_TIMESTAMP.value in params_dict):
  55. print("CONFLICT: Parameters", Parameter.INJECT_AT_TIMESTAMP.value, "and",
  56. Parameter.INJECT_AFTER_PACKET.value,
  57. "given at the same time. Ignoring", Parameter.INJECT_AT_TIMESTAMP.value, "and using",
  58. Parameter.INJECT_AFTER_PACKET.value, "instead to derive the timestamp.")
  59. del params_dict[Parameter.INJECT_AT_TIMESTAMP.value]
  60. # Extract attack_note parameter, if not provided returns an empty string
  61. key_attack_note = "attack.note"
  62. attack_note = params_dict.get(key_attack_note, "")
  63. params_dict.pop(key_attack_note, None) # delete entry if found, otherwise return an empty string
  64. # Pass paramters to attack controller
  65. self.set_params(params_dict)
  66. else:
  67. attack_note = ""
  68. # Write attack into pcap file
  69. print("Generating attack packets...", end=" ")
  70. sys.stdout.flush() # force python to print text immediately
  71. total_packets, temp_attack_pcap_path = self.current_attack.generate_attack_pcap()
  72. print("done. (total: " + str(total_packets) + " pkts.)")
  73. # Store label into LabelManager
  74. l = Label(attack, self.get_attack_start_utime(),
  75. self.get_attack_end_utime(), attack_note)
  76. self.label_mgr.add_labels(l)
  77. return temp_attack_pcap_path
  78. def get_attack_start_utime(self):
  79. """
  80. :return: The start time (timestamp of first packet) of the attack as unix timestamp.
  81. """
  82. return self.current_attack.attack_start_utime
  83. def get_attack_end_utime(self):
  84. """
  85. :return: The end time (timestamp of last packet) of the attack as unix timestamp.
  86. """
  87. return self.current_attack.attack_end_utime
  88. def set_params(self, params: dict):
  89. """
  90. Sets the attack's parameters.
  91. :param params: The parameters in a dictionary: {parameter_name: parameter_value}
  92. :return: None
  93. """
  94. for param_key, param_value in params.items():
  95. self.current_attack.add_param_value(param_key, param_value)