Table of Contents
Summery
PATH is an environment variable that specifies the set of directories where an executable can be located. An account’s PATH variable is a set of absolute paths, allowing a user to type a command without specifying the absolute path to the binary. For example, a user can type cat /tmp/test.txt instead of specifying the absolute path /bin/cat /tmp/test.txt. We can check the contents of the PATH variable by typing env | grep PATH or echo $PATH.
On Windows, PATH works similarly, but directories are separated by ; instead of :. You can view it with echo %PATH% in CMD or $env:PATH in PowerShell. When you execute a command such as whoami, Windows searches the directories listed in PATH for the executable.
PATH Hijacking
If a program executes a command without specifying its absolute path, the OS searches PATH directories in order. If an attacker can place a malicious executable with the expected name in an earlier PATH directory, the program may execute the attacker’s version instead.
Adding . to a user’s PATH adds their current working directory to the list. For example, if we can modify a user’s path, we could replace a common binary such as ls with a malicious script such as a reverse shell. If we add . to the path by issuing the command PATH=.:$PATH and then export PATH, we will be able to run binaries located in our current working directory by just typing the name of the file (i.e. just typing ls will call the malicious script named ls in the current working directory instead of the binary located at /bin/ls).