PowerShell Suspicious Discovery Related Windows API Functions
This rule detects the use of discovery-related Windows API functions in PowerShell Scripts. Attackers can use these functions to perform various situational awareness related activities, like enumerating users, shares, sessions, domain trusts, groups, etc.
Elastic rule (View on GitHub)
1[metadata]
2creation_date = "2021/10/13"
3integration = ["windows"]
4maturity = "production"
5min_stack_comments = "New fields added: required_fields, related_integrations, setup"
6min_stack_version = "8.3.0"
7updated_date = "2023/04/20"
8
9[rule]
10author = ["Elastic"]
11description = """
12This rule detects the use of discovery-related Windows API functions in PowerShell Scripts. Attackers can use these
13functions to perform various situational awareness related activities, like enumerating users, shares, sessions, domain
14trusts, groups, etc.
15"""
16false_positives = ["Legitimate PowerShell scripts that make use of these functions."]
17from = "now-9m"
18index = ["winlogbeat-*", "logs-windows.*"]
19language = "kuery"
20license = "Elastic License v2"
21name = "PowerShell Suspicious Discovery Related Windows API Functions"
22note = """## Triage and analysis
23
24### Investigating PowerShell Suspicious Discovery Related Windows API Functions
25
26PowerShell is one of the main tools system administrators use for automation, report routines, and other tasks. This makes it available for use in various environments, and creates an attractive way for attackers to execute code.
27
28Attackers can use PowerShell to interact with the Win32 API to bypass command line based detections, using libraries like PSReflect or Get-ProcAddress Cmdlet.
29
30#### Possible investigation steps
31
32- Examine the script content that triggered the detection; look for suspicious DLL imports, collection or exfiltration capabilities, suspicious functions, encoded or compressed data, and other potentially malicious characteristics.
33- Investigate the script execution chain (parent process tree) for unknown processes. Examine their executable files for prevalence, whether they are located in expected locations, and if they are signed with valid digital signatures.
34- Examine file or network events from the involved PowerShell process for suspicious behavior.
35- Investigate other alerts associated with the user/host during the past 48 hours.
36- Evaluate whether the user needs to use PowerShell to complete tasks.
37- Check for additional PowerShell and command-line logs that indicate that imported functions were run.
38
39### False positive analysis
40
41- Discovery activities themselves are not inherently malicious if occurring in isolation, as long as the script does not contain other capabilities, and there are no other alerts related to the user or host; such alerts can be dismissed. However, analysts should keep in mind that this is not a common way of getting information, making it suspicious.
42
43### Related rules
44
45- PowerShell PSReflect Script - 56f2e9b5-4803-4e44-a0a4-a52dc79d57fe
46
47### Response and remediation
48
49- Initiate the incident response process based on the outcome of the triage.
50- Isolate the involved hosts to prevent further post-compromise behavior.
51- Restrict PowerShell usage outside of IT and engineering business units using GPOs, AppLocker, Intune, or similar software.
52- Determine the initial vector abused by the attacker and take action to prevent reinfection through the same vector.
53- Run a full antimalware scan. This may reveal additional artifacts left in the system, persistence mechanisms, and malware components.
54- 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).
55
56## Setup
57
58The 'PowerShell Script Block Logging' logging policy must be enabled.
59Steps to implement the logging policy with with Advanced Audit Configuration:
Computer Configuration > Administrative Templates > Windows PowerShell > Turn on PowerShell Script Block Logging (Enable)
1
2Steps to implement the logging policy via registry:
reg add "hklm\SOFTWARE\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging" /v EnableScriptBlockLogging /t REG_DWORD /d 1
1"""
2references = [
3 "https://github.com/BC-SECURITY/Empire/blob/9259e5106986847d2bb770c4289c0c0f1adf2344/data/module_source/situational_awareness/network/powerview.ps1#L21413",
4 "https://github.com/atc-project/atc-data/blob/master/docs/Logging_Policies/LP_0109_windows_powershell_script_block_log.md",
5]
6risk_score = 47
7rule_id = "61ac3638-40a3-44b2-855a-985636ca985e"
8severity = "medium"
9tags = ["Elastic", "Host", "Windows", "Threat Detection", "Discovery", "Investigation Guide", "PowerShell"]
10timestamp_override = "event.ingested"
11type = "query"
12
13query = '''
14event.category:process and host.os.type:windows and
15 powershell.file.script_block_text : (
16 NetShareEnum or
17 NetWkstaUserEnum or
18 NetSessionEnum or
19 NetLocalGroupEnum or
20 NetLocalGroupGetMembers or
21 DsGetSiteName or
22 DsEnumerateDomainTrusts or
23 WTSEnumerateSessionsEx or
24 WTSQuerySessionInformation or
25 LsaGetLogonSessionData or
26 QueryServiceObjectSecurity or
27 GetComputerNameEx or
28 NetWkstaGetInfo or
29 GetUserNameEx or
30 NetUserEnum or
31 NetUserGetInfo or
32 NetGroupEnum or
33 NetGroupGetInfo or
34 NetGroupGetUsers or
35 NetWkstaTransportEnum or
36 NetServerGetInfo or
37 LsaEnumerateTrustedDomains or
38 NetScheduleJobEnum or
39 NetUserModalsGet
40 ) and not
41 (user.id:("S-1-5-18" or "S-1-5-19") and
42 file.directory: "C:\\ProgramData\\Microsoft\\Windows Defender Advanced Threat Protection\\DataCollection")
43'''
44
45
46[[rule.threat]]
47framework = "MITRE ATT&CK"
48[[rule.threat.technique]]
49id = "T1069"
50name = "Permission Groups Discovery"
51reference = "https://attack.mitre.org/techniques/T1069/"
52[[rule.threat.technique.subtechnique]]
53id = "T1069.001"
54name = "Local Groups"
55reference = "https://attack.mitre.org/techniques/T1069/001/"
56
57[[rule.threat.technique]]
58id = "T1087"
59name = "Account Discovery"
60reference = "https://attack.mitre.org/techniques/T1087/"
61[[rule.threat.technique.subtechnique]]
62id = "T1087.001"
63name = "Local Account"
64reference = "https://attack.mitre.org/techniques/T1087/001/"
65
66[[rule.threat.technique]]
67id = "T1482"
68name = "Domain Trust Discovery"
69reference = "https://attack.mitre.org/techniques/T1482/"
70
71[[rule.threat.technique]]
72id = "T1135"
73name = "Network Share Discovery"
74reference = "https://attack.mitre.org/techniques/T1135/"
75
76
77[rule.threat.tactic]
78id = "TA0007"
79name = "Discovery"
80reference = "https://attack.mitre.org/tactics/TA0007/"
81[[rule.threat]]
82framework = "MITRE ATT&CK"
83[[rule.threat.technique]]
84id = "T1059"
85name = "Command and Scripting Interpreter"
86reference = "https://attack.mitre.org/techniques/T1059/"
87[[rule.threat.technique.subtechnique]]
88id = "T1059.001"
89name = "PowerShell"
90reference = "https://attack.mitre.org/techniques/T1059/001/"
91
92
93[[rule.threat.technique]]
94id = "T1106"
95name = "Native API"
96reference = "https://attack.mitre.org/techniques/T1106/"
97
98
99[rule.threat.tactic]
100id = "TA0002"
101name = "Execution"
102reference = "https://attack.mitre.org/tactics/TA0002/"
Triage and analysis
Investigating PowerShell Suspicious Discovery Related Windows API Functions
PowerShell is one of the main tools system administrators use for automation, report routines, and other tasks. This makes it available for use in various environments, and creates an attractive way for attackers to execute code.
Attackers can use PowerShell to interact with the Win32 API to bypass command line based detections, using libraries like PSReflect or Get-ProcAddress Cmdlet.
Possible investigation steps
- Examine the script content that triggered the detection; look for suspicious DLL imports, collection or exfiltration capabilities, suspicious functions, encoded or compressed data, and other potentially malicious characteristics.
- Investigate the script execution chain (parent process tree) for unknown processes. Examine their executable files for prevalence, whether they are located in expected locations, and if they are signed with valid digital signatures.
- Examine file or network events from the involved PowerShell process for suspicious behavior.
- 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 imported functions were run.
False positive analysis
- Discovery activities themselves are not inherently malicious if occurring in isolation, as long as the script does not contain other capabilities, and there are no other alerts related to the user or host; such alerts can be dismissed. However, analysts should keep in mind that this is not a common way of getting information, making it suspicious.
Related rules
- PowerShell PSReflect Script - 56f2e9b5-4803-4e44-a0a4-a52dc79d57fe
Response and remediation
- Initiate the incident response process based on the outcome of the triage.
- Isolate the involved hosts to prevent further post-compromise behavior.
- Restrict PowerShell usage outside of IT and engineering business units using GPOs, AppLocker, Intune, or similar software.
- Determine the initial vector abused by the attacker and take action to prevent reinfection through the same vector.
- Run a full antimalware scan. This may reveal additional artifacts left in the system, persistence mechanisms, and malware components.
- 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).
Setup
The 'PowerShell Script Block Logging' logging policy must be enabled. Steps to implement the logging policy with with Advanced Audit Configuration:
1Computer Configuration >
2Administrative Templates >
3Windows PowerShell >
4Turn on PowerShell Script Block Logging (Enable)
Steps to implement the logging policy via registry:
1reg add "hklm\SOFTWARE\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging" /v EnableScriptBlockLogging /t REG_DWORD /d 1