# KQL cookbook — the queries to know by heart

KQL (Kusto Query Language) is how you ask the logs a question. A query is a table name, then a pipe `|`, then
a chain of operators that filter and shape the rows. Read it left to right: "take this table, keep these rows,
show these columns."

The queries below are the ones that recur in almost every hunt. Learn the shapes, not the specific values.

## The operators you use constantly
| Operator | Does | Example |
|---|---|---|
| `where` | keep rows that match | `where AccountName == "r.whitfield"` |
| `project` | choose columns to show | `project TimeGenerated, FileName, ProcessCommandLine` |
| `summarize` | aggregate / count | `summarize count() by DeviceName` |
| `order by` | sort | `order by TimeGenerated asc` |
| `take` / `limit` | cap rows while exploring | `take 20` |
| `extend` | add a computed column | `extend Hour = bin(TimeGenerated, 1h)` |
| `join` | enrich one table with another | process `join` network on PID |
| `distinct` | unique values | `distinct RemoteIP` |

String tests: `==` exact, `has` whole-word (fast, preferred), `contains` substring, `startswith`, `=~`
case-insensitive, `in (...)` list. Use `has` over `contains` unless you need a partial token.

## §Orientation — always run these first
```kql
// What tables have data, and how much?
union withsource=T * | summarize Rows=count() by T | order by Rows desc

// The incident window for a table
DeviceProcessEvents | summarize min(TimeGenerated), max(TimeGenerated)

// What columns does a table have?
DeviceProcessEvents | getschema

// Which hosts and accounts are active?
DeviceProcessEvents | summarize count() by DeviceName
DeviceLogonEvents   | summarize count() by AccountName
```
> In the Cyber Range the tables are often custom logs named `*_CL` (e.g. `HarrowgateDeviceProcess_CL`). The
> columns mirror the Microsoft Defender schema below. Point your `workspace` at the hunt's own workspace GUID.

## §Execution — what ran, and who launched it
```kql
// Suspicious PowerShell: hidden, encoded, or download cradles
DeviceProcessEvents
| where FileName =~ "powershell.exe"
| where ProcessCommandLine has_any ("-enc","-EncodedCommand","Hidden","Bypass","IEX","Invoke-WebRequest","DownloadString")
| project TimeGenerated, DeviceName, AccountName, InitiatingProcessFileName, ProcessCommandLine

// Parent → child chain (what spawned this?). explorer→powershell = a user clicked something.
DeviceProcessEvents
| where DeviceName == "<host>"
| project TimeGenerated, AccountName, InitiatingProcessFileName, FileName, ProcessCommandLine
| order by TimeGenerated asc

// LOLBins — living-off-the-land binaries attackers abuse
DeviceProcessEvents
| where FileName in~ ("certutil.exe","bitsadmin.exe","mshta.exe","rundll32.exe","regsvr32.exe","wmic.exe")
```
Windows Security-log equivalent: EventID `4688` (process creation, `NewProcessName` + `CommandLine`).

## §Initial access — the phishing click
```kql
// Files written to Downloads/Temp then executed (lure payloads)
DeviceFileEvents
| where FolderPath has_any (@"\Downloads", @"\Temp")
| where FileName endswith ".lnk" or FileName endswith ".zip" or FileName endswith ".exe"
| project TimeGenerated, DeviceName, InitiatingProcessAccountName, ActionType, FolderPath, FileName
```
Look for `explorer.exe → powershell.exe` and a `smartscreen.exe` nearby (a Mark-of-the-Web prompt = a
downloaded file was opened).

## §Network — beacons and C2
```kql
// Outbound connections by a suspicious process
DeviceNetworkEvents
| where InitiatingProcessFileName has_any ("powershell","rundll32","<payload>.exe")
| project TimeGenerated, DeviceName, RemoteIP, RemotePort, RemoteUrl, InitiatingProcessFileName

// Rare destinations — an IP only one host talked to (possible C2)
DeviceNetworkEvents
| summarize Hosts=dcount(DeviceName), Hits=count() by RemoteIP
| where Hosts == 1 | order by Hits desc
```
Sysmon equivalent: EventID `3` (network connect).

## §Authentication & lateral movement
```kql
// Logons by type (3=network, 10=RDP, 2=interactive) — find the pivot
SecurityEvent
| where EventID == 4624
| project TimeGenerated, Account, LogonTypeName, IpAddress, Computer
```
Key IDs: `4624` success, `4625` failure (brute force = many 4625 then one 4624), `4672` admin logon,
`4768/4769` Kerberos tickets, `4776` NTLM.

## §Persistence
```kql
// New accounts and group changes (the classic AD escalation tell)
SecurityEvent | where EventID in (4720,4722,4728,4732,4756)
| project TimeGenerated, EventID, TargetAccount, SubjectAccount

// New services (7045) and scheduled tasks (4698) = persistence
SecurityEvent | where EventID in (7045, 4698)
```
Registry run-keys: `DeviceRegistryEvents | where RegistryKey has "CurrentVersion\\Run"`.

## §Credential access & collection
```kql
// Reads of a sensitive share/file (needs object-access auditing / a SACL) → 4663
SecurityEvent
| where EventID == 4663 and ObjectName has "MemberRecords"
| project TimeGenerated, SubjectAccount, ObjectName, AccessMask
```

## The patterns that make you fast
```kql
// let = name a value or a sub-result and reuse it
let window = ago(7d);
let host = "hg-ws01";
DeviceProcessEvents | where TimeGenerated > window and DeviceName == host

// bin() = bucket time for a timeline
DeviceProcessEvents | summarize count() by bin(TimeGenerated, 1h)

// make_set / make_list = collapse many rows into one per key
DeviceNetworkEvents | summarize RemoteIPs = make_set(RemoteIP) by DeviceName

// join = tie a process to its network traffic by PID
DeviceProcessEvents | where FileName == "<payload>.exe"
| join kind=inner (DeviceNetworkEvents) on $left.ProcessId == $right.InitiatingProcessId

// anti-join / "rare" = things that appear now but not in the baseline
```

**Rule of thumb:** filter by **time** and **host** first (cheap, shrinks the data), then by behaviour, then
`project` down to the columns you'll read. A query you can't read in one glance is too wide.
