Query Details

P0 04 Sts Assumerole Chain

Query

// Standalone KQL for Analytic Rule: AWS SecOps P0-04 - Autonomous Cross-Account or Sensitive STS AssumeRole Chain
// Rule File: analytics/P0-04-sts-assumerole-chain.yaml | Rule ID: REPLACE_WITH_RULE_GUID | Severity: High
// Schedule: 15m / Lookback: 2h | REVIEW ONLY - Do not deploy/schedule without approval.
// REVIEW ONLY. Validate AWSCloudTrail schema first via discovery/D02-awscloudtrail-schema-and-dataevents-check.kql
let ApprovedAutomation = _GetWatchlist('AWS_ApprovedAutomationPrincipals')
| where tolower(tostring(column_ifexists("Verified", ""))) == "true"
| project AutoAccountId=tostring(column_ifexists("AWSAccountId", "")), PrincipalArnPattern=tolower(tostring(column_ifexists("PrincipalArnPattern", ""))),
    ValidFrom=todatetime(column_ifexists("ValidFromUtc", "")), ValidTo=todatetime(column_ifexists("ValidToUtc", ""));
let SensitiveRoles = _GetWatchlist('AWS_SensitiveRolesAndPolicies')
| where tolower(tostring(column_ifexists("Verified", ""))) == "true" and tolower(tostring(column_ifexists("AdminEquivalent", ""))) == "true"
| project RoleArnLower=tolower(tostring(column_ifexists("ResourceArnPattern", "")));
let RoleAssumptions = materialize(
    AWSCloudTrail
    | where TimeGenerated > ago(2h)
    | where EventSource == "sts.amazonaws.com" and EventName == "AssumeRole"
    | where isempty(ErrorCode) and isempty(ErrorMessage) and isnotempty(AwsEventId)
    | extend Req = parse_json(RequestParameters), Resp = parse_json(ResponseElements)
    | extend TargetRoleArn = tostring(Req.roleArn),
        IssuedAccessKeyId = tostring(Resp.credentials.accessKeyId),
        IssuedSessionArn = tostring(Resp.assumedRoleUser.arn)
    | where isnotempty(IssuedAccessKeyId) and isnotempty(TargetRoleArn)
    | project EventTime=TimeGenerated, IngestTime=ingestion_time(), AwsEventId,
        CallerAccountId=UserIdentityAccountId, RecipientAccountId, AWSRegion,
        CallerArn=UserIdentityArn, CallerAccessKeyId=UserIdentityAccessKeyId,
        TargetRoleArn, IssuedAccessKeyId, IssuedSessionArn, SourceIpAddress, UserAgent
);
RoleAssumptions
| project Hop1Time=EventTime, Hop1Ingest=IngestTime, Hop1EventId=AwsEventId,
    OriginAccountId=CallerAccountId, OriginArn=CallerArn, OriginIp=SourceIpAddress,
    Hop1TargetRoleArn=TargetRoleArn, IssuedAccessKeyId
| join kind=inner (
    RoleAssumptions
    | project Hop2Time=EventTime, Hop2Ingest=IngestTime, Hop2EventId=AwsEventId,
        CallerAccessKeyId, Hop2TargetRoleArn=TargetRoleArn, Hop2AccountId=RecipientAccountId,
        Hop2Region=AWSRegion, Hop2Ip=SourceIpAddress, Hop2UserAgent=UserAgent
) on $left.IssuedAccessKeyId == $right.CallerAccessKeyId
| where Hop2Time between (Hop1Time .. (Hop1Time + 1h))
| extend FinalRoleLower = tolower(Hop2TargetRoleArn), OriginArnLower = tolower(OriginArn)
| extend IsCrossAccount = (OriginAccountId != Hop2AccountId),
    IsSensitiveFinalRole = FinalRoleLower in (SensitiveRoles),
    IpChangedAcrossHops = (OriginIp != Hop2Ip)
| where IsCrossAccount or IsSensitiveFinalRole or IpChangedAcrossHops
| join kind=leftouter ApprovedAutomation on $left.OriginAccountId == $right.AutoAccountId and $left.OriginArnLower == $right.PrincipalArnPattern
| where isnull(ValidFrom) or not(Hop1Time between (ValidFrom .. ValidTo))
| extend LatestIngest = max_of(Hop1Ingest, Hop2Ingest)
| where LatestIngest > ago(15m)
| summarize arg_max(LatestIngest, *) by Hop1EventId, Hop2EventId
| extend AccountName = tostring(split(OriginArn, "/")[-1]),
    ChainSummary = strcat(OriginArn, " -> ", Hop1TargetRoleArn, " -> ", Hop2TargetRoleArn)
| project TimeGenerated=Hop2Time, AwsEventId=Hop2EventId, Hop1EventId,
    AWSAccountId=Hop2AccountId, OriginAccountId, AWSRegion=Hop2Region,
    AccountName, OriginArn, Hop1TargetRoleArn, Hop2TargetRoleArn, ChainSummary,
    OriginIp, SourceIpAddress=Hop2Ip, UserAgent=Hop2UserAgent, IsCrossAccount, IpChangedAcrossHops

Explanation

This KQL query is designed to detect potentially suspicious activity involving AWS STS (Security Token Service) AssumeRole operations, focusing on cross-account or sensitive role assumptions. Here's a simplified breakdown of what the query does:

  1. Approved Automation List: It retrieves a list of approved automation principals from a watchlist, filtering for those verified as true. This list includes account IDs and principal ARN patterns that are valid within a specific time range.

  2. Sensitive Roles List: It retrieves a list of sensitive roles and policies from another watchlist, filtering for those verified as true and equivalent to admin roles. This list includes role ARN patterns.

  3. Role Assumptions: It queries AWS CloudTrail logs for AssumeRole events within the last 2 hours, filtering out any events with errors and extracting relevant details like role ARN, access key ID, and session ARN.

  4. Chaining Role Assumptions: It identifies sequences of AssumeRole operations (hops) by joining the role assumptions on the issued access key ID. It checks if the second hop occurs within 1 hour of the first.

  5. Cross-Account and Sensitive Role Checks: It checks if the role assumption involves cross-account activity, targets a sensitive role, or if the IP address changes across hops.

  6. Approved Automation Validation: It checks if the origin of the role assumption is part of the approved automation list and if the event time falls outside the valid time range.

  7. Recent Activity Filter: It filters for the most recent activity within the last 15 minutes.

  8. Summarization: It summarizes the findings, highlighting the chain of role assumptions, account details, and whether the activity is cross-account or involves IP changes.

The query is part of an analytic rule with high severity, meant for review only, and should not be deployed without approval. It requires validation of the AWSCloudTrail schema before use.