// question

On a Windows endpoint, an attacker ran a PowerShell sequence to establish a WMI event subscription using CommandLineEventConsumer. Inspect the process or script execution logs to uncover which executable was set to run by this subscription.

Reference KQL answer and model-by-model comparison.

3c64f177-28e2-49eb-a799-d767b24dd1e0
question id
8/23
models correct
1
valid answer
35%
success rate
// reference answer

Human-validated correct answer for this question.

notepad.exe
// model comparison
ModelAnswerKQL queryStatusTriesTimeCost
gemini-2.5-flash-preview-04-17
no answer
no query generatedfail50.00s0.0471
gpt-35-turbo
no answer
no query generatedfail50.00s0.0112
gpt-4-turbo-2024-04-09
no answer
no query generatedfail50.00s0.2363
gpt-4.1
notepad.exe
DeviceProcessEvents
| where FileName in~ ("powershell.exe", "pwsh.exe")
| where ProcessCommandLine has_all ("CommandLineEventConsumer") and (ProcessCommandLine has "WMI" or ProcessCommandLine has "__EventFilter" or ProcessCommandLine has "event subscription")
| extend SubscriptionCommand = extract(@'CommandLine="([^"]+)"', 1, ProcessCommandLine)
| extend SubscriptionExecutable = extract(@'([a-zA-Z]:\\[^"\s]+\.exe)', 1, SubscriptionCommand)
| project TimeGenerated, DeviceName, AccountName, InitiatingProcessCommandLine, ProcessCommandLine, SubscriptionCommand, SubscriptionExecutable
| order by TimeGenerated desc
pass18.62s0.0127
gpt-4.1-finetuned
no answer
no query generatedfail50.00s0.0544
gpt-4.1-mini
notepad.exe
DeviceProcessEvents
| where ProcessCommandLine contains "Register-WmiEvent" or ProcessCommandLine contains "Register-WmiEventSubscription" or ProcessCommandLine contains "EventFilter" or ProcessCommandLine contains "__EventFilter" or ProcessCommandLine contains "CommandLineEventConsumer"
| extend ExecutorCommand = extract(@"(?i)CommandLineEventConsumer" , 0, ProcessCommandLine)
| project TimeGenerated, DeviceName, InitiatingProcessAccountName, ProcessCommandLine, ExecutorCommand
| order by TimeGenerated desc
pass13.72s0.0023
gpt-4.1-nano
no answer
no query generatedfail50.00s0.0017
gpt-4o
no answer
no query generatedfail50.00s0.0478
gpt-5-high
notepad.exe
let wmi_keywords = dynamic(["CommandLineEventConsumer","CommandLineTemplate","__FilterToConsumerBinding","__EventFilter","root\\subscription","New-CimInstance","Set-WmiInstance","Register-WmiEvent","wmic"]);
let proc_hits =
DeviceProcessEvents
| where FileName in~ ("powershell.exe","pwsh.exe","wmic.exe","mofcomp.exe")
| where ProcessCommandLine has_any (wmi_keywords)
| extend CommandLineTemplate = coalesce(
    extract("CommandLineTemplate\\s*=\\s*\"([^\"]+)\"", 1, ProcessCommandLine),
    extract("CommandLineTemplate\\s*=\\s*'([^']+)'", 1, ProcessCommandLine),
    extract("-CommandLineTemplate\\s+\"([^\"]+)\"", 1, ProcessCommandLine),
    extract("-CommandLineTemplate\\s+'([^']+)'", 1, ProcessCommandLine),
    extract("CommandLineTemplate\\s*[:=]\\s*([^\\s,}]+)", 1, ProcessCommandLine)
)
| where isnotempty(CommandLineTemplate)
| extend ExecutableSetToRun = coalesce(
    extract("^\\s*\"([^\"]+?)\"", 1, CommandLineTemplate),
    extract("^\\s*'([^']+?)'", 1, CommandLineTemplate),
    extract("^\\s*([^\\s]+)", 1, CommandLineTemplate)
)
| extend ConsumerName = coalesce(
    extract("Name\\s*=\\s*\"([^\"]+)\"", 1, ProcessCommandLine),
    extract("Name\\s*=\\s*'([^']+)'", 1, ProcessCommandLine),
    extract("-Name\\s+\"([^\"]+)\"", 1, ProcessCommandLine),
    extract("-Name\\s+'([^']+)'", 1, ProcessCommandLine)
)
| project TimeGenerated, DeviceId, DeviceName, AccountName, InitiatingProcessAccountName, FileName, FolderPath, ProcessCommandLine, ConsumerName, CommandLineTemplate, ExecutableSetToRun;
let script_hits =
DeviceEvents
| where ActionType startswith "PowerShell"
| extend ScriptText = tostring(coalesce(AdditionalFields.ScriptContent, AdditionalFields.ScriptBlockText, AdditionalFields.DecodedCommand, AdditionalFields.Command, AdditionalFields.Content, AdditionalFields.CommandLine, AdditionalFields.Payload))
| where isnotempty(ScriptText) and ScriptText has_any (wmi_keywords)
| extend CommandLineTemplate = coalesce(
    extract("CommandLineTemplate\\s*=\\s*\"([^\"]+)\"", 1, ScriptText),
    extract("CommandLineTemplate\\s*=\\s*'([^']+)'", 1, ScriptText),
    extract("-CommandLineTemplate\\s+\"([^\"]+)\"", 1, ScriptText),
    extract("-CommandLineTemplate\\s+'([^']+)'", 1, ScriptText),
    extract("CommandLineTemplate\\s*[:=]\\s*([^\\s,}]+)", 1, ScriptText)
)
| where isnotempty(CommandLineTemplate)
| extend ExecutableSetToRun = coalesce(
    extract("^\\s*\"([^\"]+?)\"", 1, CommandLineTemplate),
    extract("^\\s*'([^']+?)'", 1, CommandLineTemplate),
    extract("^\\s*([^\\s]+)", 1, CommandLineTemplate)
)
| extend ConsumerName = coalesce(
    extract("Name\\s*=\\s*\"([^\"]+)\"", 1, ScriptText),
    extract("Name\\s*=\\s*'([^']+)'", 1, ScriptText),
    extract("-Name\\s+\"([^\"]+)\"", 1, ScriptText),
    extract("-Name\\s+'([^']+)'", 1, ScriptText)
)
| project TimeGenerated, DeviceId, DeviceName, AccountName, ActionType, ConsumerName, CommandLineTemplate, ExecutableSetToRun, ScriptText;
proc_hits
| union isfuzzy=true script_hits
| order by TimeGenerated desc
pass3319.45s0.1746
gpt-5-mini-high
no answer
no query generatedfail50.00s0.0229
gpt-5-mini-low
no answer
no query generatedfail50.00s0.0162
gpt-5-mini-medium
notepad.exe
// Known WMI subscription indicators
let wmiIndicators = dynamic(["Register-WmiEvent","New-EventFilter","Set-WmiInstance","CommandLineEventConsumer","CommandLineTemplate","FilterToConsumerBinding","__InstanceModificationEvent","__EventConsumer","Bind","Filter","Consumer","FilterToConsumerBinding"]);

// Helper to extract executables or scripts from strings
let extractExe = (s:string){
    // Try full path first, then filename-only
    coalesce(
        extract(@"([A-Za-z]:\\[\\\w\-\.\s\\/]+\\?\.(?:exe|ps1|bat|vbs|wsh|cmd|msc|jar))", 1, s),
        extract(@"([\\\w\-]+\.(?:exe|ps1|bat|vbs|cmd|msi|msc|jar))", 1, s)
    )
};

// PowerShell process events where PowerShell constructs WMI subscriptions
let psProcesses =
    DeviceProcessEvents
    | where tolower(FileName) has "powershell" or tolower(InitiatingProcessFileName) has "powershell"
    | where ProcessCommandLine has_any (wmiIndicators) or InitiatingProcessCommandLine has_any (wmiIndicators)
    | extend Source = "DeviceProcessEvents", CmdLine = tostring(ProcessCommandLine), InitiatorCmd = tostring(InitiatingProcessCommandLine)
    | project TimeGenerated, DeviceName, Source, FileName, ProcessId, ProcessCreationTime, CmdLine, InitiatorCmd, InitiatingProcessFileName;

// DeviceEvents (process creation) for PowerShell
let psCreations =
    DeviceEvents
    | where tolower(FileName) has "powershell" or tolower(InitiatingProcessFileName) has "powershell"
    | where ProcessCommandLine has_any (wmiIndicators) or InitiatingProcessCommandLine has_any (wmiIndicators)
    | extend Source = "DeviceEvents", CmdLine = tostring(ProcessCommandLine), InitiatorCmd = tostring(InitiatingProcessCommandLine)
    | project TimeGenerated, DeviceName, Source, FileName, ProcessId, ProcessCreationTime, CmdLine, InitiatorCmd, InitiatingProcessFileName;

// Registry writes under WBEM/WMI that create CommandLineTemplate or FilterToConsumerBinding
let regWrites =
    DeviceRegistryEvents
    | where tostring(RegistryKey) has "WBEM" or tostring(RegistryKey) has "WMI"
    | where RegistryValueName has_any ("CommandLineTemplate","FilterToConsumerBinding","Consumer") or tostring(RegistryValueData) has_any (wmiIndicators)
    | extend Source = "DeviceRegistryEvents", CmdLine = tostring(RegistryValueData), InitiatorCmd = tostring(InitiatingProcessCommandLine)
    | project TimeGenerated, DeviceName, Source, FileName = InitiatingProcessFileName, ProcessId = InitiatingProcessId, ProcessCreationTime = InitiatingProcessCreationTime, CmdLine, InitiatorCmd, RegistryKey, RegistryValueName;

// Additional DeviceEvents where AdditionalFields include WMI subscription indicators
let otherEvents =
    DeviceEvents
    | where tostring(AdditionalFields) has_any (wmiIndicators)
    | extend Source = "DeviceEvents_AddFields", CmdLine = iff(isnull(ProcessCommandLine) or ProcessCommandLine == "", tostring(AdditionalFields), tostring(ProcessCommandLine)), InitiatorCmd = tostring(InitiatingProcessCommandLine)
    | project TimeGenerated, DeviceName, Source, FileName, ProcessId, ProcessCreationTime, CmdLine, InitiatorCmd, InitiatingProcessFileName;

union psProcesses, psCreations, regWrites, otherEvents
| extend FoundExecutable = extractExe(tostring(CmdLine))
| extend FoundExecutable = tostring(FoundExecutable)
| where isnotempty(CmdLine) and (FoundExecutable != "" or CmdLine contains "CommandLineEventConsumer" or CmdLine contains "CommandLineTemplate" or RegistryKey contains "WBEM" )
| summarize Occurrences = count(), SampleCmdLines = make_set(CmdLine, 10), SampleInitiators = make_set(InitiatorCmd, 10), Devices = make_set(DeviceName), FirstSeen = min(TimeGenerated), LastSeen = max(TimeGenerated) by FoundExecutable, Source
| where isnotempty(FoundExecutable) or Occurrences > 0
| project FoundExecutable, Occurrences, Devices, Source, FirstSeen, LastSeen, SampleCmdLines, SampleInitiators
| top 200 by LastSeen desc
pass339.73s0.0120
gpt-5-nano-high
None
let StartTime = ago(1d);
let EndTime = now();

let WmiSubscriptions = DeviceRegistryEvents
| where TimeGenerated between (StartTime .. EndTime)
| where RegistryKey contains "WBEM" or RegistryKey contains "WMI" or RegistryValueName contains "CommandLineTemplate" or RegistryValueName contains "CommandLine" or RegistryValueData contains "CommandLineTemplate"
| project TimeGenerated, DeviceId, DeviceName, RegistryKey, RegistryValueName, RegistryValueData, InitiatingProcessId, InitiatingProcessFileName, InitiatingProcessCommandLine;

let PsInitiators = DeviceProcessEvents
| where TimeGenerated between (StartTime .. EndTime)
| where InitiatingProcessFileName in ("powershell.exe","pwsh.exe","powershell") or InitiatingProcessCommandLine contains "powershell"
| project DeviceId, InitiatingProcessId, InitiatingProcessFileName, InitiatingProcessCommandLine;

let SubWithInitiator = WmiSubscriptions
| join kind=inner (PsInitiators) on DeviceId, InitiatingProcessId
| extend SubscriptionCommandLine = tostring(RegistryValueData)
| extend ExecutableFromSubscription = extract(@"([A-Za-z]:\\[^\""]+\\.exe)", 1, SubscriptionCommandLine)
| project TimeGenerated, DeviceName, DeviceId, InitiatingProcessFileName, InitiatingProcessCommandLine, RegistryKey, RegistryValueName, RegistryValueData, SubscriptionCommandLine, ExecutableFromSubscription;

SubWithInitiator
| sort by TimeGenerated desc
fail2106.43s0.0055
gpt-5-nano-medium
no answer
no query generatedfail50.00s0.0105
grok-3-beta
no answer
no query generatedfail50.00s0.1135
grok-3-mini-beta
notepad.exe
DeviceProcessEvents
| where FileName has "powershell.exe" and ProcessCommandLine contains "CommandLineEventConsumer"
| extend Executable = extract("CommandLineTemplate=[\"']([^\"']+)[\"']", 1, ProcessCommandLine)
| project TimeGenerated, DeviceName, ProcessCommandLine, Executable
| order by TimeGenerated desc
pass335.67s0.0045
o1-high
notepad.exe
DeviceProcessEvents
| where FileName =~ "powershell.exe"
| where ProcessCommandLine has "CommandLineEventConsumer" or ProcessCommandLine has "__EventFilter" or ProcessCommandLine has "__FilterToConsumerBinding"
| parse kind=regex flags=U ProcessCommandLine with @"CommandLineTemplate\s*=\s*'([^']+)'" CommandLineTemplate
| project TimeGenerated, DeviceId, DeviceName, InitiatingProcessAccountName, ProcessCommandLine, CommandLineTemplate
pass141.21s0.2311
o1-low
notepad.exe
DeviceProcessEvents
| where FileName in~ ("powershell.exe", "pwsh.exe", "powershell_ise.exe")
| where isnotempty(ProcessCommandLine)
| where ProcessCommandLine has_any("Register-WMIEvent","CommandLineEventConsumer","__EventConsumer")
| project TimeGenerated,
          DeviceName,
          InitiatingProcessAccountName,
          FileName,
          FolderPath,
          ProcessCommandLine
pass131.21s0.2148
o3-high
no answer
no query generatedfail50.00s0.1015
o3-mini-high
notepad.exe
union withsource=TableName DeviceProcessEvents, DeviceEvents
| where (InitiatingProcessCommandLine has "PowerShell" or ProcessCommandLine has "PowerShell")
      and (InitiatingProcessCommandLine has "CommandLineEventConsumer" or ProcessCommandLine has "CommandLineEventConsumer")
| extend CmdLine = iif(isnotempty(ProcessCommandLine), ProcessCommandLine, InitiatingProcessCommandLine)
| extend ExecutableSet = extract("(?i)([\\w]+\\.exe)", 1, CmdLine)
| project TimeGenerated, DeviceName, TableName, FileName, CmdLine, ExecutableSet
| sort by TimeGenerated desc
pass225.32s0.0183
o3-mini-low
no answer
no query generatedfail50.00s0.0390
o4-mini-high
no answer
no query generatedfail50.00s0.0459
o4-mini-low
no answer
no query generatedfail50.00s0.0467