P1 03a Vpcflow Scan And Unusual Egress Baseline
Query
// Standalone KQL for Analytic Rule: AWS SecOps P1-03a - VPC Flow Port/Host Scan and High-Byte Rare-Port Egress (Single-Source)
// Rule File: analytics/P1-03a-vpcflow-scan-and-unusual-egress-baseline.yaml | Rule ID: REPLACE_WITH_RULE_GUID | Severity: Medium
// Schedule: 15m / Lookback: 1h | REVIEW ONLY - Do not deploy/schedule without approval.
// 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 KQL query is designed to detect unusual network activities in AWS VPC Flow logs. It focuses on two main patterns: internal scans and high-volume data transfers to rare ports. Here's a simplified breakdown of what the query does:
-
Setup and Filtering:
- It starts by defining an allowlist of network interfaces and IPs that are considered safe and should be excluded from analysis.
- It then filters the AWS VPC Flow logs to include only recent entries (within the last 30 minutes) that are not part of the allowlist and have a status of "OK".
-
Internal Scans Detection:
- The query identifies potential internal scanning activities by looking for private IP addresses communicating with other private IPs.
- It flags cases where a single source IP communicates with a large number of distinct destination IPs or ports, especially if many of these attempts are rejected.
- The criteria for flagging include at least 40 distinct destination ports with 30 rejections or 50 distinct destination IPs with 40 rejections.
-
High-Volume Rare-Port Egress Detection:
- This part of the query looks for large data transfers (over 100 MB) from private IPs to public IPs on uncommon ports (excluding common ports like 80, 443, 123, and 53).
- It focuses on accepted connections that are egressing (leaving the network) and are not directed towards known AWS services.
-
Output:
- The results from both detection patterns are combined and presented with details such as the time of detection, AWS account and region, VPC and subnet IDs, source and destination IPs, ports, protocols, and a description of the detection pattern.
Overall, this query is used to identify suspicious network activities that could indicate security threats, such as port scanning or data exfiltration, within an AWS environment.