0x00:前言
ngx_lua_waf是一个基于ngx_lua的web应用防火墙。
地址:https://github.com/loveshell/ngx_lua_waf
waf需要nginx环境,这里我们使用openresty
OpenResty(又称:ngx_openresty) 是一个基于 NGINX 的可伸缩的 Web 平台,由中国人章亦春发起,提供了很多高质量的第三方模块。
OpenResty 是一个强大的 Web 应用服务器,Web 开发人员可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,更主要的是在性能方面,OpenResty可以 快速构造出足以胜任 10K 以上并发连接响应的超高性能 Web 应用系统。
这次安装ngx_lua_waf基于openresty,主要是因为方便
也可以零散安装各种模块
系统:Centos 7虚拟机
0x01:openresty安装
综合看了很多安装教程
注:很多命令前都加了sudo 因为不是root,若是root可不加
(1)更新yum,不太清楚为啥,某个博客提到了
OpenResty 官方现在开始维护自己的打包虚机集合了,新的 linux 包仓库正在陆续登陆 openresty.org 官网。欢迎大家试用!原来老源的用户可以先禁用掉老的 openresty 源。
- sudo yum install -y yum-util
- sudo yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo
(2)安装依赖库
- sudo yum install -y readline-devel pcre-devel openssl-devel
(3)安装openresty
- cd src
- sudo wget "https://openresty.org/download/openresty-1.15.8.2.tar.gz" ///注意版本,可到官网找最新版本
- ./configure --prefix=/usr/local/openresty --with-luajit --with-http_stub_status_module --with-pcre --with-pcre-jit
- sudo gmake && sudo gmake install
(4)测试openresty
- sudo vim /usr/local/openresty/nginx/conf/nginx.conf
在文件中写:类似在server中写一个hello函数
server { location /hello { default_type text/html; content_by_lua_block { ngx.say("Hello world") } } }
启动nginx
- sudo /usr/local/openresty/nginx/sbin/nginx -t
- sudo /usr/local/openresty/nginx/sbin/nginx
- sudo curl http://xxxxxxx虚拟机IP地址xxxxxxxx/hello
返回hello world
或者浏览器打开http://xxx虚拟机IP地址xxx/hello
- netstat -lntp | grep 80 #服务器正常运行
服务器启动、重启命令
# /usr/local/openresty/bin/openresty # 如果没有启动服务,则启动
# /usr/local/openresty/bin/openresty -s reload # 如果已经启动,则重载配置
# /usr/local/openresty/bin/openresty -t # 测试配置是否正常
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
注:可能本机浏览器打不开虚拟机网址,原因是iptables防火墙
sudo /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT ##允许80端口通过防火墙
/etc/init.d/iptables save #这个我没用到
/etc/init.d/iptables restart #这个我也没用到
或者到防护墙文件中设置
vi /etc/sysconfig/iptables
-A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT(允许80端口通过防火墙)
-A INPUT -m state –state NEW -m tcp -p tcp –dport 3306 -j ACCEPT(允许3306端口通过防火墙)
重启防火墙
/etc/init.d/iptables restart
(5)安装配置ngx_lua_waf
https://github.com/loveshell/ngx_lua_waf中也有详细说明
- cd /usr/local/openresty/nginx/conf #到该目录下安装配置
- sudo git clone https://github.com/loveshell/ngx_lua_waf.git
- sudo mv ngx_lua_waf/ waf/ #改文件名字 中间有空格
配置:
- sudo vim /usr/local/openresty/nginx/conf/nginx.conf
...
user nobody; # 取消注释
...
http{ # 在http块下添加如下内
...
lua_package_path "/usr/local/openresty/nginx/conf/waf/?.lua";
lua_shared_dict limit 10m;
init_by_lua_file /usr/local/openresty/nginx/conf/waf/init.lua;
access_by_lua_file /usr/local/openresty/nginx/conf/waf/waf.lua;
- cd /usr/local/openresty/nginx/conf/waf/
- sudo vim config.lua
...
RulePath = "/usr/local/openresty/nginx/conf/waf/wafconf/" # 规则文件路径,路径灵活变动不是固定的
attacklog = "on" # 启用日志
logdir = "/usr/local/openresty/nginx/logs/hack/" # 日志目录
...
创建日志目录
- # mkdir -p /usr/local/openresty/nginx/logs/hack/
- # chown -R nobody:nobody /usr/local/openresty/nginx/logs/hack/
访问
http://xxxx/test.php?id=../etc/passwd