AWS cross-cloud - AWS secrets use precedes Entra application credential change
A06 Aws Secrets To Entra App Credentials
Query
let IdentityMap = _GetWatchlist('AWS_EntraIdentityMap')
| where tolower(tostring(column_ifexists("Verified", ""))) == "true" and isnotempty(tostring(column_ifexists("MappingEvidence", "")))
| project AwsAccessKeyId=tostring(column_ifexists("AwsAccessKeyId", "")), AwsPrincipalArn=tostring(column_ifexists("AwsPrincipalArn", "")),
AWSAccountId=tostring(column_ifexists("AWSAccountId", "")), EntraTenantId=tolower(tostring(column_ifexists("EntraTenantId", ""))),
EntraObjectId=tolower(tostring(column_ifexists("EntraObjectId", ""))), IdentityType=tostring(column_ifexists("IdentityType", "")),
ValidFrom=todatetime(column_ifexists("ValidFromUtc", "")), ValidTo=todatetime(column_ifexists("ValidToUtc", ""))
| summarize MappingRows=count(), take_any(*) by AwsAccessKeyId, AwsPrincipalArn, AWSAccountId
| where MappingRows == 1 and IdentityType in ("User", "ServicePrincipal")
and isnotempty(EntraTenantId) and isnotempty(EntraObjectId);
let CredentialChanges = AuditLogs
| where TimeGenerated > ago(2h) and Result =~ "success"
| where OperationName in~ ("Add service principal credentials", "Update application - Certificates and secrets management")
| extend Actor=todynamic(InitiatedBy)
| extend EntraObjectId=tolower(coalesce(tostring(Actor.user.id), tostring(Actor.app.servicePrincipalId))),
IdentityType=iff(isnotempty(tostring(Actor.user.id)), "User", "ServicePrincipal")
| where isnotempty(EntraObjectId)
| mv-expand Target=TargetResources
| where tostring(Target.type) in~ ("Application", "ServicePrincipal") and isnotempty(tostring(Target.id))
| project AuditTime=TimeGenerated, AuditIngest=ingestion_time(), AuditId=Id,
AuditCorrelationId=CorrelationId, OperationName, EntraTenantId=tolower(AADTenantId),
EntraObjectId, IdentityType, TargetObjectId=tostring(Target.id), TargetType=tostring(Target.type);
AWSCloudTrail
| where TimeGenerated > ago(2h)
| where isempty(ErrorCode) and isempty(ErrorMessage) and isnotempty(AwsEventId)
| where EventSource == "secretsmanager.amazonaws.com" and EventName == "GetSecretValue"
| project AwsTime=TimeGenerated, AwsIngest=ingestion_time(), AwsEventId, EventName,
AwsAccessKeyId=UserIdentityAccessKeyId, AwsPrincipalArn=UserIdentityArn,
AWSAccountId=UserIdentityAccountId, RecipientAccountId, SourceIpAddress
| where isnotempty(AwsAccessKeyId) and isnotempty(AwsPrincipalArn) and isnotempty(AWSAccountId)
| join kind=inner IdentityMap on AwsAccessKeyId, AwsPrincipalArn, AWSAccountId
| where AwsTime between (ValidFrom .. ValidTo)
| join kind=inner CredentialChanges on EntraTenantId, EntraObjectId, IdentityType
| where AuditTime between (AwsTime .. (AwsTime + 1h))
| extend CorrelationIngest=max_of(AwsIngest, AuditIngest)
| where CorrelationIngest > ago(15m)
| summarize arg_max(CorrelationIngest, *) by AuditId, TargetObjectId, EntraTenantId, EntraObjectId
| extend UserObjectId=iff(IdentityType == "User", EntraObjectId, "")
| project TimeGenerated=AuditTime, AwsTime, AwsEventId, EventName, AwsAccessKeyId, AwsPrincipalArn,
AWSAccountId, RecipientAccountId, SourceIpAddress, EntraTenantId, EntraObjectId,
IdentityType, UserObjectId, AuditId, AuditCorrelationId, OperationName, TargetObjectId, TargetType,
CorrelationBasis="Verified same AWS/Entra actor + ordered secret-read/credential-change; causality unproven";Explanation
This query is designed to detect potentially suspicious activity involving AWS and Microsoft Entra (formerly Azure Active Directory) accounts. Here's a simplified breakdown:
-
Purpose: The query checks if an AWS secret retrieval is followed by a change in Entra application or service principal credentials within one hour. This could indicate unauthorized access or misuse of credentials.
-
Severity: The alert is marked as "High" severity, suggesting that such an event could pose a significant security risk.
-
Data Sources: It uses data from AWS CloudTrail and Azure Active Directory Audit Logs.
-
Frequency and Period: The query runs every 15 minutes and looks at data from the past 2 hours.
-
Detection Logic:
- It first maps AWS identities to Entra identities using a predefined watchlist.
- It identifies successful credential changes in Entra within the last 2 hours.
- It checks for AWS secret retrieval events from the AWS Secrets Manager within the same timeframe.
- It correlates these events to see if the same user or workload identity retrieved an AWS secret and then changed Entra credentials within one hour.
-
Outcome: If such a sequence of events is detected, an alert is generated. However, the query notes that this does not prove causality, meaning it doesn't confirm that the AWS secret was used to change the Entra credentials, only that the events occurred in sequence.
-
Entity Mappings: The query maps relevant data to account and IP entities for further analysis.
-
Custom Details: It includes specific details like AWS Event ID, Audit ID, and principal identifiers for further investigation.
-
Version and Type: This is version 1.0.0 of a scheduled query, meaning it runs automatically at set intervals.
In essence, this query is a security measure to identify potential credential misuse across AWS and Entra environments by monitoring for specific patterns of activity.