PhilipL commited on
Commit
84012bb
·
verified ·
1 Parent(s): 6616142

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -71
app.py CHANGED
@@ -101,12 +101,9 @@ def check_anomaly(ip, action, log_time, overall_w, time_w, ip_w, dev_w):
101
  return "⚠️異常_作息或行為不符"
102
 
103
  return "success"
104
- # 🌟 關鍵修改位置:分層捕捉錯誤
105
  except ValueError:
106
- # 當 LabelEncoder 遇到從未見過的 IP 或是操作行為,會觸發 ValueError
107
  return "⚠️異常_未知特徵 (出現未授權的 IP 或操作)"
108
  except Exception as e:
109
- # 其他不可預期的錯誤,才顯示為系統錯誤
110
  return f"⚠️系統錯誤: {str(e)}"
111
 
112
  def record_log(ip, cookie, account, role, action, status, time_mode, custom_time, ow, tw, iw, dw):
@@ -151,8 +148,9 @@ def fast_login(target_account, ip, cookie, time_mode, custom_time, ow, tw, iw, d
151
  df = record_log(ip, cookie, target_account, role, "login_attempt", "success", time_mode, custom_time, ow, tw, iw, dw)
152
 
153
  welcome_msg = f"**{name}** 歡迎您 | 線上人數: 938 (⚡快速登入模式)"
154
- is_student = (role == "學生")
155
- is_admin = (role == "管理")
 
156
 
157
  return gr.update(visible=False), gr.update(visible=True), gr.update(visible=is_student), gr.update(visible=is_admin), "", welcome_msg, df, target_account
158
 
@@ -174,8 +172,9 @@ def process_login(ip, cookie, account, password, captcha, time_mode, custom_time
174
  if USER_DB[account][0] == password:
175
  welcome_msg = f"**{name}** 歡迎您 | 線上人數: 938"
176
  df = record_log(ip, cookie, account, role, "login_attempt", "success", time_mode, custom_time, ow, tw, iw, dw)
177
- is_student = (role == "學生")
178
- is_admin = (role == "管理")
 
179
 
180
  return gr.update(visible=False), gr.update(visible=True), gr.update(visible=is_student), gr.update(visible=is_admin), "", welcome_msg, df
181
 
@@ -242,73 +241,75 @@ with gr.Blocks(title="模擬校園入口與資安防護系統", css=custom_css)
242
  logout_btn = gr.Button("登出", size="sm")
243
  gr.Markdown("---")
244
 
245
- with gr.Column(visible=False) as student_view:
246
- btn_switch_admin = gr.Button("🔄 快速切換為管理員 (Admin) 視角", size="sm", variant="secondary")
247
- with gr.Accordion("▼ 1. 教務系統", open=True):
248
- with gr.Row(): btn_eval = gr.Button("▶ 期末網路教學評量", size="sm"); btn_pre_select = gr.Button("▶ 期末網路預選系統", size="sm"); btn_add_drop = gr.Button("▶ 開學後加退選系統", size="sm")
249
- with gr.Row(): btn_istudy = gr.Button("▶ 北科i學園PLUS", size="sm"); btn_card = gr.Button("▶ 學生證掛失及補發系統", size="sm"); btn_course = gr.Button("▶ 課程系統", size="sm")
250
- with gr.Row(): btn_score = gr.Button("▶ 學業成績查詢系統", size="sm")
251
- with gr.Accordion("▼ 2. 學務系統", open=True):
252
- with gr.Row(): btn_leave = gr.Button("▶ 學生請假系統", size="sm"); btn_dorm = gr.Button("▶ 學生宿舍登錄(抽籤)系統", size="sm"); btn_scholarship = gr.Button("▶ 獎助學金申請系統", size="sm")
253
- with gr.Accordion("▼ 3. 資訊服務", open=True):
254
- with gr.Row(): btn_webmail = gr.Button("▶ 網路郵局 WebMail", size="sm"); btn_vdesk = gr.Button("▶ 北科軟體雲", size="sm")
255
- with gr.Accordion("▼ 惡意操作測試區", open=True):
256
- btn_hack_score = gr.Button("💀 嘗試偷改成績 (SQL Injection)", variant="stop", size="sm")
 
257
 
258
- with gr.Column(visible=False) as admin_view:
259
- btn_switch_student = gr.Button("🔄 快速切換為學生 (Student) 視角", size="sm", variant="secondary")
260
- with gr.Accordion("▼ 防護強度微調 (AI 敏感度)", open=True, elem_classes="weight-card"):
261
- gr.Markdown("<span style='color:gray; font-size:0.9em;'>手動輸入 0~100 的數值。提高強度會增加誤報率,但能更有效攔截新型攻擊。</span>")
262
- with gr.Row():
263
- overall_weight = gr.Number(label="總體防護強度 (%)", value=75, minimum=0, maximum=100)
264
- time_weight = gr.Number(label="時間特徵權重 (%)", value=80, minimum=0, maximum=100)
265
- with gr.Row():
266
- ip_weight = gr.Number(label="IP 聲譽權重 (%)", value=65, minimum=0, maximum=100)
267
- device_weight = gr.Number(label="裝置指紋/行為權重 (%)", value=45, minimum=0, maximum=100)
 
 
 
 
268
 
269
- with gr.Accordion("▼ HDBSCAN 異常辨識自動化測試區", open=True, elem_classes="weight-card"):
270
- gr.Markdown("<span style='color:gray; font-size:0.9em;'>點擊按鈕將直接以指定時間點執行該系統的操作,結果將顯示於左側 Log 區。</span>")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
271
 
272
- gr.Markdown("#### 1. 選課相關系統 (具備強烈季節性)")
273
- with gr.Row():
274
- t_pre_sel_norm = gr.Button("✅ 正常: 期末網路預選系統 (06-13 11:00)", size="sm")
275
- t_pre_sel_anom = gr.Button("⚠️ 異常: 期末網路預選系統 (10-15 13:00)", size="sm")
276
- with gr.Row():
277
- t_course_norm = gr.Button("✅ 正常: 課程系統 (02-25 07:50)", size="sm")
278
- t_course_anom = gr.Button("⚠️ 異常: 課程系統 (07-15 05:00)", size="sm")
279
- with gr.Row():
280
- t_add_drop_norm = gr.Button("✅ 正常: 開學後加退選系統 (02-18 14:00)", size="sm")
281
- t_add_drop_anom = gr.Button("⚠️ 異常: 開學後加退選系統 (05-15 12:00)", size="sm")
282
-
283
- gr.Markdown("#### 2. 期末與成績相關 (具備週期性)")
284
- with gr.Row():
285
- t_eval_norm = gr.Button("✅ 正常: 期末網路教學評量 (12-17 13:20)", size="sm")
286
- t_eval_anom = gr.Button("⚠️ 異常: 期末網路教學評量 (03-15 03:00)", size="sm")
287
- with gr.Row():
288
- t_score_norm = gr.Button("✅ 正常: 學業成績查詢系統 (11-17 18:50)", size="sm")
289
- t_score_anom = gr.Button("⚠️ 異常: 學業成績查詢系統 (02-15 03:00)", size="sm")
290
-
291
- gr.Markdown("#### 3. 校務與行政服務 (生活與常規)")
292
- with gr.Row():
293
- t_dorm_norm = gr.Button("✅ 正常: 學生宿舍登錄(抽籤)系統 (07-21 23:10)", size="sm")
294
- t_dorm_anom = gr.Button("⚠️ 異常: 學生宿舍登錄(抽籤)系統 (11-15 16:00)", size="sm")
295
- with gr.Row():
296
- t_schol_norm = gr.Button("✅ 正常: 獎助學金申請系統 (03-12 14:00)", size="sm")
297
- t_schol_anom = gr.Button("⚠️ 異常: 獎助學金申請系統 (08-15 02:00)", size="sm")
298
- with gr.Row():
299
- t_card_norm = gr.Button("✅ 正常: 學生證掛失 (10-23 13:40)", size="sm")
300
- t_card_anom = gr.Button("⚠️ 異常: 學生證掛失 (08-10 03:00)", size="sm")
301
-
302
- gr.Markdown("#### 4. 學習與日常輔助系統 (具備作息規律)")
303
- with gr.Row():
304
- t_istudy_norm = gr.Button("✅ 正常: 北科i學園PLUS (10-18 16:00)", size="sm")
305
- t_istudy_anom = gr.Button("⚠️ 異常: 北科i學園PLUS (08-20 02:00)", size="sm")
306
- with gr.Row():
307
- t_leave_norm = gr.Button("✅ 正常: 學生請假系統 (03-29 08:45)", size="sm")
308
- t_leave_anom = gr.Button("⚠️ 異常: 學生請假系統 (08-15 13:00)", size="sm")
309
- with gr.Row():
310
- t_vdesk_norm = gr.Button("✅ 正常: 軟體雲/WebMail (03-22 18:00)", size="sm")
311
- t_vdesk_anom = gr.Button("⚠️ 異常: 軟體雲/WebMail (08-10 00:00)", size="sm")
312
 
313
  # --- 綁定事件 ---
314
  time_mode.change(fn=toggle_time_input, inputs=time_mode, outputs=custom_time_input)
 
101
  return "⚠️異常_作息或行為不符"
102
 
103
  return "success"
 
104
  except ValueError:
 
105
  return "⚠️異常_未知特徵 (出現未授權的 IP 或操作)"
106
  except Exception as e:
 
107
  return f"⚠️系統錯誤: {str(e)}"
108
 
109
  def record_log(ip, cookie, account, role, action, status, time_mode, custom_time, ow, tw, iw, dw):
 
148
  df = record_log(ip, cookie, target_account, role, "login_attempt", "success", time_mode, custom_time, ow, tw, iw, dw)
149
 
150
  welcome_msg = f"**{name}** 歡迎您 | 線上人數: 938 (⚡快速登入模式)"
151
+ # 🌟 安全修正:強制轉型為布林值,確保 UI 狀態更新穩定
152
+ is_student = bool(role == "學生")
153
+ is_admin = bool(role == "管理")
154
 
155
  return gr.update(visible=False), gr.update(visible=True), gr.update(visible=is_student), gr.update(visible=is_admin), "", welcome_msg, df, target_account
156
 
 
172
  if USER_DB[account][0] == password:
173
  welcome_msg = f"**{name}** 歡迎您 | 線上人數: 938"
174
  df = record_log(ip, cookie, account, role, "login_attempt", "success", time_mode, custom_time, ow, tw, iw, dw)
175
+ # 🌟 安全修正:強制轉型為布林值
176
+ is_student = bool(role == "學生")
177
+ is_admin = bool(role == "管理")
178
 
179
  return gr.update(visible=False), gr.update(visible=True), gr.update(visible=is_student), gr.update(visible=is_admin), "", welcome_msg, df
180
 
 
241
  logout_btn = gr.Button("登出", size="sm")
242
  gr.Markdown("---")
243
 
244
+ # 🌟 關鍵修復:將 student_view 移出 dashboard_view 之外 (解除嵌套)
245
+ with gr.Column(visible=False) as student_view:
246
+ btn_switch_admin = gr.Button("🔄 快速切換為管理員 (Admin) 視角", size="sm", variant="secondary")
247
+ with gr.Accordion("▼ 1. 教務系統", open=True):
248
+ with gr.Row(): btn_eval = gr.Button("▶ 期末網路教學評量", size="sm"); btn_pre_select = gr.Button("▶ 期末網路預選系統", size="sm"); btn_add_drop = gr.Button("▶ 開學後加退選系統", size="sm")
249
+ with gr.Row(): btn_istudy = gr.Button("▶ 北科i學園PLUS", size="sm"); btn_card = gr.Button("▶ 學生證掛失及補發系統", size="sm"); btn_course = gr.Button("▶ 課程系統", size="sm")
250
+ with gr.Row(): btn_score = gr.Button("▶ 學業成績查詢系統", size="sm")
251
+ with gr.Accordion("▼ 2. 學務系統", open=True):
252
+ with gr.Row(): btn_leave = gr.Button("▶ 學生請假系統", size="sm"); btn_dorm = gr.Button("▶ 學生宿舍登錄(抽籤)系統", size="sm"); btn_scholarship = gr.Button("▶ 獎助學金申請系統", size="sm")
253
+ with gr.Accordion("▼ 3. 資訊服務", open=True):
254
+ with gr.Row(): btn_webmail = gr.Button("▶ 網路郵局 WebMail", size="sm"); btn_vdesk = gr.Button("▶ 北科軟體雲", size="sm")
255
+ with gr.Accordion("▼ 惡意操作測試區", open=True):
256
+ btn_hack_score = gr.Button("💀 嘗試偷改成績 (SQL Injection)", variant="stop", size="sm")
257
 
258
+ # 🌟 關鍵修復:將 admin_view 移出 dashboard_view 之外 (解除嵌套)
259
+ with gr.Column(visible=False) as admin_view:
260
+ btn_switch_student = gr.Button("🔄 快速切換為學生 (Student) 視角", size="sm", variant="secondary")
261
+ with gr.Accordion("▼ 防護強度微調 (AI 敏感度)", open=True, elem_classes="weight-card"):
262
+ gr.Markdown("<span style='color:gray; font-size:0.9em;'>手動輸入 0~100 的數值。提高強度會增加誤報率,但能更有效攔截新型攻擊。</span>")
263
+ with gr.Row():
264
+ overall_weight = gr.Number(label="總體防護強度 (%)", value=75, minimum=0, maximum=100)
265
+ time_weight = gr.Number(label="時間特徵權重 (%)", value=80, minimum=0, maximum=100)
266
+ with gr.Row():
267
+ ip_weight = gr.Number(label="IP 聲譽權重 (%)", value=65, minimum=0, maximum=100)
268
+ device_weight = gr.Number(label="裝置指紋/行為權重 (%)", value=45, minimum=0, maximum=100)
269
+
270
+ with gr.Accordion("▼ HDBSCAN 異常辨識自動化測試區", open=True, elem_classes="weight-card"):
271
+ gr.Markdown("<span style='color:gray; font-size:0.9em;'>點擊按鈕將直接以指定時間點執行該系統的操作,結果將顯示於左側 Log 區。</span>")
272
 
273
+ gr.Markdown("#### 1. 選課相關系統 (具備強烈季節性)")
274
+ with gr.Row():
275
+ t_pre_sel_norm = gr.Button("✅ 正常: 期末網路預選系統 (06-13 11:00)", size="sm")
276
+ t_pre_sel_anom = gr.Button("⚠️ 異常: 期末網路預選系統 (10-15 13:00)", size="sm")
277
+ with gr.Row():
278
+ t_course_norm = gr.Button("✅ 正常: 課程系統 (02-25 07:50)", size="sm")
279
+ t_course_anom = gr.Button("⚠️ 異常: 課程系統 (07-15 05:00)", size="sm")
280
+ with gr.Row():
281
+ t_add_drop_norm = gr.Button("✅ 正常: 開學後加退選系統 (02-18 14:00)", size="sm")
282
+ t_add_drop_anom = gr.Button("⚠️ 異常: 開學後加退選系統 (05-15 12:00)", size="sm")
283
+
284
+ gr.Markdown("#### 2. 期末與成績相關 (具備週期性)")
285
+ with gr.Row():
286
+ t_eval_norm = gr.Button("✅ 正常: 期末網路教學評量 (12-17 13:20)", size="sm")
287
+ t_eval_anom = gr.Button("⚠️ 異常: 期末網路教學評量 (03-15 03:00)", size="sm")
288
+ with gr.Row():
289
+ t_score_norm = gr.Button("✅ 正常: 學業成績查詢系統 (11-17 18:50)", size="sm")
290
+ t_score_anom = gr.Button("⚠️ 異常: 學業成績查詢系統 (02-15 03:00)", size="sm")
291
+
292
+ gr.Markdown("#### 3. 校務與行政服務 (生活與常規)")
293
+ with gr.Row():
294
+ t_dorm_norm = gr.Button("✅ 正常: 學生宿舍登錄(抽籤)系統 (07-21 23:10)", size="sm")
295
+ t_dorm_anom = gr.Button("⚠️ 異常: 學生宿舍登錄(抽籤)系統 (11-15 16:00)", size="sm")
296
+ with gr.Row():
297
+ t_schol_norm = gr.Button("✅ 正常: 獎助學金申請系統 (03-12 14:00)", size="sm")
298
+ t_schol_anom = gr.Button("⚠️ 異常: 獎助學金申請系統 (08-15 02:00)", size="sm")
299
+ with gr.Row():
300
+ t_card_norm = gr.Button("✅ 正常: 學生證掛失 (10-23 13:40)", size="sm")
301
+ t_card_anom = gr.Button("⚠️ 異常: 學生證掛失 (08-10 03:00)", size="sm")
302
 
303
+ gr.Markdown("#### 4. 學習與日常輔助系統 (具備作息規律)")
304
+ with gr.Row():
305
+ t_istudy_norm = gr.Button("✅ 正常: 北科i學園PLUS (10-18 16:00)", size="sm")
306
+ t_istudy_anom = gr.Button("⚠️ 異常: 北科i學園PLUS (08-20 02:00)", size="sm")
307
+ with gr.Row():
308
+ t_leave_norm = gr.Button("✅ 正常: 學生請假系統 (03-29 08:45)", size="sm")
309
+ t_leave_anom = gr.Button("⚠️ 異常: 學生請假系統 (08-15 13:00)", size="sm")
310
+ with gr.Row():
311
+ t_vdesk_norm = gr.Button("✅ 正常: 軟體雲/WebMail (03-22 18:00)", size="sm")
312
+ t_vdesk_anom = gr.Button("⚠️ 異常: 軟體雲/WebMail (08-10 00:00)", size="sm")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
313
 
314
  # --- 綁定事件 ---
315
  time_mode.change(fn=toggle_time_input, inputs=time_mode, outputs=custom_time_input)