There are certain cases where you want to operate a not completely trusted networked machine, and write scripts to automate some task which involves an unattended SSH login to a server.
With “not completely trusted machine” I mean a computer which is reasonably secured against unauthorized logins, but is physically unattended (which means that unknown persons can have physical access to it).
An established SSH connection has a number of security implications. As I have argued in a previous blog post “Unprivileged Unix Users vs. Untrusted Unix Users”, having access to a shell on a server is problematic if the user is untrusted (as is always the case when the user originates from an untrusted machine), even if he is unprivileged on the server. In my blog post I presented a method to confine a SSH user into a jail directory (via a PAM module using the Linux kernel’s
chroot system call) to prevent reading of all world-readable files on the server. However, such a jail directory still doesn’t prevent SSH port forwarding (which I illustrated in this blog post).
In short, any kind of SSH access allows access to at least all of the server’s open TCP ports, even if they are behind its firewall.
Does this mean that giving any kind of SSH access to an untrusted machine should not be done in principle? It does seem so, but there are ways to make the attack surface smaller and make the setup reasonably secure.
Remember that SSH uses some way of authentication.This is either a plain password, or a public/private keypair. In both cases there are secrets which should not be stored on the untrusted machine in a way that allows revealing of the secrets.
So the question becomes: How to supply the secrets to SSH without making it too easy to reveal them?
A private SSH key is permanent and must be stored on a permanent medium of the untrusted machine. To mitigate the possibility that the medium (e.g. hard drive) is extracted and the private key revealed, the private key should be encrypted with a long passphrase. A SSH passphrase needn’t be manually typed every time a SSH connection is made.
ssh connects to
ssh-agent (if running) to use private keys which may have previously been decrypted via a passphrase.
ssh-agent holds this information in the RAM.
I said “RAM”: For the solution to our present problem, this will be as good as it gets. The method presented below would require technical skills to read out the RAM of a running machine with hardware probes only, which would require (extremely) specialized skills. In this blog post, this is the meaning of the term “reasonably secure”.
On desktop machines,
ssh-agent is usually started together with the graphical user interface. Keys and its passphrases can be “added” to it with the command
ssh-add. The actual program
ssh connects to
ssh-agent if the environment variables
SSH_AUTH_SOCK are present. This means that any kind of shell script (even unattended ones called from
cron) can benefit from this: passphrases won’t be asked if the corresponding key has already been decrypted in memory. The main advantage of this is that this has to be done only once after the reboot of the machine (because the reboot clears the RAM).
On a headless client, without graphical interface,
ssh-agent may not even be installed, we have to start it in a custom way. There is an excellent program called keychain which makes this very easy. The sequence of our method will look like this:
- The machine is rebooted.
- An authorized administrator logs into the machine and uses the
keychaincommand to enter the passphrase which is now stored in RAM by
- The administrator now can log out. The authentication data will remain in the RAM and will be available to unattended shell scripts.
- Every login to the machine will clear the authentication information. This ensures that even a successful login of an attacker will render the private key useless. This implies a minor inconvenience for the administrator: He has to enter the passphrase at every login too.
Keychain is available in major distro’s repositories:
apt-get install keychain
Add the following line to either
~/.bashrc or to the system-wide
eval `keychain --clear --eval /path/to/.ssh/id_rsa`
This line will be executed at each login to the server. What does this command do?
keychainwill read the private key from the specified path.
keychainwill prompt for the passphrase belonging to this key (if there is one).
keychainwill look for a running instance of
ssh-agent. If there is none, it will start it. If there is one, it will re-use it.
- Due to the
keychainwill clear all keys from
ssh-agent. This renders the private key useless even if an attacker manages to successfully log in.
keychainadds the private key plus entered passphrase to
ssh-agentwhich stores it in the RAM.
keychainoutputs a short shell script (to
stdout) which exports two environment variables (mentioned above) which point to the running instance of
ssh-agentfor consumption by
evalcommand executes the shell script from
keychainwhich does nothing more but set the two environment variables.
Environment variables are not fully global, they always belong to a running process. Thus, in every unattended script which uses
ssh, you need to set these environment variables by evaluating the output of
for example, in a Bash script:
#!/bin/bash # Set up environment variables pointing to ssh-agent. eval `keychain --eval` # Do tasks involving ssh
It makes sense to gracefully catch SSH connection problems in your scripts. If you don’t do that, the script may hang indefinitely prompting for a passphrase if it has not been added properly. To do this, do a ‘preflight’ ssh connection which simply returns an error:
#!/bin/bash # Set up environment variables pointing to ssh-agent. eval `keychain --eval` # 'Preflight' connection test. ssh -q -o "BatchMode=yes" -o "ConnectTimeout=10" user@host echo ok if [ "$?" != "0" ]; then echo "SSH connection could not be established" exit 99 fi # At this point, the SSH connection will work.
In everyday practice, security is never perfect. This method is just one way to protect — within reasonable limits — a SSH connection of an unattended/untrusted machine “in the field” to a protected server. As always when dealing with the question of ‘security’, any kind of solution needs to be carefully vetted before deployment in production!