Table of Contents
Summery
UID (User ID): Identifies a user account. Example: 1000 might be your user.
GUID (Globally Unique ID): A 128-bit value designed to uniquely identify an object across systems.
SUID (Set User ID): A special permission on a Linux executable. When executed, the program runs with the file owner’s privileges, rather than the privileges of the user executing it. For example, an SUID binary owned by root can execute with root privileges. This is important in privilege-escalation attacks.
EUID (Effective User ID): Which determines the user privileges a process currently has.
GID (Group ID): Identifies a group. Example: 1000 might identify a group your user belongs to.
EGID (Effective Group ID): Which determines the group privileges a process currently has.
PID (Process ID): Identifies a running process. Example: 1234 might be the PID of Firefox.
Commands
find / -perm /4000 -type f 2>/dev/null - Searches for SUID-marked binaries
find / -perm /2000 -type f 2>/dev/null - Searches for SGID-marked binaries
find / -perm /6000 -type f 2>/dev/null - Searches for SUID and SGID-marked binaries
SUID backdoor - A great backdoor for when you can write files into a suid folders.
Exploitation
GTFO bins
You can test if any suid and guids are mentioned in the GTFO bins, there might be some privilege escalation path avaiable to you. I have a script that tests suids automatically listen in Linux privilege escalation checker.
PATH Injection
SUID (and likely GUID) binaries run processes with an effective UID of the process owner (like root) but it usually uses the real (your) UID for PATH inheritance. This meaning, that even if you run a binary as root, you can edit your user’s PATH to decide form where that binary (and all it’s dependencies mentioned in the source code) run. This, in essence allows for PATH injection of a root process by editing the user’s PATH.
Example: Your SUID binary creates archives of files in a given directory. Inside, it uses tar, but it doesn’t call it with /usr/bin/tar, just tar meaning it uses PATH to know which binary called tar in the system to run. If you add a new PATH entry to your PATHs and make file called tar containing a reverse shell, you will be able run your SUID binary and execute that reverse shell as root.
This process is explained in full in PATH injection
Deep dive
Let’s review the purpose behind a setuid binary using a brief example. When a user or a system-automated script launches a process, it inherits the UID/GID of its initiating script: this is known as the real UID/GID.
As previously discussed, user passwords are stored as hashes within /etc/shadow, which is owned and writable only by root (uid=0). How, then, can non-privileged users access this file to change their own password?
To circumvent this issue, the effective UID/GID was introduced, which represents the actual value being checked when performing sensitive operations.
To better demonstrate this concept, let’s analyze the passwd program, which is responsible for changing the password for the user executing it. On the Debian lab machine, we’ll connect as joe and execute the passwd command without typing anything afterwards, so that the process remains active in memory.
joe@debian-privesc:~$ passwd
Changing password for joe.
Current password:
Leaving the program in standby, let’s open another shell as joe to further inspect the process.
To find the PID (process ID) of the passwd program, we can list all processes and filter the output based on the target name:
joe@debian-privesc:~$ ps u -C passwd
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1932 0.0 0.1 9364 2984 pts/0 S+ 01:51 0:00 passwd
Interestingly, passwd is running as the root user: this is needed for it to access and modify /etc/shadow.
We can also inspect the real UID and effective UID assigned for the process by inspecting the proc pseudo-filesystem, which allows us to interact with kernel information. Using the passwd’s PID (1932) from the previous output, let’s inspect the content at /proc/1932/status, which provides a summary of the process attributes:
joe@debian-privesc:~$ grep Uid /proc/1932/status
Uid: 1000 0 0 0
Filtering by the “Uid” keyword returns four parameters that correspond to the real, effective, saved set, and filesystem UIDs. In this case, the Real UID value is 1000, which is expected as it belongs to joe. However, the other three values, including the effective UID, equal the root’s ID 0: let’s consider why.
Under normal circumstances, all four values would belong to the same user who launched the executable. For instance, the bash process for joe (PID 1131 in this case) has the following values:
joe@debian-privesc:~$ cat /proc/1131/status | grep Uid
Uid: 1000 1000 1000 1000
The passwd binary behaves differently because the binary program has a special flag named Set-User-ID, or SUID in short. Let’s inspect it:
joe@debian-privesc:~$ ls -asl /usr/bin/passwd
64 -rwsr-xr-x 1 root root 63736 Jul 27 2018 /usr/bin/passwd
The SUID flag is depicted with the s flag in the above output. This flag can be configured using the chmod u+s <filename> command and it sets the effective UID of the running process to the executable owner’s user ID - in this case root’s.
Using this technique results in a legitimate and constrained privilege escalation and because of this, the SUID binary must be bug-free to avoid any misuse of the application.