Detection Engineering · Threat Hunting · 2 min read

ssh.exe -R Tunnel Connections in Defender and Elastic

Detects: the outbound connections ssh.exe opens while it acts as the SOCKS client of a -R tunnel.

ATT&CK: T1572 Protocol Tunneling, T1090.002 External Proxy

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

Tested on: my lab, three ssh.exe -R scenarios from one Windows host, on both stacks

Full write-up: Detecting Reverse SSH SOCKS Tunnels: Separating Benign -R From Akira Pivoting

You do not have to see the tool

Each connection ssh.exe opens for the proxy lands in network telemetry, tagged with ssh.exe as the initiating process and the compromised host as the local IP. The outbound connections come from the machine running ssh.exe, not from the C2 box, because SSH dials them on the remote client’s behalf. The tunneled tool’s command line is still nowhere in sight, but the traffic’s shape shows up, and the shape carries enough.

Elastic splits the evidence: Sysmon Event ID 1 holds the -R command line, Event ID 3 holds the destinations and no command line. On its own neither proves the pivot, so the ES|QL version keeps process.entity_id to join them.

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" 
| project Timestamp, DeviceName, ActionType, RemoteIP, RemotePort, RemoteUrl,
    LocalIP, LocalPort, Protocol,
    InitiatingProcessFileName, InitiatingProcessCommandLine, InitiatingProcessParentFileName,
    InitiatingProcessVersionInfoProductVersion, InitiatingProcessIntegrityLevel
| order by Timestamp asc

Defender results: ssh.exe connections, the session to the server on port 22, a loopback hop on 8080, then connections to an internal host on 80, 445, 135 and 389, addresses masked

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

// EID 3 has no CommandLine field — filter by image only, pivot via process.entity_id
FROM logs-windows.sysmon_operational-default
| WHERE @timestamp > NOW() - 1 hour
| WHERE event.code == "3"
  AND process.name == "ssh.exe"
| KEEP @timestamp, host.name, destination.ip, destination.port, destination.domain,
       source.ip, source.port, network.transport,
       process.name, process.entity_id, user.name
| SORT @timestamp ASC
| LIMIT 1000

Elastic results: Sysmon network events for ssh.exe, five connections to the server on port 22, addresses and user masked

What it catches

Every destination and port behind one ssh.exe tunnel, plus the success or failure outcome in Defender: the raw rows the cardinality and churn read is built from.

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

IP reputation is enrichment, never proof.

A workstation reverse-tunneling out to a consumer VPS in another country bumps the priority; it does not settle the verdict. Legitimate admin reverse-forwarding usually terminates at a known bastion, a sanctioned jump host or a documented vendor endpoint, so enrich the tunnel endpoint with ASN and geolocation and treat the result as priority, not proof.

Questions

Why does the ES|QL query not filter on -R?

Sysmon Event ID 3 carries no command line at all. Filter by image, then stitch it back to the Event ID 1 start on process.entity_id, the Sysmon ProcessGuid.

What if the query comes back empty?

The network signal needs activity to exist, so the correlation is empty while the tunnel is idle. The process event is your trigger; this is your confirmation once activity shows up.

More SSH tunnel hunts

#t1572#t1090-002#ransomware#kql#esql#mde#elasticsearch#sysmon

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