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

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