Query Details

H02 Service Principal Lifecycle

Query

// REVIEW ONLY. Keep tenant, service-principal object ID and application object ID distinct.
// Obtain these IDs from an approved inventory or the verified AWS identity pivot.
let Start = ago(7d);
let End = now();
let EntraTenant = "REPLACE_WITH_ENTRA_TENANT_GUID";
let ServicePrincipalObjectId = "REPLACE_WITH_SERVICE_PRINCIPAL_OBJECT_GUID";
let ApplicationObjectId = "REPLACE_WITH_APPLICATION_OBJECT_GUID";
let InputsValid = assert(isnotnull(toguid(EntraTenant))
    and isnotnull(toguid(ServicePrincipalObjectId))
    and isnotnull(toguid(ApplicationObjectId)),
    "Set tenant, service-principal OBJECT ID and application OBJECT ID; do not use the client AppId in their place.");
let Lifecycle = AuditLogs
| where InputsValid and TimeGenerated between (Start .. End) and AADTenantId =~ EntraTenant
| extend Actor=todynamic(InitiatedBy)
| extend ActorObjectId=tolower(coalesce(tostring(Actor.user.id), tostring(Actor.app.servicePrincipalId))),
    ActorType=iff(isnotempty(tostring(Actor.user.id)), "User", "ServicePrincipal")
| mv-expand Target=TargetResources
| where ActorObjectId == tolower(ServicePrincipalObjectId)
    or tostring(Target.id) in~ (ServicePrincipalObjectId, ApplicationObjectId)
| summarize TargetIds=make_set(tostring(Target.id), 20), TargetTypes=make_set(tostring(Target.type), 20)
    by Time=TimeGenerated, RecordId=Id, Operation=OperationName, Result, ActorObjectId, ActorType
| project Time, Source="AuditLogs: actor or lifecycle target", RecordId, Operation, Result, ActorObjectId,
    Details=strcat("ActorType=", ActorType, "; TargetIds=", tostring(TargetIds), "; TargetTypes=", tostring(TargetTypes));
union Lifecycle,
    (AADServicePrincipalSignInLogs
    | where InputsValid and TimeGenerated between (Start .. End) and AADTenantId =~ EntraTenant
    | where ServicePrincipalId =~ ServicePrincipalObjectId
    | project Time=TimeGenerated, Source="AADServicePrincipalSignInLogs", RecordId=Id,
        Operation=ServicePrincipalName, Result=ResultType, ActorObjectId=ServicePrincipalId,
        Details=strcat("ClientAppId=", AppId, "; IP=", IPAddress, "; ResourceSP=", ResourceServicePrincipalId))
| order by Time asc
| take 5000

Explanation

This KQL (Kusto Query Language) query is designed to review and analyze audit logs and sign-in logs related to a specific service principal and application within a given Azure Active Directory (AAD) tenant. Here's a simplified breakdown of what the query does:

  1. Time Range: It looks at logs from the past 7 days up to the current time.

  2. Identifiers: It requires specific identifiers for the AAD tenant, service principal, and application. These identifiers need to be replaced with actual GUIDs (Globally Unique Identifiers).

  3. Validation: It checks that these identifiers are valid GUIDs. If not, it prompts the user to set them correctly.

  4. Audit Logs:

    • It filters the AuditLogs table for entries within the specified time range and tenant.
    • It identifies actions initiated by the specified service principal or targeting the specified service principal or application.
    • It extracts details about the actors and targets involved in these actions.
    • It summarizes and formats this information for each log entry.
  5. Sign-In Logs:

    • It also filters the AADServicePrincipalSignInLogs table for entries within the specified time range and tenant.
    • It focuses on sign-in attempts by the specified service principal.
    • It extracts details such as the client application ID, IP address, and resource service principal ID.
  6. Output:

    • The results from both the audit logs and sign-in logs are combined.
    • The combined results are sorted by time in ascending order.
    • The query limits the output to the first 5000 entries.

In summary, this query is used to review and analyze recent activities and sign-ins related to a specific service principal and application within an Azure tenant, ensuring that the identifiers are correctly set and valid.