Bypassing Web Application Firewalls with Shell Globbing
Follow me on Medium for more blogs!
Introduction
Web Application Firewalls (WAFs) are a critical line of defense for modern web applications, meticulously inspecting incoming traffic to identify and block malicious requests. While they offer robust protection, WAFs are not infallible. Attackers are constantly innovating, devising new techniques to circumvent these security measures. One such technique, often overlooked, is the exploitation of shell globbing — a powerful feature inherent in Unix-like operating systems. This blog post delves into the intricacies of shell globbing, demonstrating how it can be strategically employed to evade WAFs and execute OS command injection attacks. We’ll also explore the limitations of this approach, discuss essential mitigation strategies for robust web application security, and examine real-world examples, including specific WAF evasion scenarios.
As highlighted by the OWASP Top 10, “Injection” flaws are a major concern. Remote Command Execution (RCE) vulnerabilities, a subset of injection attacks, allow attackers to execute arbitrary commands on the server. While modern WAFs aim to block these attempts, Linux systems offer a variety of ways to bypass WAF rules. One of the penetration tester’s biggest friends is “wildcard”.
Understanding Shell Globbing
Shell globbing, also known as wildcard expansion or filename generation, is a mechanism that allows the shell to interpret special characters as patterns, expanding them into matching filenames and directories on the filesystem before a command is executed. Think of it as a way to represent multiple files or paths with a single, concise pattern. The most common globbing characters are:
These globbing patterns, documented in man 7 glob, are often used in the command line. However, the power to expand patterns with characters like ?, /, numbers and letters allows for executing system commands and enumerating files.
The Exploit: How Globbing Evades WAFs
WAFs often use pattern matching or regular expressions to detect malicious inputs. For instance, a WAF rule might block requests containing /etc/passwd or /bin/ls to prevent unauthorized access to the system's user database or execution of system commands. However, if the WAF only looks for the explicit string /etc/passwd , it is vulnerable to techniques leveraging globbing.
Here’s how globbing comes into play:
Practical Examples: Shell Globbing in Action
领英推荐
Why Globbing Works: The WAF’s Blind Spot
The effectiveness of globbing lies in the fact that many WAFs primarily focus on blocking known malicious strings directly in the input. They often do not interpret the expanded patterns that the shell will eventually execute. The WAF analyzes the request as it comes in from the user, before the server side shell expands it, leading to the bypass. This leaves a gap that attackers can exploit.
The ? wildcard matches any single character. For example ls *.??? will list files with three-character extensions like .gif, .jpg and .txt.
Let’s consider a vulnerable PHP script:
<?php
echo 'ok: ';
print_r($_GET['cmd']);
system($_GET['cmd']);
?>
A direct RCE attempt, such as /?cmd=cat+/etc/passwd, may be blocked by Sucuri WAF with a message like "An attempted RFI/LFI was detected and blocked." However, by utilizing wildcards and encoding, /?cmd=%2f???%2f??t%20%2f???%2fp??s?? can bypass the filter and execute the command, successfully reading /etc/passwd. This shows that a WAF can easily be bypassed if it is not configured with a sufficient paranoia level. It's important to note that this test uses a deliberately vulnerable PHP script and might not represent a realistic scenario, so WAFs should not be judged solely on how many requests they block on such a simple case.
ModSecurity OWASP CRS and Paranoia Levels
ModSecurity, especially with the libmodsecurity v3 and the OWASP Core Rule Set (CRS), is a powerful WAF solution. The CRS utilizes “paranoia levels” (PL) to adjust the strictness of its rules. Here’s a breakdown of how they relate to globbing attacks:
Mitigation Strategies: Strengthening Your Defenses
To safeguard against shell globbing and similar evasion techniques, consider the following mitigation strategies:
Conclusion
Shell globbing is a potent technique that can bypass WAFs by exploiting their limitations in pattern recognition. While not foolproof, it highlights the importance of comprehensive security strategies. Organizations must focus on deploying advanced WAF configurations, performing robust input validation, and implementing comprehensive security measures to protect against these types of attack vectors. Remember, the battle for secure web applications requires constant vigilance and adaptation. Don’t rely solely on WAFs, and instead adopt a layered security approach that includes secure coding practices, continuous monitoring and proactive security audits.
#cybersecurity #bash #linux #bugbounty #commandinjection #exploit
Great insights on vulnerability testing techniques!