1)搭建靶機使用常用的DVWA + phpstudy組合
2)到以下地址下載 mod_security
https://www.apachelounge.com/download/
3)打開壓縮包後,先安裝第一個目錄
4)依照ReadMe.txt 如下圖
將mod_security2.so複製到apache/modules目錄中
將yajl.dll複製到apache/bin目錄中
5)打開apache的httpd.conf文件確保以下兩條存在且無#註釋
LoadModule security2_module modules/mod_security2.so
LoadModule unique_id_module modules/mod_unique_id.so
6)開啓日誌功能
將mlogc.exe複製到bin目錄中
會在如下圖路徑生成攔截日誌,日誌內容也很清晰,還有基於語義引擎libinjection的攔截內容
7)配置策略,在apache根目錄下載OWASP ModSecurity的策略
git clone https://github.com/coreruleset/coreruleset
將默認級別設置爲2級,原因在下面文章的末尾處
https://mp.weixin.qq.com/s/GM1YDKB_04sDvZR3ar7d3A
打開 crs-setup.conf.example 找到代碼
#SecAction \
# "id:900000,\
# phase:1,\
# nolog,\
# pass,\
# t:none,\
# setvar:tx.paranoia_level=1"
修改爲
SecAction \
"id:900000,\
phase:1,\
nolog,\
pass,\
t:none,\
setvar:tx.paranoia_level=2"
8)最後再次配置httpd.conf,在文件末尾添加以下代碼
#允許 GET POST OPTIONS 關閉TRACE
<Location "/">
AllowMethods GET POST OPTIONS
</Location>
TraceEnable off
<IfModule security2_module>
#啓動引擎
SecRuleEngine On
#對http請求體進行檢測
SecRequestBodyAccess On
#對http響應體進行檢測
SecResponseBodyAccess On
#加載策略
Include coreruleset/crs-setup.conf.example
Include coreruleset/rules/*.conf
#處理X-Powered-By頭 Server頭 Allow頭
Header always unset "X-Powered-By"
Header set Allow "GET,POST"
ServerTokens Prod
ServerSignature Off
SecServerSignature "***"
#設置日誌
SecDataDir logs
SecAuditEngine RelevantOnly
SecAuditLogRelevantStatus "^(?:5|4\d[^4])"
SecAuditLogType Concurrent
SecAuditLogParts ABCDEFGHZ
SecAuditLogStorageDir logs/mod_security/
SecAuditLog "${SRVROOT}/bin/mlogc.exe"
</IfModule>
實驗:
上傳php文件會被waf攔截,甚至我們使用burp插件chunk進行bypass也被攔截了
9)使用反向代理實現waf防護
我們使用hosts文件映射 123.com --> 127.0.0.1,在本機啓動tomcat在端口8080
這裏注意ServerName必須寫對域名
緊接着上文中的配置增加:
<VirtualHost *:80>
ServerName 123.com
AddDefaultCharset UTF-8
<IfModule security2_module>
#加載策略
Include coreruleset/crs-setup.conf.example
Include coreruleset/rules/*.conf
#啓動引擎
SecRuleEngine On
#對http請求體進行檢測
SecRequestBodyAccess On
#對http響應體進行檢測
SecResponseBodyAccess On
##設置日誌
SecAuditEngine RelevantOnly
SecAuditLogRelevantStatus "^(?:5|4\d[^4])"
SecAuditLogType Concurrent
SecAuditLogParts ABCDEFGHZ
SecAuditLogStorageDir logs/mod_security/
SecAuditLog "${SRVROOT}/bin/mlogc.exe"
</IfModule>
ProxyPass / http://127.0.0.1:8080/
ProxyPassReverse / http://127.0.0.1:8080/
</VirtualHost>
經試驗,這樣可以做到一個反向代理的waf