10 Identity Control Plane Privesc And Persistence Mutations
Query
// REVIEW ONLY - AWS Security Operations Workbook Visual Query
// Tab: IdentityControlPlane | Panel: privesc-and-persistence-mutations | Visualization: table | Mode: All
// Security Question: Which principals modified role trust policies, inline/managed policies, login profiles, MFA devices, or access keys?
// Drill-Through: Click a row to set SelectedPrincipal and inspect downstream AssumeRole or Entra/XDR activity.
// Empty/No-Data Contract: No IAM privilege-escalation or credential-persistence API calls succeeded in the selected window.
let Start = {TimeRange:start};
let End = {TimeRange:end};
let OrgFilter = base64_decode_tostring("{Organization:base64}");
let AccountFilter = base64_decode_tostring("{AwsAccount:base64}");
let RegionFilter = base64_decode_tostring("{AwsRegion:base64}");
let EnvFilter = base64_decode_tostring("{Environment:base64}");
let WorkloadFilter = base64_decode_tostring("{Workload:base64}");
let SeverityFilter = base64_decode_tostring("{Severity:base64}");
let SourceFilter = base64_decode_tostring("{SourceTable:base64}");
let SummaryPref = base64_decode_tostring("{SummaryMode:base64}");
let SelectedPrincipalFilter = base64_decode_tostring("{SelectedPrincipal:base64}");
let SelectedIpFilter = base64_decode_tostring("{SelectedIp:base64}");
union isfuzzy=true
(datatable(TimeGenerated:datetime, RecipientAccountId:string, AWSRegion:string, EventSource:string, ErrorCode:string, ErrorMessage:string, EventName:string, RequestParameters:string, SourceIpAddress:string, UserIdentityArn:string)[]),
(AWSCloudTrail | where TimeGenerated between (Start .. End)
| project TimeGenerated, RecipientAccountId, AWSRegion, EventSource, ErrorCode, ErrorMessage, EventName, RequestParameters, SourceIpAddress, UserIdentityArn)
| where (AccountFilter == "*" or RecipientAccountId == AccountFilter) and (RegionFilter == "*" or AWSRegion == RegionFilter)
| where EventSource == "iam.amazonaws.com" and isempty(ErrorCode) and isempty(ErrorMessage)
| where EventName in ("UpdateAssumeRolePolicy", "PutUserPolicy", "PutRolePolicy", "PutGroupPolicy", "CreatePolicyVersion", "SetDefaultPolicyVersion", "AttachRolePolicy", "AttachUserPolicy", "AddUserToGroup", "CreateAccessKey", "CreateLoginProfile", "UpdateLoginProfile", "CreateVirtualMFADevice", "UploadSSHPublicKey")
| extend Req = parse_json(RequestParameters)
| extend TargetEntity = coalesce(
iff(isnotempty(tostring(Req.roleName)), tostring(Req.roleName), ""),
iff(isnotempty(tostring(Req.userName)), tostring(Req.userName), ""),
iff(isnotempty(tostring(Req.groupName)), tostring(Req.groupName), ""),
tostring(Req.policyArn))
| summarize FirstSeen=min(TimeGenerated), LastSeen=max(TimeGenerated), Changes=count(),
Actions=make_set(EventName, 15), Targets=make_set(TargetEntity, 15), SourceIps=make_set(SourceIpAddress, 10)
by RecipientAccountId, AWSRegion, UserIdentityArn
| order by Changes desc
| take 5000Explanation
This query is designed to identify which AWS principals (users or roles) have made changes to certain IAM (Identity and Access Management) configurations, such as role trust policies, inline or managed policies, login profiles, MFA devices, or access keys, within a specified time range. Here's a simplified breakdown of what the query does:
-
Time and Filters Setup: It sets up a time range and various filters (like organization, account, region, environment, severity, source, selected principal, and IP) using base64-decoded values.
-
Data Collection: It gathers data from AWS CloudTrail logs within the specified time range. The data includes details like the time of the event, account ID, AWS region, event source, error codes, event names, request parameters, source IP, and user identity ARN.
-
Filtering Events: It filters the events to only include those from the IAM service (
iam.amazonaws.com) that were successful (no error codes or messages) and are related to specific IAM actions that could indicate privilege escalation or credential persistence. -
Extracting Target Entities: For each event, it extracts the target entity (role, user, group, or policy) involved in the action.
-
Summarizing Results: It summarizes the data by counting the number of changes made by each principal, listing the actions they performed, the targets they affected, and the source IPs from which the actions were initiated. The results are ordered by the number of changes and limited to 5000 entries.
-
Output: The output is a table that helps security analysts understand which principals have modified IAM configurations, potentially indicating privilege escalation or persistence activities. If no data is returned, it means no such activities were detected in the selected time window.