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

一、簡介

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

本文主要是經過春哥的開源框架openresty來實現WAF;

參考:https://github.com/openresty

 
二、架構

總體架構:春哥的openresty開源框架(該框架集成了nginx、lua、lua-nginx-module等模塊),推薦使用該框架;

若是想使用原生的nginx、lua來搭建waf,請參閱  nginx lua lua-nginx-module構建web應用防火牆(waf)html

 



三、實現功能

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

四、安裝包下載

          提供了各類版本openresty,點擊連接,去下載適合版本。

          openresty-1.13.6.2.tar.gz
五、安裝openresty

建立安裝包目錄並將下載的安裝包上傳至該目錄(也能夠直接經過wget命令直接下載)

    mkdir -p /app/openresty/install
    cd /app/openresty/install/
nginx

 


解壓 tar zxvf openresty-1.13.6.2.tar.gz
c++

 


cd openresty-1.13.6.2
git

 


安裝基本包

yum install -y readline-devel pcre-devel openssl-devel gcc gcc-c++ perl.x86_64
github

 


配置環境

 ./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
web

 

 


編譯 gmake
瀏覽器

 


安裝 gmake install
安全

 


修改nginx.conf,測試是否安裝成功
架構

 


編輯nginx.conf

vi nginx.conf

    server {
        location /hello {
                default_type text/html;
                content_by_lua_block {
                    ngx.say("HelloWorld")
                }
            }
    }
app

 


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

    cd ../sbin
    ps -ef|grep nginx
    ./nginx -t
    ./nginx
    ps -ef|grep nginx

 


關閉防火牆或者開放虛擬機防火牆端口,

這裏爲了方便,直接就關閉防火牆了,生產中必須採用開放防火牆端口的方式

systemctl stop firewalld

systemctl status firewalld

 


瀏覽器訪問測試 http://10.10.91.23/hello

 


六、部署waf

下載waf包  https://github.com/unixhot/waf

可使用wget,也能夠下載到本地,讓後上傳到虛擬機

解壓後,將waf複製到 /usr/local/openresty/nginx/conf/

cp -r waf /usr/local/openresty/nginx/conf/

 


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

/usr/local/openresty/nginx/conf

vi nginx.conf

        #WAF
        lua_shared_dict limit 50m;
        lua_package_path "/usr/local/openresty/nginx/conf/waf/?.lua";
        init_by_lua_file "/usr/local/openresty/nginx/conf/waf/init.lua";
        access_by_lua_file "/usr/local/openresty/nginx/conf/waf/access.lua";

 


 

修改waf中的配置信息

cd /waf

vi config.lua

相關文章
相關標籤/搜索