AttackController.py 5.8 KB

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