Table of Contents
Summery
PATH injection is a privilege escalation technique that exploits how an application locates executables. It affects both Windows and Linux.
Both OSes check their PATH from left to right. It usually looks something like this:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Let’s start w with the expected behavior. Some service, cron job, scheduled task (whatever) wants to run a binary like whoami.
Let’s say that whoami resides in /usr/local/bin. When a service runs whoami by it first checks /usr/local/sbin, sees that there is no whoami, moves to /usr/local/bin, sees it, and runs it.
The idea behind this injection is that you put a malicious whoami file in a directory that is checked before the expected one. So in the above example, you can put a malicious whoami binary in /usr/local/sbin and the system would execute it, rather then the original whoami when the service runs.
Commands
$env:PATH - Check PATH with PowerShell
echo %PATH% - Check PATH with CMD
echo "$PATH" - Check PATH on Linux
Privilege Escalation
In reality, there are two types of exploiting this.
Write rights over the former directory
If you have write rights over a directory that is checked before the target directory you can create a file containing a malicious content like a reverse shell (be it PowerShell one-liner or Basic Bash reverse shell) and wait, or make the target user run the vulnerable binary via PATH.
Rights to add a new PATH variable
Used in: Magic, Pandora
If you have the rights to add a PATH variable it more complicated, as in reality there are usually multiple PATHs. There is one PATH variable per process environment.
The confusion is about which process’s PATH you’re changing. For simplicity, let’s consider two PATHs only - user PATH and root PATH.
Imagine this:
- Your user has a
PATH=/home/user/bin:/usr/bin:/binPATH. - A root service has a
PATH=/usr/sbin:/usr/bin:/sbin:/binPATH. - You add a malicious
backupexecutable to/home/user/bin. - You run
backup- your shell finds your executable first, you get a shell as yourself - not very useful. - The root service runs
backup- it searches its ownPATH, so and it doesn’t find yours/home/user/binthere, so nothing changes.
Therefor, you need to be sure that you can actually edit the PATH that root, target service or target user actually uses. This is a pretty rare scenerio and usually requires administrative privilges in the first place, or some very niche configuration. the PEAS tools do play those, but they are usually just false-positives.
Interesting fact: SUID (and likely GUID) binaries run processes with an effective UID of the process owner (like root) but it usually uses the real (your) UID for PATH inheritance. This meaning, that even if you run a binary as root, you can edit your user’s PATH to decide form where that binary (and all it’s dependencies mentioned in the source code) run. This, in essence allows for PATH injection of a root process by editing the user’s PATH.
If you have correct permissions you can add an entry to PATH with:
$env:Path = "C:\NewDirectory;$env:Path" - For PowerShell
set PATH=C:\NewDirectory;%PATH% - For CMD
export PATH="/new/directory:$PATH" - For Linux
Also for Linux you may want to run echo 'export PATH="/new/directory:$PATH"' >> ~/.bashrc and source ~/.bashrc for persistance.
As far as I know there is no simple way to know if you can edit root’s PATH besides just trying to edit it.
References
Similar concepts: Injections, SQL Injection, Command injection