20 H05 Log Secret Ai
Query
// REVIEW ONLY workbook query: 7.1 H05 — Log Credential-Keyword Searches with Secret & Bedrock/SageMaker Follow-Up
let Start = {TimeRange:start};
let End = {TimeRange:end};
let AccountFilter = trim(" ", base64_decode_tostring("{AwsAccount:base64}"));
let UseAllAccounts = AccountFilter in ("", "*");
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 KeywordPattern = @"(?i)(secret|password|passwd|token|apikey|api_key|bearer|authorization|private_key|begin rsa|akias|asia)";
let LogSearches = SafeCloudTrail
| where UseAllAccounts or UserIdentityAccountId == AccountFilter or RecipientAccountId == AccountFilter
| where (EventSource == "logs.amazonaws.com" and EventName in ("FilterLogEvents", "StartQuery", "GetLogEvents"))
or (EventSource == "s3.amazonaws.com" and EventName in ("GetObject", "SelectObjectContent") and RequestParameters has_any ("log", "cloudtrail", "audit"))
| where isempty(ErrorCode) and isempty(ErrorMessage)
| extend HasCredentialKeyword = RequestParameters matches regex KeywordPattern
| summarize FirstSearch=min(TimeGenerated), LastSearch=max(TimeGenerated), SearchEvents=count(),
KeywordSearches=countif(HasCredentialKeyword), SearchApis=make_set(EventName, 10), SourceIps=make_set(SourceIpAddress, 10)
by AWSAccountId=UserIdentityAccountId, AwsAccessKeyId=UserIdentityAccessKeyId, AwsPrincipalArn=UserIdentityArn;
let SecretFollowUp = SafeCloudTrail
| where EventSource in ("secretsmanager.amazonaws.com", "ssm.amazonaws.com")
and EventName in ("GetSecretValue", "GetParameter", "GetParameters", "GetParametersByPath")
| where isempty(ErrorCode) and isempty(ErrorMessage)
| summarize FirstSecretRead=min(TimeGenerated), SecretReads=count(), SecretApis=make_set(EventName, 10)
by AWSAccountId=UserIdentityAccountId, AwsAccessKeyId=UserIdentityAccessKeyId, AwsPrincipalArn=UserIdentityArn;
let AiFollowUp = SafeCloudTrail
| where EventSource has_any ("bedrock", "sagemaker")
and EventName in ("InvokeModel", "InvokeModelWithResponseStream", "Converse", "ConverseStream", "ListFoundationModels", "CreatePresignedDomainUrl")
| summarize FirstAiCall=min(TimeGenerated), AiCalls=count(), AiApis=make_set(EventName, 10)
by AWSAccountId=UserIdentityAccountId, AwsAccessKeyId=UserIdentityAccessKeyId, AwsPrincipalArn=UserIdentityArn;
LogSearches
| join kind=leftouter SecretFollowUp on AWSAccountId, AwsAccessKeyId, AwsPrincipalArn
| join kind=leftouter AiFollowUp on AWSAccountId, AwsAccessKeyId, AwsPrincipalArn
| where KeywordSearches > 0 or isnotempty(SecretReads) or isnotempty(AiCalls)
| extend ChainStage = case(
isnotempty(SecretReads) and isnotempty(AiCalls), "🔴 3-Stage: Log Search + Secret Read + AI Service Call",
isnotempty(SecretReads), "🟠 2-Stage: Log Search + Secret/Parameter Read",
isnotempty(AiCalls), "🟠 2-Stage: Log Search + AI Service Call",
"🟡 1-Stage: Credential-Keyword Log Search Only")
| project FirstSearch, LastSearch, ChainStage, AWSAccountId, AwsPrincipalArn, AwsAccessKeyId,
SearchEvents, KeywordSearches, SearchApis, SecretReads=coalesce(SecretReads, long(0)),
FirstSecretRead, AiCalls=coalesce(AiCalls, long(0)), FirstAiCall, SourceIps
| order by SecretReads desc, KeywordSearches desc, SearchEvents desc
| take 5000Explanation
This query is designed to analyze AWS CloudTrail logs to identify potentially suspicious activities related to credential searches and follow-up actions involving secret management and AI services. Here's a simplified breakdown:
-
Time Range and Account Filtering: The query starts by defining a time range and an AWS account filter. It checks if all accounts should be included or just a specific one.
-
Data Collection: It gathers relevant log data from AWS CloudTrail, focusing on specific events related to log searches, secret management, and AI service usage.
-
Keyword Search Detection: It looks for log search events that contain credential-related keywords (like "secret", "password", etc.) in their request parameters.
-
Summarization of Log Searches: It summarizes these log search events by counting the number of searches, identifying which APIs were used, and noting the source IPs.
-
Secret Management Follow-Up: It checks for any subsequent actions involving secret management services (like AWS Secrets Manager or SSM) and summarizes these actions.
-
AI Service Follow-Up: It also checks for any subsequent actions involving AI services (like AWS Bedrock or SageMaker) and summarizes these actions.
-
Combining Results: The query combines the results of log searches with secret management and AI service follow-ups to identify patterns of activity.
-
Chain Stage Classification: It classifies the activity into stages:
- "🔴 3-Stage": Involves log search, secret read, and AI service call.
- "🟠 2-Stage": Involves either log search + secret read or log search + AI service call.
- "🟡 1-Stage": Only involves credential-keyword log search.
-
Output: Finally, it outputs a list of up to 5000 entries, sorted by the number of secret reads, keyword searches, and search events, providing details like the first and last search times, account IDs, and other relevant information.
This query helps in identifying potential security risks by tracking the sequence of actions that might indicate unauthorized access or misuse of credentials and services.