21 H06 Sts Lineage
Query
// REVIEW ONLY workbook query: 7.2 H06 — Issued STS Access Key Lineage & Child-Session IP Drift
let Start = {TimeRange:start};
let End = {TimeRange:end};
let AccountFilter = trim(" ", base64_decode_tostring("{AwsAccount:base64}"));
let UseAllAccounts = AccountFilter in ("", "*");
let SeedKey = trim(" ", base64_decode_tostring("{AccessKeyId:base64}"));
let SafeCloudTrail = union isfuzzy=true
(datatable(TimeGenerated:datetime, AwsEventId:string, EventSource:string, EventName:string, AWSRegion:string,
RecipientAccountId:string, UserIdentityAccountId:string, UserIdentityType:string, UserIdentityArn:string,
UserIdentityAccessKeyId:string, SourceIpAddress:string, UserAgent:string,
ErrorCode:string, ErrorMessage:string, RequestParameters:string, ResponseElements:string, Resources:string,
ManagementEvent:bool, ReadOnly:bool, SessionMfaAuthenticated:string)[]),
(AWSCloudTrail
| where TimeGenerated between (Start .. End)
| project TimeGenerated, AwsEventId, EventSource, EventName, AWSRegion, RecipientAccountId,
UserIdentityAccountId, UserIdentityType, UserIdentityArn,
UserIdentityAccessKeyId, SourceIpAddress, UserAgent, ErrorCode, ErrorMessage,
RequestParameters, ResponseElements, Resources, ManagementEvent, ReadOnly, SessionMfaAuthenticated);
let Hop1Issued = SafeCloudTrail
| where EventSource == "sts.amazonaws.com" and EventName in ("AssumeRole", "AssumeRoleWithSAML", "AssumeRoleWithWebIdentity", "GetSessionToken", "GetFederationToken")
| where isempty(ErrorCode) and isempty(ErrorMessage)
| where UseAllAccounts or UserIdentityAccountId == AccountFilter or RecipientAccountId == AccountFilter
| where isempty(SeedKey) or UserIdentityAccessKeyId == SeedKey
| extend Resp = parse_json(ResponseElements)
| extend IssuedAccessKeyId = tostring(Resp.credentials.accessKeyId),
AssumedRoleArn = tostring(Resp.assumedRoleUser.arn)
| where isnotempty(IssuedAccessKeyId)
| project Hop1Time=TimeGenerated, ParentAccountId=UserIdentityAccountId, TargetAccountId=RecipientAccountId,
ParentPrincipalArn=UserIdentityArn, ParentAccessKeyId=UserIdentityAccessKeyId,
Hop1Event=EventName, IssuedAccessKeyId, AssumedRoleArn, Hop1Ip=SourceIpAddress;
let Hop2Activity = SafeCloudTrail
| where isnotempty(UserIdentityAccessKeyId)
| summarize Hop2Events=count(), Hop2First=min(TimeGenerated), Hop2Last=max(TimeGenerated),
Hop2Apis=make_set(EventName, 20), Hop2Accounts=make_set(RecipientAccountId, 10),
Hop2Ips=make_set(SourceIpAddress, 10),
IssuedHop2Keys=make_set_if(tostring(parse_json(ResponseElements).credentials.accessKeyId), EventName startswith "AssumeRole", 10)
by IssuedAccessKeyId=UserIdentityAccessKeyId;
Hop1Issued
| join kind=leftouter Hop2Activity on IssuedAccessKeyId
| extend CrossAccount = iff(isnotempty(ParentAccountId) and ParentAccountId != TargetAccountId, "🟠 Cross-Account", "🟢 Same-Account"),
IpChangedInChildSession = iff(array_length(Hop2Ips) > 0 and not(set_has_element(Hop2Ips, Hop1Ip)), "🔴 YES - Child key used from new IP", "🟢 No / Not yet used")
| project Hop1Time, CrossAccount, IpChangedInChildSession, ParentAccountId, ParentPrincipalArn, ParentAccessKeyId,
Hop1Event, TargetAccountId, AssumedRoleArn, IssuedAccessKeyId, Hop1Ip,
Hop2Events=coalesce(Hop2Events, long(0)), Hop2First, Hop2Last, Hop2Ips, Hop2Accounts, Hop2Apis, IssuedHop2Keys
| order by Hop1Time desc
| take 5000Explanation
This KQL query is designed to analyze AWS CloudTrail logs to track the issuance and usage of AWS Security Token Service (STS) access keys. Here's a simplified explanation of what the query does:
-
Time Range and Account Filtering: The query starts by setting a time range and filtering based on a specific AWS account and access key ID, if provided.
-
Data Collection: It collects data from AWS CloudTrail logs within the specified time range, focusing on events related to STS operations like
AssumeRole,GetSessionToken, etc., which are used to issue temporary access keys. -
First Hop (Hop1) Analysis:
- It identifies events where an STS access key was successfully issued.
- It extracts details such as the time of issuance, the account and principal that issued the key, the event type, and the IP address from which the request was made.
-
Second Hop (Hop2) Activity:
- It summarizes activities performed using the issued access keys, including the number of events, the first and last usage times, APIs called, accounts accessed, and IP addresses used.
-
Join and Analysis:
- It joins the first hop data with the second hop activity to analyze the lineage and usage of the issued keys.
- It checks if the access key was used across different accounts (cross-account usage) and whether the IP address changed between the parent session and the child session (indicating potential unauthorized use).
-
Output:
- The query outputs a list of up to 5000 records, showing details of each issued access key, including whether it was used across accounts, if the IP changed, and other relevant details.
- The results are ordered by the time of the first hop event.
This query helps in identifying potential security issues, such as unauthorized cross-account access or suspicious IP address changes in AWS environments.