From 960999fe64fd3e5c8bbebe9397d647573be77e6d Mon Sep 17 00:00:00 2001 From: Andrew Carter Date: Mon, 4 May 2020 22:53:30 -0700 Subject: [PATCH] ssh-agent example Add example for using Windows ssh-agent with WSL2 session --- README.md | 28 +++++++++++++++++++++++++++- scripts/ssh-agent | 9 +++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 scripts/ssh-agent diff --git a/README.md b/README.md index 58c24f5..6396f8e 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ For example, you can: * Connect to MySQL Server running as a Windows service * Connect interactively to a Hyper-V Linux VM's serial console * Use gdb to connect to debug the kernel of a Hyper-V Linux VM +* Connect to Windows SSH agent via named pipe Let me know on Twitter ([@gigastarks](https://twitter.com/gigastarks)) if you come up with more interesting uses. @@ -27,7 +28,17 @@ Basic steps: To build the binary, you will need a version of [Go](https://golang.org). You can use a Windows build of Go or, as outlined here, you can use a Linux build and cross-compile the Windows binary directly from WSL. -## Building npiperelay.exe +## Building on Windows + +```powershell +git clone https://github.com/jstarks/npiperelay.git +cd npiperelay +go build -o npiperelay.exe +``` + +Copy `npiperelay.exe` to a location on your path. WSL 2 will read your path and find it. + +## Building npiperelay.exe in WSL Once you have Go installed (and your GOPATH configured), you need to download and install the tool. This is a little tricky because we are building the tool for Windows from WSL: @@ -219,6 +230,21 @@ gdb ./vmlinux target remote /home//foo-pty ``` +## Connect to Windows SSH agent + +Windows provides [OpenSSH](https://docs.microsoft.com/en-us/windows-server/administration/openssh/openssh_overview) including `ssh-agent`. If you have [configured](https://docs.microsoft.com/en-us/windows-server/administration/openssh/openssh_server_configuration) the agent to auto start and added your keys, WSL2 can connect to it using a named pipe via `SSH_AUTH_SOCK`. + +Add the following to a `.bashrc` or `.zshrc` configuration to setup WSL `ssh-agent` to use Windows agent. + +```bash +export SSH_AUTH_SOCK=${HOME}/.ssh/agent.sock +ss -a | grep -q $SSH_AUTH_SOCK +if [ $? -ne 0 ]; then + rm -f ${SSH_AUTH_SOCK} + ( setsid socat UNIX-LISTEN:${SSH_AUTH_SOCK},fork EXEC:"npiperelay.exe -ei -s //./pipe/openssh-ssh-agent",nofork & ) >/dev/null 2>&1 +fi +``` + ## Custom usage Take a look at the scripts for sample usage, or run `npiperelay.exe` without any parameters for parameter documentation. \ No newline at end of file diff --git a/scripts/ssh-agent b/scripts/ssh-agent new file mode 100644 index 0000000..1ffe5b8 --- /dev/null +++ b/scripts/ssh-agent @@ -0,0 +1,9 @@ +#!/bin/sh + +export SSH_AUTH_SOCK=${HOME}/.ssh/agent.sock + +ss -a | grep -q $SSH_AUTH_SOCK +if [ $? -ne 0 ]; then + rm -f ${SSH_AUTH_SOCK} + ( setsid socat UNIX-LISTEN:${SSH_AUTH_SOCK},fork EXEC:"npiperelay.exe -ei -s //./pipe/openssh-ssh-agent",nofork & ) >/dev/null 2>&1 +fi