Advanced Command Obfuscation
π Sophisticated Evasion: Advanced techniques for bypassing WAFs and sophisticated filtering mechanisms
Overview
In some instances, we may be dealing with advanced filtering solutions, like Web Application Firewalls (WAFs), and basic evasion techniques may not necessarily work. We can utilize more advanced techniques for such occasions, which make detecting the injected commands much less likely.
These advanced methods are particularly useful when:
Basic obfuscation fails
WAF detection is sophisticated
Multiple filter layers are present
Custom filtering solutions are deployed
Case Manipulation
π Character Case Evasion: Exploiting case-sensitivity differences between platforms
Understanding Case Sensitivity
One command obfuscation technique we can use is case manipulation, like inverting the character cases of a command (e.g. WHOAMI) or alternating between cases (e.g. WhOaMi). This usually works because a command blacklist may not check for different case variations of a single word.
Windows Case Manipulation
Windows Advantage: Commands for PowerShell and CMD are case-insensitive, meaning they will execute regardless of case:
Testing on Windows:
Linux Case Manipulation
Linux Challenge: Linux systems are case-sensitive, so we need creative solutions.
Solution: Character Translation
Testing on Linux:
Web Application Testing
Initial Attempt (Fails):
Successful Bypass:
Alternative Case Manipulation (Linux)
Bash Parameter Expansion:
Testing:
Reversed Commands
π String Reversal: Executing commands by reversing them to avoid detection
Concept
Another command obfuscation technique is reversing commands and having a command template that switches them back and executes them in real-time. We write imaohw instead of whoami to avoid triggering the blacklisted command.
Linux Implementation
Step 1: Get Reversed String
Step 2: Execute Reversed Command
Testing:
Web Application Testing
Successful Reversed Command Injection:
Windows Implementation
Step 1: Reverse String in PowerShell
Step 2: Execute with PowerShell Sub-shell
Testing:
Advanced Reversed Commands
Complex Command Reversal:
Note: Character filters must also be considered when reversing - filtered characters should be reversed as well or included when reversing the original command.
Encoded Commands
π Encoding Techniques: Using base64/hex encoding to bypass character filters
Base64 Encoding (Linux)
Concept: Encode commands containing filtered characters to avoid detection and URL-decoding issues.
Step 1: Encode the Payload
Step 2: Create Decoding Command
Testing:
Key Advantages:
β No filtered characters in payload
β Avoids pipe
|character (using<<<instead)β Bypasses URL-decoding issues
Web Application Testing
Successful Base64 Injection:
Base64 Encoding (Windows)
Step 1: Encode String in PowerShell
Cross-Platform Encoding (Linux to Windows):
Step 2: Decode and Execute
Testing:
Alternative Encoding Methods
Hex Encoding (xxd):
OpenSSL Base64 (Alternative):
HTB Academy Lab Solution
Challenge: Advanced Obfuscation
Question: Find the output of the following command using one of the techniques you learned in this section:
Why Base64 Encoding is Required
After spawning the target machine and visiting its website's root webpage, students need to use Burp Suite or ZAP to intercept the request made after clicking the Check button. Since the pipe operator (|) is in the command, students need to use the third method which encodes all characters to avoid filter detection.
Step-by-Step Solution
Step 1: Base64 Encode the Command
Command Output:
Step 2: Create Decoding Command
Step 3: Bypass Filters and Execute
Students need to:
Bypass space character filter by using either
%09(tab) or$IFSUse newline operator
%0ato separate the payload from the IP addressForward the modified intercepted request
Final Payload:
Lab Result
Expected Output:
Students will attain this output after successfully executing the base64-encoded command through the command injection vulnerability.
Alternative Methods (For Reference)
Method 2: Reversed Command
Method 3: Case Manipulation + Encoding
Key Learning Points
Base64 encoding is essential when dealing with filtered pipe operators (
|)Space filter bypass is still required even with encoding (
%09or$IFS)Newline injection (
%0a) remains the primary injection operatorBurp Suite interception is necessary to modify requests client-side validation
Multiple techniques can be combined for complex filtering scenarios
Additional Advanced Techniques
Wildcard Obfuscation
Using Asterisk Wildcards:
Integer Expansion
Using Bash Arithmetic:
Output Redirection
Avoiding Pipes with Redirection:
Environment Variable Exploitation
Advanced PATH Manipulation:
Automated Obfuscation Tools
Recommended Tools
Invoke-Obfuscation (PowerShell)
Bashfuscator (Bash)
DOSfuscation (CMD)
Custom Python Scripts
Tool Integration
These tools can automatically generate obfuscated payloads using the techniques covered in this section, making it easier to bypass sophisticated filters during penetration testing.
Detection Evasion Strategy
Layered Approach
Start Simple - Basic quote obfuscation
Add Complexity - Case manipulation
Use Encoding - Base64/hex when needed
Combine Methods - Multiple techniques together
Custom Creation - Unique payloads for specific filters
Best Practices
β Test incrementally - Add one technique at a time
β Avoid common patterns - Create unique obfuscations
β Consider platform - Use appropriate methods for OS
β Monitor responses - Adjust based on filter behavior
β Document successful methods - For future assessments
This comprehensive approach to advanced command obfuscation provides penetration testers with sophisticated techniques to bypass even the most advanced filtering mechanisms and WAF solutions.
Last updated