Skip to content
Threat Feed
medium advisory

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

  1. Initial access is established on a target Windows endpoint.
  2. The attacker executes a PowerShell process (powershell.exe) to interact with the system.
  3. The attacker attempts to determine the security context by querying environment variables.
  4. The attacker runs a command block containing $env:UserName to resolve the current user.
  5. Alternatively, the attacker executes [System.Environment]::UserName to retrieve the same information.
  6. The command output is captured and relayed to the attacker for environmental analysis.
  7. 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

IT Operations 72h

Threat Hunt

Search historical 4104 logs for the identified user discovery patterns

T1033 medium medium confidence hunt now

Data: PowerShell Script Block logs

Mitigations

Implement endpoint monitoring for PowerShell command execution

medium SOC

T1033

Detection coverage 1

Detect User Discovery via PowerShell Environment Variables

medium

Detects the use of PowerShell script blocks that query environment variables to identify the current user, a common reconnaissance technique.

sigma tactics: discovery techniques: T1033 sources: process_creation, windows

Detection queries are available on the platform. Get full rules →