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 = "2024/01/05"
  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"
108setup = """## Setup
109
110This rule requires data coming in from Elastic Defend.
111
112### Elastic Defend Integration Setup
113Elastic 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.
114
115#### Prerequisite Requirements:
116- Fleet is required for Elastic Defend.
117- To configure Fleet Server refer to the [documentation](https://www.elastic.co/guide/en/fleet/current/fleet-server.html).
118
119#### The following steps should be executed in order to add the Elastic Defend integration on a Linux System:
120- Go to the Kibana home page and click "Add integrations".
121- In the query bar, search for "Elastic Defend" and select the integration to see more details about it.
122- Click "Add Elastic Defend".
123- Configure the integration name and optionally add a description.
124- Select the type of environment you want to protect, either "Traditional Endpoints" or "Cloud Workloads".
125- 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).
126- We suggest selecting "Complete EDR (Endpoint Detection and Response)" as a configuration setting, that provides "All events; all preventions"
127- 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.
128For more details on Elastic Agent configuration settings, refer to the [helper guide](https://www.elastic.co/guide/en/fleet/8.10/agent-policy.html).
129- Click "Save and Continue".
130- 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.
131For more details on Elastic Defend refer to the [helper guide](https://www.elastic.co/guide/en/security/current/install-endpoint.html).
132"""
133severity = "medium"
134tags = [
135        "Domain: Endpoint",
136        "OS: Linux",
137        "Use Case: Threat Detection",
138        "Tactic: Persistence",
139        "Data Source: Elastic Endgame",
140        "Resources: Investigation Guide",
141        "Data Source: Elastic Defend"
142        ]
143type = "new_terms"
144timestamp_override = "event.ingested"
145query = '''
146host.os.type : "linux" and event.category : "file" and
147event.type : ("change" or "file_modify_event" or "creation" or "file_create_event") and
148file.path : "/etc/rc.local" and not process.name : (
149  "dockerd" or "docker" or "dnf" or "dnf-automatic" or "yum" or "rpm" or "dpkg"
150) and not file.extension : ("swp" or "swpx")
151'''
152
153[[rule.threat]]
154framework = "MITRE ATT&CK"
155
156[[rule.threat.technique]]
157id = "T1037"
158name = "Boot or Logon Initialization Scripts"
159reference = "https://attack.mitre.org/techniques/T1037/"
160
161[[rule.threat.technique.subtechnique]]
162id = "T1037.004"
163name = "RC Scripts"
164reference = "https://attack.mitre.org/techniques/T1037/004/"
165
166[rule.threat.tactic]
167id = "TA0003"
168name = "Persistence"
169reference = "https://attack.mitre.org/tactics/TA0003/"
170
171[rule.new_terms]
172field = "new_terms_fields"
173value = ["host.id", "process.executable", "user.id"]
174
175[[rule.new_terms.history_window_start]]
176field = "history_window_start"
177value = "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 the systemd-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.
  • 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

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

to-top