Openresty增加waf配置

PS:
防止sql注入,本地包含,部分溢出,fuzzing测试,xss,SSRF等web攻击
防止svn/备份之类文件泄漏
防止ApacheBench之类压力测试工具的攻击
屏蔽常见的扫描黑客工具,扫描器
屏蔽异常的网络请求
屏蔽图片附件类目录php执行权限
防止webshell上传
1、git clone https://github.com/loveshell/ngx_lua_waf.git
2、下载解压后,将整 ngx_lua_waf 放到 nginx conf 目录中,并命名为 waf;
(PS:为了方面..)
3、在nginx.conf配置(根据自己的NGINX位置修改路径)
lua_package_path "/usr/local/nginx/conf/waf/?.lua";
lua_shared_dict limit 10m;
init_by_lua_file /usr/local/nginx/conf/waf/init.lua;
access_by_lua_file /usr/local/nginx/conf/waf/waf.lua;
4、配置config.lua
    RulePath = "/usr/local/nginx/conf/waf/wafconf/"
--规则存放目录

attacklog = "off"
--是否开启攻击信息记录,需要配置logdir

logdir = "/usr/local/nginx/logs/hack/"
--log存储目录,该目录需要用户自己新建,切需要nginx用户的可写权限

UrlDeny="on"
--是否拦截url访问

Redirect="on"
--是否拦截后重定向

CookieMatch = "on"
--是否拦截cookie攻击

postMatch = "on"
--是否拦截post攻击

whiteModule = "on"
--是否开启URL白名单

black_fileExt={"php","jsp"}
--填写不允许上传文件后缀类型

ipWhitelist={"127.0.0.1"}
--ip白名单,多个ip用逗号分隔

ipBlocklist={"1.0.0.1"}
--ip黑名单,多个ip用逗号分隔

CCDeny="on"
--是否开启拦截cc攻击(需要nginx.conf的http段增加lua_shared_dict limit 10m;)

CCrate = "100/60"
--设置cc攻击频率,单位为秒.
--默认1分钟同一个IP只能请求同一个地址100次

html=[[Please go away~~]]
--警告内容,可在中括号内自定义
5、修改init.lua say_html与denycc函数
 
function say_html()
if Redirect then
ngx.header.content_type = "text/html;charset=UTF-8"
ngx.status = ngx.HTTP_FORBIDDEN
ngx.say(html)
ngx.exit(ngx.status)
end
end


function denycc()
if CCDeny then
local uri=ngx.var.uri
CCcount=tonumber(string.match(CCrate,'(.*)/'))
CCseconds=tonumber(string.match(CCrate,'/(.*)'))
local token = getClientIp()..uri
local limit = ngx.shared.limit
local req,_=limit:get(token)
if req then
if req > CCcount then
ngx.header.content_type = "application/json;charset=UTF-8"
local ret={returncode='22000',messages='请求拒绝,请稍后再试',body={}}
--ngx.header['Content-Type']="text/html;charset=UTF-8"
ngx.say(json.encode(ret))
ngx.exit(200)
return true
else
limit:incr(token,1)
end
else
limit:set(token,1,CCseconds)
end
end
return false
end
6、nginx -s reload 并测试
PS(http://ip/index?id=../etc/passwd)
7、记得给/usr/local/nginx/logs/hack文件夹授权 chown www:www -R hack/
已邀请:

qk356 - IT男有人权吗?

赞同来自:

#git clone https://github.com/unixhot/waf.git
#cp -a ./waf/waf /usr/local/openresty/nginx/conf/

修改Nginx的配置文件,加入以下配置。注意路径,同时WAF日志默认存放在/tmp/日期_waf.log
#WAF
lua_shared_dict limit 50m;
lua_package_path "/usr/local/openresty/nginx/conf/waf/?.lua";
init_by_lua_file "/usr/local/openresty/nginx/conf/waf/init.lua";
access_by_lua_file "/usr/local/openresty/nginx/conf/waf/access.lua";

要回复问题请先登录注册