一蚕苇、了解WAF
1.1 什么是WAF
Web應用防護系統(tǒng)(也稱:網(wǎng)站應用級入侵防御系統(tǒng) 恬涧。英文:Web Application Firewall恢氯,簡稱: WAF)。利用國際上公認的一種說法:Web應用 防火墻 是通過執(zhí)行一系列針對HTTP/HTTPS的 安全策略 來專門為Web應用提供保護的一款產品席噩。
1.2 WAF的功能
支持IP白名單和黑名單功能,直接將黑名單的IP訪問拒絕贤壁。
支持URL白名單悼枢,將不需要過濾的URL進行定義。
支持User-Agent的過濾脾拆,匹配自定義規(guī)則中的條目馒索,然后進行處理(返回403)。
支持CC攻擊防護名船,單個URL指定時間的訪問次數(shù)双揪,超過設定值,直接返回403包帚。
支持Cookie過濾渔期,匹配自定義規(guī)則中的條目,然后進行處理(返回403)渴邦。
支持URL過濾疯趟,匹配自定義規(guī)則中的條目,如果用戶請求的URL包含這些谋梭,返回403信峻。
支持URL參數(shù)過濾,原理同上瓮床。
支持日志記錄盹舞,將所有拒絕的操作产镐,記錄到日志中去
1.3 WAF的特點
異常檢測協(xié)議?
Web應用防火墻會對HTTP的請求進行異常檢測,拒絕不符合HTTP標準的請求踢步。并且癣亚,它也可以只允許HTTP協(xié)議的部分選項通過,從而減少攻擊的影響范圍获印。甚至述雾,一些Web應用防火墻還可以嚴格限定HTTP協(xié)議中那些過于松散或未被完全制定的選項。
增強的輸入驗證?
增強輸入驗證兼丰,可以有效防止網(wǎng)頁篡改玻孟、信息泄露、木馬植入等惡意網(wǎng)絡入侵行為鳍征。從而減小Web服務器被攻擊的可能性黍翎。
及時補丁?
修補Web安全漏洞,是Web應用開發(fā)者最頭痛的問題艳丛,沒人會知道下一秒有什么樣的漏洞出現(xiàn)匣掸,會為Web應用帶來什么樣的危害。WAF可以為我們做這項工作了——只要有全面的漏洞信息WAF能在不到一個小時的時間內屏蔽掉這個漏洞质礼。當然旺聚,這種屏蔽掉漏洞的方式不是非常完美的,并且沒有安裝對應的補丁本身就是一種安全威脅眶蕉,但我們在沒有選擇的情況下砰粹,任何保護措施都比沒有保護措施更好。
基于規(guī)則的保護和基于異常的保護?
基于規(guī)則的保護可以提供各種Web應用的安全規(guī)則造挽,WAF生產商會維護這個規(guī)則庫碱璃,并時時為其更新。用戶可以按照這些規(guī)則對應用進行全方面檢測饭入。還有的產品可以基于合法應用數(shù)據(jù)建立模型嵌器,并以此為依據(jù)判斷應用數(shù)據(jù)的異常。但這需要對用戶企業(yè)的應用具有十分透徹的了解才可能做到谐丢,可現(xiàn)實中這是十分困難的一件事情爽航。
狀態(tài)管理?
WAF能夠判斷用戶是否是第一次訪問并且將請求重定向到默認登錄頁面并且記錄事件。通過檢測用戶的整個操作行為我們可以更容易識別攻擊乾忱。狀態(tài)管理模式還能檢測出異常事件(比如登陸失敿フ洹),并且在達到極限值時進行處理窄瘟。這對暴力攻擊的識別和響應是十分有利的衷佃。
其他防護技術?
WAF還有一些安全增強的功能,可以用來解決WEB程序員過分信任輸入數(shù)據(jù)帶來的問題蹄葱。比如:隱藏表單域保護氏义、抗入侵規(guī)避技術锄列、響應監(jiān)視和信息泄露保護。
1.3WAF與網(wǎng)絡防火墻的區(qū)別
網(wǎng)絡防火墻作為訪問控制設備惯悠,主要工作在OSI模型三邻邮、四層,基于IP報文進行檢測吮螺。只是對端口做限制饶囚,對TCP協(xié)議做封堵帕翻。其產品設計無需理解HTTP會話鸠补,也就決定了無法理解Web應用程序語言如HTML、SQL語言嘀掸。因此紫岩,它不可能對HTTP通訊進行輸入驗證或攻擊規(guī)則分析。針對Web網(wǎng)站的惡意攻擊絕大部分都將封裝為HTTP請求睬塌,從80或443端口順利通過防火墻檢測泉蝌。?
一些定位比較綜合、提供豐富功能的防火墻揩晴,也具備一定程度的應用層防御能力勋陪,如能根據(jù)TCP會話異常性及攻擊特征阻止網(wǎng)絡層的攻擊,通過IP分拆和組合也能判斷是否有攻擊隱藏在多個數(shù)據(jù)包中硫兰,但從根本上說他仍然無法理解HTTP會話诅愚,難以應對如SQL注入、跨站腳本劫映、cookie竊取违孝、網(wǎng)頁篡改等應用層攻擊。?
web應用防火墻能在應用層理解分析HTTP會話泳赋,因此能有效的防止各類應用層攻擊雌桑,同時他向下兼容,具備網(wǎng)絡防火墻的功能祖今。
OpenResty由Nginx核心加很多第三方模塊組成校坑,默認集成了Lua開發(fā)環(huán)境,使得Nginx可以作為一個Web Server使用千诬。
借助于Nginx的事件驅動模型和非阻塞IO耍目,可以實現(xiàn)高性能的Web應用程序。
而且OpenResty提供了大量組件如Mysql大渤、Redis制妄、Memcached等等,使在Nginx上開發(fā)Web應用更方便更簡單泵三。
以下是整理的Nginx+Lua架構思維導圖:
二耕捞、使用openResty配置waf防火墻,不需要編譯nginx
①安裝依賴包和創(chuàng)建nginx運行的普通用戶
[root@linux-node1 ~]# yum install -y readline-devel pcre-devel openssl-devel
[root@linux-node1 src]# useradd -s /sbin/nologin -M www
②下載當前最新的luajit衔掸,并編譯
[root@linux-node1 ~]# cd /usr/local/src/[root@linux-node1 src]#wget http://luajit.org/download/LuaJIT-2.1.0-beta3
[root@linux-node1 src]# tar -xzf LuaJIT-2.1.0-beta3?
[root@linux-node1 src]# cd LuaJIT-2.1.0-beta3
[root@linux-node1 LuaJIT-2.1.0-beta3]# make && make install
[root@linux-node1 LuaJIT-2.1.0-beta3]# export LUAJIT_LIB=/usr/local/lib
[root@linux-node1 LuaJIT-2.1.0-beta3]# export LUAJIT_INC=/usr/local/include/luajit-2.1
[root@linux-node1 ~]# ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2 ?
?#一定創(chuàng)建此軟連接,否則會報錯如果不創(chuàng)建符號鏈接俺抽,可能出現(xiàn)以下異常: errorwhileloading shared libraries: libluajit-5.1.so.2: cannotopenshared object file: No such fileordirectory
③下載并編譯安裝openresty
[root@linux-node1 ~]#cd /usr/local/src
[root@linux-node1 src]# wget https://openresty.org/download/openresty-1.13.6.2.tar.gz
[root@linux-node1 src]#tar -zxf openresty-1.13.6.2.tar.gz
[root@linux-node1 src]#cd openresty-1.13.6.2
[root@linux-node1 openresty-1.13.6.2]# ./configure --prefix=/usr/local/openresty \--user=www \--group=www \--with-luajit \--with-http_v2_module \--with-http_stub_status_module \--with-http_ssl_module \--with-http_gzip_static_module \--with-ipv6 --with-http_sub_module \--with-pcre \--with-pcre-jit \--with-file-aio \--with-http_dav_module
[root@linux-node1 openresty-1.13.6.2]#gmake && gmake install
④測試openresty安裝
[root@linux-node1 ~]#vim /usr/local/openresty/nginx/conf/nginx.confserver?
{? ? location /hello?
????{ default_type text/html; ? ? ? ? ? ?
????????content_by_lua_block {? ? ? ? ? ? ? ??
????????????????ngx.say("HelloWorld")? ? ? ? ? ??
????????}? ? ? ?
?}
}
⑤測試并啟動nginx
[root@linux-node1 ~]#/usr/local/openresty/nginx/sbin/nginx -t
[root@linux-node1 ~]#/usr/local/openresty/nginx/sbin/nginx
3敞映、WAF部署?
①在github上克隆下代碼
[root@linux-node1 ~]#git clone https://github.com/unixhot/waf.git[root@linux-node1 ~]#cp -a ./waf/waf /usr/local/openresty/nginx/conf/
②修改Nginx的配置文件,加入(http字段)以下配置磷斧。注意路徑振愿,同時WAF日志默認存放在/tmp/日期_waf.log
[root@linux-node1 ~]# cd /usr/local/openresty/nginx/conf
[root@linux-node1 conf]# vim nginx.conf
#WAFlua_shared_dict limit50m;
#防cc使用字典,大小50
Mlua_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";
[root@linux-node1 ~]# /usr/local/openresty/nginx/sbin/nginx –t
[root@linux-node1 ~]# /usr/local/openresty/nginx/sbin/nginx -s reload
③根據(jù)日志記錄位置弛饭,創(chuàng)建日志目錄
[root@linux-node1 ~]#mkdir /tmp/waf_logs[root@linux-node1 ~]#chown nginx.nginx /tmp/waf_logs
備注:
我已經將我們生產環(huán)境中的nginx+waf的配置文件上次上去冕末,下載鏈接http://download.csdn.net/detail/m0_37886429/9869230
4、waf的模塊?
①配置模塊?
waf安裝好以后侣颂,不要直接上生產档桃,而是先記錄日志,不做任何動作憔晒。確定wafF不產生誤殺?
config.lua配置模塊
[root@linux-node1 waf]# pwd/usr/local/openresty/nginx/conf/waf
[root@linux-node2 waf]# cat config.lua--WAF config file,enable = "on",disable = "off" --waf status? ? config_waf_enable ="on"
#是否開啟配置--log dir config_log_dir ="/tmp/waf_logs"
#日志記錄地址--rule setting config_rule_dir ="/usr/local/nginx/conf/waf/rule-config"
#匹配規(guī)則縮放地址--enable/disable white url config_white_url_check ="on"
#是否開啟url檢測--enable/disable white ip config_white_ip_check ="on"
#是否開啟IP白名單檢測--enable/disable block ip config_black_ip_check ="on"
#是否開啟ip黑名單檢測--enable/disable url filtering config_url_check ="on"
#是否開啟url過濾--enalbe/disable url args filtering config_url_args_check ="on"
#是否開啟參數(shù)檢測--enable/disable user agent filtering config_user_agent_check ="on"
#是否開啟ua檢測--enable/disable cookie deny filtering config_cookie_check ="on"
#是否開啟cookie檢測--enable/disable cc filtering config_cc_check ="on"
#是否開啟防cc攻擊--cc rate the xxx of xxx seconds config_cc_rate ="10/60"
#允許一個ip60秒內只能訪問10次--enable/disable post filtering config_post_check ="on"
#是否開啟post檢測--config waf output redirect/html config_waf_output ="html"
#action一個html頁面藻肄,也可以選擇跳轉--if config_waf_output ,setting url config_waf_redirect_url ="http://www.baidu.com"config_output_html=[[#下面是html的內容 請安全上網(wǎng),注意操作規(guī)范拒担。 ]]
備注:”請安全上網(wǎng)嘹屯,注意操作規(guī)范” 這個字段可以隨意更改,安裝自己的需求來从撼。
②access.lua 規(guī)則模塊
[root@linux-node1 waf]# pwd/usr/local/openresty/nginx/conf/waf
[root@linux-node2 waf]# cat access.luarequire'init'functionwaf_main()
if white_ip_check()
then else if?black_ip_check()
then else if?user_agent_attack_check()
then else if?cc_attack_check()
then else if?cookie_attack_check()
then else if?white_url_check()
then else if?url_attack_check()
then else if url_args_attack_check()
then--elseif post_attack_check()?
then else returnendendwaf_main()
檢測順序:先檢查白名單州弟,通過即不檢測;再檢查黑名單谋逻,不通過即拒絕呆馁,檢查UA,UA不通過即拒絕毁兆;檢查cookie浙滤;URL檢查;URL參數(shù)檢查,post檢查气堕;?
啟用waf并測試纺腊,模擬sql注入即url攻擊,顯示效果如下 ()?
日志顯示如下,記錄了UA茎芭,匹配規(guī)則揖膜,URL,客戶端類型梅桩,攻擊的類型壹粟,請求的數(shù)據(jù)?
④使用ab壓測工具模擬防cc攻擊?
[root@linux-node3 ~]# ab -c 100 -n 100?http://192.168.88.133/index.php?
⑤ 模擬ip黑名單
將請求ip放入ip黑名單中[root@linux-node1 rule-config]# echo “192.168.88.1” >>/usr/local/openresty/nginx/conf/waf/rule-config/blackip.rule
顯示結果如下?
⑥模擬ip白名單?
將請求ip放入ip白名單中,此時將不對此ip進行任何防護措施宿百,所以sql注入時應該返回404
[root@linux-node2 rule-config]# echo “192.168.88.1” >>/usr/local/openresty/nginx/conf/waf/rule-config/whiteip.rule
顯示結果如下?
⑦模擬URL參數(shù)檢測?
瀏覽器輸入192.168.88.133/?a=select * from table?
顯示結果如下?
詳細規(guī)定在arg.rule中有規(guī)定,對請求進行了規(guī)范
bash[root@linux-node1 rule-config]#/usr/local/openresty/nginx/conf/waf/rule-config/cat args.rule\.\./\:\$\$\{select.+(from|limit)(?:(union(.*?)select))having|rongjitestsleep\((\s*)(\d*)(\s*)\)benchmark\((.*)\,(.*)\)base64_decode\((?:from\W+information_schema\W)(?:(?:current_)user|database|schema|connection_id)\s*\((?:etc\/\W*passwd)into(\s+)+(?:dump|out)file\s*group\s+by.+\(xwork.MethodAccessor(?:define|eval|file_get_contents|include|require|require_once|shell_exec|phpinfo|system|passthru|preg_\w+|execute|echo|print|print_r|var_dump|(fp)open|alert|showmodaldialog)\(xwork\.MethodAccessor(gopher|doc|php|glob|file|phar|zlib|ftp|ldap|dict|ogg|data)\:\/java\.lang\$_(GET|post|cookie|files|session|env|phplib|GLOBALS|SERVER)\[\<(iframe|script|body|img|layer|div|meta|style|base|object|input)(onmouseover|onerror|onload)\=[root@linux-node2 rule-config]#pwd/usr/local/openresty/nginx/conf/waf/rule-config
參考:
1.openresty :?
https://github.com/openresty/lua-nginx-module#lualuajit-bytecode-support
2.安裝 :
https://blog.csdn.net/m0_37886429/article/details/73178889
https://blog.csdn.net/m0_37886429/article/details/73178889
3.luajit:?
http://luajit.org/download.html
4.策略:
https://github.com/unixhot/waf