Skip to content

Instantly share code, notes, and snippets.

@matinrco
Last active December 6, 2017 10:55
Show Gist options
  • Save matinrco/4223935ea58b80f2406109728b013f07 to your computer and use it in GitHub Desktop.
Save matinrco/4223935ea58b80f2406109728b013f07 to your computer and use it in GitHub Desktop.
Chain ssh by ProxyCommand and Netcat

I have windows machine in corporate which has vpn connection to access multiple servers. But I want to acccess servers from my home computer.

So I installed openssh server from here on windows machine. Download OpenSSH-Win64.zip or OpenSSH-Win32.zip file . (version 0.0.24.0 tested)

Extract the package in C:\Program Files\OpenSSH . As the Administrator, install SSHD and ssh-agent services

powershell.exe -ExecutionPolicy Bypass -File install-sshd.ps1

As the Administrator, generate server keys and restrict an access to them, by running the following commands from the C:\Program Files\OpenSSH directory:

.\ssh-keygen.exe -A
powershell.exe -ExecutionPolicy Bypass -File .\FixHostFilePermissions.ps1

Open a port for the SSH server in Windows Firewall:

powershell.exe New-NetFirewallRule -Protocol TCP -LocalPort 22 -Direction Inbound -Action Allow -DisplayName SSH

Now in windows service manager run ssh-agent and sshd . Don't forget to change startup type to automatic (delayed start) .

Now windows is ssh enabled server :P

You can access your windows machine with windows username and password from your ssh client. But we need to setup ssh chain from your client.

Next step you need to install Netcat in windows machine .

Download nmap from this archive page. Download version 7.50 (7.60 has some problem with ncat which is discussed here). File name from that archive we need to use is nmap-7.50-win32.zip .

Extract archive in C:\Program Files (x86)\Nmap and add it to path.

Now in client ...

My client is Linux machine (Debian 9.2). In ~/.ssh/config define your destination hostnames:

Host winBroker
Hostname x.x.x.x
User YourWindowsUserName

Host serverName
Hostname x.x.x.x
ProxyCommand ssh -q winBroker ncat %h %p 

Host anotherServerName
Hostname x.x.x.x
ProxyCommand ssh -q winBroker ncat %h %p 

Now connect to destination server :

ssh userNameAtDestinationServer@serverName

enjoy!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment