使用ModSecurity保护AEM站点免受DoS攻击

了解如何使用Adobe Experience Manager (AEM) Publish Dispatcher上的​ OWASP ModSecurity核心规则集(CRS) ​启用ModSecurity以保护您的站点免受拒绝服务(DoS)攻击。

概述

Open Web Application Security Project® (OWASP)基础提供了​ OWASP Top 10,概述了Web应用程序十大最关键的安全问题。

ModSecurity是一种开源、跨平台的解决方案,可针对针对Web应用程序的一系列攻击提供保护。 它还支持HTTP流量监控、日志记录和实时分析。

OWSAP®还提供OWASP® ModSecurity核心规则集(CRS)。 CRS是一组用于ModSecurity的通用​ 攻击检测 ​规则。 因此,CRS旨在保护Web应用程序免受各种攻击,包括OWASP Top Ten攻击,同时尽量减少虚假警报。

本教程演示如何启用和配置​ DOS-PROTECTION CRS规则以保护您的站点免受可能的DoS攻击。

TIP
需要注意的是,AEM as a Cloud Service的托管CDN满足了大多数客户的性能和安全要求。 但是, ModSecurity提供了额外的安全层,并允许使用特定于客户的规则和配置。

将CRS添加到Dispatcher项目模块

  1. 下载并解压缩最新的OWASP ModSecurity核心规则集

    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
    
  2. 在您的AEM项目代码的dispatcher/src/conf.d/内创建modsec/crs文件夹。 例如,在AEM WKND站点项目的本地副本中。

    AEM项目代码中的 CRS文件夹 — ModSecurity {width="200" modal="regular"}

  3. 将下载的CRS发行包中的coreruleset-X.Y.Z/rules文件夹复制到dispatcher/src/conf.d/modsec/crs文件夹中。

  4. 将下载的CRS发行包中的coreruleset-X.Y.Z/crs-setup.conf.example文件复制到dispatcher/src/conf.d/modsec/crs文件夹中,并将其重命名为crs-setup.conf

  5. 通过将所有CRS规则重命名为XXXX-XXX-XXX.conf.disabled,禁用从dispatcher/src/conf.d/modsec/crs/rules复制的所有CRS规则。 可以使用以下命令一次重命名所有文件。

    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规则和配置文件。

    已禁用AEM项目代码中的CRS规则 — ModSecurity {width="200" modal="regular"}

启用和配置拒绝服务(DoS)保护规则

要启用和配置拒绝服务(DoS)保护规则,请执行以下步骤:

  1. 通过将dispatcher/src/conf.d/modsec/crs/rules文件夹中的REQUEST-912-DOS-PROTECTION.conf.disabled重命名为REQUEST-912-DOS-PROTECTION.conf(或从rulename扩展中删除.disabled)来启用DoS保护规则。

  2. 通过定义​ DOS_COUNTER_THRESHOLD、DOS_BURST_TIME_SLICE、DOS_BLOCK_TIMEOUT ​变量来配置规则。

    1. dispatcher/src/conf.d/modsec/crs文件夹中创建crs-setup.custom.conf文件。
    2. 将以下规则片段添加到新创建的文件。
    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分钟)。

WARNING
要根据您的需求定义适当的值,请与Web安全团队协作。

初始化CRS

要初始化CRS,请删除常见的误报并为站点添加本地例外,请执行以下步骤:

  1. 要初始化CRS,请从​ REQUEST-901-INITIALIZATION ​文件中删除.disabled。 换句话说,将REQUEST-901-INITIALIZATION.conf.disabled文件重命名为REQUEST-901-INITIALIZATION.conf

  2. 要删除常见误报(如本地IP (127.0.0.1) ping),请从​ REQUEST-905-COMMON-EXCEPTIONS ​文件中删除.disabled

  3. 若要添加本地异常(如AEM平台或网站特定的路径),请将REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example重命名为REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf

    1. 将特定于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                     #
    ########################################################
    ...
    
  4. 此外,从​ REQUEST-910-IP-PRESSIBILITY.conf.disabled ​中删除.disabled以进行IP信誉块检查,并从REQUEST-949-BLOCKING-EVALUATION.conf.disabled中删除异常分数检查。

TIP
在AEM 6.5上配置时,请确保将以上路径替换为验证AEM运行状况的相应AMS或内部部署路径(也称为心率路径)。

添加ModSecurity Apache配置

要启用ModSecurity(又称mod_security Apache模块),请执行以下步骤:

  1. 使用以下关键配置在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
    
  2. 从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项目的tutorial/enable-modsecurity-crs-dos-protection分支上可供您审阅。

验证Dispatcher配置

在使用AEM as a Cloud Service时,在部署​ Dispatcher配置 ​更改之前,建议使用AEM SDK的Dispatcher Toolsvalidate脚本在本地验证这些更改。

# 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 JMeterBenchmark或ab工具

使用JMeter脚本模拟DoS攻击

要使用JMeter模拟DoS攻击,请执行以下步骤:

  1. 下载Apache JMeter并在本地安装

  2. 使用<JMETER-INSTALL-DIR>/bin目录中的jmeter脚本在本地运行

  3. 使用​ Open ​工具菜单在JMeter中打开示例WKND-DoS-Attack-Simulation-Test JMX脚本。

    打开示例WKND DoS攻击JMX测试脚本 — ModSecurity

  4. 更新​ 主页 ​和​ 冒险页 HTTP请求取样器中与您的测试AEM环境URL匹配的​ 服务器名称或IP ​字段值。 查看示例JMeter脚本的其他详细信息。

    AEM服务器名称HTTP请求JMetere - ModSecurity

  5. 按工具菜单中的​ 开始 ​按钮执行脚本。 该脚本针对WKND网站的​ 主页 ​和​ 冒险页面 ​发送50个HTTP请求(5个用户和10个循环计数)。 因此,它总共向非静态文件发出100个请求,从而符合每个​ DOS-PROTECTION CRS规则自定义配置的DoS攻击。

    执行JMeter脚本 — ModSecurity

  6. 表​ JMeter侦听器中的 ​查看结果显示请求编号~53及更高版本的​ 失败 ​响应状态。

    在表JMeter - ModSecurity中 查看结果中的响应失败

  7. 为失败的请求返回​ 503 HTTP响应代码,您可以使用​ 查看结果树 JMeter侦听器查看详细信息。

    503响应JMeter - ModSecurity

查看日志

ModSecurity记录器配置会记录DoS攻击事件的详细信息。 要查看详细信息,请执行以下步骤:

  1. 下载并打开​ Publish Dispatcher ​的httpderror日志文件。

  2. 在日志文件中搜索单词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"]
    
  3. 查看详细信息,如​ 客户端IP地址、操作、错误消息和请求详细信息。

ModSecurity的性能影响

启用ModSecurity和相关规则会对性能产生影响,因此请注意哪些规则是必需的、多余的以及已跳过。 与您的Web安全专家合作,共同启用和自定义CRS规则。

其他规则

本教程仅出于演示目的启用和自定义​ DOS-PROTECTION CRS规则。 建议与Web安全专家合作,了解、审查和配置适当的规则。

recommendation-more-help
c92bdb17-1e49-4e76-bcdd-89e4f85f45e6