Controller.py 4.3 KB

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