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

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


waf的作用:

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

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


好了步驟開始:


1、安裝Luagit:

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

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

# cd LuaJIT-2.1.0-beta1

# make

# make install

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


2、安裝openresty:

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

gmake

gmake install


3、測試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


4、下載開源項目:

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

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


5、然後修改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; ##相應地址


6、修改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次


7、創建日誌存放目錄:

[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/


8、啓動nginx測試:

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


9、網頁訪問一條測試:

face/6HHjsZ4KasDNjpi7sj5TtEEWwesApXbD.png


10、壓力測試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.


後期起到更多的lua + nginx學習成果,第一篇完成,希望我們後期都能寫出自己的waf防火牆。


http://www.roncoo.com/article/detail/126294


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章