DefensiveHub

KQL Hunting Library

Ready queries for Advanced Hunting and Microsoft Sentinel. Each one comes with its technique, its table and what to do after the result.

Queries
13 queries
Last updated
November 10, 2026
Built for
SOC analysts, threat hunters and detection engineers

Before you use them: These queries are a starting point for hunting, not finished detections. Try them over a short time range first, and adjust thresholds and table names to your environment and connectors. A result is a lead to investigate, not a verdict.

Identity and cloud

From Sentinel. These tables arrive from Microsoft Entra ID and Microsoft 365 through the data connectors.

Password spray

T1110.003

One IP trying a password against many users. Code 50126 means a wrong password.

SigninLogs
| where TimeGenerated > ago(1d)
| where ResultType == "50126"
| summarize Users = dcount(UserPrincipalName), Attempts = count() by IPAddress, bin(TimeGenerated, 1h)
| where Users >= 10
| order by Users desc

After the result: Check whether any of these IPs later had a successful sign-in (ResultType 0). Tune the 10 to the size of the tenant.

SigninLogs · Entra ID sign-in logs connected to Sentinel

Successful sign-in from a new country

T1078

Compares the last day with the previous 30 days and returns users who signed in from a country they never used.

let known = SigninLogs
| where TimeGenerated between (ago(30d) .. ago(1d))
| where ResultType == "0"
| extend Country = tostring(LocationDetails.countryOrRegion)
| summarize by UserPrincipalName, Country;
SigninLogs
| where TimeGenerated > ago(1d)
| where ResultType == "0"
| extend Country = tostring(LocationDetails.countryOrRegion)
| join kind=leftanti known on UserPrincipalName, Country
| project TimeGenerated, UserPrincipalName, Country, IPAddress, AppDisplayName

After the result: Travelers and VPN users will show up. What matters: did the sign-in pass MFA, and what happened next (an inbox rule, for example)?

SigninLogs · Entra ID sign-in logs

Inbox rule that forwards or deletes

T1114.003

A new mailbox rule that forwards, redirects or deletes mail. One of the most common moves after a mailbox compromise.

OfficeActivity
| where TimeGenerated > ago(7d)
| where OfficeWorkload == "Exchange"
| where Operation in ("New-InboxRule", "Set-InboxRule", "UpdateInboxRules")
| where Parameters has_any ("ForwardTo", "ForwardAsAttachmentTo", "RedirectTo", "DeleteMessage")
| project TimeGenerated, UserId, Operation, ClientIP, Parameters

After the result: Compare ClientIP with the user's recent sign-ins. A rule from an unfamiliar IP right after a sign-in from a new country is a compromise until proven otherwise.

OfficeActivity · Office 365 connector connected to Sentinel

Email

From Advanced Hunting in Microsoft Defender XDR.

Phishing delivered and clicked

T1566.002

A phishing email reached the inbox and a recipient clicked a link inside it.

EmailEvents
| where Timestamp > ago(7d)
| where DeliveryAction == "Delivered"
| where ThreatTypes has "Phish"
| project NetworkMessageId, RecipientEmailAddress, SenderFromAddress, Subject
| join kind=inner (
    UrlClickEvents
    | where Timestamp > ago(7d)
    | where ActionType == "ClickAllowed"
    | project NetworkMessageId, ClickTime = Timestamp, Url
  ) on NetworkMessageId
| order by ClickTime desc

After the result: The users in the result are the priority: review their sign-ins and inbox rules, and revoke sessions if needed.

EmailEvents + UrlClickEvents · Needs Defender for Office 365 with Safe Links, otherwise UrlClickEvents stays empty

Endpoint

From Advanced Hunting, covering devices onboarded to Defender for Endpoint. Each can become a detection rule once you tune the false positives.

Encoded PowerShell

T1059.001

PowerShell running Base64 or -EncodedCommand. Attackers use it to hide the command.

DeviceProcessEvents
| where Timestamp > ago(1d)
| where FileName in~ ("powershell.exe", "pwsh.exe")
| where ProcessCommandLine has_any ("-enc", "-encodedcommand", "FromBase64String")
| project Timestamp, DeviceName, AccountName, InitiatingProcessFileName, ProcessCommandLine
| order by Timestamp desc

After the result: Decode the Base64 and read the real command. Check the parent process: Office or a browser launching PowerShell is not normal. Management tools can show up here as false positives.

DeviceProcessEvents · Device onboarded to Defender for Endpoint

Office app spawning a shell

T1204.002

Word, Excel or Outlook launched cmd, PowerShell or a script host. Usually an attachment with a macro.

DeviceProcessEvents
| where Timestamp > ago(1d)
| where InitiatingProcessFileName in~ ("winword.exe", "excel.exe", "powerpnt.exe", "outlook.exe")
| where FileName in~ ("cmd.exe", "powershell.exe", "wscript.exe", "cscript.exe", "mshta.exe")
| project Timestamp, DeviceName, AccountName, InitiatingProcessFileName, FileName, ProcessCommandLine

After the result: Start from the email or file opened a few minutes earlier. Very few of these are legitimate.

DeviceProcessEvents · Device onboarded to Defender for Endpoint

New scheduled task with schtasks

T1053.005

A scheduled task created from the command line. A common persistence method.

DeviceProcessEvents
| where Timestamp > ago(1d)
| where FileName =~ "schtasks.exe"
| where ProcessCommandLine has "/create"
| project Timestamp, DeviceName, AccountName, InitiatingProcessFileName, ProcessCommandLine

After the result: Check the command the task runs and its path. A task that runs a script from a temp folder or as SYSTEM deserves an investigation.

DeviceProcessEvents · Device onboarded to Defender for Endpoint

Deleting shadow copies and backups

T1490

Ransomware deletes local backups before it encrypts.

DeviceProcessEvents
| where Timestamp > ago(7d)
| where (FileName =~ "vssadmin.exe" and ProcessCommandLine has "delete" and ProcessCommandLine has "shadows")
    or (FileName =~ "wbadmin.exe" and ProcessCommandLine has "delete" and ProcessCommandLine has "catalog")
| project Timestamp, DeviceName, AccountName, InitiatingProcessFileName, ProcessCommandLine

After the result: If it appears on a device, isolate it right away. Contain first, investigate after.

DeviceProcessEvents · Device onboarded to Defender for Endpoint

certutil downloading a file

T1105

certutil is a legitimate tool, but attackers use it to download files from the internet.

DeviceProcessEvents
| where Timestamp > ago(7d)
| where FileName =~ "certutil.exe"
| where ProcessCommandLine has_any ("urlcache", "-split", "-decode")
| where ProcessCommandLine has_any ("http://", "https://")
| project Timestamp, DeviceName, AccountName, InitiatingProcessFileName, ProcessCommandLine

After the result: Review the URL and the domain, and check whether the saved file was executed.

DeviceProcessEvents · Device onboarded to Defender for Endpoint

Device logons

From Advanced Hunting. Looks at remote access and brute force.

Successful RDP from a public IP

T1021.001

Someone signed in to a device over Remote Desktop from a non-private address.

DeviceLogonEvents
| where Timestamp > ago(7d)
| where ActionType == "LogonSuccess"
| where LogonType == "RemoteInteractive"
| where isnotempty(RemoteIP)
| where ipv4_is_private(RemoteIP) == false
| project Timestamp, DeviceName, AccountName, RemoteIP

After the result: RDP exposed to the internet is a common initial access path. Link the result to failed attempts from the same IP beforehand.

DeviceLogonEvents · Device onboarded to Defender for Endpoint

Brute force against devices

T1110

One source with many failed logons against the same device.

DeviceLogonEvents
| where Timestamp > ago(1d)
| where ActionType == "LogonFailed"
| summarize Failures = count(), Accounts = dcount(AccountName) by RemoteIP, DeviceName
| where Failures >= 20
| order by Failures desc

After the result: Many accounts = spray. One account = brute force. Check for a LogonSuccess from the same IP afterwards.

DeviceLogonEvents · Device onboarded to Defender for Endpoint

Windows events

From Sentinel, the SecurityEvent table. The audit policy must be set correctly, otherwise the events are not logged.

Member added to a privileged group

T1098

Events 4728, 4732 and 4756 on Domain Admins, Administrators and similar groups.

SecurityEvent
| where TimeGenerated > ago(7d)
| where EventID in (4728, 4732, 4756)
| where TargetUserName in ("Domain Admins", "Enterprise Admins", "Schema Admins", "Administrators")
| project TimeGenerated, Computer, SubjectUserName, MemberName, TargetUserName, EventID

After the result: Compare with an approved change request. If it follows a 4720 right away, suspect a backdoor account.

SecurityEvent · Windows security events collected in Sentinel

Audit log cleared

T1070.001

Event 1102. It is almost never legitimate.

SecurityEvent
| where TimeGenerated > ago(30d)
| where EventID == 1102
| project TimeGenerated, Computer, SubjectUserName, SubjectDomainName

After the result: Treat it as a high-severity alert. Identify the account and review what happened on the device before the wipe.

SecurityEvent · Windows security events collected in Sentinel