Query Details

16 H02 Workload Lifecycle

Query

// REVIEW ONLY workbook query: 5.2 H02 — Entra Service Principal & Application Credential / Owner / Consent / Role Lifecycle
let Start = {TimeRange:start};
let End = {TimeRange:end};
let TenantFilter = tolower(trim(" ", base64_decode_tostring("{XdrTenantId:base64}")));
let SpObjFilter = tolower(trim(" ", base64_decode_tostring("{ServicePrincipalObjectId:base64}")));
let AppObjFilter = tolower(trim(" ", base64_decode_tostring("{ApplicationObjectId:base64}")));
union isfuzzy=true
  (datatable(TimeGenerated:datetime, AADTenantId:string, OperationName:string, Category:string, Result:string, InitiatedBy:dynamic, TargetResources:dynamic, CorrelationId:string)[]),
  (AuditLogs
   | where TimeGenerated between (Start .. End)
   | project TimeGenerated, AADTenantId, OperationName, Category, Result, InitiatedBy=todynamic(InitiatedBy), TargetResources=todynamic(TargetResources), CorrelationId)
| where isempty(TenantFilter) or tolower(AADTenantId) == TenantFilter
| where Category in~ ("ApplicationManagement", "RoleManagement", "DirectoryManagement")
    or OperationName has_any ("service principal", "application", "credential", "certificate", "secret", "owner", "consent", "role")
| mv-expand Target = TargetResources
| extend TargetId = tolower(tostring(Target.id)), TargetName = tostring(Target.displayName), TargetType = tostring(Target.type)
| where (isempty(SpObjFilter) and isempty(AppObjFilter))
    or TargetId == SpObjFilter or TargetId == AppObjFilter
| extend ActorUpn = tostring(InitiatedBy.user.userPrincipalName),
    ActorSp = tostring(InitiatedBy.servicePrincipal.displayName),
    ActorIp = tostring(InitiatedBy.user.ipAddress),
    ModifiedProps = tostring(Target.modifiedProperties)
| extend Actor = coalesce(iff(isnotempty(ActorUpn), ActorUpn, ""), ActorSp, "System/Unknown")
| project TimeGenerated, OperationName, Result, Actor, ActorIp, TargetType, TargetName, TargetId, ModifiedProps, CorrelationId
| order by TimeGenerated desc
| take 5000

Explanation

This KQL (Kusto Query Language) query is designed to analyze and review activities related to service principals and applications within Microsoft's Entra (Azure Active Directory) environment. Here's a simplified breakdown of what the query does:

  1. Time Range and Filters:

    • It sets a time range (Start and End) for the data to be analyzed.
    • It decodes and trims tenant and object IDs from base64 strings to use as filters (TenantFilter, SpObjFilter, AppObjFilter).
  2. Data Source:

    • It pulls data from AuditLogs and combines it with an empty datatable (likely for structure).
    • The query looks for logs generated within the specified time range.
  3. Filtering:

    • It filters logs to include only those related to "ApplicationManagement", "RoleManagement", or "DirectoryManagement".
    • It also includes logs with operations involving "service principal", "application", "credential", "certificate", "secret", "owner", "consent", or "role".
  4. Target Resource Expansion:

    • It expands the TargetResources field to analyze each target individually.
    • It extracts and standardizes the target ID, name, and type.
  5. Additional Filtering:

    • It applies additional filters based on the decoded service principal or application object IDs.
    • If no specific object IDs are provided, it includes all relevant entries.
  6. Actor Information:

    • It extracts information about who initiated the action, including user principal name, service principal display name, and IP address.
    • It determines the actor's identity, defaulting to "System/Unknown" if not available.
  7. Projection and Sorting:

    • It selects specific fields to display: time of the event, operation name, result, actor details, target details, modified properties, and correlation ID.
    • It sorts the results by the time the event was generated, in descending order.
  8. Limiting Results:

    • It limits the output to the most recent 5000 entries.

In summary, this query is used to review and analyze lifecycle events related to service principals and applications in Azure AD, focusing on changes and actions taken, who initiated them, and their outcomes, within a specified time frame.