Persistence via PowerShell profile

Identifies the creation or modification of a PowerShell profile. PowerShell profile is a script that is executed when PowerShell starts to customize the user environment, which can be abused by attackers to persist in a environment where PowerShell is common.

Elastic rule (View on GitHub)

  1[metadata]
  2creation_date = "2022/10/13"
  3integration = ["endpoint", "windows"]
  4maturity = "production"
  5min_stack_comments = "New fields added: required_fields, related_integrations, setup"
  6min_stack_version = "8.3.0"
  7updated_date = "2024/03/28"
  8
  9[transform]
 10[[transform.osquery]]
 11label = "Osquery - Retrieve DNS Cache"
 12query = "SELECT * FROM dns_cache"
 13
 14[[transform.osquery]]
 15label = "Osquery - Retrieve All Services"
 16query = "SELECT description, display_name, name, path, pid, service_type, start_type, status, user_account FROM services"
 17
 18[[transform.osquery]]
 19label = "Osquery - Retrieve Services Running on User Accounts"
 20query = """
 21SELECT description, display_name, name, path, pid, service_type, start_type, status, user_account FROM services WHERE
 22NOT (user_account LIKE '%LocalSystem' OR user_account LIKE '%LocalService' OR user_account LIKE '%NetworkService' OR
 23user_account == null)
 24"""
 25
 26[[transform.osquery]]
 27label = "Osquery - Retrieve Service Unsigned Executables with Virustotal Link"
 28query = """
 29SELECT concat('https://www.virustotal.com/gui/file/', sha1) AS VtLink, name, description, start_type, status, pid,
 30services.path FROM services JOIN authenticode ON services.path = authenticode.path OR services.module_path =
 31authenticode.path JOIN hash ON services.path = hash.path WHERE authenticode.result != 'trusted'
 32"""
 33
 34[rule]
 35author = ["Elastic"]
 36description = """
 37Identifies the creation or modification of a PowerShell profile. PowerShell profile is a script that is executed when
 38PowerShell starts to customize the user environment, which can be abused by attackers to persist in a environment where
 39PowerShell is common.
 40"""
 41from = "now-9m"
 42index = ["winlogbeat-*", "logs-endpoint.events.file-*", "logs-windows.sysmon_operational-*", "endgame-*"]
 43language = "eql"
 44license = "Elastic License v2"
 45name = "Persistence via PowerShell profile"
 46note = """## Triage and analysis
 47
 48### Investigating Persistence via PowerShell profile
 49
 50PowerShell profiles are scripts executed when PowerShell starts, customizing the user environment. They are commonly used in Windows environments for legitimate purposes, such as setting variables or loading modules. However, adversaries can abuse PowerShell profiles to establish persistence by inserting malicious code that executes each time PowerShell is launched.
 51
 52This rule identifies the creation or modification of a PowerShell profile. It does this by monitoring file events on Windows systems, specifically targeting profile-related file paths and names, such as `profile.ps1` and `Microsoft.Powershell_profile.ps1`. By detecting these activities, security analysts can investigate potential abuse of PowerShell profiles for malicious persistence.
 53
 54> **Note**:
 55> 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.
 56
 57### Possible investigation steps
 58
 59- Retrive and inspect the PowerShell profile content; look for suspicious DLL imports, collection or persistence capabilities, suspicious functions, encoded or compressed data, suspicious commands, and other potentially malicious characteristics.
 60- Identify the process responsible for the PowerShell profile creation/modification. Use the Elastic Defend events to examine all the activity of the subject process by filtering by the process's `process.entity_id`.
 61- Investigate other alerts associated with the user/host during the past 48 hours.
 62- Evaluate whether the user needs to use PowerShell to complete tasks.
 63- Check for additional PowerShell and command-line logs that indicate that any suspicious command or function were run.
 64- Examine the host for derived artifacts that indicate suspicious activities:
 65  - Observe and collect information about the following activities in the alert subject host:
 66    - Attempts to contact external domains and addresses.
 67      - Use the Elastic Defend network events to determine domains and addresses contacted by the subject process by filtering by the process's `process.entity_id`.
 68      - Examine the DNS cache for suspicious or anomalous entries.
 69        - $osquery_0
 70    - Use the Elastic Defend registry events to examine registry keys accessed, modified, or created by the related processes in the process tree.
 71    - Examine the host services for suspicious or anomalous entries.
 72      - $osquery_1
 73      - $osquery_2
 74      - $osquery_3
 75
 76### False positive analysis
 77
 78- This is a dual-use mechanism, meaning its usage is not inherently malicious. Analysts can dismiss the alert if the script doesn't contain malicious functions or potential for abuse, no other suspicious activity was identified, and the user has business justifications to use PowerShell.
 79
 80### Response and remediation
 81
 82- Initiate the incident response process based on the outcome of the triage.
 83  - If malicious activity is confirmed, perform a broader investigation to identify the scope of the compromise and determine the appropriate remediation steps.
 84- Isolate the involved hosts to prevent further post-compromise behavior.
 85- If the triage identified malware, search the environment for additional compromised hosts.
 86  - Implement temporary network rules, procedures, and segmentation to contain the malware.
 87  - Stop suspicious processes.
 88  - Immediately block the identified indicators of compromise (IoCs).
 89  - Inspect the affected systems for additional malware backdoors like reverse shells, reverse proxies, or droppers that attackers could use to reinfect the system.
 90- Remove and block malicious artifacts identified during triage.
 91- Reimage the host operating system or restore the compromised files to clean versions.
 92- Restrict PowerShell usage outside of IT and engineering business units using GPOs, AppLocker, Intune, or similar software.
 93- 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.
 94- Run a full antimalware scan. This may reveal additional artifacts left in the system, persistence mechanisms, and malware components.
 95- Determine the initial vector abused by the attacker and take action to prevent reinfection through the same vector.
 96- Using the incident response data, update logging and audit policies to improve the mean time to detect (MTTD) and the mean time to respond (MTTR).
 97  - Consider enabling and collecting PowerShell logs such as transcription, module, and script block logging, to improve visibility into PowerShell activities.
 98"""
 99references = [
100    "https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles",
101    "https://www.welivesecurity.com/2019/05/29/turla-powershell-usage/",
102]
103risk_score = 47
104rule_id = "5cf6397e-eb91-4f31-8951-9f0eaa755a31"
105severity = "medium"
106tags = ["Domain: Endpoint", "OS: Windows", "Use Case: Threat Detection", "Tactic: Persistence", "Tactic: Privilege Escalation", "Data Source: Elastic Endgame", "Data Source: Elastic Defend", "Data Source: Sysmon"]
107timestamp_override = "event.ingested"
108type = "eql"
109
110query = '''
111file where host.os.type == "windows" and event.type != "deletion" and
112  file.path : ("?:\\Users\\*\\Documents\\WindowsPowerShell\\*",
113               "?:\\Users\\*\\Documents\\PowerShell\\*",
114               "?:\\Windows\\System32\\WindowsPowerShell\\*") and
115  file.name : ("profile.ps1", "Microsoft.Powershell_profile.ps1")
116'''
117
118
119[[rule.threat]]
120framework = "MITRE ATT&CK"
121[[rule.threat.technique]]
122id = "T1546"
123name = "Event Triggered Execution"
124reference = "https://attack.mitre.org/techniques/T1546/"
125[[rule.threat.technique.subtechnique]]
126id = "T1546.013"
127name = "PowerShell Profile"
128reference = "https://attack.mitre.org/techniques/T1546/013/"
129
130
131
132[rule.threat.tactic]
133id = "TA0003"
134name = "Persistence"
135reference = "https://attack.mitre.org/tactics/TA0003/"
136
137[[rule.threat]]
138framework = "MITRE ATT&CK"
139[[rule.threat.technique]]
140id = "T1546"
141name = "Event Triggered Execution"
142reference = "https://attack.mitre.org/techniques/T1546/"
143[[rule.threat.technique.subtechnique]]
144id = "T1546.013"
145name = "PowerShell Profile"
146reference = "https://attack.mitre.org/techniques/T1546/013/"
147
148
149
150[rule.threat.tactic]
151id = "TA0004"
152name = "Privilege Escalation"
153reference = "https://attack.mitre.org/tactics/TA0004/"

Triage and analysis

Investigating Persistence via PowerShell profile

PowerShell profiles are scripts executed when PowerShell starts, customizing the user environment. They are commonly used in Windows environments for legitimate purposes, such as setting variables or loading modules. However, adversaries can abuse PowerShell profiles to establish persistence by inserting malicious code that executes each time PowerShell is launched.

This rule identifies the creation or modification of a PowerShell profile. It does this by monitoring file events on Windows systems, specifically targeting profile-related file paths and names, such as profile.ps1 and Microsoft.Powershell_profile.ps1. By detecting these activities, security analysts can investigate potential abuse of PowerShell profiles for malicious 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.

Possible investigation steps

  • Retrive and inspect the PowerShell profile content; look for suspicious DLL imports, collection or persistence capabilities, suspicious functions, encoded or compressed data, suspicious commands, and other potentially malicious characteristics.
  • Identify the process responsible for the PowerShell profile creation/modification. Use the Elastic Defend events to examine all the activity of the subject process by filtering by the process's process.entity_id.
  • Investigate other alerts associated with the user/host during the past 48 hours.
  • Evaluate whether the user needs to use PowerShell to complete tasks.
  • Check for additional PowerShell and command-line logs that indicate that any suspicious command or function were run.
  • Examine the host for derived artifacts that indicate suspicious activities:
    • Observe and collect information about the following activities in the alert subject host:
      • Attempts to contact external domains and addresses.
        • Use the Elastic Defend network events to determine domains and addresses contacted by the subject process by filtering by the process's process.entity_id.
        • Examine the DNS cache for suspicious or anomalous entries.
          • $osquery_0
      • Use the Elastic Defend registry events to examine registry keys accessed, modified, or created by the related processes in the process tree.
      • Examine the host services for suspicious or anomalous entries.
        • $osquery_1
        • $osquery_2
        • $osquery_3

False positive analysis

  • This is a dual-use mechanism, meaning its usage is not inherently malicious. Analysts can dismiss the alert if the script doesn't contain malicious functions or potential for abuse, no other suspicious activity was identified, and the user has business justifications to use PowerShell.

Response and remediation

  • Initiate the incident response process based on the outcome of the triage.
    • If malicious activity is confirmed, perform a broader investigation to identify the scope of the compromise and determine the appropriate remediation steps.
  • Isolate the involved hosts 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.
  • Remove and block malicious artifacts identified during triage.
  • Reimage the host operating system or restore the compromised files to clean versions.
  • Restrict PowerShell usage outside of IT and engineering business units using GPOs, AppLocker, Intune, or similar software.
  • 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.
  • Using the incident response data, update logging and audit policies to improve the mean time to detect (MTTD) and the mean time to respond (MTTR).
    • Consider enabling and collecting PowerShell logs such as transcription, module, and script block logging, to improve visibility into PowerShell activities.

References

Related rules

to-top