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 = "KQL handles backslash and ? characters differently in 8.12+."
 6min_stack_version = "8.12.0"
 7updated_date = "2024/03/12"
 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 = 47
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 = "medium"
  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  )
 35'''
 36
 37[[rule.filters]]
 38[rule.filters.meta]
 39negate = true
 40[rule.filters.query.wildcard."file.path"]
 41"case_insensitive" = true
 42"value" = "?:\\\\ProgramData\\\\Microsoft\\\\Windows Defender Advanced Threat Protection\\\\DataCollection\\\\*"
 43
 44[[rule.threat]]
 45framework = "MITRE ATT&CK"
 46[[rule.threat.technique]]
 47id = "T1069"
 48name = "Permission Groups Discovery"
 49reference = "https://attack.mitre.org/techniques/T1069/"
 50[[rule.threat.technique.subtechnique]]
 51id = "T1069.001"
 52name = "Local Groups"
 53reference = "https://attack.mitre.org/techniques/T1069/001/"
 54
 55[[rule.threat.technique]]
 56id = "T1087"
 57name = "Account Discovery"
 58reference = "https://attack.mitre.org/techniques/T1087/"
 59[[rule.threat.technique.subtechnique]]
 60id = "T1087.001"
 61name = "Local Account"
 62reference = "https://attack.mitre.org/techniques/T1087/001/"
 63
 64[[rule.threat.technique]]
 65id = "T1482"
 66name = "Domain Trust Discovery"
 67reference = "https://attack.mitre.org/techniques/T1482/"
 68
 69[[rule.threat.technique]]
 70id = "T1135"
 71name = "Network Share Discovery"
 72reference = "https://attack.mitre.org/techniques/T1135/"
 73
 74
 75[rule.threat.tactic]
 76id = "TA0007"
 77name = "Discovery"
 78reference = "https://attack.mitre.org/tactics/TA0007/"
 79[[rule.threat]]
 80framework = "MITRE ATT&CK"
 81[[rule.threat.technique]]
 82id = "T1059"
 83name = "Command and Scripting Interpreter"
 84reference = "https://attack.mitre.org/techniques/T1059/"
 85[[rule.threat.technique.subtechnique]]
 86id = "T1059.001"
 87name = "PowerShell"
 88reference = "https://attack.mitre.org/techniques/T1059/001/"
 89
 90
 91[[rule.threat.technique]]
 92id = "T1106"
 93name = "Native API"
 94reference = "https://attack.mitre.org/techniques/T1106/"
 95
 96
 97[rule.threat.tactic]
 98id = "TA0002"
 99name = "Execution"
100reference = "https://attack.mitre.org/tactics/TA0002/"
101
102
103[[rule.threat]]
104framework = "MITRE ATT&CK"
105
106[[rule.threat.technique]]
107id = "T1039"
108name = "Data from Network Shared Drive"
109reference = "https://attack.mitre.org/techniques/T1039/"
110
111[rule.threat.tactic]
112id = "TA0009"
113name = "Collection"
114reference = "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