Query Details

04 Device Map Rejections

Query

// REVIEW ONLY workbook query: 💻 1.3 EC2-to-Defender Endpoint Mapping Health (`AWS_AssetIdentityMap`)
let AccountFilter = trim(" ", base64_decode_tostring("{AwsAccount:base64}"));
let UseAllAccounts = AccountFilter in ("", "*");
union isfuzzy=true
  (datatable(SearchKey:string)[]),
  (_GetWatchlist('AWS_AssetIdentityMap'))
| where isnotempty(tostring(column_ifexists("SearchKey", "")))
| extend AWSAccountId=tostring(column_ifexists("AWSAccountId", "")),
    AwsPrincipalArn=tostring(column_ifexists("AwsPrincipalArn", "")),
    AwsAccessKeyId=tostring(column_ifexists("AwsAccessKeyId", "")),
    AwsInstanceId=tostring(column_ifexists("AwsInstanceId", "")),
    DeviceId=tostring(column_ifexists("DeviceId", "")),
    XdrTenantId=tostring(column_ifexists("XdrTenantId", "")),
    Verified=tostring(column_ifexists("Verified", "")),
    MappingEvidence=tostring(column_ifexists("MappingEvidence", "")),
    From=todatetime(column_ifexists("ValidFromUtc", "")),
    To=todatetime(column_ifexists("ValidToUtc", ""))
| where UseAllAccounts or AWSAccountId == AccountFilter
| summarize Rows=count(), take_any(*) by AWSAccountId, AwsPrincipalArn, AwsAccessKeyId
| extend State=case(
    Rows != 1, "🟠 REVIEW: duplicate rows for same AWS account/ARN/key",
    tolower(Verified) != "true" or isempty(MappingEvidence), "🔴 REJECT: Verified!=true or MappingEvidence empty",
    isempty(DeviceId) or isempty(AwsInstanceId) or isempty(AwsAccessKeyId) or isnull(toguid(XdrTenantId)), "🔴 REJECT: missing DeviceId, AwsInstanceId, AwsAccessKeyId, or valid XdrTenantId GUID",
    isnull(From) or isnull(To) or To < From, "🔴 REJECT: invalid ValidFromUtc/ValidToUtc interval",
    To < now(), "🟡 EXPIRED: historical binding only",
    From > now(), "🟡 FUTURE: validity window has not started yet",
    "🟢 VALID: active & accepted by endpoint correlation rules (A03, A04, H03, H07)")
| project State, AWSAccountId, AwsInstanceId, AwsPrincipalArn, AwsAccessKeyId, DeviceId, XdrTenantId, From, To, Rows
| take 5000

Explanation

This KQL query is designed to evaluate the health of mappings between AWS EC2 instances and Microsoft Defender for Endpoint. Here's a simplified breakdown of what the query does:

  1. Account Filtering: It starts by decoding a base64-encoded AWS account filter. If the filter is empty or set to "*", it considers all accounts.

  2. Data Union: It combines data from an empty table and a watchlist named 'AWS_AssetIdentityMap'. This is done to ensure the query structure is consistent even if the watchlist is empty.

  3. Data Extraction: It extracts various fields from the data, such as AWS account ID, principal ARN, access key ID, instance ID, device ID, tenant ID, verification status, mapping evidence, and validity dates.

  4. Account Filtering: It filters the data to include only the specified AWS account unless all accounts are being considered.

  5. Data Summarization: It summarizes the data by counting rows and selecting any row for each unique combination of AWS account ID, principal ARN, and access key ID.

  6. State Evaluation: It evaluates the state of each mapping based on several conditions:

    • If there are duplicate rows for the same account/ARN/key, it flags it for review.
    • If the mapping is not verified or lacks evidence, it rejects it.
    • If essential fields like device ID or instance ID are missing, or if the tenant ID is not a valid GUID, it rejects it.
    • If the validity dates are invalid or the mapping has expired, it flags it accordingly.
    • If the validity window hasn't started yet, it flags it as future.
    • If none of the above issues are present, it marks the mapping as valid.
  7. Projection and Limitation: It projects the relevant fields and limits the output to 5000 records.

In summary, this query checks the integrity and validity of AWS-to-Defender mappings, categorizing them into different states based on specific criteria.