☼
⌃
☰

Kusto Query Language

Table of Contents

Kusto Query Language — the primary query language for Microsoft Sentinel, Azure Log Analytics, and Azure Data Explorer. Queries are pipelines: data flows from a table through a sequence of operators separated by |. Each operator filters or transforms the dataset before passing it to the next.

Related: Microsoft Sentinel Windows Event Logs

Core Operators

Operator Purpose Example
where Filter rows =\ where EventID = 4625
project Select/rename columns =\ project TimeGenerated, AccountName=
extend Add computed column =\ extend Hour = bin(TimeGenerated, 1h)=
summarize Aggregate =\ summarize count() by AccountName=
order by Sort results =\ order by TimeGenerated desc=
limit / take Return N rows =\ limit 100=
distinct Unique values =\ distinct AccountName=
join Combine tables =\ join kind=inner (…)=
union Combine rows from tables =\ union SecurityEvent, Syslog=
parse Extract structured data from string =\ parse Message with …=
mv-expand Expand array/dynamic field to rows =\ mv-expand ParsedFields=
render Visualise output =\ render timechart=

Time Functions

KQL
// Relative time
| where TimeGenerated > ago(1h)
| where TimeGenerated > ago(24h)
| where TimeGenerated between (ago(7d) .. now())

// Bin into intervals (for time charts)
| summarize count() by bin(TimeGenerated, 1h)
| summarize count() by bin(TimeGenerated, 15m)

String Operators

KQL
// Case-insensitive contains (most common)
| where AccountName contains "admin"

// Case-sensitive
| where AccountName contains_cs "Admin"

// Starts with / ends with
| where IpAddress startswith "192.168"

// Regex
| where AccountName matches regex @"^svc_"

// In list
| where EventID in (4624, 4625, 4648, 4672)

// Not in list
| where EventID !in (4624, 4634)

// Has (token-based — faster than contains for words)
| where Message has "failed"
| where Message has_all ("failed", "login")
| where Message has_any ("admin", "administrator")

Let Statements

Define variables and sub-queries for reuse:

KQL
// Variable
let threshold = 10;
let lookback = 24h;
SecurityEvent
| where TimeGenerated > ago(lookback)
| where EventID == 4625
| summarize FailedAttempts = count() by AccountName
| where FailedAttempts > threshold

// Reusable sub-query
let FailedLogins = SecurityEvent
    | where EventID == 4625
    | summarize FailedCount = count() by AccountName;
FailedLogins
| where FailedCount > 10
| order by FailedCount desc

Security Examples

Failed logon enumeration

KQL
SecurityEvent
| where TimeGenerated > ago(1h)
| where EventID == 4625
| summarize FailedAttempts = count() by AccountName, IpAddress
| where FailedAttempts > 5
| order by FailedAttempts desc

Successful logon after repeated failures (brute force success)

KQL
let FailedAccounts =
    SecurityEvent
    | where TimeGenerated > ago(1h)
    | where EventID == 4625
    | summarize FailedAttempts = count() by AccountName
    | where FailedAttempts > 10;
SecurityEvent
| where TimeGenerated > ago(1h)
| where EventID == 4624
| join kind=inner FailedAccounts on AccountName
| project TimeGenerated, AccountName, IpAddress, FailedAttempts

Privilege escalation — special privileges assigned

KQL
SecurityEvent
| where TimeGenerated > ago(24h)
| where EventID == 4672          // Special privileges assigned to logon
| where AccountName !endswith "$" // Exclude machine accounts
| project TimeGenerated, AccountName, PrivilegeList
| order by TimeGenerated desc

New local admin account created

KQL
SecurityEvent
| where TimeGenerated > ago(24h)
| where EventID == 4732          // Member added to security-enabled local group
| where TargetUserName == "Administrators"
| project TimeGenerated, AccountName, MemberName

Scheduled task creation

KQL
SecurityEvent
| where TimeGenerated > ago(24h)
| where EventID == 4698          // Scheduled task created
| project TimeGenerated, AccountName, TaskName, TaskContent

Outbound connections from unusual processes (requires Sysmon)

KQL
SysmonEvent
| where TimeGenerated > ago(1h)
| where EventID == 3             // Network connection
| where InitiatingProcessName !in ("chrome.exe","msedge.exe","svchost.exe")
| project TimeGenerated, InitiatingProcessName, DestinationIp, DestinationPort
| order by TimeGenerated desc

Lateral movement — network logons

KQL
SecurityEvent
| where TimeGenerated > ago(24h)
| where EventID == 4624
| where LogonType == 3           // Network logon
| where AccountName !endswith "$"
| summarize count() by AccountName, IpAddress, Computer
| order by count_ desc

Cross-table correlation — join security events with sign-in logs

KQL
SecurityEvent
| where TimeGenerated > ago(1h)
| where EventID == 4776          // Credential validation
| project TimeGenerated, AccountName, IpAddress
| join kind=inner (
    SigninLogs
    | where TimeGenerated > ago(1h)
    | where ResultType == 50126  // Possible MFA bypass
    | project SigninTime = TimeGenerated, UserPrincipalName
) on $left.AccountName == $right.UserPrincipalName
| extend TimeDiff = abs(TimeGenerated - SigninTime)
| where TimeDiff < 1m

Traffic spike detection (time chart)

KQL
AzureDiagnostics
| where ResourceProvider == "MICROSOFT.NETWORK"
| where Category == "AzureFirewallApplicationRule"
| summarize TotalRequests = count() by bin(TimeGenerated, 1h)
| render timechart

Tips

  • has is faster than contains for searching word tokens in large tables — prefer it when searching for complete words
  • ago() is relative to query execution time — use between (datetime(...) .. datetime(...)) for fixed ranges
  • let statements run once and are reused — essential for readable multi-step threat hunting queries
  • project-away removes columns instead of selecting them — useful when you want all columns except a few noisy ones
  • When joining, specify kind= explicitly: inner, leftouter, fullouter — the default is innerunique which deduplicates the left side, which is often not what you want
  • render timechart only works in the Sentinel/Log Analytics portal — it has no effect in API or programmatic queries