New 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"
  5min_stack_comments = "Multiple field support in the New Terms rule type was added in Elastic 8.6"
  6min_stack_version = "8.6.0"
  7updated_date = "2023/08/21"
  8
  9[transform]
 10[[transform.osquery]]
 11label = "Osquery - Retrieve File Information"
 12query = "SELECT * FROM file WHERE path = {{file.path}}"
 13
 14[[transform.osquery]]
 15label = "Osquery - Retrieve File Listing Information"
 16query = """
 17SELECT * FROM file WHERE (
 18path LIKE '/etc/systemd/system/%' OR 
 19path LIKE '/usr/local/lib/systemd/system/%' OR 
 20path LIKE '/lib/systemd/system/%' OR
 21path LIKE '/usr/lib/systemd/system/%' OR
 22path LIKE '/home/user/.config/systemd/user/%'
 23)
 24"""
 25
 26[[transform.osquery]]
 27label = "Osquery - Retrieve Additional File Listing Information"
 28query = """
 29SELECT
 30  f.path,
 31  u.username AS file_owner,
 32  g.groupname AS group_owner,
 33  datetime(f.atime, 'unixepoch') AS file_last_access_time,
 34  datetime(f.mtime, 'unixepoch') AS file_last_modified_time,
 35  datetime(f.ctime, 'unixepoch') AS file_last_status_change_time,
 36  datetime(f.btime, 'unixepoch') AS file_created_time,
 37  f.size AS size_bytes
 38FROM
 39  file f
 40  LEFT JOIN users u ON f.uid = u.uid
 41  LEFT JOIN groups g ON f.gid = g.gid
 42WHERE (
 43path LIKE '/etc/systemd/system/%' OR 
 44path LIKE '/usr/local/lib/systemd/system/%' OR 
 45path LIKE '/lib/systemd/system/%' OR
 46path LIKE '/usr/lib/systemd/system/%' OR
 47path LIKE '/home/{{user.name}}/.config/systemd/user/%'
 48)
 49"""
 50
 51[[transform.osquery]]
 52label = "Osquery - Retrieve Running Processes by User"
 53query = "SELECT pid, username, name FROM processes p JOIN users u ON u.uid = p.uid ORDER BY username"
 54
 55[[transform.osquery]]
 56label = "Osquery - Retrieve Crontab Information"
 57query = "SELECT * FROM crontab"
 58
 59[rule]
 60author = ["Elastic"]
 61description = """
 62Detects the creation of a systemd timer within any of the default systemd timer directories. Systemd timers can be used 
 63by an attacker to gain persistence, by scheduling the execution of a command or script. Similarly to cron/at, systemd 
 64timers can be set up to execute on boot time, or on a specific point in time, which allows attackers to regain access in 
 65case the connection to the infected asset was lost. 
 66"""
 67from = "now-9m"
 68index = ["logs-endpoint.events.*", "endgame-*"]
 69language = "kuery"
 70license = "Elastic License v2"
 71name = "New Systemd Timer Created"
 72note = """## Triage and analysis
 73
 74### Investigating New Systemd Timer Created
 75
 76Systemd timers are used for scheduling and automating recurring tasks or services on Linux systems. 
 77
 78Attackers 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. 
 79
 80This rule monitors the creation of new systemd timer files, potentially indicating the creation of a persistence mechanism.
 81
 82> **Note**:
 83> 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.
 84> 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.
 85
 86#### Possible Investigation Steps
 87
 88- Investigate the timer file that was created or modified.
 89  - $osquery_0
 90- Investigate the currently enabled systemd timers through the following command `sudo systemctl list-timers`.
 91- Search for the systemd service file named similarly to the timer that was created.
 92- Investigate whether any other files in any of the available systemd directories have been altered through OSQuery.
 93  - $osquery_1
 94  - $osquery_2
 95- 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.
 96  - $osquery_3
 97- Investigate other alerts associated with the user/host during the past 48 hours.
 98- Validate the activity is not related to planned patches, updates, network administrator activity, or legitimate software installations.
 99- Investigate whether the altered scripts call other malicious scripts elsewhere on the file system. 
100  - If scripts or executables were dropped, retrieve the files and determine if they are malicious:
101    - Use a private sandboxed malware analysis system to perform analysis.
102      - Observe and collect information about the following activities:
103        - Attempts to contact external domains and addresses.
104          - Check if the domain is newly registered or unexpected.
105          - Check the reputation of the domain or IP address.
106        - File access, modification, and creation activities.
107        - Cron jobs, services and other persistence mechanisms.
108            - $osquery_4
109
110### False Positive Analysis
111
112- If this activity is related to new benign software installation activity, consider adding exceptions — preferably with a combination of user and command line conditions.
113- 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. 
114- 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.
115
116### Response and remediation
117
118- Initiate the incident response process based on the outcome of the triage.
119- Isolate the involved host to prevent further post-compromise behavior.
120- If the triage identified malware, search the environment for additional compromised hosts.
121  - Implement temporary network rules, procedures, and segmentation to contain the malware.
122  - Stop suspicious processes.
123  - Immediately block the identified indicators of compromise (IoCs).
124  - Inspect the affected systems for additional malware backdoors like reverse shells, reverse proxies, or droppers that attackers could use to reinfect the system.
125- 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.
126- Delete the service/timer or restore its original configuration.
127- Run a full antimalware scan. This may reveal additional artifacts left in the system, persistence mechanisms, and malware components.
128- Determine the initial vector abused by the attacker and take action to prevent reinfection through the same vector.
129- Leverage the incident response data and logging to improve the mean time to detect (MTTD) and the mean time to respond (MTTR).
130"""
131references = [
132    "https://opensource.com/article/20/7/systemd-timers",
133    "https://pberba.github.io/security/2022/01/30/linux-threat-hunting-for-persistence-systemd-timers-cron/"
134]
135risk_score = 21
136rule_id = "7fb500fa-8e24-4bd1-9480-2a819352602c"
137severity = "low"
138tags = ["Domain: Endpoint", "OS: Linux", "Use Case: Threat Detection", "Tactic: Persistence", "Data Source: Elastic Endgame", "Resources: Investigation Guide", "Data Source: Elastic Defend"]
139timestamp_override = "event.ingested"
140type = "new_terms"
141query = '''
142host.os.type : "linux" and event.action : ("creation" or "file_create_event") and file.extension : "timer" and
143file.path : (/etc/systemd/system/* or /usr/local/lib/systemd/system/* or /lib/systemd/system/* or 
144/usr/lib/systemd/system/* or /home/*/.config/systemd/user/*) and not process.name : ("docker" or "dockerd" or "dnf" or "yum" or "rpm" or "dpkg" or "executor")
145'''
146
147[[rule.threat]]
148framework = "MITRE ATT&CK"
149
150[[rule.threat.technique]]
151id = "T1053"
152name = "Scheduled Task/Job"
153reference = "https://attack.mitre.org/techniques/T1053/"
154
155[[rule.threat.technique.subtechnique]]
156id = "T1053.006"
157name = "Systemd Timers"
158reference = "https://attack.mitre.org/techniques/T1053/006/"
159
160[rule.threat.tactic]
161id = "TA0003"
162name = "Persistence"
163reference = "https://attack.mitre.org/tactics/TA0003/"
164
165[rule.new_terms]
166field = "new_terms_fields"
167value = ["file.path", "process.name"]
168
169[[rule.new_terms.history_window_start]]
170field = "history_window_start"
171value = "now-7d"

Triage and analysis

Investigating New 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