# T1548.001 - Abuse Elevation Control Mechanism: Setuid and Setgid

## Description from ATT&CK

> 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 Permissions](https://attack.mitre.org/techniques/T1222/002)). The <code>chmod</code> command can set these bits with bitmasking, <code>chmod 4777 [file]</code> or via shorthand naming, <code>chmod u+s [file]</code>. This will enable the setuid bit. To enable the setgid bit, <code>chmod 2775</code> and <code>chmod g+s</code> 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 via <code>ls -l</code>. The <code>find</code> command can also be used to search for such files. For example, <code>find / -perm +4000 2>/dev/null</code> can be used to find files with setuid set and <code>find / -perm +2000 2>/dev/null</code> may be used for setgid. Binaries that have these bits set may then be abused by adversaries.(Citation: GTFOBins Suid)

[Source](https://attack.mitre.org/techniques/T1548/001)

## Atomic Tests

- [Atomic Test #1: Make and modify binary from C source](#atomic-test-1-make-and-modify-binary-from-c-source)
- [Atomic Test #2: Make and modify binary from C source (freebsd)](#atomic-test-2-make-and-modify-binary-from-c-source-freebsd)
- [Atomic Test #3: Set a SetUID flag on file](#atomic-test-3-set-a-setuid-flag-on-file)
- [Atomic Test #4: Set a SetUID flag on file (freebsd)](#atomic-test-4-set-a-setuid-flag-on-file-freebsd)
- [Atomic Test #5: Set a SetGID flag on file](#atomic-test-5-set-a-setgid-flag-on-file)
- [Atomic Test #6: Set a SetGID flag on file (freebsd)](#atomic-test-6-set-a-setgid-flag-on-file-freebsd)
- [Atomic Test #7: Make and modify capabilities of a binary](#atomic-test-7-make-and-modify-capabilities-of-a-binary)
- [Atomic Test #8: Provide the SetUID capability to a file](#atomic-test-8-provide-the-setuid-capability-to-a-file)
- [Atomic Test #9: Do reconnaissance for files that have the setuid bit set](#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](#atomic-test-10-do-reconnaissance-for-files-that-have-the-setgid-bit-set)

### 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

**auto_generated_guid:** `896dfe97-ae43-4101-8e96-9a7996555d80`

#### Inputs

| Name | Description | Type | Default Value |
|------|-------------|------|---------------|
| payload | hello.c payload | path | PathToAtomicsFolder/T1548.001/src/hello.c|

#### Attack Commands: Run with `sh`! Elevation Required (e.g. root or admin)

```sh
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
```

#### Cleanup Commands

```sh
sudo rm /tmp/hello
sudo rm /tmp/hello.c
```
### 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

**auto_generated_guid:** `dd580455-d84b-481b-b8b0-ac96f3b1dc4c`

#### Inputs

| Name | Description | Type | Default Value |
|------|-------------|------|---------------|
| payload | hello.c payload | path | PathToAtomicsFolder/T1548.001/src/hello.c|

#### Attack Commands: Run with `sh`! Elevation Required (e.g. root or admin)

```sh
cp #{payload} /tmp/hello.c
chown root /tmp/hello.c
make /tmp/hello
chown root /tmp/hello
chmod u+s /tmp/hello
/tmp/hello
```

#### Cleanup Commands

```sh
rm /tmp/hello
rm /tmp/hello.c
```
### Atomic Test #3: Set a SetUID flag on file

This test sets the SetUID flag on a file in FreeBSD.

**Supported Platforms:** macOS, Linux

**auto_generated_guid:** `759055b3-3885-4582-a8ec-c00c9d64dd79`

#### Inputs

| Name | Description | Type | Default Value |
|------|-------------|------|---------------|
| file_to_setuid | Path of file to set SetUID flag | path | /tmp/evilBinary|

#### Attack Commands: Run with `sh`! Elevation Required (e.g. root or admin)

```sh
sudo touch #{file_to_setuid}
sudo chown root #{file_to_setuid}
sudo chmod u+xs #{file_to_setuid}
```

#### Cleanup Commands

```sh
sudo rm #{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

**auto_generated_guid:** `9be9b827-ff47-4e1b-bef8-217db6fb7283`

#### Inputs

| Name | Description | Type | Default Value |
|------|-------------|------|---------------|
| file_to_setuid | Path of file to set SetUID flag | path | /tmp/evilBinary|

#### Attack Commands: Run with `sh`! Elevation Required (e.g. root or admin)

```sh
touch #{file_to_setuid}
chown root #{file_to_setuid}
chmod u+xs #{file_to_setuid}
```

#### Cleanup Commands

```sh
rm #{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

**auto_generated_guid:** `db55f666-7cba-46c6-9fe6-205a05c3242c`

#### Inputs

| Name | Description | Type | Default Value |
|------|-------------|------|---------------|
| file_to_setuid | Path of file to set SetGID flag | path | /tmp/evilBinary|

#### Attack Commands: Run with `sh`! Elevation Required (e.g. root or admin)

```sh
sudo touch #{file_to_setuid}
sudo chown root #{file_to_setuid}
sudo chmod g+xs #{file_to_setuid}
```

#### Cleanup Commands

```sh
sudo rm #{file_to_setuid}
```
### Atomic Test #6: Set a SetGID flag on file (freebsd)

This test sets the SetGID flag on a file in FreeBSD.

**Supported Platforms:** Linux

**auto_generated_guid:** `1f73af3