Query Details

P1 03b Vpcflow Ti And Beacon Correlated

Query

// Standalone KQL for Analytic Rule: AWS SecOps P1-03b - VPC Flow Threat-Intel IP Egress and Low-Jitter Beaconing
// Rule File: analytics/P1-03b-vpcflow-ti-and-beacon-correlated.yaml | Rule ID: REPLACE_WITH_RULE_GUID | Severity: High
// Schedule: 1h / Lookback: 14d | REVIEW ONLY - Do not deploy/schedule without approval.
// REVIEW ONLY. Validate AWSVPCFlow and ThreatIntelIndicators schemas first.
let InfraAllowlist = _GetWatchlist('AWS_NetworkInfraAllowlist')
| where tolower(tostring(column_ifexists("Verified", ""))) == "true" and isnotempty(tostring(column_ifexists("InterfaceId", "")))
| project tostring(column_ifexists("InterfaceId", ""));
let ActiveIpTi = ThreatIntelIndicators
| where TimeGenerated > ago(14d)
| summarize arg_max(TimeGenerated, *) by Id
| where IsActive == true and IsDeleted != true and Revoked != true
| where ValidUntil > now() and Confidence >= 65
| where ObservableKey has "ipv4" and isnotempty(ObservableValue)
| project TiIp=ObservableValue, TiId=Id, TiConfidence=Confidence;
let RecentEgress = materialize(
    AWSVPCFlow
    | where TimeGenerated > ago(2h) and LogStatus == "OK" and Action == "ACCEPT"
    | where InterfaceId !in (InfraAllowlist)
    | 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 != 123
);
let TiMatches = RecentEgress
| where ingestion_time() > ago(30m)
| join kind=inner ActiveIpTi on $left.OrigDst == $right.TiIp
| summarize TimeGenerated=min(TimeGenerated), TotalBytes=sum(Bytes), TotalPackets=sum(Packets),
    ActiveBins=dcount(bin(TimeGenerated, 5m))
    by AccountId, Region, VpcId, SubnetId, InterfaceId, InstanceId, OrigSrc, OrigDst, DstPort, Protocol, TiId, TiConfidence
| project TimeGenerated, AWSAccountId=AccountId, AWSRegion=Region, VpcId, SubnetId, InterfaceId,
    InstanceId, SourceIpAddress=OrigSrc, DestinationIpAddress=OrigDst, DstPortStr=tostring(DstPort),
    ProtocolStr=tostring(Protocol), TotalBytesStr=tostring(TotalBytes),
    SignalType=strcat("ThreatIntelIpMatch(Confidence=", TiConfidence, ",Id=", TiId, ")");
let BeaconCandidates = RecentEgress
| summarize BinBytes=sum(Bytes), BinFlows=count()
    by Window5m=bin(TimeGenerated, 5m), AccountId, Region, VpcId, SubnetId, InterfaceId, InstanceId, OrigSrc, OrigDst, DstPort, Protocol
| summarize TimeGenerated=max(Window5m), ActiveBins=count(), AvgBinBytes=avg(todouble(BinBytes)),
    StdevBinBytes=stdev(todouble(BinBytes)), TotalBytes=sum(BinBytes)
    by AccountId, Region, VpcId, SubnetId, InterfaceId, InstanceId, OrigSrc, OrigDst, DstPort, Protocol
| extend CoeffVariation = StdevBinBytes / max_of(AvgBinBytes, 1.0)
| where ActiveBins >= 16 and AvgBinBytes between (64.0 .. 65536.0) and CoeffVariation <= 0.20
| project TimeGenerated, AWSAccountId=AccountId, AWSRegion=Region, VpcId, SubnetId, InterfaceId,
    InstanceId, SourceIpAddress=OrigSrc, DestinationIpAddress=OrigDst, DstPortStr=tostring(DstPort),
    ProtocolStr=tostring(Protocol), TotalBytesStr=tostring(TotalBytes),
    SignalType=strcat("LowJitterPeriodicBeacon(Bins=", ActiveBins, ",CV=", round(CoeffVariation, 3), ")");
union TiMatches, BeaconCandidates

Explanation

This KQL query is designed to detect potential security threats in AWS VPC flow logs by identifying suspicious outbound traffic patterns and matching them against known threat intelligence indicators. Here's a simplified breakdown of what the query does:

  1. Infrastructure Allowlist: It first creates a list of network interfaces that are verified and should be ignored in the analysis. This is done by checking a watchlist called AWS_NetworkInfraAllowlist.

  2. Active Threat Intelligence Indicators: It retrieves active threat intelligence indicators from the ThreatIntelIndicators table, filtering for those that are not expired, have a confidence level of 65 or higher, and are related to IPv4 addresses.

  3. Recent Egress Traffic: It examines recent outbound traffic from the AWSVPCFlow logs within the last 2 hours, focusing on accepted traffic that originates from private IPs and goes to public IPs, excluding certain AWS services and specific ports.

  4. Threat Intelligence Matches: It identifies traffic that matches the threat intelligence indicators by joining the recent egress traffic with the active threat intelligence data. It summarizes this matched traffic by various AWS identifiers and calculates total bytes and packets transferred.

  5. Beaconing Candidates: It analyzes the recent egress traffic for patterns of low-jitter periodic beaconing, which is a common behavior of malware communicating with a command and control server. It looks for traffic with consistent byte sizes and low variation over time, indicating potential beaconing behavior.

  6. Union of Results: Finally, it combines the results of the threat intelligence matches and beaconing candidates into a single output, providing details such as the AWS account, region, VPC, subnet, interface, instance, source and destination IPs, destination port, protocol, total bytes, and a signal type indicating the nature of the potential threat.

The query is set to run every hour and looks back over the last 14 days of data. However, it is marked for review only and should not be deployed without further validation and approval.