Stealing unencrypted SSH-agent keys from memory

简介: If you've ever used SSH keys to manage multiple machines, then chances are you've used SSH-agent.

If you've ever used SSH keys to manage multiple machines, then chances are you've used SSH-agent. This tool is designed to keep a SSH key in memory so that the user doesn't have to type their passphrase in every time. However, this can create some security risk. A user running as root may have the ability to pull the decrypted SSH key from memory and reconstruct it.

Due to needing root access, this attack may seem useless. For example, an attacker may be able to install a keylogger and use that to obtain the passphrase for the SSH key. However, this causes the attacker to have to wait for the target to type in their passphrase. This might be hours, days, or weeks, depending on how often the target logs out. This is why obtaining the SSH key from memory is vital to pivoting to other machines in a speedy fashion.

Using SSH-agent

A common method of using SSH-agent is running "SSH-agent bash" and then "SSH-add" to add the key to the agent. Once added, the key will stay in the SSH-agent's stack until the process ends, another key is added, or the user uses the -d or -D option with SSH-add. Most people will run this once and then forget about it until they need to reboot.

Pulling a SSH Key From Memory

There are a few ways to create a copy of the SSH-agents memory. The easiest way is through the use of gdb. Gdb uses the ptrace call to attach to the SSH-agent. This provides gdb with the privileges necessary to create a memory dump of the running process. The grabagentmem.sh script provides a way of automating the dumping of this memory. By default, when it runs it will create a memory dump of the stack for each SSH-agent process. These files are named SSHagent-PID.stack.

root@test:/tmp# grabagentmem.sh 
Created /tmp/SSHagent-17019.stack 

If gdb is not available on the system, then it might be feasible to take a memory dump of the entire machine and use volatility to extract the stack of the SSH-agent processes. However, this process is currently out of the scope for this document.

Parsing SSH Keys From the Memory Dump

Once we have a copy of the stack it becomes possible to extract the key from this file. However, the key is kept in the stack in a different format then the one that was generated by SSH-keygen. This is where the parse_mem.py script comes in handy. This script requires the installation of the pyasn1 python module. Once that is installed the script can be run against the memory file. If that memory file contains a valid RSA SSH key then it will save it to disk. Future versions of the tool may support additional key formats, such as DSA, ECDSA, ED25519, and RSA1.

root@test:/tmp# parse_mem.py /tmp/SSHagent-17019.stack /tmp/key
Found rsa key
Creating rsa key: /tmp/key.rsa 

This key.rsa file can then be used as an argument to the -i switch in SSH. This will act like the original user's key, only without requiring a pass phrase to unlock it.

Obtaining valid, usable SSH keys can help a penetration tester gain further access into a client's network. It's common for keys to be used on both the user's account, as well as the root account on servers. It is also possible that a server is configured to only allow key access. Having access to an unencrypted key can make moving around the environment much easier.

目录
相关文章
|
10天前
|
Linux 网络安全
linux免密登录报错 Bad owner or permissions on /etc/ssh/ssh_config.d/05-redhat.conf
linux免密登录报错 Bad owner or permissions on /etc/ssh/ssh_config.d/05-redhat.conf
22 1
|
网络安全
无法启动ssh:/run/sshd must be owned by root and not group or world-writable.
无法启动ssh:/run/sshd must be owned by root and not group or world-writable.
261 0
无法启动ssh:/run/sshd must be owned by root and not group or world-writable.
|
网络安全
ssh启用报错 unable to load host key /etc/ssh/ssh_host_ed25519_key bad permissions
ssh启用报错 unable to load host key /etc/ssh/ssh_host_ed25519_key bad permissions
306 0
|
Ubuntu Linux 网络安全
Ubuntu的ssh免密登录出现错误:sign_and_send_pubkey: signing failed: agent refused operation
Ubuntu的ssh免密登录出现错误:sign_and_send_pubkey: signing failed: agent refused operation
271 0
Ubuntu的ssh免密登录出现错误:sign_and_send_pubkey: signing failed: agent refused operation
|
网络安全
SSH 遇到 WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
SSH 遇到 WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
230 0
|
网络安全
将ssh key添加到ssh-agent使用命令ssh-add ~/.ssh/id_rsa结果报错
将ssh key添加到ssh-agent使用命令ssh-add ~/.ssh/id_rsa结果报错 Error connecting to agent: No such file or directory 开启ssh-agent:ssh-agent 报错: unable to start ssh-...
8752 0
|
网络安全
生成多个ssh key添加到ssh-agent测试连接报错
ssh key添加到ssh-agent测试连接报错 ssh -T git@github.com 如下报错,说明是~/.ssh/config文件问题.ssh/config: line 5: Bad configuration option: usekeychain修改配置文件解决问题,完整流程看这里Mac系统如下 Host new HostName github.
3044 0
|
Linux 网络安全 数据安全/隐私保护