天天看点

Nginx + Lua 搭建网站WAF防火墙

目录:

  • 前言
  • 1.在线安装
  • 1.1.修改yum源地址
  • 1.2.在线安装Nginx
  • 1.3.端口放行
  • 1.4.验证安装
  • 2.知识拓展
  • 2.1.编译参数
  • 2.2.安装目录
  • 2.3.默认配置
  • 2.4.systemctl配置
  • 3.编译安装
  • 3.1.安装编译环境
  • 3.2.Nginx编译安装
  • 3.2.1.下载解压
  • 3.2.2.配置编译参数
  • 3.2.3.进行编译安装
  • 3.2.4.配置systemctl
  • 3.2.5.端口放行
  • 3.2.6.验证
  • 3.3.编译安装Lua模块
  • 大体思路
  • 3.3.1.编译安装luajit并导入环境变量
  • 3.3.2.共享lua动态库
  • 3.3.3.配置nginx的编译参数
  • 3.3.4.重新编译安装nginx
  • 3.3.5.验证Lua模块
  • 4.Nginx+Lua搭建WAF防火墙
  • 4.1.环境
  • 4.2.配置
  • 4.3.生效
  • 4.4.简单验证
  • 4.5.CC验证
  • 扩展:隐藏Nginx版本信息

对于项目里面只是使用代理等常用功能,在线安装即可,如需制定化模块,则推荐编译安装

PS:本文不仅仅包含Nginx相关的知识点,还包含了逆天学习方法(对待新事物的处理)

清华源:​​https://mirrors.tuna.tsinghua.edu.cn/help/centos/​​

Nginx + Lua 搭建网站WAF防火墙

更新软件包缓存:​

​yum makecache​

Nginx + Lua 搭建网站WAF防火墙

在线安装比较简单,参考官方文档即可:​​https://nginx.org/en/linux_packages.html​​

PS:线上选​

​stable​

​的就行了,记得把​

​$releasever​

​改成你的版本号,eg:​

​7​

Nginx + Lua 搭建网站WAF防火墙

安装图示:

Nginx + Lua 搭建网站WAF防火墙
# 创建nginx的yum
vi /etc/yum.repos.d/nginx.repo

# 内容如下:
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key

# 在线安装
yum install nginx -y      

放行80端口:​

​firewall-cmd --zone=public --add-port=80/tcp --permanent​

PS:规则生效:​

​firewall-cmd --reload​

Nginx + Lua 搭建网站WAF防火墙

Nginx + Lua 搭建网站WAF防火墙

离线安装可以参考在线安装的配置:​

​nginx -V​

​:编译参数(​

​nginx -v​

​:查看版本)

Nginx + Lua 搭建网站WAF防火墙

编译参数详解(点我展开)

在线安装的包都可以通过:​

​rpm -ql xxx​

​查看安装到哪些目录

安装目录详解(点我展开)

配置语法检查:​

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

PS:不重启的方式加载配置:​

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

全局以及服务级别的配置:

参数 说明

​user​

使用用户来运行nginx

​worker_processes​

工作进程数

​error_log​

nginx的错误日记
pid nginx启动时的pid

events相关配置:

​worker_connections​

每个进程的最大连接数

​use​

常用中间件配置:

http {
    ......
    server {
        listen          80;             # 端口号
        server_name     localhost;      # 域名
        # 路径访问控制(默认访问路径,eg:/ ==> 根目录)
        location / {
            root /usr/share/nginx/html; # 网站根目录
            index index.html index.htm index.py; # 首页配置
        }

        error_page 500 502 503 504 /50x.html; # 错误页面(可以自定义添404页面,error_page 404 /404.html;...)
        # 访问xxx/50x.html的时候去指定目录找
        location = /50x.html {
            root /usr/share/nginx/html; # 错误页面所在路径
        }
    }
    # 一个server配置一个虚拟 or 独立的站点(通过listen和server_name来区别多个server)
    server {
        ......
    }
}      

nginx:(等会编译安装的时候可以参考)

[root@localhost dnt]# cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target      

nginx-debug:

[root@localhost dnt]# cat /usr/lib/systemd/system/nginx-debug.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx-debug -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target      

一步到位:​

​yum install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel -y​

Nginx + Lua 搭建网站WAF防火墙

简单拆分解析一下:

  1. Nginx使用​

    ​C/C++​

    ​编写的,安装一下依赖:​

    ​yum install gcc-c++ -y​

  2. Nginx需要使用PCRE来进行正则解析:​

    ​yum install pcre pcre-devel -y​

  3. 现在服务器和浏览器一般都是使用gzip:​

    ​yum install -y zlib zlib-devel -y​

  4. 让Nginx支持https:​

    ​yum install openssl openssl-devel -y​

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

下载:​

​curl -o nginx.tar.gz http://nginx.org/download/nginx-1.16.0.tar.gz​

解压:​

​tar -zxvf nginx.tar.gz​

参考前面说的在线版Nginx来设置编译参数的配置:

PS:​

​nginx -V​

切换到nginx的解压目录:​

​cd nginx-1.16.0​

​ 然后执行下面命令

PS:root权限编译哦~
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'      

接着编译安装:​

​make && make install​

PS:提速:​

​make -j 4 && make install​

Nginx + Lua 搭建网站WAF防火墙

利用systemctl添加自定义系统服务

Nginx + Lua 搭建网站WAF防火墙
# vi /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target      

PS:如果不生效可以重载下systemctl:​

​systemctl daemon-reload​

​firewall-cmd --zone=public --add-port=80/tcp --permanent​

​firewall-cmd --reload​

Nginx + Lua 搭建网站WAF防火墙

Nginx + Lua 搭建网站WAF防火墙

运行的时候如果出现​

​nginx: [emerg] getpwnam("nginx") failed​

​的错误可以参考我写这篇文章:javascript:void(0)

PS:核心:​

​useradd -s /sbin/nologin -M nginx​

默认是不支持Lua的,所以需要自己编译安装下

PS:记得安装下Lua库:​

​yum install lua lua-devel -y​

主要就3步走:

  1. 安装Lua即时编译器:​

    ​LuaJIT​

  • 目前最新:​​http://luajit.org/download/LuaJIT-2.0.5.tar.gz​​
  1. 安装Nginx模块:​

    ​ngx_devel_kit​

    ​ and ​

    ​lua-nginx-module​

  1. ngx_devel_kit:​​https://github.com/simplresty/ngx_devel_kit/archive/v0.3.1.tar.gz​​
  2. lua-nginx-module:​​https://github.com/openresty/lua-nginx-module/archive/v0.10.15.tar.gz​​
  1. 重新编译Nginx:复制在线安装的编译参数(​

    ​nginx -V​

    ​)然后添加两个参数
  1. ​--add-module=../ngx_devel_kit-0.3.1​

  2. ​--add-module=../lua-nginx-module-0.10.15​

解压缩

Nginx + Lua 搭建网站WAF防火墙
# 编译安装
make install PREFIX=/usr/local/LuaJIT
# 导入环境变量
export LUAJIT_LIB=/usr/local/LuaJIT/lib
export LUAJIT_INC=/usr/local/LuaJIT/include/luajit-2.0      
Nginx + Lua 搭建网站WAF防火墙

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

​echo "/usr/local/LuaJIT/lib" >> /etc/ld.so.conf​

Nginx + Lua 搭建网站WAF防火墙

执行​

​ldconfig​

​让动态函式库加载到缓存中

Nginx + Lua 搭建网站WAF防火墙

Nginx + Lua 搭建网站WAF防火墙

完整参数附录:

./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=../ngx_devel_kit-0.3.1 --add-module=../lua-nginx-module-0.10.15      

编译安装:​

​make && make install​

Nginx + Lua 搭建网站WAF防火墙

验证下Lua是否已经可用:

在nginx.config的server节点下添加:

​vi /etc/nginx/nginx.conf​

Nginx + Lua 搭建网站WAF防火墙
server {
    listen       80;
    server_name  localhost;
    charset utf-8; # 默认编码为utf-8

    location / {
        root   html;
        index  index.html index.htm;
    }
    ...
    # 测试Nginx的Lua(添加这一段)
    location /hello {
        default_type 'text/plain';
        content_by_lua 'ngx.say("欢迎访问逸鹏说道公众号~")';
    }
    ...
}      

检查配置:​

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

PS:配置生效:​

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

Nginx + Lua 搭建网站WAF防火墙

看看效果:

Nginx + Lua 搭建网站WAF防火墙

扩展:你可以试试获取ip哦~

# 获取客户端ip
location /myip {
    default_type 'text/plain';
    content_by_lua '
        clientIP = ngx.req.get_headers()["x_forwarded_for"]
        ngx.say("IP:",clientIP)
    ';  
}      

市面上比较常用一块开源项目:​

​ngx_lua_waf​

​​https://github.com/loveshell/ngx_lua_waf​​
  1. 拦截Cookie类型工具
  2. 拦截异常post请求
  3. 拦截CC洪水攻击
  4. 拦截URL
  5. 拦截arg(提交的参数)
Nginx + Lua 搭建网站WAF防火墙

clone代码并移动到nginx的waf目录下

Nginx + Lua 搭建网站WAF防火墙

简单说下里面的规则分别有啥用:

  1. args里面的规则get参数进行过滤的
  2. url是只在get请求url过滤的规则
  3. post是只在post请求过滤的规则
  4. whitelist是白名单,里面的url匹配到不做过滤
  5. user-agent是对user-agent的过滤规则

修改必要配置

Nginx + Lua 搭建网站WAF防火墙

详细说明我引用一下我的上篇文章:

参数简单说明下:红色字体部分需要修改
Nginx + Lua 搭建网站WAF防火墙

​nginx.config​

​的​

​http​

​下添加如下内容:

Nginx + Lua 搭建网站WAF防火墙
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;      

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

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

Nginx + Lua 搭建网站WAF防火墙

Nginx + Lua 搭建网站WAF防火墙

PS:其实绕过很简单,看看他默认规则即可,这款WAF的强大之处在于轻量级,而且规则可以自定化

过滤规则在wafconf下,可根据需求自行调整,每条规则需换行,或者用|分割

举个例子:​

​http://192.168.0.10/hello?id=1 or 1=1​

PS:默认规则没有这点的防护
Nginx + Lua 搭建网站WAF防火墙

那么我们可以在args规则中添加比如​

​\sor\s+​

​,然后​

​nginx -s reload​

​一下就行了

PS:如果是从post进行注入,或者cookie中转注入,那么在对应规则里面添加就行,我这边只是演示下防火墙被绕过该怎么解决~(多看看日志)
Nginx + Lua 搭建网站WAF防火墙

留个课后作业:使用ab来测试下nginx+lua的waf对cc的防御效果

提示:可以使用​

​ab -n 2000 -c 200 http://192.168.0.10​

​来简单测试

PS:测试前curl http://192.168.0.10/hello 看看返回内容,测试后再curl看看返回内容

防止被黑客进行针对性渗透,隐藏下版本信息

PS:其他配置今天就不详细讲解了,下次讲Nginx的时候会说的

原来:

Nginx + Lua 搭建网站WAF防火墙

配置下:​

​vi /etc/nginx/nginx.conf​

http下添加:​

​server_tokens off;​

Nginx + Lua 搭建网站WAF防火墙

检查下语法:​

​nginx -t​

不重启的方式加载配置文件:​

​nginx -s reload​

Nginx + Lua 搭建网站WAF防火墙

现在效果:

Nginx + Lua 搭建网站WAF防火墙

作者:毒逆天

Nginx + Lua 搭建网站WAF防火墙

​yum makecache​

Nginx + Lua 搭建网站WAF防火墙

​stable​

​$releasever​

​7​

Nginx + Lua 搭建网站WAF防火墙
Nginx + Lua 搭建网站WAF防火墙
# 创建nginx的yum
vi /etc/yum.repos.d/nginx.repo

# 内容如下:
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key

# 在线安装
yum install nginx -y      

​firewall-cmd --zone=public --add-port=80/tcp --permanent​

​firewall-cmd --reload​

Nginx + Lua 搭建网站WAF防火墙

Nginx + Lua 搭建网站WAF防火墙

​nginx -V​

​nginx -v​

Nginx + Lua 搭建网站WAF防火墙

​rpm -ql xxx​

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

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

​user​

​worker_processes​

​error_log​

​worker_connections​

​use​

http {
    ......
    server {
        listen          80;             # 端口号
        server_name     localhost;      # 域名
        # 路径访问控制(默认访问路径,eg:/ ==> 根目录)
        location / {
            root /usr/share/nginx/html; # 网站根目录
            index index.html index.htm index.py; # 首页配置
        }

        error_page 500 502 503 504 /50x.html; # 错误页面(可以自定义添404页面,error_page 404 /404.html;...)
        # 访问xxx/50x.html的时候去指定目录找
        location = /50x.html {
            root /usr/share/nginx/html; # 错误页面所在路径
        }
    }
    # 一个server配置一个虚拟 or 独立的站点(通过listen和server_name来区别多个server)
    server {
        ......
    }
}      

[root@localhost dnt]# cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target      
[root@localhost dnt]# cat /usr/lib/systemd/system/nginx-debug.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx-debug -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target      

​yum install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel -y​

  1. ​C/C++​

    ​yum install gcc-c++ -y​

  2. ​yum install pcre pcre-devel -y​

  3. ​yum install -y zlib zlib-devel -y​

  4. ​yum install openssl openssl-devel -y​

​curl -o nginx.tar.gz http://nginx.org/download/nginx-1.16.0.tar.gz​

​tar -zxvf nginx.tar.gz​

​nginx -V​

​cd nginx-1.16.0​

./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'      

​make && make install​

​make -j 4 && make install​

Nginx + Lua 搭建网站WAF防火墙

Nginx + Lua 搭建网站WAF防火墙
# vi /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target      

​systemctl daemon-reload​

​firewall-cmd --zone=public --add-port=80/tcp --permanent​

​firewall-cmd --reload​

Nginx + Lua 搭建网站WAF防火墙

Nginx + Lua 搭建网站WAF防火墙

​nginx: [emerg] getpwnam("nginx") failed​

​useradd -s /sbin/nologin -M nginx​

​yum install lua lua-devel -y​

  1. ​LuaJIT​

  1. ​ngx_devel_kit​

    ​lua-nginx-module​

  1. ​nginx -V​

  1. ​--add-module=../ngx_devel_kit-0.3.1​

  2. ​--add-module=../lua-nginx-module-0.10.15​

Nginx + Lua 搭建网站WAF防火墙
# 编译安装
make install PREFIX=/usr/local/LuaJIT
# 导入环境变量
export LUAJIT_LIB=/usr/local/LuaJIT/lib
export LUAJIT_INC=/usr/local/LuaJIT/include/luajit-2.0      
Nginx + Lua 搭建网站WAF防火墙

​echo "/usr/local/LuaJIT/lib" >> /etc/ld.so.conf​

​ldconfig​

Nginx + Lua 搭建网站WAF防火墙

Nginx + Lua 搭建网站WAF防火墙
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=../ngx_devel_kit-0.3.1 --add-module=../lua-nginx-module-0.10.15      

​make && make install​

Nginx + Lua 搭建网站WAF防火墙

​vi /etc/nginx/nginx.conf​

Nginx + Lua 搭建网站WAF防火墙
server {
    listen       80;
    server_name  localhost;
    charset utf-8; # 默认编码为utf-8

    location / {
        root   html;
        index  index.html index.htm;
    }
    ...
    # 测试Nginx的Lua(添加这一段)
    location /hello {
        default_type 'text/plain';
        content_by_lua 'ngx.say("欢迎访问逸鹏说道公众号~")';
    }
    ...
}      

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

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

Nginx + Lua 搭建网站WAF防火墙
Nginx + Lua 搭建网站WAF防火墙
# 获取客户端ip
location /myip {
    default_type 'text/plain';
    content_by_lua '
        clientIP = ngx.req.get_headers()["x_forwarded_for"]
        ngx.say("IP:",clientIP)
    ';  
}      

​ngx_lua_waf​

Nginx + Lua 搭建网站WAF防火墙

Nginx + Lua 搭建网站WAF防火墙

Nginx + Lua 搭建网站WAF防火墙
Nginx + Lua 搭建网站WAF防火墙

​nginx.config​

​http​

Nginx + Lua 搭建网站WAF防火墙
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;      

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

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

Nginx + Lua 搭建网站WAF防火墙

Nginx + Lua 搭建网站WAF防火墙

​http://192.168.0.10/hello?id=1 or 1=1​

Nginx + Lua 搭建网站WAF防火墙

​\sor\s+​

​nginx -s reload​

Nginx + Lua 搭建网站WAF防火墙

​ab -n 2000 -c 200 http://192.168.0.10​

Nginx + Lua 搭建网站WAF防火墙

​vi /etc/nginx/nginx.conf​

​server_tokens off;​

Nginx + Lua 搭建网站WAF防火墙

​nginx -t​

​nginx -s reload​

Nginx + Lua 搭建网站WAF防火墙
Nginx + Lua 搭建网站WAF防火墙

继续阅读