27 A05 Workload New Source Aws Secrets
Query
// REVIEW ONLY workbook query: 8.5 A05 Preview — AWS cross-cloud - New Entra workload source also retrieves AWS secrets
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 SpBridge = IdentityMap | where IdentityType == "ServicePrincipal" and isnotempty(AppClientId);
let SpSignins = union isfuzzy=true
(datatable(TimeGenerated:datetime, EntraTenantId:string, EntraObjectId:string, AppClientId:string, IPAddress:string, ResultType:string)[]),
(AADServicePrincipalSignInLogs
| where TimeGenerated between ((Start - 7d) .. End)
| where ResultType == "0" and isnotempty(IPAddress)
| project TimeGenerated, EntraTenantId=tolower(AADTenantId), EntraObjectId=tolower(ServicePrincipalId),
AppClientId=tolower(AppId), IPAddress, ResultType);
let BaselineIps = SpSignins
| where TimeGenerated < Start
| distinct EntraTenantId, EntraObjectId, AppClientId, IPAddress;
let NewSpIps = SpSignins
| where TimeGenerated between (Start .. End)
| join kind=leftanti BaselineIps on EntraTenantId, EntraObjectId, AppClientId, IPAddress
| summarize FirstNewSpSignIn=min(TimeGenerated), NewSpIps=make_set(IPAddress, 10)
by EntraTenantId, EntraObjectId, AppClientId;
SafeCloudTrail
| where EventSource == "secretsmanager.amazonaws.com" and EventName == "GetSecretValue"
| where isempty(ErrorCode) and isempty(ErrorMessage)
| where UseAllAccounts or UserIdentityAccountId == AccountFilter or RecipientAccountId == AccountFilter
| project TimeGenerated, AwsEventId, AWSAccountId=UserIdentityAccountId, RecipientAccountId, AWSRegion,
AwsAccessKeyId=UserIdentityAccessKeyId, AwsPrincipalArn=UserIdentityArn, SourceIpAddress
| join kind=inner SpBridge on AWSAccountId, AwsAccessKeyId, AwsPrincipalArn
| where TimeGenerated between (ValidFrom .. ValidTo)
| join kind=inner NewSpIps on EntraTenantId, EntraObjectId, AppClientId
| where FirstNewSpSignIn <= TimeGenerated
| project TimeGenerated, AwsEventId, AWSAccountId, RecipientAccountId, AWSRegion, AwsPrincipalArn,
AwsAccessKeyId, SourceIpAddress, EntraTenantId, EntraObjectId, AppClientId, FirstNewSpSignIn, NewSpIps
| order by TimeGenerated desc
| take 5000Explanation
This KQL query is designed to analyze AWS CloudTrail logs to identify specific events related to AWS Secrets Manager, particularly focusing on the retrieval of secret values. Here's a simplified breakdown of what the query does:
-
Time Range and Filters:
- It defines a time range (
StartandEnd) and filters for specific AWS accounts and tenants using base64 encoded values.
- It defines a time range (
-
Data Collection:
- It gathers data from AWS CloudTrail logs within the specified time range, focusing on events where secrets are accessed (
GetSecretValueevents fromsecretsmanager.amazonaws.com). - It also retrieves identity mappings from a watchlist to correlate AWS identities with Entra (Azure AD) identities.
- It gathers data from AWS CloudTrail logs within the specified time range, focusing on events where secrets are accessed (
-
Identity and Asset Mapping:
- It creates mappings between AWS access keys, principal ARNs, and Entra identities, ensuring these mappings are verified and valid.
- It also maps AWS assets to device IDs and XDR tenant IDs.
-
Service Principal Sign-ins:
- It identifies service principal sign-ins from Azure AD logs, focusing on successful sign-ins and capturing the IP addresses used.
-
IP Address Analysis:
- It distinguishes between baseline IP addresses (used before the start time) and new IP addresses (used during the query period) for service principal sign-ins.
-
Event Filtering and Joining:
- The query filters CloudTrail events to include only those without errors and that match the specified account filters.
- It joins these events with the identity mappings and new IP addresses to find cases where AWS secrets were accessed by service principals using new IP addresses.
-
Output:
- The final result includes details of these events, such as the time, AWS account and region, principal ARN, access key ID, source IP address, and related Entra identity information.
- The results are ordered by time and limited to the most recent 5000 events.
In summary, this query is used to detect potentially suspicious access to AWS secrets by correlating AWS and Entra identities and identifying new IP addresses used by service principals.