Query Details

H01 Aws To Entra Actor Timeline

Query

// REVIEW ONLY. Exact AWS access-key pivot; verified mappings only for Entra rows.
// Set the incident window in UTC. Required tables must exist; no fuzzy unions.
let Start = ago(1d);
let End = now();
let SeedAWSAccountId = "REPLACE_WITH_12_DIGIT_ACCOUNT";
let SeedAccessKeyId = "REPLACE_WITH_ACCESS_KEY_ID";
let InputsValid = assert(SeedAWSAccountId matches regex @"^[0-9]{12}$"
    and isnotempty(SeedAccessKeyId) and SeedAccessKeyId != "REPLACE_WITH_ACCESS_KEY_ID",
    "Set the AWS identity account and incident access key ID.");
let IdentityMap = _GetWatchlist('AWS_EntraIdentityMap')
| where tolower(tostring(column_ifexists("Verified", ""))) == "true" and isnotempty(tostring(column_ifexists("MappingEvidence", "")))
| 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", "")),
    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 AwsEvents = materialize(AWSCloudTrail
| where InputsValid and TimeGenerated between (Start .. End)
| where UserIdentityAccountId == SeedAWSAccountId and UserIdentityAccessKeyId == SeedAccessKeyId
| project Time=TimeGenerated, RecordId=AwsEventId, Operation=EventName,
    Result=iff(isempty(ErrorCode), "No top-level API error recorded", ErrorCode), IPAddress=SourceIpAddress,
    AwsAccessKeyId=UserIdentityAccessKeyId, AwsPrincipalArn=UserIdentityArn, AWSAccountId=UserIdentityAccountId);
let Subjects = AwsEvents
| join kind=inner IdentityMap on AwsAccessKeyId, AwsPrincipalArn, AWSAccountId
| where Time between (ValidFrom .. ValidTo)
| distinct EntraTenantId, EntraObjectId, IdentityType;
union
    (AwsEvents | project Time, Source="AWSCloudTrail", RecordId, Operation, Result,
        IPAddress, EntraTenantId="", EntraObjectId="", IdentityType="",
        CorrelationBasis="Exact incident access key; AWS rows remain even if identity is unmapped"),
    (union SigninLogs, AADNonInteractiveUserSignInLogs
    | where TimeGenerated between (Start .. End)
    | project Time=TimeGenerated, RecordId=Id, Operation=AppDisplayName, Result=ResultType, IPAddress,
        EntraTenantId=tolower(AADTenantId), EntraObjectId=tolower(UserId), IdentityType="User"
    | join kind=inner Subjects on EntraTenantId, EntraObjectId, IdentityType
    | project Time, Source="Entra user sign-ins", RecordId, Operation, Result, IPAddress,
        EntraTenantId, EntraObjectId, IdentityType, CorrelationBasis="Verified session-to-user mapping"),
    (AADServicePrincipalSignInLogs
    | where TimeGenerated between (Start .. End)
    | project Time=TimeGenerated, RecordId=Id, Operation=ServicePrincipalName, Result=ResultType, IPAddress,
        EntraTenantId=tolower(AADTenantId), EntraObjectId=tolower(ServicePrincipalId), IdentityType="ServicePrincipal"
    | join kind=inner Subjects on EntraTenantId, EntraObjectId, IdentityType
    | project Time, Source="Entra workload sign-ins", RecordId, Operation, Result, IPAddress,
        EntraTenantId, EntraObjectId, IdentityType, CorrelationBasis="Verified session-to-workload mapping"),
    (AuditLogs
    | where TimeGenerated between (Start .. End)
    | extend Actor=todynamic(InitiatedBy)
    | project Time=TimeGenerated, RecordId=Id, Operation=OperationName, Result,
        IPAddress=coalesce(tostring(Actor.user.ipAddress), tostring(Actor.app.ipAddress)),
        EntraTenantId=tolower(AADTenantId),
        EntraObjectId=tolower(coalesce(tostring(Actor.user.id), tostring(Actor.app.servicePrincipalId))),
        IdentityType=iff(isnotempty(tostring(Actor.user.id)), "User", "ServicePrincipal")
    | join kind=inner Subjects on EntraTenantId, EntraObjectId, IdentityType
    | project Time, Source="Entra audit actor", RecordId, Operation, Result, IPAddress,
        EntraTenantId, EntraObjectId, IdentityType, CorrelationBasis="Verified same actor; not inferred from IP")
| order by Time asc
| take 5000

Explanation

This query is designed to investigate specific AWS access key usage and correlate it with Entra identity data. Here's a simplified breakdown:

  1. Setup:

    • Define a time window for the last day (Start and End).
    • Specify the AWS account ID and access key ID to investigate (SeedAWSAccountId and SeedAccessKeyId).
    • Validate these inputs to ensure they are correctly formatted and not placeholders.
  2. Identity Mapping:

    • Retrieve a verified mapping of AWS access keys to Entra identities from a watchlist (IdentityMap).
    • Ensure that each mapping is unique and contains necessary Entra identity information.
  3. AWS Events:

    • Extract AWS CloudTrail events within the specified time window that match the given AWS account and access key.
    • Gather details like event time, operation, result, and source IP address.
  4. Subjects:

    • Identify Entra identities associated with the AWS events by joining with the identity map.
    • Ensure the events fall within the valid time range of the identity mapping.
  5. Data Union:

    • Combine AWS events with Entra sign-in logs, service principal sign-ins, and audit logs.
    • For each log type, join with the identified Entra subjects to correlate AWS activity with Entra identities.
    • Include both mapped and unmapped AWS events to ensure comprehensive coverage.
  6. Output:

    • Sort the results by time and limit the output to 5000 records.
    • Provide details such as time, source, operation, result, IP address, and correlation basis for each event.

Overall, the query aims to provide a detailed view of AWS access key usage and its correlation with Entra identities, helping to identify potential security incidents or unauthorized access.