Potential Persistence Through init.d Detected

Files that are placed in the /etc/init.d/ directory in Unix can be used to start custom applications, services, scripts or commands during start-up. Init.d has been mostly replaced in favor of Systemd. However, the "systemd-sysv-generator" can convert init.d files to service unit files that run at boot. Adversaries may add or alter files located in the /etc/init.d/ directory to execute malicious code upon boot in order to gain persistence on the system.

Elastic rule (View on GitHub)

  1[metadata]
  2creation_date = "2023/03/21"
  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/02/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 File Listing Information"
 16query = "SELECT * FROM file WHERE (path LIKE '/etc/init.d/%' OR path LIKE '/run/systemd/generator.late/%')"
 17
 18[[transform.osquery]]
 19label = "Osquery - Retrieve Additional File Listing Information"
 20query = """
 21SELECT
 22  f.path,
 23  u.username AS file_owner,
 24  g.groupname AS group_owner,
 25  datetime(f.atime, 'unixepoch') AS file_last_access_time,
 26  datetime(f.mtime, 'unixepoch') AS file_last_modified_time,
 27  datetime(f.ctime, 'unixepoch') AS file_last_status_change_time,
 28  datetime(f.btime, 'unixepoch') AS file_created_time,
 29  f.size AS size_bytes
 30FROM
 31  file f
 32  LEFT JOIN users u ON f.uid = u.uid
 33  LEFT JOIN groups g ON f.gid = g.gid
 34WHERE (path LIKE '/etc/init.d/%' OR path LIKE '/run/systemd/generator.late/%')
 35"""
 36
 37[[transform.osquery]]
 38label = "Osquery - Retrieve Running Processes by User"
 39query = "SELECT pid, username, name FROM processes p JOIN users u ON u.uid = p.uid ORDER BY username"
 40
 41[[transform.osquery]]
 42label = "Osquery - Retrieve Crontab Information"
 43query = "SELECT * FROM crontab"
 44
 45[rule]
 46author = ["Elastic"]
 47description = """
 48Files that are placed in the /etc/init.d/ directory in Unix can be used to start custom applications, services, scripts
 49or commands during start-up. Init.d has been mostly replaced in favor of Systemd. However, the 
 50"systemd-sysv-generator" can convert init.d files to service unit files that run at boot. Adversaries may add or 
 51alter files located in the /etc/init.d/ directory to execute malicious code upon boot in order to gain persistence 
 52on the system. 
 53"""
 54from = "now-9m"
 55index = ["logs-endpoint.events.*", "endgame-*"]
 56language = "kuery"
 57license = "Elastic License v2"
 58name = "Potential Persistence Through init.d Detected"
 59note = """## Triage and analysis
 60
 61### Investigating Potential Persistence Through init.d Detected
 62
 63The `/etc/init.d` directory is used in Linux systems to store the initialization scripts for various services and daemons that are executed during system startup and shutdown.
 64
 65Attackers can abuse files within the `/etc/init.d/` directory to run scripts, commands or malicious software every time a system is rebooted by converting an executable file into a service file through the `systemd-sysv-generator`. After conversion, a unit file is created within the `/run/systemd/generator.late/` directory.
 66
 67This rule looks for the creation of new files within the `/etc/init.d/` directory. Executable files in these directories will automatically run at boot with root privileges.
 68
 69> **Note**:
 70> 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.
 71> 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.
 72#### Possible Investigation Steps
 73
 74- Investigate the file that was created or modified.
 75  - $osquery_0
 76- Investigate whether any other files in the `/etc/init.d/` or `/run/systemd/generator.late/` directories have been altered.
 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 syslog through the `sudo cat /var/log/syslog | grep 'LSB'` command to find traces of the LSB header of the script (if present). If syslog is being ingested into Elasticsearch, the same can be accomplished through Kibana.
 82- Investigate other alerts associated with the user/host during the past 48 hours.
 83- Validate whether this activity is related to planned patches, updates, network administrator activity, or legitimate software installations.
 84- Investigate whether the altered scripts call other malicious scripts elsewhere on the file system. 
 85  - If scripts or executables were dropped, retrieve the files and determine if they are malicious:
 86    - Use a private sandboxed malware analysis system to perform analysis.
 87      - Observe and collect information about the following activities:
 88        - Attempts to contact external domains and addresses.
 89          - Check if the domain is newly registered or unexpected.
 90          - Check the reputation of the domain or IP address.
 91        - File access, modification, and creation activities.
 92        - Cron jobs, services and other persistence mechanisms.
 93            - $osquery_4
 94
 95### False Positive Analysis
 96
 97- If this activity is related to new benign software installation activity, consider adding exceptions — preferably with a combination of user and command line conditions.
 98- If this activity is related to a system administrator who uses init.d for administrative purposes, consider adding exceptions for this specific administrator user account. 
 99- 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.
100
101### Related Rules
102
103- Suspicious File Creation in /etc for Persistence - 1c84dd64-7e6c-4bad-ac73-a5014ee37042
104
105### Response and remediation
106
107- Initiate the incident response process based on the outcome of the triage.
108- Isolate the involved host to prevent further post-compromise behavior.
109- If the triage identified malware, search the environment for additional compromised hosts.
110  - Implement temporary network rules, procedures, and segmentation to contain the malware.
111  - Stop suspicious processes.
112  - Immediately block the identified indicators of compromise (IoCs).
113  - Inspect the affected systems for additional malware backdoors like reverse shells, reverse proxies, or droppers that attackers could use to reinfect the system.
114- Remove and block malicious artifacts identified during triage.
115- 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.
116- Delete the maliciously created service/init.d files or restore it to the original configuration.
117- Run a full antimalware scan. This may reveal additional artifacts left in the system, persistence mechanisms, and malware components.
118- Determine the initial vector abused by the attacker and take action to prevent reinfection through the same vector.
119- Leverage the incident response data and logging to improve the mean time to detect (MTTD) and the mean time to respond (MTTR).
120"""
121references = [
122    "https://www.intezer.com/blog/malware-analysis/hiddenwasp-malware-targeting-linux-systems/",
123    "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",
124    "https://www.cyberciti.biz/faq/how-to-enable-rc-local-shell-script-on-systemd-while-booting-linux-system/"
125
126]
127risk_score = 47
128rule_id = "474fd20e-14cc-49c5-8160-d9ab4ba16c8b"
129setup = """## Setup
130
131This rule requires data coming in from Elastic Defend.
132
133### Elastic Defend Integration Setup
134Elastic 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.
135
136#### Prerequisite Requirements:
137- Fleet is required for Elastic Defend.
138- To configure Fleet Server refer to the [documentation](https://www.elastic.co/guide/en/fleet/current/fleet-server.html).
139
140#### The following steps should be executed in order to add the Elastic Defend integration on a Linux System:
141- Go to the Kibana home page and click "Add integrations".
142- In the query bar, search for "Elastic Defend" and select the integration to see more details about it.
143- Click "Add Elastic Defend".
144- Configure the integration name and optionally add a description.
145- Select the type of environment you want to protect, either "Traditional Endpoints" or "Cloud Workloads".
146- 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).
147- We suggest selecting "Complete EDR (Endpoint Detection and Response)" as a configuration setting, that provides "All events; all preventions"
148- 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.
149For more details on Elastic Agent configuration settings, refer to the [helper guide](https://www.elastic.co/guide/en/fleet/8.10/agent-policy.html).
150- Click "Save and Continue".
151- 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.
152For more details on Elastic Defend refer to the [helper guide](https://www.elastic.co/guide/en/security/current/install-endpoint.html).
153"""
154severity = "medium"
155tags = [
156        "Domain: Endpoint",
157        "OS: Linux",
158        "Use Case: Threat Detection",
159        "Tactic: Persistence",
160        "Data Source: Elastic Endgame",
161        "Resources: Investigation Guide",
162        "Data Source: Elastic Defend"
163        ]
164timestamp_override = "event.ingested"
165type = "new_terms"
166query = '''
167host.os.type :"linux" and event.action:("creation" or "file_create_event" or "rename" or "file_rename_event") and 
168file.path : /etc/init.d/* and not (
169  (process.name : ("dpkg" or "dockerd" or "rpm" or "dnf" or "chef-client" or "apk" or "yum" or "rpm" or
170  "vmis-launcher" or "exe" or "platform-python" or "executor" or "podman")) or
171  (file.extension : ("swp" or "swpx")) or
172  (process.name:mv and file.name:*.dpkg-remove) or
173  (process.name:sed and file.name:sed*) or
174  (process.name:systemd and file.name:*.dpkg-new)
175)
176'''
177
178[[rule.threat]]
179framework = "MITRE ATT&CK"
180
181[[rule.threat.technique]]
182id = "T1037"
183name = "Boot or Logon Initialization Scripts"
184reference = "https://attack.mitre.org/techniques/T1037/"
185
186[rule.threat.tactic]
187id = "TA0003"
188name = "Persistence"
189reference = "https://attack.mitre.org/tactics/TA0003/"
190
191[rule.new_terms]
192field = "new_terms_fields"
193value = ["file.path", "process.name", "host.id"]
194
195[[rule.new_terms.history_window_start]]
196field = "history_window_start"
197value = "now-7d"

Triage and analysis

Investigating Potential Persistence Through init.d Detected

The /etc/init.d directory is used in Linux systems to store the initialization scripts for various services and daemons that are executed during system startup and shutdown.

Attackers can abuse files within the /etc/init.d/ directory to run scripts, commands or malicious software every time a system is rebooted by converting an executable file into a service file through the systemd-sysv-generator. After conversion, a unit file is created within the /run/systemd/generator.late/ directory.

This rule looks for the creation of new files within the /etc/init.d/ directory. Executable files in these directories will automatically run at boot with root privileges.

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 file that was created or modified.
    • $osquery_0
  • Investigate whether any other files in the /etc/init.d/ or /run/systemd/generator.late/ directories have been altered.
    • $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 syslog through the sudo cat /var/log/syslog | grep 'LSB' command to find traces of the LSB header of the script (if present). If syslog is being ingested into Elasticsearch, the same can be accomplished through Kibana.
  • 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_4

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 init.d 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.
  • Suspicious File Creation in /etc for Persistence - 1c84dd64-7e6c-4bad-ac73-a5014ee37042

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.
  • Remove and block malicious artifacts identified during triage.
  • 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 maliciously created service/init.d files or restore it to the 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