AWS SecOps P1-03a - VPC Flow Port/Host Scan and High-Byte Rare-Port Egress (Single-Source)
P1 03a Vpcflow Scan And Unusual Egress Baseline
Query
// REVIEW ONLY. Validate AWSVPCFlow v2-v5 field population via discovery/D04 first.
// High-volume environments: point at AWS_VPCFlow_5m_CL (summary-rules/S04).
let InfraAllowlist = _GetWatchlist('AWS_NetworkInfraAllowlist')
| where tolower(tostring(column_ifexists("Verified", ""))) == "true"
| project AllowEni=tostring(column_ifexists("InterfaceId", "")), AllowIp=tostring(column_ifexists("IpOrCidr", ""));
let ExcludedEnis = InfraAllowlist | where isnotempty(AllowEni) | project AllowEni;
let ExcludedIps = InfraAllowlist | where isnotempty(AllowIp) | project AllowIp;
let BaseFlows = materialize(
AWSVPCFlow
| where TimeGenerated > ago(30m) and ingestion_time() > ago(15m)
| where LogStatus == "OK"
| where InterfaceId !in (ExcludedEnis) and SrcAddr !in (ExcludedIps)
| extend OrigSrc = iff(isnotempty(PktSrcAddr), PktSrcAddr, SrcAddr),
OrigDst = iff(isnotempty(PktDstAddr), PktDstAddr, DstAddr)
| where isnotnull(parse_ipv4(OrigSrc)) and isnotnull(parse_ipv4(OrigDst))
);
let InternalScans = BaseFlows
| where ipv4_is_private(OrigSrc) and ipv4_is_private(OrigDst)
| summarize TimeGenerated=min(TimeGenerated), DistinctDstIps=dcount(OrigDst),
DistinctDstPorts=dcount(DstPort), RejectCount=countif(Action == "REJECT"),
TotalFlows=count(), TotalBytes=sum(Bytes), SampleDstIp=take_any(OrigDst),
SamplePort=take_any(DstPort), SampleProto=take_any(Protocol)
by bin(TimeGenerated, 15m), AccountId, Region, VpcId, SubnetId, InterfaceId, InstanceId, OrigSrc
| where (DistinctDstPorts >= 40 and RejectCount >= 30) or (DistinctDstIps >= 50 and RejectCount >= 40)
| project TimeGenerated, AWSAccountId=AccountId, AWSRegion=Region, VpcId, SubnetId, InterfaceId,
InstanceId, SourceIpAddress=OrigSrc, DestinationIpAddress=SampleDstIp,
DstPortStr=tostring(SamplePort), ProtocolStr=tostring(SampleProto), Action="REJECT_BURST",
BytesStr=tostring(TotalBytes), PacketsOrFlowsStr=tostring(TotalFlows),
DetectionPattern=strcat("InternalScan(DstIps=", DistinctDstIps, ",DstPorts=", DistinctDstPorts, ",Rejects=", RejectCount, ")");
let RarePortLargeEgress = BaseFlows
| where Action == "ACCEPT" and (isempty(FlowDirection) or FlowDirection == "egress")
| where ipv4_is_private(OrigSrc) and ipv4_is_private(OrigDst) == false
| where isempty(PktDstAwsService)
| where DstPort !in (80, 443, 123, 53) and DstPort < 32768
| summarize TimeGenerated=min(TimeGenerated), TotalBytes=sum(Bytes), TotalPackets=sum(Packets),
FlowCount=count()
by bin(TimeGenerated, 15m), AccountId, Region, VpcId, SubnetId, InterfaceId, InstanceId,
OrigSrc, OrigDst, DstPort, Protocol
| where TotalBytes >= 104857600
| project TimeGenerated, AWSAccountId=AccountId, AWSRegion=Region, VpcId, SubnetId, InterfaceId,
InstanceId, SourceIpAddress=OrigSrc, DestinationIpAddress=OrigDst,
DstPortStr=tostring(DstPort), ProtocolStr=tostring(Protocol), Action="ACCEPT",
BytesStr=tostring(TotalBytes), PacketsOrFlowsStr=tostring(TotalPackets),
DetectionPattern="HighVolumeRarePortInternetEgress";
union InternalScans, RarePortLargeEgressExplanation
This query is designed to detect unusual network activities in AWS environments by analyzing VPC Flow logs. It focuses on two main types of suspicious activities:
-
Internal Scans: It identifies potential internal network scans by looking for bursts of rejected connection attempts. Specifically, it flags cases where a single source IP tries to connect to 40 or more different ports or 50 or more different internal hosts within a 15-minute window, with a significant number of these attempts being rejected.
-
High-Volume Rare-Port Egress: It detects large data transfers (over 100 MB in 15 minutes) from internal sources to external, non-AWS destinations over uncommon ports (excluding standard ports like 80, 443, 123, and 53). This excludes known AWS infrastructure like NAT gateways and load balancers.
The query uses a watchlist to exclude known infrastructure from the analysis and generates alerts for any detected suspicious activities. These alerts are grouped by IP address and other AWS-specific identifiers to facilitate incident management. The query runs every 15 minutes and considers data from the past hour.