Controller.py 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import os
  2. from ID2TLib.AttackController import AttackController
  3. from ID2TLib.LabelManager import LabelManager
  4. from ID2TLib.PcapFile import PcapFile
  5. from ID2TLib.Statistics import Statistics
  6. class Controller:
  7. def __init__(self, pcap_file_path: str):
  8. """
  9. Creates a new Controller, acting as a central coordinator for the whole application.
  10. :param pcap_file_path:
  11. """
  12. # Fields
  13. self.pcap_src_path = pcap_file_path
  14. self.pcap_dest_path = ''
  15. self.written_pcaps = []
  16. # Initialize class instances
  17. print("Input file: %s" % self.pcap_src_path)
  18. self.pcap_file = PcapFile(self.pcap_src_path)
  19. self.label_manager = LabelManager(self.pcap_src_path)
  20. self.statistics = Statistics(self.pcap_file)
  21. self.statisticsDB = self.statistics.get_statistics_database()
  22. self.attack_controller = AttackController(self.pcap_file, self.statistics, self.label_manager)
  23. def load_pcap_statistics(self, flag_write_file: bool, flag_recalculate_stats: bool, flag_print_statistics: bool):
  24. """
  25. Loads the PCAP statistics either from the database, if the statistics were calculated earlier, or calculates
  26. the statistics and creates a new database.
  27. :param flag_write_file: Writes the statistics to a file.
  28. :param flag_recalculate_stats: Forces the recalculation of statistics.
  29. :param flag_print_statistics: Prints the statistics on the terminal.
  30. :return: None
  31. """
  32. self.statistics.load_pcap_statistics(flag_write_file, flag_recalculate_stats, flag_print_statistics)
  33. def process_attacks(self, attacks_config):
  34. """
  35. Creates the attack based on the attack name and the attack parameters given in the attacks_config. The
  36. attacks_config is a list of attacks, e.g.
  37. [['PortscanAttack', 'ip.src="192.168.178.2",'dst.port=80'],['PortscanAttack', 'ip.src="10.10.10.2"]]
  38. :param attacks_config: A list of attacks with their attack parameters.
  39. """
  40. # load attacks sequentially
  41. for attack in attacks_config:
  42. self.pcap_dest_path = self.attack_controller.process_attack(attack[0], attack[1:])
  43. # delete intermediate PCAP files
  44. for i in range(len(self.written_pcaps) - 1):
  45. os.remove(self.written_pcaps[i])
  46. # print status message
  47. print('\nOutput file created: ', self.pcap_dest_path)
  48. # write label file with attacks
  49. self.label_manager.write_label_file(self.pcap_dest_path)
  50. def process_db_queries(self, query, print_results=False):
  51. """
  52. Processes a statistics database query. This can be a standard SQL query or a named query.
  53. :param query: The query as a string or multiple queries as a list of strings.
  54. :param print_results: Must be True if the results should be printed to terminal.
  55. :return: The query's result
  56. """
  57. print("Processing database query/queries...")
  58. if isinstance(query, list) or isinstance(query, tuple):
  59. for q in query:
  60. self.statisticsDB.process_db_query(q, print_results)
  61. else:
  62. self.statisticsDB.process_db_query(query, print_results)
  63. def enter_query_mode(self):
  64. """
  65. Enters into the query mode. This is a read-eval-print-loop, where the user can input named queries or SQL
  66. queries and the results are printed.
  67. """
  68. print("Entering into query mode...")
  69. print("Enter statement ending by ';' and press ENTER to send query. Exit by sending an empty query..")
  70. buffer = ""
  71. while True:
  72. line = input()
  73. if line == "":
  74. break
  75. buffer += line
  76. import sqlite3
  77. if sqlite3.complete_statement(buffer):
  78. try:
  79. buffer = buffer.strip()
  80. self.statisticsDB.process_db_query(buffer, True)
  81. except sqlite3.Error as e:
  82. print("An error occurred:", e.args[0])
  83. buffer = ""