# T1543.002 - Create or Modify System Process: SysV/Systemd Service

## Description from ATT&CK

> Adversaries may create or modify systemd services to repeatedly execute malicious payloads as part of persistence. Systemd is a system and service manager commonly used for managing background daemon processes (also known as services) and other system resources.(Citation: Linux man-pages: systemd January 2014) Systemd is the default initialization (init) system on many Linux distributions replacing legacy init systems, including SysVinit and Upstart, while remaining backwards compatible.
> 
> Systemd utilizes unit configuration files with the `.service` file extension to encode information about a service's process. By default, system level unit files are stored in the `/systemd/system` directory of the root owned directories (`/`). User level unit files are stored in the `/systemd/user` directories of the user owned directories (`$HOME`).(Citation: lambert systemd 2022)
> 
> Inside the `.service` unit files, the following directives are used to execute commands:(Citation: freedesktop systemd.service)
> 
> * `ExecStart`, `ExecStartPre`, and `ExecStartPost` directives execute when a service is started manually by `systemctl` or on system start if the service is set to automatically start.
> * `ExecReload` directive executes when a service restarts.
> * `ExecStop`, `ExecStopPre`, and `ExecStopPost` directives execute when a service is stopped.
> 
> Adversaries have created new service files, altered the commands a `.service` file’s directive executes, and modified the user directive a `.service` file executes as, which could result in privilege escalation. Adversaries may also place symbolic links in these directories, enabling systemd to find these payloads regardless of where they reside on the filesystem.(Citation: Anomali Rocke March 2019)(Citation: airwalk backdoor unix systems)(Citation: Rapid7 Service Persistence 22JUNE2016)
> 
> The `.service` file’s User directive can be used to run service as a specific user, which could result in privilege escalation based on specific user/group permissions.
> 
> Systemd services can be created via systemd generators, which support the dynamic generation of unit files. Systemd generators are small executables that run during boot or configuration reloads to dynamically create or modify systemd unit files by converting non-native configurations into services, symlinks, or drop-ins (i.e., [Boot or Logon Initialization Scripts](https://attack.mitre.org/techniques/T1037)).(Citation: Elastic Security Labs Linux Persistence 2024)(Citation: Pepe Berba Systemd 2022)

[Source](https://attack.mitre.org/techniques/T1543/002)

## Atomic Tests

- [Atomic Test #1: Create Systemd Service](#atomic-test-1-create-systemd-service)
- [Atomic Test #2: Create SysV Service](#atomic-test-2-create-sysv-service)
- [Atomic Test #3: Create Systemd Service file,  Enable the service , Modify and Reload the service.](#atomic-test-3-create-systemd-service-file--enable-the-service--modify-and-reload-the-service)

### Atomic Test #1: Create Systemd Service

This test creates a Systemd service unit file and enables it as a service.

**Supported Platforms:** Linux

**auto_generated_guid:** `d9e4f24f-aa67-4c6e-bcbf-85622b697a7c`

#### Inputs

| Name | Description | Type | Default Value |
|------|-------------|------|---------------|
| systemd_service_path | Path to systemd service unit file | path | /etc/systemd/system|
| systemd_service_file | File name of systemd service unit file | string | art-systemd-service.service|
| execstoppost_action | ExecStopPost action for Systemd service | string | /bin/touch /tmp/art-systemd-execstoppost-marker|
| execreload_action | ExecReload action for Systemd service | string | /bin/touch /tmp/art-systemd-execreload-marker|
| execstart_action | ExecStart action for Systemd service | string | /bin/touch /tmp/art-systemd-execstart-marker|
| execstop_action | ExecStop action for Systemd service | string | /bin/touch /tmp/art-systemd-execstop-marker|
| execstartpre_action | ExecStartPre action for Systemd service | string | /bin/touch /tmp/art-systemd-execstartpre-marker|
| execstartpost_action | ExecStartPost action for Systemd service | string | /bin/touch /tmp/art-systemd-execstartpost-marker|

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

```bash
echo "[Unit]" > #{systemd_service_path}/#{systemd_service_file}
echo "Description=Atomic Red Team Systemd Service" >> #{systemd_service_path}/#{systemd_service_file}
echo "" >> #{systemd_service_path}/#{systemd_service_file}
echo "[Service]" >> #{systemd_service_path}/#{systemd_service_file}
echo "Type=simple"
echo "ExecStart=#{execstart_action}" >> #{systemd_service_path}/#{systemd_service_file}
echo "ExecStartPre=#{execstartpre_action}" >> #{systemd_service_path}/#{systemd_service_file}
echo "ExecStartPost=#{execstartpost_action}" >> #{systemd_service_path}/#{systemd_service_file}
echo "ExecReload=#{execreload_action}" >> #{systemd_service_path}/#{systemd_service_file}
echo "ExecStop=#{execstop_action}" >> #{systemd_service_path}/#{systemd_service_file}
echo "ExecStopPost=#{execstoppost_action}" >> #{systemd_service_path}/#{systemd_service_file}
echo "" >> #{systemd_service_path}/#{systemd_service_file}
echo "[Install]" >> #{systemd_service_path}/#{systemd_service_file}
echo "WantedBy=default.target" >> #{systemd_service_path}/#{systemd_service_file}
systemctl daemon-reload
systemctl enable #{systemd_service_file}
systemctl start #{systemd_service_file}
```

#### Cleanup Commands

```bash
systemctl stop #{systemd_service_file}
systemctl disable #{systemd_service_file}
rm -rf #{systemd_service_path}/#{systemd_service_file}
systemctl daemon-reload
```
### Atomic Test #2: Create SysV Service

This test creates a SysV service unit file and enables it as a service.

**Supported Platforms:** Linux

**auto_generated_guid:** `760fe8d2-79d9-494f-905e-a239a3df86f6`

#### Inputs

| Name | Description | Type | Default Value |
|------|-------------|------|---------------|
| rc_service_path | Path to rc service file | path | /usr/local/etc/rc.d|
| rc_service_file | File name of rc service file | string | art-test|

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

```sh
echo '#\!/bin/sh' > #{rc_service_path}/#{rc_service_file}
echo ' ' >> #{rc_service_path}/#{rc_service_file}
echo '#' >> #{rc_service_path}/#{rc_service_file}
echo '# PROVIDE: art-test' >> #{rc_service_path}/#{rc_service_file}
echo '# REQUIRE: LOGIN' >> #{rc_service_path}/#{rc_service_file}
echo '# KEYWORD: shutdown' >> #{rc_service_path}/#{rc_service_file}
echo ' ' >> #{rc_service_path}/#{rc_service_file}
echo '. /etc/rc.subr' >> #{rc_service_path}/#{rc_service_file}
echo ' ' >> #{rc_service_path}/#{rc_service_file}
echo 'name="art_test"' >> #{rc_service_path}/#{rc_service_file}
echo 'rcvar=art_test_enable' >> #{rc_service_path}/#{rc_service_file}
echo 'load_rc_config ${na