Query Details

S01 S3 Data Access 5m Summary

Query

// REVIEW ONLY - Summary Rule KQL Spec: S01 -> Target Table: AWS_S3DataAccess_5m_CL
// Schedule: Every 5m (or 15m) over S3 CloudTrail data events. Parses RequestParameters ONCE so neither
// S3 exfiltration detections nor workbook trend visuals re-parse raw S3 data-event JSON repeatedly.
AWSCloudTrail
| where TimeGenerated > ago(15m)
| where EventSource == "s3.amazonaws.com"
| where EventName in ("GetObject", "HeadObject", "CopyObject", "ListObjects", "ListObjectsV2", "DeleteObjects", "PutBucketPolicy", "DeleteBucketPolicy", "PutBucketAcl", "PutBucketPublicAccessBlock", "DeletePublicAccessBlock")
| extend Req = parse_json(RequestParameters)
| extend BucketName = tostring(Req.bucketName),
    ObjectKey = tostring(Req.key)
| extend ObjectPrefix = iff(ObjectKey contains "/", tostring(split(ObjectKey, "/")[0]), ObjectKey)
| summarize
    TotalEvents = count(),
    SuccessEvents = countif(isempty(ErrorCode) and isempty(ErrorMessage)),
    DeniedEvents = countif(ErrorCode in ("AccessDenied", "AllAccessDisabled", "InvalidAccessKeyId")),
    DistinctObjects = dcountif(ObjectKey, EventName in ("GetObject", "CopyObject") and isempty(ErrorCode)),
    DistinctPrefixes = dcountif(ObjectPrefix, isnotempty(ObjectPrefix)),
    SampleEvents = make_set(AwsEventId, 10)
    by WindowStart=bin(TimeGenerated, 5m), RecipientAccountId, ActorAccountId=UserIdentityAccountId,
       AWSRegion, EventName, UserIdentityType, AwsPrincipalArn=UserIdentityArn,
       AwsAccessKeyId=UserIdentityAccessKeyId, SourceIpAddress, BucketName
| take 10000

Explanation

This KQL (Kusto Query Language) query is designed to analyze AWS S3 data access events from CloudTrail logs. Here's a simplified breakdown of what it does:

  1. Data Source and Time Frame: It looks at AWS CloudTrail logs specifically for S3 events that occurred in the last 15 minutes.

  2. Event Filtering: It filters the events to include only those related to specific S3 actions like getting, copying, listing, or deleting objects, as well as modifying bucket policies and access controls.

  3. Parsing Request Parameters: The query extracts and parses the RequestParameters from the event data to get details like the bucket name and object key.

  4. Object Prefix Extraction: It determines the prefix of the object key, which is useful for identifying groups of objects.

  5. Event Summarization: The query summarizes the data by:

    • Counting total events.
    • Counting successful events (those without errors).
    • Counting denied events (those with specific error codes like "AccessDenied").
    • Counting distinct objects accessed successfully.
    • Counting distinct object prefixes.
    • Collecting a sample set of event IDs for further analysis.
  6. Grouping and Output: The results are grouped by various attributes such as time window, account IDs, AWS region, event name, user identity details, source IP address, and bucket name. The query limits the output to 10,000 records.

In essence, this query helps in monitoring and analyzing S3 access patterns, identifying potential security issues, and understanding usage trends over short time intervals.