Reverse SSH with golang

I have a server and a remote computer. Now I want to access the remote computer via SSH, but it is possible that it is behind NAT. This is one of the reasons why I want a reverse SSH connection.

The idea is to establish a connection from a remote computer to the server and use this connection to communicate via SSH from the server to the remote computer.

With OpenSSH, it's pretty simple. From a remote computer, I open a reverse SSH tunnel:

ssh- R 19999:localhost:22 user@192.168.0.10 

Now I can use this from the server side:

 ssh localhost -p 19999 

I want to achieve the same behavior using the go and go SSH library application on a remote computer. The server will still use OpenSSH, so it should not be changed.

I know how to connect to a computer via SSH, but how to get the back part?

 func main() { config := &ssh.ClientConfig{ User: "vagrant", Auth: []ssh.AuthMethod{ ssh.Password("vagrant"), }, } conn, err := ssh.Dial("tcp", "192.168.0.10:22", config) if err != nil { panic("Failed to dial: " + err.Error()) } } 
+5
source share
2 answers

I have found a solution. OpenSSH returns a terminal session or command output if you supply it. In addition, it allows you to authenticate the connection with the server with the client through an open tunnel.

To support this behavior, I needed to provide an open connection listener to the ssh implementation server on the remote side. Harder than I thought, but it works.

I got a working server example here . I just need to exchange a listener in the sample code for a listener on a client connection.

+3
source

You use Client.Listen (or Client.ListenTCP ) to configure the forwarding port on the server.

You can then accept connections from the returned listener just like any other network listener.

+2
source

Source: https://habr.com/ru/post/1233361/


All Articles