Query Details

30 Ueba Aws Network Layer

Query

// REVIEW ONLY workbook query: 9.2 AWS CloudTrail UEBA Network-Layer Behaviors & Live EC2/VPC Network Control-Plane Fallback
let Start = {TimeRange:start};
let End = {TimeRange:end};
let AccountFilter = trim(" ", base64_decode_tostring("{AwsAccount:base64}"));
let UseAllAccounts = AccountFilter in ("", "*");
let NetworkBehaviorActions = dynamic([
    "BehaviorAwsVpnVpcEnumeration", "BehaviorBulkRouteTableReassignment", "BehaviorCloudCidrAllocation",
    "BehaviorCloudIPAMEnumeration", "BehaviorCloudPermissionDiscovery", "BehaviorCloudTopologyEnumeration",
    "BehaviorConcentratedRouteChanges", "BehaviorCrossAccountInterfaceAttach", "BehaviorEc2AclTerraformBurst",
    "BehaviorEc2InterfacePermissionDiscovery", "BehaviorEc2NetworkEnumeration", "BehaviorEndpointCreationEnumeration",
    "BehaviorEphemeralS3Endpoint", "BehaviorGatewayAttach", "BehaviorGatewayEnumerationBurst",
    "BehaviorGatewayEnumerationModification", "BehaviorGatewayPropagationToggle", "BehaviorIaCSecurityGroupManagement",
    "BehaviorIpv6PoolAssignment", "BehaviorMulticastGroupEnumeration", "BehaviorNetworkEnumerationBlocked",
    "BehaviorNetworkInterfaceRapidReattach", "BehaviorNetworkVisibilityEnumeration", "BehaviorPrivateConnectivityEnumeration",
    "BehaviorPrivateEndpointEnumeration", "BehaviorRapidNetworkInsightsDeletion", "BehaviorRapidRuleChange",
    "BehaviorRemoteAccessExposure", "BehaviorRepeatedEC2PolicyUnauthorized", "BehaviorRepeatedFlowLogsDeletion",
    "BehaviorRepeatedMirrorSessionFailures", "BehaviorRepeatedNetworkAnalysisAdmin", "BehaviorRepeatedNetworkInterfaceModificationFailures",
    "BehaviorSubnetEIPReconfig", "BehaviorTransitGatewayConfigBurst", "BehaviorTransitGatewayEnumeration",
    "BehaviorTransitGatewayRouteManipulation", "BehaviorTransitGatewayRouteModification", "BehaviorVPCDefaultRouteTableDeletion",
    "BehaviorVPCPeeringDNSResolution", "BehaviorVpcBlockAccessEnumeration", "BehaviorVpcBulkDeletion",
    "BehaviorVpcCidrReassignment", "BehaviorVpcEndpointAdminActivity", "BehaviorVpcEndpointPermissionChange",
    "BehaviorVpcEndpointServiceCreation", "BehaviorVpcGatewayAttachment", "BehaviorVpcSubnetDeletion"
]);
let UebaNetBehaviors = union isfuzzy=true
    (datatable(TimeGenerated:datetime, BehaviorId:string, ActionType:string, Title:string, Categories:string, AttackTechniques:string, DataSources:string, Description:string, Insights:string, AdditionalFields:string, AccountUpn:string, AccountObjectId:string)[]),
    (SentinelBehaviorInfo | where TimeGenerated between (Start .. End)
     | project TimeGenerated, BehaviorId, ActionType, Title, Categories, AttackTechniques, DataSources, Description, Insights, AdditionalFields, AccountUpn, AccountObjectId),
    (BehaviorInfo | where TimeGenerated between (Start .. End) and ServiceSource == "Microsoft Sentinel"
     | project TimeGenerated, BehaviorId, ActionType, Title=tostring(column_ifexists("Title", ActionType)), Categories, AttackTechniques, DataSources, Description, Insights=tostring(column_ifexists("Insights", "")), AdditionalFields, AccountUpn, AccountObjectId)
| where DataSources has_any ("AWS", "CloudTrail", "Amazon") or AdditionalFields has "AWSCloudTrail"
| where ActionType in (NetworkBehaviorActions)
    or Title has_any ("VPC", "Security Group", "Network ACL", "Transit Gateway", "Internet Gateway", "Route Table", "Flow Logs", "Network Interface", "Peering", "Traffic Mirroring", "Endpoint")
| extend Enrich = parse_json(AdditionalFields)["ueba.enrichments"]
| extend AnomalyFlags = strcat_delim(",",
    iff(Insights has "FirstSeen", "FirstSeen", ""),
    iff(Insights has "HighVolumeAnomaly", "HighVolumeAnomaly", ""),
    iff(Insights has "UncommonValue", "UncommonValue", ""),
    iff(Insights has "ThreatIntel" or isnotempty(tostring(Enrich.ThreatIntelIndicatorType)), "ThreatIntelMatch", ""))
| project TimeGenerated, SourceLayer="🧠 UEBA Behaviors Layer (SentinelBehaviorInfo)",
    NetworkBehavior=coalesce(iff(isnotempty(Title), Title, ""), ActionType),
    ActionOrApi=ActionType, Actor=coalesce(iff(isnotempty(AccountUpn), AccountUpn, ""), AccountObjectId, "See BehaviorEntities"),
    AWSAccountOrScope="AWSCloudTrail", SourceIpOrIsp=strcat(tostring(Enrich.IPLocation), " ", tostring(Enrich.ISP)),
    MitreMapping=strcat(Categories, " | ", AttackTechniques),
    AnomalyInsights=iff(isnotempty(AnomalyFlags), strcat("🔴 ", AnomalyFlags), "🟢 Neutral Behavior Summary"),
    Summary=Description;
let RawCloudTrailNetFallback = union isfuzzy=true
    (datatable(TimeGenerated:datetime, EventSource:string, EventName:string, RecipientAccountId:string, AWSRegion:string, UserIdentityArn:string, SourceIpAddress:string, ErrorCode:string)[]),
    (AWSCloudTrail | where TimeGenerated between (Start .. End)
     | project TimeGenerated, EventSource, EventName, RecipientAccountId, AWSRegion, UserIdentityArn, SourceIpAddress, ErrorCode)
| where (UseAllAccounts or RecipientAccountId == AccountFilter) and EventSource == "ec2.amazonaws.com"
| where EventName in (
    "DeleteFlowLogs", "AuthorizeSecurityGroupIngress", "AuthorizeSecurityGroupEgress", "RevokeSecurityGroupIngress", "RevokeSecurityGroupEgress",
    "CreateNetworkAclEntry", "ReplaceNetworkAclEntry", "CreateInternetGateway", "AttachInternetGateway", "DetachInternetGateway",
    "AcceptVpcPeeringConnection", "CreateVpcPeeringConnection", "ModifyVpcPeeringConnectionOptions",
    "CreateTransitGatewayRoute", "ReplaceTransitGatewayRoute", "CreateRoute", "ReplaceRoute", "DeleteRouteTable",
    "CreateVpcEndpoint", "ModifyVpcEndpoint", "ModifyVpcEndpointServicePermissions", "AttachNetworkInterface",
    "CreateTrafficMirrorSession", "DeleteTrafficMirrorSession")
| summarize LastTime=max(TimeGenerated), Calls=count(), Denied=countif(isnotempty(ErrorCode)), Regions=make_set(AWSRegion, 10)
    by RecipientAccountId, EventName, UserIdentityArn, SourceIpAddress
| extend NetUseCase = case(
    EventName == "DeleteFlowLogs" or EventName has "TrafficMirror", "🔴 Defense Evasion: Flow Log / Traffic Mirror Tampering",
    EventName has "SecurityGroup" or EventName has "NetworkAcl", "🟠 Firewall / Perimeter Rule Modification",
    EventName has "Peering" or EventName == "AttachNetworkInterface", "🟠 Lateral Movement / Cross-VPC Connectivity",
    EventName has "Gateway" or EventName has "Route", "🟡 VPC Routing & Internet/TGW Egress Path Change",
    "🟡 VPC Endpoint / Private Connectivity Modification")
| project TimeGenerated=LastTime, SourceLayer="📡 AWSCloudTrail Network Control-Plane (Live Fallback)",
    NetworkBehavior=NetUseCase, ActionOrApi=EventName, Actor=UserIdentityArn,
    AWSAccountOrScope=RecipientAccountId, SourceIpOrIsp=SourceIpAddress,
    MitreMapping=iff(EventName == "DeleteFlowLogs", "Defense Evasion | T1562.008", "Cloud Network Control Plane | T1578"),
    AnomalyInsights=iff(Denied > 0, strcat("🟠 Calls=", Calls, " Denied=", Denied), strcat("🔵 Calls=", Calls, " Denied=0")),
    Summary=strcat("Regions=", tostring(Regions));
union UebaNetBehaviors, RawCloudTrailNetFallback
| order by SourceLayer desc, TimeGenerated desc
| take 5000

Explanation

This query is designed to analyze and summarize network-related behaviors and activities within AWS CloudTrail and Microsoft Sentinel. Here's a simplified breakdown of what it does:

  1. Time Range and Account Filtering:

    • It sets a time range (Start and End) for the data to be analyzed.
    • It filters the data based on a specific AWS account, unless all accounts are to be used.
  2. Network Behavior Actions:

    • It defines a list of specific network behavior actions that are of interest.
  3. UEBA Network Behaviors:

    • It gathers data from Microsoft Sentinel's behavior information, focusing on network-related actions.
    • It filters this data to include only those actions related to AWS, CloudTrail, or Amazon, and those that match the specified network behaviors or titles.
    • It enriches the data with additional insights and flags anomalies such as "FirstSeen" or "HighVolumeAnomaly".
    • It projects relevant fields like the time of the event, the type of network behavior, the actor involved, and any anomaly insights.
  4. Raw CloudTrail Network Fallback:

    • It collects raw data from AWS CloudTrail, focusing on network control-plane events.
    • It filters events based on the specified AWS account and relevant event names related to network activities.
    • It summarizes the data by counting the number of calls and denied actions, and identifies the regions involved.
    • It categorizes the network use cases, such as defense evasion or firewall rule modifications, and provides insights into the number of calls and denials.
  5. Union and Ordering:

    • It combines the data from UEBA network behaviors and raw CloudTrail events.
    • It orders the results by the source layer and time, and limits the output to the most recent 5000 entries.

Overall, this query is used to monitor and analyze network-layer behaviors and control-plane activities in AWS environments, providing insights into potential security anomalies and changes in network configurations.