AttackController.py 4.8 KB

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