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.(Citation: setuid man page) 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](https://attack.mitre.org/techniques/T1222/002)). Thechmod
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
andchmod 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.(Citation: OSX Keydnap malware) 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](https://attack.mitre.org/techniques/T1083)). The setuid and setguid bits are indicated with an "s" instead of an "x" when viewing a file's attributes vials -l
. Thefind
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 andfind / -perm +2000 2>/dev/null
may be used for setgid. Binaries that have these bits set may then be abused by adversaries.(Citation: GTFOBins Suid)
Atomic Test #2 - Make and modify binary from C source (freebsd)
Atomic Test #9 - Do reconnaissance for files that have the setuid bit set
Atomic Test #10 - Do reconnaissance for files that have the setgid bit set
Make, change owner, and change file attributes on a C source code file
Supported Platforms: macOS, Linux
auto_generated_guid: 896dfe97-ae43-4101-8e96-9a7996555d80
| Name | Description | Type | Default Value | |——|————-|——|—————| | payload | hello.c payload | path | PathToAtomicsFolder/T1548.001/src/hello.c|
1
sh
! Elevation Required (e.g. root or admin)1
2
3
4
5
6
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
1
2
sudo rm /tmp/hello
sudo rm /tmp/hello.c
Make, change owner, and change file attributes on a C source code file
Supported Platforms: Linux
auto_generated_guid: dd580455-d84b-481b-b8b0-ac96f3b1dc4c
| Name | Description | Type | Default Value | |——|————-|——|—————| | payload | hello.c payload | path | PathToAtomicsFolder/T1548.001/src/hello.c|
1
sh
! Elevation Required (e.g. root or admin)1
2
3
4
5
6
cp #{payload} /tmp/hello.c
chown root /tmp/hello.c
make /tmp/hello
chown root /tmp/hello
chmod u+s /tmp/hello
/tmp/hello
1
2
rm /tmp/hello
rm /tmp/hello.c
This test sets the SetUID flag on a file in FreeBSD.
Supported Platforms: macOS, Linux
auto_generated_guid: 759055b3-3885-4582-a8ec-c00c9d64dd79
| Name | Description | Type | Default Value | |——|————-|——|—————| | file_to_setuid | Path of file to set SetUID flag | path | /tmp/evilBinary|
1
sh
! Elevation Required (e.g. root or admin)1
2
3
sudo touch #{file_to_setuid}
sudo chown root #{file_to_setuid}
sudo chmod u+xs #{file_to_setuid}
1
sudo rm #{file_to_setuid}
This test sets the SetUID flag on a file in FreeBSD.
Supported Platforms: Linux
auto_generated_guid: 9be9b827-ff47-4e1b-bef8-217db6fb7283
| Name | Description | Type | Default Value | |——|————-|——|—————| | file_to_setuid | Path of file to set SetUID flag | path | /tmp/evilBinary|
1
sh
! Elevation Required (e.g. root or admin)1
2
3
touch #{file_to_setuid}
chown root #{file_to_setuid}
chmod u+xs #{file_to_setuid}
1
rm #{file_to_setuid}
This test sets the SetGID flag on a file in Linux and macOS.
Supported Platforms: macOS, Linux
auto_generated_guid: db55f666-7cba-46c6-9fe6-205a05c3242c
| Name | Description | Type | Default Value | |——|————-|——|—————| | file_to_setuid | Path of file to set SetGID flag | path | /tmp/evilBinary|
1
sh
! Elevation Required (e.g. root or admin)1
2
3
sudo touch #{file_to_setuid}
sudo chown root #{file_to_setuid}
sudo chmod g+xs #{file_to_setuid}
1
sudo rm #{file_to_setuid}
This test sets the SetGID flag on a file in FreeBSD.
Supported Platforms: Linux
auto_generated_guid: 1f73af33-62a8-4bf1-bd10-3bea931f2c0d
| Name | Description | Type | Default Value | |——|————-|——|—————| | file_to_setuid | Path of file to set SetGID flag | path | /tmp/evilBinary|
1
sh
! Elevation Required (e.g. root or admin)1
2
3
touch #{file_to_setuid}
chown root #{file_to_setuid}
chmod g+xs #{file_to_setuid}
1
rm #{file_to_setuid}
Make and modify capabilities of a C source code file.
The binary doesn’t have to modify the UID, but the binary is given the capability to arbitrarily modify it at any time with
.
Without being owned by root, the binary can set the UID to 0.1
setuid(0)
Supported Platforms: Linux
auto_generated_guid: db53959c-207d-4000-9e7a-cd8eb417e072
| Name | Description | Type | Default Value | |——|————-|——|—————| | payload | cap.c payload | path | PathToAtomicsFolder/T1548.001/src/cap.c|
1
sh
! Elevation Required (e.g. root or admin)1
2
3
4
cp #{payload} /tmp/cap.c
make /tmp/cap
sudo setcap cap_setuid=ep /tmp/cap
/tmp/cap
1
2
rm /tmp/cap
rm /tmp/cap.c
This test gives a file the capability to set UID without using flags.
Supported Platforms: Linux
auto_generated_guid: 1ac3272f-9bcf-443a-9888-4b1d3de785c1
| Name | Description | Type | Default Value | |——|————-|——|—————| | file_to_setcap | Path of file to provide the SetUID capability | path | /tmp/evilBinary|
1
sh
! Elevation Required (e.g. root or admin)1
2
touch #{file_to_setcap}
sudo setcap cap_setuid=ep #{file_to_setcap}
1
rm #{file_to_setcap}
This test simulates a command that can be run to enumerate files that have the setuid bit set
Supported Platforms: Linux
auto_generated_guid: 8e36da01-cd29-45fd-be72-8a0fcaad4481
1
sh
!1
find /usr/bin -perm -4000
This test simulates a command that can be run to enumerate files that have the setgid bit set
Supported Platforms: Linux
auto_generated_guid: 3fb46e17-f337-4c14-9f9a-a471946533e2
1
sh
!1
find /usr/bin -perm -2000