什么是WAF
Web應用防護系統(也稱為:網站應用級入侵防御系統。英文:Web Application Firewall,簡稱: WAF)。利用國際上公認的一種說法:Web應用防火牆是通過執行一系列針對HTTP/HTTPS的安全策略來專門為Web應用提供保護的一款產品。
實現WAF
兩種方式
- 使用nginx+lua來實現WAF,須在編譯nginx的時候配置上lua
- 部署OpenResty,不需要在編譯nginx的時候指定lua
功能列表
- 支持IP白名單和黑名單功能,直接將黑名單的IP訪問拒絕。
- 支持URL白名單,將不需要過濾的URL進行定義。
- 支持User-Agent的過濾,匹配自定義規則中的條目,然后進行處理(返回403)。
- 支持CC攻擊防護,單個URL指定時間的訪問次數,超過設定值,直接返回403。
- 支持Cookie過濾,匹配自定義規則中的條目,然后進行處理(返回403)。
- 支持URL過濾,匹配自定義規則中的條目,如果用戶請求的URL包含這些,返回403。
- 支持URL參數過濾,原理同上。
- 支持日志記錄,將所有拒絕的操作,記錄到日志中去。
- 日志記錄為JSON格式,便於日志分析,例如使用ELKStack進行攻擊日志收集、存儲、搜索和展示
具體操作
采用第二種方式實現WAF較為方便
以下操作的前提是:Centos 7.2 系統
部署OpenResty
# 安裝依賴包
yum install -y readline-devel pcre-devel openssl-devel zlib-devel
cd /usr/local/src
# 下載編譯安裝openresty
wget https://openresty.org/download/openresty-1.15.8.1.tar.gz
tar -zxv -f openresty-1.15.8.1.tar.gz
cd openresty-1.15.8.1
./configure --with-luajit --with-http_stub_status_module --with-pcre --with-pcre-jit
gmake && gmake install
使用yum方式安裝,推薦使用
同時結合配置yum下載的rpm包不刪除,可以保留安裝包
yum install yum-utils
yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo
yum install openresty
測試openresty安裝,在nginx的配置文件加入
server {
location /hello {
default_type text/html;
content_by_lua_block {
ngx.say("HelloWorld")
}
}
}
保存退出,訪問ip/hellp
,出現HelloWorld證明安裝成功。
部署WAF
# 從github克隆waf
git clone https://github.com/unixhot/waf.git
cp -a ./waf/waf /usr/local/openresty/nginx/conf/
waf目錄:/usr/local/openresty/nginx/conf/waf
lua配置文件:/usr/local/openresty/nginx/conf/waf/config.lua
Waf的ip黑名單:/usr/local/openresty/nginx/conf/waf/rule-config/blackip.rule
Waf的ip白名單:/usr/local/openresty/nginx/conf/waf/rule-config/whiteip.rule
Waf的規則存放目錄:/usr/local/openresty/nginx/conf/waf/rule-config
修改nginx的配置文件,在http里加入
#WAF
lua_shared_dict limit 10m;
lua_package_path "/usr/local/openresty/nginx/conf/waf/?.lua";
init_by_lua_file "/usr/local/openresty/nginx/conf/waf/init.lua";
access_by_lua_file "/usr/local/openresty/nginx/conf/waf/access.lua";
保存退出。
#測試配置
/usr/local/openresty/nginx/sbin/nginx –t
#重新加載配置
/usr/local/openresty/nginx/sbin/nginx reload
至此,WAF已經配置成功.
測試WAF
-
模擬sql注入,訪問
ip/test.sql
,看是否出現攔截 -
模擬參數檢測,訪問
ip/?id=test
,看是否出現攔截 -
黑白名單
#白名單路徑 /usr/local/openresty/nginx/conf/waf/rule-config/whiteip.rule #黑名單路徑 /usr/local/openresty/nginx/conf/waf/rule-config/blackip.rule
### WAF配置文件詳細說明
#路徑
/usr/local/openresty/nginx/conf/waf/config.lua
#詳細說明,lua文件中,--為注釋
--WAF config file,enable = "on",disable = "off"
--waf status waf狀態是否開啟
config_waf_enable = "on"
--log dir 日志位置,json格式,根據實際情況修改
config_log_dir = "/tmp"
--rule setting 匹配規則地址,根據實際情況修改
config_rule_dir = "/usr/local/openresty/nginx/conf/waf/rule-config"
--enable/disable white url 是否開啟url檢測
config_white_url_check = "on"
--enable/disable white ip 是否開啟白名單ip檢測
config_white_ip_check = "on"
--enable/disable block ip 是否開啟黑名單ip檢測
config_black_ip_check = "on"
--enable/disable url filtering 是否開啟url過濾
config_url_check = "on"
--enalbe/disable url args filtering 是否開啟參數檢測
config_url_args_check = "on"
--enable/disable user agent filtering 是否開啟ua檢測
config_user_agent_check = "on"
--enable/disable cookie deny filtering 是否開啟cookie檢測
config_cookie_check = "on"
--enable/disable cc filtering 是否開啟CC檢測
config_cc_check = "on"
--cc rate the xxx of xxx seconds 允許一個ip60秒內只能訪問10次
config_cc_rate = "10/60"
--enable/disable post filtering 是否開啟post檢測
config_post_check = "on"
--config waf output redirect/html 攔截開啟跳轉還是一個html頁面
config_waf_output = "html"
--if config_waf_output ,setting url 跳轉地址和輸出頁面
config_waf_redirect_url = "https://www.gov.cn/"
--訪問被攔截后出現的頁面
config_output_html=[[
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="zh-cn" />
<title>網站防火牆</title>
</head>
<body>
<h1 align="center"> 您的行為已違反本網站相關規定,注意操作規范。
</body>
</html>
]]
一些說明
過濾規則在waf/rule-config/下,可根據需求自行調整,每條規則需換行,或者用|分割
args里面的規則get參數進行過濾的
url是只在get請求url過濾的規則
post是只在post請求過濾的規則
whitelist是白名單,里面的url匹配到不做過濾
blackip.rule是黑名單
user-agent是對user-agent的過濾規則
默認沒有開啟post參數過濾,在文件access.lua中取消注釋即可
WAF日志默認存放在/tmp/日期_waf.log
config.lua文件中的提示信息:
放在兩個中括號中
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>網站防火牆</title>
<style>
p {
line-height:20px;
}
ul{ list-style-type:none;}
li{ list-style-type:none;}
</style>
</head>
<body style=" padding:0; margin:0; font:14px/1.5 Microsoft Yahei, 宋體,sans-serif; color:#555;">
<div style="margin: 0 auto; width:1000px; padding-top:70px; overflow:hidden;">
<div style="width:600px; float:left;">
<div style=" height:40px; line-height:40px; color:#fff; font-size:16px; overflow:hidden; background:#6bb3f6; padding-left:20px;">網站防火牆 </div>
<div style="border:1px dashed #cdcece; border-top:none; font-size:14px; background:#fff; color:#555; line-height:24px; height:220px; padding:20px 20px 0 20px; overflow-y:auto;background:#f3f7f9;">
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-weight:600; color:#fc4f03;">您的請求帶有不合法參數,已被網站管理員設置攔截!</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">可能原因:您提交的內容包含危險的攻擊請求</p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:1; text-indent:0px;">如何解決:</p>
<ul style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; -qt-list-indent: 1;"><li style=" margin-top:12px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">1)檢查提交內容;</li>
<li style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">2)如網站托管,請聯系空間提供商;</li>
<li style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">3)普通網站訪客,請聯系網站管理員;</li></ul>
</div>
</div>
</div>
</body></html>