Service principal credential addition followed by immediate sign-in
Description
Identifies service principal sign-ins occurring within 30 minutes of a credential addition to the same service principal. The tight correlation is consistent with post-compromise staging where credentials are added and used immediately.
Query · kql
let timeframe = 1d;
let correlationWindow = 30m;
let CredAdded =
AuditLogs
| where TimeGenerated >= ago(timeframe)
| where OperationName in~ (
"Add service principal credentials"
)
| where Result =~ "success"
| project
CredAddedTime = TimeGenerated,
SpId = tostring(TargetResources[0].id),
SpName = tostring(TargetResources[0].displayName),
ActorUpn = tostring(InitiatedBy.user.userPrincipalName),
ActorApp = tostring(InitiatedBy.app.displayName),
AuditCorrelationId = CorrelationId;
AADServicePrincipalSignInLogs
| where TimeGenerated >= ago(timeframe)
| where ResultType == 0
| join kind=inner CredAdded on $left.ServicePrincipalId == $right.SpId
| where TimeGenerated between (CredAddedTime .. (CredAddedTime + correlationWindow))
| extend Actor = iff(isnotempty(ActorUpn), ActorUpn, ActorApp)
| project
TimeGenerated,
ServicePrincipalId,
ServicePrincipalName = SpName,
AppId,
IPAddress,
Location,
ResourceDisplayName,
CredAddedTime,
Actor,
AuditCorrelationId,
SignInCorrelationId = CorrelationId
| sort by TimeGenerated desc