ngx_lua_waf项目介绍:
ngx_lua_waf是一个基于lua-nginx-module(openresty)的web应用防火墙,主要用途包括:
- 防止sql注入,本地包含,部分溢出,fuzzing测试,xss,SSRF等web攻击
- 防止svn/备份之类文件泄漏
- 防止ApacheBench之类压力测试工具的攻击
- 屏蔽常见的扫描黑客工具,扫描器
- 屏蔽异常的网络请求
- 屏蔽图片附件类目录php执行权限
- 防止webshell上传
安装环境为基于Docker的Ubuntu 20.04,Web应用环境为某漏洞靶场平台,具体配置如下:
1、下载ngx_devel_kit
| cd /usr/local/src
 wget https://github.com/simplresty/ngx_devel_kit/archive/v0.3.1rc1.tar.gz
 
 tar xf v0.3.1rc1.tar.gz
 
 | 
2、下载lua-nginx-module
| wget https://github.com/openresty/lua-nginx-module/archive/v0.10.14.tar.gz
 tar xf v0.10.14.tar.gz
 
 | 
3、安装luajit 2.1
| wget https://github.com/openresty/luajit2/archive/v2.1-20190329.tar.gz
 cd luajit2-2.1-20190329
 
 make
 
 make install
 
 | 
4、导入环境变量
| export LUAJIT_LIB=/usr/local/lib
 export LUAJIT_INC=/usr/local/include/luajit-2.1
 
 | 
5、编译nginx模块
| useradd -s /bin/false -M www
 cd /usr/local/src
 
 wget http://nginx.org/download/nginx-1.14.2.tar.gz
 
 tar xf nginx-1.14.2.tar.gz
 
 cd nginx-1.14.2
 
 ./configure --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 --add-module=/usr/local/src/ngx_devel_kit-0.3.1rc1 --add-module=/usr/local/src/lua-nginx-module-0.10.14 --with-ld-opt=-Wl,-rpath,$LUAJIT_LIB
 
 # error:openssl未安装
 apt-get install openssl libssl-dev
 
 make
 
 make install
 
 | 
6、设置Nginx服务
| ln -sv /usr/local/nginx/sbin/nginx /usr/local/sbin/
 wget -P /etc/init.d/ http://down.whsir.com/downloads/nginx
 
 chmod +x /etc/init.d/nginx
 
 # 此处可设置开机启动服务
 
 /etc/init.d/nginx start
 
 | 
7、下载ngx_lua_waf
| cd /usr/local/nginx/conf
 git clone https://github.com/loveshell/ngx_lua_waf.git
 
 mv ngx_lua_waf waf
 
 | 
8、在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;
 
 | 
9、重启nginx
| /etc/init.d/nginx restart
 | 
10、配置端口转发,将流量通过80端口(waf)转发到8000端口(靶场)
| vim nginx.conf
 # 在server/location字段中添加如下内容
 proxy_pass  http://127.0.0.1:8000; # 转发规则
 proxy_set_header Host $proxy_host; # 修改转发请求头,让8000端口的应用可以受到真实的请求
 proxy_set_header X-Real-IP $remote_addr;
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 
 # 这样访问 http://xxxx 时就会转发到本地的 8000 端口
 
 | 
11、测试
| 访问http://域名或IP地址?id=../etc/passwd
 例如:http://192.168.157.132?id=../etc/passwd
 
 看到拦截提示则表示配置成功
 
 |