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 $IFS

  • Use newline operator %0a to separate the payload from the IP address

  • Forward 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

  1. Base64 encoding is essential when dealing with filtered pipe operators (|)

  2. Space filter bypass is still required even with encoding (%09 or $IFS)

  3. Newline injection (%0a) remains the primary injection operator

  4. Burp Suite interception is necessary to modify requests client-side validation

  5. 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

  1. Invoke-Obfuscation (PowerShell)

  2. Bashfuscator (Bash)

  3. DOSfuscation (CMD)

  4. 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

  1. Start Simple - Basic quote obfuscation

  2. Add Complexity - Case manipulation

  3. Use Encoding - Base64/hex when needed

  4. Combine Methods - Multiple techniques together

  5. 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