Query Details

AWS SecOps P0-07b - S3 Exfiltration GuardDuty Finding Correlated with Permission Change or VPC Egress

P0 07b S3 Exfil Guardduty Vpc Correlated

Query

// REVIEW ONLY. Validate AWSGuardDuty, AWSCloudTrail, and AWSVPCFlow schemas first via D02-D04.
let GdS3 = AWSGuardDuty
| where TimeGenerated > ago(1h)
| summarize arg_max(TimeGenerated, *) by Id
| where ingestion_time() > ago(15m) and tobool(ServiceDetails.archived) != true
| where ActivityType has_any ("Exfiltration:S3/", "Discovery:S3/", "Impact:S3/", "Policy:S3/")
| extend BucketName = tostring(ResourceDetails.s3BucketDetails[0].name),
    PrincipalName = tostring(ResourceDetails.accessKeyDetails.userName),
    CallerIp = tostring(ServiceDetails.action.awsApiCallAction.remoteIpDetails.ipAddressV4)
| where isnotempty(BucketName)
| project FindingTime=TimeGenerated, FindingId=Id, AWSAccountId=AccountId, AWSRegion=Region,
    Severity, ActivityType, BucketName, PrincipalName, CallerIp;
let S3PermChanges = AWSCloudTrail
| where TimeGenerated > ago(2h) and EventSource == "s3.amazonaws.com" and isempty(ErrorCode)
| where EventName in ("PutBucketPolicy", "PutBucketAcl", "PutBucketPublicAccessBlock", "DeletePublicAccessBlock", "PutBucketCors", "PutBucketReplication")
| extend BucketName = tostring(parse_json(RequestParameters).bucketName)
| where isnotempty(BucketName)
| summarize PermEvents=make_set(EventName, 10), PermActors=make_set(UserIdentityArn, 10)
    by AWSAccountId=RecipientAccountId, BucketName;
let VpcEgress = AWSVPCFlow
| where TimeGenerated > ago(2h) and Action == "ACCEPT" and isnotempty(SrcAddr)
| summarize EgressBytes=sum(Bytes) by AWSAccountId=AccountId, CallerIp=SrcAddr;
GdS3
| join kind=leftouter S3PermChanges on AWSAccountId, BucketName
| join kind=leftouter VpcEgress on AWSAccountId, CallerIp
| where array_length(PermEvents) > 0 or coalesce(EgressBytes, 0) >= 52428800 or Severity >= 7
| extend PermEventsStr = tostring(coalesce(PermEvents, dynamic([]))),
    PermActorsStr = tostring(coalesce(PermActors, dynamic([]))),
    EgressBytesStr = tostring(coalesce(EgressBytes, 0)),
    DynamicSeverity = iff(array_length(PermEvents) > 0 and ActivityType startswith "Exfiltration:S3/", "Critical", "High")
| project TimeGenerated=FindingTime, FindingId, AWSAccountId, AWSRegion, BucketName,
    ActivityType, PrincipalName, SourceIpAddress=CallerIp, PermEventsStr, PermActorsStr,
    EgressBytesStr, DynamicSeverity

Explanation

This query is designed to detect potential security incidents involving Amazon S3 buckets in AWS. It focuses on identifying suspicious activities by correlating different types of data from AWS services. Here's a simplified breakdown:

  1. Purpose: The query aims to detect S3 exfiltration (unauthorized data transfer) incidents that are associated with recent changes in S3 bucket permissions or unusual outbound data flow from a Virtual Private Cloud (VPC).

  2. Data Sources: It uses data from AWS GuardDuty, AWS CloudTrail, and AWS VPC Flow Logs. These services provide information about security findings, API calls, and network traffic, respectively.

  3. Time Frame: The query looks at events within the last 2 hours and runs every 15 minutes.

  4. Detection Logic:

    • It first identifies recent GuardDuty findings related to S3 activities such as exfiltration, discovery, or impact.
    • It checks for any recent changes to S3 bucket policies or access controls in CloudTrail logs.
    • It examines VPC Flow Logs for significant outbound data transfers from the same IP address involved in the GuardDuty findings.
  5. Correlation: The query correlates these findings to identify if there are:

    • Recent permission changes on the S3 bucket.
    • Large amounts of data being sent out from the VPC.
    • High-severity security alerts.
  6. Alerting: If any of these conditions are met, it generates an alert with details about the incident, including the AWS account, region, bucket name, activity type, and the IP address involved.

  7. Severity: The alert severity is dynamically set to "Critical" if there are permission changes and exfiltration activities, otherwise, it is "High".

  8. Incident Management: The query is configured to create incidents and group related alerts to manage them efficiently.

Overall, this query helps security teams monitor and respond to potential data exfiltration incidents in AWS by correlating multiple data sources and identifying suspicious patterns.