Nginx安裝配置naxsi waf防火墻的方法實現(xiàn)
Naxsi 是第三方 nginx 模塊 ,它和 Modsecurity 都是開源 WAF ,但是它們的防御模式不同。 Naxsi 不依賴像防病毒軟件這樣的簽名庫,因此不會被“未知”攻擊模式所規(guī)避,它就像是 windows 下默認的防火墻。Naxsi 和其他 WAF 之間的另一個主要區(qū)別就是僅過濾 GET 和 POST 請求。
之前一直在用 modsecurity ,效果還不錯,但是它對 nginx 支持真的不太好~.~ 。經(jīng)常會產(chǎn)生大量錯誤日志,不過這個并不影響它的正常功能,只是看著揪心。讓我想更換它的主要原因是 Modsecurity 經(jīng)常在處理某個請求(正?;虿徽#r,會突然導致 CPU 99.9% 以上,這是最不能忍受的。
1、Naxsi 和 Modsecurity對比
WAF | Naxsi | Modsecurity |
---|---|---|
Nginx 兼容性 | 好(第三方 nginx 模塊) | 不好(原 Apache 下的模塊,Nginx 下bug較多) |
防御模式 | 簡單(不依賴簽名庫) | 復雜(依賴更新規(guī)則) |
白名單規(guī)則 | 支持 | 支持 |
安裝難度 | 容易 | 一般(需要安裝依賴庫) |
社區(qū)支持 | 一般 | 較好 |
內存占用 | 小 | 大?不到哪去 |
特色 | 支持學習模式,可借助 nxapi/nxtool 自動分析白名單 | 可開啟只記錄不阻止,但是官方?jīng)]有提供分析工具。 |
總結 | Naxsi 比較靈活,所以學習成本較大,白名單規(guī)則需要自己完成。對 Nginx 支持良好,快速輕便。 | Modsecurity 操作簡單,規(guī)則更新較快,比較占用服務器資源,對于生產(chǎn)環(huán)境下的 Nginx 來說是個噩夢 |
在日常使用中,可以發(fā)現(xiàn) Modsecurity 具有非常嚴格的防御規(guī)則(誤報挺多的),并且規(guī)則支持較好(有強大的后臺?)。如果你使用 Apache 服務器,推薦使用 Modsecurity WAF。如果你使用的是 Nginx 服務器,建議先嘗試使用 Naxsi 。
下面就來在 Centos 下編譯安裝 Nginx + Naxsi WAF 。 【占位:Modsecurity 的編譯安裝】
2、編譯 Nginx + Naxsi
首先先運行:
# nginx -V
然后可以看到現(xiàn)有的模塊,復制保存一下備用。
configure arguments: --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-openssl=../openssl-1.0.2l --with-pcre=../pcre-8.40 --with-pcre-jit --with-ld-opt=-ljemalloc
3、下載 Nginx 和 Naxsi
Naxsi 應該使用所有高于 0.8.X 的 Nginx 版本。 Naxsi 版本可以在 https://github.com/nbs-system/naxsi 這里,選擇 Branch –> Tags 查看版本號。
下載 Nginx 和 Naxsi ,并解壓,然后進入解壓后的 Nginx 目錄:
# wget http://nginx.org/download/nginx-1.12.1.tar.gz # wget https://github.com/nbs-system/naxsi/archive/0.55.3.tar.gz # tar xvzf nginx-1.12.1.tar.gz # tar xvzf naxsi-0.55.3.tar.gz # cd nginx-1.12.1/
Naxsi 不要求任何特定的依賴,它需要的 libpcre ,libssl ,zlib ,gzip 這些 Nginx 已經(jīng)集成了。
然后編譯(記得在 ./configure 后面加上 --add-module=../naxsi-0.55.3/naxsi_src/ 和你之前備份的模塊):
./configure --prefix=/usr/local/nginx --user=www --group=www --add-module=../naxsi-0.55.3/naxsi_src/ --with-http_stub_status_module --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-openssl=../openssl-1.0.2l --with-pcre=../pcre-8.40 --with-pcre-jit --with-ld-opt=-ljemalloc make //不要 make install,不然就真的覆蓋了
等待編譯完成。Naxsi 安裝完成。
4、替換nginx二進制文件
# cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak # cp ./objs/nginx /usr/local/nginx/sbin/
注:如果提示 cp: cannot create regular file ‘/usr/local/nginx/sbin/nginx’: Text file busy ,請先 service nginx stop
5、檢查 nginx 模塊
# nginx -V
看到有 –add-module=../naxsi-0.55.3/naxsi_src/ 就成功了。
6、nginx/naxsi 基本配置
6.1、http 部分配置
打開 nginx.conf 在 http 部分配置:
http { include naxsi_core.rules; #導入 naxsi 核心規(guī)則 ... } server 部分配置 在 nginx.conf 的 server 部分配置: location / { #開啟 naxsi SecRulesEnabled; #開啟學習模式 LearningMode; #定義阻止請求的位置 DeniedUrl "/50x.html"; #CheckRules, 確定 naxsi 何時采取行動 CheckRule "$SQL >= 8" BLOCK; CheckRule "$RFI >= 8" BLOCK; CheckRule "$TRAVERSAL >= 4" BLOCK; CheckRule "$EVADE >= 4" BLOCK; CheckRule "$XSS >= 8" BLOCK; #naxsi 日志文件 error_log /.../foo.log; ... } error_page 500 502 503 504 /50x.html; #This is where the blocked requests are going location = /50x.html { return 418; #I'm a teapot \o/ }
6.2、測試
測試 nginx 配置
/nginx/sbin/nginx -t nginx: the configuration file /nginx/conf/nginx.conf syntax is ok nginx: configuration file /nginx/conf/nginx.conf test is successful
重啟 nginx
service nginx reload
6.3、防御測試
瀏覽器中打開 http://www.test.com/?a=<>‘ ,出現(xiàn) 403 錯誤,并且在 foo.log 中出現(xiàn) NAXSI_FMT 開頭的日志。恭喜你 Naxsi 啟用成功。
6.4、白名單規(guī)則
Naxsi 社區(qū)提供了一些常用的白名單規(guī)則,例如 wordpress 。可以在 https://github.com/nbs-system/naxsi-rules下載白名單規(guī)則。
然后將規(guī)則 include 到 server 內的 location 中。重啟 nginx 即可。不過目前這些白名單最近的修改日期有點久了~.~ ,可根據(jù)自身需要添加白名單規(guī)則。
詳細的白名單規(guī)則以及 Naxsi 其他支持,可參考 Naxsi WIKI。
到此這篇關于Nginx安裝配置naxsi waf防火墻的方法實現(xiàn)的文章就介紹到這了,更多相關Nginx安裝配置naxsi waf防火墻內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Keepalived+Nginx+Tomcat 實現(xiàn)高可用Web集群的示例代碼
這篇文章主要介紹了Keepalived+Nginx+Tomcat 實現(xiàn)高可用Web集群的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-09-09nginx禁止直接通過ip進行訪問并跳轉到自定義500頁面的操作
這篇文章主要介紹了nginx禁止直接通過ip進行訪問并跳轉到自定義500頁面的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05Nginx服務器作反向代理實現(xiàn)內部局域網(wǎng)的url轉發(fā)配置
這篇文章主要介紹了Nginx服務器作反向代理實現(xiàn)內部局域網(wǎng)的url轉發(fā)實例,文中提到需要注意proxy_read_timeout參數(shù)的相關調整,需要的朋友可以參考下2016-01-01