| 類別 | 傳統防禦 | 自學防禦 |
|---|---|---|
| 規則設定 | 人工設置、手動更新 | 模型自動調整 |
| 閾值變化 | 固定值(如 score > 0.8 判定高風險) | 動態變化(根據近期誤判率調整) |
| 學習資料 | 定期 retrain | 持續流入、即時微調 |
| 防禦行為 | 被動攔截 | 主動預測與調整防線 |
AI 模型在實際運行中會隨著資料分布改變而出現漂移(Concept Drift)。
我們可透過監控模型的 Precision(精確率)、Recall(召回率) 與 False Positive Rate(誤報率),動態調整決策閾值:
# dynamic_threshold.py
import pandas as pd
# 假設 daily_metrics.csv 記錄每日模型表現
# columns: date, precision, recall, fpr, threshold
df = pd.read_csv("metrics/daily_metrics.csv")
latest = df.iloc[-1]
precision, recall, fpr, threshold = latest["precision"], latest["recall"], latest["fpr"], latest["threshold"]
# 動態調整規則
if fpr > 0.05: # 誤報高 → 提高閾值(放寬)
new_threshold = min(threshold + 0.02, 0.95)
elif recall < 0.85: # 漏報多 → 降低閾值(嚴格)
new_threshold = max(threshold - 0.02, 0.50)
else:
new_threshold = threshold # 維持
print(f"Adjusted threshold: {new_threshold:.2f}")
df.loc[len(df)] = [pd.Timestamp.now(), precision, recall, fpr, new_threshold]
df.to_csv("metrics/daily_metrics.csv", index=False)
結果:模型閾值可自動根據表現微調,避免長期偏移導致過度封鎖或放行。
四、模型自動微調(Online Learning)
4.1 增量學習範例(使用 SGDClassifier)
當每日有新標註資料產生(例如人工審核或自動封鎖結果),可進行「增量訓練(Incremental Training)」:
python
複製程式碼
# online_learning.py
import joblib
import pandas as pd
from sklearn.linear_model import SGDClassifier
from sklearn.feature_extraction.text import TfidfVectorizer
# 載入先前模型與向量器
clf = joblib.load("phishing_model.pkl")
vec = joblib.load("vectorizer.pkl")
# 新資料(例如前一日封鎖記錄)
df = pd.read_csv("data/new_feedback.csv")
X_new = vec.transform(df["text"])
y_new = df["label"]
# 增量更新模型
clf.partial_fit(X_new, y_new)
joblib.dump(clf, "phishing_model.pkl")
print("Model incrementally updated.")
這樣可實現 每日自我更新,不必重新訓練整個模型。
五、行為導向的防禦調整
5.1 概念
系統不僅根據統計數據調整閾值,也能根據「攻擊者行為」自動修正規則。例如:
攻擊行為 系統反應
同一 IP 在 5 分鐘內嘗試多個短網址 自動啟動封鎖延遲機制(增加 rate-limit)
同一 domain 在 24 小時內多次被標為高風險 自動加入灰名單(暫時降信任值)
短網址被解展後 domain 為新註冊 (<30 天) 自動降低判定閾值,增加警示權重
使用者回報「誤判」次數上升 自動提升判定閾值,放寬過濾標準
5.2 實作範例
adaptive_rules.py
python
複製程式碼
import pandas as pd
from datetime import datetime, timedelta
events = pd.read_csv("data/events_with_patterns.csv", parse_dates=["timestamp"])
# 偵測高頻行為
window = datetime.utcnow() - timedelta(minutes=5)
recent = events[events["timestamp"] > window]
ip_counts = recent["client_ip_hash"].value_counts()
suspicious_ips = ip_counts[ip_counts > 20].index.tolist()
for ip in suspicious_ips:
print(f"IP {ip} exceeded rate threshold, temporarily restricted.")
接著自動觸發:
bash
複製程式碼
echo "add deny $ip" >> /etc/nginx/deny_rules.conf && nginx -s reload
六、AI 策略模組整合流程
6.1 模組運作架構
css
複製程式碼
[防禦日誌 & 模型指標]
↓
[動態閾值調整器]
↓
[增量學習器 (Online Trainer)]
↓
[行為調整器 (Adaptive Rule Engine)]
↓
[防火牆 / API 更新]
6.2 定期自動執行
設定每日凌晨自動運行:
bash
複製程式碼
0 2 * * * /usr/bin/python3 /app/dynamic_threshold.py
15 2 * * * /usr/bin/python3 /app/online_learning.py
30 2 * * * /usr/bin/python3 /app/adaptive_rules.py
七、成效評估
指標 意義 觀察目標
Precision 正確判為惡意的比例 > 90%
Recall 成功攔截惡意的比例 > 85%
FPR(誤報率) 被誤封的比例 < 5%
MTTR(平均反應時間) 從發現攻擊到更新防線 < 10 分鐘
成功的自學系統應能在長期運行中自動維持高 Precision / Recall 並減少人工干預次數。
八、挑戰與風險
資料偏移(Data Drift):需持續監測樣本分布變化,避免模型過時。
對抗樣本(Adversarial Samples):駭客可能利用模型的自學規則反制,需加入隨機性或混淆處理。
資源管理:頻繁 retrain 可能耗費記憶體與 CPU,需以批次或分層更新方式執行。
誤判回滾:若自動調整造成封鎖過多,需設置自動回滾機制以還原閾值。