天天看點

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防火牆

繼續閱讀