Path hijacking

Understanding Path Hijacking

What Makes Path Hijacking Dangerous

Path hijacking exploits the way Linux systems resolve executable file locations. When a program calls another program without specifying the full path, the system searches through directories listed in the PATH environment variable. By manipulating this search order or placing malicious executables in writable directories within the PATH, attackers can execute arbitrary code with elevated privileges.

The Attack Principle: Exploit scenarios where:

  • SUID/SGID binaries call other programs without absolute paths

  • Scripts executed by privileged users reference commands without full paths

  • Cron jobs run commands without absolute path specifications

  • Service scripts use relative path references

Why This Works: The system searches PATH directories in order. If you can write to a directory that appears before the legitimate binary's location, your malicious binary will be executed instead.

Path Hijacking Discovery and Enumeration

Finding Vulnerable Binaries

Basic SUID/SGID Discovery:

# Find all SUID binaries
find / -type f -perm -4000 2>/dev/null

# Find all SGID binaries
find / -type f -perm -2000 2>/dev/null

# Find both SUID and SGID
find / -type f \( -perm -4000 -o -perm -2000 \) 2>/dev/null

# Focus on user-writable areas
find /home /tmp /var/tmp -type f \( -perm -4000 -o -perm -2000 \) 2>/dev/null

Analyzing Binary Dependencies:

PATH Environment Analysis:

High-Value Path Hijacking Targets

SUID Binary Analysis

Identifying Vulnerable SUID Programs:

Testing for Path Hijacking:

Cron Job Path Hijacking

Cron Job Discovery:

Exploiting Cron Path Vulnerabilities:

Service Script Hijacking

Service Analysis:

Exploitation Techniques

Basic Path Hijacking Exploit

Standard Exploitation Process:

Advanced PATH Manipulation

PATH Environment Hijacking:

Library Path Hijacking

LD_LIBRARY_PATH Exploitation:

Script-Based Path Hijacking

Shell Script Vulnerabilities

Identifying Vulnerable Scripts:

Script Exploitation Example:

Python Script Path Hijacking

Python Module Hijacking:

Real-World Exploitation Examples

Example 1: SUID Binary with ps Command

Discovery:

Exploitation:

Example 2: Cron Job Exploitation

Discovery:

Exploitation:

Example 3: Custom Application

Discovery:

Exploitation:

Key Operational Considerations

Success Indicators

  • Writable directories found in PATH environment

  • SUID/SGID binaries calling commands with relative paths

  • Cron jobs executing scripts without absolute paths

  • Privilege escalation achieved through path manipulation

Common Failure Points

  • No writable directories in PATH

  • All commands use absolute paths

  • Modern systems with restricted PATH handling

  • AppArmor/SELinux preventing path manipulation

Exploitation Notes

  • Custom applications more likely vulnerable than system binaries

  • Development environments often have relaxed PATH security

  • Cron jobs frequently vulnerable to path hijacking

  • Service scripts may use relative paths for configuration flexibility

Path hijacking remains a reliable privilege escalation technique, particularly effective against custom applications and in environments where PATH security is not strictly controlled.

Last updated

Was this helpful?