On an event
WMI Event Subscriptions: Windows Persistence via WMI
How permanent WMI event subscriptions run code as SYSTEM when an event fires, how attackers create and hide them, and how to find and remove them safely.
- MITRE ATT&CK
- T1546.003
- Tactics
- Persistence, Privilege escalation
- Runs
- On an event
- Set up by
- Administrators
- Locations
- root\subscription
- C:\Windows\System32\wbem\Repository\OBJECTS.DATA
- C:\Windows\System32\wbem\Repository\INDEX.BTR
- C:\Windows\System32\wbem\Repository\MAPPING1.MAP
- HKLM\SOFTWARE\Microsoft\Wbem\CIMOM
How it works
A permanent WMI event subscription is three objects stored in a WMI namespace, normally root\subscription:
| Object | Role |
|---|---|
__EventFilter | A WQL query that describes an event, with the namespace it watches (EventNamespace, usually root\cimv2) |
| Event consumer | What to do: an instance of a class derived from __EventConsumer |
__FilterToConsumerBinding | Links one filter to one consumer; without it nothing happens |
The WMI service (winmgmt, in svchost.exe) evaluates every bound filter and, when the event occurs, hands it to the consumer. Windows ships five standard consumer classes:
| Consumer | Does |
|---|---|
CommandLineEventConsumer | Starts a process (ExecutablePath, CommandLineTemplate) as a child of WmiPrvSE.exe, as SYSTEM |
ActiveScriptEventConsumer | Runs VBScript or JScript (ScriptText or ScriptFileName) inside scrcons.exe, as SYSTEM |
NTEventLogEventConsumer | Writes an event log entry |
LogFileEventConsumer | Appends text to a file |
SMTPEventConsumer | Sends an e-mail |
Filters can watch almost anything: instance creation or modification polled every N seconds (WITHIN 60), process starts (Win32_ProcessStartTrace), logons, a time of day (Win32_LocalTime), or uptime. A common persistence filter fires a few minutes after each boot:
SELECT * FROM __InstanceModificationEvent WITHIN 60 WHERE TargetInstance ISA 'Win32_PerfFormattedData_PerfOS_System' AND TargetInstance.SystemUpTime >= 240 AND TargetInstance.SystemUpTime < 325
Subscriptions live in the WMI repository, not in the registry or in a startup folder, and survive reboots. A temporary subscription, made by a running process through the API, ends with that process. Windows 10 added event 5861 to Microsoft-Windows-WMI-Activity/Operational, logged when a permanent consumer is registered.
Where it lives
| Location | What |
|---|---|
C:\Windows\System32\wbem\Repository\OBJECTS.DATA | The objects themselves, including freed pages that still hold deleted ones |
INDEX.BTR, MAPPING1.MAP to MAPPING3.MAP | The index and the page maps of the same repository |
root\subscription | The usual namespace for permanent subscriptions; root\cimv2 and others accept them too |
HKLM\SOFTWARE\Microsoft\Wbem\CIMOM, value Autorecover MOFs | MOF files recompiled when the repository is rebuilt (added by #pragma autorecover) |
The repository files are held open by the WMI service; copy them through a shadow copy. A stock Windows 10 or 11 system usually has one subscription in root\subscription, "SCM Event Log Filter" bound to "SCM Event Log Consumer" (an NTEventLogEventConsumer). Some older images also carry "BVTFilter" and "BVTConsumer", which run cscript KernCap.vbs when the processor load passes 99%.
Creating a permanent subscription in root\subscription needs administrator rights.
How attackers use it
WMI subscriptions appeal because nothing lands in the usual autostart places, the payload can live entirely in the repository, and it runs as SYSTEM. APT29's POSHSPY backdoor, described by Mandiant in 2017, used a permanent subscription to start PowerShell code that it kept in WMI.
Ways to create one:
- PowerShell:
Set-WmiInstance(Windows PowerShell) orNew-CimInstance -Namespace root/subscription -ClassName __EventFilter -Property @{…}, then the consumer and the binding. wmic /namespace:\\root\subscription PATH __EventFilter CREATE Name="Updater", EventNameSpace="root\cimv2", QueryLanguage="WQL", Query="…";wmicis deprecated but still used.- A MOF file compiled with
mofcomp.exe. Stuxnet abused the old automatic compilation of MOFs dropped intoSystem32\wbem\mofon Windows XP. - Remotely, over DCOM or WinRM, with the same calls.
Typical consumers run powershell -NoP -W Hidden -Enc …, an mshta or rundll32 line, or a script that downloads the next stage. Disguise is by name ("Windows Update", "SCM Event Log Consumer" reused), by namespace (outside root\subscription), or by deleting the binding until it is needed.
How to check a host
On a live machine, in PowerShell as administrator:
Get-CimInstance -Namespace root/subscription -ClassName __EventFilter | Select-Object Name, EventNamespace, Query
Get-CimInstance -Namespace root/subscription -ClassName __EventConsumer | Format-List *
Get-CimInstance -Namespace root/subscription -ClassName __FilterToConsumerBinding | Select-Object Filter, Consumer
Get-CimInstance -Namespace root -ClassName __Namespace | Select-Object Name
autorunsc.exe -accepteula -a m -h -s -c > wmi.csv
__EventConsumer is the base class, so the query returns consumers of every type. Repeat the three queries for other namespaces, since a subscription outside root\subscription escapes tools that look only there.
Offline, the repository can be parsed without WMI: WMI Parser reads OBJECTS.DATA and recovers deleted objects from its freed pages. Check Autorecover MOFs in the SOFTWARE hive for MOF paths that are not Windows' own.
Events: Microsoft-Windows-WMI-Activity/Operational 5861 names the namespace, filter and consumer of a new permanent subscription. Sysmon events 19, 20 and 21 log filter, consumer and binding activity. Processes started by a CommandLineEventConsumer have WmiPrvSE.exe as parent; scrcons.exe running at all deserves a look.
Suspicious: any CommandLineEventConsumer or ActiveScriptEventConsumer outside a known management product, encoded or downloading commands, a subscription in an unusual namespace, an SCM or BVT name with different content, and consumers with no binding.
What Windows Persistence flags
The tool parses OBJECTS.DATA, with INDEX.BTR and the mapping files when present, with WMI Parser's engine. It gets the filters, consumers and bindings, live or recovered from freed space, with their namespace. It lists one entry per binding (filter → consumer), and one per live consumer that no binding points to. Each entry shows the query, the consumer class and its action (ExecutablePath when set, else CommandLineTemplate; ScriptFileName, else ScriptText), the creator account from the CreatorSID of the binding, else of the consumer, else of the filter, the status, and any decoded -EncodedCommand. When ExecutablePath is set, the template appears as "Command line template" and is checked too. The trigger comes from the query: uptime is boot, logon classes are logon, timers and Win32_LocalTime are a schedule, process starts are a program start, anything else is an event.
The SCM Event Log and BVT subscriptions are Windows defaults only when the filter and consumer both match Windows' content; "Hide Windows entries" hides them. One named like them but different gets WMI subscription named like a Windows default but different. Every other subscription gets:
- WMI script consumer for an
ActiveScriptEventConsumer. - The command-line rules on the action, in system context (not for event log consumers), such as Encoded PowerShell command, Downloads and runs code, Obfuscated command or Windows program used to run outside code.
- WMI binding with a missing filter or consumer, WMI subscription outside root\subscription (namespace), WMI consumer not bound to any filter and Deleted WMI subscription recovered.
- WMI event subscription when none of these applies.
- WMI consumer rewritten when an older copy of the consumer in freed space ran something else; the previous command is checked by the same rules.
From the SOFTWARE hive, the tool also reads Autorecover MOFs. Each MOF there is an entry; those in Windows' folders are Windows' own and hidden by the filter. Any other gets Extra Autorecover MOF (medium, low under Program Files), plus the path rules such as System-wide autostart from a writable folder.
Across mechanisms: Written date, when the newest time WMI Parser recovers for the objects falls in the 7 days before the newest time seen for that host, Inside a Defender exclusion (exclusion) and program also runs from mechanisms.
Limits: temporary subscriptions exist only in memory. Filters with no binding, timer instructions and payloads stored in other WMI classes are not listed. The MOF files that Autorecover MOFs names are not read, only their paths.
Cleaning up
- Preserve the repository first: copy
C:\Windows\System32\wbem\Repositorythrough a shadow copy (the collection command does this). Deleted objects can stay in freed pages, but new writes overwrite them. - Remove the binding, then the consumer and the filter:
Get-CimInstance -Namespace root/subscription -ClassName __FilterToConsumerBinding | Where-Object { $_.Filter.Name -eq 'Updater' } | Remove-CimInstance
Get-CimInstance -Namespace root/subscription -ClassName CommandLineEventConsumer -Filter "Name='Updater'" | Remove-CimInstance
Get-CimInstance -Namespace root/subscription -ClassName __EventFilter -Filter "Name='Updater'" | Remove-CimInstance
- Leave the SCM Event Log subscription alone. Do not reset the repository (
winmgmt /resetrepository) to clean up: it destroys the evidence and the registrations of installed software. - Remove any MOF path the attacker added to
Autorecover MOFs, or it can come back after a repository rebuild. - Delete the payload, then look for the same program in scheduled tasks, services and PowerShell profiles. The consumer ran as SYSTEM: rotate credentials used on the host.
See also
Related techniques
On Windows Forensics
The artifacts behind this technique, in the Windows Forensics reference: