• Stars
    star
    171
  • Rank 222,266 (Top 5 %)
  • Language
    Go
  • License
    MIT License
  • Created almost 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

golang/Go library for ssh tunneling (secure port forwarding)

sshego, a usable ssh library for Go

executive summary

Google's "golang.org/x/crypto/ssh" library offers a fantastic full implementation of the ssh client and server protocols. However this library is minimalistic by design, cumbersome to figure out how to use with RSA keys, and needs additional code to support tunneling and receiving connections as an sshd.

sshego bridges this usability gap, providing a drop-in Go library to secure your tcp connections. In places sshego can be used in preference to a virtual-private-network (VPN), for both convenience and speed. Moreover the SSH protocol's man-in-the-middle attack protection is better than a VPN in almost all cases.

usable three-factor auth in an embeddable sshd

For strong security, our embedded sshd offers three-factor auth (3FA). The three security factors are: a passphrase ("what you know"); a 6-digit Google authenticator code (TOTP/RFC 6238; "what you have": your phone); and the use of PKI in the form of 4096-bit RSA keys.

To promote strong passphrases, we follow the inspiration of https://xkcd.com/936/, and offer a user- friendly 3-word starting prompt (the user completes the sentence) to spark the user's imagination in creating a strong and memorizable passphrase. Passphrases of up to 100 characters are supported.

Although not for the super-security conscious, if desired and configured, passphrases can automatically be backed up to email (via the Mailgun email service).

On new account creation with gosshtun -adduser yourlogin, we will attempt to pop-up the QR-code on your local desktop for quick Google Authenticator setup on your phone.

introducing sshego: a gopher's do-it-yourself ssh tunneling library

sshego is a golang (Go) library for ssh tunneling (secure port forwarding). It also offers an embeddable 3-factor authentication sshd server, which can be useful for securing reverse forwards.

This means you can easily create an ssh-based vpn with 3-factor authentication requrirements: the embedded sshd requires passphrase, RSA keys, and a TOTP Google Authenticator one-time password.

In addition to the libary, gosshtun is also a command line utility (see the cmd/ subdir) that demonstrates use of the library and may prove useful on its own.

The intent of having a Go library is so that it can be used to secure (via SSH tunnel) any other traffic that your Go application would normally have to do over cleartext TCP.

While you could always run a tunnel as a separate process, by running the tunnel in process with your application, you know the tunnel is running when the process is running. It's just simpler to administer; only one thing to start instead of two.

Also this is much simpler, and much faster, than using a virtual private network (VPN). For a speed comparison, consider [1] where SSH is seen to be at least 2x faster than OpenVPN.

[1] http://serverfault.com/questions/653211/ssh-tunneling-is-faster-than-openvpn-could-it-be

In its principal use, sshego is the equivalent to using the ssh client and giving -L and/or -R. It acts like an ssh client without a remote shell; it simply tunnels other TCP connections securely. There are also options to run an embedded SSHD. This can be useful for securing reverse forwards, if allowed.

For example,

gosshtun -listen 127.0.0.1:89  -sshd jumpy:55  -remote 10.0.1.5:80 -user alice -key ~/.ssh/id_rsa_nopw

is equivalent to

ssh -N -L 89:10.0.1.5:80 alice@jumpy -port 55

with the addendum that gosshtun requires the use of passwordless private -key file, and will never prompt you for a password at the keyboard. This makes it ideal for embedding inside your application to secure your (e.g. mysql, postgres, other cleartext) traffic. As many connections as you need will be multiplexed over the same ssh tunnel.

grain of access

If you don't trust the other users on the host where your process is running, you can also use sshego to (a) secure a direct TCP connection (see DialConfig.Dial() and the example in cli_test.go; https://github.com/glycerine/sshego/blob/master/cli_test.go#L72); or (b) forward via a file-system secured unix-domain sockets.

The first option (a) would disallow any other process (even under the same user) from multiplexing your original connection, and the second (b) would disallow any other user from accessing your tunnel, so long as you use the file-system permissions to make the unix-domain socket path inaccessible to others.

In either case, note that keys are by default stored on disk under the user's $HOME/.ssh folder, so as usual that folder should not be readable by others. Using a direct connection as in (a) in no way prevents you from starting another process (from the same executable or another) that reads the same keys and starts its own direct tcp connection.

theory of operation

gosshtun and sshego will check the sshd server's host key. We prevent MITM attacks by only allowing new servers if -new (a.k.a. SshegoConfig.AddIfNotKnown == true) is given.

When running the standalone gosshtun to test a foward, you should give -new only once at setup time.

Then the lack of -new protects you on subsequent runs, because the server's host key must match what we were given the very first time.

flags accepted, see gosshtun -h for complete list

Usage of gosshtun:
  -cfg string
        path to our config file
  -esshd string
        (optional) start an in-process embedded sshd (server),
        binding this host:port, with both RSA key and 2FA
        checking; useful for securing -revfwd connections.
  -esshd-host-db string
        (only matters if -esshd is also given) path
        to database holding sshd persistent state
        such as our host key, registered 2FA secrets, etc.
        (default "$HOME/.ssh/.sshego.sshd.db")        
  -key string
        private key for sshd login (default "$HOME/.ssh/id_rsa_nopw")
  -known-hosts string
        path to gosshtun's own known-hosts file (default
        "$HOME/.ssh/.sshego.cli.known.hosts")
  -listen string
        (forward tunnel) We listen on this host:port locally,
        securely tunnel that traffic to sshd, then send it
        cleartext to -remote. The forward tunnel is active
        if and only if -listen is given.  If host starts with
        a '/' then we treat it as the path to a unix-domain
        socket to listen on, and the port can be omitted.
  -new
        allow connecting to a new sshd host key, and store it
        for future reference. Otherwise prevent MITM attacks by
        rejecting unknown hosts.
  -quiet
        if -quiet is given, we don't log to stdout as each
        connection is made. The default is false; we log
        each tunneled connection.        
  -remote string
        (forward tunnel) After traversing the secured forward
        tunnel, -listen traffic flows in cleartext from the
        sshd to this host:port. The foward tunnel is active
        only if -listen is given too.  If host starts with a
        '/' then we treat it as the path to a unix-domain
        socket to forward to, and the port can be omitted.
  -revfwd string
        (reverse tunnel) The gosshtun application will receive
        securely tunneled connections from -revlisten on the
        sshd side, and cleartext forward them to this host:port.
        For security, it is recommended that this be 127.0.0.1:22,
        so that the sshd service on your gosshtun host
        authenticates all remotely initiated traffic.
        See also the -esshd option which can be used to
        secure the -revfwd connection as well.
        The reverse tunnel is active only if -revlisten is given
        too. (default "127.0.0.1:22")
  -revlisten string
        (reverse tunnel) The sshd will listen on this host:port,
        securely tunnel those connections to the gosshtun application,
        whence they will cleartext connect to the -revfwd address.
        The reverse tunnel is active if and only if -revlisten is given.  
  -sshd string
        The remote sshd host:port that we establish a secure tunnel to;
        our public key must have been already deployed there.
  -user string
        username for sshd login (default is $USER)
  -v    verbose debug mode
  -write-config string
        (optional) write our config to this path before doing
        connections

installation

go get github.com/glycerine/sshego/...

example use of the command

$ gosshtun -listen localhost:8888 -sshd 10.0.1.68:22 -remote 127.0.0.1:80

means the following two network hops will happen, when a local browser connects to localhost:8888

                       `gosshtun`             `sshd`
local browser ----> localhost:8888 --(a)--> 10.0.1.68:22 --(b)--> 127.0.0.1:80
  `host A`             `host A`               `host B`              `host B`

where (a) takes place inside the previously established ssh tunnel.

Connection (b) takes place over basic, un-adorned, un-encrypted TCP/IP. Of course you could always run gosshtun again on the remote host to secure the additional hop as well, but typically -remote is aimed at the 127.0.0.1, which will be internal to the remote host itself and so needs no encryption.

specifying username to login to sshd host with

The -user flag should be used if your local $USER is different from that on the sshd host.

source code for gosshtun command

See github.com/glycerine/sshego/cmd/gosshtun/main.go for the source code. This also serves as an example of how to use the library.

host key storage location (default)

~/.ssh/.sshego.known.hosts.json.snappy

prep before running

a) install your passwordless ssh-private key in ~/.ssh/id_rsa_nopw or use -key to say where it is.

b) add the corresponding public key to the user's .ssh/authorized_keys file on the sshd host.

config file format

a) see demo.env for an example

b) run gosshtun -write-config - to generate a sample config file to stdout

c) comments are allowed; lines must start with #, comments continue until end-of-line

d) fields recognized (see gosshtun -write-config - for a full list)

#
# config file for sshego:
#
SSHD_ADDR="1.2.3.4:22"
FWD_LISTEN_ADDR="127.0.0.1:8888"
FWD_REMOTE_ADDR="127.0.0.1:22"
REV_LISTEN_ADDR=""
REV_REMOTE_ADDR=""
SSHD_LOGIN_USERNAME="$USER"
SSH_PRIVATE_KEY_PATH="$HOME/.ssh/id_rsa_nopw"
SSH_KNOWN_HOSTS_PATH="$HOME/.ssh/.sshego.known.hosts"
#
# optional in-process sshd
#
EMBEDDED_SSHD_HOST_DB_PATH="$HOME/.ssh/.sshego.sshd.db"
EMBEDDED_SSHD_LISTEN_ADDR="127.0.0.1:2022"

d) special environment reads

  • The SSHD_LOGIN_USERNAME will subsitute $USER from the environment, if present.

  • The *PATH keys will substitute $HOME from the environment, if present.

MIT license

See the LICENSE file.

Author

Jason E. Aten, Ph.D.

More Repositories

1

zygomys

Zygo is a Lisp interpreter written in 100% Go. Central use case: dynamically compose Go struct trees in a zygo script, then invoke compiled Go functions on those trees. Makes Go reflection easy.
Go
1,636
star
2

offheap

an off-heap hash-table in Go. Used to be called go-offheap-hashtable, but we shortened it.
Go
349
star
3

rbuf

a small circular ring buffer library in go / golang
Go
182
star
4

zebrapack

ZebraPack format is like gobs version 2: serialization in Go, *but* extremely fast and friendly to other languages. Use Go as your schema. Strong typing. Well documented (and msgpack2 compatible) format so other languages can be readily supported. See also https://github.com/glycerine/greenpack for a more recent alternative. Docs:
Go
168
star
5

greenpack

Cross-language serialization for Golang: greenpack adds versioning, stronger typing, and optional schema atop msgpack2. `greenpack -msgpack2` produces classic msgpack2, and handles nils. Cousin to ZebraPack (https://github.com/glycerine/zebrapack), greenpack's advantage is fully self-describing data. Oh, and faster than protobufs.
Go
113
star
6

goq

goq: a job queuing system written in go (golang). "Pronounced Go-Queue. Don't Gawk at this!"
Go
86
star
7

bambam

auto-generate capnproto schema from your golang source files. Depends on go-capnproto-1.0 at https://github.com/glycerine/go-capnproto
Go
67
star
8

sofia-ml

Automatically exported from code.google.com/p/sofia-ml
C++
61
star
9

go-sliding-window

a library for reliable and flow-controlled nats sessions using the sliding window protocol. Written in golang.
Go
58
star
10

golang-thrift-minimal-example

the apache thrift starter tutorial for golang, as a standalone repo
Go
44
star
11

gozbus

nanocap based messaging system
Go
43
star
12

grpc-demo

code to stream arbitrarily large files between hosts using gRPC and golang
Go
39
star
13

fast-elliptic-curve-p256

repackage the golang elliptic library enhancements by Vlad Krasnov and Shay Gueron as a stand alone library. Works with Go 1.4 or Go 1.5.
Go
35
star
14

tmframe

TMFRAME, pronounced "time frame", is a binary standard for compactly encoding time series data
Go
28
star
15

xcryptossh

golang.org/x/crypto/ssh the next generation: provide idle timeouts, avoid memory leaks, and gracefully cancel connections
Go
27
star
16

rmq

R package providing msgpack and websockets; demonstrates how to utilize Go libraries from R.
Go
24
star
17

libzipfs

Ship a zip file of media resources inside your golang web-app for complete standalone one-binary deployment
Go
21
star
18

golang-embed-julia

simple example of calling julia from Go
Go
20
star
19

thinkgo

Think Go. Pointers and resources for learning Go. Go (golang) is an elegant, fast, and rapid development language.
19
star
20

nack-oriented-reliable-multicast

NACK-Oriented Reliable Multicast (NORM): http://www.nrl.navy.mil/itd/ncs/products/norm
C++
15
star
21

hello_gio

hello world for Gio graphics for Golang. Gio runs on macOS, Windows, Wayland (linux), X11, WebASM, iOS, and Android. All in Go, no bridge C/Java to write.
Go
14
star
22

arogue

A Go-Repl using R underneath
Go
9
star
23

go-unsnap-stream

small golang library for decoding the snappy streaming format https://github.com/google/snappy/blob/master/framing_format.txt
Go
9
star
24

truepack

like https://github.com/glycerine/greenpack, but no integer compression based on the int's value
Go
8
star
25

lush2

Lush2 sources from svn 908, starting point -- https://lush.svn.sourceforge.net/svnroot/lush -- Last Changed Date: 2011-03-20 16:29:13 -0500 (Sun, 20 Mar 2011). Now patched for OSX 10.6 and up.
C
7
star
26

PrattParserInC

An implementation of Pratt Parsing (Vaughn Pratt 1973 "Top Down Operator Precedence") in C/C++
7
star
27

ruid

ruid: a really unique id
Go
6
star
28

webiperf

webiperf is a web-app that makes it easy to generate iperf commands
JavaScript
6
star
29

rustxi

rust + transactions + interpreter = rustxi. rustxi is a transactional jit-compilation-based REPL for the Rust language.
C
5
star
30

liblmdb

git clone -b mdb.master git://git.openldap.org/openldap.git # on June 11, 2014 / 17c09fa476a7dbd49aca5e4caf0384cb1c3d244a
C
5
star
31

monotime

Go library for monotonic time source on platforms where one is available to the Go runtime.
Go
4
star
32

low-level-lush

(LLL) : Low-Level-Lush with Lisp is a combination of Lush2.0.1 and LLVM2.8 to bring out the best in both.
C++
4
star
33

swig-cpp-to-cffi-for-common-lisp-enhancements

forking from SWIG svn 12570 to generate better common lisp bindings from c++
C++
4
star
34

gopass

Go
4
star
35

idem

idem.Halter: a pattern for halting goroutines in Go
Go
4
star
36

muse

golang code to convert from go/types.Type to reflect.Type
Go
3
star
37

crack-language

Crack programming language
C++
3
star
38

buzz

broadcasting channels in Go
Go
3
star
39

json2msgpack

convert from newline delimited json to size-header based msgpack frames
Go
3
star
40

shore-mt

shore-mt (Scalable Heterogeneous Object REpository - MultiThreaded version), import of the 6.0.2 release of 03-Jan-2012 http://research.cs.wisc.edu/shore-mt/ ( differs from the DIAS version from http://diaswww.epfl.ch/shore-mt/ )
C++
3
star
41

configs-in-golang

demonstrate pattern for command line flag handling that allows library configuration/reuse and testing
Go
3
star
42

golang-fisher-exact

Fisher's exact test for 2x2 contingency tables, in Golang
Go
2
star
43

vprint

debug Go faster with this simple print library. prints show timestamp and file location
Go
2
star
44

selfie

generate and validate self-signed public keys
Go
2
star
45

nanomsgardvark

R bindings for nanomsg
C
2
star
46

justinjuddeasyssh

Go
2
star
47

golang-hex-dumper

Go
2
star
48

libcmm

Ralf Juengling's libcmm: a C Memory Management Library, last commit f980fd1715780c99f54ebad8b49dbc10befe192d from Thu Nov 12 13:35:24 2009. Obtained from the sourceforge git repos on 28 April 2011 by: git clone git://libcmm.git.sourceforge.net/gitroot/libcmm/libcmm
C++
2
star
49

xml2csv

Parse an XML file on stdin, write csv to stdout. No schema, no structs required.
Go
2
star
50

gossainterpdemo

demonstration of the go ssa interpreter
Go
2
star
51

pingbuf

a minimal ring buffer
Go
2
star
52

spread-src-4.4.0

An open source implementation of virtual synchrony, package spread-src-4.4.0, from Spread Concepts LLC. Details at http://www.spread.org See also https://github.com/glycerine/spread-src-5.0.1
C
2
star
53

ugorji-go-codec

Go
1
star
54

spread-src-5.0.1

see motivation https://github.com/glycerine/spread-src-4.4.0
C
1
star
55

latch

Go
1
star
56

dyg

Go
1
star
57

rogue

use R as a repl and worker under golang master
Go
1
star
58

mps-kit-1.108.0-x86_64-linux-port

See if the excellent Memory Pool System (MPS) can be ported to x86_64 on Linux
C
1
star
59

basic-bolt

basic boltdb example
Go
1
star
60

avfs

a virtual filesystem. mirror from http://avf.sourceforge.net/ and http://sourceforge.net/projects/avf/
C
1
star
61

vpython-emacs-mode

my vpython.el mode for ipython
Emacs Lisp
1
star
62

verb

Go
1
star
63

iris2

Community driven successor of the iris web framework
Go
1
star
64

yield

C++ web application server
C++
1
star
65

lcon

local pipe that looks like net.Conn
Go
1
star
66

pelican-protocol

In ancient Egypt the pelican was believed to possess the ability to prophesy safe passage in the underworld. Pelicans are ferocious eaters of fish.
Go
1
star
67

nats

friendly fork of nats-io/nats for use with glycerine/hnatsd
Go
1
star
68

go-inthash

go version of basic hash map, open addressing with linear probing. uint64 keys. Inspired by http://preshing.com/20130107/this-hash-table-is-faster-than-a-judy-array/
Go
1
star
69

fastbit

fastbit-2.0.3 from https://sdm.lbl.gov/fastbit/, BSD licensed.
C++
1
star
70

geist

the geist script runner turns golang into a scripting language
Go
1
star
71

gopls-emacs-how-to

how to get gopls in emacs with golang go-mode working
1
star
72

golang-repl

Go
1
star
73

double-messages-received-bug

Go
1
star
74

python-extension-in-golang

code for https://hackernoon.com/extending-python-3-in-go-78f3a69552ac
Makefile
1
star
75

L3

L3: experiments in aggressive Garbage-collection and interpreter implementation based on the Pratt parser. Uses protocol buffers for serialization, and Judy arrays for fast, sparse arrays/hashtables. Linux and OSX.
1
star
76

SchemeTL

Scheme To Learn : Learning Scheme by implementing STL data structure equivalents
Common Lisp
1
star