nginx+lua實現簡單的waf網頁防火牆功能
- 2019 年 10 月 5 日
- 筆記
Nginx+Lua實現WAF
參考地址:http://www.2cto.com/Article/201303/198425.html
2016年8月2日
安裝LuaJIT
http://luajit.org/download/LuaJIT-2.0.4.tar.gz
tar xf LuaJIT-2.0.4.tar.gz
cd LuaJIT-2.0.4
make && make install 即可
下載ngx_devel_kit
https://codeload.github.com/simpl/ngx_devel_kit/zip/master
unzip ngx_devel_kit-master.zip
解壓後的路徑為:root/ngx_devel_kit-master
下載nginx_lua_module解壓
https://github.com/openresty/lua-nginx-module#readme
unzip lua-nginx-module-master.zip
cd lua-nginx-module-master
安裝nginx或給nginx打補丁
nginx -v 查看nginx的版本號
# nginx -v
nginx version: nginx/1.8.0
nginx -V 查看以前的編譯參數
# nginx -V
nginx version: nginx/1.8.0
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: –user=www –group=www –prefix=/usr/local/nginx –with-http_stub_status_module –with-http_ssl_module –with-http_spdy_module –with-http_gzip_static_module –with-ipv6 –with-http_sub_module –with-google_perftools_module
我這裡已經安裝過nginx1.8了。
那麼下面就是給nginx打補丁的事情了。如下:
進到nginx1.8的源代碼目錄下。執行下面的一系列命令:
# 導入環境變量,編譯
# exportLUAJIT_LIB=/usr/local/lib #這個很有可能不一樣
# exportLUAJIT_INC=/usr/local/include/luajit-2.0 #這個很有可能不一樣
# cd /home/tools/lnmp1.2-full/src/nginx-1.8.0
#./configure
–user=www –group=www
–prefix=/usr/local/nginx
–with-http_stub_status_module
–with-http_ssl_module
–with-http_spdy_module
–with-http_gzip_static_module
–with-ipv6
–with-http_sub_module
–with-google_perftools_module
–add-module=/root/ngx_devel_kit-master
–add-module=/root/lua-nginx-module-master
–with-ld-opt="-Wl,-rpath,$LUAJIT_LIB"
# make -j4 && make install
準備nginx的***日誌目錄
# mkdir -p /home/wwwlogs/attack
# chown www.www /home/wwwlogs/attack
# chmod -R 755 /home/wwwlogs/attack
安裝nginx的Lua_waf模塊
官方地址:https://github.com/loveshell/ngx_lua_waf
# wget https://codeload.github.com/loveshell/ngx_lua_waf/zip/master
# unzip ngx_lua_waf-master.zip
# cd ngx_lua_waf-master
# mkdir /usr/local/nginx/conf/waf
# cp -a ./ /usr/local/nginx/conf/waf
修改nginx的配置文件,在http段加入如下內容:
lua_package_path"/usr/local/nginx/conf/waf/?.lua";
lua_shared_dict limit 10m; 開啟攔截cc***必須加這條規則
init_by_lua_file /usr/local/nginx/conf/waf/init.lua;
access_by_lua_file /usr/local/nginx/conf/waf/waf.lua;
修改/usr/local/nginx/conf/waf/config.lua中如下2部分內容即可:
RulePath ="/usr/local/nginx/conf/waf/wafconf/"
attacklog = "on"
logdir ="/home/wwwlogs/attack"
UrlDeny="on"
Redirect="on"
CookieMatch="on"
postMatch="on"
whiteModule="on"
black_fileExt={"php","jsp"}
ipWhitelist={"127.0.0.1"}
ipBlocklist={"1.0.0.1"}
CCDeny="on"
CCrate="100/60"
配置文件說明:
RulePath = "/usr/local/nginx/conf/waf/wafconf/" –規則存放目錄
attacklog = "off" –是否開啟***信息記錄,需要配置logdir
logdir ="/usr/local/nginx/logs/hack/" –log存儲目錄,該目錄需要用戶自己新建,需要nginx用戶的可寫權限
UrlDeny="on" –是否攔截url訪問
Redirect="on" –是否攔截後重定向
CookieMatch = "on" –是否攔截cookie***
postMatch = "on" –是否攔截post***
whiteModule = "on" –是否開啟URL白名單
black_fileExt={"php","jsp"} –填寫不允許上傳文件後綴類型
ipWhitelist={"127.0.0.1"} –ip白名單,多個ip用逗號分隔
ipBlocklist={"1.0.0.1"} –ip黑名單,多個ip用逗號分隔
CCDeny="on" –是否開啟攔截cc***(需要nginx.conf的http段增加lua_shared_dict limit 10m;)
CCrate ="100/60" –設置cc***頻率,單位為秒. 默認1分鐘同一個IP只能請求同一個地址100次
html=[[Please go away~~]] –警告內容,可在中括號內自定義
備註:不要亂動雙引號,區分大小寫
重啟nginx
# nginx -t
# /etc/init.d/nginx restart 重啟nginx
惡意訪問測試
# curl http://xxxx/test.php?id=../etc/passwd
# curl http://192.168.2.12/index.php?cmd=phpinfo();
或者直接在網頁上請求
結果都是如下圖所示,被攔截了。
此外,在/home/wwwlogs/attack目錄下已經有日誌文件記錄下整個***的日誌了。
一些說明:
過濾規則在wafconf下,可根據需求自行調整,每條規則需換行,或者用|分割
args裏面的規則get參數進行過濾的
url是只在get請求url過濾的規則
post是只在post請求過濾的規則
whitelist是白名單,裏面的url匹配到不做過濾
user-agent是對user-agent的過濾規則
默認開啟了get和post過濾,需要開啟cookie過濾的,編輯waf.lua取消部分–注釋即可。
攔截到的非法請求,記錄在日誌文件名稱格式如下:虛擬主機名_sec.log
說明:
這玩意貌似只能防止一些簡單的sql注入類的語句,對於一些精心構造的惡意語句還是攔截不了的。
另外,我在公司的服務器上裝了它,後台客服反應會出現form表單中圖片無法上傳的情況。