close

開源框架openresty+nginx 實現web應用防火牆(WAF)


1、簡介
     Web應用防火牆(Web Application Firewall, WAF),通過對HTTP(S)請求進行檢測,識別並阻斷SQL注入、跨站腳本攻擊(Cross Site Scripting   xss)、網頁木馬上傳、命令/代碼注入、文件包含、敏感文件訪問、第三方應用漏洞攻擊、CC(挑戰黑洞)攻擊、惡意爬蟲掃描、跨站請求偽造等攻擊,保護Web服務安全穩定。

     本文主要是通過春哥的開源框架openresty來實現WAF;
     參考:https://github.com/openresty

 

2、架構
     整體架構:春哥的openresty開源框架(該框架集成了nginx、lua、lua-nginx-module等模塊),推薦使用該框架;
     如果想使用原生的nginx、lua來搭建waf,請參閱  nginx lua lua-nginx-module構建web應用防火牆(waf)

開源框架openresty+nginx 實現web應用防火牆(WAF)

開源框架openresty+nginx 實現web應用防火牆(WAF)

 3、實現功能

  •  
支持IP白名單和黑名單功能,直接將黑名單的IP訪問拒絕。支持URL白名單,將不需要過濾的URL進行定義。支持User-Agent的過濾,匹配自定義規則中的條目,然後進行處理(返回403)。支持CC攻擊防護,單個URL指定時間的訪問次數,超過設定值,直接返回403支持Cookie過濾,匹配自定義規則中的條目,然後進行處理(返回403)。支持URL過濾,匹配自定義規則中的條目,如果用戶請求的URL包含這些,返回403支持URL參數過濾,原理同上。支持日誌記錄,將所有拒絕的操作,記錄到日誌中去。日誌記錄為JSON格式,便於日誌分析,例如使用ELKStack進行攻擊日誌收集、存儲、搜索和展示。

4、安裝包下載
          提供了各種版本openresty,點擊鏈接,去下載適合版本。
          openresty-1.13.6.2.tar.gz

 

5、安裝openresty
      創建安裝包目錄並將下載的安裝包上傳至該目錄(也可以直接通過wget命令直接下載)
       mkdir -p /app/openresty/install
       cd /app/openresty/install/
開源框架openresty+nginx 實現web應用防火牆(WAF)

 

       解壓 tar zxvf openresty-1.13.6.2.tar.gz
開源框架openresty+nginx 實現web應用防火牆(WAF)

 
      cd openresty-1.13.6.2
開源框架openresty+nginx 實現web應用防火牆(WAF)

 
     安裝基本包
     yum install -y readline-devel pcre-devel openssl-devel gcc gcc-c++ perl.x86_64
開源框架openresty+nginx 實現web應用防火牆(WAF)

 

配置環境

  •  
./configure --prefix=/usr/local/openresty --with-luajit --with-http_v2_module --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module --with-pcre --with-pcre-jit --with-file-aio --with-http_dav_module

開源框架openresty+nginx 實現web應用防火牆(WAF)

 

開源框架openresty+nginx 實現web應用防火牆(WAF)

開源框架openresty+nginx 實現web應用防火牆(WAF)

 
編譯 gmake
開源框架openresty+nginx 實現web應用防火牆(WAF)

 
安裝 gmake install
開源框架openresty+nginx 實現web應用防火牆(WAF)

 
修改nginx.conf,測試是否安裝成功
開源框架openresty+nginx 實現web應用防火牆(WAF)

 

編輯nginx.conf

vi nginx.conf

開源框架openresty+nginx 實現web應用防火牆(WAF)

啟動nginx 一次執行下面代碼段

  •  
cd ../sbinps -ef|grep nginx./nginx -t./nginx ps -ef|grep nginx

開源框架openresty+nginx 實現web應用防火牆(WAF)

關閉防火牆或者開放虛擬機防火牆端口,
這裡為了方便,直接就關閉防火牆了,生產中必須採用開放防火牆端口的方式
systemctl stop firewalld
systemctl status firewalld
開源框架openresty+nginx 實現web應用防火牆(WAF)

 

瀏覽器訪問測試 http://10.10.91.23/hello
開源框架openresty+nginx 實現web應用防火牆(WAF)

 

6、部署waf
下載waf包  https://github.com/unixhot/waf
可以使用wget,也可以下載到本地,讓後上傳到虛擬機
解壓後,將waf復制到 /usr/local/openresty/nginx/conf/
cp -r waf /usr/local/openresty/nginx/conf/
開源框架openresty+nginx 實現web應用防火牆(WAF)

 


修改Nginx的配置文件,加入以下配置。注意路徑,同時WAF日誌默認存放在/tmp/日期_waf.log
/usr/local/openresty/nginx/conf
vi nginx.conf

開源框架openresty+nginx 實現web應用防火牆(WAF)

 

修改waf中的配置信息
cd /waf
vi config.lua
開源框架openresty+nginx 實現web應用防火牆(WAF)

開源框架openresty+nginx 實現web應用防火牆(WAF)

 

重啟nginx即可測試

 

----------------------------------------------------------------------------

 

作者:ccx_jy
來源:CSDN
原文:https://blog.csdn.net/chuanxincui/article/details/86089763

文章標籤
WAF
全站熱搜
創作者介紹
創作者 好康搬運工 的頭像
好康搬運工

好康搬運工

好康搬運工 發表在 痞客邦 留言(0) 人氣()