WAF主要分為硬件WAF和軟件防火墻,硬件WAF如綠盟的NSFOCUS Web Application Firewall,軟件防火墻比較有名的是ModSecurity,再就是代碼級別的ngx_lua_waf。下面談談個人對幾款防火墻的理解:
硬件WAF個人覺得只適合在那種訪問量較少的網站,比如政府網站,公司的介紹網站等等。硬件WAF的的優勢在于規則有專門的安全公司維護,管理方便,但也存在一個致命的弱點,使用傳統的方式來解包到應用層對性能的需求較高,而且當訪問量很大的時候延時比較大,這樣在高并發訪問的情況下要使用硬件WAF就只能使用很多臺WAF了,這樣成本就非常高了;還有一個在接觸過程中發現的問題,就是硬件WAF的規則雖然多而且有人維護,但是一般公司很難敢直接開啟阻難,很多都是只記錄,并不能阻難,這樣WAF的意義就變得小多了。
ModSecurity在網上的評價都是很高的,性能高,規則全。最開始我研究的也是這款WAF,但是在實際使用過程中發現問題,就是在高并發的情況下,運行一段時間,會出現內存飆升,而且不下來的問題。這個問題再ModSecurity的討論論壇上面也發現了有人提出這樣的問題,但一直未解決(https://github.com/SpiderLabs/ModSecurity/issues/785)。針對于規則全的優勢,一般使用者也不敢直接開啟所有的規則攔截,畢竟每個公司的業務不同,規則也不可能直接套用。
基于高性能,低成本的想法,[email protected]_lua_waf,經過實際使用下來,確實性能極好,由于LUA語言的性能是接近于C的,而且ngx_lua_module本身就是基于為nginx開發的高性能的模塊。安全寶的云 WAF,以及cloudflare的新waf也是基于此模塊使用LUA開發的。結合ModSecurity的思路,[email protected]_lua_waf來開發適合自己用的WAF,[email protected],再此也表示感謝。
WAF開發過程中的主要方向為:
WAF的主要功能為:
WAF的總體檢測思路:
圖示如下:
規則說明:
比如規則:{"rule00001","rules","args|post|cookie",[[../]],"deny","logon"},
rule00001:規則編號,隨意寫
rules:規則名稱,如xssrules,隨意寫
args|post|cookie|header:檢測位置,|表示或,args,post,cookie,header可多選
../:匹配的正則表達式,標準PCRE正則
deny:處理方式,可選deny ,allow
logon:日志記錄與否,可選logon,logoff
#!bash
--在nginx.conf的HTTP中加入
--lua_shared_dict limit 50m; 根據主機內存調合適的值
--lua_shared_dict iplimit 20m;
--lua_shared_dict blockiplimit 5m;
-------------------------------------------------------------
CCDeny="on" --cc攻擊開關
CCrate="60/60"--基于url的計數 次/秒
ipCCrate="600/60"--基于ip的計數 次/秒
-------------------------------------------------
ccdenyrules={"ccdeny1","ccdeny","","","","logon"}
function gethost()
host = ngx.var.host
if host == nil or type(host) ~= "string" then
math.randomseed(os.time())
host = "nohost"..math.random()
end
return host
end
function denycc(clientdata)
if CCDeny=="on" then
local uri=clientdata[2]
local host = gethost()
CCcount=tonumber(string.match(CCrate,'(.*)/'))
CCseconds=tonumber(string.match(CCrate,'/(.*)'))
ipCCcount=tonumber(string.match(ipCCrate,'(.*)/'))
ipCCseconds=tonumber(string.match(ipCCrate,'/(.*)'))
local token = clientdata[1]..host..uri
local clientip = clientdata[1]..host
local limit = ngx.shared.limit
local iplimit = ngx.shared.iplimit
local blockiplimit = ngx.shared.blockiplimit
local req,_=limit:get(token)
local ipreq,_=iplimit:get(clientip)
local blockipreq,_=blockiplimit:get(clientip)
if blockipreq or ipreq then
if blockipreq or req then
if blockipreq or req >= CCcount or ipreq >= ipCCcount then
log(ccdenyrules,clientdata)
blockiplimit:set(clientip,1,300)
ngx.exit(403)
return true
else
limit:incr(token,1)
iplimit:incr(clientip,1)
end
else
limit:set(token,1,CCseconds)
end
else
iplimit:set(clientip,1,ipCCseconds)
end
end
return false
end
可以很靈活的實現復雜的控制
比如我在我的個人網站上面就使用了這樣一個功能,后臺頁面需要特定useragent才能訪問。
代碼如下:
#!bash
--特定頁面容許特定useragent可訪問
function houtai(clientdata)
if stringmatch(clientdata[2],"wp-admin") then
if stringmatch(clientdata[4],"hahahaha") then
return
else
ngx.exit(403)
return
end
else
return
end
end
可以測試http://www.zhangsan.me/wp-admin/
只有在特定的useragent才可以訪問此頁面,否則報403錯誤。
源碼下載地址為:http://pan.baidu.com/s/18QQya
環境搭建就參考:http://wiki.nginx.org/HttpLuaModule#Installation
waf使用主要就是配置config.lua
SecRuleEngine = "on" attacklog = "on" logpath = "/home/waflog/"
分別為引擎是否開啟 是否記錄日志 日志的存儲路徑 日志的存儲路徑需要給予nginx運行用戶的讀寫權限
寫的很簡單,大牛勿噴,希望大家多提建議。
1. https://github.com/loveshell/ngx_lua_waf
2. http://wiki.nginx.org/HttpLuaModule
3. http://www.freebuf.com/tools/54221.html
……