Query Details

09 Session Identity Bridge

Query

// REVIEW ONLY workbook query: 4.1 Active AWS Credential Sessions & Entra Identity Bridge Coverage
let Start = {TimeRange:start};
let End = {TimeRange:end};
let AccountFilter = trim(" ", base64_decode_tostring("{AwsAccount:base64}"));
let UseAllAccounts = AccountFilter in ("", "*");
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);
SafeCloudTrail
| where UseAllAccounts or UserIdentityAccountId == AccountFilter or RecipientAccountId == AccountFilter
| where isnotempty(UserIdentityAccessKeyId) and isnotempty(UserIdentityArn)
| summarize FirstObserved=min(TimeGenerated), LastObserved=max(TimeGenerated), Events=count(),
    ResourceAccounts=make_set(RecipientAccountId, 100), Regions=make_set(AWSRegion, 25)
    by AWSAccountId=UserIdentityAccountId, AwsAccessKeyId=UserIdentityAccessKeyId, AwsPrincipalArn=UserIdentityArn
| join kind=leftouter IdentityMap on AWSAccountId, AwsAccessKeyId, AwsPrincipalArn
| extend BridgeState=case(
    isempty(EntraObjectId), "🟠 UNMAPPED: no verified AWS_EntraIdentityMap entry",
    FirstObserved < ValidFrom or LastObserved > ValidTo, "🟡 PARTIAL: activity extends outside watchlist [ValidFrom .. ValidTo]",
    "🟢 VERIFIED: AWS session mapped to Entra identity")
| project BridgeState, AWSAccountId, AwsAccessKeyId, AwsPrincipalArn, Events, FirstObserved, LastObserved,
    ResourceAccounts, Regions, IdentityType, EntraTenantId, EntraObjectId, ValidFrom, ValidTo
| order by Events desc
| take 5000

Explanation

This query is designed to analyze AWS credential sessions and their mapping to Entra identities. Here's a simplified breakdown of what it does:

  1. Time Range and Account Filter Setup:

    • It sets a time range (Start and End) for the data to be analyzed.
    • It decodes a base64 string to filter for a specific AWS account, unless all accounts are to be used.
  2. Data Collection from AWS CloudTrail:

    • It gathers AWS CloudTrail logs within the specified time range, focusing on events related to AWS credentials and user identities.
  3. Identity Mapping:

    • It retrieves a watchlist of AWS to Entra identity mappings.
    • Filters this list to include only verified mappings with evidence, and summarizes the data to ensure each mapping is unique.
  4. Session Analysis:

    • It filters the CloudTrail data to include only relevant AWS accounts and sessions with valid access keys and ARNs.
    • It summarizes the data to find the first and last observed events, count of events, and the regions and accounts involved.
  5. Mapping Verification:

    • It joins the session data with the identity mapping to check if AWS sessions are mapped to Entra identities.
    • It categorizes the mapping status into three states:
      • "🟠 UNMAPPED": No verified mapping found.
      • "🟡 PARTIAL": Activity occurs outside the valid mapping period.
      • "🟢 VERIFIED": Successfully mapped to an Entra identity.
  6. Output:

    • It projects relevant fields and orders the results by the number of events, showing up to 5000 records.

In essence, this query helps identify and verify active AWS credential sessions and their association with Entra identities, highlighting any discrepancies or unmapped sessions.