Path Hijacking and Environment Manipulation
What You'll Learn
You'll learn how attackers exploit environment variables like PATH, LD_PRELOAD, and LD_LIBRARY_PATH to execute malicious code during privilege escalation attacks.
The Core Concept
When Linux programs run, they rely on environment variables to locate executables and shared libraries. These variables act like a phone directory—the system checks them to find what it needs. If an attacker can manipulate these directories or their search order, they can trick the system into loading malicious code instead of legitimate programs.
Think of it like swapping street signs: if you change where "Main Street" points, anyone following those directions ends up at the wrong destination—potentially your trap.
Key Attack Vectors
PATH Hijacking
When a script or SUID binary calls a command without using its full path (like ls instead of /bin/ls), the system searches directories listed in PATH in order. By prepending a writable directory to PATH, you can place a malicious executable that gets found first.
Example scenario: A root-owned script runs cat file.txt. You create a malicious cat script in /tmp, modify PATH=/tmp:$PATH, and your fake cat executes with elevated privileges.
LD_PRELOAD Exploitation
LD_PRELOAD tells the dynamic linker to load specific shared libraries before all others. You can inject a malicious .so file that hijacks function calls. If a SUID binary doesn't sanitize environment variables, your library loads with elevated privileges.
LD_LIBRARY_PATH Manipulation
Similar to LD_PRELOAD, but specifies directories to search for shared libraries. By pointing to a directory containing malicious versions of common libraries, you intercept library loads.
Detection Strategy
During enumeration, look for SUID/sudo programs that preserve environment variables or call commands without absolute paths.
Key Takeaway: Environment variable manipulation turns the system's trust in configuration against itself—always sanitize environments in privileged contexts and use absolute paths in security-sensitive scripts.