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 = "Breaking change at 8.14.0 for the Windows Integration."
 6min_stack_version = "8.14.0"
 7updated_date = "2024/10/28"
 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.powershell*"]
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"""
56references = [
57    "https://github.com/BC-SECURITY/Empire/blob/9259e5106986847d2bb770c4289c0c0f1adf2344/data/module_source/situational_awareness/network/powerview.ps1#L21413",
58    "https://github.com/atc-project/atc-data/blob/master/docs/Logging_Policies/LP_0109_windows_powershell_script_block_log.md",
59]
60risk_score = 21
61rule_id = "61ac3638-40a3-44b2-855a-985636ca985e"
62setup = """## Setup
63
64The 'PowerShell Script Block Logging' logging policy must be enabled.
65Steps 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 = ["Domain: Endpoint", "OS: Windows", "Use Case: Threat Detection", "Tactic: Discovery", "Tactic: Collection", "Tactic: Execution", "Resources: Investigation Guide", "Data Source: PowerShell Logs"]
  4timestamp_override = "event.ingested"
  5type = "query"
  6
  7query = '''
  8event.category:process and host.os.type:windows and
  9  powershell.file.script_block_text : (
 10    NetShareEnum or
 11    NetWkstaUserEnum or
 12    NetSessionEnum or
 13    NetLocalGroupEnum or
 14    NetLocalGroupGetMembers or
 15    DsGetSiteName or
 16    DsEnumerateDomainTrusts or
 17    WTSEnumerateSessionsEx or
 18    WTSQuerySessionInformation or
 19    LsaGetLogonSessionData or
 20    QueryServiceObjectSecurity or
 21    GetComputerNameEx or
 22    NetWkstaGetInfo or
 23    GetUserNameEx or
 24    NetUserEnum or
 25    NetUserGetInfo or
 26    NetGroupEnum or
 27    NetGroupGetInfo or
 28    NetGroupGetUsers or
 29    NetWkstaTransportEnum or
 30    NetServerGetInfo or
 31    LsaEnumerateTrustedDomains  or
 32    NetScheduleJobEnum or
 33    NetUserModalsGet
 34  ) and
 35  not powershell.file.script_block_text : (
 36    ("DsGetSiteName" and ("DiscoverWindowsComputerProperties.ps1" and "param($SourceType, $SourceId, $ManagedEntityId, $ComputerIdentity)")) or
 37    ("# Copyright: (c) 2018, Ansible Project" and "#Requires -Module Ansible.ModuleUtils.AddType" and "#AnsibleRequires -CSharpUtil Ansible.Basic") or
 38    ("Ansible.Windows.Setup" and "Ansible.Windows.Setup" and "NativeMethods.NetWkstaGetInfo(null, 100, out netBuffer);")
 39  )
 40'''
 41
 42[[rule.filters]]
 43[rule.filters.meta]
 44negate = true
 45[rule.filters.query.wildcard."file.path"]
 46"case_insensitive" = true
 47"value" = "?:\\\\ProgramData\\\\Microsoft\\\\Windows Defender Advanced Threat Protection\\\\DataCollection\\\\*"
 48
 49[[rule.threat]]
 50framework = "MITRE ATT&CK"
 51[[rule.threat.technique]]
 52id = "T1069"
 53name = "Permission Groups Discovery"
 54reference = "https://attack.mitre.org/techniques/T1069/"
 55[[rule.threat.technique.subtechnique]]
 56id = "T1069.001"
 57name = "Local Groups"
 58reference = "https://attack.mitre.org/techniques/T1069/001/"
 59
 60[[rule.threat.technique]]
 61id = "T1087"
 62name = "Account Discovery"
 63reference = "https://attack.mitre.org/techniques/T1087/"
 64[[rule.threat.technique.subtechnique]]
 65id = "T1087.001"
 66name = "Local Account"
 67reference = "https://attack.mitre.org/techniques/T1087/001/"
 68
 69[[rule.threat.technique]]
 70id = "T1482"
 71name = "Domain Trust Discovery"
 72reference = "https://attack.mitre.org/techniques/T1482/"
 73
 74[[rule.threat.technique]]
 75id = "T1135"
 76name = "Network Share Discovery"
 77reference = "https://attack.mitre.org/techniques/T1135/"
 78
 79
 80[rule.threat.tactic]
 81id = "TA0007"
 82name = "Discovery"
 83reference = "https://attack.mitre.org/tactics/TA0007/"
 84[[rule.threat]]
 85framework = "MITRE ATT&CK"
 86[[rule.threat.technique]]
 87id = "T1059"
 88name = "Command and Scripting Interpreter"
 89reference = "https://attack.mitre.org/techniques/T1059/"
 90[[rule.threat.technique.subtechnique]]
 91id = "T1059.001"
 92name = "PowerShell"
 93reference = "https://attack.mitre.org/techniques/T1059/001/"
 94
 95
 96[[rule.threat.technique]]
 97id = "T1106"
 98name = "Native API"
 99reference = "https://attack.mitre.org/techniques/T1106/"
100
101
102[rule.threat.tactic]
103id = "TA0002"
104name = "Execution"
105reference = "https://attack.mitre.org/tactics/TA0002/"
106
107
108[[rule.threat]]
109framework = "MITRE ATT&CK"
110
111[[rule.threat.technique]]
112id = "T1039"
113name = "Data from Network Shared Drive"
114reference = "https://attack.mitre.org/techniques/T1039/"
115
116[rule.threat.tactic]
117id = "TA0009"
118name = "Collection"
119reference = "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