Query Details

Fn AWS Actor Normalized

Query

// REVIEW ONLY - Reusable Workspace Function Spec: fn_AWS_ActorNormalized
// Deploy as a saved KQL function (Alias: fn_AWS_ActorNormalized) ONLY after validating AWSCloudTrail schema via D02.
// Below is the standalone testable KQL body (uses Lookback = 24h by default).
let Lookback = 24h;
AWSCloudTrail
| where TimeGenerated > ago(Lookback)
| extend AddData = parse_json(AdditionalEventData),
    Resp = parse_json(ResponseElements),
    ResList = parse_json(Resources)
| extend ActorArn = iff(isempty(UserIdentityArn), tostring(ResList[0].ARN), UserIdentityArn),
    ActorAccountId = coalesce(UserIdentityAccountId, RecipientAccountId),
    ConsoleMfa = tostring(AddData.MFAUsed)
| extend MfaAuthenticated = (SessionMfaAuthenticated == true or ConsoleMfa =~ "Yes"),
    IsSuccess = (isempty(ErrorCode) and isempty(ErrorMessage) and tostring(Resp.ConsoleLogin) !~ "Failure"),
    IsAwsServiceInternal = (UserIdentityType =~ "AWSService" or isnotempty(UserIdentityInvokedBy) or EventTypeName =~ "AwsServiceEvent")
| extend RawActorName = case(
    UserIdentityPrincipalid == "Anonymous", "Anonymous",
    isnotempty(UserIdentityUserName), UserIdentityUserName,
    isnotempty(SessionIssuerUserName), SessionIssuerUserName,
    tostring(split(ActorArn, "/")[-1]))
| extend AccountName = iff(RawActorName contains "@", tostring(split(RawActorName, "@")[0]), RawActorName),
    AccountUPNSuffix = iff(RawActorName contains "@", tostring(split(RawActorName, "@")[1]), "")
| project TimeGenerated, IngestTime=ingestion_time(), AwsEventId, EventSource, EventName, EventTypeName,
    ManagementEvent, ReadOnly, AWSRegion, RecipientAccountId, ActorAccountId, UserIdentityType,
    ActorArn, UserIdentityPrincipalid, UserIdentityAccessKeyId, SessionIssuerArn, SessionIssuerUserName,
    AccountName, AccountUPNSuffix, SourceIpAddress, UserAgent, MfaAuthenticated, IsSuccess,
    IsAwsServiceInternal, ErrorCode, ErrorMessage, RequestParameters, ResponseElements, AdditionalEventData
| take 1000

Explanation

This KQL query is designed to analyze AWS CloudTrail logs over the past 24 hours. Here's a simplified breakdown of what it does:

  1. Data Filtering: It starts by filtering the AWSCloudTrail logs to only include events generated within the last 24 hours.

  2. Data Parsing and Extension:

    • It parses additional event data, response elements, and resources from the logs.
    • It determines the actor's Amazon Resource Name (ARN) and account ID, handling cases where these might be missing.
    • It checks if multi-factor authentication (MFA) was used during the event.
    • It assesses whether the event was successful and if it was an internal AWS service event.
  3. Actor Information:

    • It extracts the raw actor name, handling cases for anonymous users and different user identity types.
    • It splits the actor's name to identify the account name and UPN suffix if applicable.
  4. Data Projection:

    • It selects specific fields to include in the output, such as event details, actor information, and authentication status.
    • It limits the output to the first 1000 records.

Overall, this query is used to normalize and analyze AWS CloudTrail logs, focusing on actor identification, authentication details, and event success status.