MDE - Scheduled Task Execution
MDE Scheduled 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. Here's a simplified breakdown of what the query does:
-
Purpose: The query aims to provide an overview of process activities related to the Task Scheduler service. It helps identify potential misuse of scheduled tasks, which can be used for unauthorized execution, persistence, or privilege escalation.
-
Query Variants:
- First Variant: Focuses on Windows servers. It retrieves the latest device records, filters for onboarded Windows servers, and joins them with process events initiated by
svchost.execontaining "Schedule" in the command line. This variant does not have a specific time filter and relies on the portal's time range. - Second Variant: Looks at the last seven days of process events across all devices, not just servers. It calculates the intervals between consecutive events with the same device name, command line, and run-as account, then summarizes the timing to infer a schedule pattern.
- First Variant: Focuses on Windows servers. It retrieves the latest device records, filters for onboarded Windows servers, and joins them with process events initiated by
-
Risk Consideration: The query identifies activities that might indicate abuse of scheduled tasks but does not confirm malicious intent. Legitimate activities might also match the same process characteristics.
-
Data Sources: It uses data from the
DeviceInfoandDeviceProcessEventstables in Microsoft Defender for Endpoint. -
Optional Filters: The query includes optional filters to narrow down results to specific executables like
cmd.exe,powershell.exe, andcscript.exe, or to exclude common system accounts. -
Investigation Guidance: Users are advised to validate unusual commands and accounts against registered task definitions and expected maintenance activities. The inferred schedule labels are heuristic and should be compared with actual task configurations.
-
Output: The query provides details such as device name, run-as account, executed file, inferred schedule pattern, execution count, and timing intervals, helping users understand the behavior of scheduled tasks in their environment.