1. 程式人生 > 實用技巧 >Nginx新增Lua實現WAF防火牆

Nginx新增Lua實現WAF防火牆

Web應用防護系統(也稱:網站應用級入侵防禦系統 。英文:Web Application Firewall,簡稱: WAF)。利用國際上公認的一種說法:Web應用 防火牆是通過執行一系列針對HTTP/HTTPS的 安全策略來專門為Web應用提供保護的一款產品。

nginx+lua安裝方法

方法一:安裝nginx並整合lua模組

安裝LuaJIT

LuaJIT的意思是Lua Just-In-Time,是即時的Lua程式碼直譯器。必須去github下載否則執行是會出現報錯,專案地址:https://github.com/openresty/luajit2

git clone https://github.com/openresty/luajit2.git
cd luajit2
make PREFIX=/usr/local/luajit
make install PREFIX=/usr/local/luajit

安裝完成後將如下環境變數加入到/etc/profile中,並執行source /etc/profile

export LUAJIT_LIB=/usr/local/luajit/lib
export LUAJIT_INC=/usr/local/luajit/include/luajit-2.1

安裝ngx_devel_kit(NDK)

版本地址:https://github.com/vision5/ngx_devel_kit/tags

下載並解壓

cd /mnt
wget https://github.com/vision5/ngx_devel_kit/archive/v0.3.1.tar.gz
tar -xzvf v0.3.1.tar.gz

安裝最新版的lua-nginx-module

版本地址:https://github.com/openresty/lua-nginx-module/tags

下載最新穩定版並解壓

cd /mnt
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.15.tar.gz
tar -xzvf v0.10.16rc5.tar.gz

編譯Nginx並加入lua模組

cd /mnt/nginx-1.18.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 \
--user=nginx \
--group=nginx \
--with-http_gzip_static_module \
--with-http_realip_module \
--with-http_ssl_module \
--with-openssl=/mnt/openssl-1.1.1g \
--with-zlib=/mnt/zlib-1.2.11 \
--with-pcre=/mnt/pcre-8.44 \
--add-module=/mnt/lua-nginx-module-0.10.15 \
--add-module=/mnt/ngx_devel_kit-0.3.1

{% note warning %}
其中的openssl,pcre以及zlib需要額外下載並解壓到/mnt目錄下
{% endnote %}

啟動nginx發現報錯

[root@k8s-node mnt]# nginx
nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory

解決辦法

echo "/usr/local/luajit/lib/" >> /etc/ld.so.conf
ldconfig

在預設的location中加入如下指令,訪問測試

content_by_lua 'ngx.say("hello, lua")';

安裝成功

方法二 :直接安裝OpenResty

OpenResty是一個基於Nginx與 Lua 的高效能 Web 平臺,其內部集成了大量精良的 Lua 庫、第三方模組以及大多數的依賴項。用於方便地搭建能夠處理超高併發、擴充套件性極高的動態 Web 應用、Web 服務和動態閘道器。最新版Openresty

cd /opt 
tar -xzvf openresty-1.15.8.3.tar.gz
./configure \ 
--prefix=/opt/openresty \
--with-pcre=/opt/pcre-8.44 \
--with-zlib=/opt/zlib-1.2.11 \ 
--with-openssl=/opt/openssl-1.1.1g \
--with-poll_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-stream \
--with-stream_ssl_module \
--with-stream_ssl_preread_module\
--with-http_ssl_module
gmake
gmake install

按方法一測試可以訪問即可

WAF模組安裝

ngx_lua_waf專案地址:https://github.com/loveshell/ngx_lua_waf.git

這裡以Openresty為例,Nginx方法類似

cd /opt/openresty/lualib
git clone https://github.com/loveshell/ngx_lua_waf.git waf

在openresry的配置檔案中新增如下配置項:

lua_package_path "/opt/openresty/lualib/waf/?.lua";
lua_shared_dict limit 10m;
init_by_lua_file  /opt/openresty/lualib/waf/init.lua;           
access_by_lua_file /opt/openresty/lualib/waf/waf.lua;

整個waf目錄結構如下:

[root@k8s-node lualib]# tree waf
waf
├── config.lua
├── init.lua
├── wafconf
│   ├── args
│   ├── cookie
│   ├── post
│   ├── url
│   ├── user-agent
│   └── whiteurl
└── waf.lua

1 directory, 9 files

config.lua中定義了整個waf的配置,詳細如下:

#攔截規則的存放目錄
RulePath = "/opt/openresty/lualib/waf/wafconf/"
#是否開啟攔截日誌記錄
attacklog = "on"
#攔截日誌的記錄目錄,nginx的worker程序需要對該目錄有許可權
logdir = "/opt/openresty/nginx/logs/"
#是否開啟url攔截
UrlDeny="on"
#是否開啟攔截重定向
Redirect="on"
#是否開啟cookie攻擊防護
CookieMatch="on"
#是否開啟post攻擊防護
postMatch="on"
whiteModule="on"
#禁止訪問的副檔名
black_fileExt={"php","jsp"}
#IP地址白名單
ipWhitelist={"127.0.0.1"}
#IP地址黑名單
ipBlocklist={"1.0.0.1"}
#是否開啟CC攻擊防護
CCDeny="off"
#定義CC攻擊速率,該例為每60秒100次請求
CCrate="100/60"
#定義重定向後的html頁面
html=[[...]]

攔截測試:

#出現攔截頁面即表示安裝成功
curl http://www.example.com/test.php?id=../etc/passwd

相關日誌:

192.168.0.101 [2020-06-20 01:44:01] "GET localhost/index.php?id=/../../../etc/passwd" "-"  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36" "\.\./"