From 44851da0da85f77772e6fe147cc523a6b3148d90 Mon Sep 17 00:00:00 2001 From: samwaf Date: Fri, 5 Dec 2025 15:52:58 +0800 Subject: [PATCH] feat:add rule safe bot #580 --- innerbean/web_log.go | 10 ++++++++-- wafenginecore/checkbot.go | 9 +++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/innerbean/web_log.go b/innerbean/web_log.go index d0d7dca..ebe2c21 100644 --- a/innerbean/web_log.go +++ b/innerbean/web_log.go @@ -98,6 +98,12 @@ func (WebLog) TableName() string { return "web_logs" } +// IsSafeBot 判断是否是安全bot +// 返回: true表示是安全bot(是bot且风险等级为0),false表示不是 +func (w *WebLog) IsSafeBot() bool { + return w.IsBot == 1 && w.RISK_LEVEL == 0 +} + // GetIPFailureCount 获取IP在指定时间窗口内的失败次数(用于规则引擎) // minutes: 时间窗口(分钟) // 返回: 失败次数 @@ -107,7 +113,7 @@ func (w *WebLog) GetIPFailureCount(minutes int64) int64 { } // 如果是bot且危险程度是0,不统计失败次数 - if w.IsBot == 1 && w.RISK_LEVEL == 0 { + if w.IsSafeBot() { return 0 } @@ -132,7 +138,7 @@ func (w *WebLog) RecordIPFailureThreshold(minutes int64, count int64) { } // 如果是bot且危险程度是0,不记录阈值 - if w.IsBot == 1 && w.RISK_LEVEL == 0 { + if w.IsSafeBot() { return } diff --git a/wafenginecore/checkbot.go b/wafenginecore/checkbot.go index ea1b295..a4f6248 100644 --- a/wafenginecore/checkbot.go +++ b/wafenginecore/checkbot.go @@ -53,13 +53,18 @@ func (waf *WafEngine) CheckBot(r *http.Request, weblogbean *innerbean.WebLog, fo result.Title = botResult.BotName result.Content = "请正确访问" - if !isBotCacheExist { - //如果是bot 加入cache里面 + if !isBotCacheExist && botResult.BotName != "查询超时" && botResult.BotName != "查询失败" { + //如果是bot 加入cache里面(排除查询超时和查询失败的情况) global.GCACHE_WAFCACHE.SetWithTTl(enums.CACHE_DNS_BOT_IP+weblogbean.SRC_IP, botResult, time.Duration(global.GCONFIG_RECORD_DNS_BOT_EXPIRE_HOURS)*time.Hour) } return result } + if !isBotCacheExist && botResult.BotName != "查询超时" && botResult.BotName != "查询失败" { + //如果是正常爬虫,也保存结果(排除查询超时和查询失败的情况) + global.GCACHE_WAFCACHE.SetWithTTl(enums.CACHE_DNS_BOT_IP+weblogbean.SRC_IP, botResult, time.Duration(global.GCONFIG_RECORD_DNS_BOT_EXPIRE_HOURS)*time.Hour) + } + } else { //如果不是bot 加入到正常cache里面 global.GCACHE_WAFCACHE.SetWithTTl(enums.CACHE_DNS_NORMAL_IP+weblogbean.SRC_IP, weblogbean.SRC_IP, time.Duration(global.GCONFIG_RECORD_DNS_NORMAL_EXPIRE_HOURS)*time.Hour)