Query Details

H13 Vpcflow Rare Port And Asymmetric Egress Hunt

Query

// REVIEW ONLY - Hunting Query H13: VPC Flow First-Seen External Destination Ports & Asymmetric Egress Ratio
// Identifies workloads sending high outbound-to-inbound byte ratios to uncommon external ports.
let HistoricalPorts = AWSVPCFlow
| where TimeGenerated between (ago(7d) .. ago(1d)) and Action == "ACCEPT"
| where ipv4_is_private(DstAddr) == false and isempty(PktDstAwsService)
| distinct VpcId, DstPort;
AWSVPCFlow
| where TimeGenerated > ago(24h) and LogStatus == "OK" and Action == "ACCEPT"
| extend OrigSrc = iff(isnotempty(PktSrcAddr), PktSrcAddr, SrcAddr),
    OrigDst = iff(isnotempty(PktDstAddr), PktDstAddr, DstAddr)
| where ipv4_is_private(OrigSrc) and isnotnull(parse_ipv4(OrigDst)) and ipv4_is_private(OrigDst) == false
| where isempty(PktDstAwsService) and DstPort < 32768
| summarize TotalBytes=sum(Bytes), TotalPackets=sum(Packets), Flows=count(),
    FirstSeen=min(TimeGenerated), LastSeen=max(TimeGenerated)
    by AccountId, Region, VpcId, SubnetId, InterfaceId, InstanceId, OrigSrc, OrigDst, DstPort, Protocol
| join kind=leftouter (HistoricalPorts | extend SeenBefore=true) on VpcId, DstPort
| where coalesce(SeenBefore, false) == false or TotalBytes >= 52428800
| extend PortNovelty = iff(coalesce(SeenBefore, false), "KnownPortHighVolume", "NewExternalPortIn7d")
| order by TotalBytes desc
| take 1000

Explanation

This query is designed to identify unusual network activity in AWS VPC (Virtual Private Cloud) flows by focusing on external destination ports that have not been seen in the past week or have high outbound-to-inbound byte ratios. Here's a simplified breakdown:

  1. Historical Data Collection: The query first gathers a list of external destination ports that have been accessed in the past week (excluding the last day) from accepted VPC flow logs. It filters out private IP addresses and AWS service endpoints.

  2. Current Data Analysis: It then examines VPC flow logs from the last 24 hours, focusing on accepted connections with valid source and destination IPs. It filters for connections to external (non-private) IP addresses on ports below 32768, excluding AWS service endpoints.

  3. Data Aggregation: The query calculates the total bytes, packets, and flow count for each unique combination of account, region, VPC, subnet, interface, instance, source, destination, port, and protocol. It also notes the first and last time each flow was seen.

  4. Comparison with Historical Data: It checks whether each destination port was seen in the past week. If a port is new or if the total bytes transferred are 50 MB or more, it flags these flows.

  5. Result Categorization: The query categorizes the results into "KnownPortHighVolume" for high-volume traffic on known ports and "NewExternalPortIn7d" for traffic on new ports.

  6. Output: Finally, it orders the results by total bytes in descending order and limits the output to the top 1000 entries.

This query helps identify potentially suspicious network activity by highlighting new or high-volume traffic to external ports.