Spot SSH SOCKS Pivot Sweeps (KQL and ES|QL)
Detects: an ssh.exe -R session whose connections spray across many IPs and ports with success and failure churning in a tight window: a sweep through the tunnel.
ATT&CK: T1572 Protocol Tunneling, T1090.002 External Proxy
Works on: Microsoft Defender for Endpoint, KQL (DeviceNetworkEvents); Elastic with Sysmon Event IDs 1 and 3 correlated by ProcessGuid, ES|QL
Tested on: my lab, three ssh.exe -R scenarios from one Windows host: three rows, one per scenario
Full write-up: Detecting Reverse SSH SOCKS Tunnels: Separating Benign -R From Akira Pivoting
Collapse the session into one row
The tunnel is opaque, but whatever moves through it leaves a shape: how many destinations, how many ports, how many connections failed. This is the query that pulls benign apart from malicious.
The queries
Published verbatim from the newsletter issue.
KQL (Microsoft Defender for Endpoint):
DeviceNetworkEvents
| where Timestamp > ago(1h)
| where InitiatingProcessFileName =~ "ssh.exe"
| where InitiatingProcessCommandLine matches regex @"\s-\w*R"
| summarize
DistinctRemoteIPs = dcount(RemoteIP),
DistinctRemotePorts = dcount(RemotePort),
Successes = countif(ActionType == "ConnectionSuccess"),
Failures = countif(ActionType == "ConnectionFailed"),
Window = max(Timestamp) - min(Timestamp)
by DeviceName, InitiatingProcessCommandLine

ES|QL (Elastic, Sysmon Event IDs 1 and 3):
// SSH reverse-tunnel detection (Sysmon EID 1 + EID 3 correlated by ProcessGuid)
FROM logs-windows.sysmon_operational-default
| WHERE @timestamp > NOW() - 1 hour
| WHERE event.code IN ("1", "3")
| WHERE process.name == "ssh.exe"
| EVAL is_tunnel_start = event.code == "1"
// hyphen flag-cluster containing the -R (remote forward) flag: -R, -NR, -fNR, -fNTR ...
AND process.command_line RLIKE """.*\s-[A-Za-z]*R.*"""
| STATS tunnel_starts = COUNT(*) WHERE is_tunnel_start,
DistinctRemoteIPs = COUNT_DISTINCT(destination.ip),
DistinctRemotePorts = COUNT_DISTINCT(destination.port),
NetworkConnections = COUNT(*) WHERE event.code == "3",
InitiatingProcessCommandLine = VALUES(process.command_line),
first_seen = MIN(@timestamp),
last_seen = MAX(@timestamp)
BY DeviceName = host.name, process.entity_id
| WHERE tunnel_starts > 0
| EVAL Window = DATE_DIFF("minutes", first_seen, last_seen)
| KEEP DeviceName, InitiatingProcessCommandLine, DistinctRemoteIPs, DistinctRemotePorts,
NetworkConnections, Window, first_seen, last_seen
| SORT DeviceName ASC
| LIMIT 1000

What it catches
The three lab scenarios collapse into three rows:
- A fixed forward to a local service: one IP, one port, every connection succeeds.
- Destination-less SOCKS pointed at a handful of known services: low cardinality, failures near zero, human pacing.
- Destination-less SOCKS sweeping the subnet: many IPs, many ports, successes and failures churning inside a tight window. The scanner never appears in the command line, but its footprint is impossible to miss.
One stable RemoteIP:RemotePort and no failures points to row 1 or 2: verify and write it up. Many distinct destinations with churn in a tight window is row 3: escalate.
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
Low cardinality through a destination-less SOCKS proxy you cannot tie to a known operator is ambiguous on telemetry alone.
Settle it on authorization context, not on the proxy type, and not on whether the target is internal: a malicious sweep produces private RemoteIPs just the same.
Did this query fire in your environment, or miss something? Email me what you saw: ayush@securityinbits.com (open in Gmail). I read every reply, and it decides what I test next.
Questions
Can the shape tell an authorized scan from an attacker?
No. A legitimate operator enumerating through the SOCKS proxy looks like the attack. Fall back on authorization context: a change ticket, a recognized operator, an expected window.
Why does the ES|QL version not count failures?
Sysmon Event ID 3 has no success or fail field. In Elastic the churn signal comes from distinct destinations and the raw connection count instead.
Is ssh -R with no destination always malicious?
No. Benign SOCKS to a handful of known services and the malicious sweep use the identical -R proxy. What pulls them apart is shape.