Query Details

15 H01 Actor Timeline

Query

// REVIEW ONLY workbook query: 5.1 H01 — Unified AWS CloudTrail & Microsoft Entra Actor Timeline
let Start = {TimeRange:start};
let End = {TimeRange:end};
let AccountFilter = trim(" ", base64_decode_tostring("{AwsAccount:base64}"));
let UseAllAccounts = AccountFilter in ("", "*");
let SeedKey = trim(" ", base64_decode_tostring("{AccessKeyId:base64}"));
let SafeCloudTrail = union isfuzzy=true
  (datatable(TimeGenerated:datetime, AwsEventId:string, EventSource:string, EventName:string, AWSRegion:string,
    RecipientAccountId:string, UserIdentityAccountId:string, UserIdentityType:string, UserIdentityArn:string,
    UserIdentityAccessKeyId:string, SourceIpAddress:string, UserAgent:string,
    ErrorCode:string, ErrorMessage:string, RequestParameters:string, ResponseElements:string, Resources:string,
    ManagementEvent:bool, ReadOnly:bool, SessionMfaAuthenticated:string)[]),
  (AWSCloudTrail
   | where TimeGenerated between (Start .. End)
   | project TimeGenerated, AwsEventId, EventSource, EventName, AWSRegion, RecipientAccountId,
       UserIdentityAccountId, UserIdentityType, UserIdentityArn,
       UserIdentityAccessKeyId, SourceIpAddress, UserAgent, ErrorCode, ErrorMessage,
       RequestParameters, ResponseElements, Resources, ManagementEvent, ReadOnly, SessionMfaAuthenticated);
let IdentityMap = union isfuzzy=true
  (datatable(SearchKey:string)[]),
  (_GetWatchlist('AWS_EntraIdentityMap'))
| where isnotempty(tostring(column_ifexists("SearchKey", "")))
| extend VerifiedVal = tolower(tostring(column_ifexists("Verified", ""))),
    MappingEvidenceVal = tostring(column_ifexists("MappingEvidence", ""))
| where VerifiedVal == "true" and isnotempty(MappingEvidenceVal)
| project AwsAccessKeyId=tostring(column_ifexists("AwsAccessKeyId", "")),
    AwsPrincipalArn=tostring(column_ifexists("AwsPrincipalArn", "")),
    AWSAccountId=tostring(column_ifexists("AWSAccountId", "")),
    EntraTenantId=tolower(tostring(column_ifexists("EntraTenantId", ""))),
    EntraObjectId=tolower(tostring(column_ifexists("EntraObjectId", ""))),
    IdentityType=tostring(column_ifexists("IdentityType", "")),
    AppClientId=tolower(tostring(column_ifexists("AppClientId", ""))),
    ValidFrom=todatetime(column_ifexists("ValidFromUtc", "")),
    ValidTo=todatetime(column_ifexists("ValidToUtc", ""))
| summarize MappingRows=count(), take_any(*) by AwsAccessKeyId, AwsPrincipalArn, AWSAccountId
| where MappingRows == 1 and isnotempty(EntraTenantId) and isnotempty(EntraObjectId);
let ScopedAws = SafeCloudTrail
| where (UseAllAccounts or UserIdentityAccountId == AccountFilter or RecipientAccountId == AccountFilter)
| where (isempty(SeedKey) or UserIdentityAccessKeyId == SeedKey)
| where isnotempty(UserIdentityAccessKeyId) and isnotempty(UserIdentityArn)
| project AwsTime=TimeGenerated, AwsEventId, EventSource, EventName, AWSRegion,
    AWSAccountId=UserIdentityAccountId, RecipientAccountId, AwsAccessKeyId=UserIdentityAccessKeyId,
    AwsPrincipalArn=UserIdentityArn, AwsIp=SourceIpAddress, ErrorCode
| join kind=inner IdentityMap on AWSAccountId, AwsAccessKeyId, AwsPrincipalArn
| where AwsTime between (ValidFrom .. ValidTo);
let MappedIds = ScopedAws | distinct EntraTenantId, EntraObjectId;
let EntraEvents = union isfuzzy=true
  (datatable(EventTime:datetime, Plane:string, EntraTenantId:string, EntraObjectId:string, ActionOrApp:string, SourceIp:string, Result:string, Detail:string)[]),
  (SigninLogs
   | where TimeGenerated between (Start .. End)
   | project EventTime=TimeGenerated, Plane="Entra:SigninLogs", EntraTenantId=tolower(AADTenantId),
       EntraObjectId=tolower(UserId), ActionOrApp=AppDisplayName, SourceIp=IPAddress,
       Result=ResultType, Detail=strcat("Risk=", RiskLevelDuringSignIn)),
  (AADNonInteractiveUserSignInLogs
   | where TimeGenerated between (Start .. End)
   | project EventTime=TimeGenerated, Plane="Entra:NonInteractiveSignIn", EntraTenantId=tolower(AADTenantId),
       EntraObjectId=tolower(UserId), ActionOrApp=AppDisplayName, SourceIp=IPAddress,
       Result=ResultType, Detail=strcat("Risk=", RiskLevelDuringSignIn)),
  (AADServicePrincipalSignInLogs
   | where TimeGenerated between (Start .. End)
   | project EventTime=TimeGenerated, Plane="Entra:ServicePrincipalSignIn", EntraTenantId=tolower(AADTenantId),
       EntraObjectId=tolower(ServicePrincipalId), ActionOrApp=ServicePrincipalName, SourceIp=IPAddress,
       Result=ResultType, Detail=strcat("AppId=", AppId)),
  (AuditLogs
   | where TimeGenerated between (Start .. End)
   | extend ActorId=coalesce(
       iff(isnotempty(tostring(parse_json(tostring(InitiatedBy.user)).id)), tolower(tostring(parse_json(tostring(InitiatedBy.user)).id)), ""),
       tolower(tostring(parse_json(tostring(InitiatedBy.servicePrincipal)).id)))
   | where isnotempty(ActorId)
   | project EventTime=TimeGenerated, Plane="Entra:AuditLogs", EntraTenantId=tolower(AADTenantId),
       EntraObjectId=ActorId, ActionOrApp=OperationName,
       SourceIp=tostring(parse_json(tostring(InitiatedBy.user)).ipAddress),
       Result=Result, Detail=Category)
| join kind=inner MappedIds on EntraTenantId, EntraObjectId
| project TimeGenerated=EventTime, Plane, EntraTenantId, EntraObjectId, AwsPrincipalArn="", AwsAccessKeyId="",
    ActionOrApp, SourceIp, Result, Detail;
let AwsTimeline = ScopedAws
| project TimeGenerated=AwsTime, Plane=strcat("AWS:", EventSource), EntraTenantId, EntraObjectId,
    AwsPrincipalArn, AwsAccessKeyId, ActionOrApp=EventName, SourceIp=AwsIp,
    Result=iff(isempty(ErrorCode), "Success", ErrorCode), Detail=strcat("Acct=", RecipientAccountId, " Region=", AWSRegion);
union AwsTimeline, EntraEvents
| order by TimeGenerated desc
| take 5000

Explanation

This query is designed to create a timeline of activities involving AWS CloudTrail and Microsoft Entra (formerly Azure Active Directory) actors. 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 filters for AWS account and access key ID.
  2. Data Collection:

    • AWS CloudTrail Data: It gathers AWS CloudTrail logs within the specified time range, focusing on events related to user identity and access.
    • Identity Mapping: It retrieves a mapping of AWS access keys to Microsoft Entra identities, ensuring that the mapping is verified and valid.
  3. Data Filtering and Joining:

    • It filters AWS CloudTrail logs based on account and access key filters, ensuring only relevant events are considered.
    • It joins the filtered AWS data with the identity mapping to associate AWS events with Microsoft Entra identities.
  4. Microsoft Entra Events:

    • It collects various Microsoft Entra logs (Signin, Non-Interactive SignIn, Service Principal SignIn, and Audit Logs) within the specified time range.
    • It filters these logs to include only those that match the mapped identities from the AWS data.
  5. Timeline Creation:

    • It creates two timelines: one for AWS events and another for Microsoft Entra events.
    • Each event is characterized by details such as time, source, action, result, and additional details.
  6. Union and Sorting:

    • It combines the AWS and Microsoft Entra timelines into a single dataset.
    • The combined events are sorted by time in descending order, and the query returns up to 5000 of the most recent events.

In summary, this query is used to correlate and visualize activities across AWS and Microsoft Entra, providing a unified view of actions taken by users and services in both environments within a specified time frame.