Detection Engineering · Threat Hunting · 2 min read

Detect Internal Network Sweeps (KQL and ES|QL)

Detects: any process making rapid connections to many private IPs across multiple ports, in 5-minute buckets.

ATT&CK: T1046 Network Service Discovery, T1018 Remote System Discovery

Works on: Microsoft Defender for Endpoint, KQL (DeviceNetworkEvents); Elastic with Sysmon Event ID 3, ES|QL

Tested on: my lab, against Sliver C2, on both stacks

Full write-up: Hunt Ransomware Before It Encrypts: The Three Tools 45+ Gangs Reach For

The signal that survives everything

Rename the binary, change the hash, strip the metadata: the vendor check is gone. The sweep does not change.

One host hitting 50+ private IPs across 3+ ports inside 5 minutes is not normal user behavior, whichever tool produced it.

That makes this the tool-agnostic backstop to the process and file hunts for NetScan, Advanced IP Scanner and Advanced Port Scanner.

The queries

Published from the newsletter issue with one change: the time window is relative, the last 30 days. Widen it if you need more history.

KQL (Microsoft Defender for Endpoint):

let startTime = ago(30d);
let endTime   = now();
let ipThreshold   = 50;
let portThreshold = 3;
let bucket = 5m;
DeviceNetworkEvents
| where Timestamp between (startTime .. endTime)
| where RemoteIPType == "Private"
| where ActionType startswith "Connection"
| summarize
    DistinctIPs   = dcount(RemoteIP),
    DistinctPorts = dcount(RemotePort),
    SampleIPs     = make_set(RemoteIP, 10),
    SamplePorts   = make_set(RemotePort, 10),
    FirstSeen     = min(Timestamp),
    LastSeen      = max(Timestamp)
    by DeviceName, InitiatingProcessFileName, InitiatingProcessSHA256, bin(Timestamp, bucket)
| where DistinctIPs >= ipThreshold and DistinctPorts >= portThreshold
| sort by DistinctIPs desc

Defender results: advanced_ip_scanner.exe reached 254 private IPs and advanced_port_scanner.exe 190, each on 5 ports in one 5-minute bucket, addresses masked

ES|QL (Elastic, Sysmon Event ID 3):

FROM logs-windows.sysmon_operational-default
| WHERE @timestamp >= NOW() - 30 days AND @timestamp <= NOW()
    AND event.code == "3"
    AND process.name IS NOT NULL
    AND destination.ip IS NOT NULL
    AND CIDR_MATCH(destination.ip, "10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16")
| STATS DistinctIPs   = COUNT_DISTINCT(destination.ip),
        DistinctPorts = COUNT_DISTINCT(destination.port),
        TotalConns    = COUNT(*),
        SampleIPs     = TOP(destination.ip, 10, "asc"),
        SamplePorts   = TOP(destination.port, 10, "asc"),
        FirstSeen     = MIN(@timestamp),
        LastSeen      = MAX(@timestamp)
    BY host.name,
       process.executable,
       time_bucket = BUCKET(@timestamp, 5 minutes)
| WHERE DistinctIPs >= 3 AND DistinctPorts >= 2
| SORT DistinctIPs DESC

Elastic results: two of the rows over the ES|QL thresholds, with distinct IPs, ports and connection counts, addresses masked

What it catches

Per host, per process and per 5-minute bucket: the distinct private IPs and ports, a sample of each, and the first and last connection.

Get the next detection in your inbox

One lab-proven detection per issue: the attacker command, the telemetry it leaves, and the KQL / ES|QL that catches it.

You confirm on Substack in one tap.

No spam. Unsubscribe anytime.

Tuning note

Tune ipThreshold and portThreshold for your environment; the defaults are 50 IPs and 3 ports per 5-minute bucket in KQL, and 3 IPs and 2 ports for Sysmon in ES|QL. Legitimate IT teams scan too: filter out known scanning hosts, such as a vuln scanner or an asset management agent, by hostname or account rather than by tool name.

Questions

Does this need the scanner to keep its name or metadata?

No. It keys on the sweep shape, not the binary, so it catches the activity even if the binary is fully renamed and its metadata is stripped.

What are the default thresholds?

The KQL default is 50 IPs and 3 ports per 5-minute bucket. The Sysmon default in ES|QL is 3 IPs and 2 ports per bucket. Tune both for your environment.

More network scanner hunts

#ransomware#t1018#t1046#kql#esql#mde#elasticsearch#sysmon

New detections go out by email first: subscribe to the newsletter.