Query Details

AWS SecOps P0-01 - Root or Break-Glass Runtime Activity and Escalation

P0 01 Root Breakglass Activity

Query

// 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 query is designed to monitor and alert on potentially risky activities performed by the root user in an AWS environment. Here's a simplified breakdown:

  1. Purpose: The query detects interactive console logins or non-service API activities by the AWS root user that occur outside of approved "break-glass" windows. These windows are predefined periods when such activities are allowed.

  2. Severity Escalation: The severity of the alert is escalated to "Critical" if:

    • Multi-Factor Authentication (MFA) is not used.
    • The source IP address is not within the approved range.
    • High-impact actions are performed, such as creating access keys, modifying IAM policies, or changing account password policies.
  3. Exclusions: It excludes events initiated by AWS internal services to avoid false positives.

  4. Data Source: The query uses data from AWS CloudTrail logs, accessed via an AWS S3 connector.

  5. Frequency and Period: The query runs every 15 minutes and looks back over the past hour.

  6. Alert Generation: An alert is triggered if any suspicious activity is detected, and incidents are created for further investigation.

  7. Entity Mapping: The query maps relevant data to entities like Account and IP for better context in alerts.

  8. Custom Details and Alert Display: The alert includes custom details such as AWS account ID, region, user identity, MFA status, and a summary of events and errors. The alert's display name highlights the AWS account and MFA status.

  9. Incident Configuration: Incidents are grouped by account and IP to manage related alerts efficiently.

Overall, this query helps security teams identify and respond to unauthorized or risky root user activities in AWS environments, ensuring that any deviations from expected behavior are promptly investigated.