Detection: PowerShell Environment Variable Execution

Description

The following analytic detects the execution of PowerShell scripts that combine environment variable access ($env: or [Environment]::SetEnvironmentVariable) with Invoke-Expression or its alias iex to dynamically construct and run code at runtime. This technique is commonly used by adversaries to stage and execute payloads by embedding commands or encoded content inside environment variables, then evaluating them on the fly — effectively hiding the true execution intent from static inspection. Detection is based on PowerShell Script Block Logging (Event ID 4104), which captures the de-obfuscated script block before it executes. Triggering this analytic indicates a potential attempt to execute environment-variable-stored code, a behavior observed in malware loaders and stagers, including those associated with the VIP Keylogger campaign.

 1`powershell`
 2EventCode=4104
 3ScriptBlockText="*$env:*"
 4ScriptBlockText IN (
 5    "*.Invoke()*",
 6    "*[scriptblock]::Create*",
 7    "*iex *",
 8    "*Invoke-Expression*"
 9)
10ScriptBlockText="*[Environment]::SetEnvironmentVariable*"
11
12| regex ScriptBlockText="(?i)((invoke-expression
13|iex\s+
14|\biex\b).*\$env:
15|\[scriptblock\]::create\s*\(\s*\$env:[^)]+\)\s*(?:\.\s*invoke\s*\(\s*\))?)"
16
17| fillnull
18
19| stats count min(_time) as firstTime max(_time) as lastTime
20by dest signature signature_id user_id vendor_product EventID Guid Opcode Name Path ProcessID ScriptBlockId ScriptBlockText
21
22| `security_content_ctime(firstTime)`
23
24| `security_content_ctime(lastTime)`
25
26| `powershell_environment_variable_execution_filter`

Data Source

Name Platform Sourcetype Source
Powershell Script Block Logging 4104 Windows icon Windows 'XmlWinEventLog' 'XmlWinEventLog:Microsoft-Windows-PowerShell/Operational'

Macros Used

Name Value
powershell (source=WinEventLog:Microsoft-Windows-PowerShell/Operational OR source="XmlWinEventLog:Microsoft-Windows-PowerShell/Operational" OR source=WinEventLog:PowerShellCore/Operational OR source="XmlWinEventLog:PowerShellCore/Operational")
powershell_environment_variable_execution_filter search *
powershell_environment_variable_execution_filter is an empty macro by default. It allows the user to filter out any results (false positives) without editing the SPL.

Annotations

Default Configuration

This detection is configured by default in Splunk Enterprise Security to run with the following settings:

Setting Value
Disabled true
Cron Schedule 0 * * * *
Earliest Time -70m@m
Latest Time -10m@m
Schedule Window auto
Creates Risk Event True
This configuration file applies to all detections of type anomaly. These detections will use Risk Based Alerting.

Implementation

The following analytic requires PowerShell operational logs to be imported. Modify the powershell macro as needed to match the sourcetype or add index. This analytic is specific to 4104, or PowerShell Script Block Logging.

Known False Positives

No false positives have been identified at this time.

Associated Analytic Story

Risk Based Analytics (RBA)

Risk Message:

A suspicious powershell script execute environment variable in [$ScriptBlockText$] on host [$dest$]

Risk Object Risk Object Type Risk Score Threat Objects
dest system 20 No Threat Objects

References

Detection Testing

Test Type Status Dataset Source Sourcetype
Validation Passing N/A N/A N/A
Unit Passing Dataset XmlWinEventLog:Microsoft-Windows-PowerShell/Operational XmlWinEventLog
Integration ✅ Passing Dataset XmlWinEventLog:Microsoft-Windows-PowerShell/Operational XmlWinEventLog

Replay any dataset to Splunk Enterprise by using our replay.py tool or the UI. Alternatively you can replay a dataset into a Splunk Attack Range


Source: GitHub | Version: 1