眾所周知,寶塔面板有商用的Nginx防火墻插件(要錢),具有豐富的WAF(Web Application Firewall)功能,但是其防CC能力在面對攻擊時形同虛設,使用簡單的CC工具就可以輕松實現503 Page。而較老的httpguard、ngx_lua_waf等均已年久失修。偶然間在Github上發現了ngx_waf后折騰了一下發現確實不錯。
NGX_WAF簡介
方便且高性能的 Nginx 防火墻模塊。(以下內容來自原項目介紹)
為什么選擇 ngx_waf
功能齊全:「網絡應用防火墻」的基本功能都有。
安裝方便:大多數情況下你可以直接下載使用預構建的模塊,而不是編譯代碼。
使用方便:配置指令簡單易懂,不用看文檔都能猜到大概是什么意思。
規則靈活:提供高級規則,將動作(如攔截或放行)和多個條件表達式組合起來。
高性能:經過較為極限的測試,啟動本模塊后 RPS(每秒請求數) 降低約 4%。測試說明和結果見使用文檔。
功能
SQL 注入防護(Powered By libinjection)。
XSS 攻擊防護(Powered By libinjection)。
支持 IPV4 和 IPV6。
CC 防御,超出限制后自動拉黑對應 IP 一段時間。
IP 黑白名單,同時支持類似 192.168.0.0/16 和 fe80::/10,即支持點分十進制和冒號十六進制表示法和網段劃分。
POST 黑名單。
URL 黑白名單
查詢字符串(Query String)黑名單。
UserAgent 黑名單。
Cookie 黑名單。
Referer 黑白名單。
高級規則,將動作(如攔截或放行)和多個條件表達式組合起來。
相關鏈接
使用文檔:https://docs.addesp.com/ngx_waf/zh-cn/
GitHub:https://github.com/ADD-SP/ngx_waf
總結
一個用C構建的高效率Nginx防火墻,開發者積極維護(在寫稿的時候就更新了3個版本導致不得不反復修改),并且支持開啟5秒盾,實測抗CC性能優秀!
安裝
以下內容源于官方文檔和自己的折騰記錄,僅供參考!
環境
CentOS 7
Nginx 1.21.0
寶塔 7.6.0
ngx_waf v7.0.0 Current
如果您的環境與我相同,以下安裝指令可以照抄,反之請自行檢查相關指令。
安裝過程
首先創建臨時的工作目錄,并將項目拷貝到本地。
mkdir /root/nginx #臨時工作目錄,這個在后面會用到
cd /root/nginx
git clone https://github.com/ADD-SP/ngx_waf.git #獲取最新版本的ngx_waf,也可以直接去Release找以往的版本
cd ngx_waf
git clone https://github.com/libinjection/libinjection.git inc/libinjection
nginx 提供兩種安裝模塊的方式,即「靜態鏈接」和「動態加載」,通過兩種方式安裝的模塊也分別稱為「靜態模塊」和「動態模塊」。
你可以通過運行腳本 assets/guide.sh 來選擇使用靜態模塊還是動態模塊。
sh assets/guide.sh
# It is recommended that you use dynamic modules.
# 如果輸出上面這行則建議使用動態模塊。
# It is recommended that you use static modules.
# 如果輸出上面這行則建議使用靜態模塊。
在寶塔環境下得到的推薦是靜態模塊,實際在折騰過文章介紹的動態模塊后均會出現奇怪的錯誤,于是在后文介紹的是靜態模塊安裝。
到這一步我們需要開始進行編譯,這里可能會涉及各種依賴問題如gcc等,由于不同環境的依賴問題不一樣,這里就不再贅述,請自行通過搜索引擎解決。
在搞定依賴問題后需要獲取當前nginx的configure 腳本的參數,在寶塔環境下輸入以下指令即可獲得。
/www/server/nginx/sbin/nginx -V
得到以下內容,這里將configure arguments: 后面的內容記錄下來,并在結尾加上–add-module=/usr/local/src/ngx_waf即可得到編譯參數。如果您使用的編譯器是 GCC,請在 –with-cc-opt 中追加 -fstack-protector-strong, 例如 –with-cc-opt=’-Werror -g’ —> –with-cc-opt=’-Werror -g -fstack-protector-strong’。
nginx version: nginx/1.21.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.1.1k 25 Mar 2021
TLS SNI support enabled
configure arguments: --user=www --group=www --prefix=/www/server/nginx --add-module=/www/server/nginx/src/ngx_devel_kit --add-module=/www/server/nginx/src/lua_nginx_module --add-module=/www/server/nginx/src/ngx_cache_purge --add-module=/www/server/nginx/src/nginx-sticky-module --with-openssl=/www/server/nginx/src/openssl --with-pcre=pcre-8.43 --with-http_v2_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_stub_status_module --with-http_ssl_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_gunzip_module --with-ipv6 --with-http_sub_module --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --with-ld-opt=-Wl,-E --with-cc-opt=-Wno-error --with-ld-opt=-ljemalloc --with-http_dav_module --add-module=/www/server/nginx/src/nginx-dav-ext-module --with-cc-opt=-std=gnu99
接下來開始編譯ngx_waf模塊與nginx,以下代碼會用到上述處理好的編譯參數。
cd /root/nginx/ngx_waf
make
cd /www/server/nginx/src
./configure --user=www --group=www --prefix=/www/server/nginx --add-module=/www/server/nginx/src/ngx_devel_kit --add-module=/www/server/nginx/src/lua_nginx_module --add-module=/www/server/nginx/src/ngx_cache_purge --add-module=/www/server/nginx/src/nginx-sticky-module --with-openssl=/www/server/nginx/src/openssl --with-pcre=pcre-8.43 --with-http_v2_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_stub_status_module --with-http_ssl_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_gunzip_module --with-ipv6 --with-http_sub_module --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --with-ld-opt=-Wl,-E --with-cc-opt=-Wno-error --with-ld-opt=-ljemalloc --with-http_dav_module --add-module=/www/server/nginx/src/nginx-dav-ext-module --add-module=/root/nginx/ngx_waf --with-cc-opt='-std=gnu99'
make
mv ../sbin/nginx ../sbin/nginx.bak
cp ./objs/nginx ../sbin/nginx
chmod +x /www/server/nginx/sbin/nginx
到這里就已經替換原有的Nginx程序,接下來在寶塔中啟動Nginx即可!
若您出現如下報錯:
nginx: the configuration file /www/server/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /www/server/nginx/conf/nginx.conf test failed
則需要先卸載寶塔的Nginx防火墻再重新安裝!
到此為止,安裝與編譯結束。
配置
在安裝完成后我們可以將配置文件加入單個站點的配置文件中以實現未不同網站定制不同的規則,配置填寫位置如圖:
![圖片[1]-寶塔強化WAF防護-開啟5秒盾-輕松防CC攻擊!](http://m.oilmaxhydraulic.com.cn/wp-content/uploads/2023/05/d2b5ca33bd092629.png)
以下介紹我的配置,內容并不完善,各參數與各模塊具體功能請詳見參考文檔!
waf on; # 是否啟用模塊
waf_rule_path /root/nginx/ngx_waf/assets/rules/; # 模塊規則
waf_mode DYNAMIC; # 啟用動態網站模式
waf_cache capacity=100; # 設置緩存規則檢查結果相關的參數,過小會導致頻繁地淘汰緩存,增加內存碎片,降低性能
waf_under_attack on file=/www/wwwroot/defence.html; # 對所有訪問的用戶開啟5秒盾,file需要填寫單個html的絕對路徑!請僅在網站遭受大量攻擊的時候打開!其中該html的作用為5秒之后刷新頁面!
waf_cc_deny rate=300r/m duration=10m size=20m; #每分鐘請求大于300次則封禁10分鐘,最多緩存20M大小的請求記錄
如果使用CDN或負載均衡等反向代理程序,為獲取真實的客戶IP地址,需要用到http_realip_module,寶塔的Nginx已經默認編譯該模塊。此時我們需要在配置文件中加入:
set_real_ip_from 0.0.0.0/0; #CDN節點的IP段,此項可多次申明,請根據實際情況填寫,照抄危險!
real_ip_header X-Forwarded-For;
以上提到了用于5秒盾跳轉的HTML頁面,以下是我自己的一個版本,僅供參考。
![圖片[2]-寶塔強化WAF防護-開啟5秒盾-輕松防CC攻擊!](http://m.oilmaxhydraulic.com.cn/wp-content/uploads/2023/05/d2b5ca33bd092844-1024x644.png)
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>正在檢查您的瀏覽器...</title>
<link href="http://m.oilmaxhydraulic.com.cn/666666/meihua/css?family=Kanit:200" rel="stylesheet">
<style type="text/css">
*{-webkit-box-sizing:border-box;box-sizing:border-box}body{padding:0;margin:0}#defence{position:relative;height:100vh}#defence .defence{position:absolute;left:50%;top:50%;-webkit-transform:translate(-50%,-50%);-ms-transform:translate(-50%,-50%);transform:translate(-50%,-50%)}.defence{max-width:767px;width:100%;line-height:1.4;text-align:center;padding:15px}.defence .defence-404{position:relative;height:220px}.defence .defence-404 h1{font-family:Kanit,sans-serif;position:absolute;left:50%;top:50%;-webkit-transform:translate(-50%,-50%);-ms-transform:translate(-50%,-50%);transform:translate(-50%,-50%);font-size:186px;font-weight:200;margin:0;background:linear-gradient(130deg,#ffa34f,#ff6f68);color:transparent;-webkit-background-clip:text;background-clip:text;text-transform:uppercase}.defence h2{font-family:Kanit,sans-serif;font-size:33px;font-weight:200;text-transform:uppercase;margin-top:0;margin-bottom:25px;letter-spacing:3px}.defence p{font-family:Kanit,sans-serif;font-size:16px;font-weight:200;margin-top:0;margin-bottom:25px}.defence a{font-family:Kanit,sans-serif;color:#ff6f68;font-weight:200;text-decoration:none;border-bottom:1px dashed #ff6f68;border-radius:2px}.defence-social>a{display:inline-block;height:40px;line-height:40px;width:40px;font-size:14px;color:#ff6f68;border:1px solid #efefef;border-radius:50%;margin:3px;-webkit-transition:.2s all;transition:.2s all}.defence-social>a:hover{color:#fff;background-color:#ff6f68;border-color:#ff6f68}@media only screen and (max-width:480px){.defence .defence-404{position:relative;height:168px}.defence .defence-404 h1{font-size:142px}.defence h2{font-size:22px}}
</style>
<script type="text/javascript">
setTimeout("window.location.reload();","6000");
</script>
</head>
<body>
<div id="defence">
<div class="defence">
<div class="defence-404">
<h1>OOPS!</h1>
</div>
<h2>站點正在被攻擊!您的請求正在接受檢查...</h2>
<p>請給我們一些時間,您馬上就會回到站點,這會很快~</p>
</div>
</div>
</body>
</html>
將以上內容保存到/www/wwwroot/defence.html即可。
測試
訪問 /www.bak,如果返回 403 狀態碼則表示模塊成功啟動。
結語
至此ngx_waf模塊安裝結束,在配置好一定的防御策略之后可以獲得不錯的防御能力。其實跟云鎖的部署差不多。













