Overview
Theexec action creates and executes processes on local and remote systems using the Win32_Process.Create WMI method. It supports command execution with optional output retrieval and AMSI evasion capabilities.
Syntax
- Basic Execution
- With Output Retrieval
The
exec action can also be invoked using action=execute or action=create aliases.Parameters
| Parameter | Required | Description |
|---|---|---|
action | Yes | Must be exec, execute, or create |
command | Yes | Command line to execute |
computername | No | Target host(s), comma-separated. Defaults to localhost |
result | No | Set to true to retrieve command output. Default: false |
amsi | No | Set to disable to bypass AMSI (requires result=true) |
username | No | Username for authentication (requires password) |
password | No | Password for authentication (requires username) |
Usage Examples
Basic Process Creation
Command Output Retrieval
Capture and display command output:Example Output
Command Output Mechanism
Whenresult=true is specified, SharpWMI uses a clever technique to retrieve command output:
1
Command Wrapping
The command is wrapped in PowerShell that captures output and XOR-encodes it
2
Environment Variable Storage
Encoded output is stored in a WMI environment variable with a random name
3
Output Retrieval
SharpWMI queries the environment variable and decodes the output
4
Cleanup
Environment variables are automatically deleted after retrieval
Technical Details
Technical Details
The output retrieval mechanism:
- Creates a random XOR key
- Wraps your command in PowerShell:
$o=(COMMAND | Out-String).Trim() - XOR-encodes the output
- Stores in environment variable via
Set-WmiInstance -Class Win32_Environment - Retrieves and decodes the output
- Cleans up the environment variable
AMSI Evasion
Whenamsi=disable is specified (requires result=true), SharpWMI:
1
Registry Modification
Sets
HKCU\Software\Microsoft\Windows Script\Settings\AmsiEnable to 02
Command Execution
Executes your command with AMSI disabled
3
Registry Restoration
Restores original AMSI registry value
Operational Scenarios
Scenario 1: Initial Access and Beacon Deployment
Scenario 2: Credential Harvesting
Scenario 3: Lateral Movement
Scenario 4: Domain Reconnaissance
Scenario 5: Persistence Mechanisms
Remote vs Local Usage
- Local Execution
- Remote Execution
When to use:
- Post-exploitation on compromised system
- Testing commands before remote execution
- Local privilege escalation
- No network traffic
- No authentication required
- Faster execution
Command Line Best Practices
Quote Handling
Use proper escaping for complex commands:
Encoded Commands
Base64 encode complex PowerShell:
Detection Considerations
Host-Based Detection
Host-Based Detection
- Event ID 4688: Process creation events
- Event ID 4648: Logon with explicit credentials
- Sysmon Event ID 1: Process creation
- Parent process:
wmiprvse.exe(WMI Provider Host) - Unusual process trees (wmiprvse.exe → cmd.exe → powershell.exe)
WMI-Specific Detection
WMI-Specific Detection
- Event ID 5857: WMI activity
- Event ID 5860-5861: WMI event consumers
- Sysmon Event ID 19-21: WMI activity
- Registry modifications for AMSI evasion
- Creation/deletion of environment variables
Network Detection
Network Detection
- DCOM traffic on port 135
- Dynamic RPC connections
- Multiple WMI connections from single source
- WMI traffic to sensitive systems (DCs, servers)
Command-Line Detection
Command-Line Detection
- Suspicious command-line arguments
- Base64 encoded commands
- Web requests in command line
- Credential dumping tools (mimikatz, etc.)
- PowerShell download cradles
Best Practices
Operational Security
- Use AMSI evasion when executing PowerShell
- Avoid obvious malicious command lines
- Use encoded commands when possible
- Clean up created processes/files
- Limit use of result=true (creates artifacts)
Execution Strategy
- Test commands locally before remote execution
- Use short-lived processes
- Consider process names that blend in
- Stage payloads via upload action first
- Monitor for command failures
Error Handling
- Check return values (0 = success)
- Verify process started successfully
- Handle timeout scenarios
- Account for AV/EDR blocking
Credential Management
- Use minimal necessary privileges
- Rotate credentials between operations
- Avoid reusing domain admin credentials
- Monitor for lockouts
Return Values
TheCreation of process returned value indicates success or failure:
| Return Value | Meaning |
|---|---|
| 0 | Successful completion |
| 2 | Access denied |
| 3 | Insufficient privilege |
| 8 | Unknown failure |
| 9 | Path not found |
| 21 | Invalid parameter |
Troubleshooting
Return Value: 2 (Access Denied)
Return Value: 2 (Access Denied)
Cause: Insufficient privileges or security restrictionsSolution:
- Verify admin credentials
- Check UAC remote restrictions
- Ensure user is in local Administrators group
- Try with domain admin account
Return Value: 9 (Path Not Found)
Return Value: 9 (Path Not Found)
Cause: Executable or file path doesn’t existSolution:
- Verify path exists on target system
- Use full paths instead of relative
- Upload executable first with upload action
- Check for typos in command
Process Created But No Output
Process Created But No Output
Cause: Process doesn’t generate stdout or requires admin contextSolution:
- Verify command produces output when run locally
- Check if process requires elevated privileges
- Try without result=true first
- Increase timeout if process is slow
AMSI Evasion Not Working
AMSI Evasion Not Working
Cause: Insufficient privileges or EDR blockingSolution:
- Ensure you have admin rights
- Verify registry modification succeeded
- Try alternative AMSI bypass methods
- Check for EDR blocking registry changes