Writable PATH Hijacking for Privilege Escalation
When a privileged process calls a command without an absolute path, the OS resolves it by searching each directory in PATH from left to right. If you control a directory that appears before the real binary's location, your malicious version executes instead. This requires finding a writable directory in PATH and identifying at least one unqualified command that a root process calls.
Prerequisites Check
Check your current PATH and which directories you can write to:
echo $PATH
# Find writable directories in PATH
echo $PATH | tr ':' '\n' | while read dir; do
[ -w "$dir" ] && echo "WRITABLE: $dir"
done
Check the system-wide PATH set in common locations:
If the staff group has write access to /usr/local/bin and you are in that group, that is writable PATH. See https://hackindex.io/platforms/linux/privilege-escalation/group-based-privilege-escalation.
Finding What Root Calls Without Absolute Paths
You need to find a command that a root-owned script or cron job calls without a full path like /usr/bin/tar. Use pspy to observe live process invocations:
Watch for UID=0 processes and note the exact command line. Look for short command names without leading /.
Also check cron jobs and systemd services statically:
Confirming the Resolution Order
Before planting, confirm that the binary you identified resolves to a system path — not an absolute call:
If the script sets PATH=/usr/bin:/bin with no writable directory, hijacking is not viable for that script. If it sets PATH=/tmp:/usr/bin or does not set PATH at all (inheriting from cron's minimal PATH), check where cron's PATH directories are writable.
Planting the Malicious Binary
Once you have confirmed a writable directory that appears before the real binary:
SUID bash — most reliable payload:
Wait for the privileged process to run. Then:
Reverse shell payload:
Start your listener first:
Common Writable PATH Scenarios
/usr/local/bin writable (staff group or misconfigured permissions):
Cron PATH with /tmp prepended:
When /etc/crontab has PATH=/tmp:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:
Current directory (.) in PATH:
If PATH contains . or starts with : (implicit current directory), any directory you are in when a root process runs becomes a hijack target:
Plant a binary in any directory a root process changes into before executing the command.
Confirming Execution
After planting, confirm the binary runs when triggered:
Check for /tmp/.hijack_proof after the next cron interval or service restart. If it exists, your payload ran. If /tmp/.rootbash is also SUID:
Cleanup
Remove your planted binary and the SUID shell after escalating to avoid leaving artefacts:
References
-
which(1) Manualman7.org/linux/man-pages/man1/which.1.html (opens in new tab)
Command resolution and PATH search order.
-
crontab(5) Manualman7.org/linux/man-pages/man5/crontab.5.html (opens in new tab)
PATH variable behavior in cron environments.
Was this helpful?
Your feedback helps improve this page.