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", "m365_defender", "sentinel_one_cloud_funnel"]
4maturity = "production"
5updated_date = "2024/10/15"
6min_stack_version = "8.14.0"
7min_stack_comments = "Breaking change at 8.14.0 for the Windows Integration."
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
35[rule]
36author = ["Elastic"]
37description = """
38Identifies the creation or modification of a PowerShell profile. PowerShell profile is a script that is executed when
39PowerShell starts to customize the user environment, which can be abused by attackers to persist in a environment where
40PowerShell is common.
41"""
42from = "now-9m"
43index = ["winlogbeat-*", "logs-endpoint.events.file-*", "logs-windows.sysmon_operational-*", "endgame-*", "logs-m365_defender.event-*", "logs-sentinel_one_cloud_funnel.*"]
44language = "eql"
45license = "Elastic License v2"
46name = "Persistence via PowerShell profile"
47note = """## Triage and analysis
48
49### Investigating Persistence via PowerShell profile
50
51PowerShell 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.
52
53This 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.
54
55> **Note**:
56> 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.
57
58### Possible investigation steps
59
60- 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.
61- 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`.
62- Investigate other alerts associated with the user/host during the past 48 hours.
63- Evaluate whether the user needs to use PowerShell to complete tasks.
64- Check for additional PowerShell and command-line logs that indicate that any suspicious command or function were run.
65- Examine the host for derived artifacts that indicate suspicious activities:
66 - Observe and collect information about the following activities in the alert subject host:
67 - Attempts to contact external domains and addresses.
68 - 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`.
69 - Examine the DNS cache for suspicious or anomalous entries.
70 - $osquery_0
71 - Use the Elastic Defend registry events to examine registry keys accessed, modified, or created by the related processes in the process tree.
72 - Examine the host services for suspicious or anomalous entries.
73 - $osquery_1
74 - $osquery_2
75 - $osquery_3
76
77### False positive analysis
78
79- 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.
80
81### Response and remediation
82
83- Initiate the incident response process based on the outcome of the triage.
84 - If malicious activity is confirmed, perform a broader investigation to identify the scope of the compromise and determine the appropriate remediation steps.
85- Isolate the involved hosts to prevent further post-compromise behavior.
86- If the triage identified malware, search the environment for additional compromised hosts.
87 - Implement temporary network rules, procedures, and segmentation to contain the malware.
88 - Stop suspicious processes.
89 - Immediately block the identified indicators of compromise (IoCs).
90 - Inspect the affected systems for additional malware backdoors like reverse shells, reverse proxies, or droppers that attackers could use to reinfect the system.
91- Remove and block malicious artifacts identified during triage.
92- Reimage the host operating system or restore the compromised files to clean versions.
93- Restrict PowerShell usage outside of IT and engineering business units using GPOs, AppLocker, Intune, or similar software.
94- 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.
95- Run a full antimalware scan. This may reveal additional artifacts left in the system, persistence mechanisms, and malware components.
96- Determine the initial vector abused by the attacker and take action to prevent reinfection through the same vector.
97- 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).
98 - Consider enabling and collecting PowerShell logs such as transcription, module, and script block logging, to improve visibility into PowerShell activities.
99"""
100references = [
101 "https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles",
102 "https://www.welivesecurity.com/2019/05/29/turla-powershell-usage/",
103]
104risk_score = 47
105rule_id = "5cf6397e-eb91-4f31-8951-9f0eaa755a31"
106severity = "medium"
107tags = [
108 "Domain: Endpoint",
109 "OS: Windows",
110 "Use Case: Threat Detection",
111 "Tactic: Persistence",
112 "Tactic: Privilege Escalation",
113 "Data Source: Elastic Endgame",
114 "Data Source: Elastic Defend",
115 "Data Source: Sysmon",
116 "Data Source: Microsoft Defender for Endpoint",
117 "Data Source: SentinelOne",
118]
119timestamp_override = "event.ingested"
120type = "eql"
121
122query = '''
123file where host.os.type == "windows" and event.type != "deletion" and
124 file.path : ("?:\\Users\\*\\Documents\\WindowsPowerShell\\*",
125 "?:\\Users\\*\\Documents\\PowerShell\\*",
126 "?:\\Windows\\System32\\WindowsPowerShell\\*") and
127 file.name : ("profile.ps1", "Microsoft.Powershell_profile.ps1")
128'''
129
130
131[[rule.threat]]
132framework = "MITRE ATT&CK"
133[[rule.threat.technique]]
134id = "T1546"
135name = "Event Triggered Execution"
136reference = "https://attack.mitre.org/techniques/T1546/"
137[[rule.threat.technique.subtechnique]]
138id = "T1546.013"
139name = "PowerShell Profile"
140reference = "https://attack.mitre.org/techniques/T1546/013/"
141
142
143
144[rule.threat.tactic]
145id = "TA0003"
146name = "Persistence"
147reference = "https://attack.mitre.org/tactics/TA0003/"
148[[rule.threat]]
149framework = "MITRE ATT&CK"
150[[rule.threat.technique]]
151id = "T1546"
152name = "Event Triggered Execution"
153reference = "https://attack.mitre.org/techniques/T1546/"
154[[rule.threat.technique.subtechnique]]
155id = "T1546.013"
156name = "PowerShell Profile"
157reference = "https://attack.mitre.org/techniques/T1546/013/"
158
159
160
161[rule.threat.tactic]
162id = "TA0004"
163name = "Privilege Escalation"
164reference = "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 network events to determine domains and addresses contacted by the subject process by filtering by the process's
- 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
- Attempts to contact external domains and addresses.
- Observe and collect information about the following activities in the alert subject host:
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
- Creation or Modification of a new GPO Scheduled Task or Service
- Persistence via TelemetryController Scheduled Task Hijack
- Potential Persistence via Time Provider Modification
- Registry Persistence via AppCert DLL
- Suspicious WerFault Child Process