WAF(Web Application Firewall),中文名叫做“Web应用防火墙”
WAF的定义是这样的:Web应用防火墙是通过执行一系列针对HTTP/HTTPS的安全策略来专门为Web应用提供保护的一款产品,通过从上面对WAF的定义中,
我们可以很清晰地了解到:WAF是一种工作在应用层的、通过特定的安全策略来专门为Web应用提供安全防护的产品。
ngx_lua_waf是一个基于ngx_lua的web应用防火墙。
# ngx_lua_waf用途:
防止sql注入,本地包含,部分溢出,fuzzing测试,xss,SSRF等web攻击
防止svn/备份之类文件泄露
防止ApacheBench之类的压力测试工具的攻击
屏蔽常见的黑客扫描工具,扫描器
屏蔽异常的网络请求
屏蔽图片附件类目录PHP执行权限
防止webshell上传
# 安装
1、部署环境说明:
[root@localhost ~]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core)
[root@localhost ~]# uname -r
3.10.0-862.el7.x86_64
2、安装依赖包:
yum -y install gcc gcc-c++ autoconf automake make unzip yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
3、安装LuaJIT2.0(LuaJIT是采用C语言写的Lua代码的解释器 这里推荐使用lujit2.1做lua支持)
下载luajit 2.0并安装 [root@localhost ~]# cd /usr/local/src/ [root@localhost src]# wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz [root@localhost src]# tar xf LuaJIT-2.0.5.tar.gz [root@localhost src]# cd LuaJIT-2.0.5 [root@localhost LuaJIT-2.0.5]# make && make install
4、安装ngx_devel_kit(NDK(nginx development kit)模块是一个拓展nginx服务器核心功能的模块,第三方模块开发可以基于它来快速实现。)
[root@localhost src]# wget https://github.com/simplresty/ngx_devel_kit/archive/v0.3.0.tar.gz [root@localhost src]# tar xf v0.3.0.tar.gz
5、安装nginx_lua_module
[root@localhost src]# wget https://github.com/openresty/lua-nginx-module/archive/v0.10.13.tar.gz [root@localhost src]# tar xf v0.10.13.tar.gz
6、导入环境变量:
echo "export LUAJIT_LIB=/usr/local/lib" >> /etc/profile echo "export LUAJIT_INC=/usr/local/include/luajit-2.0" >> /etc/profile
source /etc/profile
7、编译安装Nginx
[root@localhost ~]# cd /usr/local/src/ [root@localhost src]# wget http://nginx.org/download/nginx-1.14.0.tar.gz [root@localhost src]# tar xf nginx-1.14.0.tar.gz [root@localhost src]# cd nginx-1.14.0
[root@localhost src]# useradd -s /sbin/nologin -M www
[root@localhost nginx-1.14.0]# ./configure --user=www --group=www \
--prefix=/usr/local/nginx-1.14.0 \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
--pid-path=/usr/local/nginx-1.14.0/nginx.pid \
--with-http_realip_module \
--add-module=/usr/local/src/ngx_devel_kit-0.3.0 \
--add-module=/usr/local/src/lua-nginx-module-0.10.13 \
--with-ld-opt="-Wl,-rpath,$LUAJIT_LIB"
[root@localhost nginx-1.14.0]#make -j2
[root@localhost nginx-1.14.0]# make install
[root@localhost nginx-1.14.0]#ln -s /usr/local/nginx-1.14.0 /usr/local/nginx
8、新建/usr/local/nginx/logs/hack/攻击日志目录,并赋予nginx用户对该目录的写入权限。
mkdir -p /usr/local/nginx/logs/hack/chown -R www.www /usr/local/nginx/logs/hack/
chmod -R 755 /usr/local/nginx/logs/hack/
至此nginx支持WAF防护功能已经搭建完成!
#使用说明:
nginx配置文件路径为:/usr/local/nginx/conf/
把ngx_lua_waf下载到conf目录下,解压命名为waf
wget https://github.com/loveshell/ngx_lua_waf/archive/master.zip unzip master.zip -d /usr/local/nginx/conf/ mv /usr/local/nginx/conf/ngx_lua_waf-master /usr/local/nginx/conf/waf
在nginx.conf的http段添加下面这段:
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;
配置config.lua里的waf规则目录(一般在waf/conf/目录下):
RulePath = "/usr/local/nginx/conf/waf/wafconf/" #绝对路径如有变动,需对应修改
然后重启nginx即可
###配置文件详细说明:
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~~]] --警告内容,可在中括号内自定义 备注:不要乱动双引号,区分大小写
启动nginx:
/usr/local/nginx/sbin/nginx
###检查规则是否生效:
部署完毕可以尝试如下命令:
curl http://your_ip/test.php?id=../etc/passwd
结果如下则说明规则生效(页面修改地址:/usr/local/nginx/conf/waf/config.lua):
注意:默认,本机在白名单不过滤,可自行调整config.lua配置
###一些说明:
过滤规则在wafconf下,可根据需求自行调整,每条规则需换行,或者用|分割 args里面的规则get参数进行过滤的 url是只在get请求url过滤的规则 post是只在post请求过滤的规则 whitelist是白名单,里面的url匹配到不做过滤 user-agent是对user-agent的过滤规则 默认开启了get和post过滤,需要开启cookie过滤的,编辑waf.lua取消部分--注释即可 日志文件名称格式如下:虚拟主机名_sec.log
一键脚本:
#!/bin/bash #Author: Template yum -y install gcc gcc-c++ autoconf automake make unzip wget yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel cd /usr/local/src/ [ ! -f "LuaJIT-2.0.5.tar.gz" ] && wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz [ ! -f "nginx-1.14.0.tar.gz" ] && wget http://nginx.org/download/nginx-1.14.0.tar.gz && [ ! -f "v0.3.0.tar.gz" ] && wget https://github.com/simplresty/ngx_devel_kit/archive/v0.3.0.tar.gz [ ! -f "v0.10.13.tar.gz" ] && wget https://github.com/openresty/lua-nginx-module/archive/v0.10.13.tar.gz [ ! -f "master.zip" ] && wget https://github.com/loveshell/ngx_lua_waf/archive/master.zip --no-check-certificate ls *.tar.gz | xargs -n 1 tar xf cd LuaJIT-2.0.5 && make && make install && cd .. echo "export LUAJIT_LIB=/usr/local/lib" >> /etc/profile && \ echo "export LUAJIT_INC=/usr/local/include/luajit-2.0" >> /etc/profile source /etc/profile cd nginx-1.14.0 && useradd -s /sbin/nologin -M www ./configure --user=www --group=www \ --prefix=/usr/local/nginx-1.14.0 \ --with-http_stub_status_module \ --with-http_ssl_module \ --with-http_gzip_static_module \ --pid-path=/usr/local/nginx-1.14.0/nginx.pid \ --with-http_realip_module \ --add-module=/usr/local/src/ngx_devel_kit-0.3.0 \ --add-module=/usr/local/src/lua-nginx-module-0.10.13 \ --with-ld-opt="-Wl,-rpath,$LUAJIT_LIB" && make -j8 && make install && ln -s /usr/local/nginx-1.14.0 /usr/local/nginx mkdir -p /usr/local/nginx/logs/hack/ && chown -R www.www /usr/local/nginx/logs/hack/ && chmod -R 755 /usr/local/nginx/logs/hack/ sed -i '25 a lua_package_path \"/usr/local/nginx/conf/waf/?.lua\";\nlua_shared_dict limit 10m;\ninit_by_lua_file /usr/local/nginx/conf/waf/init.lua;\naccess_by_lua_file /usr/local/nginx/conf/waf/waf.lua;' /usr/local/nginx/conf/nginx.conf cd /usr/local/src/ && unzip master.zip -d /usr/local/nginx/conf/ && mv /usr/local/nginx/conf/ngx_lua_waf-master /usr/local/nginx/conf/waf /usr/local/nginx/sbin/nginx
参考地址:https://github.com/loveshell/ngx_lua_waf