MDE - Scheduled Task Execution
MDE Schedulted Tasks
Query
let Lookback = 7d;
DeviceProcessEvents
| where Timestamp > ago(Lookback)
| where InitiatingProcessFileName =~ "svchost.exe"
| where InitiatingProcessCommandLine has "Schedule"
//
// Process started by Task Scheduler
//
| extend RunAsAccount = strcat(AccountDomain, @"\", AccountName)
| project
Timestamp,
DeviceName,
RunAsAccount,
AccountSid,
ExecutedFile = FileName,
CommandLine = ProcessCommandLine,
InitiatingProcessFileName,
InitiatingProcessCommandLine,
InitiatingProcessAccountName,
ProcessId,
InitiatingProcessId
//
// Calculate execution intervals
//
| sort by DeviceName asc, CommandLine asc, RunAsAccount asc, Timestamp asc
| serialize
| extend
PreviousTimestamp = prev(Timestamp),
PreviousDeviceName = prev(DeviceName),
PreviousCommandLine = prev(CommandLine),
PreviousRunAsAccount = prev(RunAsAccount)
| where DeviceName == PreviousDeviceName
and CommandLine == PreviousCommandLine
and RunAsAccount == PreviousRunAsAccount
| extend IntervalMinutes = datetime_diff("minute", Timestamp, PreviousTimestamp)
//
// Summarize schedule behavior
//
| summarize
FirstSeen = min(PreviousTimestamp),
LastSeen = max(Timestamp),
ExecutionCount = count() + 1,
MinIntervalMinutes = min(IntervalMinutes),
AvgIntervalMinutes = round(avg(IntervalMinutes),2),
MaxIntervalMinutes = max(IntervalMinutes),
ObservedIntervals = make_set(IntervalMinutes,20)
by
DeviceName,
RunAsAccount,
AccountSid,
ExecutedFile,
CommandLine
//
// Human readable schedule guess
//
| extend InferredSchedulePattern = case(
AvgIntervalMinutes between (0 .. 2), "Every few minutes",
AvgIntervalMinutes between (14 .. 16), "Every ~15 minutes",
AvgIntervalMinutes between (29 .. 31), "Every ~30 minutes",
AvgIntervalMinutes between (55 .. 65), "Every ~1 hour",
AvgIntervalMinutes between (115 .. 125), "Every ~2 hours",
AvgIntervalMinutes between (350 .. 370), "Every ~6 hours",
AvgIntervalMinutes between (710 .. 730), "Every ~12 hours",
AvgIntervalMinutes between (1430 .. 1450), "Daily",
AvgIntervalMinutes between (10000 .. 10120), "Weekly",
strcat("~Every ", tostring(AvgIntervalMinutes), " minutes")
)
| project
DeviceName,
RunAsAccount,
AccountSid,
ExecutedFile,
InferredSchedulePattern,
ExecutionCount,
FirstSeen,
LastSeen,
MinIntervalMinutes,
AvgIntervalMinutes,
MaxIntervalMinutes,
ObservedIntervals,
CommandLine
| order by ExecutionCount descAbout this query
Explanation
This query is designed to monitor and analyze scheduled task executions on Windows servers using Microsoft Defender for Endpoint data. It consists of two main parts:
-
Scheduled Task Executions on Servers:
- This part identifies processes initiated by the Task Scheduler service (
svchost.exe) with command lines containing "Schedule". - It focuses on Windows servers that are onboarded and retrieves the latest device records.
- The query joins these records with process events to find tasks that have been executed.
- Optional filters can narrow down results to specific executables like
cmd.exe,powershell.exe, andcscript.exe, or exclude common system accounts to focus on custom accounts.
- This part identifies processes initiated by the Task Scheduler service (
-
Scheduled Task Intervals:
- This section examines process events from the last seven days to identify patterns in task execution intervals.
- It calculates the time between consecutive task executions on the same device, with the same command line and run-as account.
- The query summarizes this data to infer potential scheduling patterns, such as tasks running every few minutes, hourly, daily, etc.
- The results provide insights into how often tasks are executed and help identify any unusual or potentially malicious scheduling behavior.
Overall, this query helps in threat hunting by identifying and analyzing scheduled task activities, which can be used for malicious purposes like persistence or privilege escalation. However, it does not directly indicate malicious intent, as legitimate tasks may exhibit similar characteristics.