使用 ModSecurity 保护您的 AEM 网站免受 DoS 攻击
了解如何使用 51黑料不打烊 Experience Manager (AEM) Publish Dispatcher 上的 OWASP ModSecurity 核心规则集 (CRS) 使 ModSecurity 可保护您的网站免受拒绝服务 (DoS) 攻击。
概述
基金会发布了 ,概述了 Web 应用程序的十大最关键安全问题。
ModSecurity 是一款开源的跨平台解决方案,能够为 Web 应用程序提供一系列攻击防护。此外,它还支持 HTTP 流量监控、日志记录和实时分析。
OWSAP? 还提供 。CRS 是一套用于 ModSecurity 的通用? 攻击检测 ?规则。因此,CRS 旨在保护 Web 应用程序免受各种攻击(包括 OWASP 十大攻击),同时还会尽量减少误报。
本教程将演示如何启用和配置 DOS-PROTECTION CRS 规则,以保护您的网站免受潜在的 DoS 攻击。
将 CRS 添加到 Dispatcher 项目模块
-
下载并提取。
code language-shell # Replace the X.Y.Z with relevent version numbers. $ wget https://github.com/coreruleset/coreruleset/archive/refs/tags/vX.Y.Z.tar.gz # For version v3.3.5 when this tutorial is published $ wget https://github.com/coreruleset/coreruleset/archive/refs/tags/v3.3.5.tar.gz # Extract the downloaded file $ tar -xvzf coreruleset-3.3.5.tar.gz
-
在 AEM 项目代码中创建
modsec/crs
文件夹dispatcher/src/conf.d/
。例如,在 的本地副本中。 -
将下载的 CRS 发布包中的
coreruleset-X.Y.Z/rules
文件夹复制到dispatcher/src/conf.d/modsec/crs
文件夹中。 -
将下载的 CRS 发布包中的
coreruleset-X.Y.Z/crs-setup.conf.example
文件复制到dispatcher/src/conf.d/modsec/crs
文件夹中,并将其重命名为crs-setup.conf
。 -
将
dispatcher/src/conf.d/modsec/crs/rules
目录下所有复制的 CRS 规则,通过重命名为XXXX-XXX-XXX.conf.disabled
的形式全部禁用。您可以使用以下命令一次重命名所有文件。code language-shell # Go inside the newly created rules directory within the dispathcher module $ cd dispatcher/src/conf.d/modsec/crs/rules # Rename all '.conf' extension files to '.conf.disabled' $ for i in *.conf; do mv -- "$i" "$i.disabled"; done
请参阅 WKND 项目代码中重命名的 CRS 规则和配置文件。
启用并配置拒绝服务 (DoS) 保护规则
要启用和配置拒绝服务 (DoS) 保护规则,请按照以下步骤操作:
-
通过将
REQUEST-912-DOS-PROTECTION.conf.disabled
重命名为REQUEST-912-DOS-PROTECTION.conf
(或将.disabled
从规则名称扩展中移除)来启用 DoS 保护规则。dispatcher/src/conf.d/modsec/crs/rules
-
通过定义 顿翱厂冲颁翱鲍狈罢贰搁冲罢贬搁贰厂贬翱尝顿、顿翱厂冲叠鲍搁厂罢冲罢滨惭贰冲厂尝滨颁贰和顿翱厂冲叠尝翱颁碍冲罢滨惭贰翱鲍罢 变量来配置规则。
- 在
dispatcher/src/conf.d/modsec/crs
文件夹中创建一个crs-setup.custom.conf
文件。 - 将以下规则片段添加到新创建的文件中。
code language-none # The Denial of Service (DoS) protection against clients making requests too quickly. # When a client is making more than 25 requests (excluding static files) within # 60 seconds, this is considered a 'burst'. After two bursts, the client is # blocked for 600 seconds. SecAction \ "id:900700,\ phase:1,\ nolog,\ pass,\ t:none,\ setvar:'tx.dos_burst_time_slice=60',\ setvar:'tx.dos_counter_threshold=25',\ setvar:'tx.dos_block_timeout=600'"
- 在
在此示例规则配置中,DOS_COUNTER_THRESHOLD 为 25,DOS_BURST_TIME_SLICE 为 60 秒,DOS_BLOCK_TIMEOUT 超时时间为 600 秒。此配置将 60 秒内出现两次以上(不包括静态文件)的 25 个请求视为 DoS 攻击,导致请求客户端被阻塞 600 秒(即 10 分钟)。
初始化 CRS
要初始化 CRS,请先排除常见的误报,然后为您的网站添加本地例外情况,具体步骤如下:
-
要初始化 CRS,请从 REQUEST-901-INITIALIZATION 文件中移除
.disabled
。 换句话说,将REQUEST-901-INITIALIZATION.conf.disabled
文件重命名为REQUEST-901-INITIALIZATION.conf
。 -
要消除常见的误报,如本地 IP (127.0.0.1) 的 ping 操作,请从 REQUEST-905-COMMON-EXCEPTIONS 文件中移除
.disabled
。 -
要添加本地例外情况(如 AEM 平台或网站特定路径),请将
REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example
重命名为REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
- 将特定于 AEM 平台的路径例外情况添加到新重命名的文件中。
code language-none ######################################################## # AEM as a Cloud Service exclusions # ######################################################## # Ignoring AEM-CS Specific internal and reserved paths SecRule REQUEST_URI "@beginsWith /systemready" \ "id:1010,\ phase:1,\ pass,\ nolog,\ ctl:ruleEngine=Off" SecRule REQUEST_URI "@beginsWith /system/probes" \ "id:1011,\ phase:1,\ pass,\ nolog,\ ctl:ruleEngine=Off" SecRule REQUEST_URI "@beginsWith /gitinit-status" \ "id:1012,\ phase:1,\ pass,\ nolog,\ ctl:ruleEngine=Off" ######################################################## # ADD YOUR SITE related exclusions # ######################################################## ...
-
此外,请删除 REQUEST-910-IP-REPUTATION.conf.disabled(用于 IP 信誉阻止检查)和
REQUEST-949-BLOCKING-EVALUATION.conf.disabled
中的.disabled
。
添加 ModSecurity Apache 配置
要启用 ModSecurity(又名 mod_security
Apache 模块),请按照以下步骤操作:
-
使用以下关键配置在
dispatcher/src/conf.d/modsec/modsecurity.conf
创建modsecurity.conf
。code language-none # Include the baseline crs setup Include conf.d/modsec/crs/crs-setup.conf # Include your customizations to crs setup if exist IncludeOptional conf.d/modsec/crs/crs-setup.custom.conf # Select all available CRS rules: #Include conf.d/modsec/crs/rules/*.conf # Or alternatively list only specific ones you want to enable e.g. Include conf.d/modsec/crs/rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf Include conf.d/modsec/crs/rules/REQUEST-901-INITIALIZATION.conf Include conf.d/modsec/crs/rules/REQUEST-905-COMMON-EXCEPTIONS.conf Include conf.d/modsec/crs/rules/REQUEST-910-IP-REPUTATION.conf Include conf.d/modsec/crs/rules/REQUEST-912-DOS-PROTECTION.conf Include conf.d/modsec/crs/rules/REQUEST-949-BLOCKING-EVALUATION.conf # Start initially with engine off, then switch to detection and observe, and when sure enable engine actions #SecRuleEngine Off #SecRuleEngine DetectionOnly SecRuleEngine On # Remember to use relative path for logs: SecDebugLog logs/httpd_mod_security_debug.log # Start with low debug level SecDebugLogLevel 0 #SecDebugLogLevel 1 # Start without auditing SecAuditEngine Off #SecAuditEngine RelevantOnly #SecAuditEngine On # Tune audit accordingly: SecAuditLogRelevantStatus "^(?:5|4(?!04))" SecAuditLogParts ABIJDEFHZ SecAuditLogType Serial # Remember to use relative path for logs: SecAuditLog logs/httpd_mod_security_audit.log # You might still use /tmp for temporary/work files: SecTmpDir /tmp SecDataDir /tmp
-
从 AEM 项目的 Dispatcher 模块
dispatcher/src/conf.d/available_vhosts
中选择所需的.vhost
,例如wknd.vhost
,并在<VirtualHost>
区块外添加以下条目。code language-none # Enable the ModSecurity and OWASP CRS <IfModule mod_security2.c> Include conf.d/modsec/modsecurity.conf </IfModule> ... <VirtualHost *:80> ServerName "publish" ... </VirtualHost>
上述所有 ModSecurity CRS 和 DOS-PROTECTION 配置均可在 AEM WKND Sites 项目的 分支上查看。
验证 Dispatcher 配置
在使用 AEM as a Cloud Service 时,在部署 Dispatcher 配置 ?更改之前,建议使用 AEM SDK 的 Dispatcher 工具的 validate
脚本进行本地验证。
# Go inside Dispatcher SDK 'bin' directory
$ cd <YOUR-AEM-SDK-DIR>/<DISPATCHER-SDK-DIR>/bin
# Validate the updated Dispatcher configurations
$ ./validate.sh <YOUR-AEM-PROJECT-CODE-DIR>/dispatcher/src
部署
使用 Cloud Manager Web 层或全栈管道部署本地验证的 Dispatcher 配置。您也可以使用快速开发环境,以缩短周转时间。
验证
为了验证 DoS 保护,在此示例中,我们将在 60 秒内发送超过 50 个请求(25 个请求阈值乘以两次出现)。但是,这些请求应通过 AEM as a Cloud Service 的内置功能,或任何作为你网站前端的其他 CDN 进行传递。
实现 CDN 透传的一种方法是? 在每个网站页面请求中添加一个带有新的随机值 ?的查询参数。
若要在短时间内(如 60 秒内)触发大量请求(50 个或更多),可使用 Apache 、。
使用 JMeter 脚本模拟 DoS 攻击
要使用 JMeter 模拟 DoS 攻击,请按照以下步骤操作:
-
并在本地进行
-
使用位于
<JMETER-INSTALL-DIR>/bin
目录下的jmeter
脚本在本地它。 -
使用? 打开 ?工具菜单,将示例 WKND-DoS-Attack-Simulation-Test JMX 脚本导入 JMeter。
-
在? 主页 ?和? 冒险页面 HTTP 请求采样器中更新? 服务器名称或 IP 字段值,使其与您的测试 AEM 环境 URL 相匹配。检查 JMeter 脚本样本的其他详细信息。
-
通过点击工具菜单中的? 开始 ?按钮来执行脚本。该脚本向 WKND 网站的? 主页 ?和? 冒险页面 ?发送 50 个 HTTP 请求(5 个用户和 10 次循环计数)。因此,总共向非静态文件发送了 100 个请求,符合 DOS-PROTECTION CRS 规则自定义配置中的 DoS 攻击条件。
-
JMeter 的? View Results in Table 监听器显示,从第约 53 个请求开始,其响应状态为? 失败。
-
对于失败的请求,会返回 503 HTTP 响应码,您可以使用 JMeter 的 View Results Tree 监听器查看详细信息。
审查日志
ModSecurity 日志配置会记录 DoS 攻击事件的详细信息。要查看这些详细信息,请按照以下步骤操作:
-
下载并打开? 发布 Dispatcher 的
httpderror
日志文件。 -
在日志文件中搜索单词
burst
,以查看? 错误 ?行code language-none Tue Aug 15 15:19:40.229262 2023 [security2:error] [pid 308:tid 140200050567992] [cm-p46652-e1167810-aem-publish-85df5d9954-bzvbs] [client 192.150.10.209] ModSecurity: Warning. Operator GE matched 2 at IP:dos_burst_counter. [file "/etc/httpd/conf.d/modsec/crs/rules/REQUEST-912-DOS-PROTECTION.conf"] [line "265"] [id "912170"] [msg "Potential Denial of Service (DoS) Attack from 192.150.10.209 - # of Request Bursts: 2"] [ver "OWASP_CRS/3.3.5"] [tag "application-multi"] [tag "language-multi"] [tag "platform-multi"] [tag "paranoia-level/1"] [tag "attack-dos"] [tag "OWASP_CRS"] [tag "capec/1000/210/227/469"] [hostname "publish-p46652-e1167810.adobeaemcloud.com"] [uri "/content/wknd/us/en/adventures.html"] [unique_id "ZNuXi9ft_9sa85dovgTN5gAAANI"] ... Tue Aug 15 15:19:40.515237 2023 [security2:error] [pid 309:tid 140200051428152] [cm-p46652-e1167810-aem-publish-85df5d9954-bzvbs] [client 192.150.10.209] ModSecurity: Access denied with connection close (phase 1). Operator EQ matched 0 at IP. [file "/etc/httpd/conf.d/modsec/crs/rules/REQUEST-912-DOS-PROTECTION.conf"] [line "120"] [id "912120"] [msg "Denial of Service (DoS) attack identified from 192.150.10.209 (1 hits since last alert)"] [ver "OWASP_CRS/3.3.5"] [tag "application-multi"] [tag "language-multi"] [tag "platform-multi"] [tag "paranoia-level/1"] [tag "attack-dos"] [tag "OWASP_CRS"] [tag "capec/1000/210/227/469"] [hostname "publish-p46652-e1167810.adobeaemcloud.com"] [uri "/us/en.html"] [unique_id "ZNuXjAN7ZtmIYHGpDEkmmwAAAQw"]
-
查看? 客户端 IP 地址、操作、错误消息和请求详情等详细信息。
ModSecurity 的性能影响
启用 ModSecurity 及其相关规则会对性能产生一定影响,因此需注意哪些规则是必需的、哪些是多余的、哪些可以跳过。请与您的 Web 安全专家合作,以启用并自定义 CRS 规则。
其他规则
本教程仅出于演示目的启用并自定义 DOS-PROTECTION CRS 规则。建议与网络安全专家合作,以了解、审查和配置适当的规则。