Query Details

P0 07a S3 Publicblock And Anomalous Read Baseline

Query

// Standalone KQL for Analytic Rule: AWS SecOps P0-07a - S3 PublicAccessBlock Weakening and Sensitive Bucket Burst Read (Single-Source)
// Rule File: analytics/P0-07a-s3-publicblock-and-anomalous-read-baseline.yaml | Rule ID: REPLACE_WITH_RULE_GUID | Severity: High
// Schedule: 15m / Lookback: 1h | REVIEW ONLY - Do not deploy/schedule without approval.
// REVIEW ONLY. Verify S3 data events (ManagementEvent == false) via discovery/D02 first.
// High-volume alternative: point BurstReads at AWS_S3DataAccess_5m_CL (summary-rules/S01).
let SensitiveBuckets = _GetWatchlist('AWS_SensitiveBuckets')
| where tolower(tostring(column_ifexists("Verified", ""))) == "true"
| project SbAccountId=tostring(column_ifexists("AWSAccountId", "")), BucketNameLower=tolower(tostring(column_ifexists("BucketName", ""))),
    DataClassification=tostring(column_ifexists("DataClassification", "")), ExpectedReaders=tostring(column_ifexists("ExpectedReaderArnsJson", "")),
    MaxObjectsPer15m=toint(column_ifexists("MaxObjectsPer15m", "")), AllowedRegions=tostring(column_ifexists("AllowedRegionsJson", ""));
let ExposureWeakening = AWSCloudTrail
| where TimeGenerated > ago(1h) and ingestion_time() > ago(15m)
| where EventSource == "s3.amazonaws.com" and isempty(ErrorCode) and isempty(ErrorMessage)
| where EventName in ("DeletePublicAccessBlock", "PutBucketPublicAccessBlock", "PutBucketPolicy")
| extend Req = parse_json(RequestParameters), ReqText = tostring(RequestParameters)
| extend BucketName = tostring(Req.bucketName),
    AWSAccountId = coalesce(RecipientAccountId, UserIdentityAccountId)
| extend IsBlockDisabled = (EventName == "DeletePublicAccessBlock"
    or (EventName == "PutBucketPublicAccessBlock" and ReqText matches regex @"(?i)(BlockPublicAcls|IgnorePublicAcls|BlockPublicPolicy|RestrictPublicBuckets)""\s*:\s*false")),
    ExternalAccountArn = extract(@"arn:aws[a-z-]*:iam::([0-9]{12}):", 1, tostring(Req.bucketPolicy))
| extend IsExternalAccountPolicy = (EventName == "PutBucketPolicy" and isnotempty(ExternalAccountArn) and ExternalAccountArn != AWSAccountId)
| where IsBlockDisabled or IsExternalAccountPolicy
| project TimeGenerated, AwsEventId, AWSAccountId, AWSRegion, BucketName, UserIdentityArn,
    SourceIpAddress, UserAgent, DetectionTrigger=iff(IsBlockDisabled, "PublicAccessBlockWeakened", strcat("CrossAccountPolicyTo:", ExternalAccountArn)),
    DistinctObjects=0, DataClassification="ControlPlaneExposure";
let BurstReads = AWSCloudTrail
| where TimeGenerated > ago(30m) and ingestion_time() > ago(15m)
| where EventSource == "s3.amazonaws.com" and EventName in ("GetObject", "CopyObject")
| where isempty(ErrorCode) and isempty(ErrorMessage)
| where UserIdentityAccountId !~ "ANONYMOUS_PRINCIPAL" and UserIdentityPrincipalid != "Anonymous"
| extend Req = parse_json(RequestParameters)
| extend BucketName = tostring(Req.bucketName), ObjectKey = tostring(Req.key),
    AWSAccountId = coalesce(RecipientAccountId, UserIdentityAccountId)
| extend BucketNameLower = tolower(BucketName)
| join kind=inner SensitiveBuckets on $left.AWSAccountId == $right.SbAccountId and BucketNameLower
| where not(ExpectedReaders has UserIdentityArn)
| summarize TimeGenerated=min(TimeGenerated), AwsEventId=take_any(AwsEventId),
    DistinctObjects=dcount(ObjectKey), SamplePrefixes=make_set(tostring(split(ObjectKey, "/")[0]), 10)
    by bin(TimeGenerated, 15m), AWSAccountId, AWSRegion, BucketName, UserIdentityArn,
       SourceIpAddress, UserAgent, DataClassification, MaxObjectsPer15m, AllowedRegions
| where DistinctObjects >= coalesce(MaxObjectsPer15m, 100) or (isnotempty(AllowedRegions) and not(AllowedRegions has AWSRegion))
| project TimeGenerated, AwsEventId, AWSAccountId, AWSRegion, BucketName, UserIdentityArn,
    SourceIpAddress, UserAgent, DetectionTrigger=strcat("AuthenticatedSensitiveBucketBurst(Prefixes=", tostring(SamplePrefixes), ")"),
    DistinctObjects, DataClassification;
union ExposureWeakening, BurstReads
| extend AccountName = tostring(split(UserIdentityArn, "/")[-1]),
    DistinctObjectsStr = tostring(DistinctObjects)
| project TimeGenerated, AwsEventId, AWSAccountId, AWSRegion, BucketName, AccountName,
    UserIdentityArn, SourceIpAddress, UserAgent, DetectionTrigger, DistinctObjectsStr, DataClassification

Explanation

This KQL query is designed to detect two specific security concerns related to AWS S3 buckets:

  1. Public Access Block Weakening: The query identifies events where the public access settings of an S3 bucket are weakened. This includes actions like deleting or modifying the public access block settings or adding a bucket policy that allows access from external accounts. It checks for these events in the last hour but only considers those ingested in the last 15 minutes.

  2. Sensitive Bucket Burst Read: The query detects unusual read activity on sensitive S3 buckets. It looks for a high number of object read operations (like GetObject or CopyObject) within a 30-minute window, focusing on buckets marked as sensitive. It checks if the number of distinct objects accessed exceeds a predefined threshold or if the access occurs from unauthorized regions.

The query uses two main data sources:

  • SensitiveBuckets: A watchlist of sensitive buckets with details like expected readers, maximum allowed reads, and allowed regions.
  • AWSCloudTrail: Logs of AWS events, filtered to focus on S3-related activities.

The results from both checks are combined and include details such as the time of the event, AWS account ID, region, bucket name, user identity, source IP, user agent, detection trigger, number of distinct objects accessed, and data classification. This helps in identifying potential security breaches or policy violations related to S3 bucket access.