dev1461 commited on
Commit
6c5f9b0
Β·
verified Β·
1 Parent(s): b5ed450

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -46
app.py CHANGED
@@ -1,13 +1,58 @@
1
  import gradio as gr
2
  import numpy as np
 
3
 
4
  # =========================
5
- # Prediction Function
6
  # =========================
7
- def predict(duration, src_bytes, dst_bytes, failed_logins,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 and safe behavior detected."
23
- risk_level = "Low"
24
  elif score < 60:
25
  status = "⚠️ Suspicious Activity"
26
- explanation = "Moderate anomaly detected. Could indicate unusual behavior."
27
- risk_level = "Medium"
28
  else:
29
  status = "🚨 Attack Detected!"
30
- explanation = "High anomaly detected. Strong indication of malicious activity."
31
- risk_level = "High"
32
 
33
  return f"""
34
  {status}
35
 
36
- ### πŸ”’ Risk Score: {score:.2f}
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
- ### 🧠 Analysis:
45
- {explanation}
 
46
 
47
- ### πŸ›‘οΈ Recommendation:
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 Design
54
  # =========================
55
  with gr.Blocks() as demo:
56
 
57
- gr.Markdown("# πŸ” Intrusion Detection System (IDS)")
58
- gr.Markdown(
59
- "This system analyzes network traffic features and predicts whether the activity is **Normal** or a potential **Cyber Attack**.\n\n"
60
- "πŸ‘‰ Enter values below to simulate network behavior."
61
- )
 
 
 
 
 
 
 
62
 
63
- with gr.Row():
64
- duration = gr.Number(label="Connection Duration")
65
- src_bytes = gr.Number(label="Source Bytes (Data Sent)")
66
- dst_bytes = gr.Number(label="Destination Bytes (Data Received)")
67
 
68
- with gr.Row():
69
- failed_logins = gr.Number(label="Failed Login Attempts")
70
- login_attempts = gr.Number(label="Total Login Attempts")
71
- count = gr.Number(label="Number of Connections")
72
 
73
- with gr.Row():
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
- host_count = gr.Number(label="Host Count")
79
 
80
- predict_btn = gr.Button("πŸ” Analyze Traffic")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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