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.

目录
相关文章
|
6月前
|
Linux 网络安全
|
7月前
|
Shell 网络安全 开发工具
GitLab创建项目的时候需要SSH Keys
GitLab创建项目的时候需要SSH Keys
41 0
|
网络安全 开发工具 git
git 生成ssh keys
最近从svn转到gitlab 记录一下如何生成ssh keys 首先下载GitExtensions246SetupComplete.msi并安装,如果已经安装了git,那么安装途中就跳过就行 安装完毕后,选择如下图中的命令行   然后会弹出这样的一个命令行界面:   然后输入以下命令: ssh-keygen -t ras -C "your_mail_address@bangdao-tech.
1747 0
|
网络安全 数据安全/隐私保护
TortoiseGit为github账号添加SSH keys,解决pull总是提示输入密码的问题
每次同步或者上传代码到githun上的代码库时,需要每次都输入用户名和密码,这时我们设置一下SSH key就可以省去这些麻烦了。若果使用TortoiseGit作为github本地管理工具,TortoiseGit使用扩展名为ppk的秘钥,而不是ssh-keygen生成的rsa密钥。
1667 0
|
网络安全 开发工具 git
gitlab 创建SSH Keys 报500错
gitlab 创建SSH Keys 报500错 看了一下日志 root@322323:/home/git/gitlab/log# cat production.log Errno::ENOMEM (Cannot allocate memory - ssh): lib/gitlab/popen.
1203 0
mjb
|
Shell 网络安全 开发工具
Git多个SSH KEYS解决方案(含windows自动化、TortoiseGit、SourceTree等)
工作过程中,经常会使用到多个git仓库,每个git仓库对应一个账号,可以理解为每个git仓库对应一个ssh key,因此我们需要管理多个ssh key。 一、快速创建ssh key 1. 创建SSH keys: mkdir -p ~/.ssh s...
mjb
11752 0
|
网络安全 开发工具 git
git生成SSH keys遇到的问题
<p>使用git有一段时间了,每次push更新github上的project都需要输入username和password,之前不以为然,但是时间久了就开始厌倦了,所以还是重新在本机子上生成一个SSH key绑定到github账号里,其实之前有弄过,只是后来换系统后就没弄了,现在github上代码更新的比频繁,所以有必要再生成一次。但是按照官方的方法(https://help.github.
2181 0
|
网络安全 Go
Loading GPG / SSH Keys from a USB Key, Round 2
Back in January I talked about setting up some scripts to automatically load ssh/gpg keys into t...
1287 0
|
网络安全
Using Rsync and SSH Keys, Validating, and Automation
http://troy.jdmz.net/rsync/index.html
632 0