[Python][資安]偵測短時間內來自同一 IP 的大量請求

文、意如

from collections import Counter

# 模擬日誌 (IP: '203.0.113.42' 請求次數異常高)
LOGS = [
    "192.168.1.10", "203.0.113.42", "192.168.1.10", "192.68.5.5", 
    "203.0.113.42", "192.68.5.5", "203.0.113.42", "203.0.113.42",
    "203.0.113.42", "203.0.113.42", "192.168.1.10", "192.68.5.5"
]

THRESHOLD = 4  # 設定爬蟲警報閾值

print("---啟動---")

#程式碼計算所有 IP 的請求次數
ip_counts = Counter(LOGS)

# 檢查結果並發出警報
for ip, count in ip_counts.items():
    if count >= THRESHOLD:
        print("發現可疑 IP!")
        print(f"  IP: {ip}, 請求數: {count} (超過閾值 {THRESHOLD})")

print("---日誌分析完成---")

執行結果:

Yiru@Studio - 關於我 - 意如