Systemd Service Created
This rule detects the creation or renaming of a new Systemd file in all of the common Systemd service locations for both root and regular users. Systemd service files are configuration files in Linux systems used to define and manage system services. Malicious actors can leverage systemd service files to achieve persistence by creating or modifying services to execute malicious commands or payloads during system startup or at a predefined interval by adding a systemd timer. This allows them to maintain unauthorized access, execute additional malicious activities, or evade detection.
Elastic rule (View on GitHub)
1[metadata]
2creation_date = "2023/06/09"
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[[transform.osquery]]
44label = "Osquery - Retrieve Listening Ports"
45query = "SELECT pid, address, port, socket, protocol, path FROM listening_ports"
46
47[[transform.osquery]]
48label = "Osquery - Retrieve Open Sockets"
49query = "SELECT pid, family, remote_address, remote_port, socket, state FROM process_open_sockets"
50
51[[transform.osquery]]
52label = "Osquery - Retrieve Information for a Specific User"
53query = "SELECT * FROM users WHERE username = {{user.name}}"
54
55[[transform.osquery]]
56label = "Osquery - Investigate the Account Authentication Status"
57query = "SELECT * FROM logged_in_users WHERE user = {{user.name}}"
58
59
60[rule]
61author = ["Elastic"]
62description = """
63This rule detects the creation or renaming of a new Systemd file in all of the common Systemd service locations for both
64root and regular users. Systemd service files are configuration files in Linux systems used to define and manage system
65services. Malicious actors can leverage systemd service files to achieve persistence by creating or modifying services
66to execute malicious commands or payloads during system startup or at a predefined interval by adding a systemd timer.
67This allows them to maintain unauthorized access, execute additional malicious activities, or evade detection.
68"""
69from = "now-9m"
70index = ["logs-endpoint.events.file*"]
71language = "eql"
72license = "Elastic License v2"
73name = "Systemd Service Created"
74note = """## Triage and analysis
75
76### Investigating Systemd Service Created
77
78Systemd service files are configuration files in Linux systems used to define and manage system services.
79
80Malicious actors can leverage systemd service files to achieve persistence by creating or modifying service files to execute malicious commands or payloads during system startup. This allows them to maintain unauthorized access, execute additional malicious activities, or evade detection.
81
82This rule monitors the creation of new systemd service files, potentially indicating the creation of a persistence mechanism.
83
84> **Note**:
85> 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.
86> 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.
87
88#### Possible Investigation Steps
89
90- Investigate the systemd service file that was created or modified.
91 - $osquery_0
92- Investigate the currently enabled systemd services through the following command `sudo systemctl list-unit-files`.
93- Investigate whether any other files in any of the available systemd directories have been altered through OSQuery.
94 - $osquery_1
95 - $osquery_2
96- 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.
97 - $osquery_3
98- Investigate other alerts associated with the user/host during the past 48 hours.
99- Validate the activity is not related to planned patches, updates, network administrator activity, or legitimate software installations.
100- Investigate whether the altered scripts call other malicious scripts elsewhere on the file system.
101 - If scripts or executables were dropped, retrieve the files and determine if they are malicious:
102 - Use a private sandboxed malware analysis system to perform analysis.
103 - Observe and collect information about the following activities:
104 - Attempts to contact external domains and addresses.
105 - Check if the domain is newly registered or unexpected.
106 - Check the reputation of the domain or IP address.
107 - File access, modification, and creation activities.
108 - Cron jobs, services and other persistence mechanisms.
109 - $osquery_4
110- Investigate abnormal behaviors by the subject process/user such as network connections, file modifications, and any other spawned child processes.
111 - Investigate listening ports and open sockets to look for potential command and control traffic or data exfiltration.
112 - $osquery_5
113 - $osquery_6
114 - Identify the user account that performed the action, analyze it, and check whether it should perform this kind of action.
115 - $osquery_7
116- Investigate whether the user is currently logged in and active.
117 - $osquery_8
118
119### False Positive Analysis
120
121- If this activity is related to new benign software installation activity, consider adding exceptions — preferably with a combination of user and command line conditions.
122- If this activity is related to a system administrator who uses systemd services for administrative purposes, consider adding exceptions for this specific administrator user account.
123- 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.
124
125### Related Rules
126
127- Potential Persistence Through Run Control Detected - 0f4d35e4-925e-4959-ab24-911be207ee6f
128- Potential Persistence Through init.d Detected - 474fd20e-14cc-49c5-8160-d9ab4ba16c8b
129- Systemd Timer Created - 7fb500fa-8e24-4bd1-9480-2a819352602c
130
131### Response and remediation
132
133- Initiate the incident response process based on the outcome of the triage.
134- Isolate the involved host to prevent further post-compromise behavior.
135- If the triage identified malware, search the environment for additional compromised hosts.
136 - Implement temporary network rules, procedures, and segmentation to contain the malware.
137 - Stop suspicious processes.
138 - Immediately block the identified indicators of compromise (IoCs).
139 - Inspect the affected systems for additional malware backdoors like reverse shells, reverse proxies, or droppers that attackers could use to reinfect the system.
140- 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.
141- Delete the service/timer or restore its original configuration.
142- Run a full antimalware scan. This may reveal additional artifacts left in the system, persistence mechanisms, and malware components.
143- Determine the initial vector abused by the attacker and take action to prevent reinfection through the same vector.
144- Leverage the incident response data and logging to improve the mean time to detect (MTTD) and the mean time to respond (MTTR).
145"""
146references = [
147 "https://pberba.github.io/security/2022/01/30/linux-threat-hunting-for-persistence-systemd-timers-cron/",
148 "https://www.elastic.co/security-labs/primer-on-persistence-mechanisms",
149]
150risk_score = 47
151rule_id = "17b0a495-4d9f-414c-8ad0-92f018b8e001"
152setup = """## Setup
153
154This rule requires data coming in from Elastic Defend.
155
156### Elastic Defend Integration Setup
157Elastic 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.
158
159#### Prerequisite Requirements:
160- Fleet is required for Elastic Defend.
161- To configure Fleet Server refer to the [documentation](https://www.elastic.co/guide/en/fleet/current/fleet-server.html).
162
163#### The following steps should be executed in order to add the Elastic Defend integration on a Linux System:
164- Go to the Kibana home page and click "Add integrations".
165- In the query bar, search for "Elastic Defend" and select the integration to see more details about it.
166- Click "Add Elastic Defend".
167- Configure the integration name and optionally add a description.
168- Select the type of environment you want to protect, either "Traditional Endpoints" or "Cloud Workloads".
169- 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).
170- We suggest selecting "Complete EDR (Endpoint Detection and Response)" as a configuration setting, that provides "All events; all preventions"
171- 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.
172For more details on Elastic Agent configuration settings, refer to the [helper guide](https://www.elastic.co/guide/en/fleet/8.10/agent-policy.html).
173- Click "Save and Continue".
174- 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.
175For more details on Elastic Defend refer to the [helper guide](https://www.elastic.co/guide/en/security/current/install-endpoint.html).
176"""
177severity = "medium"
178tags = [
179 "Domain: Endpoint",
180 "OS: Linux",
181 "Use Case: Threat Detection",
182 "Tactic: Persistence",
183 "Tactic: Privilege Escalation",
184 "Data Source: Elastic Defend",
185]
186timestamp_override = "event.ingested"
187type = "eql"
188query = '''
189file where host.os.type == "linux" and event.action in ("rename", "creation") and file.path : (
190 "/etc/systemd/system/*", "/etc/systemd/user/*", "/usr/local/lib/systemd/system/*",
191 "/lib/systemd/system/*", "/usr/lib/systemd/system/*", "/usr/lib/systemd/user/*",
192 "/home/*/.config/systemd/user/*", "/home/*/.local/share/systemd/user/*",
193 "/root/.config/systemd/user/*", "/root/.local/share/systemd/user/*"
194) and file.extension == "service" and not (
195 process.executable in (
196 "/bin/dpkg", "/usr/bin/dpkg", "/bin/dockerd", "/usr/bin/dockerd", "/usr/sbin/dockerd", "/bin/microdnf",
197 "/usr/bin/microdnf", "/bin/rpm", "/usr/bin/rpm", "/bin/snapd", "/usr/bin/snapd", "/bin/yum", "/usr/bin/yum",
198 "/bin/dnf", "/usr/bin/dnf", "/bin/podman", "/usr/bin/podman", "/bin/dnf-automatic", "/usr/bin/dnf-automatic",
199 "/bin/pacman", "/usr/bin/pacman", "/usr/bin/dpkg-divert", "/bin/dpkg-divert", "/sbin/apk", "/usr/sbin/apk",
200 "/usr/local/sbin/apk", "/usr/bin/apt", "/usr/sbin/pacman", "/bin/podman", "/usr/bin/podman", "/usr/bin/puppet",
201 "/bin/puppet", "/opt/puppetlabs/puppet/bin/puppet", "/usr/bin/chef-client", "/bin/chef-client",
202 "/bin/autossl_check", "/usr/bin/autossl_check", "/proc/self/exe", "/dev/fd/*", "/usr/bin/pamac-daemon",
203 "/bin/pamac-daemon", "/usr/lib/snapd/snapd", "/usr/local/bin/dockerd", "/usr/bin/crio", "/usr/sbin/crond",
204 "/opt/puppetlabs/puppet/bin/ruby", "/usr/libexec/platform-python", "/kaniko/kaniko-executor",
205 "/usr/local/bin/dockerd", "/usr/bin/podman", "/bin/install", "/proc/self/exe", "/usr/lib/systemd/systemd",
206 "/usr/sbin/sshd", "/usr/bin/gitlab-runner", "/opt/gitlab/embedded/bin/ruby", "/usr/sbin/gdm", "/usr/bin/install",
207 "/usr/local/manageengine/uems_agent/bin/dcregister"
208 ) or
209 file.extension in ("swp", "swpx", "swx", "dpkg-remove") or
210 file.Ext.original.extension == "dpkg-new" or
211 process.executable : (
212 "/nix/store/*", "/var/lib/dpkg/*", "/tmp/vmis.*", "/snap/*", "/dev/fd/*", "/usr/lib/virtualbox/*"
213 ) or
214 process.executable == null or
215 process.name like (
216 "ssm-agent-worker", "python*", "platform-python*", "dnf_install", "cloudflared", "lxc-pve-prestart-hook",
217 "convert-usrmerge", "elastic-agent", "google_metadata_script_runner", "update-alternatives", "gitlab-runner",
218 "install", "crio", "apt-get", "package-cleanup", "dcservice", "dcregister", "jumpcloud-agent", "executor"
219 ) or
220 (process.name == "sed" and file.name : "sed*") or
221 (process.name == "perl" and file.name : "e2scrub_all.tmp*")
222)
223'''
224
225[[rule.threat]]
226framework = "MITRE ATT&CK"
227
228[[rule.threat.technique]]
229id = "T1543"
230name = "Create or Modify System Process"
231reference = "https://attack.mitre.org/techniques/T1543/"
232
233[[rule.threat.technique.subtechnique]]
234id = "T1543.002"
235name = "Systemd Service"
236reference = "https://attack.mitre.org/techniques/T1543/002/"
237
238[rule.threat.tactic]
239id = "TA0003"
240name = "Persistence"
241reference = "https://attack.mitre.org/tactics/TA0003/"
242
243[[rule.threat]]
244framework = "MITRE ATT&CK"
245
246[[rule.threat.technique]]
247id = "T1543"
248name = "Create or Modify System Process"
249reference = "https://attack.mitre.org/techniques/T1543/"
250
251[[rule.threat.technique.subtechnique]]
252id = "T1543.002"
253name = "Systemd Service"
254reference = "https://attack.mitre.org/techniques/T1543/002/"
255
256[rule.threat.tactic]
257id = "TA0004"
258name = "Privilege Escalation"
259reference = "https://attack.mitre.org/tactics/TA0004/"
Triage and analysis
Investigating Systemd Service Created
Systemd service files are configuration files in Linux systems used to define and manage system services.
Malicious actors can leverage systemd service files to achieve persistence by creating or modifying service files to execute malicious commands or payloads during system startup. This allows them to maintain unauthorized access, execute additional malicious activities, or evade detection.
This rule monitors the creation of new systemd service 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 systemd service file that was created or modified.
- $osquery_0
- Investigate the currently enabled systemd services through the following command
sudo systemctl list-unit-files
. - 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:
- Investigate abnormal behaviors by the subject process/user such as network connections, file modifications, and any other spawned child processes.
- Investigate listening ports and open sockets to look for potential command and control traffic or data exfiltration.
- $osquery_5
- $osquery_6
- Identify the user account that performed the action, analyze it, and check whether it should perform this kind of action.
- $osquery_7
- Investigate listening ports and open sockets to look for potential command and control traffic or data exfiltration.
- Investigate whether the user is currently logged in and active.
- $osquery_8
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 services 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.
Related Rules
- Potential Persistence Through Run Control Detected - 0f4d35e4-925e-4959-ab24-911be207ee6f
- Potential Persistence Through init.d Detected - 474fd20e-14cc-49c5-8160-d9ab4ba16c8b
- Systemd Timer Created - 7fb500fa-8e24-4bd1-9480-2a819352602c
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
- Shadow File Modification
- Systemd Generator Created
- Kernel Load or Unload via Kexec Detected
- At Job Created or Modified
- Potential Sudo Hijacking