【轉】Nginx 學習筆記(十一)nginx下安裝配置naxsi waf防火牆(附完整編譯、配置)


原文地址:http://f2ex.cn/nginx-installed-configuration-naxsi-waf/

Naxsi 是第三方 nginx 模塊 ,它和 Modsecurity 都是開源 WAF ,但是它們的防御模式不同。 Naxsi 不依賴像防病毒軟件這樣的簽名庫,因此不會被“未知”攻擊模式所規避,它就像是 windows 下默認的防火牆。Naxsi 和其他 WAF 之間的另一個主要區別就是僅過濾 GET 和 POST 請求。

我之前一直在用 modsecurity ,效果還不錯,但是它對 nginx 支持真的不太好~.~ 。經常會產生大量錯誤日志,不過這個並不影響它的正常功能,只是看着揪心。讓我想更換它的主要原因是 Modsecurity 經常在處理某個請求(正常或不正常)時,會突然導致 CPU 99.9% 以上,這是最不能忍受的。

我們先來簡單對比下 Naxsi 和 Modsecurity :

WAF Naxsi Modsecurity
Nginx 兼容性 好(第三方 nginx 模塊) 不好(原 Apache 下的模塊,Nginx 下bug較多)
防御模式 簡單(不依賴簽名庫) 復雜(依賴更新規則)
白名單規則 支持 支持
安裝難度 容易 一般(需要安裝依賴庫)
社區支持 一般 較好
內存占用 大?不到哪去
特色 支持學習模式,可借助 nxapi/nxtool 自動分析白名單 可開啟只記錄不阻止,但是官方沒有提供分析工具。
總結 Naxsi 比較靈活,所以學習成本較大,白名單規則需要自己完成。對 Nginx 支持良好,快速輕便。 Modsecurity 操作簡單,規則更新較快,比較占用服務器資源,對於生產環境下的 Nginx 來說是個噩夢

在日常使用中,可以發現 Modsecurity 具有非常嚴格的防御規則(誤報挺多的),並且規則支持較好(有強大的后台?)。如果你使用 Apache 服務器,推薦使用 Modsecurity WAF。如果你使用的是 Nginx 服務器,建議先嘗試使用 Naxsi 。

下面就來在 Centos 下編譯安裝 Nginx + Naxsi WAF 。Modsecurity 的編譯安裝在這里

編譯 Nginx + Naxsi

首先先運行:

# nginx -V

然后可以看到現有的模塊,復制保存一下備用。

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
下載 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 已經集成了。

然后編譯(記得在 ./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 安裝完成。

替換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

檢查 nginx 模塊
# nginx -V

看到有 –add-module=../naxsi-0.55.3/naxsi_src/ 就成功了。

nginx/naxsi 基本配置

首先將 naxsi 目錄下的 naxsi_core.rules 拷貝至 nginx.conf 所在目錄。

http 部分配置

打開 nginx.conf 在 http 部分配置:

http {
 include naxsi_core.rules;  #導入 naxsi 核心規則
 ...
}
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/
  }
server 完整示例配置
server {
listen 80 default;
access_log /wwwlogs/access_nginx.log combined;
root /www/site;
index index.html index.htm index.php;
location ~ [^/]\.php(/|$) {        
    SecRulesEnabled; 
    #LearningMode;     
    DeniedUrl "/RequestDenied";
    CheckRule "$SQL >= 8" BLOCK;
    CheckRule "$RFI >= 8" BLOCK;
    CheckRule "$TRAVERSAL >= 4" BLOCK;
    CheckRule "$EVADE >= 4" BLOCK;
    CheckRule "$XSS >= 8" BLOCK;    
    error_log /wwwlogs/foo.log;   
    fastcgi_pass unix:/dev/shm/php-cgi.sock;
    fastcgi_index index.php;
    include fastcgi.conf;
}
location /RequestDenied {
    return 403;
}    
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
    expires 30d;
    access_log off;
    }
location ~ .*\.(js|css)?$ {
    expires 7d;
    access_log off;
    }
}

測試

測試 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
防御測試

瀏覽器中打開 http://www.test.com/?a=<>‘ ,出現 403 錯誤,並且在 foo.log 中出現 NAXSI_FMT 開頭的日志。恭喜你 Naxsi 啟用成功。

白名單規則

Naxsi 社區提供了一些常用的白名單規則,例如 wordpress 。可以在 https://github.com/nbs-system/naxsi-rules下載白名單規則。

然后將規則 include 到 server 內的 location 中。重啟 nginx 即可。不過目前這些白名單最近的修改日期顯示是1年前~.~ ,可根據自身需要添加白名單規則。

詳細的白名單規則以及 Naxsi 其他支持,可參考 Naxsi WIKI


免責聲明!

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



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