Path Interception by PATH Environment Variable
Last updated
Last updated
Adversaries may execute their own malicious payloads by hijacking environment variables used to load libraries. Adversaries may place a program in an earlier entry in the list of directories stored in the PATH environment variable, which Windows will then execute when it searches sequentially through that PATH listing in search of the binary that was called form a script or the command line.
The PATH environment variable contains a list of directories. Certain methods of executing a program (namely using cmd.exe or the command-line) rely solely on the PATH environment variable to determine the locations that are searched for a program when the path for the program is not given. If any directories are listed in the PATH environment variable before the Windows directory %SystemRoot%\System32 (e.g.: C:\Windows\System32), a program may be placed in the preceding directory that is named the same as a Windows program (such as cmd, PowerShell, or python), which will be executed when that command is executed from a script or command-line.
For example, if C:\example Path precedes C:\Windows\System32 is in the PATH environment variable, a program that is named net.exe and placed in C:\example Path will be called instead of the Windows system "net" when "net" is executed from the command-line.
Example:
It's possible to abuse the %Path% variable environment variable to elevate privileges as long as the user has permissions to (W) Write and it comes BEFORE C:\Windows\System32.
By using the set path= we can set the path we have control of.
Let us check our PATH variable and see how it looks
Ok so now I will add our malicious payload which in this case it's calc.exe replacing net.exe.
We add our new path and make sure this is before C:\Windows\System32.
Perfect, now I will run net.exe regularly and Calc should prompt instead.
Adversaries can place a malicious program in an earlier entry in the list of directories stored in the PATH environment variable, resulting in the operating system executing the malicious binary rather than the legitimate binary when it searches sequentially through that PATH listing.
Adversaries may also directly modify the $PATH variable specifying the directories to be searched. Am adversary can modify the $PATH variable to point to a directory they have write access. When a program using the $PATH variable is called, the OS searches the specified directory and executes the malicious binary.
Notice in my new terminal I've renamed an ELF binary called the same as the "pigz" tool then I utilized the "which" command to tell me where is the binary located at which in this case it's actually now pointing at the new path BEFORE it reaches to the legitimate path then simply calling the tools regularly I would get an execution of CALC