Before you run
Data & scope
- Required tables
- Time window
- 3-day scan; credential changes over 48 hours with a 2-hour follow-up window
- Microsoft Entra ID connector collecting AuditLogs and AADServicePrincipalSignInLogs into the same workspace for at least three days.
- Microsoft Entra ID P1 or P2 for sign-in-log ingestion; Analytics table plans and query access supporting the cross-table join.
Documentation review by Codex (AI). No live-tenant execution or detection-performance claim. Validate schema, data coverage and expected results in your workspace.
// Hunting starter. Documentation checked 2026-11-13; not tenant-tested.
let followup = 2h;
let changeEnd = now() - followup;
let changeStart = changeEnd - 2d;
let changes =
AuditLogs
| where TimeGenerated >= ago(3d) and TimeGenerated <= now()
| extend AuditOperation = coalesce(ActivityDisplayName, OperationName)
| where AuditOperation =~ "Add service principal credentials" and Result =~ "success"
| extend ChangeTime = coalesce(ActivityDateTime, TimeGenerated)
| where ChangeTime >= changeStart and ChangeTime < changeEnd
| mv-expand Target = TargetResources
| where tostring(Target.type) =~ "ServicePrincipal"
| extend DirectoryId = tolower(AADTenantId),
PrincipalObjectId = tolower(tostring(Target.id))
| where isnotempty(DirectoryId) and isnotempty(PrincipalObjectId)
| project DirectoryId, PrincipalObjectId, ChangeTime, AuditId = Id,
ChangedPrincipalName = tostring(Target.displayName),
ActorObjectId = coalesce(tostring(InitiatedBy.user.id),
tostring(InitiatedBy.app.servicePrincipalId)),
ChangedProperties = Target.modifiedProperties,
AuditCorrelationId = CorrelationId;
let signins =
AADServicePrincipalSignInLogs
| where TimeGenerated >= ago(3d) and TimeGenerated <= now()
| where ResultType in~ ("0", "Success")
| extend SignInTime = coalesce(CreatedDateTime, TimeGenerated),
DirectoryId = tolower(AADTenantId), PrincipalObjectId = tolower(ServicePrincipalId)
| where SignInTime >= changeStart and SignInTime <= now()
| where isnotempty(DirectoryId) and isnotempty(PrincipalObjectId)
| project DirectoryId, PrincipalObjectId, SignInTime, SignInId = Id,
AppId, IPAddress, ResourceDisplayName, ClientCredentialType,
ServicePrincipalCredentialKeyId, SignInCorrelationId = CorrelationId;
changes
| join kind=inner signins on DirectoryId, PrincipalObjectId
| where SignInTime >= ChangeTime and SignInTime < ChangeTime + followup
| extend MinutesAfterChange = datetime_diff("minute", SignInTime, ChangeTime)
| project ChangeTime, SignInTime, MinutesAfterChange, DirectoryId,
PrincipalObjectId, ChangedPrincipalName, ActorObjectId, AppId,
IPAddress, ResourceDisplayName, ClientCredentialType,
ServicePrincipalCredentialKeyId, AuditId, SignInId,
AuditCorrelationId, SignInCorrelationId, ChangedProperties
| order by SignInTime desc, ChangeTime desc
| take 200
Read-only query. Run in your own authorized workspace after checking prerequisites.
This hunt asks whether a service principal successfully signed in within two hours of a recorded credential addition. The result is a temporal association worth investigating, not evidence that the added credential was used or that the activity was unauthorized. The distinction matters during routine rotations, when an old credential and a new credential can both remain usable.
Join the correct identities
The audit side requires the target type to be ServicePrincipal. Its target object ID is joined to ServicePrincipalId in the sign-in table, with the Entra directory ID included in both keys. Empty keys are excluded to prevent unrelated unidentified events from matching.
An Application target is deliberately excluded. An application object ID, its client/app ID and a tenant’s service-principal object ID are different identifiers. Supporting application-registration credential changes requires an authoritative application-to-service-principal mapping; matching names or substituting AppId would manufacture a relationship. Application and service-principal model.
The sign-in filter accepts the numeric success representation used in Microsoft’s query examples and the Success label described by the table reference. Confirm the values present in your workspace. The result retains the sign-in credential key ID and credential type for follow-up. A populated key ID is useful evidence, but this query does not parse the audit credential list or claim an exact key match. Sign-in fields.
Interpret the time relationship
Credential changes are selected from a 48-hour interval ending two hours before the query runs. That allows a full follow-up interval, although delayed ingestion can still leave it incomplete. Both tables have a separate three-day scan bound. The correlation uses the source activity timestamps where available, falling back to TimeGenerated.
An explicit inner join preserves matching pairs. One sign-in can follow multiple credential changes, and one change can precede many sign-ins. Those are separate candidate relationships, not independent compromises. Keep both event IDs when reviewing or deduplicating an investigation. The two correlation IDs belong to their respective transactions; equality between them is not required.
Investigate and tune
Start with the initiating actor, the service principal’s owner and the approved rotation schedule. Compare the sign-in IP, target resource and credential type with the workload’s normal deployment environment. Resolve the returned credential key ID against the relevant credential metadata, and inspect the old/new audit values before asserting that the credential addition enabled the sign-in.
Prioritize an unexplained credential change followed by unfamiliar resource access or infrastructure over a healthy post-rotation authentication. Routine renewal tests, deployment pipelines and disaster-recovery exercises are common benign explanations. Conversely, expected sign-in locations do not establish that the credential change was authorized.
The two-hour follow-up is an investigation parameter, not a universal attack timing rule. A longer window increases unrelated matches and join cost. The latest 200 pairs are a result cap, not a cap on scanned or joined rows. Narrow the directory or time range when volumes are high.
This read-only query was documentation-checked and has not been tenant-tested. Validate an authorized service-principal credential addition, an unrelated Application target, and a later sign-in outside the follow-up window. No result cannot establish safety: credential use may occur later, in a missing log source, or under an application-object change excluded by design.
Verify the source
Microsoft references
- Microsoft Learn — AuditLogs table schema ↗
- Microsoft Learn — AADServicePrincipalSignInLogs schema ↗
- Microsoft Learn — Service principal sign-in query examples ↗
- Microsoft Learn — Application and service principal objects ↗
- Microsoft Learn — Microsoft Graph targetResource ↗
- Microsoft Learn — Microsoft Entra ID connector requirements ↗
- Microsoft Learn — Kusto inner join ↗
Measure normal activity, validate known test cases, account for ingestion delays, choose a threshold and entity mapping, and assign an owner. Results require investigation.
Follow the detection learning path →