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