Query Details

AWS cross-cloud - Risky Entra identity pivots from log searches to secrets

A01 Risky Identity Log To Secrets

Query

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 query is designed to detect potentially risky behavior involving AWS and Azure Entra (formerly Azure Active Directory) accounts. Here's a simplified breakdown of what it does:

  1. Purpose: The query aims to identify situations where a high-risk sign-in to an Entra account is linked to AWS activities involving secret searches and retrievals. This is done by correlating different logs to find patterns of suspicious behavior.

  2. Data Sources: It uses data from AWS CloudTrail logs and Azure Entra sign-in logs. Specifically, it looks at AWS secret management activities and Entra sign-in events.

  3. Process:

    • Identity Mapping: It first verifies a mapping between AWS sessions and Entra users, ensuring that the mapping is valid and based on evidence.
    • Secret Retrievals: It checks for AWS events where secrets are accessed, such as retrieving secret values from AWS Secrets Manager or parameters from AWS Systems Manager.
    • Log Searches: It looks for AWS CloudWatch log searches that include patterns related to sensitive information like passwords or secret keys.
    • Risky Sign-ins: It identifies Entra sign-ins that are marked as high-risk or confirmed compromised.
  4. Correlation: The query correlates these events to find cases where:

    • A verified AWS session is linked to an Entra user.
    • There is a sequence of log searches followed by secret retrievals.
    • These activities occur within a specific timeframe of a high-risk Entra sign-in.
  5. Output: If such a pattern is detected, it generates an alert with details about the involved AWS and Entra accounts, the specific events, and the correlation basis.

  6. Severity and Tactics: The alert is marked as high severity, focusing on tactics like credential access, discovery, and defense evasion.

In essence, this query helps security teams identify potential security incidents where compromised Entra identities are used to perform unauthorized secret-related activities in AWS.