Overview
Thesetenv 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
| Parameter | Required | Description |
|---|---|---|
action | Yes | Must be setenv |
name | Yes | Environment variable name |
value | Yes | Environment variable value |
computername | No | Target host(s), comma-separated. Defaults to localhost |
username | No | Username for authentication |
password | No | Password for authentication |
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
- Local Set
- Remote Set
- Local configuration
- Testing
- Post-exploitation setup
Detection Considerations
WMI Detection
WMI Detection
- Win32_Environment class modifications
- Event ID 5857: WMI activity
- Sysmon Event ID 19-21: WMI operations
- Environment variable creation/modification
Registry Detection
Registry Detection
- Registry modifications in environment key
HKCU\EnvironmentchangesHKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environmentchanges- Event ID 4657: Registry value modification
Suspicious Variables
Suspicious Variables
- 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
Recommendations:- Keep values under 32KB
- Use compression for large data
- Split large data across multiple variables
- Test with small values first
Troubleshooting
Access Denied
Access Denied
Cause: Insufficient privilegesSolution:
- Use
usernameandpasswordparameters - Verify admin rights on target
- Check UAC remote restrictions
Value Not Set
Value Not Set
Cause: WMI operation failedSolution:
- Check value length
- Verify WMI service is running
- Try with shorter value
- Check for special characters
Variable Not Visible
Variable Not Visible
Cause: Scope or context issueSolution:
- Variables are user-specific
- Check with getenv action
- May need to refresh environment
- Verify correct username context
Related Actions
getenv
Retrieve environment variables
delenv
Delete environment variables
exec
Execute commands that use variables
query
Custom WMI queries
Examples with Execution
Set and Use Variable
Set and Use Variable
Store Command Output
Store Command Output