Query Details

23 A01 Risky Identity Log To Secrets

Query

// REVIEW ONLY workbook query: 8.1 A01 Preview — AWS cross-cloud - Risky Entra identity pivots from log searches to 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 UserBridge = IdentityMap | where IdentityType == "User";
let RiskyEntra = union isfuzzy=true
  (datatable(EntraTime:datetime, EntraTenantId:string, EntraObjectId:string, RiskLevelDuringSignIn:string, RiskLevelAggregated:string, SignInIp:string, SigninTable:string, CorrelationId:string)[]),
  (SigninLogs
   | where TimeGenerated between (Start .. End)
   | where RiskLevelDuringSignIn in ("medium", "high") or RiskLevelAggregated in ("medium", "high")
   | project EntraTime=TimeGenerated, EntraTenantId=tolower(AADTenantId), EntraObjectId=tolower(UserId),
       RiskLevelDuringSignIn, RiskLevelAggregated, SignInIp=IPAddress, SigninTable="SigninLogs", CorrelationId),
  (AADNonInteractiveUserSignInLogs
   | where TimeGenerated between (Start .. End)
   | where RiskLevelDuringSignIn in ("medium", "high") or RiskLevelAggregated in ("medium", "high")
   | project EntraTime=TimeGenerated, EntraTenantId=tolower(AADTenantId), EntraObjectId=tolower(UserId),
       RiskLevelDuringSignIn, RiskLevelAggregated, SignInIp=IPAddress, SigninTable="AADNonInteractiveUserSignInLogs", CorrelationId);
let LogRecon = SafeCloudTrail
| where EventName in ("FilterLogEvents", "StartQuery", "GetLogEvents")
| where isempty(ErrorCode) and isempty(ErrorMessage)
| where UseAllAccounts or UserIdentityAccountId == AccountFilter or RecipientAccountId == AccountFilter
| summarize ReconStart=min(TimeGenerated), ReconEvents=count(), ReconActions=make_set(EventName, 10)
    by AWSAccountId=UserIdentityAccountId, AwsAccessKeyId=UserIdentityAccessKeyId, AwsPrincipalArn=UserIdentityArn;
let SecretReads = SafeCloudTrail
| where EventSource == "secretsmanager.amazonaws.com" and EventName == "GetSecretValue"
| where isempty(ErrorCode) and isempty(ErrorMessage)
| where UseAllAccounts or UserIdentityAccountId == AccountFilter or RecipientAccountId == AccountFilter
| project SecretTime=TimeGenerated, AwsEventId, AWSAccountId=UserIdentityAccountId, RecipientAccountId,
    AWSRegion, AwsAccessKeyId=UserIdentityAccessKeyId, AwsPrincipalArn=UserIdentityArn, SourceIpAddress, UserAgent;
SecretReads
| join kind=inner UserBridge on AWSAccountId, AwsAccessKeyId, AwsPrincipalArn
| where SecretTime between (ValidFrom .. ValidTo)
| join kind=inner LogRecon on AWSAccountId, AwsAccessKeyId, AwsPrincipalArn
| where ReconStart between ((SecretTime - 1h) .. SecretTime)
| join kind=inner RiskyEntra on EntraTenantId, EntraObjectId
| where EntraTime between ((ReconStart - 2h) .. SecretTime)
| summarize EntraRiskStart=min(EntraTime), RiskLevels=make_set(RiskLevelDuringSignIn, 5),
    SignInIps=make_set(SignInIp, 10), SigninTables=make_set(SigninTable, 4), take_any(ReconStart, ReconEvents, ReconActions)
    by TimeGenerated=SecretTime, AwsEventId, AWSAccountId, RecipientAccountId, AWSRegion,
       AwsPrincipalArn, AwsAccessKeyId, SourceIpAddress, EntraTenantId, EntraObjectId
| order by TimeGenerated desc
| take 5000

Explanation

This query is designed to identify potentially risky activities involving AWS accounts and Entra identities. Here's a simplified breakdown of what it does:

  1. Time Range and Account Filtering: It sets a time range and filters for specific AWS accounts based on input parameters.

  2. Data Collection:

    • CloudTrail Logs: It gathers AWS CloudTrail logs within the specified time range, focusing on events related to log searches and secret accesses.
    • Identity Mapping: It retrieves mappings between AWS identities and Entra identities, ensuring these mappings are verified and valid.
    • Asset Mapping: It collects mappings between AWS assets and their associated identities, ensuring these are also verified and valid.
    • Risky Sign-ins: It gathers sign-in logs from Entra (formerly Azure AD) that have medium or high-risk levels.
  3. Event Analysis:

    • Log Reconnaissance: It identifies AWS log search activities that might indicate reconnaissance behavior.
    • Secret Access: It identifies events where secrets were accessed in AWS Secrets Manager.
  4. Correlation and Risk Assessment:

    • It correlates secret access events with log reconnaissance activities and risky Entra sign-ins.
    • It checks if these events are related by matching AWS and Entra identities and ensuring the events occur within specific time windows.
  5. Output:

    • The query outputs a list of potentially risky activities, including details like the time of the secret access, AWS account information, source IP addresses, and associated Entra risk levels.
    • The results are ordered by the time of the secret access and limited to the most recent 5000 entries.

Overall, this query aims to detect suspicious cross-cloud activities that involve AWS and Entra identities, focusing on scenarios where log searches might precede unauthorized access to secrets.