P0 06b Guardduty Multitable Correlated
Query
// Standalone KQL for Analytic Rule: AWS SecOps P0-06b - GuardDuty Correlated with CloudTrail Mutations, VPC Egress and Entra Bridge
// Rule File: analytics/P0-06b-guardduty-multitable-correlated.yaml | Rule ID: REPLACE_WITH_RULE_GUID | Severity: High
// Schedule: 15m / Lookback: 2h | REVIEW ONLY - Do not deploy/schedule without approval.
// REVIEW ONLY. Validate AWSGuardDuty, AWSCloudTrail, and AWSVPCFlow schemas first via D02-D04.
let IdentityMap = _GetWatchlist('AWS_EntraIdentityMap')
| where tolower(tostring(column_ifexists("Verified", ""))) == "true" and isnotempty(tostring(column_ifexists("MappingEvidence", "")))
| project AwsAccessKeyId=tostring(column_ifexists("AwsAccessKeyId", "")), AWSAccountId=tostring(column_ifexists("AWSAccountId", "")),
EntraTenantId=tolower(tostring(column_ifexists("EntraTenantId", ""))), EntraObjectId=tolower(tostring(column_ifexists("EntraObjectId", ""))),
EntraUPN=tostring(column_ifexists("EntraUPN", ""));
let Findings = AWSGuardDuty
| where TimeGenerated > ago(1h)
| summarize arg_max(TimeGenerated, *) by Id
| where ingestion_time() > ago(15m)
| where tobool(ServiceDetails.archived) != true and Severity >= 7
| extend ActionBlock = ServiceDetails.action,
AwsAccessKeyId = tostring(ResourceDetails.accessKeyDetails.accessKeyId),
PrincipalName = tostring(ResourceDetails.accessKeyDetails.userName),
InstanceId = tostring(ResourceDetails.instanceDetails.instanceId)
| extend RemoteIpAddress = coalesce(
tostring(ActionBlock.awsApiCallAction.remoteIpDetails.ipAddressV4),
tostring(ActionBlock.networkConnectionAction.remoteIpDetails.ipAddressV4), "")
| project FindingTime=TimeGenerated, FindingId=Id, AWSAccountId=AccountId, AWSRegion=Region,
Severity, ActivityType, Title, AwsAccessKeyId, PrincipalName, InstanceId, RemoteIpAddress;
let RecentMutations = AWSCloudTrail
| where TimeGenerated > ago(2h) and ReadOnly == false and isempty(ErrorCode)
| summarize ControlPlaneChanges=make_set(EventName, 20), MutationCount=count()
by AWSAccountId=RecipientAccountId, AwsAccessKeyId=UserIdentityAccessKeyId;
let InstanceFlows = AWSVPCFlow
| where TimeGenerated > ago(2h) and Action == "ACCEPT" and isnotempty(InstanceId)
| summarize VpcAcceptedBytes=sum(Bytes), DistinctRemotePeers=dcount(DstAddr)
by AWSAccountId=AccountId, InstanceId;
Findings
| join kind=leftouter RecentMutations on AWSAccountId, AwsAccessKeyId
| join kind=leftouter InstanceFlows on AWSAccountId, InstanceId
| join kind=leftouter IdentityMap on AWSAccountId, AwsAccessKeyId
| where coalesce(MutationCount, 0) > 0 or coalesce(VpcAcceptedBytes, 0) > 10485760 or isnotempty(EntraObjectId)
| extend ControlPlaneSummary = tostring(coalesce(ControlPlaneChanges, dynamic([]))),
VpcBytesStr = tostring(coalesce(VpcAcceptedBytes, 0))
| project TimeGenerated=FindingTime, FindingId, AWSAccountId, AWSRegion, Severity, ActivityType,
Title, PrincipalName, AwsAccessKeyId, InstanceId, RemoteIpAddress, MutationCount=coalesce(MutationCount, 0),
ControlPlaneSummary, VpcBytesStr, EntraTenantId=coalesce(EntraTenantId, ""),
EntraObjectId=coalesce(EntraObjectId, ""), EntraUPN=coalesce(EntraUPN, "")Explanation
This KQL query is designed to identify and correlate suspicious activities in AWS environments by analyzing data from AWS GuardDuty, CloudTrail, and VPC Flow logs. Here's a simplified breakdown of what the query does:
-
Identity Mapping: It first retrieves a list of verified AWS access keys and their associated Entra identities from a watchlist, filtering out any entries that are not verified or lack mapping evidence.
-
GuardDuty Findings: It collects recent GuardDuty findings from the past hour, focusing on those with a severity of 7 or higher that are not archived. It extracts relevant details such as the AWS access key ID, principal name, instance ID, and remote IP address.
-
CloudTrail Mutations: It gathers recent non-read-only CloudTrail events (indicating changes) from the past two hours, excluding those with errors. It summarizes the types of changes and counts them by AWS account and access key ID.
-
VPC Flow Logs: It analyzes VPC flow logs from the past two hours to calculate the total bytes accepted and count distinct remote peers for each instance.
-
Correlation and Filtering: The query then joins the findings with the recent mutations, VPC flows, and identity map data. It filters the results to include only those with significant activity, such as:
- At least one mutation event.
- More than 10 MB of accepted VPC traffic.
- An associated Entra identity.
-
Output: Finally, it projects a set of fields for each correlated finding, including details like the time of the finding, AWS account and region, severity, activity type, principal name, and any associated Entra identity information.
This query is part of an analytic rule designed to detect potentially malicious activities by correlating different data sources and is intended for review before deployment.