Query Details

AWS SecOps P0-05 - CloudTrail Selector Downgrade and Logging Delivery Tampering

P0 05 Cloudtrail Selector Delivery Evasion

Query

// REVIEW ONLY. Validate AWSCloudTrail schema first via discovery/D02-awscloudtrail-schema-and-dataevents-check.kql
let ProtectedLogging = _GetWatchlist('AWS_ProtectedLoggingAssets')
| where tolower(tostring(column_ifexists("Verified", ""))) == "true"
| project LogAccountId=tostring(column_ifexists("AWSAccountId", "")), AssetIdentifier=tolower(tostring(column_ifexists("AssetIdentifier", ""))),
    TrailOrPipelineName=tostring(column_ifexists("TrailOrPipelineName", ""));
AWSCloudTrail
| where TimeGenerated > ago(1h)
| where isempty(ErrorCode) and isempty(ErrorMessage) and isnotempty(AwsEventId)
| where EventSource in ("cloudtrail.amazonaws.com", "s3.amazonaws.com", "kms.amazonaws.com")
| where EventName in (
    "PutEventSelectors", "PutInsightSelectors", "DeleteEventDataStore", "StopImport",
    "UpdateEventDataStore", "PutBucketPolicy", "DeleteBucketPolicy", "PutBucketLifecycleConfiguration",
    "DisableKey", "ScheduleKeyDeletion", "PutKeyPolicy")
| extend Req = parse_json(RequestParameters),
    AWSAccountId = coalesce(RecipientAccountId, UserIdentityAccountId)
| extend TargetAsset = tolower(coalesce(
    tostring(Req.trailName), tostring(Req.eventDataStore), tostring(Req.bucketName), tostring(Req.keyId), "")),
    ReqText = tostring(RequestParameters)
| join kind=leftouter ProtectedLogging on $left.AWSAccountId == $right.LogAccountId and $left.TargetAsset == $right.AssetIdentifier
| extend TamperMode = case(
    EventName == "PutEventSelectors" and (ReqText has "\"IncludeManagementEvents\":false" or ReqText has "\"ReadWriteType\":\"WriteOnly\"" or ReqText has "\"DataResources\":[]" or ReqText has "\"AdvancedEventSelectors\":[]"),
        "CloudTrailSelectorCoverageReduction",
    EventName in ("DeleteEventDataStore", "StopImport"),
        "CloudTrailLakeDataStoreHalted",
    EventName == "UpdateEventDataStore" and ReqText has "\"TerminationProtectionEnabled\":false",
        "CloudTrailLakeProtectionDisabled",
    EventSource == "s3.amazonaws.com" and isnotempty(TrailOrPipelineName),
        "ProtectedLoggingBucketTampered",
    EventSource == "kms.amazonaws.com" and isnotempty(TrailOrPipelineName),
        "ProtectedLoggingKmsKeyTampered",
    "")
| where isnotempty(TamperMode)
| where ingestion_time() > ago(15m)
| extend AccountName = tostring(split(UserIdentityArn, "/")[-1]),
    DynamicSeverity = "Critical",
    RequestParamsSummary = tostring(RequestParameters)
| project TimeGenerated, AwsEventId, AWSAccountId, AWSRegion, AccountName, UserIdentityArn,
    EventSource, EventName, TargetAsset, TrailOrPipelineName, TamperMode, SourceIpAddress,
    UserAgent, RequestParamsSummary, DynamicSeverity

Explanation

This query is designed to monitor and detect potential tampering or downgrading of AWS CloudTrail logging configurations, which could indicate attempts to evade security monitoring. Here's a simplified breakdown:

  1. Purpose: The query aims to identify changes that could create blind spots in AWS CloudTrail logging, which is crucial for tracking activities in AWS environments. It complements existing AWS log tampering detection mechanisms without overlapping them.

  2. Monitored Actions: It looks for specific actions that could reduce logging coverage or affect the integrity of logging data. These actions include:

    • Modifying event selectors to reduce logging (e.g., excluding management events or setting write-only logging).
    • Deleting or stopping data stores in CloudTrail Lake.
    • Tampering with S3 bucket policies or lifecycle configurations.
    • Disabling or scheduling the deletion of KMS keys used for logging.
  3. Data Sources: The query uses data from AWS CloudTrail logs, specifically focusing on events from cloudtrail.amazonaws.com, s3.amazonaws.com, and kms.amazonaws.com.

  4. Detection Logic:

    • It checks for recent events (within the last hour) that match the specified actions.
    • It identifies if these actions affect protected logging assets by comparing them with a predefined watchlist (AWS_ProtectedLoggingAssets).
    • It categorizes the type of tampering detected (e.g., "CloudTrailSelectorCoverageReduction" or "ProtectedLoggingBucketTampered").
  5. Alerting:

    • If tampering is detected, it generates an alert with a "Critical" severity level.
    • The alert includes details such as the AWS account involved, the specific asset targeted, and the type of tampering detected.
    • Alerts are grouped by account and tamper mode to facilitate incident management.
  6. Incident Management:

    • The query is configured to create incidents in a security operations platform, allowing for efficient tracking and response.
    • It supports grouping related alerts into a single incident to streamline investigation and response efforts.

Overall, this query helps security teams detect and respond to potential attempts to evade logging in AWS environments, ensuring that critical security monitoring remains intact.