rc.local/rc.common File Creation
This rule monitors the creation/alteration of the rc.local/rc.common file. The /etc/rc.local file is used to start custom applications, services, scripts or commands during start-up. The rc.local file has mostly been replaced by Systemd. However, through the "systemd-rc-local-generator", rc.local files can be converted to services that run at boot. Adversaries may alter rc.local/rc.common to execute malicious code at start-up, and gain persistence onto the system.
Elastic rule (View on GitHub)
1[metadata]
2creation_date = "2023/02/28"
3integration = ["endpoint"]
4maturity = "production"
5updated_date = "2024/09/23"
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 Running Processes by User"
14query = "SELECT pid, username, name FROM processes p JOIN users u ON u.uid = p.uid ORDER BY username"
15
16[[transform.osquery]]
17label = "Osquery - Retrieve rc-local.service File Information"
18query = """
19SELECT * FROM file WHERE (path = '/run/systemd/generator/multi-user.target.wants/rc-local.service' OR path =
20'/run/systemd/generator/multi-user.target.wants/rc-local.service')
21"""
22
23[[transform.osquery]]
24label = "Osquery - Retrieve Crontab Information"
25query = "SELECT * FROM crontab"
26
27
28[rule]
29author = ["Elastic"]
30description = """
31This rule monitors the creation/alteration of the rc.local/rc.common file. The /etc/rc.local file is used to start
32custom applications, services, scripts or commands during start-up. The rc.local file has mostly been replaced by
33Systemd. However, through the "systemd-rc-local-generator", rc.local files can be converted to services that run at
34boot. Adversaries may alter rc.local/rc.common to execute malicious code at start-up, and gain persistence onto the
35system.
36"""
37from = "now-9m"
38index = ["logs-endpoint.events.*"]
39language = "eql"
40license = "Elastic License v2"
41name = "rc.local/rc.common File Creation"
42note = """## Triage and analysis
43
44### Investigating rc.local/rc.common File Creation
45
46The `rc.local` file executes custom commands or scripts during system startup on Linux systems. `rc.local` has been deprecated in favor of the use of `systemd services`, and more recent Unix distributions no longer leverage this method of on-boot script execution.
47
48There might still be users that use `rc.local` in a benign matter, so investigation to see whether the file is malicious is vital.
49
50Detection alerts from this rule indicate the creation of a new `/etc/rc.local` file.
51
52> **Note**:
53> 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.
54> 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.
55
56#### Possible Investigation Steps
57
58- Identify the user account that performed the action and whether it should perform this kind of action.
59- Investigate the file that was created or modified.
60 - $osquery_0
61- 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.
62 - $osquery_1
63- Investigate whether the `/lib/systemd/system/rc-local.service` and `/run/systemd/generator/multi-user.target.wants/rc-local.service` files were created through the `systemd-rc-local-generator` located at `/usr/lib/systemd/system-generators/systemd-rc-local-generator`.
64 - $osquery_2
65 - In case the file is not present here, `sudo systemctl status rc-local` can be executed to find the location of the rc-local unit file.
66 - If `rc-local.service` is found, manual investigation is required to check for the rc script execution. Systemd will generate syslogs in case of the execution of the rc-local service. `sudo cat /var/log/syslog | grep "rc-local.service|/etc/rc.local Compatibility"` can be executed to check for the execution of the service.
67 - If logs are found, it's likely that the contents of the `rc.local` file have been executed. Analyze the logs. In case several syslog log files are available, use a wildcard to search through all of the available logs.
68- Investigate other alerts associated with the user/host during the past 48 hours.
69- Validate whether this activity is related to planned patches, updates, network administrator activity, or legitimate software installations.
70- Investigate whether the altered scripts call other malicious scripts elsewhere on the file system.
71 - If scripts or executables were dropped, retrieve the files and determine if they are malicious:
72 - Use a private sandboxed malware analysis system to perform analysis.
73 - Observe and collect information about the following activities:
74 - Attempts to contact external domains and addresses.
75 - Check if the domain is newly registered or unexpected.
76 - Check the reputation of the domain or IP address.
77 - File access, modification, and creation activities.
78 - Cron jobs, services and other persistence mechanisms.
79 - $osquery_3
80
81### False Positive Analysis
82
83- If this activity is related to new benign software installation activity, consider adding exceptions — preferably with a combination of user and command line conditions.
84- If this activity is related to a system administrator who uses `rc.local` for administrative purposes, consider adding exceptions for this specific administrator user account.
85- 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.
86
87### Response and remediation
88- Initiate the incident response process based on the outcome of the triage.
89- Isolate the involved host to prevent further post-compromise behavior.
90- If the triage identified malware, search the environment for additional compromised hosts.
91 - Implement temporary network rules, procedures, and segmentation to contain the malware.
92 - Stop suspicious processes.
93 - Immediately block the identified indicators of compromise (IoCs).
94 - Inspect the affected systems for additional malware backdoors like reverse shells, reverse proxies, or droppers that attackers could use to reinfect the system.
95- 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.
96- Delete the `service/rc.local` files or restore their original configuration.
97- Run a full antimalware scan. This may reveal additional artifacts left in the system, persistence mechanisms, and malware components.
98- Determine the initial vector abused by the attacker and take action to prevent reinfection through the same vector.
99- Leverage the incident response data and logging to improve the mean time to detect (MTTD) and the mean time to respond (MTTR).
100"""
101references = [
102 "https://www.intezer.com/blog/malware-analysis/hiddenwasp-malware-targeting-linux-systems/",
103 "https://pberba.github.io/security/2022/02/06/linux-threat-hunting-for-persistence-initialization-scripts-and-shell-configuration/#8-boot-or-logon-initialization-scripts-rc-scripts",
104 "https://www.cyberciti.biz/faq/how-to-enable-rc-local-shell-script-on-systemd-while-booting-linux-system/",
105 "https://www.elastic.co/security-labs/sequel-on-persistence-mechanisms",
106]
107risk_score = 47
108rule_id = "0f4d35e4-925e-4959-ab24-911be207ee6f"
109setup = """## Setup
110
111This rule requires data coming in from Elastic Defend.
112
113### Elastic Defend Integration Setup
114Elastic 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.
115
116#### Prerequisite Requirements:
117- Fleet is required for Elastic Defend.
118- To configure Fleet Server refer to the [documentation](https://www.elastic.co/guide/en/fleet/current/fleet-server.html).
119
120#### The following steps should be executed in order to add the Elastic Defend integration on a Linux System:
121- Go to the Kibana home page and click "Add integrations".
122- In the query bar, search for "Elastic Defend" and select the integration to see more details about it.
123- Click "Add Elastic Defend".
124- Configure the integration name and optionally add a description.
125- Select the type of environment you want to protect, either "Traditional Endpoints" or "Cloud Workloads".
126- 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).
127- We suggest selecting "Complete EDR (Endpoint Detection and Response)" as a configuration setting, that provides "All events; all preventions"
128- 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.
129For more details on Elastic Agent configuration settings, refer to the [helper guide](https://www.elastic.co/guide/en/fleet/8.10/agent-policy.html).
130- Click "Save and Continue".
131- 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.
132For more details on Elastic Defend refer to the [helper guide](https://www.elastic.co/guide/en/security/current/install-endpoint.html).
133"""
134severity = "medium"
135tags = [
136 "Domain: Endpoint",
137 "OS: Linux",
138 "Use Case: Threat Detection",
139 "Tactic: Persistence",
140 "Resources: Investigation Guide",
141 "Data Source: Elastic Defend",
142]
143timestamp_override = "event.ingested"
144type = "eql"
145
146query = '''
147file where host.os.type == "linux" and event.action in ("rename", "creation") and
148file.path in ("/etc/rc.local", "/etc/rc.common") and not (
149 process.executable in (
150 "/bin/dpkg", "/usr/bin/dpkg", "/bin/dockerd", "/usr/bin/dockerd", "/usr/sbin/dockerd", "/bin/microdnf",
151 "/usr/bin/microdnf", "/bin/rpm", "/usr/bin/rpm", "/bin/snapd", "/usr/bin/snapd", "/bin/yum", "/usr/bin/yum",
152 "/bin/dnf", "/usr/bin/dnf", "/bin/podman", "/usr/bin/podman", "/bin/dnf-automatic", "/usr/bin/dnf-automatic",
153 "/bin/pacman", "/usr/bin/pacman", "/usr/bin/dpkg-divert", "/bin/dpkg-divert", "/sbin/apk", "/usr/sbin/apk",
154 "/usr/local/sbin/apk", "/usr/bin/apt", "/usr/sbin/pacman", "/bin/podman", "/usr/bin/podman", "/usr/bin/puppet",
155 "/bin/puppet", "/opt/puppetlabs/puppet/bin/puppet", "/usr/bin/chef-client", "/bin/chef-client",
156 "/bin/autossl_check", "/usr/bin/autossl_check", "/proc/self/exe", "/dev/fd/*", "/usr/bin/pamac-daemon",
157 "/bin/pamac-daemon", "/usr/lib/snapd/snapd", "/usr/local/bin/dockerd", "/usr/libexec/platform-python"
158 ) or
159 file.extension in ("swp", "swpx", "swx", "dpkg-remove") or
160 file.Ext.original.extension == "dpkg-new" or
161 process.executable : (
162 "/nix/store/*", "/var/lib/dpkg/*", "/snap/*", "/dev/fd/*", "/usr/lib/virtualbox/*"
163 ) or
164 process.executable == null or
165 (process.name == "sed" and file.name : "sed*") or
166 (process.name == "perl" and file.name : "e2scrub_all.tmp*")
167)
168'''
169
170
171[[rule.threat]]
172framework = "MITRE ATT&CK"
173[[rule.threat.technique]]
174id = "T1037"
175name = "Boot or Logon Initialization Scripts"
176reference = "https://attack.mitre.org/techniques/T1037/"
177[[rule.threat.technique.subtechnique]]
178id = "T1037.004"
179name = "RC Scripts"
180reference = "https://attack.mitre.org/techniques/T1037/004/"
181
182
183
184[rule.threat.tactic]
185id = "TA0003"
186name = "Persistence"
187reference = "https://attack.mitre.org/tactics/TA0003/"
Triage and analysis
Investigating rc.local/rc.common File Creation
The rc.local
file executes custom commands or scripts during system startup on Linux systems. rc.local
has been deprecated in favor of the use of systemd services
, and more recent Unix distributions no longer leverage this method of on-boot script execution.
There might still be users that use rc.local
in a benign matter, so investigation to see whether the file is malicious is vital.
Detection alerts from this rule indicate the creation of a new /etc/rc.local
file.
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
- Identify the user account that performed the action and whether it should perform this kind of action.
- Investigate the file that was created or modified.
- $osquery_0
- 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_1
- Investigate whether the
/lib/systemd/system/rc-local.service
and/run/systemd/generator/multi-user.target.wants/rc-local.service
files were created through thesystemd-rc-local-generator
located at/usr/lib/systemd/system-generators/systemd-rc-local-generator
.- $osquery_2
- In case the file is not present here,
sudo systemctl status rc-local
can be executed to find the location of the rc-local unit file. - If
rc-local.service
is found, manual investigation is required to check for the rc script execution. Systemd will generate syslogs in case of the execution of the rc-local service.sudo cat /var/log/syslog | grep "rc-local.service|/etc/rc.local Compatibility"
can be executed to check for the execution of the service.- If logs are found, it's likely that the contents of the
rc.local
file have been executed. Analyze the logs. In case several syslog log files are available, use a wildcard to search through all of the available logs.
- If logs are found, it's likely that the contents of the
- Investigate other alerts associated with the user/host during the past 48 hours.
- Validate whether this activity is 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_3
- 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
rc.local
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/rc.local
files or restore their 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
- Process Spawned from Message-of-the-Day (MOTD)
- Potential Remote Code Execution via Web Server
- At Job Created or Modified