From 29343f4a5dcffcc2059e77da8c2c73f2ffb17e4c Mon Sep 17 00:00:00 2001 From: Vratika Date: Tue, 29 Oct 2024 18:49:42 +0530 Subject: [PATCH] new cheges on this --- Final_Merged.py | 112 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 81 insertions(+), 31 deletions(-) diff --git a/Final_Merged.py b/Final_Merged.py index 0dcd7fd..2b4043f 100644 --- a/Final_Merged.py +++ b/Final_Merged.py @@ -42,7 +42,7 @@ import requests # Global variable for thread control stop_event = Event() value = False - +sudo_verified = False # Important features and weights as provided important_features = [ 'pktcount', @@ -247,21 +247,57 @@ def capture_packets(interface=None): except KeyboardInterrupt: print("\nPacket capturing stopped.") -def start_capture(): - global thread - if os.geteuid() != 0: - root.withdraw() # Hide the main window - password = simpledialog.askstring("Password", "Enter your sudo password and run again:", show='*') +# def start_capture(): +# global thread +# if os.geteuid() != 0: + +# root.withdraw() # Hide the main window +# password = simpledialog.askstring("Password", "Enter your sudo password and run again:", show='*') +# if password: +# try: +# subprocess.run(['sudo', '-S', sys.executable] + sys.argv, input=password.encode(), check=True) +# except subprocess.CalledProcessError: +# messagebox.showerror("Error", "Failed to run the script with sudo.") +# finally: +# root.destroy() +# else: +# messagebox.showerror("Error", "No password provided. Unable to run with sudo.") +# elif not stop_event.is_set(): +# global value +# value = True +# stop_event.clear() +# thread = Thread(target=capture_packets) +# thread.start() + +# start_button.config(state=tk.DISABLED) +# stop_button.config(state=tk.NORMAL) +def verify_sudo(): + global sudo_verified + if not sudo_verified: + # Ask for sudo password without hiding the main window + password = simpledialog.askstring("Password", "Enter your sudo password to proceed:", show='*') if password: try: - subprocess.run(['sudo', '-S', sys.executable] + sys.argv, input=password.encode(), check=True) + # Run a command to verify and cache sudo privileges + result = subprocess.run(['sudo', '-S', 'echo', 'Password is correct'], input=password.encode(), check=True, stdout=subprocess.PIPE) + + if "Password is correct" in result.stdout.decode(): + # Confirmation of successful sudo verification + sudo_verified = True + messagebox.showinfo("Success", "Password is correct. Capture is ready to start.") + start_button.config(state=tk.NORMAL) + else: + messagebox.showerror("Error", "Failed to verify sudo privileges. Please check your password.") except subprocess.CalledProcessError: - messagebox.showerror("Error", "Failed to run the script with sudo.") - finally: - root.destroy() + messagebox.showerror("Error", "Failed to verify sudo privileges. Please check your password.") else: - messagebox.showerror("Error", "No password provided. Unable to run with sudo.") - elif not stop_event.is_set(): + messagebox.showerror("Error", "No password provided. Unable to proceed.") +def start_capture(): + global thread + if not sudo_verified: + verify_sudo() + elif not stop_event.is_set() and sudo_verified: + # Start capturing packets if no stop event is set and sudo is verified global value value = True stop_event.clear() @@ -271,9 +307,6 @@ def start_capture(): start_button.config(state=tk.DISABLED) stop_button.config(state=tk.NORMAL) - - - def stop_capture(): global value value = False @@ -344,6 +377,7 @@ def send_predictions_to_api(file_path): files = {'file': f} response = requests.post(url, files=files) if response.status_code == 200: + print(f"Successfully sent {file_path} to API.") else: print(f"Failed to send {file_path} to API. Status code: {response.status_code}") @@ -1107,7 +1141,7 @@ logging.basicConfig(level=logging.INFO) def send_predictions_to_api(file_path): url = "http://127.0.0.1:8000/predict-malware/" with open(file_path, 'rb') as f: - files = {'file': f} + files = {'csv_file': f} response = requests.post(url, files=files) if response.status_code == 200: print(f"Successfully sent {file_path} to API.") @@ -1118,7 +1152,7 @@ def send_predictions_to_api(file_path): def send_asm_predictions_to_api(file_path): url = "http://127.0.0.1:8000/predict-malware/" with open(file_path, 'rb') as f: - files = {'file': f} + files = {'csv_file': f} response = requests.post(url, files=files) if response.status_code == 200: print(f"Successfully sent {file_path} to API.") @@ -1642,11 +1676,13 @@ def get_unique_id(): # Function to handle the "Check Device" button def check_device(): - response = messagebox.askyesno("Check Device", "Do you want to check your device?") - if response: # User clicked "Yes" - email_label.pack() - email_entry.pack() - submit_button.pack() + # Hide the "Check Device" button + check_button.pack_forget() + + # Show the email label, entry, and submit button + email_label.pack(pady=(20, 5)) + email_entry.pack(pady=(0, 20)) + submit_button.pack(pady=10) # Function to validate and submit the entered email and call the send-otp API def submit_email(): @@ -1665,7 +1701,7 @@ def submit_email(): webbrowser.open('http://127.0.0.1:8000/signup') # Show OTP verification window after successful OTP request - root.after(600000, show_otp_verification_window, email) + root.after(180000, show_otp_verification_window, email) else: messagebox.showwarning("Error", "Failed to send OTP.") except Exception as e: @@ -1737,7 +1773,7 @@ def send_device_info(user_profile_id): messagebox.showinfo("Success", "Device info sent successfully!") webbrowser.open('http://127.0.0.1:8000/home') - root.destroy() + else: messagebox.showwarning("Error", f"Failed to send device info. {response.json().get('error')}") @@ -1749,7 +1785,7 @@ from tkinter import ttk # Requires `pip install ttkthemes` def create_wizard_window(): - global root,email_label, email_entry, submit_button + global root,email_label, email_entry, submit_button,check_button root = tk.Tk() root.title("File Conversion and Disassembly Wizard") root.geometry("700x450") @@ -1803,18 +1839,32 @@ def create_wizard_window(): desc_label1 = tk.Label(frame1, text="This wizard will guide you through the steps.", bg="#ffffff", font=("Arial", 12)) desc_label1.pack(pady=10) - check_button = tk.Button(frame1, text="Check Device", command=check_device) + check_button = tk.Button( + frame1, + text="Check Device", + font=("Arial", 12), + bg="#4CAF50", + fg="white", + activebackground="#45A049", + width=15, + command=check_device + ) check_button.pack(pady=20) - + # Label and entry for email input (initially hidden) - email_label = tk.Label(frame1, text="Enter your email:") + email_label = tk.Label(frame1, text="Enter your email:", font=("Arial", 12)) email_entry = tk.Entry(frame1) - submit_button = tk.Button(frame1, text="Submit", command=submit_email) + # Frame for Submit and Next buttons (initially hidden) + button_frame = tk.Frame(frame1, bg="#ffffff") + submit_button = tk.Button(button_frame, text="Submit", command=submit_email) + next_button1 = ttk.Button(button_frame, text="Next ➡️", command=lambda: [update_progress(2), show_frame(frame2)]) + # Arrange buttons within button_frame for side-by-side layout + button_frame.pack(side="bottom", pady=10) + + next_button1.pack(side="right", padx=10, pady=10) - next_button1 = ttk.Button(frame1, text="Next ➡️", command=lambda: [update_progress(2), show_frame(frame2)]) - next_button1.pack(pady=20) # Frame 2 - Packet Capture Setup