LuaJIT安装
yum -y install gcc gcc-c++ autoconf automake make unzip
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
cd /usr/local/src/
wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz
tar xf LuaJIT-2.0.5.tar.gz
cd LuaJIT-2.0.5
make && make install
安装Lua Ngx模块
wget https://github.com/simplresty/ngx_devel_kit/archive/v0.3.0.tar.gz
tar xf v0.10.13.tar.gz
echo "export LUAJIT_LIB=/usr/local/lib" >> /etc/profile
echo "export LUAJIT_INC=/usr/local/include/luajit-2.0" >> /etc/profile
source /etc/profile
Nginx添加Lua模块
cd /usr/local/src/
wget http://nginx.org/download/nginx-1.14.0.tar.gz
tar xf nginx-1.14.0.tar.gz
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 -j2
make install
ln -s /usr/local/nginx-1.14.0 /usr/local/nginx
准备Lua waf防护脚本
https://github.com/loveshell/ngx_lua_waf
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