Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,87 +2,146 @@ import gradio as gr
|
|
| 2 |
import numpy as np
|
| 3 |
import random
|
| 4 |
|
|
|
|
|
|
|
|
|
|
| 5 |
users = {}
|
| 6 |
otp_store = {}
|
| 7 |
|
| 8 |
# =========================
|
| 9 |
-
# SIGNUP
|
| 10 |
# =========================
|
| 11 |
def signup(username, password):
|
| 12 |
if username in users:
|
| 13 |
return "β User already exists"
|
|
|
|
| 14 |
users[username] = password
|
| 15 |
-
return "β
Signup successful!
|
| 16 |
|
| 17 |
# =========================
|
| 18 |
-
# LOGIN
|
| 19 |
# =========================
|
| 20 |
def login(username, password):
|
| 21 |
if username in users and users[username] == password:
|
| 22 |
otp = random.randint(1000, 9999)
|
| 23 |
otp_store[username] = otp
|
| 24 |
-
return f"OTP generated: {otp} (demo)"
|
| 25 |
-
|
|
|
|
| 26 |
|
| 27 |
# =========================
|
| 28 |
-
# OTP
|
| 29 |
# =========================
|
| 30 |
def verify_otp(username, otp_input):
|
| 31 |
if username in otp_store and str(otp_store[username]) == str(otp_input):
|
| 32 |
-
return "β
Login Successful", True
|
|
|
|
| 33 |
return "β Invalid OTP", False
|
| 34 |
|
| 35 |
# =========================
|
| 36 |
-
#
|
| 37 |
# =========================
|
| 38 |
-
def predict(logged_in,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
if not logged_in:
|
| 40 |
-
return "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
data = np.array(inputs)
|
| 43 |
score = np.sum(data)
|
| 44 |
|
|
|
|
| 45 |
if score < 30:
|
| 46 |
-
|
|
|
|
|
|
|
| 47 |
elif score < 60:
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
else:
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
# =========================
|
| 53 |
-
# UI
|
| 54 |
# =========================
|
| 55 |
with gr.Blocks() as demo:
|
| 56 |
|
| 57 |
logged_in = gr.State(False)
|
| 58 |
|
| 59 |
-
gr.Markdown("# π Secure
|
| 60 |
|
| 61 |
with gr.Tabs():
|
| 62 |
|
| 63 |
-
# SIGNUP
|
| 64 |
with gr.Tab("Signup"):
|
|
|
|
|
|
|
| 65 |
su_user = gr.Textbox(label="Username")
|
| 66 |
su_pass = gr.Textbox(label="Password", type="password")
|
|
|
|
| 67 |
su_btn = gr.Button("Signup")
|
| 68 |
su_out = gr.Textbox()
|
| 69 |
|
| 70 |
su_btn.click(signup, [su_user, su_pass], su_out)
|
| 71 |
|
| 72 |
-
# LOGIN
|
| 73 |
with gr.Tab("Login"):
|
|
|
|
|
|
|
| 74 |
li_user = gr.Textbox(label="Username")
|
| 75 |
li_pass = gr.Textbox(label="Password", type="password")
|
|
|
|
| 76 |
li_btn = gr.Button("Login")
|
| 77 |
li_out = gr.Textbox()
|
| 78 |
|
| 79 |
li_btn.click(login, [li_user, li_pass], li_out)
|
| 80 |
|
| 81 |
-
# OTP
|
| 82 |
with gr.Tab("OTP Verification"):
|
|
|
|
|
|
|
| 83 |
otp_user = gr.Textbox(label="Username")
|
| 84 |
otp_input = gr.Textbox(label="OTP")
|
| 85 |
-
|
|
|
|
| 86 |
otp_out = gr.Textbox()
|
| 87 |
|
| 88 |
otp_btn.click(
|
|
@@ -91,12 +150,38 @@ with gr.Blocks() as demo:
|
|
| 91 |
[otp_out, logged_in]
|
| 92 |
)
|
| 93 |
|
| 94 |
-
# DASHBOARD
|
| 95 |
with gr.Tab("IDS Dashboard"):
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
output = gr.Markdown()
|
| 99 |
|
| 100 |
-
pred_btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
demo.launch()
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import random
|
| 4 |
|
| 5 |
+
# =========================
|
| 6 |
+
# DATABASE (temporary)
|
| 7 |
+
# =========================
|
| 8 |
users = {}
|
| 9 |
otp_store = {}
|
| 10 |
|
| 11 |
# =========================
|
| 12 |
+
# SIGNUP FUNCTION
|
| 13 |
# =========================
|
| 14 |
def signup(username, password):
|
| 15 |
if username in users:
|
| 16 |
return "β User already exists"
|
| 17 |
+
|
| 18 |
users[username] = password
|
| 19 |
+
return "β
Signup successful! Now go to Login tab."
|
| 20 |
|
| 21 |
# =========================
|
| 22 |
+
# LOGIN FUNCTION
|
| 23 |
# =========================
|
| 24 |
def login(username, password):
|
| 25 |
if username in users and users[username] == password:
|
| 26 |
otp = random.randint(1000, 9999)
|
| 27 |
otp_store[username] = otp
|
| 28 |
+
return f"β
OTP generated: {otp} (for demo use this OTP)"
|
| 29 |
+
|
| 30 |
+
return "β Invalid username or password"
|
| 31 |
|
| 32 |
# =========================
|
| 33 |
+
# OTP VERIFICATION
|
| 34 |
# =========================
|
| 35 |
def verify_otp(username, otp_input):
|
| 36 |
if username in otp_store and str(otp_store[username]) == str(otp_input):
|
| 37 |
+
return "β
Login Successful! You can now use IDS Dashboard.", True
|
| 38 |
+
|
| 39 |
return "β Invalid OTP", False
|
| 40 |
|
| 41 |
# =========================
|
| 42 |
+
# IDS PREDICTION FUNCTION
|
| 43 |
# =========================
|
| 44 |
+
def predict(logged_in, duration, src_bytes, dst_bytes, failed_logins,
|
| 45 |
+
count, error_rate, same_srv_rate, diff_srv_rate,
|
| 46 |
+
host_count, login_attempts):
|
| 47 |
+
|
| 48 |
+
# π Access control
|
| 49 |
if not logged_in:
|
| 50 |
+
return """
|
| 51 |
+
π« **Access Denied**
|
| 52 |
+
|
| 53 |
+
You must:
|
| 54 |
+
1. Signup
|
| 55 |
+
2. Login
|
| 56 |
+
3. Verify OTP
|
| 57 |
+
|
| 58 |
+
π Only then you can access IDS Dashboard
|
| 59 |
+
"""
|
| 60 |
+
|
| 61 |
+
# Prepare data
|
| 62 |
+
data = np.array([
|
| 63 |
+
duration, src_bytes, dst_bytes, failed_logins,
|
| 64 |
+
count, error_rate, same_srv_rate, diff_srv_rate,
|
| 65 |
+
host_count, login_attempts
|
| 66 |
+
])
|
| 67 |
|
|
|
|
| 68 |
score = np.sum(data)
|
| 69 |
|
| 70 |
+
# Decision logic
|
| 71 |
if score < 30:
|
| 72 |
+
status = "β
Normal Traffic"
|
| 73 |
+
explanation = "Low activity and safe behavior detected."
|
| 74 |
+
recommendation = "No action needed."
|
| 75 |
elif score < 60:
|
| 76 |
+
status = "β οΈ Suspicious Activity"
|
| 77 |
+
explanation = "Moderate anomaly detected."
|
| 78 |
+
recommendation = "Monitor the system."
|
| 79 |
else:
|
| 80 |
+
status = "π¨ Attack Detected!"
|
| 81 |
+
explanation = "High anomaly detected. Possible malicious activity."
|
| 82 |
+
recommendation = "Immediate action required!"
|
| 83 |
+
|
| 84 |
+
return f"""
|
| 85 |
+
{status}
|
| 86 |
+
|
| 87 |
+
### π’ Risk Score: {score:.2f}
|
| 88 |
+
The risk score represents combined network activity intensity.
|
| 89 |
+
|
| 90 |
+
### π Interpretation:
|
| 91 |
+
- 0 β 30 β Normal (Low Risk)
|
| 92 |
+
- 30 β 60 β Suspicious (Medium Risk)
|
| 93 |
+
- 60+ β Attack (High Risk)
|
| 94 |
+
|
| 95 |
+
### π§ Analysis:
|
| 96 |
+
{explanation}
|
| 97 |
+
|
| 98 |
+
### π‘οΈ Recommendation:
|
| 99 |
+
{recommendation}
|
| 100 |
+
"""
|
| 101 |
|
| 102 |
# =========================
|
| 103 |
+
# UI DESIGN
|
| 104 |
# =========================
|
| 105 |
with gr.Blocks() as demo:
|
| 106 |
|
| 107 |
logged_in = gr.State(False)
|
| 108 |
|
| 109 |
+
gr.Markdown("# π Secure Intrusion Detection System")
|
| 110 |
|
| 111 |
with gr.Tabs():
|
| 112 |
|
| 113 |
+
# ================= SIGNUP =================
|
| 114 |
with gr.Tab("Signup"):
|
| 115 |
+
gr.Markdown("### Create a new account")
|
| 116 |
+
|
| 117 |
su_user = gr.Textbox(label="Username")
|
| 118 |
su_pass = gr.Textbox(label="Password", type="password")
|
| 119 |
+
|
| 120 |
su_btn = gr.Button("Signup")
|
| 121 |
su_out = gr.Textbox()
|
| 122 |
|
| 123 |
su_btn.click(signup, [su_user, su_pass], su_out)
|
| 124 |
|
| 125 |
+
# ================= LOGIN =================
|
| 126 |
with gr.Tab("Login"):
|
| 127 |
+
gr.Markdown("### Enter credentials")
|
| 128 |
+
|
| 129 |
li_user = gr.Textbox(label="Username")
|
| 130 |
li_pass = gr.Textbox(label="Password", type="password")
|
| 131 |
+
|
| 132 |
li_btn = gr.Button("Login")
|
| 133 |
li_out = gr.Textbox()
|
| 134 |
|
| 135 |
li_btn.click(login, [li_user, li_pass], li_out)
|
| 136 |
|
| 137 |
+
# ================= OTP =================
|
| 138 |
with gr.Tab("OTP Verification"):
|
| 139 |
+
gr.Markdown("### Enter OTP sent after login")
|
| 140 |
+
|
| 141 |
otp_user = gr.Textbox(label="Username")
|
| 142 |
otp_input = gr.Textbox(label="OTP")
|
| 143 |
+
|
| 144 |
+
otp_btn = gr.Button("Verify OTP")
|
| 145 |
otp_out = gr.Textbox()
|
| 146 |
|
| 147 |
otp_btn.click(
|
|
|
|
| 150 |
[otp_out, logged_in]
|
| 151 |
)
|
| 152 |
|
| 153 |
+
# ================= IDS DASHBOARD =================
|
| 154 |
with gr.Tab("IDS Dashboard"):
|
| 155 |
+
|
| 156 |
+
gr.Markdown("### Analyze Network Traffic")
|
| 157 |
+
|
| 158 |
+
duration = gr.Number(label="Connection Duration")
|
| 159 |
+
src_bytes = gr.Number(label="Source Bytes (Data Sent)")
|
| 160 |
+
dst_bytes = gr.Number(label="Destination Bytes (Data Received)")
|
| 161 |
+
|
| 162 |
+
failed_logins = gr.Number(label="Failed Login Attempts")
|
| 163 |
+
login_attempts = gr.Number(label="Total Login Attempts")
|
| 164 |
+
count = gr.Number(label="Connection Count")
|
| 165 |
+
|
| 166 |
+
error_rate = gr.Number(label="Error Rate")
|
| 167 |
+
same_srv_rate = gr.Number(label="Same Service Rate")
|
| 168 |
+
diff_srv_rate = gr.Number(label="Different Service Rate")
|
| 169 |
+
|
| 170 |
+
host_count = gr.Number(label="Host Count")
|
| 171 |
+
|
| 172 |
+
pred_btn = gr.Button("π Analyze Traffic")
|
| 173 |
+
|
| 174 |
output = gr.Markdown()
|
| 175 |
|
| 176 |
+
pred_btn.click(
|
| 177 |
+
predict,
|
| 178 |
+
[
|
| 179 |
+
logged_in,
|
| 180 |
+
duration, src_bytes, dst_bytes, failed_logins,
|
| 181 |
+
count, error_rate, same_srv_rate, diff_srv_rate,
|
| 182 |
+
host_count, login_attempts
|
| 183 |
+
],
|
| 184 |
+
output
|
| 185 |
+
)
|
| 186 |
|
| 187 |
demo.launch()
|