Skip to main content

Overview

The setenv action creates or modifies environment variables on local and remote systems using the Win32_Environment WMI class. It can be used for configuration, data storage, or as a covert communication channel.

Syntax

Parameters

Usage Examples

Operational Use Cases

Scenario 1: Data Exfiltration Channel

Scenario 2: Configuration Management

Scenario 3: Staging Data

Scenario 4: Persistence Marker

Variable Scope

Environment variables are set in the user context:
  • System scope: Requires SYSTEM privileges
  • User scope: Current user or specified user
  • Volatile: Temporary (lost on reboot)
SharpWMI sets variables in the user scope by default. The variable persists across sessions but not reboots unless explicitly set as system-level.

Remote vs Local Usage

Use for:
  • Local configuration
  • Testing
  • Post-exploitation setup

Detection Considerations

  • Win32_Environment class modifications
  • Event ID 5857: WMI activity
  • Sysmon Event ID 19-21: WMI operations
  • Environment variable creation/modification
  • Registry modifications in environment key
  • HKCU\Environment changes
  • HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment changes
  • Event ID 4657: Registry value modification
  • Unusual variable names
  • Large variable values
  • Base64-encoded content
  • Variables created by non-standard processes

Best Practices

Variable Naming

  • Use legitimate-sounding names
  • Blend with existing variables
  • Avoid obvious malicious names
  • Examples: BUILD_CONFIG, SYSTEM_ID, CACHE_PATH

Operational Security

  • Clean up variables after use
  • Don’t store sensitive data in plaintext
  • Use encoding/encryption
  • Monitor for defensive responses

Data Management

  • Keep values reasonably sized
  • Don’t exceed value length limits
  • Consider compression for large data
  • Use delenv to clean up

Persistence

  • Variables persist across logons
  • Lost on system reboot
  • Not suitable for long-term storage
  • Consider scheduled task for persistence

Value Length Limits

Environment variable values have length limitations. Very large values may fail to set.
Recommendations:
  • Keep values under 32KB
  • Use compression for large data
  • Split large data across multiple variables
  • Test with small values first

Troubleshooting

Cause: Insufficient privilegesSolution:
  • Use username and password parameters
  • Verify admin rights on target
  • Check UAC remote restrictions
Cause: WMI operation failedSolution:
  • Check value length
  • Verify WMI service is running
  • Try with shorter value
  • Check for special characters
Cause: Scope or context issueSolution:
  • Variables are user-specific
  • Check with getenv action
  • May need to refresh environment
  • Verify correct username context

getenv

Retrieve environment variables

delenv

Delete environment variables

exec

Execute commands that use variables

query

Custom WMI queries

Examples with Execution