github.com/devops-filetransfer/sshego@v7.0.4+incompatible/README.md (about)

     1  # sshego, a usable ssh library for Go
     2  
     3  ### executive summary
     4  
     5  Google's "golang.org/x/crypto/ssh" library offers a
     6  fantastic full implementation of the ssh
     7  client and server protocols.
     8  However this library is minimalistic by
     9  design, cumbersome to figure out how to
    10  use with RSA keys, and needs additional code to
    11  support tunneling
    12  and receiving connections as an sshd.
    13  
    14  `sshego` bridges this usability gap,
    15  providing a drop-in Go library
    16  to secure your tcp connections. In 
    17  places `sshego` can be used in preference to
    18  a virtual-private-network (VPN), for both
    19  convenience and speed. Moreover the SSH
    20  protocol's man-in-the-middle attack protection is
    21  better than a VPN in almost all cases.
    22  
    23  #### usable three-factor auth in an embeddable sshd
    24  
    25  For strong security, our embedded sshd
    26  offers three-factor auth (3FA). The three
    27  security factors are: a passphrase ("what you know");
    28  a 6-digit Google authenticator code (TOTP/RFC 6238;
    29  "what you have": your phone); and
    30  the use of PKI in the form of 4096-bit RSA keys.
    31  
    32  To promote strong passphrases, we follow the
    33  inspiration of https://xkcd.com/936/, and offer a user-
    34  friendly 3-word starting prompt (the user
    35  completes the sentence) to spark the
    36  user's imagination in creating
    37  a strong and memorizable passphrase. Passphrases of up
    38  to 100 characters are supported.
    39  
    40  Although not for the super-security conscious,
    41  if desired and configured, passphrases can automatically
    42  be backed up to email (via the Mailgun email service).
    43  
    44  On new account creation with `gosshtun -adduser yourlogin`,
    45  we will attempt to pop-up the QR-code on your
    46  local desktop for quick Google Authenticator setup
    47  on your phone.
    48  
    49  
    50  ### introducing sshego: a gopher's do-it-yourself ssh tunneling library
    51  
    52  `sshego` is a golang (Go) library for ssh
    53  tunneling (secure port forwarding). It also offers an
    54  embeddable 3-factor authentication sshd server,
    55  which can be useful for securing reverse forwards.
    56  
    57  This means you can easily create an ssh-based vpn
    58  with 3-factor authentication requrirements:
    59  the embedded sshd requires passphrase, RSA keys,
    60  and a TOTP Google Authenticator one-time password.
    61  
    62  In addition to the libary, `gosshtun` is also
    63  a command line utility (see the cmd/ subdir) that
    64  demonstrates use of the library and may prove
    65  useful on its own.
    66  
    67  The intent of having a Go library is so that it can be used
    68  to secure (via SSH tunnel) any other traffic that your
    69  Go application would normally have to do over cleartext TCP.
    70  
    71  While you could always run a tunnel as a separate process,
    72  by running the tunnel in process with your application, you
    73  know the tunnel is running when the process is running. It's
    74  just simpler to administer; only one thing to start instead of two.
    75  
    76  Also this is much simpler, and much faster, than using a
    77  virtual private network (VPN). For a speed comparison,
    78  consider [1] where SSH is seen to be at least 2x faster
    79  than OpenVPN.
    80  
    81  [1] http://serverfault.com/questions/653211/ssh-tunneling-is-faster-than-openvpn-could-it-be
    82  
    83  In its principal use, `sshego` is the equivalent
    84  to using the ssh client and giving `-L` and/or `-R`.
    85  It acts like an ssh client without a remote shell; it simply
    86  tunnels other TCP connections securely. There are
    87  also options to run an embedded SSHD. This can
    88  be useful for securing reverse forwards, if allowed.
    89  
    90  For example,
    91  
    92      gosshtun -listen 127.0.0.1:89  -sshd jumpy:55  -remote 10.0.1.5:80 -user alice -key ~/.ssh/id_rsa_nopw
    93  
    94  is equivalent to
    95  
    96      ssh -N -L 89:10.0.1.5:80 alice@jumpy -port 55
    97  
    98  with the addendum that `gosshtun` requires the use of passwordless
    99  private `-key` file, and will never prompt you for a password at the keyboard.
   100  This makes it ideal for embedding inside your application to
   101  secure your (e.g. mysql, postgres, other cleartext) traffic. As
   102  many connections as you need will be multiplexed over the
   103  same ssh tunnel.
   104  
   105  # grain of access
   106  
   107  If you don't trust the other users on the host where your
   108  process is running, you can also use sshego to (a) secure a direct
   109  TCP connection (see DialConfig.Dial() and the example in cli_test.go; https://github.com/glycerine/sshego/blob/master/cli_test.go#L72);
   110  or (b) forward via a file-system secured unix-domain sockets.
   111  
   112  The first option (a) would disallow any other process (even
   113  under the same user) from multiplexing 
   114  your original connection, and the second (b) would disallow
   115  any other user from accessing your tunnel, so long as
   116  you use the file-system permissions
   117  to make the unix-domain socket path inaccessible to others.
   118  
   119  In either case, note that keys are by default stored
   120  on disk under the user's $HOME/.ssh folder, so as usual that
   121  folder should not be readable by others. Using a direct
   122  connection as in (a) in no way prevents you from starting
   123  another process (from the same executable or another) that
   124  reads the same keys and starts its own direct tcp connection.
   125  
   126  # theory of operation
   127  
   128  `gosshtun` and `sshego` will check the sshd server's host key. We prevent MITM attacks
   129  by only allowing new servers if `-new` (a.k.a. SshegoConfig.AddIfNotKnown == true) is given.
   130  
   131  When running the standalone `gosshtun` to test
   132  a foward, you should give `-new` only once at setup time.
   133  
   134  Then the lack of `-new` protects you on subsequent runs,
   135  because the server's host key must match what we were
   136  given the very first time.
   137  
   138  # flags accepted, see `gosshtun -h` for complete list
   139  
   140  ~~~
   141  Usage of gosshtun:
   142    -cfg string
   143          path to our config file
   144    -esshd string
   145          (optional) start an in-process embedded sshd (server),
   146          binding this host:port, with both RSA key and 2FA
   147          checking; useful for securing -revfwd connections.
   148    -esshd-host-db string
   149          (only matters if -esshd is also given) path
   150          to database holding sshd persistent state
   151          such as our host key, registered 2FA secrets, etc.
   152          (default "$HOME/.ssh/.sshego.sshd.db")        
   153    -key string
   154          private key for sshd login (default "$HOME/.ssh/id_rsa_nopw")
   155    -known-hosts string
   156          path to gosshtun's own known-hosts file (default
   157          "$HOME/.ssh/.sshego.cli.known.hosts")
   158    -listen string
   159          (forward tunnel) We listen on this host:port locally,
   160          securely tunnel that traffic to sshd, then send it
   161          cleartext to -remote. The forward tunnel is active
   162          if and only if -listen is given.  If host starts with
   163          a '/' then we treat it as the path to a unix-domain
   164          socket to listen on, and the port can be omitted.
   165    -new
   166          allow connecting to a new sshd host key, and store it
   167          for future reference. Otherwise prevent MITM attacks by
   168          rejecting unknown hosts.
   169    -quiet
   170          if -quiet is given, we don't log to stdout as each
   171          connection is made. The default is false; we log
   172          each tunneled connection.        
   173    -remote string
   174          (forward tunnel) After traversing the secured forward
   175          tunnel, -listen traffic flows in cleartext from the
   176          sshd to this host:port. The foward tunnel is active
   177          only if -listen is given too.  If host starts with a
   178          '/' then we treat it as the path to a unix-domain
   179          socket to forward to, and the port can be omitted.
   180    -revfwd string
   181          (reverse tunnel) The gosshtun application will receive
   182          securely tunneled connections from -revlisten on the
   183          sshd side, and cleartext forward them to this host:port.
   184          For security, it is recommended that this be 127.0.0.1:22,
   185          so that the sshd service on your gosshtun host
   186          authenticates all remotely initiated traffic.
   187          See also the -esshd option which can be used to
   188          secure the -revfwd connection as well.
   189          The reverse tunnel is active only if -revlisten is given
   190          too. (default "127.0.0.1:22")
   191    -revlisten string
   192          (reverse tunnel) The sshd will listen on this host:port,
   193          securely tunnel those connections to the gosshtun application,
   194          whence they will cleartext connect to the -revfwd address.
   195          The reverse tunnel is active if and only if -revlisten is given.  
   196    -sshd string
   197          The remote sshd host:port that we establish a secure tunnel to;
   198          our public key must have been already deployed there.
   199    -user string
   200          username for sshd login (default is $USER)
   201    -v    verbose debug mode
   202    -write-config string
   203          (optional) write our config to this path before doing
   204          connections
   205  ~~~
   206  
   207  # installation
   208  
   209    go get github.com/glycerine/sshego/...
   210  
   211  # example use of the command
   212  
   213      $ gosshtun -listen localhost:8888 -sshd 10.0.1.68:22 -remote 127.0.0.1:80
   214  
   215  means the following two network hops will happen, when a local browser connects to localhost:8888
   216  
   217                             `gosshtun`             `sshd`
   218      local browser ----> localhost:8888 --(a)--> 10.0.1.68:22 --(b)--> 127.0.0.1:80
   219        `host A`             `host A`               `host B`              `host B`
   220  
   221  where (a) takes place inside the previously established ssh tunnel.
   222  
   223  Connection (b) takes place over basic, un-adorned, un-encrypted TCP/IP. Of
   224  course you could always run `gosshtun` again on the remote host to
   225  secure the additional hop as well, but typically -remote is aimed at the 127.0.0.1,
   226  which will be internal to the remote host itself and so needs no encryption.
   227  
   228  
   229  # specifying username to login to sshd host with
   230  
   231  The `-user` flag should be used if your local $USER is different from that on the sshd host.
   232  
   233  # source code for gosshtun command
   234  
   235  See `github.com/glycerine/sshego/cmd/gosshtun/main.go` for the source code. This
   236  also serves as an example of how to use the library.
   237  
   238  # host key storage location (default)
   239  
   240  `~/.ssh/.sshego.known.hosts.json.snappy`
   241  
   242  # prep before running
   243  
   244  a) install your passwordless ssh-private key in `~/.ssh/id_rsa_nopw` or use -key to say where it is.
   245  
   246  b) add the corresponding public key to the user's .ssh/authorized_keys file on the sshd host.
   247  
   248  # config file format
   249  
   250  a) see demo.env for an example
   251  
   252  b) run `gosshtun -write-config -` to generate a sample config file to stdout
   253  
   254  c) comments are allowed; lines must start with `#`, comments continue until end-of-line
   255  
   256  d) fields recognized (see `gosshtun -write-config -` for a full list)
   257  
   258  ~~~
   259  #
   260  # config file for sshego:
   261  #
   262  SSHD_ADDR="1.2.3.4:22"
   263  FWD_LISTEN_ADDR="127.0.0.1:8888"
   264  FWD_REMOTE_ADDR="127.0.0.1:22"
   265  REV_LISTEN_ADDR=""
   266  REV_REMOTE_ADDR=""
   267  SSHD_LOGIN_USERNAME="$USER"
   268  SSH_PRIVATE_KEY_PATH="$HOME/.ssh/id_rsa_nopw"
   269  SSH_KNOWN_HOSTS_PATH="$HOME/.ssh/.sshego.known.hosts"
   270  #
   271  # optional in-process sshd
   272  #
   273  EMBEDDED_SSHD_HOST_DB_PATH="$HOME/.ssh/.sshego.sshd.db"
   274  EMBEDDED_SSHD_LISTEN_ADDR="127.0.0.1:2022"
   275  ~~~
   276  
   277  d) special environment reads
   278  
   279  * The SSHD_LOGIN_USERNAME will subsitute $USER from the environment, if present.
   280  
   281  * The *PATH keys will substitute $HOME from the environment, if present.
   282  
   283  # MIT license
   284  
   285  See the LICENSE file.
   286  
   287  # Author
   288  
   289  Jason E. Aten, Ph.D.
   290