P0 07b S3 Exfil Guardduty Vpc Correlated
Query
// Standalone KQL for Analytic Rule: AWS SecOps P0-07b - S3 Exfiltration GuardDuty Finding Correlated with Permission Change or VPC Egress
// Rule File: analytics/P0-07b-s3-exfil-guardduty-vpc-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 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, DynamicSeverityExplanation
This KQL query is designed to identify potential security threats related to Amazon S3 data exfiltration. It correlates findings from AWS GuardDuty with changes in S3 permissions and VPC egress activities. Here's a simplified breakdown:
-
GuardDuty Findings (GdS3):
- It retrieves GuardDuty findings from the last hour related to S3 activities like exfiltration, discovery, impact, or policy changes.
- Filters out archived findings and those ingested more than 15 minutes ago.
- Extracts relevant details such as the bucket name, principal name, and caller IP.
-
S3 Permission Changes (S3PermChanges):
- Looks at CloudTrail logs from the last two hours for events that change S3 bucket permissions (e.g., policy changes, ACL updates).
- Groups these events by AWS account and bucket name, collecting the types of permission changes and actors involved.
-
VPC Egress Activity (VpcEgress):
- Analyzes VPC flow logs from the last two hours for accepted outbound traffic.
- Sums up the egress bytes for each AWS account and source IP.
-
Correlation and Alerting:
- Joins the GuardDuty findings with S3 permission changes and VPC egress data.
- Filters for cases where there are permission changes, significant egress data (50 MB or more), or high-severity findings (severity 7 or above).
- Calculates a dynamic severity level, marking findings as "Critical" if there are both permission changes and S3 exfiltration activities.
- Projects the relevant information for further analysis or alerting.
This query is part of a security analytic rule that should be reviewed and validated before deployment.