OpenResty + ngx_lua_waf使用


  本篇介紹在CentOS7.6上安裝、測試使用ngx_lua_waf + openresty。

Preface

# yum install epel-release -y
# yum group install "Development Tools" -y    # 安裝基本編譯工具

安裝Luagit

# cd /opt/
# wget http://luajit.org/download/LuaJIT-2.1.0-beta3.tar.gz
# tar -xvf LuaJIT-2.1.0-beta3.tar.gz
# cd LuaJIT-2.1.0-beta3/
# make && make install
# ln -sf luajit-2.1.0-beta3 /usr/local/bin/luajit

安裝OpenResty

安裝依賴

# yum install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel -y

下載安裝

# cd /opt/
# wget https://openresty.org/download/openresty-1.13.6.1.tar.gz
# tar -xvf openresty-1.13.6.1.tar.gz
# cd openresty-1.13.6.1/
# ./configure --prefix=/usr/local/openresty --with-luajit --with-http_stub_status_module --with-pcre --with-pcre-jit
# gmake && gmake install
# ln -sf /usr/local/openresty/nginx/sbin/nginx /usr/local/openresty/bin/openresty
# /usr/local/openresty/bin/openresty   # 啟動openresty
# netstat -lntp | grep 80              # 服務運行正常
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN 

配置ngx_lua_waf

下載

# cd /usr/local/openresty/nginx/conf                  # 到openresty配置文件目錄
# git clone https://github.com/loveshell/ngx_lua_waf.git      # 下載
Cloning into 'ngx_lua_waf'...
# mv ngx_lua_waf/ waf/                           # 改個簡單的名字

添加配置

修改openresty配置文件。

# vim /usr/local/openresty/nginx/conf/nginx.conf
...
user nobody; # 取消注釋
...
http{ # 在http塊下添加如下內
...
lua_package_path "/usr/local/openresty/nginx/conf/waf/?.lua";
lua_shared_dict limit 10m;
init_by_lua_file /usr/local/openresty/nginx/conf/waf/init.lua;
access_by_lua_file /usr/local/openresty/nginx/conf/waf/waf.lua;

...

修改ngx_lua_waf配置

# cd /usr/local/openresty/nginx/conf/waf/     # ngx_lua_waf目錄
# vim config.lua
...
RulePath = "/usr/local/openresty/nginx/conf/waf/wafconf/"    # 規則文件路徑
attacklog = "on"                                             # 啟用日志
logdir = "/usr/local/openresty/nginx/logs/hack/"             # 日志目錄
...

創建日志目錄

# mkdir -p /usr/local/openresty/nginx/logs/hack/
# chown -R nobody:nobody  /usr/local/openresty/nginx/logs/hack/

測試openresty配置是否正常:

# /usr/local/openresty/bin/openresty               # 如果沒有啟動服務,則啟動
# /usr/local/openresty/bin/openresty -s reload     # 如果已經啟動,則重載配置
# /usr/local/openresty/bin/openresty -t            # 測試配置是否正常
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful

WAF測試

 直接訪問站點(此處為虛擬機的ip地址),應該能看到Openresty的歡迎頁。

 攻擊測試

嘗試目錄遍歷攻擊,即使用../,跳轉目錄讀取文件。

訪問:http://192.168.139.139//test.php?id=../etc/passwd

提示,檢測到攻擊行為,請求被攔截,說明可以正常工作。

 ngx_lua_waf配置文件說明

配置文件路徑:/usr/local/openresty/nginx/conf/waf/config.lua

RulePath = "/usr/local/openresty/nginx/ngx_lua_waf/wafconf/"   ##指定相應位置
attacklog = "on"                            ##開啟日志
logdir = "/usr/local/openresty/nginx/logs/hack/"           ##日志存放位置
UrlDeny="on"                              ##是否開啟URL防護
Redirect="on"                             ##地址重定向
CookieMatch="on"                           ##cookie攔截
postMatch="on"                            ##post攔截
whiteModule="on"                           ##白名單
black_fileExt={"php","jsp"}                        
ipWhitelist={"127.0.0.1"}                    ##白名單IP
ipBlocklist={"1.0.0.1"}                     ##黑名單IP
CCDeny="on"                             ##開啟CC防護        
CCrate="100/60"                          ##60秒內允許同一個IP訪問100次

參考

  centos下安裝openresty+ngx_lua_waf防火牆部署

   https://github.com/loveshell/ngx_lua_waf

  https://blog.51cto.com/tar0cissp/1980249

注:參考鏈接中都有幾個小問題,寫的不夠清晰。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM