Safeguard
Application Security•

Detecting and Mitigating SSH Hijacking (T1563.001)

A forwarded SSH agent or an exposed agent socket can let an attacker ride your sessions. What MITRE documents, how to find agent sockets, and the settings that close the gap.

Safeguard Security Team
6 min read

SSH is built to avoid typing passwords again and again. The agent holds your unlocked key so every new connection is instant. That convenience is also a target. SSH Hijacking (T1563.001) is a Lateral Movement technique for Linux and macOS, and a subtechnique of Remote Service Session Hijacking.

How attackers use it

MITRE explains that SSH is the standard means of remote access on Linux and macOS. It builds an encrypted tunnel, and authentication commonly uses a password, a certificate, or an asymmetric key pair.

To move laterally from a compromised host, adversaries can use the trust relationships created through public key authentication in active SSH sessions, by hijacking an existing connection to another system. MITRE says this can happen through compromising the SSH agent itself, or through access to the agent's socket. With root access, MITRE says hijacking SSH sessions is likely trivial.

This differs from use of SSH as a Remote Service, because it takes over an existing session rather than creating a new one with Valid Accounts. So there may be no new login to see.

How an agent socket becomes a risk

When you connect with an agent, a socket file is created, and an environment variable named SSH_AUTH_SOCK points to it. Any process that can read that socket can ask the agent to authenticate. The owner's file permissions protect it, and root can bypass them.

Agent forwarding extends the problem. It makes the socket reachable on the remote machine you logged into, so an administrator or attacker with root on that machine can use your keys. This is why MITRE's first recommendation on this technique is to disable agent forwarding where it is not needed.

How to detect it

MITRE's analytics, for Linux and macOS:

  • Reuse of SSH agent sockets across multiple users or processes.
  • Unusual access to agent sockets in /tmp or /private/tmp, and to the user's ~/.ssh directory.
  • Process access to another user's SSH_AUTH_SOCK.
  • Lateral SSH activity with no corresponding login events, correlated with unusual network flows to internal systems.

Auditing access to socket files, with auditd on Linux, is a good place to start. You are looking for a process owned by one user touching another user's socket.

Making agent forwarding the exception

Agent forwarding is useful but it expands who can use your keys. If you need to reach a server through a jump host, there are safer patterns. The SSH client supports a ProxyJump option, written as ssh -J jumphost target, which passes the connection through the jump host without exposing your agent there. That is a widely documented alternative, and it is worth standardizing in your team's SSH configuration.

Where an agent is needed, limit its reach:

  • Add keys with a lifetime, so they drop out of the agent automatically. The ssh-add -t option sets that lifetime.
  • Use keys protected by a passphrase, as MITRE advises.
  • Use separate keys for separate purposes, so a hijacked session reaches less.

Responding to suspected hijacking

If you think an agent or a session was abused:

  1. Identify which user's session and which hosts it could reach, using the authorized keys files on those hosts to see where the key was trusted.
  2. End the agent and any suspicious sessions. Unload the keys.
  3. Rotate the key pair, remove the old public key from every authorized keys file, and distribute the new one.
  4. Review those target hosts for activity that has no matching login event, which is the signature MITRE describes.
  5. Close the path that gave the adversary access to the socket, such as a root compromise or an over-permissive forwarding setting.

Remember the point from MITRE: with root, hijacking is likely trivial. If root was compromised on the host where the agent lived, assume the keys were usable.

How to reduce the risk

MITRE lists four mitigations.

  1. Restrict file and directory permissions. Set proper file permissions, and harden the system to prevent root privilege escalation, since root makes hijacking trivial.
  2. Disable or remove feature or program. Disable agent forwarding on systems that do not explicitly need it.
  3. Password policies. Protect SSH key pairs with strong passphrases, and avoid key stores such as ssh-agent unless they are properly protected.
  4. Privileged account management. Do not allow SSH access as root or other privileged accounts.

Check your own environment

These checks read state only.

# Linux or macOS: which keys does my agent currently hold?
ssh-add -l

# Linux or macOS: where is my agent socket?
echo $SSH_AUTH_SOCK

# Linux: list agent sockets in /tmp and their owners
ls -l /tmp/ssh-*

# Linux (as root): what is the effective agent forwarding setting for the SSH server?
sshd -T | grep -i allowagentforwarding

Then ask:

  • Is agent forwarding enabled on servers that are only ever jump targets?
  • Do your SSH keys have passphrases, and do you know which hosts trust each key?
  • Does anyone log in as root over SSH?

Private keys also leak through code and configuration. The demo below shows how secret scanning finds credentials that were never meant to be shared.

Click through it at your own pace, or open it full screen.

Common mistakes

  • Enabling agent forwarding globally in the client configuration for convenience, so every connection exposes the agent.
  • Relying on file permissions alone for sockets on hosts where many people have root.
  • Rotating the key but leaving the old public key in authorized keys files, which keeps the old trust path open.

Quick check

What two routes does MITRE name for hijacking an SSH session?

Compromising the SSH agent itself, or gaining access to the agent's socket.

Why is root access so important to this technique?

File permissions protect the agent socket from other users, but root can bypass them. MITRE says hijacking is likely trivial for an adversary with root.

Sources

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.