repositories

daemon-sec-cheatsheet

The cheatsheet vault for operators: AD, enumeration, exploitation, priv-esc, web, DFIR

clonegit clone https://git.daemon-sec.xyz/daemon-sec-cheatsheet.git
ssh-forward-agent-exploitation.mdMarkdown55 lines3.0 KiBrendered raw download
---
title: "SSH Agent Forwarding Exploitation"
section: "Linux"
sectionSlug: "linux-hardening"
sourcePath: "src/linux-hardening/user-information/ssh-forward-agent-exploitation.md"
sourceUrl: "https://github.com/HackTricks-wiki/hacktricks/blob/188de82beb54e70956b2952367a0af91d26758b8/src/linux-hardening/user-information/ssh-forward-agent-exploitation.md"
sha: "188de82beb54e70956b2952367a0af91d26758b8"
isIndex: false
modified: true
license: "CC-BY-NC-4.0"
---

# SSH Agent Forwarding Exploitation

## Summary

What can you do if the system-wide SSH client configuration (commonly `/etc/ssh/ssh_config`) or `$HOME/.ssh/config` contains the following directive?<sup>[[3]](#references)</sup>

```text
ForwardAgent yes
```

`ForwardAgent yes` forwards the authentication agent to the remote host for matching connections. If you have root on an intermediate host where another user's agent was forwarded, you can access the forwarded Unix-domain socket and ask that agent to authenticate with its loaded identities; this can let you access hosts where those identities are authorized.<sup>[[1]](#references)[[2]](#references)[[3]](#references)</sup>

Useful socket hunting commands on Linux systems include (the exact socket directory depends on the client and forwarding setup):<sup>[[1]](#references)[[2]](#references)</sup>

```bash
ls -la /run/user/*/ssh-* /tmp/ssh-* 2>/dev/null
find /run/user /tmp -type s -name 'agent.*' 2>/dev/null
```

If you have access to Bob's forwarded socket, use it for an SSH connection:<sup>[[1]](#references)[[2]](#references)</sup>

```bash
SSH_AUTH_SOCK=/tmp/ssh-haqzR16816/agent.16816 ssh bob@boston
```

### Why does this work?

Setting `SSH_AUTH_SOCK` points the SSH client at a Unix-domain socket for the agent; it does not load Bob's private-key file. The agent performs private-key operations itself and returns the result, rather than sending private-key material to the client or over the network.<sup>[[1]](#references)[[2]](#references)</sup>

If Bob has already loaded a key into the agent and that key is authorized on another host, anyone who can use the socket can ask the agent to authenticate there as Bob without knowing the key's passphrase at request time; confirmation and destination constraints may still limit the request.<sup>[[1]](#references)[[2]](#references)[[4]](#references)</sup>

Access to the socket is normally enough; extracting key material from the agent process is unnecessary and is not part of the agent protocol. The owning user or root may be able to bypass socket permissions, so protect hosts receiving forwarded agents accordingly.<sup>[[2]](#references)[[3]](#references)</sup>

## Long explanation and exploitation

**Read [Clockwork's original research](https://www.clockwork.com/insights/ssh-agent-hijacking/)**.<sup>[[1]](#references)</sup>

## References

- [1] [SSH Agent Hijacking](https://www.clockwork.com/insights/ssh-agent-hijacking/)
- [2] [ssh-agent(1)](https://man.openbsd.org/ssh-agent.1)
- [3] [ssh_config(5)](https://man.openbsd.org/ssh_config.5)
- [4] [ssh-add(1)](https://man.openbsd.org/ssh-add.1)