最近上新項目,搭建nginx的時候為保證安全,決定安裝下waf模組,以下是具體步驟,首先下載需要的安裝包,進行每個安裝:
wget http://luajit.org/download/LuaJIT-2.0.3.tar.gztar -zxvf LuaJIT-2.0.3.tar.gzcd LuaJIT-2.0.3makemake install
然後下載nginx,
wget http://nginx.org/download/nginx-1.7.6.tar.gztar -zxvf nginx-1.7.6.tar.gz
下載ngx_devel_kit,
wget https://github.com/simpl/ngx_devel_kit/archive/v0.2.19.tar.gz –no-check-certificatetar -zxvf v0.2.19.tar.gz
下載nginx_lua_module
wget https://github.com/openresty/lua-nginx-module/archive/v0.9.13rc1.tar.gz –no-check-certificatetar -zxvf v0.9.13rc1.tar.gzcd nginx-1.7.6
安裝pcre,
tar -zxvf pcre-8.30.tar.gzcd pcre-8.30./configuremakemake install
安裝openssl,
tar -zxvf openssl-1.0.0.tar.gz
安裝ngx_cache_purge模組:
tar -zxvf ngx_cache_purge-1.3.tar.gz
安裝zlib, yum install zlib zlib-devel
設定環境變數,進行編譯,
export LUAJIT_LIB=/usr/local/libexport LUAJIT_INC=/usr/local/include/luajit-2.0./configure –prefix=/usr/local/nginx –with-http_stub_status_module –with-http_ssl_module –add-module=/root/ngx_devel_kit-0.2.19 –add-module=/root/lua-nginx-module-0.9.13rc1 –with-http_ssl_module –with-openssl=/root/openssl-1.0.0 –add-module=/root/ngx_cache_purge-1.3 –with-pcre=/root/pcre-8.30 –with-ld-opt=”-Wl,-rpath,$LUAJIT_LIB”make -j2make install
建立目錄存放攻擊日誌:
mkdir -p /data/logs/hack/chown -R nobody:nobody /data/logs/hack/chmod -R 755 /data/logs/hack/
下載waf,
wget https://github.com/loveshell/ngx_lua_waf/archive/master.zip –no-check-certificateunzip master.zipcd ngx_lua_waf-master/mkdir /usr/local/nginx/conf/wafmv * /usr/local/nginx/conf/waf/cd /usr/local/nginx/conf/vi nginx.conf
在http 段加入:
lua_package_path “/usr/local/nginx/conf/waf/?.lua”;
lua_shared_dict limit 10m;
init_by_lua_file /usr/local/nginx/conf/waf/init.lua;
access_by_lua_file /usr/local/nginx/conf/waf/waf.lua;
啟動前運行: ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2 否則會找不到這個模組報錯。
過濾規則在wafconf下,可根據需求自行調整,每條規則需換行,或者用|分割
global是全域過濾檔案,裡面的規則對post和get都過濾
get是只在get請求過濾的規則
post是只在post請求過濾的規則
whitelist是白名單,裡面的url匹配到不做過濾
user-agent是對user-agent的過濾規則