Suspicious File Creation in /etc for Persistence

Detects the manual creation of files in specific etc directories, via user root, used by Linux malware to persist and elevate privileges on compromised systems. File creation in these directories should not be entirely common and could indicate a malicious binary or script installing persistence mechanisms for long term access.

Elastic rule (View on GitHub)

  1[metadata]
  2creation_date = "2022/07/22"
  3integration = ["endpoint"]
  4maturity = "production"
  5min_stack_comments = "New fields added: required_fields, related_integrations, setup"
  6min_stack_version = "8.3.0"
  7updated_date = "2024/02/21"
  8
  9[transform]
 10[[transform.osquery]]
 11label = "Osquery - Retrieve File Listing Information"
 12query = """
 13SELECT * FROM file WHERE (
 14  path LIKE '/etc/ld.so.conf.d/%' OR
 15  path LIKE '/etc/cron.d/%' OR
 16  path LIKE '/etc/sudoers.d/%' OR
 17  path LIKE '/etc/rc%.d/%' OR
 18  path LIKE '/etc/init.d/%' OR
 19  path LIKE '/etc/systemd/system/%' OR
 20  path LIKE '/usr/lib/systemd/system/%'
 21)
 22"""
 23[[transform.osquery]]
 24label = "Osquery - Retrieve Additional File Listing Information"
 25query = """
 26SELECT
 27  f.path,
 28  u.username AS file_owner,
 29  g.groupname AS group_owner,
 30  datetime(f.atime, 'unixepoch') AS file_last_access_time,
 31  datetime(f.mtime, 'unixepoch') AS file_last_modified_time,
 32  datetime(f.ctime, 'unixepoch') AS file_last_status_change_time,
 33  datetime(f.btime, 'unixepoch') AS file_created_time,
 34  f.size AS size_bytes
 35FROM
 36  file f
 37  LEFT JOIN users u ON f.uid = u.uid
 38  LEFT JOIN groups g ON f.gid = g.gid
 39WHERE (
 40  path LIKE '/etc/ld.so.conf.d/%' OR
 41  path LIKE '/etc/cron.d/%' OR
 42  path LIKE '/etc/sudoers.d/%' OR
 43  path LIKE '/etc/rc%.d/%' OR
 44  path LIKE '/etc/init.d/%' OR
 45  path LIKE '/etc/systemd/system/%' OR
 46  path LIKE '/usr/lib/systemd/system/%'
 47)
 48"""
 49
 50[[transform.osquery]]
 51label = "Osquery - Retrieve Running Processes by User"
 52query = "SELECT pid, username, name FROM processes p JOIN users u ON u.uid = p.uid ORDER BY username"
 53
 54[[transform.osquery]]
 55label = "Osquery - Retrieve Crontab Information"
 56query = "SELECT * FROM crontab"
 57
 58[[transform.osquery]]
 59label = "Osquery - Retrieve Listening Ports"
 60query = "SELECT pid, address, port, socket, protocol, path FROM listening_ports"
 61
 62[[transform.osquery]]
 63label = "Osquery - Retrieve Open Sockets"
 64query = "SELECT pid, family, remote_address, remote_port, socket, state FROM process_open_sockets"
 65
 66[[transform.osquery]]
 67label = "Osquery - Retrieve Information for a Specific User"
 68query = "SELECT * FROM users WHERE username = {{user.name}}"
 69
 70[[transform.osquery]]
 71label = "Osquery - Investigate the Account Authentication Status"
 72query = "SELECT * FROM logged_in_users WHERE user = {{user.name}}"
 73
 74[rule]
 75author = ["Elastic"]
 76description = """
 77Detects the manual creation of files in specific etc directories, via user root, used by Linux malware to persist and
 78elevate privileges on compromised systems. File creation in these directories should not be entirely common and could
 79indicate a malicious binary or script installing persistence mechanisms for long term access.
 80"""
 81from = "now-9m"
 82index = ["logs-endpoint.events.*", "endgame-*"]
 83language = "eql"
 84license = "Elastic License v2"
 85name = "Suspicious File Creation in /etc for Persistence"
 86note = """## Triage and analysis
 87
 88### Investigating Suspicious File Creation in /etc for Persistence
 89
 90The /etc/ directory in Linux is used to store system-wide configuration files and scripts.
 91
 92By creating or modifying specific system-wide configuration files, attackers can leverage system services to execute malicious commands or scripts at predefined intervals, ensuring their continued presence and enabling unauthorized activities.
 93
 94This rule monitors for the creation of the most common system-wide configuration files and scripts abused by attackers for persistence. 
 95
 96> **Note**:
 97> 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.
 98> 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.
 99
100#### Possible Investigation Steps
101
102- Investigate the file that was created or modified.
103- Investigate whether any other files in any of the commonly abused directories have been altered through OSQuery.
104  - $osquery_0
105  - $osquery_1
106- 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.
107  - $osquery_2
108- Investigate other alerts associated with the user/host during the past 48 hours.
109- Validate the activity is not related to planned patches, updates, network administrator activity, or legitimate software installations.
110- Investigate whether the altered scripts call other malicious scripts elsewhere on the file system. 
111  - If scripts or executables were dropped, retrieve the files and determine if they are malicious:
112    - Use a private sandboxed malware analysis system to perform analysis.
113      - Observe and collect information about the following activities:
114        - Attempts to contact external domains and addresses.
115          - Check if the domain is newly registered or unexpected.
116          - Check the reputation of the domain or IP address.
117        - File access, modification, and creation activities.
118        - Cron jobs, services and other persistence mechanisms.
119            - $osquery_3
120- Investigate abnormal behaviors by the subject process/user such as network connections, file modifications, and any other spawned child processes.
121  - Investigate listening ports and open sockets to look for potential command and control traffic or data exfiltration.
122    - $osquery_4
123    - $osquery_5
124  - Identify the user account that performed the action, analyze it, and check whether it should perform this kind of action.
125    - $osquery_6
126- Investigate whether the user is currently logged in and active.
127    - $osquery_7
128
129### False Positive Analysis
130
131- If this activity is related to new benign software installation activity, consider adding exceptions — preferably with a combination of user and command line conditions.
132- If this activity is related to a system administrator that performed these actions for administrative purposes, consider adding exceptions for this specific administrator user account. 
133- 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.
134
135### Related Rules
136
137- Cron Job Created or Changed by Previously Unknown Process - ff10d4d8-fea7-422d-afb1-e5a2702369a9
138- Potential Persistence Through Run Control Detected - 0f4d35e4-925e-4959-ab24-911be207ee6f
139- Potential Persistence Through init.d Detected - 474fd20e-14cc-49c5-8160-d9ab4ba16c8b
140- New Systemd Timer Created - 7fb500fa-8e24-4bd1-9480-2a819352602c
141- New Systemd Service Created by Previously Unknown Process - 17b0a495-4d9f-414c-8ad0-92f018b8e001
142
143### Response and remediation
144
145- Initiate the incident response process based on the outcome of the triage.
146- Isolate the involved host to prevent further post-compromise behavior.
147- If the triage identified malware, search the environment for additional compromised hosts.
148  - Implement temporary network rules, procedures, and segmentation to contain the malware.
149  - Stop suspicious processes.
150  - Immediately block the identified indicators of compromise (IoCs).
151  - Inspect the affected systems for additional malware backdoors like reverse shells, reverse proxies, or droppers that attackers could use to reinfect the system.
152- 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.
153- Delete the service/timer or restore its original configuration.
154- Run a full antimalware scan. This may reveal additional artifacts left in the system, persistence mechanisms, and malware components.
155- Determine the initial vector abused by the attacker and take action to prevent reinfection through the same vector.
156- Leverage the incident response data and logging to improve the mean time to detect (MTTD) and the mean time to respond (MTTR).
157"""
158references = [
159    "https://www.intezer.com/blog/incident-response/orbit-new-undetected-linux-threat/",
160    "https://www.intezer.com/blog/research/lightning-framework-new-linux-threat/"
161]
162risk_score = 47
163rule_id = "1c84dd64-7e6c-4bad-ac73-a5014ee37042"
164setup = """## Setup
165
166This rule requires data coming in from Elastic Defend.
167
168### Elastic Defend Integration Setup
169Elastic 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.
170
171#### Prerequisite Requirements:
172- Fleet is required for Elastic Defend.
173- To configure Fleet Server refer to the [documentation](https://www.elastic.co/guide/en/fleet/current/fleet-server.html).
174
175#### The following steps should be executed in order to add the Elastic Defend integration on a Linux System:
176- Go to the Kibana home page and click "Add integrations".
177- In the query bar, search for "Elastic Defend" and select the integration to see more details about it.
178- Click "Add Elastic Defend".
179- Configure the integration name and optionally add a description.
180- Select the type of environment you want to protect, either "Traditional Endpoints" or "Cloud Workloads".
181- 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).
182- We suggest selecting "Complete EDR (Endpoint Detection and Response)" as a configuration setting, that provides "All events; all preventions"
183- 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.
184For more details on Elastic Agent configuration settings, refer to the [helper guide](https://www.elastic.co/guide/en/fleet/8.10/agent-policy.html).
185- Click "Save and Continue".
186- 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.
187For more details on Elastic Defend refer to the [helper guide](https://www.elastic.co/guide/en/security/current/install-endpoint.html).
188"""
189severity = "medium"
190tags = [
191        "Domain: Endpoint",
192        "OS: Linux",
193        "Use Case: Threat Detection",
194        "Tactic: Persistence",
195        "Threat: Orbit",
196        "Threat: Lightning Framework",
197        "Data Source: Elastic Endgame",
198        "Data Source: Elastic Defend"
199        ]
200timestamp_override = "event.ingested"
201type = "eql"
202query = '''
203file where host.os.type == "linux" and event.type in ("creation", "file_create_event") and user.id == "0" and
204file.path : ("/etc/ld.so.conf.d/*", "/etc/cron.d/*", "/etc/sudoers.d/*", "/etc/rc.d/init.d/*", "/etc/systemd/system/*",
205"/usr/lib/systemd/system/*") and not (
206  (process.name : (
207    "chef-client", "ruby", "pacman", "packagekitd", "python*", "platform-python", "dpkg", "yum", "apt", "dnf", "rpm",
208    "systemd", "snapd", "dnf-automatic", "yum-cron", "elastic-agent", "dnfdaemon-system", "dockerd", "executor",
209    "rhn_check"
210    )
211  ) or 
212  (file.extension in ("swp", "swpx", "tmp"))
213)
214'''
215
216[[rule.threat]]
217framework = "MITRE ATT&CK"
218
219[rule.threat.tactic]
220id = "TA0003"
221name = "Persistence"
222reference = "https://attack.mitre.org/tactics/TA0003/"
223
224[[rule.threat.technique]]
225id = "T1037"
226name = "Boot or Logon Initialization Scripts"
227reference = "https://attack.mitre.org/techniques/T1037/"
228
229[[rule.threat.technique.subtechnique]]
230id = "T1037.004"
231name = "RC Scripts"
232reference = "https://attack.mitre.org/techniques/T1037/004/"
233
234[[rule.threat.technique]]
235id = "T1574"
236name = "Hijack Execution Flow"
237reference = "https://attack.mitre.org/techniques/T1574/"
238
239[[rule.threat.technique.subtechnique]]
240id = "T1574.006"
241name = "Dynamic Linker Hijacking"
242reference = "https://attack.mitre.org/techniques/T1574/006/"
243
244[[rule.threat.technique]]
245id = "T1543"
246name = "Create or Modify System Process"
247reference = "https://attack.mitre.org/techniques/T1543/"
248
249[[rule.threat.technique.subtechnique]]
250id = "T1543.002"
251name = "Systemd Service"
252reference = "https://attack.mitre.org/techniques/T1543/002/"
253
254[[rule.threat]]
255framework = "MITRE ATT&CK"
256
257[rule.threat.tactic]
258id = "TA0002"
259name = "Execution"
260reference = "https://attack.mitre.org/tactics/TA0002/"
261
262[[rule.threat.technique]]
263id = "T1053"
264name = "Scheduled Task/Job"
265reference = "https://attack.mitre.org/techniques/T1053/"
266
267[[rule.threat.technique.subtechnique]]
268id = "T1053.003"
269name = "Cron"
270reference = "https://attack.mitre.org/techniques/T1053/003/"
271
272[[rule.threat]]
273framework = "MITRE ATT&CK"
274
275[rule.threat.tactic]
276id = "TA0004"
277name = "Privilege Escalation"
278reference = "https://attack.mitre.org/tactics/TA0004/"
279
280[[rule.threat.technique]]
281id = "T1548"
282name = "Abuse Elevation Control Mechanism"
283reference = "https://attack.mitre.org/techniques/T1548/"
284
285[[rule.threat.technique.subtechnique]]
286id = "T1548.003"
287name = "Sudo and Sudo Caching"
288reference = "https://attack.mitre.org/techniques/T1548/003/"

Triage and analysis

Investigating Suspicious File Creation in /etc for Persistence

The /etc/ directory in Linux is used to store system-wide configuration files and scripts.

By creating or modifying specific system-wide configuration files, attackers can leverage system services to execute malicious commands or scripts at predefined intervals, ensuring their continued presence and enabling unauthorized activities.

This rule monitors for the creation of the most common system-wide configuration files and scripts abused by attackers for persistence.

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.
  • Investigate whether any other files in any of the commonly abused directories have been altered through OSQuery.
    • $osquery_0
    • $osquery_1
  • 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_2
  • 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_3
  • 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_4
      • $osquery_5
    • Identify the user account that performed the action, analyze it, and check whether it should perform this kind of action.
      • $osquery_6
  • Investigate whether the user is currently logged in and active.
    • $osquery_7

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 that performed these actions 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.
  • Cron Job Created or Changed by Previously Unknown Process - ff10d4d8-fea7-422d-afb1-e5a2702369a9
  • Potential Persistence Through Run Control Detected - 0f4d35e4-925e-4959-ab24-911be207ee6f
  • Potential Persistence Through init.d Detected - 474fd20e-14cc-49c5-8160-d9ab4ba16c8b
  • New Systemd Timer Created - 7fb500fa-8e24-4bd1-9480-2a819352602c
  • New Systemd Service Created by Previously Unknown Process - 17b0a495-4d9f-414c-8ad0-92f018b8e001

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

to-top