SSH and ssh-agent
by Brian Hatch
|
No one likes typing passwords. If people had their way, computers would simply know who they were and what they should have access to without us proving it at every turn.[1] In my last article I showed you how to create SSH Identities/Pubkeys, which can be used as an alternative to password authentication. However, I then went right back and told you to passphrase protect them, so now you were substituting one password for another, seemingly gaining nothing. This week we have the payoff. We'll take the Identity/Pubkey trust we created last time, and learn how to use the Starting up the AgentTo start up the agent you can simply run it on the command line: $ ssh-agent SSH_AUTH_SOCK=/tmp/ssh-OqdW7921/agent.7921; export SSH_AUTH_SOCK; SSH_AGENT_PID=7922; export SSH_AGENT_PID; echo Agent pid 7922; When the agent starts, it writes some information to your screen that you can use to set up your shell's environment variables. In the above example, it is using Bourne shell syntax. If you were in a C-shell, say The # Note: no ssh agent related variables yet $ set | grep SSH_ # Run it inside backticks, which will capture the output and # pass it to 'eval' which will run it in your current shell. $ eval `ssh-agent` Agent pid 7943 # And now those variables are in your shell, ready to use. $ set | grep SSH_ SSH_AUTH_SOCK=/tmp/ssh-xoGi7942/agent.7942 SSH_AGENT_PID=7943 If you have the Putting keys into the agentThe agent isn't very useful until you've actually put keys into it. All your agent key management is handled by the So, let's actually put our keys into the agent:
$ ps -fp $SSH_AGENT_PID UID PID PPID C STIME TTY TIME CMD lainee 7943 1 0 15:52 ? 00:00:00 ssh-agent # Are there any keys in there currently? # 'ssh-add -l' (list) will show us. $ ssh-add -l The agent has no identities. # Let's import the default keys. In our case, we have # each key protected with the same passphrase, which is # why it only asks once. $ ssh-add Enter passphrase for /home/lainee/.ssh/id_rsa: Identity added: /home/lainee/.ssh/id_rsa (/home/lainee/.ssh/id_rsa) Identity added: /home/lainee/.ssh/id_dsa (/home/lainee/.ssh/id_dsa) Identity added: /home/lainee/.ssh/identity (lainee@desktop) # What's in our agent now? $ ssh-add -l 1024 79:e9:6f:99:a3:2d:ae:f3:bd:3a:87:6c:ed:4e:bb:ad lainee@desktop (RSA1) 1024 23:d5:2b:20:02:a4:1a:c2:d0:d8:66:8f:a9:67:db:c0 id_dsa (DSA) 1024 e8:17:67:cf:9c:24:2b:59:ad:48:1d:e6:ea:d6:d9:3d id_rsa(RSA) # And let's add a few one-off keys also $ ssh-add ssh-keys/id* Enter passphrase for id_dsa.webrooters: Identity added: id_dsa.webrooters (id_dsa.webrooters) Enter passphrase for identity.webrooters: Identity added: identity.webrooters (webrooters@my_company.com) # What's in our agent now? $ ssh-add -l 1024 79:e9:6f:99:a3:2d:ae:f3:bd:3a:87:6c:ed:4e:bb:ad lainee@desktop (RSA1) 1024 23:d5:2b:20:02:a4:1a:a9:67:db:c0:c2:d0:d8:66:8f id_dsa (DSA) 1024 e8:17:67:cf:9c:24:2b:59:ad:48:1d:e6:ea:d6:d9:3d id_rsa(RSA) 1024 1a:c2:d0:d8:66:23:d5:2b:20:02:a4:8f:a9:67:db:c0 id_dsa.webrooters (DSA) 1024 ae:f3:bd:3a:87:79:e9:6f:99:4e:bb:ad:a3:2d:6c:ed webrooters@my_company.com (RSA1) Above we used Deleting keys from the agent You can use the # List keys $ ssh-add -l 1024 79:e9:6f:99:a3:2d:ae:f3:bd:3a:87:6c:ed:4e:bb:ad lainee@desktop (RSA1) 1024 23:d5:2b:20:02:a4:1a:a9:67:db:c0:c2:d0:d8:66:8f id_dsa (DSA) 1024 e8:17:67:cf:9c:24:2b:59:ad:48:1d:e6:ea:d6:d9:3d id_rsa(RSA) 1024 1a:c2:d0:d8:66:23:d5:2b:20:02:a4:8f:a9:67:db:c0 id_dsa.webrooters (DSA) 1024 ae:f3:bd:3a:87:79:e9:6f:99:4e:bb:ad:a3:2d:6c:ed webrooters@my_company.com (RSA1) # Remove the key that came from the file ~/.ssh/id_dsa.webrooters # from the agent. (Does not remove the file from the directory.) $ ssh-add -d ~/.ssh/id_dsa.webrooters Identity removed: id_dsa.webrooters (id_dsa.webrooters.pub) # List keys again $ ssh-add -l 1024 79:e9:6f:99:a3:2d:ae:f3:bd:3a:87:6c:ed:4e:bb:ad lainee@desktop (RSA1) 1024 23:d5:2b:20:02:a4:1a:a9:67:db:c0:c2:d0:d8:66:8f id_dsa (DSA) 1024 e8:17:67:cf:9c:24:2b:59:ad:48:1d:e6:ea:d6:d9:3d id_rsa(RSA) 1024 ae:f3:bd:3a:87:79:e9:6f:99:4e:bb:ad:a3:2d:6c:ed webrooters@my_company.com (RSA1) Why might you want to delete keys from the agent? The most common reasons are:
Too Many Agent Keys?SSH servers only allow you to attempt to authenticate a certain number of times. Each failed password attempt, each failed pubkey/identity that is offered, etc, take up one of these attempts. If you have a lot of SSH keys in your agent, you may find that an SSH server may kick you out before allowing you to attempt password authentication at all. If this is the case, there are a few different workarounds.
Agent Security Concerns The When the agent starts, it creates a new directory in
root# set | grep SSH_ root# ssh-add -l Cannot connect to your agent. root# ls -l /tmp/ssh-*/* srwx------ 1 lainee alandra 0 Jan 21 11:51 /tmp/ssh-OqdW7921/agent.7921 root# SSH_AUTH_SOCK=/tmp/ssh-OqdW7921/agent.7921 root# export SSH_AUTH_SOCK root# ssh-add -l 1024 79:e9:6f:99:a3:2d:ae:f3:bd:3a:87:6c:ed:4e:bb:ad lainee@desktop (RSA1) 1024 23:d5:2b:20:02:a4:1a:a9:67:db:c0:c2:d0:d8:66:8f id_dsa (DSA) 1024 e8:17:67:cf:9c:24:2b:59:ad:48:1d:e6:ea:d6:d9:3d id_rsa(RSA) 1024 ae:f3:bd:3a:87:79:e9:6f:99:4e:bb:ad:a3:2d:6c:ed webrooters@my_company.com (RSA1) So the bad news is that your agent keys are usable by the Is there any way to keep At this point you're probably going to realize that we're still fighting a losing battle. The local However this will prevent root on machines to which you've forwarded the agent from accessing your agent. Agent forwardingOne of the nice things about the agent is that it can follow you as you SSH from machine to machine. The default in newer versions of OpenSSH is to disable agent forwarding by default, so you'll need to decide when it's correct for you to use and specify it appropriately. How does the agent forwarding actually work? In short, the agent is running on one machine, and each time you SSH with agent forwarding, the server creates a 'tunnel' back through the SSH connection to the agent so it's available for any further SSH connections. Let's say we're on our desktop, we SSH to a management server with agent forwarding, and from the management server SSH to our mail server. Here's what happens:
Using agent forwarding can save you a lot of time and typing. Also note that since your agent is available to any machine to which you forward it, it's also accessible by Turn off agent forwarding globally Unless you have a good reason to forward the agent by default, you should verify that the agent forwarding is disabled by default. Locate the global
Host * ForwardAgent no This will disable Agent forwarding on the command line To forward your agent via the command line, just include a
desktop$ ssh -A user@remotehost The Agent forwarding via the config file If you have a host to which you always wish to forward your agent, without the trouble of supplying the
$ cat ~/.ssh/config Host shellserver ForwardAgent yes Host management-server ForwardAgent yes Host * ForwardAgent no Although the restrictive Other Useful Features There are several other command line flags and features of
Conclusion We've seen how ssh-agent can save a great deal of time and typing when used with SSH Identity/Pubkey authentication. While we're still some ways away from computers simply knowing who we are and what we have access to, tools like ssh-agent go a long way to keeping authentication strong yet making it easy-to-use. |
Notes
[1] Unfortunately, many of those users are the same ones that choose their sweethearts's name for their password, and then stick it on their monitor in case they forget it. [2] You may choose to have different passphrases for the keys to prefer security over convienience, but if you use a strong passphrase and each key has equivalent access, then a compromise of one is no worse than a compromise of all anyway. About the author Brian Hatch is the author of Hacking Linux Exposed, 2nd Edition, Building Linux VPNs, and of the Linux Security: Tips, Tricks, and Hackery Newsletter. In order to exit an xterm, he frequently needs to log out of ten or more SSH connections, each with cascaded port forwards, to get back to his desktop shell. And that's not even including all the virtual More articles by this author View more articles by Brian Hatch on SecurityFocus. |
This article originally appeared on SecurityFocus.com -- reproduction in whole or in part is not allowed without expressed written consent.