http://blog.chinaunix.net/uid-1728743-id-3546152.html
https://github.com/loveshell/ngx_lua_waf
ngx_lua_waf 安装说明文档
作者github地址:
https://github.com/loveshell/ngx_lua_waf
转自作者说明文档:
ngx_lua_waf是我刚入职趣游时候开发的一个基于ngx_lua的web应用防火墙。
代码很简单,开发初衷主要是使用简单,高性能和轻量级。
现在开源出来,遵从MIT许可协议。其中包含我们的过滤规则。如果大家有什么建议和想fa,欢迎和我一起完善。
作用
防止sql注入,本地包含,部分溢出,fuzzing测试,xss,×××F等web*** 防止svn/备份之类文件泄漏 防止ApacheBench之类压力测试工具的*** 屏蔽常见的扫描***工具,扫描器 屏蔽异常的网络请求 屏蔽图片附件类目录php执行权限 防止webshell上传
下载luajit 2.0并安装
http://luajit.org/download.html
[root@server01 src]# wget http://luajit.org/download/LuaJIT-2.0.0.tar.gz [root@server01 src]# cd LuaJIT-2.0.0 [root@server01 LuaJIT-2.0.0]# make && make install lib和include是直接放在/usr/local/lib和usr/local/include
2. 下载ngx_devel_kit解压
https://github.com/simpl/ngx_devel_kit/tags
[root@server01 src]# wget https://codeload.github.com/simpl/ngx_devel_kit/tar.gz/v0.2.18 [root@server01 src]# tar zxvf ngx_devel_kit-0.2.18.tar.gz
3.下载nginx_lua_module解压
https://github.com/chaoslawful/lua-nginx-module/tags
[root@server01 src]# wget https://codeload.github.com/openresty/lua-nginx-module/tar.gz/v0.9.15 [root@server01 src]# tar zxvf lua-nginx-module-0.9.15.tar.gz
4.进入nginx源码文件夹,导入模块
[root@server01 ~]# cd /usr/local/src/nginx-1.6.2 [root@server01 nginx-1.6.2]# export LUAJIT_LIB=/usr/local/lib [root@server01 nginx-1.6.2]# export LUAJIT_INC=/usr/local/include/luajit-2.0 [root@server01 nginx-1.6.2]# ./configure --prefix=/usr/local/nginx-1.6.2 \ > --with-ld-opt="-Wl,-rpath,$LUAJIT_LIB" \ > --add-module=/usr/local/src/ngx_devel_kit-0.2.18 \ > --add-module=/usr/local/src/lua-nginx-module-0.9.15 [root@server01 nginx-1.6.2]# make -j2 #[root@server01 nginx-1.6.2]# make install [root@server01 sbin]# ./nginx -s stop [root@server01 nginx-1.6.2]# cp /usr/local/nginx-1.6.2/sbin/nginx{,.bak} [root@server01 nginx-1.6.2]# cp ./objs/nginx /usr/local/nginx-1.6.2/sbin/ cp:是否覆盖"/usr/local/nginx-1.6.2/sbin/nginx"? y
5.安装ngx_lua_waf模块
[root@server01 src]# wget [root@server01 src]# mv ngx_lua_waf-master.zip /usr/local/nginx-1.6.2/conf/ root@server01 src]# cd /usr/local/nginx-1.6.2/conf/ [root@server01 conf]# unzip ngx_lua_waf-master.zip [root@server01 conf]# mv ngx_lua_waf-master waf [root@server01 conf]# vim /usr/local/nginx-1.6.2/conf/nginx.conf #WAF lua_package_path "/usr/local/nginx-1.6.2/conf/waf/?.lua"; lua_shared_dict limit 10m; init_by_lua_file /usr/local/nginx-1.6.2/conf/waf/init.lua; access_by_lua_file /usr/local/nginx-1.6.2/conf/waf/waf.lua; [root@server01 conf]# /usr/local/nginx-1.6.2/sbin/nginx -t /usr/local/nginx-1.6.2/sbin/nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory[报错] 解决方案: # cat /etc/ld.so.conf include ld.so.conf.d/*.conf # echo "/usr/local/lib" >> /etc/ld.so.conf # ldconfig [root@server01 local]# /usr/local/nginx-1.6.2/sbin/nginx -t nginx: the configuration file /usr/local/nginx-1.6.2/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx-1.6.2/conf/nginx.conf test is successful [root@server01 local]# /usr/local/nginx-1.6.2/sbin/nginx -V nginx version: nginx/1.6.2 built by gcc 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC) configure arguments: --prefix=/usr/local/nginx-1.6.2 --with-ld-opt=-Wl,-rpath,$LUAJIT_LIB --add-module=/usr/local/src/ngx_devel_kit-0.2.18 --add-module=/usr/local/src/lua-nginx-module-0.9.15
配置config.lua里的waf规则目录(一般在waf/conf/目录下)
[root@server01 waf]# vim /usr/local/nginx-1.6.2/conf/waf/config.lua RulePath = "/usr/local/nginx-1.6.2/conf/waf/wafconf/" logdir = "/usr/local/nginx-1.6.2/logs/hack/ [root@server01 waf]# /usr/local/nginx-1.6.2/sbin/nginx -s reload
配置文件详细说明
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白名单 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~~]] --警告内容,可在中括号内自定义 备注:不要乱动双引号,区分大小写
***测试
[root@server02 ~]# curl http://192.168.65.129:8888/test.php?id=../etc/passwd Please go away~~ [root@server02 ~]# curl http://192.168.65.129:8888/test.php?id=../etc/passwd Please go away~~ [root@server02 ~]# curl http://192.168.65.129:8888/test.php?id=../etc/passwd Please go away~~ [root@server02 ~]# curl http://192.168.65.129:8888/test.php?id=../etc/passwd Please go away~~
查看waf***日志
[root@server01 nginx-1.6.2]# cd logs/ [root@server01 logs]# ls 192.168.65.129_2015-01-09_sec.log 192.168.65.129-access.log access.log error.log nginx.pid [root@server01 logs]# tail 192.168.65.129_2015-01-09_sec.log 192.168.65.128 [2015-01-09 17:26:06] "GET 192.168.65.129/test.php?id=../etc/passwd" "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2" "\.\./" 192.168.65.128 [2015-01-09 17:26:24] "GET 192.168.65.129/test.php?id=../etc/passwd" "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2" "\.\./" 192.168.65.128 [2015-01-09 17:26:24] "GET 192.168.65.129/test.php?id=../etc/passwd" "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2" "\.\./" 192.168.65.128 [2015-01-09 17:26:25] "GET 192.168.65.129/test.php?id=../etc/passwd" "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2" "\.\./"
规则更新:
考虑到正则的缓存问题,动态规则会影响性能,所以暂没用共享内存字典和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
@Copyright
神奇的魔法师 | |
Forum | http://bbs.linuxtone.org/ |
Copyright | Copyright (c) 2013- loveshell |
License | MIT License |
转载于:https://blog.51cto.com/kaibinyuan/1620458