28 A06 Aws Secrets To Entra App Credentials
Query
// REVIEW ONLY workbook query: 8.6 A06 Preview — AWS cross-cloud - AWS secrets use precedes Entra application credential change
let Start = {TimeRange:start};
let End = {TimeRange:end};
let AccountFilter = trim(" ", base64_decode_tostring("{AwsAccount:base64}"));
let UseAllAccounts = AccountFilter in ("", "*");
let TargetXdrTenant = tolower(trim(" ", base64_decode_tostring("{XdrTenantId: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 IdentityMap = union isfuzzy=true
(datatable(SearchKey:string)[]),
(_GetWatchlist('AWS_EntraIdentityMap'))
| where isnotempty(tostring(column_ifexists("SearchKey", "")))
| extend VerifiedVal = tolower(tostring(column_ifexists("Verified", ""))),
MappingEvidenceVal = tostring(column_ifexists("MappingEvidence", ""))
| where VerifiedVal == "true" and isnotempty(MappingEvidenceVal)
| 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", "")),
AppClientId=tolower(tostring(column_ifexists("AppClientId", ""))),
ValidFrom=todatetime(column_ifexists("ValidFromUtc", "")),
ValidTo=todatetime(column_ifexists("ValidToUtc", ""))
| summarize MappingRows=count(), take_any(*) by AwsAccessKeyId, AwsPrincipalArn, AWSAccountId
| where MappingRows == 1 and isnotempty(EntraTenantId) and isnotempty(EntraObjectId);
let AssetMap = union isfuzzy=true
(datatable(SearchKey:string)[]),
(_GetWatchlist('AWS_AssetIdentityMap'))
| where isnotempty(tostring(column_ifexists("SearchKey", "")))
| extend VerifiedVal = tolower(tostring(column_ifexists("Verified", ""))),
MappingEvidenceVal = tostring(column_ifexists("MappingEvidence", ""))
| where VerifiedVal == "true" and isnotempty(MappingEvidenceVal)
| project AWSAccountId=tostring(column_ifexists("AWSAccountId", "")),
AwsPrincipalArn=tostring(column_ifexists("AwsPrincipalArn", "")),
AwsAccessKeyId=tostring(column_ifexists("AwsAccessKeyId", "")),
DeviceId=tostring(column_ifexists("DeviceId", "")),
XdrTenantId=tolower(tostring(column_ifexists("XdrTenantId", ""))),
AwsInstanceId=tostring(column_ifexists("AwsInstanceId", "")),
ValidFrom=todatetime(column_ifexists("ValidFromUtc", "")),
ValidTo=todatetime(column_ifexists("ValidToUtc", ""))
| summarize MappingRows=count(), take_any(*) by AWSAccountId, AwsPrincipalArn, AwsAccessKeyId
| where MappingRows == 1 and isnotempty(DeviceId) and isnotempty(AwsInstanceId) and isnotempty(AwsAccessKeyId);
let SecretReads = SafeCloudTrail
| where EventSource == "secretsmanager.amazonaws.com" and EventName == "GetSecretValue"
| where isempty(ErrorCode) and isempty(ErrorMessage)
| where UseAllAccounts or UserIdentityAccountId == AccountFilter or RecipientAccountId == AccountFilter
| project AwsSecretTime=TimeGenerated, AwsEventId, AWSAccountId=UserIdentityAccountId, RecipientAccountId,
AWSRegion, AwsAccessKeyId=UserIdentityAccessKeyId, AwsPrincipalArn=UserIdentityArn, SourceIpAddress
| join kind=inner IdentityMap on AWSAccountId, AwsAccessKeyId, AwsPrincipalArn
| where AwsSecretTime between (ValidFrom .. ValidTo);
let AppCredChanges = union isfuzzy=true
(datatable(AuditTime:datetime, OperationName:string, Result:string, EntraObjectId:string, TargetResources:dynamic, CorrelationId:string)[]),
(AuditLogs
| where TimeGenerated between (Start .. End)
| where Result =~ "success"
| where OperationName has_any ("Update application – Certificates and secrets management", "Add service principal credentials", "Update service principal", "Add owner to application", "Add owner to service principal")
| extend InitUser = tolower(tostring(parse_json(tostring(InitiatedBy.user)).id)),
InitSp = tolower(tostring(parse_json(tostring(InitiatedBy.servicePrincipal)).id))
| extend EntraObjectId = coalesce(iff(isnotempty(InitUser), InitUser, ""), InitSp)
| where isnotempty(EntraObjectId)
| project AuditTime=TimeGenerated, OperationName, Result, EntraObjectId, TargetResources=todynamic(TargetResources), CorrelationId);
SecretReads
| join kind=inner AppCredChanges on EntraObjectId
| where AuditTime between (AwsSecretTime .. (AwsSecretTime + 1h))
| extend TargetDisplayName = tostring(TargetResources[0].displayName), TargetId = tostring(TargetResources[0].id)
| project TimeGenerated=AuditTime, AwsSecretTime, AwsEventId, AWSAccountId, RecipientAccountId, AWSRegion,
AwsPrincipalArn, AwsAccessKeyId, SourceIpAddress, EntraTenantId, EntraObjectId, OperationName, TargetDisplayName, TargetId, CorrelationId
| order by TimeGenerated desc
| take 5000Explanation
This query is designed to identify instances where AWS secrets are accessed shortly before changes are made to Entra application credentials. Here's a simplified breakdown of what the query does:
-
Time Range and Account Filters: It sets up a time range and filters for specific AWS accounts and Entra tenants.
-
Data Collection:
- CloudTrail Data: It gathers AWS CloudTrail logs related to secret access events (
GetSecretValuefromsecretsmanager.amazonaws.com) within the specified time range. - Identity Mapping: It retrieves mappings between AWS identities and Entra identities from a watchlist, ensuring these mappings are verified and valid.
- Asset Mapping: It retrieves mappings between AWS assets and their associated information, ensuring these mappings are verified and valid.
- CloudTrail Data: It gathers AWS CloudTrail logs related to secret access events (
-
Secret Access Events:
- It filters for successful secret access events (no errors) and matches them with the identity mappings to ensure the access is valid and within the specified accounts.
-
Application Credential Changes:
- It collects logs of successful operations related to changes in application credentials (like adding/updating certificates, secrets, or owners) from Entra audit logs.
-
Correlation:
- It joins the secret access events with the application credential change events, looking for cases where a secret was accessed within one hour before a credential change.
-
Output:
- The query outputs a list of these correlated events, including details like the time of the events, AWS and Entra identifiers, operation names, and target resources, sorted by the time of the credential change.
-
Limit:
- The results are limited to the most recent 5000 events.
This query helps in identifying potential security risks by highlighting situations where AWS secrets are accessed just before changes are made to application credentials, which could indicate suspicious activity.