Trustwave's 2024 Retail Report Series Highlights Alarming E-Commerce Threats and Growing Fraud Against Retailers. Learn More
Get access to immediate incident response assistance.
Get access to immediate incident response assistance.
Trustwave's 2024 Retail Report Series Highlights Alarming E-Commerce Threats and Growing Fraud Against Retailers. Learn More
This week's topic highlights a community contribution by long time ModSecurity "Power User" Marc Stern. Marc created a new transformation function called "t:cmdLine" that is intended to help normalize command line strings, to avoid common evasion techniques. This contribution has been integrated into to the ModSecurity SVN trunk codebase for v2.6. Thanks for the contribution Marc!
In Windows and Unix, commands may be escaped by different means, such as:
c^ommand /c ...
"command" /c ...
command,/c ...
who\ami
The cmdLine transformation function avoids this problem by manipulating the variable contend in the following ways:
SecRule ARGS "(?:command(?:.com)?|cmd(?:.exe)?)(?:/.*)?/[ck]" \ "phase:2,t:none,t:cmdLine"
Let's say that you receive the following attack request which is injecting Microsoft OS commands into the argument payload:
POST http://www.example.com/public/doc HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1) Gecko/20061010 FireFox/2.0
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: it-it,it;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Proxy-Connection: keep-alive
Referer: http://127.0.0.1/WebGoat/attack?Screen=20
Cookie: JSESSIONID=295500AD2AAEEBEDC9DB86E34F24A0A5
Authorization: Basic T2Vbc1Q9Z3V2Tc3e=
Content-Type: application/x-www-form-urlencoded
Content-length: 33
Doc1.pdf+|+cmd.exe+/c+dir+c:\
This attack would be identified by the following ModSecurity CRS rule -
SecRule REQUEST_FILENAME|ARGS_NAMES|ARGS|XML:/* "\bcmd\.exe\b" \
"phase:2,rev:'2.1.1',capture,t:none,t:htmlEntityDecode,t:compressWhitespace,t:lowercase,ctl:auditLogParts=+E,block,
msg:'System Command Access',id:'958500',tag:'WEB_ATTACK/FILE_INJECTION',tag:'WASCTC/WASC-31',tag:'OWASP_TOP_10/A1',tag:'PCI/6.5.2',logdata:'%{TX.0}',severity:'2',setvar:'tx.msg=%{rule.msg}',setvar:tx.anomaly_score=+%{tx.critical_anomaly_score},
setvar:tx.command_access_score=+%{tx.critical_anomaly_score},setvar:tx.%{rule.id}-WEB_ATTACK/COMMAND_ACCESS-%{matched_var_name}=%{tx.0}"
If an attacker where to insert the "^" character into the "cmd.exe" string, it would cause an evasion of CRS rule ID 958500 since the regular expression would no longer match, however, the Windows command interpreter would still process the command successfully. Here is an example updated attack request with the new "c^md.exe" payload:
POST http://www.example.com/public/doc HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1) Gecko/20061010 FireFox/2.0
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: it-it,it;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Proxy-Connection: keep-alive
Referer: http://127.0.0.1/WebGoat/attack?Screen=20
Cookie: JSESSIONID=295500AD2AAEEBEDC9DB86E34F24A0A5
Authorization: Basic T2Vbc1Q9Z3V2Tc3e=
Content-Type: application/x-www-form-urlencoded
Content-length: 30
Doc1.pdf+|+c^md.exe+/c+dir+c:\
If we update the rule to include the new transformation function, we can counteract this evasion technique:
SecRule REQUEST_FILENAME|ARGS_NAMES|ARGS|XML:/* "\bcmd\.exe\b" \
"phase:2,rev:'2.1.1',capture,t:none,t:htmlEntityDecode,t:compressWhitespace,t:cmdLine,ctl:auditLogParts=+E,block,
msg:'System Command Access',id:'958500',tag:'WEB_ATTACK/FILE_INJECTION',tag:'WASCTC/WASC-31',tag:'OWASP_TOP_10/A1',tag:'PCI/6.5.2',logdata:'%{TX.0}',severity:'2',setvar:'tx.msg=%{rule.msg}',setvar:tx.anomaly_score=+%{tx.critical_anomaly_score},
setvar:tx.command_access_score=+%{tx.critical_anomaly_score},setvar:tx.%{rule.id}-WEB_ATTACK/COMMAND_ACCESS-%{matched_var_name}=%{tx.0}"
With this new rule configuration in place, this is how the processing looks in the ModSecurity debug log file:
T (0) htmlEntityDecode: "Doc1.pdf | c^md.exe /c dir c:\\"
T (0) compressWhitespace: "Doc1.pdf | c^md.exe /c dir c:\\"
T (0) lowercase: "doc1.pdf | c^md.exe /c dir c:\\"
T (0) cmdline: "doc1.pdf | cmd.exe/c dir c:"
Transformation completed in 31 usec.
Executing operator "rx" with param "\\bcmd\\.exe\\b" against ARGS_NAMES:Doc1.pdf | c^md.exe /c dir c:\\.
Target value: "doc1.pdf | cmd.exe/c dir c:"
Added regex subexpression to TX.0: cmd.exe
Operator completed in 12 usec.
Ctl: Set auditLogParts to ABIFHZE.
Setting variable: tx.msg=%{rule.msg}
Resolved macro %{rule.msg} to: System Command Access
Set variable "tx.msg" to "System Command Access".
Setting variable: tx.anomaly_score=+%{tx.critical_anomaly_score}
Original collection variable: tx.anomaly_score = "2"
Resolved macro %{tx.critical_anomaly_score} to: 5
Relative change: anomaly_score=2+5
Set variable "tx.anomaly_score" to "7".
Setting variable: tx.command_access_score=+%{tx.critical_anomaly_score}
Recorded original collection variable: tx.command_access_score = "0"
Resolved macro %{tx.critical_anomaly_score} to: 5
Relative change: command_access_score=0+5
Set variable "tx.command_access_score" to "5".
Setting variable: tx.%{rule.id}-WEB_ATTACK/COMMAND_ACCESS-%{matched_var_name}=%{tx.0}
Resolved macro %{rule.id} to: 958500
Resolved macro %{matched_var_name} to: ARGS_NAMES:Doc1.pdf | c^md.exe /c dir c:\\\\
Resolved macro %{tx.0} to: cmd.exe
Set variable "tx.958500-WEB_ATTACK/COMMAND_ACCESS-ARGS_NAMES:Doc1.pdf | c^md.exe /c dir c:\\\\\\\\" to "cmd.exe".
Resolved macro %{TX.0} to: cmd.exe
Warning. Pattern match "\\bcmd\\.exe\\b" at ARGS_NAMES:Doc1.pdf | c^md.exe /c dir c:\\. [file "/usr/local/apache/conf/modsec_current/base_rules/modsecurity_crs_40_generic_attacks.conf"] [line "281"] [id "958500"] [rev "2.1.1"]
[msg "System Command Access"] [data "cmd.exe"] [severity "CRITICAL"] [tag "WEB_ATTACK/FILE_INJECTION"] [tag "WASCTC/WASC-31"] [tag "OWASP_TOP_10/A1"] [tag "PCI/6.5.2"]
Community developer support for ModSecurity is available on the mod-security-developers mailing list. You mustsubscribe first in order to post. The list archives are available as Developer Archives.
Trustwave is a globally recognized cybersecurity leader that reduces cyber risk and fortifies organizations against disruptive and damaging cyber threats. Our comprehensive offensive and defensive cybersecurity portfolio detects what others cannot, responds with greater speed and effectiveness, optimizes client investment, and improves security resilience. Learn more about us.
Copyright © 2024 Trustwave Holdings, Inc. All rights reserved.