Query Details

H03 Aws To Xdr Alert Evidence

Query

// REVIEW ONLY. Alert membership and EvidenceRole are context, not proof an entity is malicious.
let Start = ago(1d);
let End = now();
let SeedAWSAccountId = "REPLACE_WITH_12_DIGIT_ACCOUNT";
let SeedAccessKeyId = "REPLACE_WITH_ACCESS_KEY_ID";
let XdrEntraTenantId = "REPLACE_WITH_XDR_ENTRA_TENANT_GUID";
let InputsValid = assert(SeedAWSAccountId matches regex @"^[0-9]{12}$"
    and isnotempty(SeedAccessKeyId) and SeedAccessKeyId != "REPLACE_WITH_ACCESS_KEY_ID"
    and isnotnull(toguid(XdrEntraTenantId)), "Set incident account/key and XDR Entra tenant.");
let AwsSeed = materialize(AWSCloudTrail
| where InputsValid and TimeGenerated between (Start .. End)
| where UserIdentityAccountId == SeedAWSAccountId and UserIdentityAccessKeyId == SeedAccessKeyId
| project AwsTime=TimeGenerated, AWSAccountId=UserIdentityAccountId,
    AwsAccessKeyId=UserIdentityAccessKeyId, AwsPrincipalArn=UserIdentityArn);
let Users = _GetWatchlist('AWS_EntraIdentityMap')
| where tolower(tostring(column_ifexists("Verified", ""))) == "true" and isnotempty(tostring(column_ifexists("MappingEvidence", "")))
| project AWSAccountId=tostring(column_ifexists("AWSAccountId", "")), AwsAccessKeyId=tostring(column_ifexists("AwsAccessKeyId", "")),
    AwsPrincipalArn=tostring(column_ifexists("AwsPrincipalArn", "")), AccountObjectId=tolower(tostring(column_ifexists("EntraObjectId", ""))),
    EntraTenantId=tolower(tostring(column_ifexists("EntraTenantId", ""))), IdentityType=tostring(column_ifexists("IdentityType", "")),
    ValidFrom=todatetime(column_ifexists("ValidFromUtc", "")), ValidTo=todatetime(column_ifexists("ValidToUtc", ""))
| summarize MappingRows=count(), take_any(*) by AWSAccountId, AwsAccessKeyId, AwsPrincipalArn
| where MappingRows == 1 and IdentityType == "User" and EntraTenantId == tolower(XdrEntraTenantId)
| join kind=inner AwsSeed on AWSAccountId, AwsAccessKeyId, AwsPrincipalArn
| where AwsTime between (ValidFrom .. ValidTo) and isnotempty(AccountObjectId)
| distinct AccountObjectId;
let Assets = _GetWatchlist('AWS_AssetIdentityMap')
| where tolower(tostring(column_ifexists("Verified", ""))) == "true" and isnotempty(tostring(column_ifexists("MappingEvidence", "")))
| project AWSAccountId=tostring(column_ifexists("AWSAccountId", "")), AwsPrincipalArn=tostring(column_ifexists("AwsPrincipalArn", "")), AwsAccessKeyId=tostring(column_ifexists("AwsAccessKeyId", "")),
    DeviceId=tostring(column_ifexists("DeviceId", "")), XdrTenantId=tolower(tostring(column_ifexists("XdrTenantId", ""))),
    ValidFrom=todatetime(column_ifexists("ValidFromUtc", "")), ValidTo=todatetime(column_ifexists("ValidToUtc", ""))
| summarize MappingRows=count(), take_any(*) by AWSAccountId, AwsPrincipalArn, AwsAccessKeyId
| where MappingRows == 1 and XdrTenantId == tolower(XdrEntraTenantId) and isnotempty(AwsAccessKeyId)
| join kind=inner AwsSeed on AWSAccountId, AwsPrincipalArn, AwsAccessKeyId
| where AwsTime between (ValidFrom .. ValidTo) and isnotempty(DeviceId)
| distinct DeviceId;
AlertEvidence
| where Timestamp between ((Start - 1d) .. End)
| where tolower(AccountObjectId) in (Users) or DeviceId in (Assets)
| project EvidenceTime=Timestamp, AlertId, EntityType, EvidenceRole, AccountObjectId, AccountUpn,
    DeviceId, DeviceName, RemoteIP
| join kind=inner (
    AlertInfo
    | where Timestamp between ((Start - 1d) .. End)
    | summarize arg_max(Timestamp, *) by AlertId
    | project AlertId, AlertTime=Timestamp, Title, Severity, ServiceSource
) on AlertId
| extend DefenderAlertUrl=strcat("https://security.microsoft.com/alerts/", AlertId),
    DefenderDeviceUrl=iff(isnotempty(DeviceId), strcat("https://security.microsoft.com/machines/", DeviceId), ""),
    CorrelationBasis=iff(tolower(AccountObjectId) in (Users), "Verified AWS-to-Entra user", "Verified AWS issued-key origin-to-MDE device")
| project AlertTime, EvidenceTime, AlertId, Title, Severity, ServiceSource, EntityType, EvidenceRole,
    AccountObjectId, AccountUpn, DeviceId, DeviceName, RemoteIP, CorrelationBasis, DefenderAlertUrl, DefenderDeviceUrl
| order by AlertTime desc
| take 5000

Explanation

This query is designed to analyze and correlate security alerts related to AWS accounts and access keys with Microsoft Entra (formerly Azure Active Directory) and Microsoft Defender for Endpoint (MDE) data. Here's a simplified breakdown of what the query does:

  1. Time Frame Setup: It defines a time frame from one day ago to the current time.

  2. Input Validation: It checks if the provided AWS account ID, access key ID, and Entra tenant ID are valid and not placeholders.

  3. AWS CloudTrail Data Extraction: It retrieves AWS CloudTrail logs for the specified AWS account and access key within the defined time frame.

  4. User Mapping: It fetches a list of verified AWS-to-Entra user mappings from a watchlist, ensuring that the mapping is valid and corresponds to the specified Entra tenant ID. It then joins this data with the AWS CloudTrail data to find matching user activities.

  5. Asset Mapping: Similarly, it retrieves verified AWS-to-MDE device mappings from another watchlist, ensuring the mapping is valid for the specified Entra tenant ID. It joins this data with the AWS CloudTrail data to find matching device activities.

  6. Alert Correlation: It correlates the identified users and devices with security alerts from the AlertEvidence table within the specified time frame. It joins this data with additional alert information from the AlertInfo table.

  7. Output Formatting: The query formats the output to include details such as alert time, evidence time, alert ID, title, severity, source, entity type, evidence role, account and device information, and URLs for more details in Microsoft Defender.

  8. Sorting and Limiting: Finally, it orders the results by alert time in descending order and limits the output to the top 5000 entries.

In summary, this query is used to identify and correlate security alerts related to specific AWS accounts and access keys with user and device information from Microsoft Entra and MDE, providing a comprehensive view of potential security incidents.