Persistence via KDE AutoStart Script or Desktop File Modification

Identifies the creation or modification of a K Desktop Environment (KDE) AutoStart script or desktop file that will execute upon each user logon. Adversaries may abuse this method for persistence.

Elastic rule (View on GitHub)

  1[metadata]
  2creation_date = "2021/01/06"
  3integration = ["endpoint"]
  4maturity = "production"
  5updated_date = "2024/10/17"
  6
  7[transform]
  8[[transform.osquery]]
  9label = "Osquery - Retrieve File Listing Information"
 10query = """
 11SELECT * FROM file WHERE ( path LIKE '/home/%/.config/autostart/%.sh' OR path LIKE '/home/%/.config/autostart/%.desktop'
 12OR path LIKE '/root/.config/autostart/%.sh' OR path LIKE '/root/.config/autostart/%.desktop' OR path LIKE
 13'/home/%/.kde/Autostart/%.sh' OR path LIKE '/home/%/.kde/Autostart/%.desktop' OR path LIKE '/root/.kde/Autostart/%.sh'
 14OR path LIKE '/root/.kde/Autostart/%.desktop' OR path LIKE '/home/%/.kde4/Autostart/%.sh' OR path LIKE
 15'/home/%/.kde4/Autostart/%.desktop' OR path LIKE '/root/.kde4/Autostart/%.sh' OR path LIKE
 16'/root/.kde4/Autostart/%.desktop' OR path LIKE '/home/%/.kde/share/autostart/%.sh' OR path LIKE
 17'/home/%/.kde/share/autostart/%.desktop' OR path LIKE '/root/.kde/share/autostart/%.sh' OR path LIKE
 18'/root/.kde/share/autostart/%.desktop' OR path LIKE '/home/%/.kde4/share/autostart/%.sh' OR path LIKE
 19'/home/%/.kde4/share/autostart/%.desktop' OR path LIKE '/root/.kde4/share/autostart/%.sh' OR path LIKE
 20'/root/.kde4/share/autostart/%.desktop' OR path LIKE '/home/%/.local/share/autostart/%.sh' OR path LIKE
 21'/home/%/.local/share/autostart/%.desktop' OR path LIKE '/root/.local/share/autostart/%.sh' OR path LIKE
 22'/root/.local/share/autostart/%.desktop' OR path LIKE '/home/%/.config/autostart-scripts/%.sh' OR path LIKE
 23'/home/%/.config/autostart-scripts/%.desktop' OR path LIKE '/root/.config/autostart-scripts/%.sh' OR path LIKE
 24'/root/.config/autostart-scripts/%.desktop' OR path LIKE '/etc/xdg/autostart/%.sh' OR path LIKE
 25'/etc/xdg/autostart/%.desktop' OR path LIKE '/usr/share/autostart/%.sh' OR path LIKE '/usr/share/autostart/%.desktop' )
 26"""
 27
 28[[transform.osquery]]
 29label = "Osquery - Retrieve Additional File Listing Information"
 30query = """
 31SELECT f.path, u.username AS file_owner, g.groupname AS group_owner, datetime(f.atime, 'unixepoch') AS
 32file_last_access_time, datetime(f.mtime, 'unixepoch') AS file_last_modified_time, datetime(f.ctime, 'unixepoch') AS
 33file_last_status_change_time, datetime(f.btime, 'unixepoch') AS file_created_time, f.size AS size_bytes FROM file f LEFT
 34JOIN users u ON f.uid = u.uid LEFT JOIN groups g ON f.gid = g.gid WHERE ( path LIKE '/home/%/.config/autostart/%.sh' OR
 35path LIKE '/home/%/.config/autostart/%.desktop' OR path LIKE '/root/.config/autostart/%.sh' OR path LIKE
 36'/root/.config/autostart/%.desktop' OR path LIKE '/home/%/.kde/Autostart/%.sh' OR path LIKE
 37'/home/%/.kde/Autostart/%.desktop' OR path LIKE '/root/.kde/Autostart/%.sh' OR path LIKE
 38'/root/.kde/Autostart/%.desktop' OR path LIKE '/home/%/.kde4/Autostart/%.sh' OR path LIKE
 39'/home/%/.kde4/Autostart/%.desktop' OR path LIKE '/root/.kde4/Autostart/%.sh' OR path LIKE
 40'/root/.kde4/Autostart/%.desktop' OR path LIKE '/home/%/.kde/share/autostart/%.sh' OR path LIKE
 41'/home/%/.kde/share/autostart/%.desktop' OR path LIKE '/root/.kde/share/autostart/%.sh' OR path LIKE
 42'/root/.kde/share/autostart/%.desktop' OR path LIKE '/home/%/.kde4/share/autostart/%.sh' OR path LIKE
 43'/home/%/.kde4/share/autostart/%.desktop' OR path LIKE '/root/.kde4/share/autostart/%.sh' OR path LIKE
 44'/root/.kde4/share/autostart/%.desktop' OR path LIKE '/home/%/.local/share/autostart/%.sh' OR path LIKE
 45'/home/%/.local/share/autostart/%.desktop' OR path LIKE '/root/.local/share/autostart/%.sh' OR path LIKE
 46'/root/.local/share/autostart/%.desktop' OR path LIKE '/home/%/.config/autostart-scripts/%.sh' OR path LIKE
 47'/home/%/.config/autostart-scripts/%.desktop' OR path LIKE '/root/.config/autostart-scripts/%.sh' OR path LIKE
 48'/root/.config/autostart-scripts/%.desktop' OR path LIKE '/etc/xdg/autostart/%.sh' OR path LIKE
 49'/etc/xdg/autostart/%.desktop' OR path LIKE '/usr/share/autostart/%.sh' OR path LIKE '/usr/share/autostart/%.desktop' )
 50"""
 51
 52[[transform.osquery]]
 53label = "Osquery - Retrieve Running Processes by User"
 54query = "SELECT pid, username, name FROM processes p JOIN users u ON u.uid = p.uid ORDER BY username"
 55
 56[[transform.osquery]]
 57label = "Osquery - Retrieve Crontab Information"
 58query = "SELECT * FROM crontab"
 59
 60[[transform.osquery]]
 61label = "Osquery - Retrieve Listening Ports"
 62query = "SELECT pid, address, port, socket, protocol, path FROM listening_ports"
 63
 64[[transform.osquery]]
 65label = "Osquery - Retrieve Open Sockets"
 66query = "SELECT pid, family, remote_address, remote_port, socket, state FROM process_open_sockets"
 67
 68[[transform.osquery]]
 69label = "Osquery - Retrieve Information for a Specific User"
 70query = "SELECT * FROM users WHERE username = {{user.name}}"
 71
 72[[transform.osquery]]
 73label = "Osquery - Investigate the Account Authentication Status"
 74query = "SELECT * FROM logged_in_users WHERE user = {{user.name}}"
 75
 76
 77[rule]
 78author = ["Elastic"]
 79description = """
 80Identifies the creation or modification of a K Desktop Environment (KDE) AutoStart script or desktop file that will
 81execute upon each user logon. Adversaries may abuse this method for persistence.
 82"""
 83from = "now-9m"
 84index = ["auditbeat-*", "logs-endpoint.events.*", "endgame-*"]
 85language = "eql"
 86license = "Elastic License v2"
 87name = "Persistence via KDE AutoStart Script or Desktop File Modification"
 88note = """## Triage and analysis
 89
 90### Investigating Persistence via KDE AutoStart Script or Desktop File Modification
 91
 92K Desktop Environment (KDE) is a popular graphical desktop environment for Linux systems. It supports AutoStart scripts and desktop files that execute automatically upon user logon.
 93
 94Adversaries may exploit this feature to maintain persistence on a compromised system by creating or modifying these files.
 95
 96The detection rule 'Persistence via KDE AutoStart Script or Desktop File Modification' is designed to identify such activities by monitoring file events on Linux systems. It specifically targets the creation or modification of files with extensions ".sh" or ".desktop" in various AutoStart directories. By detecting these events, the rule helps security analysts identify potential abuse of KDE AutoStart functionality by malicious actors.
 97
 98> **Note**:
 99> 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.
100> 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.
101
102### Possible investigation steps
103
104- Investigate the file that was created or modified.
105  - $osquery_0
106  - $osquery_1
107- 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.
108  - $osquery_2
109- Investigate other alerts associated with the user/host during the past 48 hours.
110- Validate the activity is not related to planned patches, updates, network administrator activity, or legitimate software installations.
111- Investigate whether the altered scripts call other malicious scripts elsewhere on the file system. 
112  - If scripts or executables were dropped, retrieve the files and determine if they are malicious:
113    - Use a private sandboxed malware analysis system to perform analysis.
114      - Observe and collect information about the following activities:
115        - Attempts to contact external domains and addresses.
116          - Check if the domain is newly registered or unexpected.
117          - Check the reputation of the domain or IP address.
118        - File access, modification, and creation activities.
119        - Cron jobs, services and other persistence mechanisms.
120            - $osquery_3
121- Investigate abnormal behaviors by the subject process/user such as network connections, file modifications, and any other spawned child processes.
122  - Investigate listening ports and open sockets to look for potential command and control traffic or data exfiltration.
123    - $osquery_4
124    - $osquery_5
125  - Identify the user account that performed the action, analyze it, and check whether it should perform this kind of action.
126    - $osquery_6
127- Investigate whether the user is currently logged in and active.
128    - $osquery_7
129
130### False positive analysis
131
132- If this activity is related to new benign software installation activity, consider adding exceptions — preferably with a combination of user and command line conditions.
133- If this activity is related to a system administrator who uses cron jobs for administrative purposes, consider adding exceptions for this specific administrator user account. 
134- 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.
135
136### Response and Remediation
137
138- Initiate the incident response process based on the outcome of the triage.
139- Isolate the involved host to prevent further post-compromise behavior.
140- If the triage identified malware, search the environment for additional compromised hosts.
141  - Implement temporary network rules, procedures, and segmentation to contain the malware.
142  - Stop suspicious processes.
143  - Immediately block the identified indicators of compromise (IoCs).
144  - Inspect the affected systems for additional malware backdoors like reverse shells, reverse proxies, or droppers that attackers could use to reinfect the system.
145- 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.
146- Run a full antimalware scan. This may reveal additional artifacts left in the system, persistence mechanisms, and malware components.
147- Determine the initial vector abused by the attacker and take action to prevent reinfection through the same vector.
148- Leverage the incident response data and logging to improve the mean time to detect (MTTD) and the mean time to respond (MTTR).
149"""
150references = [
151    "https://userbase.kde.org/System_Settings/Autostart",
152    "https://www.amnesty.org/en/latest/research/2020/09/german-made-finspy-spyware-found-in-egypt-and-mac-and-linux-versions-revealed/",
153    "https://www.intezer.com/blog/research/operation-electrorat-attacker-creates-fake-companies-to-drain-your-crypto-wallets/",
154    "https://www.elastic.co/security-labs/primer-on-persistence-mechanisms",
155]
156risk_score = 47
157rule_id = "e3e904b3-0a8e-4e68-86a8-977a163e21d3"
158setup = """## Setup
159
160This rule requires data coming in from one of the following integrations:
161- Elastic Defend
162- Auditbeat
163
164### Elastic Defend Integration Setup
165Elastic 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.
166
167#### Prerequisite Requirements:
168- Fleet is required for Elastic Defend.
169- To configure Fleet Server refer to the [documentation](https://www.elastic.co/guide/en/fleet/current/fleet-server.html).
170
171#### The following steps should be executed in order to add the Elastic Defend integration on a Linux System:
172- Go to the Kibana home page and click "Add integrations".
173- In the query bar, search for "Elastic Defend" and select the integration to see more details about it.
174- Click "Add Elastic Defend".
175- Configure the integration name and optionally add a description.
176- Select the type of environment you want to protect, either "Traditional Endpoints" or "Cloud Workloads".
177- 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).
178- We suggest selecting "Complete EDR (Endpoint Detection and Response)" as a configuration setting, that provides "All events; all preventions"
179- 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.
180For more details on Elastic Agent configuration settings, refer to the [helper guide](https://www.elastic.co/guide/en/fleet/8.10/agent-policy.html).
181- Click "Save and Continue".
182- 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.
183For more details on Elastic Defend refer to the [helper guide](https://www.elastic.co/guide/en/security/current/install-endpoint.html).
184
185### Auditbeat Setup
186Auditbeat is a lightweight shipper that you can install on your servers to audit the activities of users and processes on your systems. For example, you can use Auditbeat to collect and centralize audit events from the Linux Audit Framework. You can also use Auditbeat to detect changes to critical files, like binaries and configuration files, and identify potential security policy violations.
187
188#### The following steps should be executed in order to add the Auditbeat on a Linux System:
189- Elastic provides repositories available for APT and YUM-based distributions. Note that we provide binary packages, but no source packages.
190- To install the APT and YUM repositories follow the setup instructions in this [helper guide](https://www.elastic.co/guide/en/beats/auditbeat/current/setup-repositories.html).
191- To run Auditbeat on Docker follow the setup instructions in the [helper guide](https://www.elastic.co/guide/en/beats/auditbeat/current/running-on-docker.html).
192- To run Auditbeat on Kubernetes follow the setup instructions in the [helper guide](https://www.elastic.co/guide/en/beats/auditbeat/current/running-on-kubernetes.html).
193- For complete “Setup and Run Auditbeat” information refer to the [helper guide](https://www.elastic.co/guide/en/beats/auditbeat/current/setting-up-and-running.html).
194
195#### Custom Ingest Pipeline
196For versions <8.2, you need to add a custom ingest pipeline to populate `event.ingested` with @timestamp for non-elastic-agent indexes, like auditbeats/filebeat/winlogbeat etc. For more details to add a custom ingest pipeline refer to the [guide](https://www.elastic.co/guide/en/fleet/current/data-streams-pipeline-tutorial.html).
197"""
198severity = "medium"
199tags = [
200    "Domain: Endpoint",
201    "OS: Linux",
202    "Use Case: Threat Detection",
203    "Tactic: Persistence",
204    "Data Source: Elastic Endgame",
205    "Data Source: Elastic Defend",
206]
207timestamp_override = "event.ingested"
208type = "eql"
209
210query = '''
211file where host.os.type == "linux" and event.type != "deletion" and
212  file.extension in ("sh", "desktop") and
213  file.path :
214    (
215      "/home/*/.config/autostart/*", "/root/.config/autostart/*",
216      "/home/*/.kde/Autostart/*", "/root/.kde/Autostart/*",
217      "/home/*/.kde4/Autostart/*", "/root/.kde4/Autostart/*",
218      "/home/*/.kde/share/autostart/*", "/root/.kde/share/autostart/*",
219      "/home/*/.kde4/share/autostart/*", "/root/.kde4/share/autostart/*",
220      "/home/*/.local/share/autostart/*", "/root/.local/share/autostart/*",
221      "/home/*/.config/autostart-scripts/*", "/root/.config/autostart-scripts/*",
222      "/etc/xdg/autostart/*", "/usr/share/autostart/*"
223    ) and
224    not process.name in (
225      "yum", "dpkg", "install", "dnf", "teams", "yum-cron", "dnf-automatic", "docker", "dockerd", "rpm", "pacman",
226      "podman", "nautilus", "remmina", "cinnamon-settings.py", "executor", "xfce4-clipman", "jetbrains-toolbox",
227      "ansible-admin"
228    )
229'''
230
231[[rule.threat]]
232framework = "MITRE ATT&CK"
233
234[[rule.threat.technique]]
235id = "T1547"
236name = "Boot or Logon Autostart Execution"
237reference = "https://attack.mitre.org/techniques/T1547/"
238
239[rule.threat.tactic]
240id = "TA0003"
241name = "Persistence"
242reference = "https://attack.mitre.org/tactics/TA0003/"

Triage and analysis

Investigating Persistence via KDE AutoStart Script or Desktop File Modification

K Desktop Environment (KDE) is a popular graphical desktop environment for Linux systems. It supports AutoStart scripts and desktop files that execute automatically upon user logon.

Adversaries may exploit this feature to maintain persistence on a compromised system by creating or modifying these files.

The detection rule 'Persistence via KDE AutoStart Script or Desktop File Modification' is designed to identify such activities by monitoring file events on Linux systems. It specifically targets the creation or modification of files with extensions ".sh" or ".desktop" in various AutoStart directories. By detecting these events, the rule helps security analysts identify potential abuse of KDE AutoStart functionality by malicious actors.

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
    • $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 who uses cron jobs 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.
  • 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