Linux Privilege Escalation - SUID/SGID for CTF Creators
SUID/SGID
A Set User ID (SUID) can be misconfigured to elevate privileges. Similarly, a Set Group ID (SGID) could have incorrect permissions in place.
Set UID (SUID)
A file with the SUID bit set will always execute with the privileges of the file’s owner, regardless of the user executing the command. This means that if a file owned by the root user has the SUID bit set, it will execute with root privileges, potentially leading to privilege escalation if misconfigured.
Set GID (SGID)
- On a File: If the SGID bit is set on a file, it allows the file to be executed with the privileges of the group that owns the file.
- On a Directory: If the SGID bit is set on a directory, any files created within that directory will inherit the group ownership of the directory, rather than the primary group of the user who created the file.
Privilege Escalation via SUID
Let’s set a SUID bit to the find
command, to do that we need to locate the find
binary:
|
|
Once we found the absolute path of the find
binary, add a SUID bit to it:
|
|
Then by using the following command, we can enumerate all the binaries and scripts that have the SUID permission set, this is known as the Symbolic Method because it uses symbols.
|
|
Alternatively, we could use the Octal/Numeric Method, which uses octal values:
|
|
Now we know that the SUID bit is enabled for the find
command which means that we can execute any command as the root user using the find
command since the Super User ID (SUID) is 0 (root). Try the following command, which tries to find anything in the current working directory with the command (find .
) then it uses the -exec
parameter which handles the id
command, then we have a \
to escape the semicolon character (;
) because the shell will interpret the semicolon character if it’s not escaped:
|
|
This means that we can execute any command as root, so we could, for example, spawn a bash shell as root:
|
|
For more SUID binary shell escapes we can search on GTFOBins with the SUID tag, as we can see, GTFOBins
has a lot of SUID binary commands that we can use to escalate privileges. This repository is a cheat sheet of binaries. On this site, we can find ways to escalate privileges using SUID bit sets and more.
Privilege Escalation via SGID
Let’s add an SGID
bit to the /usr/bin/find
binary:
|
|
Then by using the following command, we can enumerate all the binaries and scripts having SGID
permission. We can find SGID
binaries using the Symbolic method:
|
|
Alternatively, we can use the Octal/Numeric method, which uses octal values:
|
|
Now we know that the SGID
bit is enabled by using the find
command which means that we can execute any command as the root user using the find
command since the Super Group ID (SGID) is 0 (root). Try the following command, which tries to find anything in the current working directory with the command (find .
) then it uses the -exec
parameter which handles the id
command, then we have a \
to escape the semicolon character (;
) because the shell will interpret the semicolon character if it’s not escaped, lastly we pipe this output and filter the gid
string or text with the grep
command:
|
|
This allows us to read files that are readable by the root group, we can test this by creating a file as root:
|
|
Write any message that we want:
|
|
Change the permissions for other users and groups:
|
|
Now try to read the file as others
:
|
|
Since the find
binary has an SGID bit set we can try to read some files on the system that belong to the root group.
|
|