Web應用防禦系統(也稱爲:網站應用級入侵防護系統。英文:Web Application Firewall,簡稱: WAF)。利用國際上公認的一種說法:Web應用防火牆是經過執行一系列針對HTTP/HTTPS的安全策略來專門爲Web應用提供保護的一款產品。html
兩種方式nginx
功能列表git
採用第二種方式實現WAF較爲方便github
如下操做的前提是:Centos 7.2 系統sql
# 安裝依賴包 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包不刪除,能夠保留安裝包shell
yum install yum-utils yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo yum install openresty
測試openresty安裝,在nginx的配置文件加入json
server { location /hello { default_type text/html; content_by_lua_block { ngx.say("HelloWorld") } } }
保存退出,訪問ip/hellp
,出現HelloWorld證實安裝成功。centos
# 從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";
保存退出。bash
#測試配置 /usr/local/openresty/nginx/sbin/nginx –t #從新加載配置 /usr/local/openresty/nginx/sbin/nginx reload
至此,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>