WAF(Web Application Firewall),中文名稱叫作「Web應用防火牆php
WAF的定義是這樣的:Web應用防火牆是經過執行一系列針對HTTP/HTTPS的安全策略來專門爲Web應用提供保護的一款產品,經過從上面對WAF的定義中,咱們能夠很清晰地瞭解到:WAF是一種工做在應用層的、經過特定的安全策略來專門爲Web應用提供安全防禦的產品。
用途:
用於過濾post,get,cookie方式常見的web攻擊
防止sql注入,本地包含,部分溢出,fuzzing測試,xss,SSRF等web攻擊
防止svn/備份之類文件泄漏
防止ApacheBench之類壓力測試工具的攻擊
屏蔽常見的掃描黑客工具,掃描器
屏蔽異常的網絡請求
屏蔽圖片附件類目錄php執行權限
防止webshell上傳html
安裝前準備:python
系統:CentOS6.5nginx
nginx:/opt/nginx-1.4.4(nginx我以前已經安裝,在/opt/nginx/目錄,如今只是安裝 Nginx 支持 WAF 防禦功能實戰 )c++
yum -y install gcc gcc-c++ autoconf automake make
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-develgit
開始安裝:github
1.安裝LuaJIT 2.0(推薦使用lujit2.1作lua支持)web
下載luajit 2.0並安裝 http://luajit.org/download.html tar zxf LuaJIT-2.0.0.tar.gz cd LuaJIT-2.0.0 make && make install 注:lib和include是直接放在/usr/local/lib和usr/local/include
2.安裝ngx_devel_kitsql
下載ngx_devel_kit解壓 https://github.com/simpl/ngx_devel_kit/tags wget https://github.com/simpl/ngx_devel_kit/archive/v0.2.18.tar.gz --no-check-certificate tar -zxvf v0.2.18
3.安裝nginx_lua_moduleshell
下載nginx_lua_module解壓 https://github.com/chaoslawful/lua-nginx-module/tags wget https://github.com/chaoslawful/lua-nginx-module/archive/v0.7.18rc2.tar.gz --no-check-certificate tar -zxvf v0.7.18rc2
4.導入環境變量,編譯
export LUAJIT_LIB=/usr/local/lib export LUAJIT_INC=/usr/local/include/luajit-2.0
5.進入nginx源碼目錄對nginx進行從新編譯,將waf功能編譯進nginx中:
[root@localhost ~]# cd /root/soft/nginx-1.4.4 [root@localhost nginx-1.4.4]# ./configure --prefix=/opt/nginx/ --with-http_stub_status_module --with-http_realip_module --with-http_ssl_module \ --add-module=/root/soft/ngx_devel_kit-0.2.18 \ --add-module=/root/soft/lua-nginx-module-0.7.18rc2 \ --with-ld-opt="-Wl,-rpath,$LUAJIT_LIB" [root@localhost nginx-1.4.4]#make -j2 [root@localhost nginx-1.4.4]#make install
6.新建/opt/nginx/logs/hack/目錄攻擊日誌,並賦予nginx用戶對該目錄的寫入權限.
mkdir -p /opt/nginx/logs/hack/ www帳戶是跑nginx服務的用戶 chown -R www:www /opt/nginx/logs/hack/ chmod -R 755 /opt/nginx/logs/hack/
至此nginx支持WAF防禦功能已經搭建完成!
nginx安裝路徑假設爲:/opt/nginx/conf/
把ngx_lua_waf模塊下載到conf目錄下,解壓命名爲waf
[root@oracle-demo conf]# mv ngx_lua_waf-master /opt/nginx/conf/waf
ngx_lua_waf下載地址:https://github.com/loveshell/ngx_lua_waf
在nginx.conf的http段添加:
http { ... lua_package_path "/opt/nginx/conf/waf/?.lua"; lua_shared_dict limit 10m; init_by_lua_file /opt/nginx/conf/waf/init.lua; access_by_lua_file /opt/nginx/conf/waf/waf.lua; ... }
配置config.lua裏的waf規則目錄(通常在wafconf/目錄下)
RulePath = "/opt/nginx/conf/waf/wafconf/"
絕對路徑若有變更,需對應修改
而後重啓nginx便可
部署完畢能夠嘗試以下命令:
curl http://xxxx/test.php?id=../etc/passwd 返回"Please go away~~"字樣,說明規則生效。
#vim test.php
<?phpinfo()?>
RulePath = "/opt/nginx/conf/waf/wafconf/" --規則存放目錄 attacklog = "off" --是否開啓攻擊信息記錄,須要配置logdir logdir = "/opt/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~~]] --警告內容,可在中括號內自定義 備註:不要亂動雙引號,區分大小寫
過濾規則在wafconf下,可根據需求自行調整,每條規則需換行,或者用|分割
args 裏面的規則get參數進行過濾的
url 是隻在get請求url過濾的規則
post 是隻在post請求過濾的規則
whitelist 是白名單,裏面的url匹配到不作過濾
user-agent是對user-agent的過濾規則
默認開啓了get和post過濾,須要開啓cookie過濾的,編輯waf.lua取消部分--註釋便可