Controller.py 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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: list):
  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. self.written_pcaps.append(self.pcap_dest_path)
  44. # delete intermediate PCAP files
  45. for i in range(len(self.written_pcaps) - 1):
  46. os.remove(self.written_pcaps[i])
  47. # print status message
  48. print('\nOutput file created: ', self.pcap_dest_path)
  49. # write label file with attacks
  50. self.label_manager.write_label_file(self.pcap_dest_path)
  51. def process_db_queries(self, query, print_results=False):
  52. """
  53. Processes a statistics database query. This can be a standard SQL query or a named query.
  54. :param query: The query as a string or multiple queries as a list of strings.
  55. :param print_results: Must be True if the results should be printed to terminal.
  56. :return: The query's result
  57. """
  58. print("Processing database query/queries...")
  59. if isinstance(query, list) or isinstance(query, tuple):
  60. for q in query:
  61. self.statisticsDB.process_db_query(q, print_results)
  62. else:
  63. self.statisticsDB.process_db_query(query, print_results)
  64. def enter_query_mode(self):
  65. """
  66. Enters into the query mode. This is a read-eval-print-loop, where the user can input named queries or SQL
  67. queries and the results are printed.
  68. """
  69. print("Entering into query mode...")
  70. print("Enter statement ending by ';' and press ENTER to send query. Exit by sending an empty query..")
  71. buffer = ""
  72. while True:
  73. line = input("> ")
  74. if line == "":
  75. break
  76. buffer += line
  77. import sqlite3
  78. if sqlite3.complete_statement(buffer):
  79. try:
  80. buffer = buffer.strip()
  81. self.statisticsDB.process_db_query(buffer, True)
  82. except sqlite3.Error as e:
  83. print("An error occurred:", e.args[0])
  84. buffer = ""