参考了一下链接的内容:
让 Nginx 支持 WAF 防护功能实战 (安装使用)
作者github地址(源码)
通过nginx配置文件抵御攻击(原理)
一、安装
1 下载luajit 2.0并安装http://luajit.org/download.html直接使用源码make && make install所以lib和include是直接放在/usr/local/lib和usr/local/include2 下载nginx源码解压wget http://nginx.org/download/nginx-1.2.7.tar.gz注意版本号,如果机子上已经装了nginx,不想升级的话,请使用/to/nginx/sbin/nginx -v 来查看版本号tar -zxvf nginx-1.2.7.tar.gz3 下载ngx_devel_kit解压https://github.com/simpl/ngx_devel_kit/tagswget https://github.com/simpl/ngx_devel_kit/archive/v0.2.18.tar.gz --no-check-certificatetar -zxvf v0.2.18 4 下载nginx_lua_module解压https://github.com/chaoslawful/lua-nginx-module/tagswget https://github.com/chaoslawful/lua-nginx-module/archive/v0.7.18rc2.tar.gz --no-check-certificatetar -zxvf v0.7.18rc2 5 进入nginx源码文件夹cd nginx-1.2.7/6 导入环境变量,编译export LUAJIT_LIB=/usr/local/lib #这个很有可能不一样 export LUAJIT_INC=/usr/local/include/luajit-2.0 #这个很有可能不一样./configure --prefix=/opt/nginx \ #nginx的安装路径 --add-module=/path/to/ngx_devel_kit \ #ngx_devel_kit 的源码路径 --add-module=/path/to/lua-nginx-module #nginx_lua_module 的源码路径例子:./configure --prefix=/usr/local/nginx-help --add-module=/root/jiangbin/ngx_devel_kit-0.2.18 --add-module=/root/jiangbin/lua-nginx-module-0.7.18rc2 --with-ld-opt="-Wl,-rpath,$LUAJIT_LIB"make -j2 make install安装lua模块发现的问题:我在编译安装 Nginx 的第三方模块时,碰到一个错误:[plain] view plaincopyprint?/usr/local/nginx/sbin/ngxin -s reload /usr/local/nginx/sbin/nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory 百事不得其解,后来Google之,发现了解决办法。在 Nginx 编译时,需要指定 RPATH,加入下面选项即可:[plain] view plaincopyprint?./configure --with-ld-opt="-Wl,-rpath,$LUAJIT_LIB"或者export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH
7 请提前新建/data/logs/hack/目录攻击日志,并赋予nginx用户对该目录的写入权限。mkdir -p /data/logs/hack/www账户是跑nginx和php-fpmchown -R www:www /data/logs/hack/chmod -R 755 /data/logs/hack/
该目录是lua日志目录,可以自定义配置路径,查看源码下config.lua文件
attacklog = "on"logdir = "/data/logs/hack/"
8 安装ngx_lua_waf模块
git clone 'https://github.com/loveshell/ngx_lua_waf.git'll ngx_lua_waf/
-rw-r--r-- 1 root root 288 Apr 22 15:51 config.lua
-rw-r--r-- 1 root root 5705 Apr 22 15:51 init.lua-rw-r--r-- 1 root root 1587 Apr 22 15:51 install.sh-rw-r--r-- 1 root root 4547 Apr 22 15:51 README.mddrwxr-xr-x 2 root root 4096 Apr 22 15:51 wafconf-rw-r--r-- 1 root root 1721 Apr 22 15:51 waf.luamv ngx_lua_waf /opt/nginx/etc/waf
在nginx中引入ngx_lua_waf模块
#lua_code_cache off; #是否开启cache,off:修改.lua文件不需要reload nginx lua_need_request_body on;
lua_package_path "/opt/nginx/etc/waf/?.lua"; lua_shared_dict limit 20m; init_by_lua_file "/opt/nginx/etc/waf/init.lua"; access_by_lua_file /opt/nginx/etc/waf/waf.lua;9 使用
server {
listen 80; server_name nginxlua.com; error_log /var/nginx/logs/debug_preview.log debug; location = /hello.html{default_type 'text/plain';
content_by_lua 'ngx.say("hello,nginx_lua!")';
} }测试一下有没有500 如果有一般就是ngx_lua_waf模块目录不对。
10 进阶
模块的逻辑关系都在waf.lua文件里面,通过调用init.lua中定义的函数实现各个不同的功能。白名单,黑名单,cc攻击,whiteurl,useragent,url,参数,cookie,post参数过滤等。
再次我们需要根据业务来适当修改添加某些功能。添加的函数都放在init.lua文件里面,在waf.lua中应用。
--添加单纯的对于ip并发的限制
config.lua 中设置CCiprate = "10/60"
每分钟10个。可设置。
适用于:类似爬虫类的高并发cc
function denyipcc()
if CCDeny then CCipcount=tonumber(string.match(CCiprate,'(.*)/')) CCipseconds=tonumber(string.match(CCiprate,'/(.*)')) local token = getClientIp() local limit = ngx.shared.limit local req,_=limit:get(token) if req then if req > CCipcount then local rule="ip-cc-attrack" log('GET',ngx.var.request_uri,"-",rule) ngx.exit(503) return true else limit:incr(token,1) end else limit:set(token,1,CCipseconds) end end return falseend --重新改写denycc()函数适用于:
function denycookiecc()
if CCDeny then local ck = ngx.var.http_cookie CCcookiecount=tonumber(string.match(CCcookierate,'(.*)/')) CCcookieseconds=tonumber(string.match(CCcookierate,'/(.*)')) if ck then local token = string.match(ck,'_session_id=(.*)') local limit = ngx.shared.limit local req,_=limit:get(token) if req then if req > CCcookiecount then local rule="cookie-cc-attrack" log('GET',ngx.var.request_uri,"-",rule) ngx.exit(503) return true else limit:incr(token,1) end else limit:set(token,1,CCcookieseconds) end else denycc() end end return falseend根据是否包含cookie来处理,很多程序或者说肉鸡发起cc攻击时不包含cookie可以将这部分流量分离出来,进入更加严格的函数denycc(),设置的频率同样通过config.lua来读取。CCrate = "10/60"
CCcookierate = "15/60"--添加日志标记
是由哪个函数处理的,添加标志,方便以后的日志分析。
local rule="cookie-cc-attrack"
log('GET',ngx.var.request_uri,"-",rule)更多功能。可以自己动手尝试。可以开启nginx的debug日志,方便调试,前提是编译了nginx debug module。一些lua的语法,不懂的自查下,脚本语言学起来还是不太费力的。
本文出自 “” 博客,请务必保留此出处