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