17

ssh-add -l displays that I have 3 RSA keys added to my SSH agent.

ssh-add -L displays those public keys.

How do I get the private keys as well, so that I can save them to a file? Or is it by design that this is impossible? How does it work then?

Can ssh-agent be asked to do operations using the private key? How can I ask it to encrypt/decrypt a number for me?

It's OK that I have to write code for this (the programming language doesn't matter), but I'd prefer using an existing tool or a library.

Vanuan
  • 25,939
  • 9
  • 90
  • 96
pts
  • 64,123
  • 15
  • 92
  • 159

4 Answers4

11

It's not possible to get the private key or to perform encryption using the protocol between ssh and ssh-agent, but it's possible to get the private key by dumping the memory of the ssh-agent. On Linux you have to be root to do the memory dump.

I've just found a very good explanation about how ssh-agent works: http://www.unixwiz.net/techtips/ssh-agent-forwarding.html . This partially answers some of my questions.

  • One of the more clever aspects of the agent is how it can verify a user's identity (or more precisely, possession of a private key) without revealing that private key to anybody.

  • One of the security benefits of agent forwarding is that the user's private key never appears on remote systems or on the wire, even in encrypted form.

Thus the protocol between the SSH client and the ssh-agent proviedes no way in SSH1 or SSH2 to get out the private keys from an ssh-agent.

However, as root you can get a memory dump of ssh-agent, and try to extract the private key from there. https://blog.netspi.com/stealing-unencrypted-ssh-agent-keys-from-memory does exactly that, and there are other pieces of software mentioned in the comment section of that page. However, the software on that page didn't work for me on Debian buster: the memory dump didn't contain any keys, even though ssh-add -l has displayed an RSA key.

pts
  • 64,123
  • 15
  • 92
  • 159
  • 2
    That is not really an answer to the question - namely, whether there is any way to ask ssh-agent for its decrypted keys, for example to feed them to some other SSH client which does not know how to use ssh-agent. – Jesse Glick Aug 07 '12 at 13:52
  • 5
    The answer to the question: no, it's not possible. What's possible lis listed in the linked document, and this one isn't. – pts Dec 09 '13 at 15:45
  • 3
    Quite contrary, _everything_ is possible in a virtual world, it just depends on the time and effort to achieve the goal. For example, this is how you can extract/steal keys from ssh-agent: https://blog.netspi.com/stealing-unencrypted-ssh-agent-keys-from-memory/ – galaxy Sep 05 '15 at 05:43
  • @galaxy: Thank you for the link! I've added it to my answer with some explanation. – pts Apr 25 '18 at 21:56
1

In my case I accidentally deleted the .ssh folder but my keys are loaded. But since I can't recover those from ssh-add so I had to use file recovery software.

Zelocox
  • 31
  • 4
  • 1
    As an alternative of disk recovery software, you can dump the memory of a running Linux process (e.g. *ssh-agent*) as root (`sudo cat /proc/$$/mem`, but add proper seeking). Probably disk recovery software is easier to use. – pts Feb 20 '18 at 13:13
1

If on Windows 10, apparently the method described in this blog post worked in 2018. The author links to proof-of-concept code on github:

tl;dr

Private keys are protected with DPAPI and stored in the HKCU registry hive. I released some PoC code here to extract and reconstruct the RSA private key from the registry

jhnc
  • 5,023
  • 4
  • 19
-6

You probably don't need to. Most likely you just want a public key:

ssh-add -L > ~/.ssh/id_rsa.pub
Vanuan
  • 25,939
  • 9
  • 90
  • 96
  • 1
    In this question I ask for the private key. I already mention `ssh-add -L` in the question to extract the public key. – pts Feb 05 '19 at 10:45
  • 1
    I wouldn't blindly redirect any output to `~/.ssh/id_rsa.pub`, because it may destroy useful data already in the file. – pts Feb 05 '19 at 10:46