Query Details

A01 Risky Identity Log To Secrets

Query

// Standalone KQL for Analytic Rule: AWS cross-cloud - Risky Entra identity pivots from log searches to secrets
// Rule File: analytics/A01-risky-identity-log-to-secrets.yaml | Rule ID: 54f1f278-4d53-4f20-a2a1-0e357f02da01 | Severity: High
// Schedule: 15m / Lookback: 2h | 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", "")),
    ValidFrom=todatetime(column_ifexists("ValidFromUtc", "")), ValidTo=todatetime(column_ifexists("ValidToUtc", ""))
| summarize MappingRows=count(), take_any(*) by AwsAccessKeyId, AwsPrincipalArn, AWSAccountId
| where MappingRows == 1 and IdentityType == "User"
    and isnotempty(EntraTenantId) and isnotempty(EntraObjectId);
let Secrets = AWSCloudTrail
| where TimeGenerated > ago(2h)
| where isempty(ErrorCode) and isempty(ErrorMessage) and isnotempty(AwsEventId)
| where (EventSource == "secretsmanager.amazonaws.com" and EventName == "GetSecretValue")
    or (EventSource == "ssm.amazonaws.com" and EventName in ("GetParameter", "GetParameters", "GetParametersByPath")
        and tobool(parse_json(RequestParameters).withDecryption) == true)
| project AwsTime=TimeGenerated, AwsIngest=ingestion_time(), AwsEventId, EventName,
    AwsAccessKeyId=UserIdentityAccessKeyId, AwsPrincipalArn=UserIdentityArn,
    AWSAccountId=UserIdentityAccountId, RecipientAccountId, SourceIpAddress, AWSRegion
| where isnotempty(AwsAccessKeyId) and isnotempty(AwsPrincipalArn) and isnotempty(AWSAccountId)
| join kind=inner IdentityMap on AwsAccessKeyId, AwsPrincipalArn, AWSAccountId
| where AwsTime between (ValidFrom .. ValidTo);
let Searches = AWSCloudTrail
| where TimeGenerated > ago(2h)
| where EventSource == "logs.amazonaws.com" and EventName == "FilterLogEvents"
| where isempty(ErrorCode) and isempty(ErrorMessage) and isnotempty(AwsEventId)
| where tostring(parse_json(RequestParameters).filterPattern) matches regex @"(?i)(password|secret|AKIA)"
| project SearchTime=TimeGenerated, SearchIngest=ingestion_time(), SearchEventId=AwsEventId,
    AwsAccessKeyId=UserIdentityAccessKeyId, AwsPrincipalArn=UserIdentityArn, AWSAccountId=UserIdentityAccountId;
let RiskySignins = union SigninLogs, AADNonInteractiveUserSignInLogs
| where TimeGenerated > ago(2h)
| where ResultType in~ ("0", "Success")
| where RiskLevelDuringSignIn =~ "high" or RiskState =~ "confirmedCompromised"
| project SigninTime=TimeGenerated, SigninIngest=ingestion_time(), SigninId=Id,
    EntraTenantId=tolower(AADTenantId), EntraObjectId=tolower(UserId), UserPrincipalName;
Secrets
| join kind=inner Searches on AwsAccessKeyId, AwsPrincipalArn, AWSAccountId
| where SearchTime between (ValidFrom .. ValidTo)
    and AwsTime between (SearchTime .. (SearchTime + 10m))
| join kind=inner RiskySignins on EntraTenantId, EntraObjectId
| where SearchTime between (SigninTime .. (SigninTime + 1h))
| extend CorrelationIngest=max_of(AwsIngest, SearchIngest, SigninIngest)
| where CorrelationIngest > ago(15m)
| summarize arg_max(CorrelationIngest, *) by AwsEventId, EntraTenantId, EntraObjectId
| project TimeGenerated=AwsTime, AwsEventId, SearchEventId, SearchTime, SigninId, SigninTime,
    EventName, AwsAccessKeyId, AwsPrincipalArn, AWSAccountId, RecipientAccountId,
    SourceIpAddress, AWSRegion, EntraTenantId, EntraObjectId, UserPrincipalName,
    CorrelationBasis="Verified session identity + ordered log-to-secret sequence";

Explanation

This KQL query is designed to detect potentially risky behavior involving AWS and Microsoft Entra (formerly Azure AD) identities. Here's a simplified breakdown of what the query does:

  1. Identity Mapping: It starts by creating a map of verified AWS identities linked to Entra identities. This map includes details like AWS Access Key ID, AWS Principal ARN, AWS Account ID, Entra Tenant ID, and Entra Object ID. Only identities verified as "true" and with mapping evidence are considered.

  2. Secrets Access: It looks for AWS CloudTrail events where secrets are accessed (e.g., using AWS Secrets Manager or Systems Manager Parameter Store) within the last 2 hours. It filters out any events with errors and ensures the events are linked to the identities in the map.

  3. Log Searches: It searches for AWS CloudTrail events where logs are filtered for sensitive information (like passwords or AWS access keys) within the last 2 hours. Again, it ensures these events are linked to the identities in the map.

  4. Risky Sign-ins: It checks for high-risk or confirmed compromised sign-ins from Entra logs within the last 2 hours. It focuses on successful sign-ins with high risk levels.

  5. Correlation: The query correlates these three activities (secrets access, log searches, and risky sign-ins) to identify cases where:

    • A log search for sensitive information is followed by a secret access within 10 minutes.
    • The log search occurs within an hour of a risky sign-in.
  6. Output: It outputs details of these correlated events, including timestamps, event IDs, AWS and Entra identity details, and the source IP address. The correlation is based on verified session identity and the sequence of events.

This query is part of an analytic rule designed to flag high-severity security incidents where an identity may be pivoting from searching logs for sensitive information to accessing secrets, potentially indicating malicious activity.