Query Details

AWS SecOps P0-04 - Autonomous Cross-Account or Sensitive STS AssumeRole Chain

P0 04 Sts Assumerole Chain

Query

// 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 query is designed to detect potentially unauthorized or risky activity involving AWS Security Token Service (STS) AssumeRole operations. Here's a simplified breakdown of what it does:

  1. Purpose: The query aims to identify situations where temporary credentials issued by one AWS account (Hop 1) are used within 60 minutes to assume a second role in another AWS account (Hop 2) or into a sensitive role defined in a watchlist. This excludes any approved automation principals.

  2. Severity: The severity of this detection is marked as "High," indicating that such activities could pose significant security risks, such as lateral movement or privilege escalation.

  3. Data Source: It uses AWS CloudTrail logs, which are collected via the AWSS3 data connector, to track these activities.

  4. Frequency and Period: The query runs every 15 minutes and looks back over the past 2 hours to find relevant events.

  5. Detection Logic:

    • It first checks for any role assumptions (AssumeRole events) in the AWS CloudTrail logs within the last 2 hours.
    • It identifies the initial role assumption (Hop 1) and checks if the temporary credentials from this event are used to assume another role (Hop 2) within 60 minutes.
    • It checks if the second role assumption involves crossing AWS accounts, assuming a sensitive role, or if the IP address changes between hops.
    • It excludes any role assumptions that match approved automation principals.
  6. Alert Generation: If such a chain of events is detected, an alert is generated. The alert includes details like the AWS account IDs, role ARNs, IP addresses, and a summary of the role assumption chain.

  7. Incident Management: The query is configured to create incidents for detected events, grouping them by account and origin ARN to manage related alerts efficiently.

In summary, this query helps security teams monitor and respond to potentially unauthorized cross-account role assumptions in AWS, focusing on scenarios that could indicate lateral movement or privilege escalation.