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"
 5updated_date = "2025/03/20"
 6
 7[rule]
 8author = ["Elastic"]
 9description = """
10This rule detects the use of discovery-related Windows API functions in PowerShell Scripts. Attackers can use these
11functions to perform various situational awareness related activities, like enumerating users, shares, sessions, domain
12trusts, groups, etc.
13"""
14false_positives = ["Legitimate PowerShell scripts that make use of these functions."]
15from = "now-9m"
16index = ["winlogbeat-*", "logs-windows.powershell*"]
17language = "kuery"
18license = "Elastic License v2"
19name = "PowerShell Suspicious Discovery Related Windows API Functions"
20note = """## Triage and analysis
21
22### Investigating PowerShell Suspicious Discovery Related Windows API Functions
23
24PowerShell 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.
25
26Attackers can use PowerShell to interact with the Win32 API to bypass command line based detections, using libraries like PSReflect or Get-ProcAddress Cmdlet.
27
28#### Possible investigation steps
29
30- 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.
31- 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.
32- Examine file or network events from the involved PowerShell process for suspicious behavior.
33- Investigate other alerts associated with the user/host during the past 48 hours.
34- Evaluate whether the user needs to use PowerShell to complete tasks.
35- Check for additional PowerShell and command-line logs that indicate that imported functions were run.
36
37### False positive analysis
38
39- 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.
40
41### Related rules
42
43- PowerShell PSReflect Script - 56f2e9b5-4803-4e44-a0a4-a52dc79d57fe
44
45### Response and remediation
46
47- Initiate the incident response process based on the outcome of the triage.
48- Isolate the involved hosts to prevent further post-compromise behavior.
49- Restrict PowerShell usage outside of IT and engineering business units using GPOs, AppLocker, Intune, or similar software.
50- Determine the initial vector abused by the attacker and take action to prevent reinfection through the same vector.
51- Run a full antimalware scan. This may reveal additional artifacts left in the system, persistence mechanisms, and malware components.
52- 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).
53"""
54references = [
55    "https://github.com/BC-SECURITY/Empire/blob/9259e5106986847d2bb770c4289c0c0f1adf2344/data/module_source/situational_awareness/network/powerview.ps1#L21413",
56    "https://github.com/atc-project/atc-data/blob/master/docs/Logging_Policies/LP_0109_windows_powershell_script_block_log.md",
57]
58risk_score = 21
59rule_id = "61ac3638-40a3-44b2-855a-985636ca985e"
60setup = """## Setup
61
62The 'PowerShell Script Block Logging' logging policy must be enabled.
63Steps to implement the logging policy 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"""
  2severity = "low"
  3tags = [
  4    "Domain: Endpoint",
  5    "OS: Windows",
  6    "Use Case: Threat Detection",
  7    "Tactic: Discovery",
  8    "Tactic: Collection",
  9    "Tactic: Execution",
 10    "Resources: Investigation Guide",
 11    "Data Source: PowerShell Logs",
 12]
 13timestamp_override = "event.ingested"
 14type = "query"
 15
 16query = '''
 17event.category:process and host.os.type:windows and
 18  powershell.file.script_block_text : (
 19    NetShareEnum or
 20    NetWkstaUserEnum or
 21    NetSessionEnum or
 22    NetLocalGroupEnum or
 23    NetLocalGroupGetMembers or
 24    DsGetSiteName or
 25    DsEnumerateDomainTrusts or
 26    WTSEnumerateSessionsEx or
 27    WTSQuerySessionInformation or
 28    LsaGetLogonSessionData or
 29    QueryServiceObjectSecurity or
 30    GetComputerNameEx or
 31    NetWkstaGetInfo or
 32    GetUserNameEx or
 33    NetUserEnum or
 34    NetUserGetInfo or
 35    NetGroupEnum or
 36    NetGroupGetInfo or
 37    NetGroupGetUsers or
 38    NetWkstaTransportEnum or
 39    NetServerGetInfo or
 40    LsaEnumerateTrustedDomains  or
 41    NetScheduleJobEnum or
 42    NetUserModalsGet
 43  ) and
 44  not powershell.file.script_block_text : (
 45    ("DsGetSiteName" and ("DiscoverWindowsComputerProperties.ps1" and "param($SourceType, $SourceId, $ManagedEntityId, $ComputerIdentity)")) or
 46    ("# Copyright: (c) 2018, Ansible Project" and "#Requires -Module Ansible.ModuleUtils.AddType" and "#AnsibleRequires -CSharpUtil Ansible.Basic") or
 47    ("Ansible.Windows.Setup" and "Ansible.Windows.Setup" and "NativeMethods.NetWkstaGetInfo(null, 100, out netBuffer);")
 48  )
 49'''
 50
 51
 52[[rule.filters]]
 53
 54[rule.filters.meta]
 55negate = true
 56[rule.filters.query.wildcard."file.path"]
 57case_insensitive = true
 58value = "?:\\\\ProgramData\\\\Microsoft\\\\Windows Defender Advanced Threat Protection\\\\DataCollection\\\\*"
 59[[rule.threat]]
 60framework = "MITRE ATT&CK"
 61[[rule.threat.technique]]
 62id = "T1069"
 63name = "Permission Groups Discovery"
 64reference = "https://attack.mitre.org/techniques/T1069/"
 65[[rule.threat.technique.subtechnique]]
 66id = "T1069.001"
 67name = "Local Groups"
 68reference = "https://attack.mitre.org/techniques/T1069/001/"
 69
 70
 71[[rule.threat.technique]]
 72id = "T1087"
 73name = "Account Discovery"
 74reference = "https://attack.mitre.org/techniques/T1087/"
 75[[rule.threat.technique.subtechnique]]
 76id = "T1087.001"
 77name = "Local Account"
 78reference = "https://attack.mitre.org/techniques/T1087/001/"
 79
 80
 81[[rule.threat.technique]]
 82id = "T1135"
 83name = "Network Share Discovery"
 84reference = "https://attack.mitre.org/techniques/T1135/"
 85
 86[[rule.threat.technique]]
 87id = "T1482"
 88name = "Domain Trust Discovery"
 89reference = "https://attack.mitre.org/techniques/T1482/"
 90
 91
 92[rule.threat.tactic]
 93id = "TA0007"
 94name = "Discovery"
 95reference = "https://attack.mitre.org/tactics/TA0007/"
 96[[rule.threat]]
 97framework = "MITRE ATT&CK"
 98[[rule.threat.technique]]
 99id = "T1059"
100name = "Command and Scripting Interpreter"
101reference = "https://attack.mitre.org/techniques/T1059/"
102[[rule.threat.technique.subtechnique]]
103id = "T1059.001"
104name = "PowerShell"
105reference = "https://attack.mitre.org/techniques/T1059/001/"
106
107
108[[rule.threat.technique]]
109id = "T1106"
110name = "Native API"
111reference = "https://attack.mitre.org/techniques/T1106/"
112
113
114[rule.threat.tactic]
115id = "TA0002"
116name = "Execution"
117reference = "https://attack.mitre.org/tactics/TA0002/"
118[[rule.threat]]
119framework = "MITRE ATT&CK"
120[[rule.threat.technique]]
121id = "T1039"
122name = "Data from Network Shared Drive"
123reference = "https://attack.mitre.org/techniques/T1039/"
124
125
126[rule.threat.tactic]
127id = "TA0009"
128name = "Collection"
129reference = "https://attack.mitre.org/tactics/TA0009/"

Triage and analysis

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.
  • 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).

References

Related rules

to-top