10 Cross Account Sensitive Scope
Query
// REVIEW ONLY workbook query: 🔑 4.2 Cross-Account Secrets Manager Reads (`Caller Account != Resource Account`)
let Start = {TimeRange:start};
let End = {TimeRange:end};
let AccountFilter = trim(" ", base64_decode_tostring("{AwsAccount:base64}"));
let UseAllAccounts = AccountFilter in ("", "*");
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);
SafeCloudTrail
| where UseAllAccounts or UserIdentityAccountId == AccountFilter or RecipientAccountId == AccountFilter
| where EventSource == "secretsmanager.amazonaws.com" and EventName == "GetSecretValue"
| where isempty(ErrorCode) and isempty(ErrorMessage)
| where isnotempty(UserIdentityAccountId) and isnotempty(RecipientAccountId)
and UserIdentityAccountId != RecipientAccountId
| project AwsTime=TimeGenerated, AwsEventId, AWSAccountId=UserIdentityAccountId,
RecipientAccountId, AWSRegion, AwsAccessKeyId=UserIdentityAccessKeyId, AwsPrincipalArn=UserIdentityArn, SourceIpAddress
| join kind=leftouter IdentityMap on AWSAccountId, AwsAccessKeyId, AwsPrincipalArn
| extend BridgeState=iff(isnotempty(EntraObjectId) and AwsTime between (ValidFrom .. ValidTo),
"🟢 VERIFIED at AWS event time", "🔴 UNMAPPED / OUTSIDE VALIDITY")
| project AwsTime, BridgeState, CallerAccountId=AWSAccountId, ResourceOwnerAccountId=RecipientAccountId,
AWSRegion, AwsPrincipalArn, AwsAccessKeyId, SourceIpAddress, EntraTenantId, EntraObjectId, AwsEventId
| order by AwsTime desc
| take 5000Explanation
This KQL (Kusto Query Language) query is designed to identify and review instances where AWS Secrets Manager secrets are accessed across different AWS accounts. Here's a simplified breakdown of what the query does:
-
Time Range and Account Filtering:
- The query starts by defining a time range (
StartandEnd) and an account filter (AccountFilter). It checks if all accounts should be used or if a specific account is targeted.
- The query starts by defining a time range (
-
Data Collection:
- It collects data from AWS CloudTrail logs within the specified time range, focusing on events related to AWS Secrets Manager (
secretsmanager.amazonaws.com) where the action isGetSecretValue.
- It collects data from AWS CloudTrail logs within the specified time range, focusing on events related to AWS Secrets Manager (
-
Cross-Account Access:
- The query filters for events where the account accessing the secret (
UserIdentityAccountId) is different from the account owning the secret (RecipientAccountId), indicating cross-account access.
- The query filters for events where the account accessing the secret (
-
Error Filtering:
- It ensures that only successful access attempts are considered by checking that there are no error codes or messages.
-
Identity Mapping:
- The query attempts to map AWS access keys and principal ARNs to Entra (Azure AD) identities using a watchlist (
AWS_EntraIdentityMap). It verifies if the mapping is valid and within a specified validity period.
- The query attempts to map AWS access keys and principal ARNs to Entra (Azure AD) identities using a watchlist (
-
Verification Status:
- For each event, it determines whether the identity mapping is verified and valid at the time of the AWS event. It marks the status as "🟢 VERIFIED" if valid, or "🔴 UNMAPPED / OUTSIDE VALIDITY" if not.
-
Output:
- The query outputs a list of up to 5000 events, showing details such as the time of the event, verification status, caller account ID, resource owner account ID, AWS region, AWS principal ARN, AWS access key ID, source IP address, Entra tenant ID, and Entra object ID.
-
Sorting:
- The results are sorted by the time of the event in descending order.
In summary, this query helps identify and review cross-account access to AWS Secrets Manager secrets, verifying if the access is legitimate based on identity mappings to Azure AD.