Create brute-force-login-prevention.lua

用于增强登录页面安全性的规则
pull/102/head
大力丸666 2024-12-20 19:44:05 +08:00 committed by GitHub
parent d79535e1b4
commit 14379e17d1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 39 additions and 0 deletions

View File

@ -0,0 +1,39 @@
--[[
:
:
:
: URL5(300)10IP 144024
--]]
local sh = waf.ipCache
local bruteForceKey = 'brute-force-login:' .. waf.ip -- 使用独立前缀标识,避免与其他规则冲突
-- 定义特征路径关键词列表
local targetPaths = { "login", "signin", "signup", "register", "reset", "passwd", "account", "user" }
-- 判断URI是否包含特征关键词
if not waf.pmMatch(waf.toLower(waf.uri), targetPaths) then
return false -- 如果路径中不包含任何特征关键词,则跳过检测
end
-- 获取缓存中的数据
local requestCount, flag = sh:get(bruteForceKey)
if not requestCount then
-- 初始化计数设置5分钟300秒的时间窗口
sh:set(bruteForceKey, 1, 300, 1)
else
-- 如果标志已经为2则IP处于封禁状态直接拦截
if flag == 2 then
return waf.block(true) -- 阻断请求返回403响应
end
-- 增加非法请求次数
sh:incr(bruteForceKey, 1)
if requestCount + 1 > 10 then
-- 达到爆破攻击检测阈值标记为封禁状态封禁时间为1440分钟24小时
sh:set(bruteForceKey, requestCount + 1, 86400, 2)
return true, "检测到登录接口发生爆破攻击已封禁IP", true -- 日志载荷改为中文
end
end
return false