Detection rules › Splunk

User Discovery With Env Vars PowerShell Script Block

Author
Mauricio Velazco, Splunk
Source
upstream

The following analytic detects the use of PowerShell environment variables to identify the current logged user by leveraging PowerShell Script Block Logging (EventCode=4104). This method monitors script blocks containing $env:UserName or [System.Environment]::UserName. Identifying this activity is significant as adversaries and Red Teams may use it for situational awareness and Active Directory discovery on compromised endpoints. If confirmed malicious, this activity could allow attackers to gain insights into user context, aiding in further exploitation and lateral movement within the network.

MITRE ATT&CK coverage

TacticTechniques
DiscoveryT1033 System Owner/User Discovery

Event coverage

ProviderEvent IDTitle
PowerShell4104Creating Scriptblock text (MessageNumber of MessageTotal).

Stages and Predicates

Stage 1: search

search (ScriptBlockText="*$env:UserName*" OR ScriptBlockText="*[System.Environment]::UserName*") EventCode=4104

Stage 2: fillnull

fillnull

Stage 3: stats

stats BY dest, signature, signature_id, user_id, vendor_product, EventID, Guid, Opcode, Name, Path, ProcessID, ScriptBlockId, ScriptBlockText

Stage 4: search

search

Stage 5: search

search

Stage 6: search

search `macro`

Indicators

Each row is a field, operator, and value that the rule matches. The corpus column counts how many other rules in the catalog look for the same combination: high numbers point to widely-used, community-vetted indicators. Blank or 1 shows that the indicator is specific to this rule.

FieldKindValues
EventCodeeq
  • 4104 corpus 108 (splunk 108)
ScriptBlockTexteq
  • "*$env:UserName*"
  • "*[System.Environment]::UserName*"

Neighbors

Broader alternatives (more inclusive than this rule)

These rules match a superset of what this rule catches. They cover the same events plus more. Use them if you want wider coverage and can absorb more false positives.