The ssh -R port that never asks who is connecting
You set up a reverse tunnel, the device dialled in, and ssh -p 2222 localhost on the jump host lands you at the device's login prompt. It works. Before you move on, try one question: who else on that host can type the sa
You set up a reverse tunnel, the device dialled in, and ssh -p 2222 localhost on the jump host lands you at the device's login prompt. It works. Before you move on, try one question: who else on that host can type the same command?
A note on method first. I had no OpenSSH server in the environment where I wrote this, so none of it was run on a live pair of hosts. It is OpenSSH's documented behaviour. Check it against your version.
What the forward is
The usual shape, run on the device:
ssh -N -R 2222:localhost:22 [email protected]
sshd on the jump host starts listening on 2222 and passes each connection back down the tunnel to the device's own sshd. A TCP listener knows nothing about which key built the tunnel or which user is connecting. Reach the port and you are talking to the device's SSH server, before any login.
So the real question is where the port is bound.
"Loopback only" is a smaller promise than it sounds
GatewayPorts defaults to no, which keeps remote forwards on loopback and ignores a client's request for something wider. That sounds private. It only means that the connecting process must be on the jump host.
Loopback ports have no owner and no file mode. Every engineer with a login, a build agent, a monitoring job, a contractor's service account, and anything an attacker runs under any of them can connect. Your devices are reachable by everyone who can run a command there.
The other settings widen it:
-
GatewayPorts yesbinds forwards on every interface. On a cloud host that can mean the internet. -
GatewayPorts clientspecifiedlets the device choose. An old-R 0.0.0.0:2222:localhost:22pasted from a wiki is enough to open a public listener, and nothing on the host records that the device was allowed to.
You pick one of these once and the next forward inherits it.
What a connection is worth
Three consequences of landing on the device's sshd:
- The device's own configuration is the only gate. Password login on, an old OpenSSH, a default account nobody renamed: all of it is as exposed as on a public address, just to a smaller crowd.
- Source-based controls go blind. The forward is made by the ssh client on the device, which talks to its own sshd over loopback, so every attempt looks like it comes from the device itself. A source-address rule cannot tell users of the jump host apart, and a ban tool that bans the address it sees would ban the path your own tunnel uses. I have not tried every ban tool, so test yours.
- It scales as a list. One port per device means a user cleared for one device can try the next port. Which port is which device usually lives in a script, not in anything that enforces it.
What you can do on your own jump host
All free, all some bookkeeping:
- Leave
GatewayPortsatno. - Give the tunnel user a dedicated no-shell account and a
permitlistenoption per device key (OpenSSH 7.8 or newer) so a device can bind only its own port. That limits what a device can ask for, not who connects afterwards. - Forward to a Unix socket, for example
-R /run/tunnel/dev17.sock:localhost:22. A socket has an owner and a mode, so "who can connect" turns into a permissions question you can answer withls -l. Remember the directory above it matters. OpenSSH supports this from 6.7. I did not run it for this post. - Keep the list of people with a login short, and read it as the list of people who can try every device.
- Turn off password login on the devices.
A small team with one host only they use, GatewayPorts at its default and key-only devices is a reasonable setup. The exposure grows with the number of accounts and devices behind the host.
The design we chose instead
Our agent does not use a forward. It dials out over HTTPS to a relay we run, and the device opens no port. When a shell is requested, the agent connects to the device's own SSH server on the device's own loopback address and nothing else, so there is no middle-host listener to try and no neighbouring port.
The key made available for a session appears only after your key has signed a fresh challenge. It is a short-lived file with no port, agent or X11 forwarding, and it is accepted only for a connection from the device's own loopback address, which is the agent's own connection. The agent refuses password sessions, and the device keeps its own list of trusted keys that our servers cannot add to.
Limits, stated plainly: someone who already has a shell on the device is on that address too, and we do not claim to separate them from their own machine. The device's SSH server is still a listener on the device, and if the image binds to all interfaces the local network can reach it with or without us. It is a relay we run, so if it is down you cannot get a shell through it. We offer no uptime SLA or high availability, and we can see metadata such as who connected to which device and when.
The full post, with the same model laid out step by step, is on the RescueShell blog: https://rescueshell.com/blog/who-can-connect-to-an-ssh-r-forwarded-port/
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.