H06 Sts Issued Key Lineage
Query
// REVIEW ONLY. Follow up to TWO AssumeRole hops using issued access-key IDs, not session-name guesses.
// Select only credentials.accessKeyId and assumedRoleUser.arn; never output tokens or secret keys.
let Start = ago(1d);
let End = now();
let SeedAWSAccountId = "REPLACE_WITH_12_DIGIT_ACCOUNT";
let SeedAccessKeyId = "REPLACE_WITH_ACCESS_KEY_ID";
let InputsValid = assert(SeedAWSAccountId matches regex @"^[0-9]{12}$"
and isnotempty(SeedAccessKeyId) and SeedAccessKeyId != "REPLACE_WITH_ACCESS_KEY_ID",
"Set the initial AWS identity account and access key ID.");
let RoleIssues = materialize(AWSCloudTrail
| where InputsValid and TimeGenerated between (Start .. End)
| where EventSource == "sts.amazonaws.com" and EventName == "AssumeRole"
| where isempty(ErrorCode) and isempty(ErrorMessage) and isnotempty(AwsEventId)
| extend Response=parse_json(ResponseElements)
| project IssueTime=TimeGenerated, IssueEventId=AwsEventId,
ParentKeyId=UserIdentityAccessKeyId, ParentAccountId=UserIdentityAccountId,
IssuedKeyId=tostring(Response.credentials.accessKeyId),
IssuedPrincipalArn=tostring(Response.assumedRoleUser.arn)
| where isnotempty(ParentKeyId) and isnotempty(IssuedKeyId) and isnotempty(IssuedPrincipalArn)
| extend IssuedAccountId=extract(@"^arn:[^:]+:sts::([0-9]{12}):", 1, IssuedPrincipalArn));
let FirstHop = RoleIssues
| where ParentAccountId == SeedAWSAccountId and ParentKeyId == SeedAccessKeyId
| project Depth=1, IssueTime, IssueEventId, ParentKeyId, IssuedKeyId, IssuedPrincipalArn, IssuedAccountId;
let SecondHop = FirstHop
| project PreviousIssueTime=IssueTime, ParentKeyId=IssuedKeyId, ParentAccountId=IssuedAccountId,
PreviousPrincipalArn=IssuedPrincipalArn
| join kind=inner (
RoleIssues
| project IssueTime, IssueEventId, ParentKeyId, ParentAccountId, IssuedKeyId, IssuedPrincipalArn, IssuedAccountId
) on ParentKeyId, ParentAccountId
| where IssueTime >= PreviousIssueTime
| project Depth=2, IssueTime, IssueEventId, ParentKeyId, IssuedKeyId, IssuedPrincipalArn, IssuedAccountId;
let Lineage = union FirstHop, SecondHop;
Lineage
| join kind=inner (
AWSCloudTrail
| where TimeGenerated between (Start .. End) and isnotempty(AwsEventId)
| project ActivityTime=TimeGenerated, ActivityEventId=AwsEventId, EventName, EventSource,
IssuedKeyId=UserIdentityAccessKeyId, IssuedPrincipalArn=UserIdentityArn,
IssuedAccountId=UserIdentityAccountId, SourceIpAddress, ErrorCode
) on IssuedKeyId, IssuedPrincipalArn, IssuedAccountId
| where ActivityTime >= IssueTime
| summarize FirstUse=min(ActivityTime), LastUse=max(ActivityTime),
Operations=make_set(strcat(EventSource, "/", EventName), 50), Sources=make_set(SourceIpAddress, 20),
DeniedEvents=countif(isnotempty(ErrorCode))
by Depth, IssueTime, IssueEventId, ParentKeyId, IssuedKeyId, IssuedPrincipalArn, IssuedAccountId
| extend CorrelationBasis="CloudTrail-issued key + observed full session ARN + identity account",
NextStep="Pivot each IssuedKeyId/IssuedAccountId through H01 and H03; do not assume a role keeps the original Entra actor"
| order by IssueTime asc
| take 2000Explanation
This query is designed to track the use of AWS AssumeRole operations over a specific time period (the last day) and identify two levels of role assumption hops starting from a specific AWS account and access key ID. Here's a simplified breakdown of what the query does:
-
Setup and Validation:
- The query starts by defining a time range (from 1 day ago to now) and requires the user to input a specific AWS account ID and access key ID.
- It checks if these inputs are valid (the account ID should be a 12-digit number, and the access key ID should not be a placeholder).
-
Identify Role Assumptions:
- It searches AWS CloudTrail logs for AssumeRole events where roles were successfully assumed (no errors) and extracts details about the issued access key ID and the assumed role's ARN (Amazon Resource Name).
-
First Hop:
- It identifies the first level of role assumptions where the parent account and access key match the user-provided inputs.
- It captures details like the time of issue, event ID, and the issued access key and principal ARN.
-
Second Hop:
- It looks for a second level of role assumptions where the issued key from the first hop is used as a parent key.
- It joins this data with the initial role issues to find subsequent role assumptions.
-
Combine and Analyze:
- It combines the first and second hop data and joins it with additional CloudTrail logs to track activities performed using the issued access keys.
- It summarizes the first and last use of these keys, the operations performed, source IP addresses, and any denied events.
-
Output:
- The query outputs a summary of these activities, ordered by the time of issue, and provides guidance on the next steps for further analysis.
- It limits the output to 2000 records to ensure manageability.
Overall, this query helps in auditing AWS role assumptions by tracing the lineage of access keys and understanding how they are used across different accounts and roles.