4

Nginx 的 WAF 规则 LuaJIT 中危险版

 9 months ago
source link: https://dusays.com/658/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

前段时间杜老师发表了一篇《自建 CDN 服务器思路》其中介绍南墙 Web 应用防火墙,有些小伙伴们对其名字很感兴趣,而杜老师注意到了它的防护规则,感觉非常实用,顺手整理了下,分享给需要的小伙伴们。此篇为中危险防护规则!

机器人攻击防护

过滤阶段:请求阶段

规则描述:通过生成滑动旋转验证码来拦截机器人攻击,如漏洞扫描、网络爬虫、CC攻击等自动化攻击行为,Token有效期30分钟。

规则内容:

local sh = ngx.shared.ipCache
local robotIp = 'rb:' .. waf.ip
local c, f = sh:get(robotIp)

-- 如果是静态页面且没有进行滑动旋转验证码验证则返回
if not (waf.isQueryString or waf.reqContentLength > 0) and f ~= 2 then
return false
end

if not c then
sh:set(robotIp, 1, 60, 1) -- 设置1分钟也就是60秒访问计数时间段
else
if f == 2 then
return waf.checkRobot(waf) -- 启动机器人滑动旋转验证码验证
end
sh:incr(robotIp, 1)
if c + 1 >= 360 then
sh:set(robotIp, c + 1, 1800, 2) -- 达到了60秒内请求超过360次的阈值,进入机器人验证模式
return true, robotIp, true
end
end

return false

弱口令检测

过滤阶段:请求阶段

规则描述:检测常见登录页面的弱口令问题

规则内容:

local check = waf.plugins.weakPwdDetection.check
local toLower = waf.toLower
local has = waf.contains

local form = waf.form
local uri = toLower(waf.uri)
if form and (has(uri, "login") or has(uri, "logon") or has(uri, "signin")) then
local f = form["FORM"]
if f then
for k, v in pairs(f) do
k = toLower(k)
if (k == "pass" or has(k, "pwd") or has(k, "passwd") or has(k, "password")) and check(v) then
return true, form["RAW"], false
end
end
end
end

return false

敏感文件泄露检测

过滤阶段:请求阶段

规则描述:检测url中各种敏感泄露文件的路径,如svn、git、sql、log、bak等,防止被攻击者利用

规则内容:

local m, d = waf.plugins.fileLeakDetection.check()
if m then
return true, d, true
end
return false

请求body大小限制

过滤阶段:请求阶段

规则描述:限制请求body大小为8M以下,黑客会尝试大数据包绕过waf过滤

规则内容:

if waf.reqContentLength>8388608 then
return true,"reqBody length is "..waf.reqContentLength ,true
end
return false

HTTP Parameter Pollution

过滤阶段:请求阶段

规则描述:http参数污染攻击,该规则查找具有相同名称的多个参数,并检查一些后端参数弱校验时产生的绕过问题,如:foo[1]a=bar&foo[1]b=或foo[1]x[1]=bar&foo[1]x[2]=等。

规则内容:

local rgx = waf.rgxMatch

local function rMatch(v)
local m = rgx(v, "(?:][^\\]]+$|][^\\]]+\\[)", "jos")
if m then
return m, v
end
return false
end

local form = waf.form
if form then
for k, v in pairs(form["FORM"]) do
if type(v) == "table" then
return true, k.."="..table.concat(v, ","), true
end
local m, d = rMatch(k)
if m then
return m, d, true
end
end
end

local queryString = waf.queryString
if queryString then
for k, v in pairs(queryString) do
if type(v) == "table" then
return true, k.."="..table.concat(v, ","), true
end
local m, d = rMatch(k)
if m then
return m, d, true
end
end
end

local cookies = waf.cookies
if cookies then
for k, v in pairs(cookies) do
if type(v) == "table" then
return true, k.."="..table.concat(v, ","), true
end
local m, d = rMatch(k)
if m then
return m, d, true
end
end
end
return false

扫描器检测

过滤阶段:请求阶段

规则描述:检测常见的各种扫描器,如awvs、sqlmap、nessus、appscan、nmap等,拦截它们有助于减少黑客发现漏洞的风险

规则内容:

local m, d = waf.plugins.scannerDetection.check()
if m then
return true, d, true
end
return false

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK