前段时间在自己的php环境上搞了个 360 的webscan 来做网页程序的攻击防护,一般的防护没有问题,上传组件控制不严格导致webshell攻击,自己对安全相关知识还是缺少积累,百度相关资料发现了LUA模块,网上有openresty 模块集成nginx + lua,然而我的nginx是生产环境,难得去改配置,随决定平滑升级LUA模块。
lnmp环境搭建可以参考LNMP一键安装包 - CentOS/RadHat/Debian/Ubuntu下自动编译安装Nginx,PHP,MySQL,PHPMyAdmin
原NGINX安装路径 /usr/local/nginx 安装前记得备份, 建议使用高版本的模块,低版本会有各种问题,难得处理、
什么是waf
Web 应用防火墙 (WAF-Web Application Firewall) 旨在保护 Web 应用免受各类应用层攻击,例如跨站点脚本 (XSS)、SQL 注入,以及 cookie 中毒等。应用是您重要数据的网关,因此针对应用发起的攻击就成为了造成漏洞的主要原因。有了 WAF 就可以拦截一系列企图通过入侵系统来泄漏数据的攻击。
开始安装
安装依赖包
yum -y install gcc gcc-c++ autoconf automake make unzip
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
安装LuaJIT2.0
LuaJIT是Lua的即时编译器,简单来说,LuaJIT是一个高效的Lua虚拟机。
下载安装LuaJIT 2.1(2.0或者2.1都是支持的,官方推荐2.1):http://luajit.org/download.html
cd /usr/local/src
wget http://luajit.org/download/LuaJIT-2.1.0-beta2.tar.gz
tar zxf LuaJIT-2.1.0-beta2.tar.gz
cd LuaJIT-2.1.0-beta2
make PREFIX=/usr/local/luajit
make install PREFIX=/usr/local/luajit
下载ngx_devel_kit(NDK)模块
https://github.com/simpl/ngx_devel_kit/tags,不需要安装
cd /usr/local/src
wget https://github.com/simplresty/ngx_devel_kit/archive/v0.3.0.tar.gz (上面附件中有注意名称)
tar -xzvf ngx_devel_kit-0.3.0.tar.gz
下载最新的lua-nginx-module 模块
https://github.com/openresty/lua-nginx-module/tags,不需要安装 附件上方有注意名称
cd /usr/local/src
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.13.tar.gz
tar -xzvf lua-nginx-module-0.10.13.tar.gz
设置环境变量
export LUAJIT_LIB=/usr/local/luajit/lib
export LUAJIT_INC=/usr/local/luajit/include/luajit-2.1
nginx -V查看已经编译的配置
nginx -V
我的配置如下:
--prefix=/usr/local/nginx --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_sub_module --with-stream --with-stream_ssl_module --with-openssl=/root/lnmp1.8/src/openssl-1.1.1k --with-openssl-opt='enable-weak-ssl-ciphers'
加入黄色部分
--user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_sub_module --with-stream --with-stream_ssl_module --with-openssl=/root/lnmp1.8/lnmp1.7/src/openssl-1.1.1g --with-openssl-opt='enable-weak-ssl-ciphers' --with-pcre=../pcre-8.38 --with-pcre-jit --with-ld-opt=-ljemalloc --with-ld-opt=-Wl,-rpath,/usr/local/luajit/lib --add-module=/usr/local/src/ngx_devel_kit-0.3.0 --add-module=/usr/local/src/lua-nginx-module-0.10.13
注意 pcre-8.38 openssl-1.1.1g 这些的需要解压到对应的路径
进入nginx目录(在lnmp src nginx-XX 目录下)
执行
./configure --prefix=/usr/local/nginx --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_sub_module --with-stream --with-stream_ssl_module --with-openssl=/root/lnmp1.8/src/openssl-1.1.1k --with-openssl-opt='enable-weak-ssl-ciphers' --with-pcre=../pcre-8.38 --with-pcre-jit --with-ld-opt=-ljemalloc --with-ld-opt=-Wl,-rpath,/usr/local/luajit/lib --add-module=/usr/local/src/ngx_devel_kit-0.3.0 --add-module=/usr/local/src/lua-nginx-module-0.10.13
然后编译安装
make -j2
make install
中间如果编译失败 则需要更换 ngx_devel_kit 或 lua-nginx-module 版本测试
安装ngx_lua_waf
cd /usr/local/src/
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段添加
vi /usr/local/nginx/conf/nginx.conf
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;
创建日志目录
mkdir /usr/local/nginx/logs/hack
chown www /usr/local/nginx/logs/hack
修改配置文件
cd /usr/local/nginx/conf/waf
cat config.lua
规则文件在wafconf中
args里面的规则get参数进行过滤的
cookie是对请求过滤的cookie过滤
url是只在get请求url过滤的规则
post是只在post请求过滤的规则
whiteurl是白名单,里面的url匹配到不做过滤
user-agent是对user-agent的过滤规则
重启nginx 测试效果
/usr/local/nginx/sbin/nginx -s reload (可能需要 重启 nginx restart )
重启如果缺少lua文件或模块 则需要到 OpenResty 官方博客 去下载对应文件 解压覆盖尝试
访问url
修改规则
/usr/local/nginx/conf/waf/wafconf/url 规则中最后一个匹配
/(attachments|upimg|images|css|uploadfiles|html|uploads|templets|static|template|data|inc|forumdata|upload|includes|cache|avatar)/(\\w+).(php|jsp)
中的 /(\\w+).(php|jsp) 应改成 /.*\.(php|jsp)