Potential Persistence Through Run Control Detected
This rule monitors the creation/alteration of the rc.local file by a previously unknown process executable through the use of the new terms rule type. 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 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"
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 Running Processes by User"
16query = "SELECT pid, username, name FROM processes p JOIN users u ON u.uid = p.uid ORDER BY username"
17
18[[transform.osquery]]
19label = "Osquery - Retrieve rc-local.service File Information"
20query = "SELECT * FROM file WHERE (path = '/run/systemd/generator/multi-user.target.wants/rc-local.service' OR path = '/run/systemd/generator/multi-user.target.wants/rc-local.service')"
21
22[[transform.osquery]]
23label = "Osquery - Retrieve Crontab Information"
24query = "SELECT * FROM crontab"
25
26[rule]
27author = ["Elastic"]
28description = """
29This rule monitors the creation/alteration of the rc.local file by a previously unknown process executable
30through the use of the new terms rule type. The /etc/rc.local file is used to start custom applications,
31services, scripts or commands during start-up. The rc.local file has mostly been replaced by Systemd.
32However, through the "systemd-rc-local-generator", rc.local files can be converted to services that run at
33boot. Adversaries may alter rc.local to execute malicious code at start-up, and gain persistence onto the
34system.
35"""
36from = "now-9m"
37index = ["logs-endpoint.events.*", "endgame-*"]
38language = "kuery"
39license = "Elastic License v2"
40name = "Potential Persistence Through Run Control Detected"
41note = """## Triage and analysis
42
43### Investigating Potential Persistence Through Run Control Detected
44
45The `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.
46
47There might still be users that use `rc.local` in a benign matter, so investigation to see whether the file is malicious is vital.
48
49Detection alerts from this rule indicate the creation of a new `/etc/rc.local` file.
50
51> **Note**:
52> 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.
53> 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.
54
55#### Possible Investigation Steps
56
57- Identify the user account that performed the action and whether it should perform this kind of action.
58- Investigate the file that was created or modified.
59 - $osquery_0
60- 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.
61 - $osquery_1
62- 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`.
63 - $osquery_2
64 - 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.
65 - 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.
66 - 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.
67- Investigate other alerts associated with the user/host during the past 48 hours.
68- Validate whether this activity is related to planned patches, updates, network administrator activity, or legitimate software installations.
69- Investigate whether the altered scripts call other malicious scripts elsewhere on the file system.
70 - If scripts or executables were dropped, retrieve the files and determine if they are malicious:
71 - Use a private sandboxed malware analysis system to perform analysis.
72 - Observe and collect information about the following activities:
73 - Attempts to contact external domains and addresses.
74 - Check if the domain is newly registered or unexpected.
75 - Check the reputation of the domain or IP address.
76 - File access, modification, and creation activities.
77 - Cron jobs, services and other persistence mechanisms.
78 - $osquery_3
79
80### False Positive Analysis
81
82- If this activity is related to new benign software installation activity, consider adding exceptions — preferably with a combination of user and command line conditions.
83- 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.
84- 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.
85
86### Response and remediation
87- Initiate the incident response process based on the outcome of the triage.
88- Isolate the involved host to prevent further post-compromise behavior.
89- If the triage identified malware, search the environment for additional compromised hosts.
90 - Implement temporary network rules, procedures, and segmentation to contain the malware.
91 - Stop suspicious processes.
92 - Immediately block the identified indicators of compromise (IoCs).
93 - Inspect the affected systems for additional malware backdoors like reverse shells, reverse proxies, or droppers that attackers could use to reinfect the system.
94- 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.
95- Delete the `service/rc.local` files or restore their original configuration.
96- Run a full antimalware scan. This may reveal additional artifacts left in the system, persistence mechanisms, and malware components.
97- Determine the initial vector abused by the attacker and take action to prevent reinfection through the same vector.
98- Leverage the incident response data and logging to improve the mean time to detect (MTTD) and the mean time to respond (MTTR).
99"""
100references = [
101 "https://www.intezer.com/blog/malware-analysis/hiddenwasp-malware-targeting-linux-systems/",
102 "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",
103 "https://www.cyberciti.biz/faq/how-to-enable-rc-local-shell-script-on-systemd-while-booting-linux-system/"
104
105]
106risk_score = 47
107rule_id = "0f4d35e4-925e-4959-ab24-911be207ee6f"
108severity = "medium"
109tags = ["Domain: Endpoint", "OS: Linux", "Use Case: Threat Detection", "Tactic: Persistence", "Data Source: Elastic Endgame", "Resources: Investigation Guide", "Data Source: Elastic Defend"]
110type = "new_terms"
111query = '''
112host.os.type : "linux" and event.category : "file" and
113event.type : ("change" or "file_modify_event" or "creation" or "file_create_event") and
114file.path : "/etc/rc.local" and not process.name : ("dockerd" or "docker" or "dnf" or "yum" or "rpm" or "dpkg") and not file.extension : ("swp" or "swx")
115'''
116
117[[rule.threat]]
118framework = "MITRE ATT&CK"
119
120[[rule.threat.technique]]
121id = "T1037"
122name = "Boot or Logon Initialization Scripts"
123reference = "https://attack.mitre.org/techniques/T1037/"
124
125[[rule.threat.technique.subtechnique]]
126id = "T1037.004"
127name = "RC Scripts"
128reference = "https://attack.mitre.org/techniques/T1037/004/"
129
130[rule.threat.tactic]
131id = "TA0003"
132name = "Persistence"
133reference = "https://attack.mitre.org/tactics/TA0003/"
134
135[rule.new_terms]
136field = "new_terms_fields"
137value = ["host.id", "process.executable"]
138
139[[rule.new_terms.history_window_start]]
140field = "history_window_start"
141value = "now-7d"
Triage and analysis
Investigating Potential Persistence Through Run Control Detected
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
- Linux User Added to Privileged Group
- New Systemd Timer Created
- Potential Linux Backdoor User Account Creation
- Potential Persistence Through MOTD File Creation Detected
- Potential Persistence Through init.d Detected