Systemd Timer Created

Detects the creation of a systemd timer within any of the default systemd timer directories. Systemd timers can be used by an attacker to gain persistence, by scheduling the execution of a command or script. Similarly to cron/at, systemd timers can be set up to execute on boot time, or on a specific point in time, which allows attackers to regain access in case the connection to the infected asset was lost.

Elastic rule (View on GitHub)

  1[metadata]
  2creation_date = "2023/02/24"
  3integration = ["endpoint"]
  4maturity = "production"
  5updated_date = "2024/07/18"
  6
  7[transform]
  8[[transform.osquery]]
  9label = "Osquery - Retrieve File Information"
 10query = "SELECT * FROM file WHERE path = {{file.path}}"
 11
 12[[transform.osquery]]
 13label = "Osquery - Retrieve File Listing Information"
 14query = """
 15SELECT * FROM file WHERE (path LIKE '/etc/systemd/system/%' OR path LIKE '/usr/local/lib/systemd/system/%'
 16OR path LIKE '/lib/systemd/system/%' OR path LIKE '/usr/lib/systemd/system/%'
 17OR path LIKE '/home/{{user.name}}/.config/systemd/user/%' OR path LIKE '/home/{{user.name}}/.local/share/systemd/user/%'
 18OR path LIKE '/root/.config/systemd/user/%' OR path LIKE '/root/.local/share/systemd/user/%' OR path LIKE
 19'/etc/systemd/user/%' OR path LIKE '/usr/lib/systemd/user/%')
 20"""
 21
 22[[transform.osquery]]
 23label = "Osquery - Retrieve Additional File Listing Information"
 24query = """
 25SELECT f.path, u.username AS file_owner, g.groupname AS group_owner, datetime(f.atime, 'unixepoch') AS
 26file_last_access_time, datetime(f.mtime, 'unixepoch') AS file_last_modified_time, datetime(f.ctime, 'unixepoch') AS
 27file_last_status_change_time, datetime(f.btime, 'unixepoch') AS file_created_time, f.size AS size_bytes FROM file f LEFT
 28JOIN users u ON f.uid = u.uid LEFT JOIN groups g ON f.gid = g.gid WHERE ( path LIKE '/etc/systemd/system/%' OR path LIKE
 29'/usr/local/lib/systemd/system/%' OR path LIKE '/lib/systemd/system/%' OR path LIKE '/usr/lib/systemd/system/%' OR path
 30LIKE '/home/{{user.name}}/.config/systemd/user/%' OR path LIKE '/home/{{user.name}}/.local/share/systemd/user/%'
 31OR path LIKE '/root/.config/systemd/user/%' OR path LIKE '/root/.local/share/systemd/user/%' OR path LIKE
 32'/etc/systemd/user/%' OR path LIKE '/usr/lib/systemd/user/%')
 33"""
 34
 35[[transform.osquery]]
 36label = "Osquery - Retrieve Running Processes by User"
 37query = "SELECT pid, username, name FROM processes p JOIN users u ON u.uid = p.uid ORDER BY username"
 38
 39[[transform.osquery]]
 40label = "Osquery - Retrieve Crontab Information"
 41query = "SELECT * FROM crontab"
 42
 43[rule]
 44author = ["Elastic"]
 45description = """
 46Detects the creation of a systemd timer within any of the default systemd timer directories. Systemd timers can be used
 47by an attacker to gain persistence, by scheduling the execution of a command or script. Similarly to cron/at, systemd
 48timers can be set up to execute on boot time, or on a specific point in time, which allows attackers to regain access in
 49case the connection to the infected asset was lost.
 50"""
 51from = "now-9m"
 52index = ["logs-endpoint.events.file*"]
 53language = "eql"
 54license = "Elastic License v2"
 55name = "Systemd Timer Created"
 56note = """## Triage and analysis
 57
 58### Investigating Systemd Timer Created
 59
 60Systemd timers are used for scheduling and automating recurring tasks or services on Linux systems. 
 61
 62Attackers can leverage systemd timers to run scripts, commands, or malicious software at system boot or on a set time interval by creating a systemd timer and a corresponding systemd service file. 
 63
 64This rule monitors the creation of new systemd timer files, potentially indicating the creation of a persistence mechanism.
 65
 66> **Note**:
 67> This investigation guide uses the [Osquery Markdown Plugin](https://www.elastic.co/guide/en/security/master/invest-guide-run-osquery.html) introduced in Elastic Stack version 8.5.0. Older Elastic Stack versions will display unrendered Markdown in this guide.
 68> This investigation guide uses [placeholder fields](https://www.elastic.co/guide/en/security/current/osquery-placeholder-fields.html) to dynamically pass alert data into Osquery queries. Placeholder fields were introduced in Elastic Stack version 8.7.0. If you're using Elastic Stack version 8.6.0 or earlier, you'll need to manually adjust this investigation guide's queries to ensure they properly run.
 69
 70#### Possible Investigation Steps
 71
 72- Investigate the timer file that was created or modified.
 73  - $osquery_0
 74- Investigate the currently enabled systemd timers through the following command `sudo systemctl list-timers`.
 75- Search for the systemd service file named similarly to the timer that was created.
 76- Investigate whether any other files in any of the available systemd directories have been altered through OSQuery.
 77  - $osquery_1
 78  - $osquery_2
 79- Investigate the script execution chain (parent process tree) for unknown processes. Examine their executable files for prevalence and whether they are located in expected locations.
 80  - $osquery_3
 81- Investigate other alerts associated with the user/host during the past 48 hours.
 82- Validate the activity is not related to planned patches, updates, network administrator activity, or legitimate software installations.
 83- Investigate whether the altered scripts call other malicious scripts elsewhere on the file system. 
 84  - If scripts or executables were dropped, retrieve the files and determine if they are malicious:
 85    - Use a private sandboxed malware analysis system to perform analysis.
 86      - Observe and collect information about the following activities:
 87        - Attempts to contact external domains and addresses.
 88          - Check if the domain is newly registered or unexpected.
 89          - Check the reputation of the domain or IP address.
 90        - File access, modification, and creation activities.
 91        - Cron jobs, services and other persistence mechanisms.
 92            - $osquery_4
 93
 94### False Positive Analysis
 95
 96- If this activity is related to new benign software installation activity, consider adding exceptions — preferably with a combination of user and command line conditions.
 97- If this activity is related to a system administrator who uses systemd timers for administrative purposes, consider adding exceptions for this specific administrator user account. 
 98- Try to understand the context of the execution by thinking about the user, machine, or business purpose. A small number of endpoints, such as servers with unique software, might appear unusual but satisfy a specific business need.
 99
100### Response and remediation
101
102- Initiate the incident response process based on the outcome of the triage.
103- Isolate the involved host to prevent further post-compromise behavior.
104- If the triage identified malware, search the environment for additional compromised hosts.
105  - Implement temporary network rules, procedures, and segmentation to contain the malware.
106  - Stop suspicious processes.
107  - Immediately block the identified indicators of compromise (IoCs).
108  - Inspect the affected systems for additional malware backdoors like reverse shells, reverse proxies, or droppers that attackers could use to reinfect the system.
109- Investigate credential exposure on systems compromised or used by the attacker to ensure all compromised accounts are identified. Reset passwords for these accounts and other potentially compromised credentials, such as email, business systems, and web services.
110- Delete the service/timer or restore its original configuration.
111- Run a full antimalware scan. This may reveal additional artifacts left in the system, persistence mechanisms, and malware components.
112- Determine the initial vector abused by the attacker and take action to prevent reinfection through the same vector.
113- Leverage the incident response data and logging to improve the mean time to detect (MTTD) and the mean time to respond (MTTR).
114"""
115references = [
116    "https://opensource.com/article/20/7/systemd-timers",
117    "https://pberba.github.io/security/2022/01/30/linux-threat-hunting-for-persistence-systemd-timers-cron/",
118]
119risk_score = 21
120rule_id = "7fb500fa-8e24-4bd1-9480-2a819352602c"
121setup = """## Setup
122
123This rule requires data coming in from Elastic Defend.
124
125### Elastic Defend Integration Setup
126Elastic Defend is integrated into the Elastic Agent using Fleet. Upon configuration, the integration allows the Elastic Agent to monitor events on your host and send data to the Elastic Security app.
127
128#### Prerequisite Requirements:
129- Fleet is required for Elastic Defend.
130- To configure Fleet Server refer to the [documentation](https://www.elastic.co/guide/en/fleet/current/fleet-server.html).
131
132#### The following steps should be executed in order to add the Elastic Defend integration on a Linux System:
133- Go to the Kibana home page and click "Add integrations".
134- In the query bar, search for "Elastic Defend" and select the integration to see more details about it.
135- Click "Add Elastic Defend".
136- Configure the integration name and optionally add a description.
137- Select the type of environment you want to protect, either "Traditional Endpoints" or "Cloud Workloads".
138- Select a configuration preset. Each preset comes with different default settings for Elastic Agent, you can further customize these later by configuring the Elastic Defend integration policy. [Helper guide](https://www.elastic.co/guide/en/security/current/configure-endpoint-integration-policy.html).
139- We suggest selecting "Complete EDR (Endpoint Detection and Response)" as a configuration setting, that provides "All events; all preventions"
140- Enter a name for the agent policy in "New agent policy name". If other agent policies already exist, you can click the "Existing hosts" tab and select an existing policy instead.
141For more details on Elastic Agent configuration settings, refer to the [helper guide](https://www.elastic.co/guide/en/fleet/8.10/agent-policy.html).
142- Click "Save and Continue".
143- To complete the integration, select "Add Elastic Agent to your hosts" and continue to the next section to install the Elastic Agent on your hosts.
144For more details on Elastic Defend refer to the [helper guide](https://www.elastic.co/guide/en/security/current/install-endpoint.html).
145"""
146severity = "low"
147tags = [
148    "Domain: Endpoint",
149    "OS: Linux",
150    "Use Case: Threat Detection",
151    "Tactic: Persistence",
152    "Resources: Investigation Guide",
153    "Data Source: Elastic Defend",
154]
155timestamp_override = "event.ingested"
156type = "eql"
157query = '''
158file where host.os.type == "linux" and event.action in ("rename", "creation") and file.path : (
159  "/etc/systemd/system/*", "/etc/systemd/user/*", "/usr/local/lib/systemd/system/*",
160  "/lib/systemd/system/*", "/usr/lib/systemd/system/*", "/usr/lib/systemd/user/*",
161  "/home/*/.config/systemd/user/*", "/home/*/.local/share/systemd/user/*",
162  "/root/.config/systemd/user/*", "/root/.local/share/systemd/user/*"
163) and file.extension == "timer" and not (
164  process.executable in (
165    "/bin/dpkg", "/usr/bin/dpkg", "/bin/dockerd", "/usr/bin/dockerd", "/usr/sbin/dockerd", "/bin/microdnf",
166    "/usr/bin/microdnf", "/bin/rpm", "/usr/bin/rpm", "/bin/snapd", "/usr/bin/snapd", "/bin/yum", "/usr/bin/yum",
167    "/bin/dnf", "/usr/bin/dnf", "/bin/podman", "/usr/bin/podman", "/bin/dnf-automatic", "/usr/bin/dnf-automatic",
168    "/bin/pacman", "/usr/bin/pacman", "/usr/bin/dpkg-divert", "/bin/dpkg-divert", "/sbin/apk", "/usr/sbin/apk",
169    "/usr/local/sbin/apk", "/usr/bin/apt", "/usr/sbin/pacman", "/bin/podman", "/usr/bin/podman", "/usr/bin/puppet",
170    "/bin/puppet", "/opt/puppetlabs/puppet/bin/puppet", "/usr/bin/chef-client", "/bin/chef-client",
171    "/bin/autossl_check", "/usr/bin/autossl_check", "/proc/self/exe", "/dev/fd/*",  "/usr/bin/pamac-daemon",
172    "/bin/pamac-daemon", "/usr/lib/snapd/snapd", "/usr/local/bin/dockerd", "/usr/bin/crio", "/usr/sbin/crond",
173    "/opt/puppetlabs/puppet/bin/ruby", "/usr/libexec/platform-python", "/kaniko/kaniko-executor",
174    "/usr/local/bin/dockerd", "/usr/bin/podman", "/bin/install", "/proc/self/exe"
175  ) or
176  file.extension in ("swp", "swpx", "swx", "dpkg-remove") or
177  file.Ext.original.extension == "dpkg-new" or
178  process.executable : (
179    "/nix/store/*", "/var/lib/dpkg/*", "/tmp/vmis.*", "/snap/*", "/dev/fd/*", "/usr/lib/virtualbox/*"
180  ) or
181  process.executable == null or
182  (process.name == "sed" and file.name : "sed*") or
183  (process.name == "perl" and file.name : "e2scrub_all.tmp*") 
184)
185'''
186
187[[rule.threat]]
188framework = "MITRE ATT&CK"
189
190[[rule.threat.technique]]
191id = "T1053"
192name = "Scheduled Task/Job"
193reference = "https://attack.mitre.org/techniques/T1053/"
194
195[[rule.threat.technique.subtechnique]]
196id = "T1053.006"
197name = "Systemd Timers"
198reference = "https://attack.mitre.org/techniques/T1053/006/"
199
200[rule.threat.tactic]
201id = "TA0003"
202name = "Persistence"
203reference = "https://attack.mitre.org/tactics/TA0003/"

Triage and analysis

Investigating Systemd Timer Created

Systemd timers are used for scheduling and automating recurring tasks or services on Linux systems.

Attackers can leverage systemd timers to run scripts, commands, or malicious software at system boot or on a set time interval by creating a systemd timer and a corresponding systemd service file.

This rule monitors the creation of new systemd timer files, potentially indicating the creation of a persistence mechanism.

Note: This investigation guide uses the Osquery Markdown Plugin introduced in Elastic Stack version 8.5.0. Older Elastic Stack versions will display unrendered Markdown in this guide. This investigation guide uses placeholder fields to dynamically pass alert data into Osquery queries. Placeholder fields were introduced in Elastic Stack version 8.7.0. If you're using Elastic Stack version 8.6.0 or earlier, you'll need to manually adjust this investigation guide's queries to ensure they properly run.

Possible Investigation Steps

  • Investigate the timer file that was created or modified.
    • $osquery_0
  • Investigate the currently enabled systemd timers through the following command sudo systemctl list-timers.
  • Search for the systemd service file named similarly to the timer that was created.
  • Investigate whether any other files in any of the available systemd directories have been altered through OSQuery.
    • $osquery_1
    • $osquery_2
  • Investigate the script execution chain (parent process tree) for unknown processes. Examine their executable files for prevalence and whether they are located in expected locations.
    • $osquery_3
  • Investigate other alerts associated with the user/host during the past 48 hours.
  • Validate the activity is not related to planned patches, updates, network administrator activity, or legitimate software installations.
  • Investigate whether the altered scripts call other malicious scripts elsewhere on the file system.
    • If scripts or executables were dropped, retrieve the files and determine if they are malicious:
      • Use a private sandboxed malware analysis system to perform analysis.
        • Observe and collect information about the following activities:
          • Attempts to contact external domains and addresses.
            • Check if the domain is newly registered or unexpected.
            • Check the reputation of the domain or IP address.
          • File access, modification, and creation activities.
          • Cron jobs, services and other persistence mechanisms.
            • $osquery_4

False Positive Analysis

  • If this activity is related to new benign software installation activity, consider adding exceptions — preferably with a combination of user and command line conditions.
  • If this activity is related to a system administrator who uses systemd timers for administrative purposes, consider adding exceptions for this specific administrator user account.
  • Try to understand the context of the execution by thinking about the user, machine, or business purpose. A small number of endpoints, such as servers with unique software, might appear unusual but satisfy a specific business need.

Response and remediation

  • Initiate the incident response process based on the outcome of the triage.
  • Isolate the involved host to prevent further post-compromise behavior.
  • If the triage identified malware, search the environment for additional compromised hosts.
    • Implement temporary network rules, procedures, and segmentation to contain the malware.
    • Stop suspicious processes.
    • Immediately block the identified indicators of compromise (IoCs).
    • Inspect the affected systems for additional malware backdoors like reverse shells, reverse proxies, or droppers that attackers could use to reinfect the system.
  • Investigate credential exposure on systems compromised or used by the attacker to ensure all compromised accounts are identified. Reset passwords for these accounts and other potentially compromised credentials, such as email, business systems, and web services.
  • Delete the service/timer or restore its original configuration.
  • Run a full antimalware scan. This may reveal additional artifacts left in the system, persistence mechanisms, and malware components.
  • Determine the initial vector abused by the attacker and take action to prevent reinfection through the same vector.
  • Leverage the incident response data and logging to improve the mean time to detect (MTTD) and the mean time to respond (MTTR).

References

Related rules

to-top