Merge pull request #581 from samwafgo/feat_rule_issafebot

feat:add rule safe bot
This commit is contained in:
samwafgo
2025-12-05 15:53:36 +08:00
committed by GitHub
2 changed files with 15 additions and 4 deletions

View File

@@ -98,6 +98,12 @@ func (WebLog) TableName() string {
return "web_logs"
}
// IsSafeBot 判断是否是安全bot
// 返回: true表示是安全bot是bot且风险等级为0false表示不是
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
}

View File

@@ -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)