Updated monitoring logic in Final_Merged.py

This commit is contained in:
Vratika 2024-11-04 18:19:49 +05:30
parent 29343f4a5d
commit ffe2f85718

View File

@ -371,7 +371,7 @@ def send_data_to_api(cpu_usage, memory_usage):
print("Error while sending data:", str(e))
# Function to send ransomware prediction data to the API
def send_predictions_to_api(file_path):
def send_ransomware_type_predictionss_to_api(file_path):
url = "http://127.0.0.1:8000/ransomware-type-predictions/"
with open(file_path, 'rb') as f:
files = {'file': f}
@ -585,7 +585,7 @@ def run_predictions(selected_dir, status_label):
status_label.config(text="Predictions Saved")
try:
send_predictions_to_api("predictions.csv")
send_ransomware_type_predictionss_to_api("predictions.csv")
except:
print("Connection to API failed")
@ -638,7 +638,9 @@ if __name__ == "__main__":
else:
print("Please specify a directory.")
########################################################################################################################
########################################################################################################################
########################################################################################################################
#remsomwareaudit
@ -718,8 +720,8 @@ def predict_ransomware():
print(f"Predictions saved to {output_file} ({timestamp})")
def send_predictions_to_api(file_path):
url = "http://127.0.0.1:8000/predict-malware/"
def send_ransomware_predictions_to_api(file_path):
url = "http://127.0.0.1:8000/ransomware-predictions/"
with open(file_path, 'rb') as f:
files = {'file': f}
response = requests.post(url, files=files)
@ -728,7 +730,7 @@ def predict_ransomware():
else:
print(f"Failed to send {file_path} to API. Status code: {response.status_code}")
try:
send_predictions_to_api(output_file)
send_ransomware_predictions_to_api(output_file)
except:
print("Error Connection Server")
@ -1062,7 +1064,9 @@ class AuditDManagerApp:
########################################################################################################################
########################################################################################################################
########################################################################################################################
#malwaretested
@ -1137,16 +1141,27 @@ logging.basicConfig(level=logging.INFO)
def send_malware_logs(file_path):
url = "http://127.0.0.1:8000/upload-logs/"
with open(file_path, 'rb') as f:
files = {'file': f}
response = requests.post(url, files=files)
if response.status_code == 200:
print(f"Successfully sent {file_path} - LOGS to API.")
else:
print(f"Failed to send {file_path} - Logs to API. Status code: {response.status_code}")
def send_predictions_to_api(file_path):
def send_bytes_predictions_to_api(file_path):
url = "http://127.0.0.1:8000/predict-malware/"
with open(file_path, 'rb') as f:
files = {'csv_file': f}
response = requests.post(url, files=files)
if response.status_code == 200:
if response.status_code == 201:
print(f"Successfully sent {file_path} to API.")
else:
print(f"Failed to send {file_path} to API. Status code: {response.status_code}")
print(f"Failed to send malware_bytes_predictions {file_path} to API. Status code: {response.status_code}")
def send_asm_predictions_to_api(file_path):
@ -1154,10 +1169,10 @@ def send_asm_predictions_to_api(file_path):
with open(file_path, 'rb') as f:
files = {'csv_file': f}
response = requests.post(url, files=files)
if response.status_code == 200:
if response.status_code == 201:
print(f"Successfully sent {file_path} to API.")
else:
print(f"Failed to send {file_path} to API. Status code: {response.status_code}")
print(f"Failed to send malware_asm_predictions {file_path} to API. Status code: {response.status_code}")
@ -1181,9 +1196,14 @@ def convert_file_to_hex(input_file, output_file):
f.write(hex_dump)
logging.info(f"Converted '{input_file}' to hex dump and saved to '{output_file}'")
with open('malware_logs.txt', 'a') as log:
log.write(f"Converted '{input_file}' to hex dump and saved to '{output_file}' \n")
send_malware_logs("malware_logs.txt")
except Exception as e:
logging.error(f"Error converting '{input_file}': {e}")
def scan_and_convert_directory(directory, output_dir):
for root, _, files in os.walk(directory, followlinks=True):
for filename in files:
@ -1236,10 +1256,16 @@ class FileChangeHandler(FileSystemEventHandler):
def monitor_directories(directories, output_dir):
event_handler = FileChangeHandler(output_dir, hex_dirs=directories, disasm_dirs=directories)
observer = Observer()
# for directory in directories:
# observer.schedule(event_handler, path=directory, recursive=True)
# logging.info(f"Monitoring directory: {directory}")
for directory in directories:
observer.schedule(event_handler, path=directory, recursive=True)
logging.info(f"Monitoring directory: {directory}")
if os.path.exists(directory) and os.path.isdir(directory):
observer.schedule(event_handler, path=directory, recursive=True)
logging.info(f"Monitoring directory: {directory}")
else:
logging.warning(f"Directory not found or is not a directory: {directory}")
print(directories)
observer.start()
try:
while True:
@ -1406,11 +1432,11 @@ def run_malware_ai_analysis_bytes():
for model_file in model_files:
model_path = os.path.join(model_folder, model_file)
output_csv = os.path.join(bytes_result_directory, f"bytes_predictions_{os.path.splitext(model_file)[0]}.csv")
test_files(directory, model_path, output_csv)
try:
send_predictions_to_api(output_csv)
send_bytes_predictions_to_api(output_csv)
except:
print("Connection Failed")
print("UNABLE TO SEND MALWARE PREDICTIONS ON SERVER!")
test_files(directory, model_path, output_csv)
@ -1549,7 +1575,7 @@ def run_hex_conversion():
else:
messagebox.showwarning("Warning", f"{hex_dir} is not a directory.")
print("Hex conversion complete.")
print("Hex conversion completed")
run_malware_ai_analysis_bytes()
global isMonitoring
if(not isMonitoring):
@ -1701,7 +1727,7 @@ def submit_email():
webbrowser.open('http://127.0.0.1:8000/signup')
# Show OTP verification window after successful OTP request
root.after(180000, show_otp_verification_window, email)
root.after(120000, show_otp_verification_window, email)
else:
messagebox.showwarning("Error", "Failed to send OTP.")
except Exception as e:
@ -1988,7 +2014,9 @@ if __name__ == "__main__":
root.mainloop()
########################################################################################################################
########################################################################################################################
########################################################################################################################