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/12/13"
  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"
137setup = """## Setup
138
139This rule requires data coming in from Elastic Defend.
140
141### Elastic Defend Integration Setup
142Elastic 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.
143
144#### Prerequisite Requirements:
145- Fleet is required for Elastic Defend.
146- To configure Fleet Server refer to the [documentation](https://www.elastic.co/guide/en/fleet/current/fleet-server.html).
147
148#### The following steps should be executed in order to add the Elastic Defend integration on a Linux System:
149- Go to the Kibana home page and click "Add integrations".
150- In the query bar, search for "Elastic Defend" and select the integration to see more details about it.
151- Click "Add Elastic Defend".
152- Configure the integration name and optionally add a description.
153- Select the type of environment you want to protect, either "Traditional Endpoints" or "Cloud Workloads".
154- 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).
155- We suggest selecting "Complete EDR (Endpoint Detection and Response)" as a configuration setting, that provides "All events; all preventions"
156- 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.
157For more details on Elastic Agent configuration settings, refer to the [helper guide](https://www.elastic.co/guide/en/fleet/8.10/agent-policy.html).
158- Click "Save and Continue".
159- 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.
160For more details on Elastic Defend refer to the [helper guide](https://www.elastic.co/guide/en/security/current/install-endpoint.html).
161"""
162severity = "low"
163tags = [
164        "Domain: Endpoint",
165        "OS: Linux",
166        "Use Case: Threat Detection",
167        "Tactic: Persistence",
168        "Data Source: Elastic Endgame",
169        "Resources: Investigation Guide",
170        "Data Source: Elastic Defend"
171        ]
172timestamp_override = "event.ingested"
173type = "new_terms"
174query = '''
175host.os.type : "linux" and event.action : ("creation" or "file_create_event") and file.extension : "timer" and
176file.path : (/etc/systemd/system/* or /usr/local/lib/systemd/system/* or /lib/systemd/system/* or 
177/usr/lib/systemd/system/* or /home/*/.config/systemd/user/*) and not (
178  (process.name : (
179    "docker" or "dockerd" or "dnf" or "yum" or "rpm" or "dpkg" or "executor" or "cloudflared" or "pacman" or "podman" or 
180    "pamac-daemon"
181  ))
182  or (file.name:apt-*.timer)
183)
184'''
185
186[[rule.threat]]
187framework = "MITRE ATT&CK"
188
189[[rule.threat.technique]]
190id = "T1053"
191name = "Scheduled Task/Job"
192reference = "https://attack.mitre.org/techniques/T1053/"
193
194[[rule.threat.technique.subtechnique]]
195id = "T1053.006"
196name = "Systemd Timers"
197reference = "https://attack.mitre.org/techniques/T1053/006/"
198
199[rule.threat.tactic]
200id = "TA0003"
201name = "Persistence"
202reference = "https://attack.mitre.org/tactics/TA0003/"
203
204[rule.new_terms]
205field = "new_terms_fields"
206value = ["host.id", "file.path", "process.executable"]
207
208[[rule.new_terms.history_window_start]]
209field = "history_window_start"
210value = "now-10d"

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