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