Query Details

19 Network Scans And Rare Port Egress

Query

// REVIEW ONLY - AWS Security Operations Workbook Visual Query
// Tab: Network | Panel: scans-and-rare-port-egress | Visualization: table | Mode: All
// Security Question: Which internal ENIs/instances are scanning peers or transferring large volumes out over uncommon ports?
// Drill-Through: Click a row to set SelectedIp and inspect Panel 5.4 Raw Flow Drill-Through below.
// Empty/No-Data Contract: No scan bursts or rare-port high-byte egress matched thresholds in window.
let Start = {TimeRange:start};
let End = {TimeRange:end};
let OrgFilter = base64_decode_tostring("{Organization:base64}");
let AccountFilter = base64_decode_tostring("{AwsAccount:base64}");
let RegionFilter = base64_decode_tostring("{AwsRegion:base64}");
let EnvFilter = base64_decode_tostring("{Environment:base64}");
let WorkloadFilter = base64_decode_tostring("{Workload:base64}");
let SeverityFilter = base64_decode_tostring("{Severity:base64}");
let SourceFilter = base64_decode_tostring("{SourceTable:base64}");
let SummaryPref = base64_decode_tostring("{SummaryMode:base64}");
let SelectedPrincipalFilter = base64_decode_tostring("{SelectedPrincipal:base64}");
let SelectedIpFilter = base64_decode_tostring("{SelectedIp:base64}");
union isfuzzy=true
    (datatable(TimeGenerated:datetime, LogStatus:string, AccountId:string, Region:string, VpcId:string, InterfaceId:string, InstanceId:string, PktSrcAddr:string, SrcAddr:string, PktDstAddr:string, DstAddr:string, Action:string, Bytes:long, DstPort:int, PktDstAwsService:string)[]),
    (AWSVPCFlow | where TimeGenerated between (Start .. End)
     | project TimeGenerated, LogStatus, AccountId, Region, VpcId, InterfaceId, InstanceId, PktSrcAddr, SrcAddr, PktDstAddr, DstAddr, Action, Bytes, DstPort, PktDstAwsService)
| where LogStatus == "OK"
| where (AccountFilter == "*" or AccountId == AccountFilter) and (RegionFilter == "*" or Region == RegionFilter)
| extend SourceIp = iff(isnotempty(PktSrcAddr), PktSrcAddr, SrcAddr),
    DestIp = iff(isnotempty(PktDstAddr), PktDstAddr, DstAddr)
| where isnotnull(parse_ipv4(SourceIp)) and isnotnull(parse_ipv4(DestIp)) and ipv4_is_private(SourceIp)
| summarize Flows=count(), Rejects=countif(Action == "REJECT"), TotalMB=round(sum(todouble(Bytes)) / 1048576.0, 2),
    DistinctDstIps=dcount(DestIp), DistinctDstPorts=dcount(DstPort),
    RareExternalPorts=make_set_if(DstPort, ipv4_is_private(DestIp) == false and isempty(PktDstAwsService) and DstPort !in (80, 443, 53, 123) and DstPort < 32768, 10)
    by AccountId, Region, VpcId, InterfaceId, InstanceId, SourceIp
| where (DistinctDstPorts >= 30 and Rejects >= 20) or (DistinctDstIps >= 40 and Rejects >= 25) or (TotalMB >= 50.0 and array_length(RareExternalPorts) > 0)
| order by TotalMB desc, Rejects desc
| take 5000

Explanation

This query is designed to identify internal AWS Elastic Network Interfaces (ENIs) or instances that are either scanning other systems or transferring large amounts of data through uncommon ports. Here's a simplified breakdown of what the query does:

  1. Time Range and Filters: It starts by setting a time range and various filters based on organization, account, region, environment, workload, severity, source table, and selected principal or IP.

  2. Data Source: It pulls data from AWS VPC Flow logs within the specified time range, focusing on logs with a status of "OK".

  3. IP Address Processing: It determines the source and destination IP addresses, ensuring they are valid IPv4 addresses and that the source IP is private.

  4. Data Aggregation: The query summarizes the data by counting the number of flows, rejected actions, total megabytes transferred, distinct destination IPs, and distinct destination ports. It also identifies rare external ports that are not commonly used (excluding ports like 80, 443, 53, and 123).

  5. Criteria for Concern: It filters the results to highlight instances where:

    • There are at least 30 distinct destination ports and 20 rejections.
    • There are at least 40 distinct destination IPs and 25 rejections.
    • At least 50 MB of data is transferred through rare external ports.
  6. Sorting and Limiting Results: The results are sorted by the total megabytes transferred and the number of rejections, and the top 5000 entries are taken.

The purpose of this query is to help security teams identify potential security issues, such as unauthorized scanning or data exfiltration, by highlighting unusual network activity.