使用Nginx+Lua實現的WAF
waf
使用Nginx+Lua實現自定義WAF(Web application firewall)
版權聲明
嚴重參考(照抄)https://github.com/loveshell/ngx_lua_waf
需求產生
由于原生態的Nginx的一些安全防護功能有限,就研究能不能自己編寫一個WAF,參考(照抄)Kindle大神的ngx_lua_waf,自己嘗試寫一個了,使用兩天時間,邊學Lua,邊寫。不過不是安全專業,只實現了一些比較簡單的功能:
功能列表:
- 支持IP白名單和黑名單功能,直接將黑名單的IP訪問拒絕。
- 支持URL白名單,將不需要過濾的URL進行定義。
- 支持User-Agent的過濾,匹配自定義規則中的條目,然后進行處理(返回403)。
- 支持CC攻擊防護,單個URL指定時間的訪問次數,超過設定值,直接返回403。
- 支持Cookie過濾,匹配自定義規則中的條目,然后進行處理(返回403)。
- 支持URL過濾,匹配自定義規則中的條目,如果用戶請求的URL包含這些,返回403。
- 支持URL參數過濾,原理同上。
- 支持日志記錄,將所有拒絕的操作,記錄到日志中去。
WAF實現
WAF一句話描述,就是解析HTTP請求(協議解析模塊),規則檢測(規則模塊),做不同的防御動作(動作模塊),并將防御過程(日志模塊)記錄下來。所以本文中的WAF的實現由五個模塊(配置模塊、協議解析模塊、規則模塊、動作模塊、錯誤處理模塊)組成。
Nginx + Lua部署
環境準備 [root@nginx-lua ~]# cd /usr/local/src 首先,現在Nginx安裝必備的Nginx和PCRE軟件包。
[root@nginx-lua src]# wget http://nginx.org/download/nginx-1.9.4.tar.gz [root@nginx-lua src]# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.37.tar.gz
其次,下載當前最新的luajit和ngx_devel_kit (NDK),以及春哥(章)編寫的lua-nginx-module
[root@nginx-lua src]# wget http://luajit.org/download/LuaJIT-2.0.4.tar.gz [root@nginx-lua src]# wget https://github.com/simpl/ngx_devel_kit/archive/v0.2.19.tar.gz [root@nginx-lua src]# wget https://github.com/openresty/lua-nginx-module/archive/v0.9.16.tar.gz
最后,創建Nginx運行的普通用戶 [root@nginx-lua src]# useradd -s /sbin/nologin -M www
解壓NDK和lua-nginx-module
[root@openstack-compute-node5 src]# tar zxvf v0.2.19 解壓后為ngx_devel_kit-0.2.19
[root@openstack-compute-node5 src]# tar zxvf v0.9.10解壓后為lua-nginx-module-0.9.16 安裝LuaJIT Luajit是Lua即時編譯器。
[root@openstack-compute-node5 src]# tar zxvf LuaJIT-2.0.3.tar.gz [root@openstack-compute-node5 src]# cd LuaJIT-2.0.3 [root@openstack-compute-node5 LuaJIT-2.0.3]# make && make install
安裝Nginx并加載模塊
[root@openstack-compute-node5 src]# tar zxvf nginx-1.9.4.tar.gz [root@openstack-compute-node5 src]# cd nginx-1.9.4 [root@openstack-compute-node5 nginx-1.9.4]# export LUAJIT_LIB=/usr/local/lib [root@openstack-compute-node5 nginx-1.9.4]# export LUAJIT_INC=/usr/local/include/luajit-2.0 [root@openstack-compute-node5 nginx-1.9.4]# ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-file-aio --with-http_dav_module --add-module=../ngx_devel_kit-0.2.19/ --add-module=../lua-nginx-module-0.9.16/ --with-pcre=/usr/local/src/pcre-8.37 [root@openstack-compute-node5 nginx-1.5.12]# make -j2 && make install [root@openstack-compute-node5 ~]# ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2
如果不創建符號鏈接,可能出現以下異常: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory
測試安裝
安裝完畢后,下面可以測試安裝了,修改nginx.conf 增加第一個配置。
location /hello {
default_type 'text/plain';
content_by_lua 'ngx.say("hello,lua")';
}
[root@openstack-compute-node5 ~]# /usr/local/nginx-1.9.4/sbin/nginx –t
[root@openstack-compute-node5 ~]# /usr/local/nginx-1.9.4/sbin/nginx 然后訪問http://xxx.xxx.xxx.xxx/hello,如果出現hello,lua。表示安裝完成,然后就可以。
注意:也可以直接部署春哥的開源項目:https://github.com/openresty
WAF部署
#git clone https://github.com/unixhot/waf.git
#cp -a ./waf/waf /usr/local/nginx/conf/
修改Nginx的配置文件,加入以下配置。注意路徑,同時WAF日志默認存放在/tmp/日期_waf.log
#WAF
lua_shared_dict limit 50m;
lua_package_path "/usr/local/nginx/conf/waf/?.lua";
init_by_lua_file "/usr/local/nginx/conf/waf/init.lua";
access_by_lua_file "/usr/local/nginx/conf/waf/access.lua";
[root@openstack-compute-node5 ~]# /usr/local/nginx-1.5.12/sbin/nginx –t
[root@openstack-compute-node5 ~]# /usr/local/nginx-1.5.12/sbin/nginx 廣告:
51CTO在線視頻:趙班長出品,絕不坑人!http://edu.51cto.com/lecturer/index/user_id-1110045.html