tags : Networking, Linux, Security, Selfhosting

Intro

A cryptographic network protocol that laregly uses PKI to do its job

  • Transport layer: Server auth, integrity check
  • User auth: validates client to the server
  • Connection protocol: multiplexes the encrypted tunnel

Applications

  • Remote login/passwordless login
  • Remote execution
  • Tunneling (different from a vpn, can also be a vpn)
  • Port forwarding
  • Used in file transfer programs
  • Forwarding x win
  • Web-browsing through encrypted proxy (SOCKS)
  • Mounting remote filesystem locally (SSHFS)
  • others.

OpenSSH

  • One of the most popular and widely used implementations.
  • This originally gets built for OpenBSD. The portability team does the work of making it support other operating systems.

How fingerprinting happens

  • Public key is always derived from the private key, so in essence if you have the private key you would know the public key.
  • Usual way how fingerprints are constructed is, the fingerprint is constructed out of the public part of the private key. Because of this the fingerprint of either private or public key of a key pair is the same.
  • See Checking ssh public key fingerprints

host and user keys

  • Host keys
    • Server keys
    • Generated when openssh is installed. Keys of different types are generated at /etc/ssh (Only root access)
    • Identical to the user-keys. ssh-keygen can be used to replace existing host keys.
  • user keys
    • Per user
    • Private keys are accessible by the root+user

How sshd works

  • sshd only deals w new connections and authentication. When it gets a new connection, it forks a new process(which handles the connection)
  • A new process is created for each new SSH session.
  • The initial process(sshd) acts as the master server that listens to incoming connections. (parent process of all the other sshd processes).
  • If the SSH server is upgraded or restarted, only the master server is affected. It’s replaced.
  • Infact, you can have a ssh session going and you can stop sshd (systemctl stop ssh), your session will still be running because you’ll be adopted by PID1 and sshd’s responsibility is to listen to new connections. In this condition, you won’t be able to make any connections to the server. Be VERY careful.

ssh-agent

You can’t really differentiate an encrypted private key from a non-encrypted one from human eye. But anyway if its encrypted, the client will ask you for a passphrase to decrypt the private key. (i have to verify this lol)

  • When we generate key pairs, we can decide to encrypt the private key with a passphrase. (It’s good practice to do so)
  • If the case you decide to encrypt the private key, you might want to use ssh-agent because otherwise you’ll just have to give the passphrase every time you want to make use of the private key. It keeps the private key in memory(unencrypted). It does not write anything to disk, it does not export the private key.
  • Private keys stored in ssh-agent can only be used for one purpose, signing a message. (This statement is controversial from the key challenge section where it uses the private key to decrypt)

SSH Handshake

Server Auth

  • Client tries to connect to server
  • Server responds with is public key
  • Using PKI, client verifies that the public key infact belongs to the server
  • This authenticates the server
  • Finally the client will check if the server is listed in the known_hosts file, and add it there if not upon prompt. This is TOFU

User auth

Now user auth is one of the application but probably the most common one. Technically once the server auth is done, we could possibly be doing anything. Following is how the user auth happens:

  1. First it is ensured that we’re connecting to the correct server using server auth
  2. User auth happens through the key challenge
    1. server creates a random no. and encrypts it using the users public key
    2. user’s ssh-agent decrypts the random no. because it has the private key. It prepares md5(ssh_session_id + decrypted random no.) and sends it back to the server
    3. this way, the random number is never goes to the wire directly(this is intentional)

It’s called the key challenge

Agent Forwarding

  • SSH Agent Forwarding considered harmful (2015) | Lobsters : Read this first
  • It’s basically a trick and done via an environment variable. the private key is never forwarded but the agent itself is forwarded to the jump server in a forwarding mode.
  • Basically, as client receives key challenges, the agent will forward these challenges upstream to our local machine, where the challenge response will be constructed via a locally stored private key and forwarded back downstream to the destination server for authentication.

How it works

  • SSH connections can have multiple channels, in this case,
    • Connecting channel
    • Forwarding channel
  • Essentially forwarding the ssh-agent unix domain socket to the remote host. sshd on the remote host does the following:
    • sshd on remote creates a unix domain socket linked to the agent forwading channel
    • sshd on remote sets $SSH_AUTH_SOCK to it. Now, ssh just looks at SSH_AUTH_SOCK for the ssh-agent and it tries to use it

If an attacker has root access to the remote host, they can simply set their SSH_AUTH_SOCK to the address the victim user’s forwarding unix socket and they can essentially impersonate them on the network.

Caution and Warnings

  • Use SSH agent forwarding only on machines we truly trust. i.e don’t turn ssh agent forwarding by default. i.e User agent forwarding based on session basis using ssh -A instead of putting it in config.
  • You can also lock your agent with a password using ssh-add -x
  • Instead of Agent forwarding, use ProxyJump

Bastion host/Gateway host/JumpBox

There’s a term bastion service, it’s nothing but marketing bs

  • An alternative to SSH agent forwarding
  • ProxyJump forwards the standard io of your local SSH client through the bastion and on to the remote host. Agent forwarding does not take place, sshd connects to the remote host and gives the local machine the connection. (ProxyCommand is similar)
  • Multiple jump hosts can be specified
  • Bastion hosts in general can be used for more things than SSH and they also help in compliance
  • Note that a bastion is not a Reverse Proxy nor router historically due to the implication of the access primarily being intended for secure interactive shell sessions.
  • AWS has SSM, which is similar to what teleport offers but limited to AWS
  • See https://github.com/ovh/the-bastion

Things I keep hearing about

  • JumpHost

Optimizations

ControlMaster and ControlPersist

  • ControlMaster: Rather than each new SSH connection to a particular server opening up a new TCP connection, you instead multiplex all of your SSH connections down one TCP connection
  • ControlPersist: When used in conjunction with ControlMaster, specifies that the master connection should remain open in the background (waiting for future client connections) after the initial client connection has been closed.

Projects and Resources

Tools

Resources

Basics

Others

Best practices and Troubleshooting