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/07/18"
  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]
155risk_score = 47
156rule_id = "e3e904b3-0a8e-4e68-86a8-977a163e21d3"
157setup = """## Setup
158
159This rule requires data coming in from one of the following integrations:
160- Elastic Defend
161- Auditbeat
162
163### Elastic Defend Integration Setup
164Elastic 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.
165
166#### Prerequisite Requirements:
167- Fleet is required for Elastic Defend.
168- To configure Fleet Server refer to the [documentation](https://www.elastic.co/guide/en/fleet/current/fleet-server.html).
169
170#### The following steps should be executed in order to add the Elastic Defend integration on a Linux System:
171- Go to the Kibana home page and click "Add integrations".
172- In the query bar, search for "Elastic Defend" and select the integration to see more details about it.
173- Click "Add Elastic Defend".
174- Configure the integration name and optionally add a description.
175- Select the type of environment you want to protect, either "Traditional Endpoints" or "Cloud Workloads".
176- 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).
177- We suggest selecting "Complete EDR (Endpoint Detection and Response)" as a configuration setting, that provides "All events; all preventions"
178- 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.
179For more details on Elastic Agent configuration settings, refer to the [helper guide](https://www.elastic.co/guide/en/fleet/8.10/agent-policy.html).
180- Click "Save and Continue".
181- 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.
182For more details on Elastic Defend refer to the [helper guide](https://www.elastic.co/guide/en/security/current/install-endpoint.html).
183
184### Auditbeat Setup
185Auditbeat 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.
186
187#### The following steps should be executed in order to add the Auditbeat on a Linux System:
188- Elastic provides repositories available for APT and YUM-based distributions. Note that we provide binary packages, but no source packages.
189- 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).
190- 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).
191- 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).
192- 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).
193
194#### Custom Ingest Pipeline
195For 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).
196"""
197severity = "medium"
198tags = [
199    "Domain: Endpoint",
200    "OS: Linux",
201    "Use Case: Threat Detection",
202    "Tactic: Persistence",
203    "Data Source: Elastic Endgame",
204    "Data Source: Elastic Defend",
205]
206timestamp_override = "event.ingested"
207type = "eql"
208
209query = '''
210file where host.os.type == "linux" and event.type != "deletion" and
211  file.extension in ("sh", "desktop") and
212  file.path :
213    (
214      "/home/*/.config/autostart/*", "/root/.config/autostart/*",
215      "/home/*/.kde/Autostart/*", "/root/.kde/Autostart/*",
216      "/home/*/.kde4/Autostart/*", "/root/.kde4/Autostart/*",
217      "/home/*/.kde/share/autostart/*", "/root/.kde/share/autostart/*",
218      "/home/*/.kde4/share/autostart/*", "/root/.kde4/share/autostart/*",
219      "/home/*/.local/share/autostart/*", "/root/.local/share/autostart/*",
220      "/home/*/.config/autostart-scripts/*", "/root/.config/autostart-scripts/*",
221      "/etc/xdg/autostart/*", "/usr/share/autostart/*"
222    ) and
223    not process.name in ("yum", "dpkg", "install", "dnf", "teams", "yum-cron", "dnf-automatic", "docker", "dockerd", 
224    "rpm", "pacman", "podman", "nautilus", "remmina", "cinnamon-settings.py", "executor")
225'''
226
227
228[[rule.threat]]
229framework = "MITRE ATT&CK"
230[[rule.threat.technique]]
231id = "T1547"
232name = "Boot or Logon Autostart Execution"
233reference = "https://attack.mitre.org/techniques/T1547/"
234
235
236[rule.threat.tactic]
237id = "TA0003"
238name = "Persistence"
239reference = "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