Blog | G5 Cyber Security

SSH Session Hijacking: Risks & Prevention

TL;DR

Yes, an SSH session can be taken from memory, but it’s not easy. Attackers need root access or specific vulnerabilities to exploit. Strong authentication (keys over passwords), regular key rotation, and monitoring for suspicious activity are your best defenses.

How SSH Sessions Can Be Compromised

An attacker gaining access to a system where an active SSH session is running can potentially hijack that session. Here’s how:

  1. Memory Dumping: If an attacker gains root privileges, they could dump the server’s memory and search for SSH keys or other sensitive information used in the session. This is a complex process but possible.
  2. Process Access: An attacker with sufficient permissions might be able to attach to the sshd (SSH daemon) process and extract session details.
  3. Shared Memory Exploits: Vulnerabilities in SSH implementations or related libraries could allow attackers to access shared memory regions containing session data.
  4. Keyloggers/Screen Recorders: While not directly taking the session from memory, malware like keyloggers can capture passwords entered during SSH login, and screen recorders can steal information displayed in the terminal.

Preventing SSH Session Hijacking

Here’s a step-by-step guide to protect your SSH sessions:

  1. Use SSH Keys: This is the most important step! Disable password authentication entirely in sshd_config.
  • Key Rotation: Regularly rotate your SSH keys. Don’t use the same key for extended periods.
  • Strong Passphrases: If you must use keys with passphrases (not recommended), make them long and complex.
  • Agent Forwarding Restrictions: Be cautious when using agent forwarding (
    -A

    option). Only enable it if absolutely necessary, and understand the security implications. It can allow an attacker on a compromised machine to use your keys to connect to other servers.

  • Limit User Access: Grant users only the permissions they need. Avoid giving unnecessary root access.
  • Firewall Rules: Restrict SSH access to specific IP addresses or networks using a firewall (e.g., ufw, iptables). For example, allow connections from your home IP address:
    sudo ufw allow from  to any port ssh
  • Disable Root Login: Prevent direct root login via SSH. Use a regular user account and then escalate privileges with sudo.
    • Edit the configuration file:
      sudo nano /etc/ssh/sshd_config
    • Find and change this line:
      PermitRootLogin no
    • Restart SSH service:
      sudo systemctl restart sshd
  • Two-Factor Authentication (2FA): Implement 2FA for an extra layer of security.
  • Monitor Logs: Regularly review SSH logs (
    /var/log/auth.log

    or

    /var/log/secure

    ) for suspicious activity, such as failed login attempts or connections from unknown IP addresses. Use tools like fail2ban to automatically block malicious IPs.

  • Keep Software Updated: Regularly update your operating system and SSH software to patch security vulnerabilities.
  • Detecting a Compromised Session

    If you suspect an SSH session has been compromised:

    Exit mobile version