[科普文]ubuntu上安装Apache2+ModSecurity及自定义WAF规则
转载地址:虽然VPS使用了云WAF功能,但还是有点小担心,为了双重保险,决定使用modsecurity来定制规则,以下介绍如何为apache服务器配置ModSecurity防护罩(modsecurity目前也支持Nginx,IIS) 。本次选择使用包管理器来安装,因为每次使用源码包的安装方式,都会被诡异的库依赖错误弄得发型都抓乱。安装环境:OS:Ubuntu 1
转载地址:http://danqingdani.blog.163.com/blog/static/186094195201481562831737/
虽然VPS使用了云WAF功能,但还是有点小担心,为了双重保险,决定使用modsecurity来定制规则,以下介绍如何为apache服务器配置ModSecurity防护罩(modsecurity目前也支持Nginx,IIS) 。
我们可以使用以下命令查看一下modsecurity的当前版本apt-get install libxml2 libxml2-dev libxml2-utils libaprutil1 libaprutil1-dev libapache2-modsecurity
我的vps上安装的是 Version: 2.7.7-2dpkg -s libapache2-modsecurity | grep Version
该命令生效后,会在 /var/log/apache2/目录下生成modsecurity的日志文件modsec_audit.logservice apache2 reload
cd /etc/modsecurity/
mv modsecurity.conf-recommended modsecurity.confvim /etc/modsecurity/modsecurity.conf修改
SecRuleEngine On
ModSecurity CRS 笔记、WAF防御checklist,及WAF架构的一些想法
选择启用base规则集cd /usr/share/modsecurity-crs/activated_rules/
你可以采用同样的办法启用其他规则集,注意不同的规则集可能需要启用特定的模块for f in $(ls ../base_rules/); do ln -s ../base_rules/$f; done
(2)2.6版本vim /etc/apache2/mods-available/security2.conf
修改
<IfModule security2_module># Default Debian dir for modsecurity's persistent dataSecDataDir /var/cache/modsecurity# Include all the *.conf files in /etc/modsecurity.# Keeping your local configuration in that directory# will allow for an easy upgrade of THIS file and# make your life easierIncludeOptional /etc/modsecurity/*.confIncludeOptional /usr/share/modsecurity-crs/*.confIncludeOptional /usr/share/modsecurity-crs/activated_rules/*.conf</IfModule>
vim /etc/apache2/mods-available/mod-security.conf
修改
Include /etc/modsecurity/*.conf
Include /usr/share/modsecurity-crs/*.conf
Include /usr/share/modsecurity-crs/activated_rules/*.conf
a2enmod headers
a2enmod security2 (版本2.6: a2enmod mod-security)service apache2 restart
我们发现请求包被403拦截了,http://www.tanjiti.com/?case=archive&act=orders&aid[typeid`%3D1%20and%20ord(mid((select/**/concat(username,0x3a,password)%20from%20cmseasy_user),1,1))%3C49%23]=1
tail /var/log/apache2/modsec_audit.log
可以看到是被base规则集的 modsecurity_crs_41_sql_injection_attacks.conf文件的规则 981231拦截,命中了SQL注释语句。
message: Access denied with code 403 (phase 2). Pattern match "(/\\*!?|\\*/|[';]--|--[\\s\\r\\n\\v\\f]|(?:--[^-]*?-)|([^\\-&])#.*?[\
\s\\r\\n\\v\\f]|;?\\x00)" at ARGS_NAMES:aid[typeid`=1 and ord(mid((select/**/concat(username,0x3a,password) from cmseasy_user),1,1))
<49#]. [file "/usr/share/modsecurity-crs/activated_rules/modsecurity_crs_41_sql_injection_attacks.conf"] [line "49"] [id "981231"] [
rev "2"] [msg "SQL Comment Sequence Detected."] [data "Matched Data: /* found within ARGS_NAMES:aid[typeid`=1 and ord(mid((select/**
/concat(username,0x3a,password) from cmseasy_user),1,1))<49#]: aid[typeid`=1 and ord(mid((select/**/concat(username,0x3a,password) f
rom cmseasy_user),1,1))<49#]"] [severity "CRITICAL"] [ver "OWASP_CRS/2.2.8"] [maturity "8"] [accuracy "8"] [tag "OWASP_CRS/WEB_ATTAC
K/SQL_INJECTION"] [tag "WASCTC/WASC-19"] [tag "OWASP_TOP_10/A1"] [tag "OWASP_AppSensor/CIE1"] [tag "PCI/6.5.2"]
ModSecurity SecRule cheatsheets
添加规则vim /usr/share/modsecurity-crs/activated_rules/MY.conf
SecRule FILES "!\\.(?i:jpe?g|gif|png|bmp)$" "deny,tag:'WEB_ATTACK/FILEUPLOAD',msg:'upload no-picture file',id:0000001,phase:2"
HTTP发包工具 -HTTPie)
我们可以看到请求包被拦截,查看modsecurity日志http www.tanjiti.com filename@a.php
可以看到命中了规则0000001more /var/log/apache2/modsec_audit.log
Message: Access denied with code 403 (phase 2). Match of "rx \\.(?i:jpe?g|gif|png|bmp)$" against "FILES:filename" required. [file "/ usr/share/modsecurity-crs/activated_rules/MY.conf"] [line "1"] [id "0000001"] [msg "upload no-picture file"] [tag "WEB_ATTACK/FILEUP LOAD"]
vim /usr/share/modsecurity-crs/activated_rules/MY.conf
SecRule FILES "@contains %00" "deny,tag:'WEB_ATTACK/FILEUPLOAD',msg:'filename has null character',id:0000002,phase:2"
我们可以看到请求包被拦截,查看modsecurity日志http www.tanjiti.com filename@a.php%00.jpeg
more /var/log/apache2/modsec_audit.log
Message: Access denied with code 403 (phase 2). String match "%00" at FILES:filename. [file "/usr/share/modsecurity-crs/activated_ru les/MY.conf"] [line "2"] [id "0000002"] [msg "filename has null character"] [tag "WEB_ATTACK/FILEUPLOAD"]
更多推荐
所有评论(0)