Chapter 62

WMI Persistence

The persistence triad in detail: EventFilter, EventConsumer types, FilterToConsumerBinding, installation telemetry, hunting queries, and removal steps.

Scenario

Endpoint detection triggers: "WMI Subscription Created." The analyst sees three new objects in root\subscription within 30 seconds of each other. The EventFilter queries Win32_ProcessStartTrace WHERE ProcessName='lsass.exe' — it fires every time LSASS starts (i.e., at every boot). The consumer is an ActiveScriptEventConsumer running VBScript that connects back to a C2. The binding links them. Since LSASS always starts at boot, this subscription executes on every reboot with SYSTEM privileges. The attacker is gone. The persistence runs silently. This chapter explains how to find it, kill it, and prevent it.

The Persistence Triad

WMI persistence requires three objects in the root\subscription namespace:


  WMI Persistence Object Model
  ══════════════════════════════════════════════════════════════════

  1. __EventFilter
     ┌────────────────────────────────────────────────┐
     │ Name        : any string (often benign-looking) │
     │ Query       : WQL SELECT statement              │
     │ QueryLanguage: "WQL"                            │
     │ EventNamespace: "root\cimv2" (usually)          │
     └────────────────────────────────────────────────┘

  2. __EventConsumer (one of four subtypes)
     ┌────────────────────────────────────────────────┐
     │ CommandLineEventConsumer                        │
     │   CommandLineTemplate: "cmd.exe /c ..."         │
     │                                                │
     │ ActiveScriptEventConsumer                       │
     │   ScriptText: "VBScript or JScript code"        │
     │   ScriptingEngine: "VBScript" | "JScript"       │
     │                                                │
     │ ScriptingEventConsumer (deprecated alias)       │
     │                                                │
     │ LogFileEventConsumer                            │
     │   Filename: path to log file                    │
     │                                                │
     │ NTEventLogEventConsumer                         │
     │   SourceName: event source name                 │
     └────────────────────────────────────────────────┘

  3. __FilterToConsumerBinding
     ┌────────────────────────────────────────────────┐
     │ Filter   : reference to __EventFilter object    │
     │ Consumer : reference to __EventConsumer object  │
     └────────────────────────────────────────────────┘

  Binding is the glue — without it, filter and consumer are inert.

    

Consumer Types in Detail

Consumer TypeWhat it doesExecution contextDetection difficulty
CommandLineEventConsumerRuns a command lineWmiPrvSE.exe spawns child processEasy — Sysmon Event 1 with WmiPrvSE.exe parent
ActiveScriptEventConsumerRuns VBScript/JScript inlineInterpreted inside WmiPrvSE.exe — no child processHard — no child process; script lives in OBJECTS.DATA only
LogFileEventConsumerWrites text to a log fileWmiPrvSE.exe writes fileEasy — file write event, rarely malicious alone
NTEventLogEventConsumerWrites Windows event log entryWmiPrvSE.exe calls ReportEvent()Easy — event log entry generated
Common mistake

ActiveScriptEventConsumer is the stealthiest consumer type because the malicious code runs entirely within WmiPrvSE.exe — no child process is spawned. Detections based purely on "WmiPrvSE.exe spawned a process" miss it completely. The only reliable detection is monitoring for new objects in root\subscription namespace (WMI activity events) or offline scanning of OBJECTS.DATA.

Installation Telemetry

Windows logs WMI activity in Microsoft-Windows-WMI-Activity/Operational:

Event IDDescription
5857WMI provider started — provider name + process ID
5858WMI query failed with error
5859WMI permanent event filter registered (subscription created)
5860WMI temporary event filter registered
5861WMI permanent event consumer registered

Events 5859 and 5861 are the alerts to tune for — any new permanent subscription should be investigated.

Hunting Queries

PowerShellhunt_wmi_persistence.ps1
# List all EventFilters
Get-WMIObject -Namespace root\subscription -Class __EventFilter |
    Select-Object Name, Query, QueryLanguage | Format-List

# List CommandLineEventConsumers
Get-WMIObject -Namespace root\subscription -Class CommandLineEventConsumer |
    Select-Object Name, CommandLineTemplate | Format-List

# List ActiveScriptEventConsumers
Get-WMIObject -Namespace root\subscription -Class ActiveScriptEventConsumer |
    Select-Object Name, ScriptingEngine, ScriptText | Format-List

# List all bindings
Get-WMIObject -Namespace root\subscription -Class __FilterToConsumerBinding |
    Select-Object Filter, Consumer | Format-List

# Cross-reference: show complete triads
$bindings = Get-WMIObject -Namespace root\subscription -Class __FilterToConsumerBinding
foreach ($b in $bindings) {
    Write-Host "=== Binding ==="
    Write-Host "Filter:   $($b.Filter)"
    Write-Host "Consumer: $($b.Consumer)"
}
YAMLwmi_subscription_created.yml
title: WMI Permanent Subscription Created
status: stable
logsource:
    product: windows
    service: wmi
    definition: 'Microsoft-Windows-WMI-Activity/Operational'
detection:
    selection:
        EventID:
            - 5859  # EventFilter registered
            - 5861  # EventConsumer registered
    condition: selection
falsepositives:
    - SCCM/Intune endpoint management
    - Legitimate monitoring software
level: high
tags:
    - attack.persistence
    - attack.t1546.003

Removing WMI Persistence

PowerShellremove_wmi_persistence.ps1
$ns = "root\subscription"
$filterName = "WindowsUpdate"  # substitute actual name

# Remove binding first (dependent on filter and consumer)
Get-WMIObject -Namespace $ns -Class __FilterToConsumerBinding |
    Where-Object { $_.Filter -like "*$filterName*" } |
    Remove-WmiObject

# Remove consumer
Get-WMIObject -Namespace $ns -Class CommandLineEventConsumer |
    Where-Object { $_.Name -like "*$filterName*" } |
    Remove-WmiObject

# Remove filter last
Get-WMIObject -Namespace $ns -Class __EventFilter |
    Where-Object { $_.Name -eq $filterName } |
    Remove-WmiObject

# Verify all gone
Get-WMIObject -Namespace $ns -Class __EventFilter
Get-WMIObject -Namespace $ns -Class __FilterToConsumerBinding
Mental model

Think of WMI persistence as a "silent cron job" that runs inside the Windows management layer. Unlike scheduled tasks (visible in Task Scheduler), WMI subscriptions have no UI, no file on disk, and no registry key. The entire mechanism is controlled via COM calls that write objects into OBJECTS.DATA. This is why WMI is favored by APT groups — it's effective and avoids the most common detection tripwires.

Known Malware Using WMI Persistence

Malware / GroupTechnique
POWERLIKSActiveScriptEventConsumer running PowerShell stored in registry, triggered by WMI timer
APT29 (Cozy Bear)WMI subscription as second-stage persistence after initial compromise
Duqu 2.0WMI providers as persistence (dropped custom provider DLL)
Empire frameworkInvoke-WMIEvent module installs CommandLineEventConsumer
Metasploitexploit/windows/local/wmi_persistence module

Q & A

Q: Why is WMI persistence called "fileless" when it still requires OBJECTS.DATA on disk?

The malicious payload — the script code or command template — is stored inside OBJECTS.DATA as a property value of the consumer object. OBJECTS.DATA is a legitimate WMI system file. There's no separate malware file. Endpoint security tools that scan for executable files, unusual PE files, or suspicious scripts on disk don't find anything, because the payload is embedded in a binary database. "Fileless" means the attacker's code is not written as a standalone file — it's data inside a system database. The WMI engine is the runner. This bypasses many file-based detections while surviving reboots.

Q: A persistence subscription fires on __InstanceCreationEvent for Win32_Process. When exactly does it trigger?

__InstanceCreationEvent for Win32_Process fires immediately after a new process is created and WMI becomes aware of it. The latency depends on the WITHIN clause in the WQL query — WITHIN 5 means WMI polls every 5 seconds. Without WITHIN, the event is immediate (synchronous) but requires an event-driven provider. The consumer's action runs in WmiPrvSE.exe after the event fires. Practically: an attacker setting a subscription to fire on lsass.exe creation (which happens at boot) gets code execution early in the boot sequence, before many defenses are fully loaded. This makes boot-triggered WMI subscriptions especially dangerous.