Query Details

P0 01 Root Breakglass Activity

Query

// Standalone KQL for Analytic Rule: AWS SecOps P0-01 - Root or Break-Glass Runtime Activity and Escalation
// Rule File: analytics/P0-01-root-breakglass-activity.yaml | Rule ID: REPLACE_WITH_RULE_GUID | Severity: High
// Schedule: 15m / Lookback: 1h | REVIEW ONLY - Do not deploy/schedule without approval.
// REVIEW ONLY. Validate AWSCloudTrail schema first via discovery/D02-awscloudtrail-schema-and-dataevents-check.kql
let ApprovedBreakGlass = _GetWatchlist('AWS_BreakGlassWindows')
| where tolower(tostring(column_ifexists("Verified", ""))) == "true"
| project BgAccountId=tostring(column_ifexists("AWSAccountId", "")), AllowedCidr=tostring(column_ifexists("AllowedSourceCidr", "")),
    AllowedRegion=tostring(column_ifexists("AllowedRegion", "")), ValidFrom=todatetime(column_ifexists("ValidFromUtc", "")), ValidTo=todatetime(column_ifexists("ValidToUtc", ""));
let EscalationEvents = dynamic([
    "CreateAccessKey", "CreateLoginProfile", "UpdateLoginProfile", "DeactivateMFADevice",
    "DeleteVirtualMFADevice", "AttachUserPolicy", "AttachRolePolicy", "PutUserPolicy",
    "PutRolePolicy", "UpdateAssumeRolePolicy", "AssumeRole", "StopLogging", "DeleteTrail"
]);
AWSCloudTrail
| where TimeGenerated > ago(1h)
| where UserIdentityType =~ "Root"
| where isempty(UserIdentityInvokedBy) and EventTypeName != "AwsServiceEvent"
| where SourceIpAddress !endswith ".amazonaws.com" and SourceIpAddress != "AWS Internal"
| extend AddData = parse_json(AdditionalEventData), Resp = parse_json(ResponseElements)
| extend ConsoleLoginResult = tostring(Resp.ConsoleLogin),
    ConsoleMfa = tostring(AddData.MFAUsed)
| where ConsoleLoginResult !~ "Failure"
| extend MfaAuthenticated = (SessionMfaAuthenticated == true or ConsoleMfa =~ "Yes"),
    AWSAccountId = coalesce(RecipientAccountId, UserIdentityAccountId)
| join kind=leftouter ApprovedBreakGlass on $left.AWSAccountId == $right.BgAccountId
| extend IsApprovedWindow = (isnotnull(ValidFrom) and TimeGenerated between (ValidFrom .. ValidTo)
    and (isempty(AllowedRegion) or AWSRegion =~ AllowedRegion)
    and (isempty(AllowedCidr) or ipv4_is_in_range(SourceIpAddress, AllowedCidr)))
| where IsApprovedWindow == false
| summarize
    TimeGenerated = min(TimeGenerated),
    LastSeenUtc = max(TimeGenerated),
    LatestIngest = max(ingestion_time()),
    EventCount = count(),
    EventsObserved = make_set(EventName, 25),
    HasEscalationAction = countif(EventName in (EscalationEvents)) > 0,
    AnyUnauthenticatedMfa = countif(MfaAuthenticated == false) > 0,
    ErrorCodes = make_set(ErrorCode, 10),
    SampleRequestParams = tostring(take_any(RequestParameters)),
    AwsEventId = take_any(AwsEventId)
    by AWSAccountId, AWSRegion, SourceIpAddress, UserAgent, UserIdentityArn
| where LatestIngest > ago(15m)
| extend DynamicSeverity = iff(AnyUnauthenticatedMfa or HasEscalationAction, "Critical", "High"),
    AccountName = "root",
    EventsSummary = tostring(EventsObserved),
    ErrorCodeSummary = tostring(ErrorCodes),
    MfaStatus = iff(AnyUnauthenticatedMfa, "MFA_MISSING_ON_ROOT", "MFA_PRESENT")
| project TimeGenerated, LastSeenUtc, AwsEventId, AWSAccountId, AWSRegion, AccountName,
    UserIdentityArn, SourceIpAddress, UserAgent, MfaStatus, EventCount, EventsSummary,
    ErrorCodeSummary, SampleRequestParams, DynamicSeverity

Explanation

This KQL query is designed to detect potentially unauthorized or suspicious activities performed by root or break-glass accounts in AWS environments. Here's a simplified explanation of what the query does:

  1. Approved Break-Glass Accounts: It first retrieves a list of approved break-glass accounts from a watchlist, including details like allowed source IP ranges, regions, and valid time windows for these accounts.

  2. Escalation Events: It defines a list of sensitive actions (escalation events) that could indicate a security risk if performed unexpectedly, such as creating access keys or modifying policies.

  3. Filter AWS CloudTrail Logs: The query examines AWS CloudTrail logs from the past hour, focusing on actions performed by root accounts. It excludes events triggered by AWS services and those originating from AWS internal IPs.

  4. Additional Checks: It checks if the login was successful and whether MFA (Multi-Factor Authentication) was used. It also identifies the AWS account ID associated with the activity.

  5. Join with Approved Break-Glass List: The query checks if the activity falls within an approved break-glass window by matching account IDs and verifying the source IP, region, and time constraints.

  6. Summarize Suspicious Activities: For activities outside approved windows, it summarizes the findings, including the first and last occurrence times, the number of events, types of events observed, and whether any escalation actions or unauthenticated MFA logins occurred.

  7. Dynamic Severity and Output: It assigns a severity level (Critical or High) based on the presence of escalation actions or missing MFA. The query then projects relevant details like account ID, region, source IP, user agent, and a summary of events and errors.

This query is intended for review and should not be deployed without proper validation and approval. It helps security teams identify and investigate potentially unauthorized or risky activities involving root or break-glass accounts in AWS.