PhilipL commited on
Commit
c74a081
·
verified ·
1 Parent(s): c865bab

record frequency of single btn

Browse files
Files changed (1) hide show
  1. app.py +17 -11
app.py CHANGED
@@ -252,7 +252,6 @@ def explain_abnormal_log(log_text):
252
  except Exception as e: return f"⚠️ 呼叫 API 發生錯誤: {str(e)}"
253
 
254
  # --- 2. 核心邏輯與真實 HDBSCAN 異常偵測 ---
255
- # 🌟 修改:加入 account 參數,為了精準計算該帳號的頻率
256
  def check_anomaly(ip, account, action, log_time, ow, tw, iw, dw, gw, fw):
257
  if not AI_READY: return "success"
258
  if action in ["login_attempt", "logout"]: return "success"
@@ -261,28 +260,36 @@ def check_anomaly(ip, account, action, log_time, ow, tw, iw, dw, gw, fw):
261
  ow, tw, iw, dw = float(ow), float(tw), float(iw), float(dw)
262
  gw, fw = float(gw), float(fw)
263
 
264
- # --- 🌟 新增:第一道防線 (Rule-based 頻率前置檢查) ---
265
  current_dt = pd.to_datetime(str(log_time))
266
  cutoff_dt = current_dt - pd.Timedelta(seconds=60) # 往前回溯 60 秒
267
 
268
  recent_clicks = 0
269
  for log in log_history:
270
  log_dt = pd.to_datetime(log['timestamp'])
271
- # 檢查這筆舊紀錄是否在 60 秒內
272
  if log_dt >= cutoff_dt:
273
- # 檢查是不是同一個帳號的動作
274
- if log.get('account') == account:
275
  recent_clicks += 1
276
  else:
277
- # 因為 log_history 是最新的在最前面,遇到超過 60 秒的就可以提早結束,節省資源
278
  break
279
 
280
- # 依據行為頻率敏感度(fw 0~100) 計算最高容忍次數 (計算結果介於 3 到 20 之間)
281
- max_allowed_clicks = max(3, int(20 - (fw / 100.0) * 17))
282
-
 
 
 
 
 
 
 
 
 
 
283
  # 觸發條件:若包含這一次的點擊,超過了容忍次數,直接攔截!
284
  if (recent_clicks + 1) > max_allowed_clicks:
285
- return f"⚠️異常_操作頻率過高 ({recent_clicks+1}次/分)"
286
 
287
  # --- 第二道防線 (HDBSCAN AI 空間異常判斷) 繼續執行 ---
288
  ip_code = float(get_ip_category(ip))
@@ -343,7 +350,6 @@ def check_anomaly(ip, account, action, log_time, ow, tw, iw, dw, gw, fw):
343
  except Exception as e:
344
  return f"⚠️系統錯誤: {str(e)}"
345
 
346
- # 🌟 修改:將 account 傳入 check_anomaly
347
  def record_log(ip, cookie, account, role, action, status, time_mode, custom_time, ow, tw, iw, dw, gw, fw):
348
  global baseline_X
349
  if time_mode == "自訂時間 (模擬過去/未來)" and custom_time and str(custom_time).strip() != "": log_time = str(custom_time)
 
252
  except Exception as e: return f"⚠️ 呼叫 API 發生錯誤: {str(e)}"
253
 
254
  # --- 2. 核心邏輯與真實 HDBSCAN 異常偵測 ---
 
255
  def check_anomaly(ip, account, action, log_time, ow, tw, iw, dw, gw, fw):
256
  if not AI_READY: return "success"
257
  if action in ["login_attempt", "logout"]: return "success"
 
260
  ow, tw, iw, dw = float(ow), float(tw), float(iw), float(dw)
261
  gw, fw = float(gw), float(fw)
262
 
263
+ # --- 🌟 修改:第一道防線 (限縮為單一按鈕判斷,並套用精確的閾值) ---
264
  current_dt = pd.to_datetime(str(log_time))
265
  cutoff_dt = current_dt - pd.Timedelta(seconds=60) # 往前回溯 60 秒
266
 
267
  recent_clicks = 0
268
  for log in log_history:
269
  log_dt = pd.to_datetime(log['timestamp'])
 
270
  if log_dt >= cutoff_dt:
271
+ # 🌟 修改:同時檢查「同一個帳號」且「同一個動作 (按鈕)」
272
+ if log.get('account') == account and log.get('action') == action:
273
  recent_clicks += 1
274
  else:
 
275
  break
276
 
277
+ # 🌟 修改:依據你要求的階梯式敏感度設定最高容忍次數
278
+ if fw <= 0: max_allowed_clicks = 100
279
+ elif fw <= 10: max_allowed_clicks = 90
280
+ elif fw <= 20: max_allowed_clicks = 80
281
+ elif fw <= 30: max_allowed_clicks = 70
282
+ elif fw <= 40: max_allowed_clicks = 60
283
+ elif fw <= 50: max_allowed_clicks = 50
284
+ elif fw <= 60: max_allowed_clicks = 40
285
+ elif fw <= 70: max_allowed_clicks = 30
286
+ elif fw <= 80: max_allowed_clicks = 15
287
+ elif fw <= 90: max_allowed_clicks = 9
288
+ else: max_allowed_clicks = 3
289
+
290
  # 觸發條件:若包含這一次的點擊,超過了容忍次數,直接攔截!
291
  if (recent_clicks + 1) > max_allowed_clicks:
292
+ return f"⚠️異常_單一操作頻率過高 ({recent_clicks+1}次/分)"
293
 
294
  # --- 第二道防線 (HDBSCAN AI 空間異常判斷) 繼續執行 ---
295
  ip_code = float(get_ip_category(ip))
 
350
  except Exception as e:
351
  return f"⚠️系統錯誤: {str(e)}"
352
 
 
353
  def record_log(ip, cookie, account, role, action, status, time_mode, custom_time, ow, tw, iw, dw, gw, fw):
354
  global baseline_X
355
  if time_mode == "自訂時間 (模擬過去/未來)" and custom_time and str(custom_time).strip() != "": log_time = str(custom_time)