PATH Hijacking
Concept
If a privileged script/cron job/SUID binary calls another program by name (not an absolute path), and a directory earlier in that process's PATH is writable, you can place a malicious binary with the same name to have it executed with elevated privileges.
When to Use
- A sudo-allowed or cron-run script calls commands without full paths (e.g. 'service' instead of '/usr/sbin/service').
Workflow
- 1Find a script/binary that calls commands by relative name
Read source if accessible.
- 2Check the PATH the privileged process uses
Can differ from your own interactive shell's PATH.
- 3Find a writable directory in that PATH
Or prepend one if you control invocation (sudo without secure_path).
- 4Drop a malicious binary with the hijacked name
Make it executable.
- 5Trigger execution
Run the sudo command / wait for cron.
Commands
cat SCRIPT_PATHcat /opt/maintenance.sh
Read the privileged script's source to find commands invoked without an absolute path.
When to use: Whenever you can read a script that sudo/cron executes as another user.
Expected output:
#!/bin/bash service apache2 restart
Look for: 'service', 'python', or any command not prefixed with /usr/bin/ etc.
Next step: Check if you can write to a directory in PATH before the real binary's location.
echo $PATH; echo 'PAYLOAD' > WRITABLE_DIR/COMMAND_NAME && chmod +x WRITABLE_DIR/COMMAND_NAMEecho $PATH; echo '/bin/bash -p' > /tmp/service && chmod +x /tmp/service; export PATH=/tmp:$PATH
Creates a malicious 'service' binary in a writable, earlier-in-PATH directory, then (where you control invocation) prepends it to PATH.
When to use: When sudo config doesn't set secure_path, or the cron job sources an environment you can influence.
Expected output:
No output from the echo/chmod; effect seen on next invocation of the hijacked command.
Look for: Confirm /tmp (or wherever) now appears before the real binary's directory in PATH resolution (which path).
Next step: Trigger the sudo command or wait for the cron run; catch the resulting privileged shell/action.
Tools
which / type
Confirm which binary a bare command name resolves to given the current PATH.
When to use: Before and after a hijack attempt, to verify resolution order.
which serviceExample workflow: which service before and after modifying PATH to confirm the hijack binary is chosen.
Findings & Interpretation
Interesting
- Scripts invoked by sudo/cron that call commands without absolute paths
- sudoers entries missing secure_path restrictions
False Positives
- sudo on modern distros usually resets PATH via secure_path — PATH hijack mainly works on cron jobs or custom scripts invoked outside sudo's controlled environment
How to Validate
- Confirm with 'sudo -l' whether env_reset/secure_path apply before relying on this technique against a sudo-run command
Next Step
- If sudo resets PATH, target cron jobs (which run with their own, often looser, environment) instead.
Troubleshooting
Hijack binary never executes even though it's first in PATH.
Likely cause: The privileged process uses an absolute path internally for that specific command, or sudo secure_path overrides your PATH.
Verify: Re-read the script carefully for an absolute path you missed; check sudoers for secure_path.
Fix: Target a different command in the script that is called without an absolute path.
Recovery: Fall back to script content editing if the script itself is writable, instead of relying on PATH.
Avoid repeating: Confirm env_reset/secure_path settings before assuming PATH hijack will work under sudo.