Query Details
let query_frequency = 1h;
let query_period = 14d;
let _ExcludedResultTypes = toscalar(
_GetWatchlist("ResultType-SignInLogsErrorCodes")
| where Notes has_any ("[Success-App]", "[Failure-App]") and not(Notes has_all ("[ClientSecret]", "[Invalid]"))
| summarize make_list(ResultType)
);
let _ExcludedIPAddresses = toscalar(
union
(
_GetWatchlist("IP-Vendors")
| where Notes has_any ("[HomeTenant]", "[Proxy]")
),
(
_GetWatchlist("IP-CorporateCollaborators")
| where Notes has_any ("[Egress]")
)
| summarize make_list(IPAddress)
);
AADServicePrincipalSignInLogs
| where TimeGenerated > ago(query_period)
| where not(ResultType in (_ExcludedResultTypes)) and not(ipv4_is_in_any_range(IPAddress, _ExcludedIPAddresses))
| summarize arg_min(TimeGenerated, *) by ResultType, ServicePrincipalId, AppId, ResourceIdentity, IPAddress //parse_ipv6_mask(IPAddress, 120)
| where TimeGenerated > ago(query_frequency)
| project
TimeGenerated,
CreatedDateTime,
ServicePrincipalName,
ResourceDisplayName,
IPAddress,
Location,
ResultType,
ConditionalAccessStatus,
ConditionalAccessPolicies,
AuthenticationProcessingDetails,
ClientCredentialType,
ServicePrincipalId,
AppId,
ResourceIdentity,
ResourceServicePrincipalId,
ServicePrincipalCredentialKeyId,
ServicePrincipalCredentialThumbprint,
Id,
CorrelationId
This query is designed to analyze Azure Active Directory (AAD) service principal sign-in logs over a specified period, while excluding certain result types and IP addresses. Here's a simplified breakdown:
Define Timeframes:
query_frequency is set to 1 hour, meaning the query focuses on the most recent hour of data.query_period is set to 14 days, indicating the overall time range for the data being analyzed.Exclude Specific Result Types:
Exclude Specific IP Addresses:
Filter Sign-In Logs:
AADServicePrincipalSignInLogs table is filtered to include only entries from the last 14 days.Summarize Data:
arg_min) occurrence of each combination of ResultType, ServicePrincipalId, AppId, ResourceIdentity, and IPAddress.Recent Data Focus:
Select Specific Fields:
In essence, this query is used to monitor and analyze recent AAD service principal sign-in activities, excluding certain known result types and IP addresses, and focusing on the most recent occurrences within the last hour.

Jose Sebastián Canós
Released: February 24, 2025
Tables
Keywords
Operators