Query Details

P2 01b Network Firewall Vpc Guardduty Iam Correlated

Query

// Standalone KQL for Analytic Rule: AWS SecOps P2-01b - AWS Network Firewall Correlated with VPC Flow, GuardDuty and IAM Changes
// Rule File: analytics/P2-01b-network-firewall-vpc-guardduty-iam-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 AWSNetworkFirewallAlert, AWSVPCFlow, AWSGuardDuty, AWSCloudTrail via D01-D08.
let CriticalAssets = _GetWatchlist('AWS_CriticalAssets')
| where tolower(tostring(column_ifexists("Verified", ""))) == "true"
| project AssetAccountId=tostring(column_ifexists("AWSAccountId", "")), ResourceKey=tolower(tostring(column_ifexists("ResourceIdOrName", ""))),
    AssetCriticality=tostring(column_ifexists("Criticality", ""));
let NetFwAlerts = AWSNetworkFirewallAlert
| where TimeGenerated > ago(1h) and ingestion_time() > ago(15m)
| extend InternalIp = iff(ipv4_is_private(SrcIp), SrcIp, iff(ipv4_is_private(DestIp), DestIp, "")),
    ExternalIp = iff(ipv4_is_private(SrcIp) == false, SrcIp, iff(ipv4_is_private(DestIp) == false, DestIp, ""))
| where isnotempty(InternalIp)
| summarize AlertTime=min(TimeGenerated), FwHits=count(), Signatures=make_set(Signature, 10),
    SignatureIds=make_set(SignatureId, 10), Verdicts=make_set(VerdictAction, 5), SampleSni=take_any(Sni)
    by FirewallName, AvailabilityZone, InternalIp, ExternalIp, DestPort, Proto;
let VpcContext = AWSVPCFlow
| where TimeGenerated > ago(2h) and LogStatus == "OK"
| extend OrigSrc = iff(isnotempty(PktSrcAddr), PktSrcAddr, SrcAddr)
| where ipv4_is_private(OrigSrc)
| summarize VpcBytes=sum(Bytes), AWSAccountId=take_any(AccountId), VpcId=take_any(VpcId),
    InstanceId=take_any(InstanceId), InterfaceId=take_any(InterfaceId)
    by InternalIp=OrigSrc;
let GdIpFindings = AWSGuardDuty
| where TimeGenerated > ago(2h)
| summarize arg_max(TimeGenerated, *) by Id
| extend InstanceId = tostring(ResourceDetails.instanceDetails.instanceId)
| where isnotempty(InstanceId)
| summarize GdTypes=make_set(ActivityType, 10) by AWSAccountId=AccountId, InstanceId;
let NetOrIamChanges = AWSCloudTrail
| where TimeGenerated > ago(2h) and ReadOnly == false and isempty(ErrorCode)
| where EventSource in ("ec2.amazonaws.com", "network-firewall.amazonaws.com", "iam.amazonaws.com")
| summarize ControlChanges=make_set(EventName, 10) by AWSAccountId=RecipientAccountId;
NetFwAlerts
| join kind=inner VpcContext on InternalIp
| extend InstanceKey = tolower(InstanceId)
| join kind=leftouter CriticalAssets on $left.AWSAccountId == $right.AssetAccountId and $left.InstanceKey == $right.ResourceKey
| join kind=leftouter GdIpFindings on AWSAccountId, InstanceId
| join kind=leftouter NetOrIamChanges on AWSAccountId
| where array_length(GdTypes) > 0 or array_length(ControlChanges) > 0 or AssetCriticality in~ ("Critical", "High") or VpcBytes >= 20971520
| extend SignatureSummary = tostring(Signatures),
    GdSummary = tostring(coalesce(GdTypes, dynamic([]))),
    ControlChangeSummary = tostring(coalesce(ControlChanges, dynamic([]))),
    VpcBytesStr = tostring(VpcBytes)
| project TimeGenerated=AlertTime, AWSAccountId, VpcId, InstanceId, InterfaceId,
    AssetCriticality=coalesce(AssetCriticality, "Standard"), FirewallName,
    SourceIpAddress=InternalIp, DestinationIpAddress=ExternalIp, DestPort, Proto,
    SignatureSummary, SampleSni, VpcBytesStr, GdSummary, ControlChangeSummary

Explanation

This KQL query is designed to identify and correlate security events in an AWS environment. Here's a simplified breakdown of what it does:

  1. Critical Assets Identification: It starts by identifying critical AWS assets from a watchlist, focusing on those marked as "true" for verification.

  2. Network Firewall Alerts: It retrieves alerts from AWS Network Firewall within the last hour, focusing on those ingested in the last 15 minutes. It identifies internal and external IPs, and summarizes alert details like signatures, verdicts, and sample server names.

  3. VPC Flow Context: It gathers data from AWS VPC Flow logs for the last 2 hours, focusing on private source IPs. It summarizes data usage and associates it with AWS account and instance details.

  4. GuardDuty Findings: It collects findings from AWS GuardDuty for the last 2 hours, focusing on instances with findings and summarizing the types of activities detected.

  5. Network and IAM Changes: It checks AWS CloudTrail logs for the last 2 hours for any non-read-only changes related to EC2, Network Firewall, or IAM, summarizing the types of changes made.

  6. Correlation and Filtering: The query correlates the data from the above sources:

    • It joins firewall alerts with VPC flow data on internal IPs.
    • It further joins with critical assets, GuardDuty findings, and CloudTrail changes.
    • It filters results to include only those with significant GuardDuty findings, control changes, high criticality, or large data transfers (over 20 MB).
  7. Output: Finally, it projects a summary of the correlated data, including account and instance details, firewall and network information, and summaries of signatures, GuardDuty findings, and control changes.

This query is intended for review and should not be deployed without approval. It helps in identifying potential security incidents by correlating various AWS security logs and alerts.