nginx+lua實現簡單的waf網頁防火牆功能


原文:http://www.2cto.com/net/201608/534272.html

安裝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();

或者直接在網頁上請求

結果都是如下圖所示,被攔截了。

 

wKioL1ekf3_xPgUdAAB7idVmlh0081.png
 

 

此外,在/home/wwwlogs/attack目錄下已經有日志文件記錄下整個攻擊的日志了。

 

wKioL1ekf4Cj-cLBAAAaAgoKr2M737.png

 

 

wKiom1ekf37ApnUsAABXpW02Dew226.png

 

一些說明:

過濾規則在wafconf下,可根據需求自行調整,每條規則需換行,或者用|分割

args里面的規則get參數進行過濾的

url是只在get請求url過濾的規則

post是只在post請求過濾的規則

whitelist是白名單,里面的url匹配到不做過濾

user-agent是對user-agent的過濾規則

默認開啟了get和post過濾,需要開啟cookie過濾的,編輯waf.lua取消部分--注釋即可。

攔截到的非法請求,記錄在日志文件名稱格式如下:虛擬主機名_sec.log

說明:

這玩意貌似只能防止一些簡單的sql注入類的語句,對於一些精心構造的惡意語句還是攔截不了的。

另外,我在公司的服務器上裝了它,后台客服反應會出現form表單中圖片無法上傳的情況。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM