T1548.001 Setuid and Setgid
Sub-technique of: T1548
High-Level Description
An adversary may abuse configurations where an application has the setuid or setgid bits set in order to get code running in a different (and possibly more privileged) user’s context. On Linux or macOS, when the setuid or setgid bits are set for an application binary, the application will run with the privileges of the owning user or group respectively. Normally an application is run in the current user’s context, regardless of which user or group owns the application. However, there are instances where programs need to be executed in an elevated context to function properly, but the user running them may not have the specific required privileges.
Instead of creating an entry in the sudoers file, which must be done by root, any user can specify the setuid or setgid flag to be set for their own applications (i.e. Linux and Mac File and Directory Permissions Modification). The chmod command can set these bits with bitmasking, chmod 4777 [file] or via shorthand naming, chmod u+s [file]. This will enable the setuid bit. To enable the setgid bit, chmod 2775 and chmod g+s can be used.
Adversaries can use this mechanism on their own malware to make sure they're able to execute in elevated contexts in the future. This abuse is often part of a "shell escape" or other actions to bypass an execution environment with restricted permissions.
Alternatively, adversaries may choose to find and target vulnerable binaries with the setuid or setgid bits already enabled (i.e. File and Directory Discovery). The setuid and setguid bits are indicated with an "s" instead of an "x" when viewing a file's attributes via ls -l. The find command can also be used to search for such files. For example, find / -perm +4000 2>/dev/null can be used to find files with setuid set and find / -perm +2000 2>/dev/null may be used for setgid. Binaries that have these bits set may then be abused by adversaries.
Kill Chain Phase
- Privilege Escalation (TA0004)
- Defense Evasion (TA0005)
Platforms: Linux, macOS
What to Check
- Identify if Setuid and Setgid technique is applicable to target environment
- Check Linux systems for indicators of Setuid and Setgid
- Check macOS systems for indicators of Setuid and Setgid
- Verify mitigations are bypassed or absent (1 known mitigations)
- Assess detection coverage (1 detection strategies)
How to Test
Atomic Red Team Tests
The following tests are from Atomic Red Team and provide actionable ways to test this technique:
Atomic Test 1: Make and modify binary from C source
Make, change owner, and change file attributes on a C source code file
Supported Platforms: macos, linux Elevation Required: Yes
cp #{payload} /tmp/hello.c
sudo chown root /tmp/hello.c
sudo make /tmp/hello
sudo chown root /tmp/hello
sudo chmod u+s /tmp/hello
/tmp/hello
Atomic Test 2: Make and modify binary from C source (freebsd)
Make, change owner, and change file attributes on a C source code file
Supported Platforms: linux Elevation Required: Yes
cp #{payload} /tmp/hello.c
chown root /tmp/hello.c
make /tmp/hello
chown root /tmp/hello
chmod u+s /tmp/hello
/tmp/hello
Atomic Test 3: Set a SetUID flag on file
This test sets the SetUID flag on a file in FreeBSD.
Supported Platforms: macos, linux Elevation Required: Yes
sudo touch #{file_to_setuid}
sudo chown root #{file_to_setuid}
sudo chmod u+xs #{file_to_setuid}
Atomic Test 4: Set a SetUID flag on file (freebsd)
This test sets the SetUID flag on a file in FreeBSD.
Supported Platforms: linux Elevation Required: Yes
touch #{file_to_setuid}
chown root #{file_to_setuid}
chmod u+xs #{file_to_setuid}
Atomic Test 5: Set a SetGID flag on file
This test sets the SetGID flag on a file in Linux and macOS.
Supported Platforms: macos, linux Elevation Required: Yes
sudo touch #{file_to_setuid}
sudo chown root #{file_to_setuid}
sudo chmod g+xs #{file_to_setuid}
Manual Testing
If Atomic Red Team tests are not applicable, manually verify the technique by:
Identify Attack Surface: Determine if the target environment is susceptible to Setuid and Setgid by examining the target platforms (Linux, macOS).
Assess Existing Defenses: Review whether mitigations for T1548.001 are in place. If defenses are absent or misconfigured, this technique may be exploitable.
Execute Test: Use tools and methods described in the MITRE ATT&CK page and external references below.
Remediation Guide
M1028 Operating System Configuration
Applications with known vulnerabilities or known shell escapes should not have the setuid or setgid bits set to reduce potential damage if an application is compromised. Additionally, the number of programs with setuid or setgid bits set should be minimized across a system.
Detection
Setuid/Setgid Privilege Abuse Detection (Linux/macOS)
Risk Assessment
| Finding | Severity | Impact |
|---|---|---|
| Setuid and Setgid technique applicable | High | Privilege Escalation |
CWE Categories
| CWE ID | Title |
|---|---|
| CWE-269 | Improper Privilege Management |