Who can connect to your ssh -R forwarded port
The tunnel is healthy. The device dialled in, the forward is up, and ssh -p 2222 localhost on the jump host drops you at the device's login prompt. That is the whole point of the setup, and it is also the moment to ask a plain question: who else can type that command?
Our earlier post on what a reverse SSH tunnel leaves open covers a jump host that decides who is trusted. This one is about the listener while everything works, and what a connection to it is worth.
One note on method. We have no OpenSSH server in the environment we wrote this in, so we did not run the commands below on a live pair of hosts. What we say about OpenSSH comes from its documented behaviour. Check it on your version before you rely on it.
The forward asks nobody
A command like this, run on the device, is the usual shape:
ssh -N -R 2222:localhost:22 tunnel@jump.example.net
It asks sshd on the jump host to listen on port 2222 and to hand every connection to that port back down the tunnel, to the device's own sshd. The listener belongs to the tunnel user's sshd process, but it does not check who connects to it. A TCP port has no idea which key opened the tunnel, and no idea which user is knocking. If you can open a connection to it, you get the device's SSH banner.
So the answer to "who can connect" is set by where the port is bound.
Loopback is not private
By default sshd binds a remote forward to the loopback address only. The setting that controls this, GatewayPorts, defaults to no, and in that state a client asking for a wider bind is ignored. Many people read "loopback only" as "safe". It means something narrower: only processes on the jump host can connect.
On a jump host that is not a lot of comfort. Loopback ports have no owner and no file mode. Any account on the machine can connect to port 2222: every engineer with a login, a build agent, a monitoring job, a contractor's service account, and any process an attacker has managed to start under any of them. The tunnel user's own session is not special. Your devices end up reachable by everyone who can run a command on that host, not only by the people you meant.
The other two values change the audience a lot.
- **
GatewayPorts yes** makes sshd bind forwards on all interfaces whatever the client asked for. Your device's login is now offered to every network that can reach the jump host, which on a cloud host may be the internet. - **
GatewayPorts clientspecified** lets the client choose. A device that runs-R 0.0.0.0:2222:localhost:22gets a public listener, and nothing on the jump host says that a particular device was allowed to ask for it. A copy-pasted command from a wiki, written years ago for a lab, is enough.
Both settings are chosen once, on the host, and forgotten. The forward you open next year inherits them.
What a connection to it reaches
The connection arrives at the device's sshd, before any login. Three things follow from that.
The device's own settings are the only gate. If password login is on, anyone who can reach the port can guess passwords against a machine that sits in somebody else's building. A device image with an old OpenSSH, or a default account nobody changed, is as exposed as it would be on a public address, only with a smaller audience. Key-only login and a short list of allowed accounts matter more here than anywhere else, because there is no other layer.
Your usual source controls do not work. The forward is made by the ssh client on the device, which connects to its own sshd on loopback. So sshd on the device sees every attempt as coming from the device itself, whoever started it on the jump host. A rule that limits logins by source address cannot tell the jump host's users apart, and a ban tool that bans the address it sees would ban loopback, which is the path your own tunnel arrives on. We have not tried every ban tool, so check yours.
It is many devices, not one. One port per device is a list. A user who is allowed to reach one device with a ticket can try the neighbouring port as easily. Which port belongs to which device is a convention kept in a script, and nothing enforces it unless you add permitlisten per key, as described below.
Narrowing it, without moving off the model
If you run your own jump host, there are real controls, and they are free.
- Leave
GatewayPortsatnoon the host, so forwarded ports bind to loopback only. - Put the tunnel user in a dedicated account with no shell, and give each device key a
permitlistenoption (OpenSSH 7.8 or newer) so it can bind only its own port. This limits what a device may ask for. It does not limit who may connect afterwards, which is the part this post is about. - Forward to a Unix socket instead of a TCP port, for example
-R /run/tunnel/dev17.sock:localhost:22. A socket has an owner and a file mode, so the question of who can connect becomes a normal permissions question that you can answer withls -l. The mode is set by an sshd setting, and the directory above it matters. OpenSSH supports this from version 6.7; we did not run it for this post. - Keep the people who may log in to the jump host a short list, and treat that list as a list of people who can try every device behind it.
- Turn off password login on the devices.
None of this is exotic, and a team that does all of it has most of the way there. The cost is the bookkeeping: sockets, directories, modes, one more thing to check each time a device is added.
What a device that opens no port changes
The RescueShell agent does not use a forward. It dials out to our relay over HTTPS, and the device opens no port. When a shell is asked for, the agent connects to the device's own SSH server on the device's own loopback address, and to nothing else. Nobody on a middle host has a listener to try. The how it works page describes the same model.
The part that matters for this post is what the sshd on the device is told to accept. A session's key is made available only after your key has signed a fresh challenge. That key is written as a short-lived file, and it carries restrictions: no port forwarding, no agent forwarding, no X11 forwarding, and it is accepted only for a connection that comes from the device's own loopback address, which is the agent's own connection. A login attempt arriving any other way cannot use it. The agent also refuses password sessions, and the device keeps its own list of trusted keys, which our servers cannot add to. See the security page for the plain statement of each.
There is no port per device and no list of ports to keep straight, so there is no neighbouring port to try.
Where this does not fit
- A local login on the device is a different case. The restriction ties the session key to connections made on the device itself. Someone who already has a shell on the device, including the customer, is on that address too. We do not claim to separate them from their own machine.
- The device's SSH server is still a listener on the device. We do not change what it binds to. If the image listens on all interfaces, the customer's network can reach it, with or without us. That is the image's configuration, and the agent does not change it.
- It is a relay run by us. If our servers are down you cannot get a shell through them. We offer no uptime SLA and no high availability, and we can see metadata such as who connected to which device and when.
- Your own jump host can be fine. A handful of devices, a host only you log in to,
GatewayPortsat its default and key-only login on the devices is a reasonable and cheap setup. The exposure described here grows with the number of accounts on the host and the number of devices behind it.
RescueShell: a shell on any machine you shipped. Hosted in the EU, exceptions listed. Try it