ngx_lua_waf 安装说明文档

作者github地址: https://github.com/loveshell/ngx_lua_waf
----------------------------------------------------------------------------------------------------------------------------------------------------------
转自作者说明文档:


ngx_lua_waf是我一个基于ngx_lua的web应用防火墙。
代码很简单,开发初衷主要是使用简单,高性能和轻量级。
现在开源出来.其中包含我们的过滤规则。如果大家有什么建议和想fa,欢迎和我一起完善。


用途:
用于过滤post,get,cookie方式常见的web攻击
防止sql注入,本地包含,部分溢出,fuzzing测试,xss,SSRF等web攻击
防止svn/备份之类文件泄漏
防止ApacheBench之类压力测试工具的攻击
屏蔽常见的扫描黑客工具,扫描器
屏蔽异常的网络请求
屏蔽图片附件类目录php执行权限
防止webshell上传

推荐安装:
请自行给nginx安装ngx_lua模块,推荐lujit2.0做lua支持
请提前新建/data/logs/hack/目录攻击日志,并赋予nginx用户对该目录的写入权限。

配置文件添加:
在http段添加
lua_need_request_body on;(开启post请求)
access_by_lua_file /usr/local/nginx/conf/waf.lua;

规则更新:
考虑到正则的缓存问题,动态规则会影响性能,所以暂没用共享内存字典和redis之类东西做动态管理。
规则更新可以把规则文件放置到其他服务器,通过crontab任务定时下载来更新规则,nginx reload即可生效。以保障ngx lua waf的高性能
只记录过滤日志,不开启过滤,在代码里在check前面加上--注释即可,如果需要过滤,反之

一些说明:
过滤规则在wafconf下,可根据需求自行调整,每条规则需换行,或者用|分割
    global是全局过滤文件,里面的规则对post和get都过滤
    get是只在get请求过滤的规则
    post是只在post请求过滤的规则
    whitelist是白名单,里面的url匹配到不做过滤
    user-agent是对user-agent的过滤规则

默认开启了get和post过滤,需要开启cookie过滤的,编辑waf.lua取消部分--注释即可
日志文件名称格式如下:虚拟主机名_sec.log
欢迎大家到http://bbs.linuxtone.org多多交流
weibo: [@ppla](http://weibo.com/opscode)
感谢ngx_lua模块的开发者[@agentzh](https://github.com/agentzh/),春哥是我见过开源精神最好的人之一

本文记录如何安装ngx_lua模块
nginx_lua_module是由淘宝的工程师清无(王晓哲)和春来(章亦春)所开发的nginx第三方模块,它能将lua语言嵌入到nginx配置中,从而使用lua就极大增强了nginx的能力
http://wiki.nginx.org/HttpLuaModule


----------------------------------------------------------------------------------------------------------------------------------------------------------


正文:
1 下载luajit 2.0并安装
http://luajit.org/download.html
直接使用源码make && make install
所以lib和include是直接放在/usr/local/lib和usr/local/include


2 下载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.gz


3  下载ngx_devel_kit解压

https://github.com/simpl/ngx_devel_kit/tags 

 wget https://github.com/simpl/ngx_devel_kit/archive/v0.2.18.tar.gz --no-check-certificate 

 tar -zxvf  v0.2.18




4  下载nginx_lua_module解压

https://github.com/chaoslawful/lua-nginx-module/tags 

 wget https://github.com/chaoslawful/lua-nginx-module/archive/v0.7.18rc2.tar.gz --no-check-certificate 

 tar -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-fpm 

 chown -R www:www /data/logs/hack/ 

 chmod -R 755 /data/logs/hack/




8 安装ngx_lua_waf模块
wget https://github.com/loveshell/ngx_lua_waf/archive/master.zip --no-check-certificate
把这个文件解压到
nginx的conf目录下.
然后在nginx.conf里的http配置里添加
http {

lua_need_request_body on; 

     access_by_lua_file /usr/local/nginx-help/conf/waf.lua; 

 注意:waf.lua一定要放在/usr/local/nginx-help/conf/waf.lua 已经他的子目录.否则会报500错误. 


 最后用/usr/local/nginx-help/sbin/nginx -V和/usr/local/nginx-help/sbin/nginx -t 验证一下 

 [root@platinum conf]# /usr/local/nginx-help/sbin/nginx -V 

 nginx version: nginx/1.2.7 

 built by gcc 4.1.2 20080704 (Red Hat 4.1.2-48) 

 configure arguments: --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,/usr/local/lib



[root@platinum conf]# /usr/local/nginx-help/sbin/nginx -t 

 nginx: the configuration file /usr/local/nginx-help/conf/nginx.conf syntax is ok 

 nginx: configuration file /usr/local/nginx-help/conf/nginx.conf test is successful



9 过滤配置说明:
过滤规则在wafconf下,可根据需求自行调整,每条规则需换行,或者用|分割
    global是全局过滤文件,里面的规则对post和get都过滤
    get是只在get请求过滤的规则
    post是只在post请求过滤的规则
    whitelist是白名单,里面的url匹配到不做过滤
    user-agent是对user-agent的过滤规则

为了不返回一些无用给用户直接把注入测试防护返回信息改为http 403状态,修改/usr/local/nginx-help/conf/waf.lua

71 function check() 

  72     ngx.header.content_type = "text/html" 

  73     --ngx.print("403") 

  74     ngx.exit(403) 

  75 end



注意:每次更改waf.lua代码需要把nginx reload一下!

感谢我的同事 @EasonChiang_63446 (新浪微博)做了详细的测试及编写这个文档