# Windows - OpenSSH

![](https://3357255970-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fb7weUpu7VBcMnESSH8vG%2Fuploads%2FjLoKNVGoCz147gMAfpeG%2FReverse%20SSH%20Tunnel.jpg?alt=media\&token=ccf52ea8-c563-4871-a09b-839a354f7e55)

## Definitions

**KCM Server:** The instance running Keeper Connection Manager.

**Remote Endpoint:** A target Windows instance in a protected network without data ingress which cannot yet be accessed directly by the KCM Server.

## Prerequisite - Install OpenSSH Client and Server

Good news, Windows now comes with OpenSSH! However, it may not be installed by default.  We recommend Installing both the OpenSSH Client and the OpenSSH Server.

{% tabs %}
{% tab title="PowerShell" %}

```
# Install the OpenSSH Client
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0

# Install the OpenSSH Server
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
```

{% endtab %}

{% tab title="GUI" %}
OpenSSH can be found in "Optional Features" in Windows 10+ and Windows Server 2019+.  You can install it from Settings > Apps > Optional Features > Add Feature > Open SSH Client / Server.
{% endtab %}
{% endtabs %}

Microsoft's instructions for installing OpenSSH are here: <https://learn.microsoft.com/en-us/windows-server/administration/openssh/openssh_install_firstuse>

## Reverse Tunnel Setup

The instructions below outline how to establish a connection from a KCM Server in the cloud, to a Remote Endpoint without network ingress.

**(1) Allow inbound SSH on KCM Server**

On the KCM Server, ensure that *inbound* SSH port 22 connections are open from the Remote Server to the KCM instance. We will be establishing an outbound connection from the Remote Server to the KCM instance to set up the reverse tunnel.

**(2) Generate SSH Keys on the Remote Endpoint**

On the Windows Remote Endpoint, create an SSH key pair which will be used to establish an *outbound* connection from the Endpoint to the KCM Server. Enter the following into an elevated command prompt:

```
ssh-keygen -t ed25519
```

This will create two files, a private key and a public key.  Leave the private key in place.

<figure><img src="https://3357255970-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fb7weUpu7VBcMnESSH8vG%2Fuploads%2FWcZNovgOlQw21mz5v1Kh%2Fchrome_axfM2DC6A2.png?alt=media&#x26;token=161f163a-5e2a-431d-bc9d-902fc26dae1b" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3357255970-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fb7weUpu7VBcMnESSH8vG%2Fuploads%2F6UdScxEkTfKULEjROHgb%2Fchrome_bO0Jz0t6j1.png?alt=media&#x26;token=8f7e2a6f-f295-4c75-9bd1-653128fb1dc3" alt=""><figcaption></figcaption></figure>

Next, we will copy the **public key file (.pub)** from the windows endpoint to the KCM Server.

{% hint style="success" %}
You can copy the .pub file using any method you choose
{% endhint %}

If you have outbound traffic allowed, you can use the following command in PowerShell as Administrator:

```powershell
PS C:\Users\Administrator\.ssh> scp id_ed25519.pub user@kcm.server.xyz:~/.ssh/authorized_keys
```

<figure><img src="https://3357255970-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fb7weUpu7VBcMnESSH8vG%2Fuploads%2FkSDjgJ5rgjH7ltJ7MxjQ%2Fchrome_tfkgvSvn4l.png?alt=media&#x26;token=9553e41e-7f4b-43ef-bb9b-8dc11b775e29" alt=""><figcaption></figcaption></figure>

**(3) Verify SSH Connectivity from Remote Endpoint to KCM Server**

You should now be able to SSH from the remote server into the KCM server, without any prompt.

```
C:\Users\Administrator> ssh username@demo.kcmdemo.com

Last login: Mon Jul  4 20:28:10 2022 from ip-10-0-1-7.my.remote
```

**(4) Establish the ssh tunnel**

{% hint style="warning" %}
Make sure that you have a firewall in place to block inbound connections on all ports except what is needed (HTTP/HTTPS/SSH). And/or change the 0.0.0.0 in the following command to the IP of your KCM server.
{% endhint %}

To create a persistent session, we will utilize a batch file with an ssh command, and the windows task scheduler. First, open notepad and copy in the following command:

{% code overflow="wrap" %}

```
ssh -fN -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -R 0.0.0.0:9000:localhost:3389 -i C:\path\to\.ssh\privkey user@kcm.server.xyz
```

{% endcode %}

Edit the command with the values that correspond to your path, desired port, and URL, and save the file as a .bat file.

<figure><img src="https://3357255970-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fb7weUpu7VBcMnESSH8vG%2Fuploads%2FhPtWEDcsdxXVErBkrJEp%2Fchrome_fubMSkN6Tu.png?alt=media&#x26;token=925fc5e9-3123-4b42-8618-1a98ca3d58ba" alt=""><figcaption></figcaption></figure>

Open Windows Task Scheduler, create a new task with a trigger set to "on startup" and an action to run the .bat file that you created.

<figure><img src="https://3357255970-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fb7weUpu7VBcMnESSH8vG%2Fuploads%2FS38z2u2K4Zyj7Zvwyqll%2Fmmc_MhmemFfSpL.png?alt=media&#x26;token=ffd2ae91-3d27-460f-93e6-d6f38b48e977" alt=""><figcaption><p>Windows Task Scheduler</p></figcaption></figure>

<figure><img src="https://3357255970-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fb7weUpu7VBcMnESSH8vG%2Fuploads%2FJv52u9rtLRxxeVuu9SFL%2Fchrome_PwYXPZnQAk.png?alt=media&#x26;token=2442aed8-1367-499a-b303-af47c1a9ff84" alt=""><figcaption></figcaption></figure>

**(5) Update GatewayPorts setting on KCM Server**

{% hint style="info" %}
This step only needs to be completed once, so if you have already completed it while setting up a different reverse ssh tunnel method, you can move on to step 7
{% endhint %}

On the KCM Server, the SSH process (sshd) must be modified to permit remote hosts (e.g. the guacd Docker container) to be allowed to connect to forwarded ports. By default, sshd binds remote port forwards to the loopback address. Setting the value of GatewayPorts to "clientspecified" allows the client to select the address to which the forwarded port is bound.

* Edit the file `/etc/ssh/sshd_config`
* Update the GatewayPorts line to this:

```
GatewayPorts clientspecified
```

* Restart sshd

```
sudo service sshd restart
```

The reverse tunnel is now established between the Remote Server and the KCM Server.

**(6) Update docker-compose to reference the host**

{% hint style="info" %}
This step applies only to the docker installations.
{% endhint %}

In the Docker install environment, it's possible to establish a connection to the Keeper Connection Manager host instance using a special host name called **host.docker.internal**.

To configure this, update the file `/etc/kcm-setup/docker-compose.yml` **guacd** section to include the "`extra_hosts`" parameter, as seen below:

```
    guacd:
        image: keeper/guacd:2
        restart: unless-stopped
        environment:
            ACCEPT_EULA: "Y"
        volumes:
            - "common-storage:/var/lib/guacamole:rw"
        extra_hosts:
            - "host.docker.internal:host-gateway"
```

Update the docker environment for the change to take effect.

```
sudo ./kcm-setup.run stop
sudo ./kcm-setup.run upgrade
```

**(7) Create Connection to the target Remote Server**

Now that the reverse SSH tunnel is set up, and the docker container is able to access the reverse tunnel, you can now simply create a connection from the Keeper Connection Manager interface.

Create a new RDP connection with the hostname of `host.docker.internal` and the port of 9000 (or your chosen port).

![Create Connection via Reverse SSH Tunnel](https://3357255970-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fb7weUpu7VBcMnESSH8vG%2Fuploads%2FPWQHq7zcnNwFHfnT7li2%2FScreen%20Shot%202022-07-04%20at%2010.43.47%20PM.png?alt=media\&token=45504026-cab5-497f-9fa7-9ca54fd2a844)

As usual, ensure that the proper Authentication parameters are populated in the connection for the remote server. In this case, the remote server is being accessed via the established reverse SSH tunnel.

Save the connection, navigate back to the "My Connections" or "Home" screen, and then click on the connection you just created to verify the routing was successful.

### Creating More Connections

If you would like to establish more connections using reverse SSH tunneling, repeat **Step 4** of this guide on a different port (e.g. 9001, 9002, etc...). Then create a connection with the specified port number when creating Connections inside Keeper Connection Manager.

### References

Several references and guides posted online contain helpful information about this configuration.

{% embed url="<https://www.everythingcli.org/ssh-tunnelling-for-fun-and-profit-autossh/>" %}

{% embed url="<https://superuser.com/questions/588591/how-to-make-an-ssh-tunnel-publicly-accessible>" %}

{% embed url="<https://serverfault.com/questions/379344/selecting-interface-for-ssh-port-forwarding>" %}

{% embed url="<https://medium.com/@TimvanBaarsen/how-to-connect-to-the-docker-host-from-inside-a-docker-container-112b4c71bc66>" %}
