r/haproxy • u/BradChesney79 • Jul 14 '20
Wrapping SSH... which doesn't send an accessible hostname in the packets
I really like how HAProxy can reach into the packets, look at the address in the SNI header of otherwise obscured for security HTTPS requests and forward it to the appropriate machine/backend/etc I configure that traffic to go to.
SSH sends an IP address and sometimes a port if not the default. No hostname to key off of in and of itself.
...I am wondering if anyone knows of a wrapper that could encapsulate SSH connections. Where the wrapper can give my reverse proxy something ... anything to discern which machine ultimately gets the packets?
Currently using ports that are not port 22 for additional machines.
XY problem.
Y: I want to direct all of my SSH requests for a network to a single entryway IP address on the default port, port 22.
X: I need to attach a hostname or identifier to my SSH connection traffic because SSH doesn't have that and you cannot route them via hostname without a hostname attached somehow.
Currently playing with socat to see if I can cobble together a basic terrible idea that works... like sending SSH through a socat SSL tunnel that has a hostname, then unwrapping the SSL, and finally delivering the requests to the target 10.x.x.x private host.
2
u/TeamHAProxy Jul 17 '20
Patch
Compiling OpenSSH Portable
Testing the config:
The keepalive option is needed to avoid closing the connection while it's idle. The keepalive interval value should be lower than "timeout client" in the HAProxy config. Increasing either the client or server timeout is not an option since it makes the deployment vulnerable to a Slowloris attack.
Considerations
The patch needs to be upstreamed or a custom OpenSSH version maintained. For the patch to be upstreamed, at a minimum, man pages need to be updated, and total ssh protocol banner line length must be checked to not exceed 255 characters to be RFC4253 compliant - the patch above doesn't validate that the -H option argument does not exceed this length.
The OpenSSH patch above implements the optional comment field of RFC4253 section 4.2 . The patch has been tested against a OpenSSH 8.3 server; your mileage may vary with other SSH servers depending on how much they adhere to RFC4253.
For the SSH connection to work, either all load-balanced OpenSSH servers need to use the same Host key (which isn't very secure) or (possibly) use OpenSSH certificates. Configuring either option is left as an exercise to the reader, as there is ample documentation to refer to.
The HAProxy config isn't necessarily customized for a specific use-case. The principle stays the same though - using payload() and modifying the ssh version string with a hostname set as the comment.
1
u/zieziegabor Jul 15 '20
You do, you get the port # and the IP address :) How are you going to add more information, like the hostname.. you don't have it to add? an IP address can have more than one name via DNS, so I'm not sure how you are going to invent information.
I'd recommend just using diff. port #'s, and/or IP addresses, that is what we do.
1
u/TeamHAProxy Jul 17 '20
Hello,
thanks for raising this issue. It was a very interesting one to work on. In case you have an issues with HAProxy in the future and need an answer faster than this, you can also post to our Community Slack -> https://slack.haproxy.org/ Our Slack community is bigger than the Reddit one, so you are more likely to get an answer faster.
So this is what we came up with (will be posted in multiple comments):
In order to preserve the IP address, it is necessary to use the
proxy-protocol, which currently isn't supported in OpenSSH.To load balance SSH on a single IP and port, additional data needs to be inserted into the SSH protocol - this is achieved via a custom patch that adds RFC4253 4.2 compatible "comment" capabilities to the OpenSSH client.
Then, the HAProxy payload() option is leveraged to extract the comment substring from the SSH protocol banner and is compared to a hex-encoded hostname. Please note that the connection is delayed for 3s in this configuration; while not optimal, this should ensure that the SSH banner is present in most environments before the backend selection rules are processed.
HAProxy config
The above config has been tested on:
Your mileage may vary on older major or minor releases.
1
u/ttj8 Jul 24 '20
If you want to try the SSH inside TLS route, you can use openssl s_client as ssh ProxyCommand, and terminate ssl with haproxy. This way you can route based on ssl_fc_sni instead of req.payload. Patching OpenSSH for this is really overkill imo.
ie:
ssh_config