let lookback = 3d;
let proxyChain = DeviceProcessEvents
| where Timestamp > ago(lookback)
| where InitiatingProcessFileName =~ "pcalua.exe" and FileName in~ ("mshta.exe", "msiexec.exe")
| project DeviceId, DeviceName, ProxyTime = Timestamp, ProxyCmd = ProcessCommandLine;
let runKey = DeviceRegistryEvents
| where Timestamp > ago(lookback)
| where ActionType == "RegistryValueSet"
| where RegistryKey has "CurrentVersion" and RegistryKey has "Run"
| where RegistryValueData has_any ("AppData", "ProgramData")
| project DeviceId, KeyTime = Timestamp, RegistryValueData;
let defenderTamper = DeviceProcessEvents
| where Timestamp > ago(lookback)
| where ProcessCommandLine has_any ("Set-MpPreference", "Add-MpPreference -ExclusionPath", "DisableRealtimeMonitoring", "Stop-Service WinDefend")
| project DeviceId, TamperTime = Timestamp, TamperCmd = ProcessCommandLine;
proxyChain
| join kind=inner runKey on DeviceId
| join kind=inner defenderTamper on DeviceId
| where abs(datetime_diff('minute', ProxyTime, KeyTime)) <= 30
and abs(datetime_diff('minute', ProxyTime, TamperTime)) <= 30
| project DeviceName, DeviceId, ProxyTime, ProxyCmd, KeyTime, RegistryValueData, TamperTime, TamperCmd, chain = "ClickFix->Loader->DefenderTamper"
| sort by ProxyTime asc