Query Details

A05 Workload New Source Aws Secrets

Query

// Standalone KQL for Analytic Rule: AWS cross-cloud - New Entra workload source also retrieves AWS secrets
// Rule File: analytics/A05-workload-new-source-aws-secrets.yaml | Rule ID: 54f1f278-4d53-4f20-a2a1-0e357f02da05 | Severity: Medium
// Schedule: 1h / Lookback: 7d | REVIEW ONLY - Do not deploy/schedule without approval.
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", "")),
    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 IdentityType == "ServicePrincipal"
    and isnotempty(EntraTenantId) and isnotempty(EntraObjectId) and isnotempty(AppClientId);
let KnownSources = materialize(AADServicePrincipalSignInLogs
| where TimeGenerated between (ago(7d) .. ago(2h))
| where ResultType in~ ("0", "Success") and isnotempty(IPAddress)
| project EntraTenantId=tolower(AADTenantId), EntraObjectId=tolower(ServicePrincipalId), SourceIpAddress=IPAddress
| distinct EntraTenantId, EntraObjectId, SourceIpAddress);
let KnownWorkloads = KnownSources | distinct EntraTenantId, EntraObjectId;
let NewSources = AADServicePrincipalSignInLogs
| where TimeGenerated > ago(2h)
| where ResultType in~ ("0", "Success") and isnotempty(IPAddress)
| project SigninTime=TimeGenerated, SigninIngest=ingestion_time(), SigninId=Id,
    EntraTenantId=tolower(AADTenantId), EntraObjectId=tolower(ServicePrincipalId),
    AppClientId=tolower(AppId), SourceIpAddress=IPAddress
| join kind=inner KnownWorkloads on EntraTenantId, EntraObjectId
| join kind=leftanti KnownSources on EntraTenantId, EntraObjectId, SourceIpAddress;
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 NewSources on EntraTenantId, EntraObjectId, AppClientId, SourceIpAddress
| where AwsTime between (SigninTime .. (SigninTime + 30m))
| extend CorrelationIngest=max_of(AwsIngest, SigninIngest)
| where CorrelationIngest > ago(1h)
| summarize arg_max(CorrelationIngest, *) by AwsEventId, EntraTenantId, EntraObjectId
| project TimeGenerated=AwsTime, AwsEventId, EventName, AwsAccessKeyId, AwsPrincipalArn,
    AWSAccountId, RecipientAccountId, SourceIpAddress, EntraTenantId, EntraObjectId,
    AppClientId, SigninId, SigninTime,
    CorrelationBasis="Verified workload/session + new observed source + same IP; NAT still possible";

Explanation

This KQL query is designed to detect potentially suspicious activity involving AWS secrets being accessed by a new source associated with an Entra workload. Here's a simplified breakdown of what the query does:

  1. Identity Mapping: It starts by creating a list of verified AWS identities that are mapped to Entra identities. This mapping includes details like AWS Access Key ID, AWS Principal ARN, AWS Account ID, Entra Tenant ID, and Entra Object ID. Only service principals with valid mapping evidence are considered.

  2. Known Sources: It identifies known source IP addresses for Entra service principals that have successfully signed in over the past 7 days, excluding the last 2 hours.

  3. New Sources: It then looks for new source IP addresses from which these service principals have signed in within the last 2 hours. These are sources that have not been seen in the past 7 days.

  4. AWS CloudTrail Events: The query examines AWS CloudTrail logs for events where the AWS Secrets Manager's GetSecretValue API was called within the last 2 hours. It filters for successful events with no errors and retrieves details like the AWS Access Key ID, AWS Principal ARN, AWS Account ID, and source IP address.

  5. Correlation: The query correlates the AWS events with the new sources of Entra sign-ins. It checks if the AWS event occurred within the valid time window of the identity mapping and within 30 minutes of the Entra sign-in. It ensures that the correlation was ingested within the last hour.

  6. Output: Finally, it outputs a summarized list of these correlated events, including details like the time of the AWS event, AWS and Entra identity details, source IP address, and a note on the correlation basis, indicating that the activity involves a verified workload/session with a new observed source from the same IP address.

This query is part of an analytic rule designed to flag potential cross-cloud security incidents where a new source is accessing AWS secrets, possibly indicating unauthorized access or configuration issues.