nginx + lua 構建網站防禦waf(一)

最近在幫朋友維護一個站點。這個站點是一個Php網站。坑爹的是用IIS作代理。出了無數問題以後忍無可忍,因而要我幫他切換到nginx上面,前期被不斷的掃描和CC。最後找到了waf這樣一個解決方案緩解一下。話很少說直接開始。php

 

waf的做用:html

防止sql注入,本地包含,部分溢出,fuzzing測試,xss,SSRF等web攻擊
防止svn/備份之類文件泄漏
防止ApacheBench之類壓力測試工具的攻擊
屏蔽常見的掃描黑客工具,掃描器
屏蔽異常的網絡請求
屏蔽圖片附件類目錄php執行權限
防止webshell上傳

nginx 的話我選擇春哥開源的:OpenResty一個偉大的項目。nginx

 

好了步驟開始:git

 

一、安裝Luagit:github

# wget http://luajit.org/download/LuaJIT-2.1.0-beta1.tar.gzweb

# tar -xvf LuaJIT-2.1.0-beta1.tar.gzredis

# cd LuaJIT-2.1.0-beta1sql

# makeshell

# make installapache

#ln -sf luajit-2.1.0-beta1 /usr/local/bin/luajit

 

二、安裝openresty:

 ./configure --prefix=/opt/openresty   --with-luajit     --without-http_redis2_module       --with-http_iconv_module

gmake

gmake install

 

三、測試openresty:

[root@www ngx_lua_waf]# cd /opt/openresty/nginx/conf/

[root@www conf]# cat nginx.conf

http {

    server { 

        listen 80;

        location / {

                 default_type text/html;

              content_by_lua_block {

                  ngx.say("HelloWorld")

                            }

                 }

     }

}

###

測試一下訪問是否輸出hello world,後面應該會有一些列的簡介。

[root@www conf]# curl localhost

HelloWorld

 

四、下載開源項目:

[root@www nginx]# cd /opt/openresty/nginx/

[root@www nginx]# git clone https://github.com/loveshell/ngx_lua_waf.git

 

五、而後修改nginx添加配置,支持lua腳本地址,在http段位置:

lua_package_path "/opt/openresty/nginx/ngx_lua_waf/?.lua";  ###相關項目存放地址

lua_shared_dict limit 10m;                       ###存放limit表的大小

init_by_lua_file  /opt/openresty/nginx/ngx_lua_waf/init.lua; ###相應地址

access_by_lua_file /opt/openresty/nginx/ngx_lua_waf/waf.lua; ##相應地址

 

六、修改ngx_lua_waf相關配置:

[root@www ngx_lua_waf]# vim config.lua 

RulePath = "/opt/openresty/nginx/ngx_lua_waf/wafconf/"   ##指定相應位置

attacklog = "on"                            ##開啓日誌

logdir = "/opt/openresty/nginx/logs/hack/"           ##日誌存放位置

UrlDeny="on"                              ##是否開啓URL防禦

Redirect="on"                             ##地址重定向

CookieMatch="on"                           ##cookie攔截

postMatch="on"                            ##post攔截

whiteModule="on"                           ##白名單

black_fileExt={"php","jsp"}                        

ipWhitelist={"127.0.0.1"}                    ##白名單IP

ipBlocklist={"1.0.0.1"}                     ##黑名單IP

CCDeny="on"                             ##開啓CC防禦        

CCrate="100/60"                          ##60秒內容許同一個IP訪問100次

 

七、建立日誌存放目錄:

[root@www ngx_lua_waf]#mkdir /opt/openresty/nginx/logs/hack/

[root@www ngx_lua_waf]#chown -R nobody:nobody /opt/openresty/nginx/logs/hack/

 

八、啓動nginx測試:

[root@www logs]# /opt/openresty/nginx/sbin/nginx 

 

九、網頁訪問一條測試:

face/6HHjsZ4KasDNjpi7sj5TtEEWwesApXbD.png

 

十、壓力測試CC攻擊:

把congfig.lua的頻率改爲以下:

CCDeny="on"

CCrate="50/60"

測試結果:

[root@www ngx_lua_waf]# ab -c 100 -n 100 http://192.168.63.242/index.heml

This is ApacheBench, Version 2.3

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Licensed to The Apache Software Foundation, http://www.apache.org/

 

Benchmarking 192.168.63.242 (be patient).....done

 

 

Server Software:        openresty/1.11.2.2

Server Hostname:        192.168.63.242

Server Port:            80

 

Document Path:          /index.heml

Document Length:        2078 bytes

 

Concurrency Level:      100

Time taken for tests:   0.052 seconds

Complete requests:      100

Failed requests:        49      ###由於作了如今,因此這麼可能是失敗的。

 

 

處處已經構建成功了一套waf防護系統,很是感謝loveshell提供這麼棒的waf開源項目,還有春哥的openresty.

原文地址:http://www.roncoo.com/article/detail/126294

相關文章
相關標籤/搜索