AWS SecOps P0-02 - Coordinated IAM Credential Persistence and Cross-Principal Backdooring
P0 02 Iam Credential Persistence
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 PrivChanges = AWSCloudTrail
| where TimeGenerated > ago(2h) and EventSource == "iam.amazonaws.com"
| where isempty(ErrorCode) and isempty(ErrorMessage)
| where EventName in ("CreateUser", "AttachUserPolicy", "PutUserPolicy", "AddUserToGroup", "UpdateAssumeRolePolicy")
| extend ReqP = parse_json(RequestParameters)
| extend ChangedUser = tolower(tostring(ReqP.userName))
| where isnotempty(ChangedUser)
| summarize PrivChangeEvents=make_set(EventName, 10), FirstPrivChange=min(TimeGenerated)
by RecipientAccountId, ChangedUser;
AWSCloudTrail
| where TimeGenerated > ago(2h)
| where EventSource == "iam.amazonaws.com"
| where isempty(ErrorCode) and isempty(ErrorMessage) and isnotempty(AwsEventId)
| where EventName in (
"CreateLoginProfile", "UpdateLoginProfile", "CreateVirtualMFADevice", "EnableMFADevice",
"ResyncMFADevice", "UploadSigningCertificate", "UploadSSHPublicKey",
"CreateServiceSpecificCredential", "CreateAccessKey")
| extend Req = parse_json(RequestParameters)
| extend TargetPrincipal = coalesce(tostring(Req.userName), tostring(Req.virtualMFADeviceName), ""),
ActorArn = UserIdentityArn,
ActorShort = tostring(split(UserIdentityArn, "/")[-1]),
AWSAccountId = coalesce(RecipientAccountId, UserIdentityAccountId)
| extend IsCrossPrincipalKey = (EventName == "CreateAccessKey" and isnotempty(TargetPrincipal) and tolower(TargetPrincipal) != tolower(ActorShort))
| where EventName != "CreateAccessKey" or IsCrossPrincipalKey
| extend ActorArnLower = tolower(ActorArn), TargetLower = tolower(TargetPrincipal)
| join kind=leftouter ApprovedAutomation on $left.AWSAccountId == $right.AutoAccountId and $left.ActorArnLower == $right.PrincipalArnPattern
| where isnull(ValidFrom) or not(TimeGenerated between (ValidFrom .. ValidTo))
| join kind=leftouter PrivChanges on $left.RecipientAccountId == $right.RecipientAccountId and $left.TargetLower == $right.ChangedUser
| summarize
TimeGenerated = min(TimeGenerated),
LatestIngest = max(ingestion_time()),
PersistenceEvents = make_set(EventName, 15),
CorrelatedPrivChanges = take_any(PrivChangeEvents),
SampleRequestParams = tostring(take_any(RequestParameters)),
MfaContext = tostring(take_any(SessionMfaAuthenticated)),
AwsEventId = take_any(AwsEventId)
by AWSAccountId, AWSRegion, ActorArn, ActorShort, TargetPrincipal, SourceIpAddress, UserAgent
| where LatestIngest > ago(15m)
| extend PersistenceSummary = tostring(PersistenceEvents),
PrivChangeSummary = tostring(coalesce(CorrelatedPrivChanges, dynamic([])))
| project TimeGenerated, AwsEventId, AWSAccountId, AWSRegion, ActorArn, AccountName=ActorShort,
TargetPrincipal, SourceIpAddress, UserAgent, MfaContext, PersistenceSummary,
PrivChangeSummary, SampleRequestParamsExplanation
This query is designed to detect suspicious activities related to IAM (Identity and Access Management) credential persistence and cross-principal backdooring in AWS environments. Here's a simplified breakdown:
-
Purpose: The query identifies potentially unauthorized actions involving IAM credentials, such as creating or updating login profiles, enabling MFA (Multi-Factor Authentication), uploading SSH keys, or creating access keys for different users (cross-principal).
-
Exclusions: It excludes actions performed by approved automation processes, which are listed in a watchlist called
AWS_ApprovedAutomationPrincipals. -
Data Source: It uses AWS CloudTrail logs to track IAM-related events over the past two hours, checking every 15 minutes.
-
Detection Criteria:
- Looks for successful IAM operations without errors.
- Focuses on actions like creating or updating login profiles, enabling MFA, and creating access keys, especially when these actions are performed by someone other than the intended user (cross-principal).
- Correlates these actions with recent privilege changes to identify potential backdoor activities.
-
Output: The query generates alerts with details such as the time of the event, AWS account ID, region, actor's identity, target principal, source IP address, and user agent. It also provides summaries of persistence events and correlated privilege changes.
-
Incident Management: If suspicious activity is detected, an incident is created. The incidents are grouped by account and IP address to manage related alerts efficiently.
-
Severity: The severity of the detected activity is marked as high, indicating a significant security concern.
Overall, this query helps security teams monitor and respond to potential security threats involving IAM credential misuse in AWS environments.