Query Details

AWS cross-cloud - New Entra workload source also retrieves AWS secrets

A05 Workload New Source Aws 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", "")),
    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 query is designed to monitor and alert on potentially suspicious activities involving AWS and Azure Entra (formerly Azure Active Directory) service principals. Here's a simplified breakdown:

  1. Purpose: The query checks for instances where a known service principal successfully signs in from a new IP address (not seen in the past week) and then retrieves AWS secrets from the same IP within 30 minutes.

  2. Data Sources: It uses data from AWS CloudTrail and Azure Active Directory (AAD) Service Principal Sign-In Logs.

  3. Process:

    • Identity Mapping: It first identifies valid AWS service principals mapped to Azure Entra identities.
    • Known Sources: It establishes a baseline of known IP addresses from which these service principals have signed in over the past week.
    • New Sources: It identifies new IP addresses from which these service principals have signed in within the last 2 hours.
    • AWS Secret Retrieval: It checks AWS CloudTrail logs for any secret retrieval actions (GetSecretValue) by these service principals from the new IP addresses within 30 minutes of the sign-in.
  4. Alerting: If such an activity is detected, an alert is generated. The alert includes details like the AWS event ID, principal ARN, account IDs, and the IP address involved.

  5. Severity and Tactics: The alert is marked with medium severity and is associated with tactics like Credential Access and Defense Evasion, referencing specific techniques (T1078.004 and T1555.006).

  6. Frequency: The query runs every hour and looks back over the past 7 days for patterns.

This setup helps in identifying potential unauthorized access or misuse of credentials across cloud environments, ensuring that any suspicious activity is promptly reviewed.