# T1548.001 - Setuid and Setgid
## [Description from ATT&CK](https://attack.mitre.org/techniques/T1548/001)
<blockquote>An adversary may perform shell escapes or exploit vulnerabilities in an application with the setsuid or setgid bits to get code running in a different user’s context. On Linux or macOS, when the setuid or setgid bits are set for an application, 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 doesn’t need the elevated 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. These bits are indicated with an "s" instead of an "x" when viewing a file's attributes via <code>ls -l</code>. The <code>chmod</code> program can set these bits with via bitmasking, <code>chmod 4777 [file]</code> or via shorthand naming, <code>chmod u+s [file]</code>.

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).</blockquote>

## 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 - Set a SetUID flag on file](#atomic-test-2---set-a-setuid-flag-on-file)

- [Atomic Test #3 - Set a SetGID flag on file](#atomic-test-3---set-a-setgid-flag-on-file)

- [Atomic Test #4 - Make and modify capabilities of a binary](#atomic-test-4---make-and-modify-capabilities-of-a-binary)

- [Atomic Test #5 - Provide the SetUID capability to a file](#atomic-test-5---provide-the-setuid-capability-to-a-file)


<br/>

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





<br/>
<br/>

## Atomic Test #2 - Set a SetUID flag on file
This test sets the SetUID flag on a file in Linux and macOS.

**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+s #{file_to_setuid}
```

#### Cleanup Commands:
```sh
sudo rm #{file_to_setuid}
```





<br/>
<br/>

## Atomic Test #3 - 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+s #{file_to_setuid}
```

#### Cleanup Commands:
```sh
sudo rm #{file_to_setuid}
```





<br/>
<br/>

## Atomic Test #4 - Make and modify capabilities of a binary
Make and modify [capabilities](https://man7.org/linux/man-pages/man7/capabilities.7.html) 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 `setuid(0)`.
Without being owned by root, the binary can set the UID to 0.

**Supported Platforms:** Linux


**auto_generated_guid:** db53959c-207d-4000-9e7a-cd8eb417e072





#### Inputs:
| Name | Description | Type | Default Value |
|------|-------------|------|---------------|
| payload | cap.c payload | Path | PathToAtomicsFolder/T1548.001/src/cap.c|


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


```sh
cp #{payload} /tmp/cap.c
make /tmp/cap
sudo setcap cap_setuid=ep /tmp/cap
/tmp/cap
```

#### Cleanup Commands:
```sh
rm /tmp/cap
rm /tmp/cap.c
```





<br/>
<br/>

## Atomic Test #5 - Provide the SetUID capability to a file
This test gives a file the capability to set UID without using flags.

**Supported Platforms:** Linux


**auto_generated_guid:** 1ac3272f-9bcf-443a-9888-4b1d3de785c1





#### Inputs:
| Name | Description | Type | Default Value |
|------|-------------|------|---------------|
| file_to_setcap | Path of file to provide the SetUID capability | Path | /tmp/evilBinary|


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


```sh
touch #{file_to_setcap}
sudo setcap cap_setuid=ep #{file_to_setcap}
```

#### Cleanup Commands:
```sh
rm #{file_to_setcap}
```





<br/>
