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/10/17"
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/%' OR path LIKE
16'/lib/systemd/system/%' OR path LIKE '/usr/lib/systemd/system/%' OR path LIKE
17'/home/{{user.name}}/.config/systemd/user/%' OR path LIKE '/home/{{user.name}}/.local/share/systemd/user/%' OR path LIKE
18'/root/.config/systemd/user/%' OR path LIKE '/root/.local/share/systemd/user/%' OR path LIKE '/etc/systemd/user/%' OR
19path 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/%' OR path
31LIKE '/root/.config/systemd/user/%' OR path LIKE '/root/.local/share/systemd/user/%' OR path LIKE '/etc/systemd/user/%'
32OR 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 "https://www.elastic.co/security-labs/primer-on-persistence-mechanisms",
119]
120risk_score = 21
121rule_id = "7fb500fa-8e24-4bd1-9480-2a819352602c"
122setup = """## Setup
123
124This rule requires data coming in from Elastic Defend.
125
126### Elastic Defend Integration Setup
127Elastic 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.
128
129#### Prerequisite Requirements:
130- Fleet is required for Elastic Defend.
131- To configure Fleet Server refer to the [documentation](https://www.elastic.co/guide/en/fleet/current/fleet-server.html).
132
133#### The following steps should be executed in order to add the Elastic Defend integration on a Linux System:
134- Go to the Kibana home page and click "Add integrations".
135- In the query bar, search for "Elastic Defend" and select the integration to see more details about it.
136- Click "Add Elastic Defend".
137- Configure the integration name and optionally add a description.
138- Select the type of environment you want to protect, either "Traditional Endpoints" or "Cloud Workloads".
139- 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).
140- We suggest selecting "Complete EDR (Endpoint Detection and Response)" as a configuration setting, that provides "All events; all preventions"
141- 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.
142For more details on Elastic Agent configuration settings, refer to the [helper guide](https://www.elastic.co/guide/en/fleet/8.10/agent-policy.html).
143- Click "Save and Continue".
144- 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.
145For more details on Elastic Defend refer to the [helper guide](https://www.elastic.co/guide/en/security/current/install-endpoint.html).
146"""
147severity = "low"
148tags = [
149 "Domain: Endpoint",
150 "OS: Linux",
151 "Use Case: Threat Detection",
152 "Tactic: Persistence",
153 "Resources: Investigation Guide",
154 "Data Source: Elastic Defend",
155]
156timestamp_override = "event.ingested"
157type = "eql"
158query = '''
159file where host.os.type == "linux" and event.action in ("rename", "creation") and file.path : (
160 "/etc/systemd/system/*", "/etc/systemd/user/*", "/usr/local/lib/systemd/system/*",
161 "/lib/systemd/system/*", "/usr/lib/systemd/system/*", "/usr/lib/systemd/user/*",
162 "/home/*/.config/systemd/user/*", "/home/*/.local/share/systemd/user/*",
163 "/root/.config/systemd/user/*", "/root/.local/share/systemd/user/*"
164) and file.extension == "timer" and not (
165 process.executable in (
166 "/bin/dpkg", "/usr/bin/dpkg", "/bin/dockerd", "/usr/bin/dockerd", "/usr/sbin/dockerd", "/bin/microdnf",
167 "/usr/bin/microdnf", "/bin/rpm", "/usr/bin/rpm", "/bin/snapd", "/usr/bin/snapd", "/bin/yum", "/usr/bin/yum",
168 "/bin/dnf", "/usr/bin/dnf", "/bin/podman", "/usr/bin/podman", "/bin/dnf-automatic", "/usr/bin/dnf-automatic",
169 "/bin/pacman", "/usr/bin/pacman", "/usr/bin/dpkg-divert", "/bin/dpkg-divert", "/sbin/apk", "/usr/sbin/apk",
170 "/usr/local/sbin/apk", "/usr/bin/apt", "/usr/sbin/pacman", "/bin/podman", "/usr/bin/podman", "/usr/bin/puppet",
171 "/bin/puppet", "/opt/puppetlabs/puppet/bin/puppet", "/usr/bin/chef-client", "/bin/chef-client",
172 "/bin/autossl_check", "/usr/bin/autossl_check", "/proc/self/exe", "/dev/fd/*", "/usr/bin/pamac-daemon",
173 "/bin/pamac-daemon", "/usr/lib/snapd/snapd", "/usr/local/bin/dockerd", "/usr/bin/crio", "/usr/sbin/crond",
174 "/opt/puppetlabs/puppet/bin/ruby", "/usr/libexec/platform-python", "/kaniko/kaniko-executor",
175 "/usr/local/bin/dockerd", "/usr/bin/podman", "/bin/install", "/proc/self/exe"
176 ) or
177 process.name like (
178 "python*", "crio", "apt-get", "install", "snapd", "cloudflared", "sshd", "convert-usrmerge", "docker-init",
179 "google_metadata_script_runner"
180 ) or
181 file.extension in ("swp", "swpx", "swx", "dpkg-remove") or
182 file.Ext.original.extension == "dpkg-new" or
183 process.executable : (
184 "/nix/store/*", "/var/lib/dpkg/*", "/tmp/vmis.*", "/snap/*", "/dev/fd/*", "/usr/lib/virtualbox/*"
185 ) or
186 process.executable == null or
187 (process.name == "sed" and file.name : "sed*") or
188 (process.name == "perl" and file.name : "e2scrub_all.tmp*")
189)
190'''
191
192[[rule.threat]]
193framework = "MITRE ATT&CK"
194
195[[rule.threat.technique]]
196id = "T1053"
197name = "Scheduled Task/Job"
198reference = "https://attack.mitre.org/techniques/T1053/"
199
200[[rule.threat.technique.subtechnique]]
201id = "T1053.006"
202name = "Systemd Timers"
203reference = "https://attack.mitre.org/techniques/T1053/006/"
204
205[rule.threat.tactic]
206id = "TA0003"
207name = "Persistence"
208reference = "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
- Attempts to contact external domains and addresses.
- Observe and collect information about the following activities:
- Use a private sandboxed malware analysis system to perform analysis.
- If scripts or executables were dropped, retrieve the files and determine if they are malicious:
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
- Message-of-the-Day (MOTD) File Creation
- Potential Linux Backdoor User Account Creation
- rc.local/rc.common File Creation
- Process Spawned from Message-of-the-Day (MOTD)
- Potential Remote Code Execution via Web Server