On this page 30 sections
  1. What SSH is
  2. Default port
  3. Initial enumeration
  4. SSH banner
  5. Useful Nmap SSH scripts
  6. SSH authentication methods
  7. Password authentication
  8. Public-key authentication
  9. Private and public SSH keys
  10. Using a private SSH key
  11. Why chmod 600 matters
  12. Private-key passphrases
  13. authorized_keys
  14. Why authorized_keys matters
  15. Host keys and fingerprints
  16. known_hosts
  17. User enumeration
  18. Hydra SSH authentication testing
  19. sshpass
  20. SCP file transfer
  21. SSH vs SCP port option
  22. Metasploit SSH version scanner
  23. Metasploit SSH login
  24. Metasploit public-key authentication
  25. Post-authentication checks
  26. Important SSH files
  27. Important sshd_config settings
  28. Enumeration workflow
  29. SSH tunneling
  30. Quick reference
01

What SSH is

SSH, or Secure Shell, provides encrypted remote administration, command execution and file transfer. It is commonly used to manage Linux and Unix systems securely over a network.

02

Default port

SSH normally listens on TCP port 22, although administrators can configure it to use another port. Service detection is therefore more reliable than assuming every service on port 22 is SSH.

03

Initial enumeration

Start by confirming the SSH service and identifying the implementation and version. The banner may reveal software such as OpenSSH and sometimes information about the operating system.

SSH version detection
nmap -sV -p 22 <TARGET>
Default scripts and version detection
nmap -sC -sV -p 22 <TARGET>
Manual banner grabbing
nc -nv <TARGET> 22
04

SSH banner

An SSH server normally presents an identification string during connection establishment. This can reveal the SSH protocol version and server implementation. Version information is useful for enumeration but does not by itself prove that a vulnerability is exploitable.

Banner detection
nmap -sV -p 22 <TARGET>
05

Useful Nmap SSH scripts

Nmap can enumerate SSH host keys, supported cryptographic algorithms and authentication methods.

Enumerate host keys
nmap -p 22 --script ssh-hostkey <TARGET>
Enumerate algorithms
nmap -p 22 --script ssh2-enum-algos <TARGET>
Authentication methods
nmap -p 22 --script ssh-auth-methods --script-args="ssh.user=<USERNAME>" <TARGET>
List local SSH NSE scripts
ls /usr/share/nmap/scripts/ssh*
06

SSH authentication methods

SSH supports different authentication mechanisms. The two most important during basic enumeration are password authentication and public-key authentication.

07

Password authentication

Password authentication requires a valid username and password. If valid credentials are obtained during an authorised assessment, they can be tested directly with the SSH client.

Connect with username
ssh <USERNAME>@<TARGET>
Custom SSH port
ssh -p <PORT> <USERNAME>@<TARGET>
08

Public-key authentication

Public-key authentication uses a cryptographic key pair. The client keeps the private key while the corresponding public key is stored on the server. The server verifies that the client possesses the correct private key.

09

Private and public SSH keys

A private SSH key should be treated like a credential. Common private-key filenames include id_rsa and id_ed25519. Their public counterparts commonly end with .pub.

Common RSA private key
~/.ssh/id_rsa
Common Ed25519 private key
~/.ssh/id_ed25519
RSA public key
~/.ssh/id_rsa.pub
10

Using a private SSH key

If a private key is discovered during an authorised assessment, it may allow authentication to systems where the corresponding public key is trusted. OpenSSH expects private-key files to have restrictive permissions.

Fix private-key permissions
chmod 600 <PRIVATE_KEY>
Authenticate with key
ssh -i <PRIVATE_KEY> <USERNAME>@<TARGET>
Key and custom port
ssh -i <PRIVATE_KEY> -p <PORT> <USERNAME>@<TARGET>
11

Why chmod 600 matters

chmod 600 gives the owner read and write permissions while removing access for group members and other users. OpenSSH may refuse to use a private key if its permissions are too permissive.

Secure private key
chmod 600 id_rsa
12

Private-key passphrases

A private SSH key may itself be protected with a passphrase. Possessing the key file therefore does not always mean it can immediately be used for authentication.

13

authorized_keys

The authorized_keys file contains public keys that are permitted to authenticate to a specific user account. It is normally stored inside the users .ssh directory.

Typical location
~/.ssh/authorized_keys
14

Why authorized_keys matters

Reading authorized_keys can reveal which public keys are trusted by an account. Write access to this file is security-sensitive because it controls which keys are allowed to authenticate.

15

Host keys and fingerprints

SSH host keys identify the server rather than the user. When connecting to a server for the first time, the SSH client normally displays the host-key fingerprint so the user can verify the identity of the server.

Collect SSH host keys
ssh-keyscan <TARGET>
Custom port
ssh-keyscan -p <PORT> <TARGET>
16

known_hosts

Previously accepted SSH server host keys are normally stored in the client known_hosts file. Unexpected changes to a host key can produce an SSH warning.

Typical known_hosts file
~/.ssh/known_hosts
17

User enumeration

Valid SSH usernames may be discovered from other exposed services, web applications, configuration files, operating-system information or previous enumeration. Whether SSH itself reveals valid usernames depends on the implementation and configuration.

18

Hydra SSH authentication testing

Hydra can validate SSH credentials during an authorised credential audit. Repeated attempts may trigger account lockouts, rate limiting, intrusion detection or temporary connection restrictions.

Single username
hydra -l <USERNAME> -P <PASSWORDLIST> ssh://<TARGET>
Username and password lists
hydra -L <USERLIST> -P <PASSWORDLIST> ssh://<TARGET>
19

sshpass

sshpass allows a password to be supplied to SSH non-interactively. It can be convenient in labs and automation, but passwords written directly into commands may be exposed through shell history or process information.

SSH with supplied password
sshpass -p '<PASSWORD>' ssh <USERNAME>@<TARGET>
20

SCP file transfer

SCP uses SSH to transfer files securely between systems. It uses the same authentication mechanisms as SSH.

Upload file
scp <LOCAL_FILE> <USERNAME>@<TARGET>:<REMOTE_PATH>
Download file
scp <USERNAME>@<TARGET>:<REMOTE_FILE> <LOCAL_PATH>
Use private key
scp -i <PRIVATE_KEY> <USERNAME>@<TARGET>:<REMOTE_FILE> .
Custom SSH port
scp -P <PORT> <FILE> <USERNAME>@<TARGET>:<REMOTE_PATH>
21

SSH vs SCP port option

SSH uses lowercase -p to specify a custom port. SCP uses uppercase -P for the same purpose.

SSH
ssh -p 2222 user@<TARGET>
SCP
scp -P 2222 file.txt user@<TARGET>:/tmp/
22

Metasploit SSH version scanner

Metasploit includes auxiliary scanners for SSH service identification and authentication testing.

Start Metasploit
msfconsole
Search SSH modules
search type:auxiliary ssh
SSH version scanner
use auxiliary/scanner/ssh/ssh_version
Set target
set RHOSTS <TARGET>
Run scanner
run
23

Metasploit SSH login

The ssh_login auxiliary module can validate username and password combinations against an SSH service.

Load SSH login scanner
use auxiliary/scanner/ssh/ssh_login
Set target
set RHOSTS <TARGET>
Set username list
set USER_FILE <USERLIST>
Set password list
set PASS_FILE <PASSWORDLIST>
Run scanner
run
24

Metasploit public-key authentication

Metasploit also provides modules capable of validating SSH authentication using private keys.

Search public-key modules
search ssh_login_pubkey
Load public-key scanner
use auxiliary/scanner/ssh/ssh_login_pubkey
Show module options
show options
25

Post-authentication checks

After obtaining authorised SSH access, identify the current account and its privileges before continuing. Valid SSH access does not automatically mean root access.

Current user
whoami
User and groups
id
Hostname
hostname
Allowed sudo commands
sudo -l
Operating system
cat /etc/os-release
26

Important SSH files

Common SSH files include private and public keys, authorized_keys, known_hosts and the system SSH client and server configuration files.

Private key
~/.ssh/id_rsa
Authorized keys
~/.ssh/authorized_keys
Known hosts
~/.ssh/known_hosts
SSH server configuration
/etc/ssh/sshd_config
SSH client configuration
/etc/ssh/ssh_config
27

Important sshd_config settings

The SSH server configuration can reveal the listening port, root-login policy and enabled authentication methods. The effective configuration may also depend on included files and Match blocks.

Relevant directives
Port 22
PermitRootLogin no
PasswordAuthentication yes
PubkeyAuthentication yes
28

Enumeration workflow

Confirm the SSH service, identify the implementation and version, enumerate authentication methods and host keys, collect potential usernames and then test valid credentials or discovered keys when authorised. After authentication, identify the current user, groups, sudo permissions and relevant SSH files.

Initial scan
nmap -sC -sV -p 22 <TARGET>
Authentication methods
nmap -p 22 --script ssh-auth-methods --script-args="ssh.user=<USERNAME>" <TARGET>
Manual login
ssh <USERNAME>@<TARGET>
29

SSH tunneling

SSH also supports local, remote and dynamic port forwarding. Tunneling and pivoting are covered separately because they belong to network pivoting rather than basic SSH enumeration.

30

Quick reference

These are the SSH enumeration and access commands worth remembering.

Version detection
nmap -sV -p 22 <TARGET>
Default enumeration
nmap -sC -sV -p 22 <TARGET>
Algorithms
nmap -p 22 --script ssh2-enum-algos <TARGET>
Host keys
nmap -p 22 --script ssh-hostkey <TARGET>
SSH login
ssh <USERNAME>@<TARGET>
Private-key login
ssh -i <PRIVATE_KEY> <USERNAME>@<TARGET>
Fix key permissions
chmod 600 <PRIVATE_KEY>
Download with SCP
scp <USERNAME>@<TARGET>:<REMOTE_FILE> .
Hydra
hydra -L <USERLIST> -P <PASSWORDLIST> ssh://<TARGET>
Metasploit version
use auxiliary/scanner/ssh/ssh_version
Metasploit login
use auxiliary/scanner/ssh/ssh_login
Metasploit key login
use auxiliary/scanner/ssh/ssh_login_pubkey