Trustwave and Cybereason Merge to Form Global MDR Powerhouse for Unparalleled Cybersecurity Value. Learn More

Trustwave and Cybereason Merge to Form Global MDR Powerhouse for Unparalleled Cybersecurity Value. Learn More

Services
Managed Detection & Response

Eliminate active threats with 24/7 threat detection, investigation, and response.

Co-Managed SOC (SIEM)

Maximize your SIEM investment, stop alert fatigue, and enhance your team with hybrid security operations support.

Advisory & Diagnostics

Advance your cybersecurity program and get expert guidance where you need it most.

Penetration Testing

Test your physical locations and IT infrastructure to shore up weaknesses before exploitation.

Database Security

Prevent unauthorized access and exceed compliance requirements.

Email Security

Stop email threats others miss and secure your organization against the #1 ransomware attack vector.

Digital Forensics & Incident Response

Prepare for the inevitable with 24/7 global breach response in-region and available on-site.

Firewall & Technology Management

Mitigate risk of a cyberattack with 24/7 incident and health monitoring and the latest threat intelligence.

Solutions
BY TOPIC
Microsoft Security
Unlock the full power of Microsoft Security
Offensive Security
Solutions to maximize your security ROI
Rapidly Secure New Environments
Security for rapid response situations
Securing the Cloud
Safely navigate and stay protected
Securing the IoT Landscape
Test, monitor and secure network objects
Why Trustwave
About Us
Awards and Accolades
Trustwave SpiderLabs Team
Trustwave Fusion Security Operations Platform
Trustwave Security Colony
Partners
Technology Alliance Partners
Key alliances who align and support our ecosystem of security offerings
Trustwave PartnerOne Program
Join forces with Trustwave to protect against the most advance cybersecurity threats

ModSecurity Advanced Topic of the Week: New Community Contribution - cmdLine Transformation Function

Community Contribution - cmdLine

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 ...
  • backslash in the middle of a Unix command - who\ami

The cmdLine transformation function avoids this problem by manipulating the variable contend in the following ways:

  • deleting all backslashes [\]
  • deleting all double quotes ["]
  • deleting all sigle quotes [']
  • deleting all carets [^]
  • deleting spaces before a slash [/]
  • deleting spaces before an open parentesis [(]
  • replacing all commas [,] and semicolon [;] into a space
  • replacing all multiple spaces (including tab, newline, etc.) into one space
  • transform all characters to lowercase

Example Usage

SecRule ARGS "(?:command(?:.com)?|cmd(?:.exe)?)(?:/.*)?/[ck]" \ "phase:2,t:none,t:cmdLine"

Example Detection

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}"

Example Evasion

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:\

cmdLine Normalization

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 Development Support

We have created a Developer page on the modsecurity.org site to help aid community contributions by extending code in one of the following three ways:

  1. Modifying the source code directly
  2. Building custom modsecurity modules
  3. Utilizing the Lua API

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.

ABOUT TRUSTWAVE

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.

Latest Intelligence

Discover how our specialists can tailor a security program to fit the needs of
your organization.

Request a Demo