Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,58 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
|
|
|
| 3 |
|
| 4 |
# =========================
|
| 5 |
-
#
|
| 6 |
# =========================
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
count, error_rate, same_srv_rate, diff_srv_rate,
|
| 9 |
host_count, login_attempts):
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
| 11 |
data = np.array([
|
| 12 |
duration, src_bytes, dst_bytes, failed_logins,
|
| 13 |
count, error_rate, same_srv_rate, diff_srv_rate,
|
|
@@ -16,78 +61,87 @@ def predict(duration, src_bytes, dst_bytes, failed_logins,
|
|
| 16 |
|
| 17 |
score = np.sum(data)
|
| 18 |
|
| 19 |
-
# Define ranges
|
| 20 |
if score < 30:
|
| 21 |
status = "β
Normal Traffic"
|
| 22 |
-
explanation = "Low activity
|
| 23 |
-
risk_level = "Low"
|
| 24 |
elif score < 60:
|
| 25 |
status = "β οΈ Suspicious Activity"
|
| 26 |
-
explanation = "Moderate anomaly detected.
|
| 27 |
-
risk_level = "Medium"
|
| 28 |
else:
|
| 29 |
status = "π¨ Attack Detected!"
|
| 30 |
-
explanation = "High anomaly detected.
|
| 31 |
-
risk_level = "High"
|
| 32 |
|
| 33 |
return f"""
|
| 34 |
{status}
|
| 35 |
|
| 36 |
-
|
| 37 |
-
The risk score represents the overall intensity of network activity based on input features.
|
| 38 |
-
|
| 39 |
-
### π Interpretation:
|
| 40 |
-
- **0 β 30 β Normal Traffic (Low Risk)**
|
| 41 |
-
- **30 β 60 β Suspicious Activity (Medium Risk)**
|
| 42 |
-
- **60+ β Attack (High Risk)**
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
{"No action needed." if score < 30 else "Monitor the system." if score < 60 else "Immediate action required! Block or investigate."}
|
| 49 |
"""
|
| 50 |
|
| 51 |
|
| 52 |
# =========================
|
| 53 |
-
# UI
|
| 54 |
# =========================
|
| 55 |
with gr.Blocks() as demo:
|
| 56 |
|
| 57 |
-
gr.Markdown("# π
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
src_bytes = gr.Number(label="Source Bytes (Data Sent)")
|
| 66 |
-
dst_bytes = gr.Number(label="Destination Bytes (Data Received)")
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
count = gr.Number(label="Number of Connections")
|
| 72 |
|
| 73 |
-
|
| 74 |
-
error_rate = gr.Number(label="Error Rate")
|
| 75 |
-
same_srv_rate = gr.Number(label="Same Service Rate")
|
| 76 |
-
diff_srv_rate = gr.Number(label="Different Service Rate")
|
| 77 |
|
| 78 |
-
|
| 79 |
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
output = gr.Markdown()
|
| 83 |
|
| 84 |
predict_btn.click(
|
| 85 |
predict,
|
| 86 |
-
inputs=[
|
| 87 |
-
duration, src_bytes, dst_bytes, failed_logins,
|
| 88 |
-
count, error_rate, same_srv_rate, diff_srv_rate,
|
| 89 |
-
host_count, login_attempts
|
| 90 |
-
],
|
| 91 |
outputs=output
|
| 92 |
)
|
| 93 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
+
import random
|
| 4 |
|
| 5 |
# =========================
|
| 6 |
+
# DATABASE (temporary)
|
| 7 |
# =========================
|
| 8 |
+
users = {}
|
| 9 |
+
|
| 10 |
+
# Store OTP temporarily
|
| 11 |
+
otp_store = {}
|
| 12 |
+
|
| 13 |
+
# =========================
|
| 14 |
+
# SIGNUP
|
| 15 |
+
# =========================
|
| 16 |
+
def signup(username, password):
|
| 17 |
+
if username in users:
|
| 18 |
+
return "β User already exists"
|
| 19 |
+
|
| 20 |
+
users[username] = password
|
| 21 |
+
return "β
Signup successful! Please login."
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
# =========================
|
| 25 |
+
# LOGIN (STEP 1)
|
| 26 |
+
# =========================
|
| 27 |
+
def login(username, password):
|
| 28 |
+
if username in users and users[username] == password:
|
| 29 |
+
otp = random.randint(1000, 9999)
|
| 30 |
+
otp_store[username] = otp
|
| 31 |
+
return f"β
OTP generated: {otp} (demo purpose)"
|
| 32 |
+
else:
|
| 33 |
+
return "β Invalid credentials"
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
# =========================
|
| 37 |
+
# OTP VERIFY (STEP 2)
|
| 38 |
+
# =========================
|
| 39 |
+
def verify_otp(username, otp_input):
|
| 40 |
+
if username in otp_store and str(otp_store[username]) == str(otp_input):
|
| 41 |
+
return "β
Login Successful", True
|
| 42 |
+
else:
|
| 43 |
+
return "β Invalid OTP", False
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
# =========================
|
| 47 |
+
# PREDICT FUNCTION
|
| 48 |
+
# =========================
|
| 49 |
+
def predict(logged_in, duration, src_bytes, dst_bytes, failed_logins,
|
| 50 |
count, error_rate, same_srv_rate, diff_srv_rate,
|
| 51 |
host_count, login_attempts):
|
| 52 |
+
|
| 53 |
+
if not logged_in:
|
| 54 |
+
return "π« Please login first"
|
| 55 |
+
|
| 56 |
data = np.array([
|
| 57 |
duration, src_bytes, dst_bytes, failed_logins,
|
| 58 |
count, error_rate, same_srv_rate, diff_srv_rate,
|
|
|
|
| 61 |
|
| 62 |
score = np.sum(data)
|
| 63 |
|
|
|
|
| 64 |
if score < 30:
|
| 65 |
status = "β
Normal Traffic"
|
| 66 |
+
explanation = "Low activity detected."
|
|
|
|
| 67 |
elif score < 60:
|
| 68 |
status = "β οΈ Suspicious Activity"
|
| 69 |
+
explanation = "Moderate anomaly detected."
|
|
|
|
| 70 |
else:
|
| 71 |
status = "π¨ Attack Detected!"
|
| 72 |
+
explanation = "High anomaly detected."
|
|
|
|
| 73 |
|
| 74 |
return f"""
|
| 75 |
{status}
|
| 76 |
|
| 77 |
+
Risk Score: {score:.2f}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
+
0β30 β Normal
|
| 80 |
+
30β60 β Suspicious
|
| 81 |
+
60+ β Attack
|
| 82 |
|
| 83 |
+
Analysis: {explanation}
|
|
|
|
| 84 |
"""
|
| 85 |
|
| 86 |
|
| 87 |
# =========================
|
| 88 |
+
# UI
|
| 89 |
# =========================
|
| 90 |
with gr.Blocks() as demo:
|
| 91 |
|
| 92 |
+
gr.Markdown("# π Secure IDS with Authentication & 2FA")
|
| 93 |
+
|
| 94 |
+
# SIGNUP
|
| 95 |
+
gr.Markdown("## π Signup")
|
| 96 |
+
su_user = gr.Textbox(label="Username")
|
| 97 |
+
su_pass = gr.Textbox(label="Password", type="password")
|
| 98 |
+
su_btn = gr.Button("Signup")
|
| 99 |
+
su_out = gr.Textbox()
|
| 100 |
+
|
| 101 |
+
su_btn.click(signup, inputs=[su_user, su_pass], outputs=su_out)
|
| 102 |
+
|
| 103 |
+
gr.Markdown("---")
|
| 104 |
|
| 105 |
+
# LOGIN
|
| 106 |
+
gr.Markdown("## π Login")
|
|
|
|
|
|
|
| 107 |
|
| 108 |
+
li_user = gr.Textbox(label="Username")
|
| 109 |
+
li_pass = gr.Textbox(label="Password", type="password")
|
| 110 |
+
login_btn = gr.Button("Login")
|
|
|
|
| 111 |
|
| 112 |
+
login_out = gr.Textbox()
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
+
login_btn.click(login, inputs=[li_user, li_pass], outputs=login_out)
|
| 115 |
|
| 116 |
+
# OTP
|
| 117 |
+
gr.Markdown("## π’ Enter OTP")
|
| 118 |
+
|
| 119 |
+
otp_input = gr.Textbox(label="OTP")
|
| 120 |
+
otp_btn = gr.Button("Verify OTP")
|
| 121 |
+
|
| 122 |
+
otp_out = gr.Textbox()
|
| 123 |
+
logged_state = gr.State(False)
|
| 124 |
+
|
| 125 |
+
otp_btn.click(
|
| 126 |
+
verify_otp,
|
| 127 |
+
inputs=[li_user, otp_input],
|
| 128 |
+
outputs=[otp_out, logged_state]
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
gr.Markdown("---")
|
| 132 |
+
|
| 133 |
+
# IDS
|
| 134 |
+
gr.Markdown("## π Network Analysis")
|
| 135 |
+
|
| 136 |
+
inputs = [gr.Number(label=f"Feature {i+1}") for i in range(10)]
|
| 137 |
+
|
| 138 |
+
predict_btn = gr.Button("Analyze")
|
| 139 |
|
| 140 |
output = gr.Markdown()
|
| 141 |
|
| 142 |
predict_btn.click(
|
| 143 |
predict,
|
| 144 |
+
inputs=[logged_state] + inputs,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
outputs=output
|
| 146 |
)
|
| 147 |
|