diff --git a/code/main.py b/code/main.py index bf9908204735fb778b591aba7f535124a04ff2fb..ed657a0e7747d764d442b84be5f5ae54513f4ffa 100644 --- a/code/main.py +++ b/code/main.py @@ -4,6 +4,7 @@ import subprocess import threading import os import sys +import duckdb sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), 'machine_learning_models'))) base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -11,12 +12,9 @@ python_executable = os.path.join(base_dir, ".venv", "Scripts", "python.exe") from machine_learning_models import utilities as util - train_file_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'machine_learning_models', 'nsl-kdd-dataset', 'KDDTrain+.arff')) test_file_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'machine_learning_models', 'nsl-kdd-dataset', 'KDDTest+.arff')) - -# Import data using the correct paths df_train, df_test, model_name = util.import_data( train_file_path=train_file_path, test_file_path=test_file_path, @@ -47,6 +45,7 @@ class PacketCaptureGUI: self.menu_bar.add_cascade(label="View", menu=self.view_menu) self.view_menu.add_command(label="Statistics", command=self.show_statistics) self.view_menu.add_command(label="Graph Viewer", command=self.open_graph_viewer) + self.view_menu.add_command(label="Packet Data", command=self.show_packet_data) # New menu item # Add a "Train" menu with a "Train Model" option self.train_menu = Menu(self.menu_bar, tearoff=0) @@ -94,6 +93,7 @@ class PacketCaptureGUI: self.rule_based_radio.config(state=tk.DISABLED) self.start_button.config(state=tk.DISABLED) self.stop_button.config(state=tk.NORMAL) + self.disable_menu_items() if self.model_var.get() == "rule_based": self.packet_display.insert(tk.END, "Starting packet capture with rule-based model...\n") @@ -136,8 +136,11 @@ class PacketCaptureGUI: self.rule_based_radio.config(state=tk.NORMAL) self.start_button.config(state=tk.NORMAL) self.stop_button.config(state=tk.DISABLED) + self.enable_menu_items() def show_statistics(self): + self.disable_menu_items() + self.start_button.config(state=tk.DISABLED) # Create a new window for statistics stats_window = tk.Toplevel(self.root) stats_window.title("Statistics") @@ -153,7 +156,11 @@ class PacketCaptureGUI: canvas.draw() canvas.get_tk_widget().pack() + stats_window.protocol("WM_DELETE_WINDOW", lambda: self.on_close_window(stats_window)) + def train_model(self): + self.disable_menu_items() + self.start_button.config(state=tk.DISABLED) # Create a new window for model training train_window = tk.Toplevel(self.root) train_window.title("Train Model") @@ -169,6 +176,8 @@ class PacketCaptureGUI: start_training_button = tk.Button(train_window, text="Start Training", command=lambda: self.start_training(model_combobox.get())) start_training_button.pack(pady=10) + train_window.protocol("WM_DELETE_WINDOW", lambda: self.on_close_window(train_window)) + def start_training(self, model_name): if model_name == "Random Forest": self.packet_display.insert(tk.END, f"Starting training with {model_name} model...\n") @@ -269,7 +278,7 @@ class PacketCaptureGUI: def on_mouse_move(self, event): if self.dragging and self.last_mouse_pos: dx = self.last_mouse_pos[0] - event.x - dy = self.last_mouse_pos[1] - event.y + dy = event.y - self.last_mouse_pos[1] # Invert vertical movement cur_xlim = self.ax.get_xlim() cur_ylim = self.ax.get_ylim() @@ -281,9 +290,61 @@ class PacketCaptureGUI: self.last_mouse_pos = (event.x, event.y) def open_graph_viewer(self): + self.disable_menu_items() + self.start_button.config(state=tk.DISABLED) viewer_window = tk.Toplevel(self.root) GraphViewer(viewer_window) + def show_packet_data(self): + self.disable_menu_items() + self.start_button.config(state=tk.DISABLED) + # Create a new window for packet data + data_window = tk.Toplevel(self.root) + data_window.title("Packet Data") + + # Create a Treeview widget to display packet data + tree = ttk.Treeview(data_window, columns=("source_ip", "destination_ip", "source_mac", "destination_mac", "source_port", "destination_port", "is_dangerous", "type_of_threat"), show="headings") + tree.pack(fill=tk.BOTH, expand=True) + + # Define column headings + tree.heading("source_ip", text="Source IP") + tree.heading("destination_ip", text="Destination IP") + tree.heading("source_mac", text="Source MAC") + tree.heading("destination_mac", text="Destination MAC") + tree.heading("source_port", text="Source Port") + tree.heading("destination_port", text="Destination Port") + tree.heading("is_dangerous", text="Is Dangerous") + tree.heading("type_of_threat", text="Type of Threat") + + # Connect to the DuckDB database and fetch data + conn = duckdb.connect('packets_data.duckdb') + query = "SELECT * FROM packets" + result = conn.execute(query).fetchall() + + # Insert data into the Treeview widget + for row in result: + tree.insert("", tk.END, values=row) + + conn.close() + + data_window.protocol("WM_DELETE_WINDOW", lambda: self.on_close_window(data_window)) + + def disable_menu_items(self): + self.view_menu.entryconfig("Statistics", state=tk.DISABLED) + self.view_menu.entryconfig("Graph Viewer", state=tk.DISABLED) + self.view_menu.entryconfig("Packet Data", state=tk.DISABLED) + self.train_menu.entryconfig("Train Model", state=tk.DISABLED) + + def enable_menu_items(self): + self.view_menu.entryconfig("Statistics", state=tk.NORMAL) + self.view_menu.entryconfig("Graph Viewer", state=tk.NORMAL) + self.view_menu.entryconfig("Packet Data", state=tk.NORMAL) + self.train_menu.entryconfig("Train Model", state=tk.NORMAL) + self.start_button.config(state=tk.NORMAL) + + def on_close_window(self, window): + window.destroy() + self.enable_menu_items() class GraphViewer: def __init__(self, root): self.root = root @@ -385,4 +446,4 @@ class GraphViewer: if __name__ == "__main__": root = tk.Tk() app = PacketCaptureGUI(root) - root.mainloop() + root.mainloop() \ No newline at end of file