Commit
·
58ce0c9
1
Parent(s):
8e67066
Update Sniffer_AI.py
Browse files- Sniffer_AI.py +38 -60
Sniffer_AI.py
CHANGED
@@ -3,88 +3,66 @@ import joblib
|
|
3 |
import requests
|
4 |
import os
|
5 |
|
6 |
-
from sklearn.ensemble import RandomForestClassifier
|
7 |
-
from sklearn.tree import DecisionTreeClassifier
|
8 |
-
|
9 |
|
10 |
# Load the saved models
|
11 |
rf_model = joblib.load('rf_model.pkl')
|
12 |
-
dt_model = joblib.load('decision_tree_model.pkl')
|
13 |
-
bagging_model = joblib.load('model_bagging.pkl')
|
14 |
-
ada_model = joblib.load('model_adaboost.pkl')
|
15 |
|
16 |
-
# Define the feature names
|
17 |
feature_names = [
|
18 |
-
"
|
19 |
-
"src_bytes", "dst_bytes", "conn_state", "missed_bytes", "src_pkts",
|
20 |
-
"src_ip_bytes", "dst_pkts", "dst_ip_bytes", "dns_query", "dns_qclass",
|
21 |
-
"dns_qtype", "dns_rcode", "dns_AA", "dns_RD", "dns_RA", "dns_rejected",
|
22 |
-
"ssl_version", "ssl_cipher", "ssl_resumed", "ssl_established", "ssl_subject",
|
23 |
-
"ssl_issuer", "http_trans_depth", "http_method", "http_uri", "http_version",
|
24 |
-
"http_request_body_len", "http_response_body_len", "http_status_code",
|
25 |
-
"http_user_agent", "http_orig_mime_types", "http_resp_mime_types",
|
26 |
-
"weird_name", "weird_addl", "weird_notice", "label"
|
27 |
]
|
28 |
|
29 |
class_labels = {
|
30 |
0: "normal",
|
31 |
1: "backdoor",
|
32 |
2: "ddos",
|
33 |
-
3: "
|
34 |
-
4: "
|
35 |
-
5: "
|
36 |
-
6: "
|
37 |
-
7: "
|
38 |
-
8: "xss",
|
39 |
-
9: "mitm"
|
40 |
}
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
if len(feature_values) != len(feature_names):
|
45 |
-
return "Please fill in all the required feature values."
|
46 |
|
47 |
-
|
|
|
48 |
try:
|
49 |
-
|
50 |
-
except
|
51 |
-
return "
|
52 |
-
|
53 |
-
# Choose the model based on user selection
|
54 |
-
if model_choice == "Random Forest":
|
55 |
-
model = rf_model
|
56 |
-
elif model_choice == "Decision Tree":
|
57 |
-
model = dt_model
|
58 |
-
elif model_choice == "Bagging Classifier":
|
59 |
-
model = bagging_model
|
60 |
-
elif model_choice == "AdaBoost Classifier":
|
61 |
-
model = ada_model
|
62 |
-
else:
|
63 |
-
return "Invalid model choice!"
|
64 |
-
|
65 |
-
# Predict the class (multi-class classification)
|
66 |
-
prediction = model.predict([feature_values])
|
67 |
-
predicted_class = prediction[0] # Get the predicted class (an integer between 0-9)
|
68 |
|
69 |
-
#
|
70 |
-
if
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
74 |
|
75 |
-
#
|
76 |
-
|
77 |
|
78 |
-
#
|
79 |
-
|
80 |
|
81 |
-
# Create
|
82 |
iface = gr.Interface(
|
83 |
-
fn=detect_intrusion,
|
84 |
-
inputs=
|
|
|
|
|
85 |
outputs="text",
|
86 |
title="Intrusion Detection System",
|
87 |
-
description="
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
)
|
89 |
|
90 |
# Launch the interface locally for testing
|
|
|
3 |
import requests
|
4 |
import os
|
5 |
|
6 |
+
from sklearn.ensemble import RandomForestClassifier
|
|
|
|
|
7 |
|
8 |
# Load the saved models
|
9 |
rf_model = joblib.load('rf_model.pkl')
|
|
|
|
|
|
|
10 |
|
11 |
+
# Define the feature names (excluding the target column 'type')
|
12 |
feature_names = [
|
13 |
+
"date", "time", "door_state", "sphone_signal", "label"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
]
|
15 |
|
16 |
class_labels = {
|
17 |
0: "normal",
|
18 |
1: "backdoor",
|
19 |
2: "ddos",
|
20 |
+
3: "injection",
|
21 |
+
4: "password",
|
22 |
+
5: "ransomware",
|
23 |
+
6: "scanning",
|
24 |
+
7: "xss",
|
|
|
|
|
25 |
}
|
26 |
|
27 |
+
# Placeholder model (replace with actual Random Forest model object)
|
28 |
+
rf_model = None # Load the actual trained Random Forest model here
|
|
|
|
|
29 |
|
30 |
+
def detect_intrusion(file):
|
31 |
+
# Read the uploaded log file as a CSV or structured data
|
32 |
try:
|
33 |
+
log_data = pd.read_csv(file.name) # Use file.name to get the path for reading
|
34 |
+
except Exception as e:
|
35 |
+
return f"Error reading file: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
+
# Check if all required feature columns are in the log file
|
38 |
+
missing_features = [feature for feature in feature_names if feature not in log_data.columns]
|
39 |
+
if missing_features:
|
40 |
+
return f"Missing features in file: {', '.join(missing_features)}"
|
41 |
+
|
42 |
+
# Extract the feature values (excluding the 'type' column which is the target)
|
43 |
+
feature_values = log_data[feature_names].astype(float).values
|
44 |
|
45 |
+
# Predict the class (multi-class classification) for each row in the log file
|
46 |
+
predictions = rf_model.predict(feature_values)
|
47 |
|
48 |
+
# Return only the 'Prediction' and 'label' columns
|
49 |
+
return log_data[['Prediction']].head().to_string()
|
50 |
|
51 |
+
# Create a Gradio interface
|
52 |
iface = gr.Interface(
|
53 |
+
fn=detect_intrusion,
|
54 |
+
inputs=[
|
55 |
+
gr.File(label="Upload Log File (CSV format)") # File input
|
56 |
+
],
|
57 |
outputs="text",
|
58 |
title="Intrusion Detection System",
|
59 |
+
description=("""
|
60 |
+
Upload a CSV log file containing the following features:
|
61 |
+
date, time, door_state, sphone_signal, label (without the 'type' column).
|
62 |
+
Example file structure:
|
63 |
+
date,time,door_state,sphone_signal,label
|
64 |
+
2025-03-12,10:45:00,1,-85,normal
|
65 |
+
""")
|
66 |
)
|
67 |
|
68 |
# Launch the interface locally for testing
|