SSH Hijacking
Adversaries may hijack a legitimate user's SSH session to move laterally within an environment. This technique exploits trust relationships established via public key authentication by taking over existing SSH connections rather than creating new ones. The primary attack vector involves accessing the SSH agent socket (typically at /tmp/ssh-XXXXX/agent.NNNN), which allows any process with access to the socket to authenticate as the session owner without knowing their password or private key. With root access, an attacker can enumerate all SSH agent sockets on the system, set SSH_AUTH_SOCK to point to a victim's agent socket, and transparently use loaded SSH keys to authenticate to remote systems. More invasive methods include using ptrace-capable debuggers (gdb, strace) to inject commands into active SSH sessions or extract credentials from sshd process memory. MEDUSA malware has been documented using SSH hijacking for credential capture, and the technique has been leveraged by UNC3886 in post-exploitation lateral movement campaigns.
let SSHHijackProcessEvents = DeviceProcessEvents
| where Timestamp > ago(24h)
| where (
// SSH agent socket direct path reference in command line
ProcessCommandLine has "/tmp/ssh-"
// Listing loaded SSH keys from an agent (reconnaissance for hijacking)
or (FileName =~ "ssh-add" and ProcessCommandLine has "-l")
// Searching /tmp for SSH agent socket files
or (FileName =~ "find" and ProcessCommandLine has "/tmp" and ProcessCommandLine has_any ("agent", "ssh-"))
// Debugger attachment to SSH processes (ptrace-based session injection)
or (FileName in~ ("gdb", "strace", "ltrace") and ProcessCommandLine has_any ("sshd", "ssh-agent"))
// SSH_AUTH_SOCK environment override (hijacking agent socket)
or (FileName =~ "ssh" and ProcessCommandLine has "SSH_AUTH_SOCK=")
)
| extend IsSocketEnum = ProcessCommandLine has_any ("/tmp/ssh-", "agent.")
| extend IsAgentKeyList = (FileName =~ "ssh-add" and ProcessCommandLine has "-l")
| extend IsDebuggerAttach = (FileName in~ ("gdb", "strace", "ltrace") and ProcessCommandLine has_any ("sshd", "ssh-agent"))
| extend IsSocketSearch = (FileName =~ "find" and ProcessCommandLine has "/tmp" and ProcessCommandLine has_any ("agent", "ssh-"))
| extend IsAuthSockOverride = (ProcessCommandLine has "SSH_AUTH_SOCK=");
let SSHHijackFileEvents = DeviceFileEvents
| where Timestamp > ago(24h)
| where FolderPath startswith "/tmp/ssh-"
| where ActionType in ("FileAccessed", "FileRead", "FileCreated")
// Exclude legitimate SSH daemon and agent processes
| where InitiatingProcessFileName !in~ ("sshd", "ssh-agent")
| extend IsSocketEnum = true
| extend IsAgentKeyList = false
| extend IsDebuggerAttach = false
| extend IsSocketSearch = false
| extend IsAuthSockOverride = false;
SSHHijackProcessEvents
| extend EventType = "ProcessEvent"
| extend ProcessCmdLine = ProcessCommandLine
| union (
SSHHijackFileEvents
| extend EventType = "FileAccessEvent"
| extend ProcessCmdLine = InitiatingProcessCommandLine
| extend FileName = InitiatingProcessFileName
| extend AccountName = AccountName
)
| project Timestamp, DeviceName, AccountName, FileName, ProcessCmdLine,
EventType, IsSocketEnum, IsAgentKeyList, IsDebuggerAttach, IsSocketSearch, IsAuthSockOverride
| sort by Timestamp desc Data Sources
Required Tables
False Positives
- Legitimate SSH key management — users listing their own loaded keys with ssh-add -l during normal workflow, particularly on developer workstations and jump boxes
- System administrators using gdb or strace for authorized debugging of SSH daemon issues on development or staging servers
- Automated configuration management agents (Ansible, Chef, Puppet) that enumerate SSH-related processes or socket paths during host inventory collection
- SSH multiplexing via ControlMaster that creates and accesses socket files in /tmp in ways structurally similar to hijacking sockets
- Security scanning and endpoint agent tools that read /proc/<pid>/environ or enumerate /tmp to collect environment variables for compliance auditing
References (10)
- https://attack.mitre.org/techniques/T1563/001/
- https://www.blackhat.com/presentations/bh-usa-05/bh-us-05-boileau.pdf
- https://web.archive.org/web/20210311184303/https://www.clockwork.com/news/2012/09/28/602/ssh_agent_hijacking/
- https://www.slideshare.net/morisson/mistrusting-and-abusing-ssh-13526219
- https://matrix.org/blog/2019/05/08/post-mortem-and-remediations-for-apr-11-security-incident/
- https://cloud.google.com/blog/topics/threat-intelligence/unc3886-uses-fortinet-vmware-0-days
- https://man7.org/linux/man-pages/man2/ptrace.2.html
- https://github.com/redcanaryco/atomic-red-team/blob/master/atomics/T1563.001/T1563.001.md
- https://linux-audit.com/linux-auditd-best-practice-configuration/
- https://falco.org/docs/rules/default-ruleset/
Unlock Pro Content
Get the full detection package for T1563.001 including response playbook, investigation guide, and atomic red team tests.