Detection of PowerShell-Based User Discovery Techniques
Adversaries utilize PowerShell commands to query environment variables as part of local system reconnaissance to identify the current logged-in user.
Adversaries and red teams frequently leverage built-in system tools to perform reconnaissance on compromised endpoints. A common technique for situational awareness involves querying environment variables via PowerShell to identify the identity of the current user session. This specific activity, centered around the use of $env:UserName or [System.Environment]::UserName within PowerShell script blocks, allows attackers to confirm their current context on a host. This information gathering is a precursor to more advanced objectives, including privilege escalation and lateral movement within an Active Directory environment. Detecting this behavior at the script block level provides security operations teams visibility into the discovery phase of an attack.
Attack Chain
- Initial access is established on a target Windows endpoint.
- The attacker executes a PowerShell process (powershell.exe) to interact with the system.
- The attacker attempts to determine the security context by querying environment variables.
- The attacker runs a command block containing
$env:UserNameto resolve the current user. - Alternatively, the attacker executes
[System.Environment]::UserNameto retrieve the same information. - The command output is captured and relayed to the attacker for environmental analysis.
- Based on the discovered user, the attacker evaluates the feasibility of lateral movement or further exploitation.
Impact
Successful user discovery provides attackers with necessary situational awareness to refine their post-exploitation tactics. This reconnaissance phase supports targeting of high-privilege users or mapping of organizational infrastructure, increasing the risk of unauthorized data access and sustained network persistence.
Recommendation
Detection engineering teams should focus on implementing PowerShell Script Block Logging (EventCode 4104) to capture the execution of suspicious queries.
- Deploy the provided Sigma rule to detect the use of environment variable discovery patterns in PowerShell script blocks.
- Enable and validate PowerShell Script Block Logging across all critical endpoints to ensure visibility into command execution.
- Tune detections to minimize alerts generated by authorized administrative or management scripts.
Immediate actions
Enable PowerShell Script Block Logging (Event 4104) across all endpoints
Threat Hunt
Search historical 4104 logs for the identified user discovery patterns
Data: PowerShell Script Block logs
Mitigations
Implement endpoint monitoring for PowerShell command execution
T1033
Detection coverage 1
Detect User Discovery via PowerShell Environment Variables
mediumDetects the use of PowerShell script blocks that query environment variables to identify the current user, a common reconnaissance technique.
Detection queries are available on the platform. Get full rules →