coreruleset: Apache 403 Forbidden error handling and duplicate HTTP response headers
Describe the bug
I’m using Apache httpd on Windows to proxy requests to Tomcat for specific subdirectories, but not /. Requests for any other resource paths should return the standard Forbidden response.
When CRS 4.1.0 is loaded with the default config, requests for undefined resource paths return an unusual error message appended to the bottom of the usual 403 Forbidden page, and custom HTTP response headers are duplicated.
Request handling for defined proxy request paths (both standard and ‘bad’ requests e.g. ?/bin/bash etc) appears to be fine.
The issue does not seem to occur with v3.3.5.
Steps to reproduce
The following PowerShell (run as admin) on Win 10/Server 2019 and above will help to set up a repro. Note that you may need to disable AV scanning or add specific exceptions if the Defender AV exclusions aren’t appropriate for your machine.
$BaseDir = "C:\Repro"
mkdir "$BaseDir\Sources"
cd "$BaseDir\Sources"
## Defender AV Path Exclusions for CRS
Set-MpPreference -ExclusionPath 'C:\Repro\Sources\'
Set-MpPreference -ExclusionPath 'C:\Repro\Apache24\conf\crs\'
$userAgent = [Microsoft.PowerShell.Commands.PSUserAgent]::Chrome
$ProgressPreference = "SilentlyContinue"
wget https://www.apachelounge.com/download/VS17/binaries/httpd-2.4.59-240404-win64-VS17.zip -outfile httpd-2.4.59-240404-win64-VS17.zip -userAgent $userAgent
wget https://www.apachelounge.com/download/VS17/modules/mod_security-2.9.7-win64-VS17.zip -outfile mod_security-2.9.7-win64-VS17.zip -userAgent $userAgent
wget https://aka.ms/vs/17/release/VC_redist.x64.exe -outfile vc_redist.x64.exe
wget https://raw.githubusercontent.com/owasp-modsecurity/ModSecurity/v2/master/modsecurity.conf-recommended -outfile modsecurity.conf-recommended
wget https://raw.githubusercontent.com/owasp-modsecurity/ModSecurity/v2/master/unicode.mapping -outfile unicode.mapping
wget https://github.com/coreruleset/coreruleset/archive/refs/tags/v4.1.0.zip -outfile crs-v4.1.0.zip
$zipFiles = Get-ChildItem -Filter *.zip
foreach ($zipFile in $zipFiles) {
Expand-Archive $zipFile.Name
}
copy httpd-*\Apache24 \Repro\ -Recurse
copy mod_security-*\*\*.so \Repro\Apache24\Modules
copy mod_security-*\*\yajl.dll \Repro\Apache24\bin
mkdir \Repro\Apache24\conf\modsecurity
copy modsecurity.conf-recommended \Repro\Apache24\conf\modsecurity\modsecurity.conf
copy unicode.mapping \Repro\Apache24\conf\modsecurity
mkdir \Repro\Apache24\tmp
rmdir \Repro\Sources\Apache*\ -Recurse
$modSecConfContent = Get-Content -Path "\Repro\Apache24\conf\modsecurity\modsecurity.conf"
$modSecConfContent = $modSecConfContent -replace 'SecRuleEngine DetectionOnly', 'SecRuleEngine On'
$modSecConfContent = $modSecConfContent -replace 'SecTmpDir /tmp/', 'SecTmpDir tmp'
$modSecConfContent = $modSecConfContent -replace 'SecDataDir /tmp/', 'SecDataDir tmp'
$modSecConfContent = $modSecConfContent -replace 'SecAuditLog /var/log/modsec_audit.log', 'SecAuditLog logs/modsec_audit.log'
$modSecConfContent | Set-Content -Path "\Repro\Apache24\conf\modsecurity\modsecurity.conf"
copy \Repro\Sources\crs-*\coreruleset-*\ \Repro\Apache24\conf\crs\ -Recurse
ren \Repro\Apache24\conf\crs\crs-setup.conf.example crs-setup.conf
$httpdConfExtraContent = @"
Header always set HeaderOne "1"
Header always set HeaderTwo "2"
Header always set HeaderThree "3"
LoadModule security2_module modules/mod_security2.so
Include conf/modsecurity/modsecurity.conf
Include conf/crs/crs-setup.conf
#Include conf/crs/plugins/*-config.conf
#Include conf/crs/plugins/*-before.conf
Include conf/crs/rules/*.conf
#Include conf/crs/plugins/*-after.conf
"@
Add-Content -Path "\Repro\Apache24\conf\httpd.conf" -Value $httpdConfExtraContent
$httpdConfContent = Get-Content -Path "\Repro\Apache24\conf\httpd.conf"
$httpdConfContent = $httpdConfContent -replace 'Define SRVROOT "c:/Apache24"', 'Define SRVROOT "C:\Repro\Apache24"'
$httpdConfContent = $httpdConfContent -replace '#ServerName www.example.com:80', 'ServerName www.example.com:80'
$httpdConfContent = $httpdConfContent -replace 'DocumentRoot "\${SRVROOT}/htdocs"', '#DocumentRoot "${SRVROOT}/htdocs"'
$httpdConfContent = $httpdConfContent -replace '#LoadModule headers_module modules/mod_headers.so', 'LoadModule headers_module modules/mod_headers.so'
$httpdConfContent = $httpdConfContent -replace '#LoadModule unique_id_module modules/mod_unique_id.so', 'LoadModule unique_id_module modules/mod_unique_id.so'
$httpdConfContent | Set-Content -Path "\Repro\Apache24\conf\httpd.conf"
Start-Process -FilePath "\Repro\Sources\vc_redist.x64.exe" -ArgumentList "/install", "/quiet", "/norestart" -Wait
Write-Host "Starting httpd..."
\Repro\Apache24\bin\httpd
Expected behaviour
Browse to “http://localhost/”
- The standard Apache 403 Forbidden error page displays without the following text “Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request.”
- Custom response headers are not duplicated.
Actual behaviour
Additional context
This issue does not occur with v.3.3.5.
Here’s a debug log entry:
--29000000-A--
[22/Apr/2024:22:49:32.274613 +1200] ZiZAvP81uSk7OfVgzl51IQAAACg ::1 55122 ::1 80
--29000000-B--
GET / HTTP/1.1
Host: localhost
Connection: keep-alive
Cache-Control: max-age=0
sec-ch-ua: "Chromium";v="124", "Microsoft Edge";v="124", "Not-A.Brand";v="99"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate, br, zstd
Accept-Language: en-US,en;q=0.9,en-NZ;q=0.8
--29000000-F--
HTTP/1.1 403 Forbidden
HeaderOne: 1
HeaderTwo: 2
HeaderThree: 3
HeaderOne: 1
HeaderTwo: 2
HeaderThree: 3
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset=iso-8859-1
--29000000-E--
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access this resource.</p>
</body></html>
--29000000-H--
Message: Access denied with code 403 (phase 4). Operator GE matched 0 at TX:blocking_outbound_anomaly_score. [file "C:/Repro/Apache24/conf/crs/rules/RESPONSE-959-BLOCKING-EVALUATION.conf"] [line "243"] [id "959100"] [msg "Outbound Anomaly Score Exceeded (Total Score: 0)"] [ver "OWASP_CRS/4.1.0"] [tag "anomaly-evaluation"] [tag "OWASP_CRS"]
Apache-Error: [file "C:\\VS17\\Win64\\httpd-2.4\\modules\\aaa\\mod_authz_core.c"] [line 879] [level 3] AH01630: client denied by server configuration: C:/apache
Apache-Error: [file "apache2_util.c"] [line 275] [level 3] [client ::1] ModSecurity: Access denied with code 403 (phase 4). Operator GE matched 0 at TX:blocking_outbound_anomaly_score. [file "C:/Repro/Apache24/conf/crs/rules/RESPONSE-959-BLOCKING-EVALUATION.conf"] [line "243"] [id "959100"] [msg "Outbound Anomaly Score Exceeded (Total Score: 0)"] [ver "OWASP_CRS/4.1.0"] [tag "anomaly-evaluation"] [tag "OWASP_CRS"] [hostname "localhost"] [uri "/"] [unique_id "ZiZAvP81uSk7OfVgzl51IQAAACg"]
Action: Intercepted (phase 4)
Stopwatch: 1713782972273612 1001 (- - -)
Stopwatch2: 1713782972273612 1001; combined=0, p1=0, p2=0, p3=0, p4=0, p5=0, sr=0, sw=0, l=0, gc=0
Response-Body-Transformed: Dechunked
Producer: ModSecurity for Apache/2.9.7 (http://www.modsecurity.org/); OWASP_CRS/4.1.0.
Server: Apache/2.4.59 (Win64)
Engine-Mode: "ENABLED"
--29000000-K--
SecRule "RESPONSE_BODY" "!@pmFromFile sql-errors.data" "phase:4,auditlog,id:951100,t:none,nolog,tag:application-multi,tag:language-multi,tag:platform-multi,tag:attack-disclosure,tag:OWASP_CRS,tag:capec/1000/118/116/54,ver:OWASP_CRS/4.1.0,skipAfter:END-SQL-ERROR-MATCH-PL1"
SecRule "RESPONSE_STATUS" "!@rx ^404$" "phase:4,log,auditlog,id:954130,block,capture,t:none,msg:'IIS Information Leakage',logdata:'Matched Data: %{TX.0} found within %{MATCHED_VAR_NAME}',tag:application-multi,tag:language-multi,tag:platform-iis,tag:platform-windows,tag:attack-disclosure,tag:paranoia-level/1,tag:OWASP_CRS,tag:capec/1000/118/116,tag:PCI/6.5.6,ver:OWASP_CRS/4.1.0,severity:ERROR,chain"
#SecRule "RESPONSE_BODY" "@rx \\bServer Error in.{0,50}?\\bApplication\\b" "capture,t:none,setvar:tx.outbound_anomaly_score_pl1=+%{tx.error_anomaly_score}"
SecAction "phase:4,auditlog,id:959059,pass,t:none,nolog,tag:OWASP_CRS,ver:OWASP_CRS/4.1.0,setvar:tx.blocking_outbound_anomaly_score=0"
SecAction "phase:4,auditlog,id:959159,pass,t:none,nolog,tag:OWASP_CRS,ver:OWASP_CRS/4.1.0,setvar:tx.detection_outbound_anomaly_score=0"
SecRule "TX:BLOCKING_OUTBOUND_ANOMALY_SCORE" "@ge %{tx.outbound_anomaly_score_threshold}" "phase:4,log,auditlog,id:959100,deny,t:none,msg:'Outbound Anomaly Score Exceeded (Total Score: %{tx.blocking_outbound_anomaly_score})',tag:anomaly-evaluation,tag:OWASP_CRS,ver:OWASP_CRS/4.1.0"
SecAction "phase:5,id:980099,pass,t:none,nolog,noauditlog,tag:OWASP_CRS,ver:OWASP_CRS/4.1.0,setvar:tx.blocking_anomaly_score=%{tx.blocking_inbound_anomaly_score},setvar:tx.blocking_anomaly_score=+%{tx.blocking_outbound_anomaly_score},setvar:tx.detection_anomaly_score=%{tx.detection_inbound_anomaly_score},setvar:tx.detection_anomaly_score=+%{tx.detection_outbound_anomaly_score},setvar:tx.anomaly_score=%{tx.blocking_inbound_anomaly_score},setvar:tx.anomaly_score=+%{tx.blocking_outbound_anomaly_score}"
SecRule "TX:DETECTION_ANOMALY_SCORE" "@eq 0" "phase:5,auditlog,id:980043,nolog,tag:OWASP_CRS,ver:OWASP_CRS/4.1.0,skipAfter:END-REPORTING"
If rule id:959100 is removed, expected behaviour returns. It appears that during the 959100 rule evaluation, the outbound anomaly limit is empty so defaults to 0.
Your Environment
- CRS version (e.g., v3.2.0): 4.1.0
- Paranoia level setting: 1
- ModSecurity version: 2.9.7
- Web Server and version: apache 2.4.59
- Operating System and version: Windows 10 & Windows Server 2022
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 16 (8 by maintainers)
@guiaume Unless you can set up an environment (VM, container…) where we can look at the issue first hand, it is unlikely that we would be able to help you as I don’t think any of use are running that particular setup. CRS is run by a lot of people in httpd, so I don’t think it’s directly related to httpd or CRS, more likely to some configuration.
Hello, I have the same problem with Apache 2.4.62 on Windows (from ApacheLounge) with ModSecurity 2.9.7 (win64-VS17) For me the problem only occurs with Modsecurity enabled, if I disable it, I have normal behavior. Were you able to solve this problem? Or does anyone have any ideas on this?
This is very odd.
I think you need to dump the value of
tx.outbound_anomaly_score_thresholdacross various points in the execution (-> start and end of every phase) to understand why it’s 0.This is either a funny malfunction or you have some additional configuration laying around.
Here is how to dump a value:
Hi @ablanken.
Thanks for the detailed report.
This line says that your
tx.outbound_anomaly_score_thresholdis set to 0. So it makes sense that ModSecurity will run thedenyaction. Every request will be denied. I don’t see how any request would ever return200.