Nginx + Lua 搭建网站WAF防火墙

  • 一、目的
  • 二、前期环境准备
  • (一)、更新下yum源
  • (二)、编译安装Nginx
  • (三)、端口放行
  • (四)、验证安装
  • (五)、lua编译安装
  • (六)、端口冲突解决办法
  • 三、Nginx+Lua搭建WAF防火墙
  • (一)、php环境配置
  • (二)、克隆代码并将其移动到nginx/waf目录下
  • (三)、进行必要配置
  • (四)、验证
  • 四、总结


一、目的

利用centos -7 和Nginx + Lua 搭建网站WAF防火墙可以防御SQL、XSS等攻击。

二、前期环境准备

(一)、更新下yum源

这边使用的是centos-7的系统,这边采用清华源

[root@wr ~]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
[root@wr ~]# vi /etc/yum.repos.d/CentOS-Base.repo.repo

nginx 转发 防火墙 nginx配置防火墙_编译安装


更新软件包缓存

[root@wr ~]# yum makecache

nginx 转发 防火墙 nginx配置防火墙_lua_02

(二)、编译安装Nginx

先编译安装一下,后面说lua模块的时候再重新编译下就行了

下载相应的包:curl -o nginx.tar.gz http://nginx.org/download/nginx-1.16.0.tar.gz 或者 wget http://nginx.org/download/nginx-1.16.0.tar.gz

解压:tar -zxvf nginx.tar.gz 进入到nginx-1.16.0目录(nginx 压缩目录),进行编译参数的配置

cd nginx-1.16.0

然后编译参数命令

./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/local/nginx/nginx.pid \
--lock-path=/var/local/nginx/nginx.lock \
--error-log-path=/var/local/nginx/error.log \
--http-log-path=/var/local/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/local/nginx/client \
--http-proxy-temp-path=/var/local/nginx/proxy \
--http-fastcgi-temp-path=/var/local/nginx/fastcgi \
--http-uwsgi-temp-path=/var/local/nginx/uwsgi \
--http-scgi-temp-path=/var/local/nginx/scgi

可以使用nginx -V来查看编译参数
最后进编译安装

make && make install

(三)、端口放行

因为我们要通过http访问,所以要方向80端口
开发80端口

[root@wr ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent

使得新建的规则生效

[root@wr ~]# firewall-cmd --reload

nginx 转发 防火墙 nginx配置防火墙_lua_03

(四)、验证安装

设置开机启动nginx

[root@wr ~]# systemctl enable nginx

启动nginx

[root@wr ~]# systemctl start nginx

查看nginx是否启动

[root@wr ~]# ps -aux |grep nginx

nginx 转发 防火墙 nginx配置防火墙_编译安装_04


在另一台机器上验证,成功安装

nginx 转发 防火墙 nginx配置防火墙_centos_05

(五)、lua编译安装

1、安装Lua库

[root@wr ~]# yum install lua lua-devel -y

nginx 转发 防火墙 nginx配置防火墙_lua_06


2、安装下载Lua即使编辑器、

[root@wr ~]# wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz

3、下载Nginx模块:ngx_devel_kit and lua-nginx-module

[root@wr ~]# wget https://github.com/simplresty/ngx_devel_kit/archive/v0.3.1.tar.gz
[root@wr ~]# wget https://github.com/openresty/lua-nginx-module/archive/v0.10.15.tar.gz

nginx 转发 防火墙 nginx配置防火墙_编译安装_07


4、编译安装luajit并导入环境变量

解压

[root@wr ~]# tar -zxf v0.3.1.tar.gz 
[root@wr ~]# tar -zxf v0.10.15.tar.gz 
[root@wr ~]# tar -zxf LuaJIT-2.0.5.tar.gz

nginx 转发 防火墙 nginx配置防火墙_nginx 转发 防火墙_08


编译安装

[root@wr ~]# cd LuaJIT-2.0.5
[root@wr LuaJIT-2.0.5]# make install RREFTX=/usr/local/LuaJIT

nginx 转发 防火墙 nginx配置防火墙_nginx 转发 防火墙_09


发现报错,确实gcc命令,安装该命令,然后再次编译安装

nginx 转发 防火墙 nginx配置防火墙_centos_10


nginx 转发 防火墙 nginx配置防火墙_nginx_11


导入环境变量

export LUAJIT_LIB=/usr/local/LuaJIT/lib
export LUAJIT_INC=/usr/local/LuaJIT/include/luajit-2.0

nginx 转发 防火墙 nginx配置防火墙_centos_12


加载lua库到ld.so.conf文件

[root@wr ~]# echo "/usr/local/LuaJIT/lib" >> /etc/ld.so.conf

nginx 转发 防火墙 nginx配置防火墙_nginx 转发 防火墙_13


查看是否加载成功

[root@wr ~]# cat /etc/ld.so.conf

nginx 转发 防火墙 nginx配置防火墙_lua_14


然后在nginx-1.60目录下执行一下命令,让动态函数加载到缓存中

[root@wr nginx-1.16.0]# ldconfig

5、配置nginx编译参数及重新编译安装nginx

6、验证lua是否可以使用
在nginx.config的server节点下添加:

vi /etc/nginx/nginx.conf

nginx 转发 防火墙 nginx配置防火墙_nginx_15


然后使得配置命令生效

检查配置

nginx -t -c /etc/nginx/nginx.conf

配置生效

nginx -s reload -c /etc/nginx/nginx.conf

开始验证,如果出现404错误,重启centos即可解决问题

nginx 转发 防火墙 nginx配置防火墙_nginx 转发 防火墙_16

(六)、端口冲突解决办法

启动nginx服务器的时候,报错,地址被占用,想起来我的80端口,apache服务器使用了

nginx 转发 防火墙 nginx配置防火墙_编译安装_17

1、查看nginx配置文件位置

whereis nginx.conf

nginx 转发 防火墙 nginx配置防火墙_centos_18


2、进入对应目录

nginx 转发 防火墙 nginx配置防火墙_nginx_19


3、nginx.conf文件对应路径/usr/local/nginx/conf/nginx.conf

nginx 转发 防火墙 nginx配置防火墙_lua_20


4、找到对应的listen 80 修改默认端口即可,这边已经修改为8012

修改前记得做好备份

cp nginx.conf nginx.conf_backup  # 备份

nginx 转发 防火墙 nginx配置防火墙_lua_21

5、启动nginx服务

./nginx
netstat -ntlp 查看正在运行的服务

nginx 转发 防火墙 nginx配置防火墙_centos_22

nginx 转发 防火墙 nginx配置防火墙_nginx 转发 防火墙_23

三、Nginx+Lua搭建WAF防火墙

这边使用开源的ngx_lua_waf来进行搭建

(一)、php环境配置

参考链接 安装php-fpm

yum install php-fpm

在nginx.conf的配置。只需要把loaction /php 里的注释符号去掉即可
还有一个问题就是fastCGI参数SCRIPT_FILENAME 是写死的。如果修改了root指令的值或者移动文件到别的目录,php-fpm会返回“No input file specified”错误,因为SCRIPT_FILENAME在配置中是写死的并没有随着$doucument_root变化而变化,我们可以修改SCRIPT_FILENAME配置如下:fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

在根目录下面。创建一个文件名为index.php文件,内容为<?php phpinfo(); ?>

nginx 转发 防火墙 nginx配置防火墙_centos_24

(二)、克隆代码并将其移动到nginx/waf目录下

[root@wr nginx-1.16.0]# git clone https://github.com/loveshell/ngx_lua_waf
[root@wr ngx_lua_waf]# mkdir /etc/nginx/waf
[root@wr ngx_lua_waf]# mv * /etc/nginx/waf/

nginx 转发 防火墙 nginx配置防火墙_nginx 转发 防火墙_25


参数说明:

1、args里面的规则get参数进行过滤的

2、url是只在get请求url过滤的规则

3、post是只在post请求过滤的规则

4、whitelist是白名单,里面的url匹配到不做过滤

5、user-agent是对user-agent的过滤规则

(三)、进行必要配置

[root@wr waf]# vi config.lua

如果有个目录不存在可以自己使用mkdir命令创建.,这边白名单加一个服务器本机的IP,然后规则存放目录应该是/etc/nginx/waf/wafconf。下图有误。

nginx 转发 防火墙 nginx配置防火墙_编译安装_26


在nginx.config的http下添加如下内容

vi /etc/nginx/nginx.conf

nginx 转发 防火墙 nginx配置防火墙_centos_27

lua_package_path "/etc/nginx/waf/?.lua";
lua_shared_dict limit 10m;
init_by_lua_file /etc/nginx/waf/init.lua;
access_by_lua_file /etc/nginx/waf/waf.lua;

(四)、验证

添加一个在args中sql规则\sor\s+,然后
重启服务

[root@wr waf]# nginx -s reload -c /etc/nginx/nginx.conf

在根目录下创建一个名为test.php文档,内容为<?php echo $_GET['id']; ?> xss防御

nginx 转发 防火墙 nginx配置防火墙_lua_28


sql防御

nginx 转发 防火墙 nginx配置防火墙_nginx 转发 防火墙_29

四、总结

在centos 7系统下配置Nginx+lua基本上都需要编译安装和配置,会比较繁琐一下,不过在线安装容易出错。在配置防火墙规则的时候注意白名单和规则目录的配置,没配置好的话。很难找出问题。特别是白名单。