# T1053.006 - Scheduled Task/Job: Systemd Timers

## Description from ATT&CK

> Adversaries may abuse systemd timers to perform task scheduling for initial or recurring execution of malicious code. Systemd timers are unit files with file extension <code>.timer</code> that control services. Timers can be set to run on a calendar event or after a time span relative to a starting point. They can be used as an alternative to [Cron](https://attack.mitre.org/techniques/T1053/003) in Linux environments.(Citation: archlinux Systemd Timers Aug 2020) Systemd timers may be activated remotely via the <code>systemctl</code> command line utility, which operates over [SSH](https://attack.mitre.org/techniques/T1021/004).(Citation: Systemd Remote Control)
> 
> Each <code>.timer</code> file must have a corresponding <code>.service</code> file with the same name, e.g., <code>example.timer</code> and <code>example.service</code>. <code>.service</code> files are [Systemd Service](https://attack.mitre.org/techniques/T1543/002) unit files that are managed by the systemd system and service manager.(Citation: Linux man-pages: systemd January 2014) Privileged timers are written to <code>/etc/systemd/system/</code> and <code>/usr/lib/systemd/system</code> while user level are written to <code>\~/.config/systemd/user/</code>.
> 
> An adversary may use systemd timers to execute malicious code at system startup or on a scheduled basis for persistence.(Citation: Arch Linux Package Systemd Compromise BleepingComputer 10JUL2018)(Citation: gist Arch package compromise 10JUL2018)(Citation: acroread package compromised Arch Linux Mail 8JUL2018) Timers installed using privileged paths may be used to maintain root level persistence. Adversaries may also install user level timers to achieve user level persistence.(Citation: Falcon Sandbox smp: 28553b3a9d)

[Source](https://attack.mitre.org/techniques/T1053/006)

## Atomic Tests

- [Atomic Test #1: Create Systemd Service and Timer](#atomic-test-1-create-systemd-service-and-timer)
- [Atomic Test #2: Create a user level transient systemd service and timer](#atomic-test-2-create-a-user-level-transient-systemd-service-and-timer)
- [Atomic Test #3: Create a system level transient systemd service and timer](#atomic-test-3-create-a-system-level-transient-systemd-service-and-timer)

### Atomic Test #1: Create Systemd Service and Timer

This test creates Systemd service and timer then starts and enables the Systemd timer

**Supported Platforms:** Linux

**auto_generated_guid:** `f4983098-bb13-44fb-9b2c[PII_REDACTED]7b`

#### Inputs

| Name | Description | Type | Default Value |
|------|-------------|------|---------------|
| path_to_systemd_service | Path to systemd service unit file | path | /etc/systemd/system/art-timer.service|
| path_to_systemd_timer | Path to service timer file | path | /etc/systemd/system/art-timer.timer|
| systemd_service_name | Name of systemd service | string | art-timer.service|
| systemd_timer_name | Name of systemd service timer | string | art-timer.timer|

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

```bash
echo "[Unit]" > #{path_to_systemd_service}
echo "Description=Atomic Red Team Systemd Timer Service" >> #{path_to_systemd_service}
echo "[Service]" >> #{path_to_systemd_service}
echo "Type=simple" >> #{path_to_systemd_service}
echo "ExecStart=/bin/touch /tmp/art-systemd-timer-marker" >> #{path_to_systemd_service}
echo "[Install]" >> #{path_to_systemd_service}
echo "WantedBy=multi-user.target" >> #{path_to_systemd_service}
echo "[Unit]" > #{path_to_systemd_timer}
echo "Description=Executes Atomic Red Team Systemd Timer Service" >> #{path_to_systemd_timer}
echo "Requires=#{systemd_service_name}" >> #{path_to_systemd_timer}
echo "[Timer]" >> #{path_to_systemd_timer}
echo "Unit=#{systemd_service_name}" >> #{path_to_systemd_timer}
echo "OnCalendar=*-*-* *:*:00" >> #{path_to_systemd_timer}
echo "[Install]" >> #{path_to_systemd_timer}
echo "WantedBy=timers.target" >> #{path_to_systemd_timer}
systemctl start #{systemd_timer_name}
systemctl enable #{systemd_timer_name}
systemctl daemon-reload
```

#### Cleanup Commands

```bash
systemctl stop #{systemd_timer_name}
systemctl disable #{systemd_timer_name}
rm #{path_to_systemd_service}
rm #{path_to_systemd_timer}
systemctl daemon-reload
```
### Atomic Test #2: Create a user level transient systemd service and timer

Schedule a user level transient task (will not survive a reboot) without having to create the .timer or .service files by using the systemd-run command.

**Supported Platforms:** Linux

**auto_generated_guid:** `3de33f5b-62e5-4e63-a2a0-6fd8808c80ec`

#### Attack Commands: Run with `sh`!

```sh
systemd-run --user --unit=Atomic-Red-Team --on-calendar '*:0/1' /bin/sh -c 'echo "$(date) $(whoami)" >>/tmp/log'
```

#### Cleanup Commands

```sh
systemctl --user stop Atomic-Red-Team.service
systemctl --user stop Atomic-Red-Team.timer
rm /tmp/log
```

#### Dependencies: Run with `sh`!

##### Description: Check if systemd-run exists on the machine

###### Check Prereq Commands

```sh
if [ -x "$(command -v systemd-run)" ]; then exit 0; else exit 1; fi;
```

###### Get Prereq Commands

```sh
echo "Install systemd on the machine."; exit 1;
```

### Atomic Test #3: Create a system level transient systemd service and timer

Schedule a system level transient task (will not survive a reboot) without having to create the .timer or .service files by using the systemd-run command.

**Supported Platforms:** Linux

**auto_generated_guid:** `d3eda496-1fc0-49e9-aff5-3bec5da9fa22`

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

```sh
systemd-run --unit=Atomic-Red-Team --on-calendar '*:0/1' /bin/sh -c 'echo "$(date) $(whoami)" >>/tmp/log'
```

#### Cleanup Commands

```sh
systemctl stop Atomic-Red-Team.service
systemctl stop Atomic-Red-Team.timer
rm /tmp/log
```

#### Dependencies: Run with `sh`!

##### Description: Check if systemd-run exists on the machine

###### Check Prereq Commands

```sh
if [ -x "$(command -v systemd-run)" ]; then exit 0; else exit 1; fi;
```

###### Get Prereq Commands

```sh
echo "Install systemd on the machine."; exit 1;
```