On this page 30 sections
- What SSH is
- Default port
- Initial enumeration
- SSH banner
- Useful Nmap SSH scripts
- SSH authentication methods
- Password authentication
- Public-key authentication
- Private and public SSH keys
- Using a private SSH key
- Why chmod 600 matters
- Private-key passphrases
- authorized_keys
- Why authorized_keys matters
- Host keys and fingerprints
- known_hosts
- User enumeration
- Hydra SSH authentication testing
- sshpass
- SCP file transfer
- SSH vs SCP port option
- Metasploit SSH version scanner
- Metasploit SSH login
- Metasploit public-key authentication
- Post-authentication checks
- Important SSH files
- Important sshd_config settings
- Enumeration workflow
- SSH tunneling
- Quick reference
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.
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.
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.
nmap -sV -p 22 <TARGET>nmap -sC -sV -p 22 <TARGET>nc -nv <TARGET> 22SSH 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.
nmap -sV -p 22 <TARGET>Useful Nmap SSH scripts
Nmap can enumerate SSH host keys, supported cryptographic algorithms and authentication methods.
nmap -p 22 --script ssh-hostkey <TARGET>nmap -p 22 --script ssh2-enum-algos <TARGET>nmap -p 22 --script ssh-auth-methods --script-args="ssh.user=<USERNAME>" <TARGET>ls /usr/share/nmap/scripts/ssh*SSH authentication methods
SSH supports different authentication mechanisms. The two most important during basic enumeration are password authentication and public-key authentication.
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.
ssh <USERNAME>@<TARGET>ssh -p <PORT> <USERNAME>@<TARGET>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.
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.
~/.ssh/id_rsa~/.ssh/id_ed25519~/.ssh/id_rsa.pubUsing 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.
chmod 600 <PRIVATE_KEY>ssh -i <PRIVATE_KEY> <USERNAME>@<TARGET>ssh -i <PRIVATE_KEY> -p <PORT> <USERNAME>@<TARGET>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.
chmod 600 id_rsaPrivate-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.
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.
~/.ssh/authorized_keysWhy 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.
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.
ssh-keyscan <TARGET>ssh-keyscan -p <PORT> <TARGET>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.
~/.ssh/known_hostsUser 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.
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.
hydra -l <USERNAME> -P <PASSWORDLIST> ssh://<TARGET>hydra -L <USERLIST> -P <PASSWORDLIST> ssh://<TARGET>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.
sshpass -p '<PASSWORD>' ssh <USERNAME>@<TARGET>SCP file transfer
SCP uses SSH to transfer files securely between systems. It uses the same authentication mechanisms as SSH.
scp <LOCAL_FILE> <USERNAME>@<TARGET>:<REMOTE_PATH>scp <USERNAME>@<TARGET>:<REMOTE_FILE> <LOCAL_PATH>scp -i <PRIVATE_KEY> <USERNAME>@<TARGET>:<REMOTE_FILE> .scp -P <PORT> <FILE> <USERNAME>@<TARGET>:<REMOTE_PATH>SSH vs SCP port option
SSH uses lowercase -p to specify a custom port. SCP uses uppercase -P for the same purpose.
ssh -p 2222 user@<TARGET>scp -P 2222 file.txt user@<TARGET>:/tmp/Metasploit SSH version scanner
Metasploit includes auxiliary scanners for SSH service identification and authentication testing.
msfconsolesearch type:auxiliary sshuse auxiliary/scanner/ssh/ssh_versionset RHOSTS <TARGET>runMetasploit SSH login
The ssh_login auxiliary module can validate username and password combinations against an SSH service.
use auxiliary/scanner/ssh/ssh_loginset RHOSTS <TARGET>set USER_FILE <USERLIST>set PASS_FILE <PASSWORDLIST>runMetasploit public-key authentication
Metasploit also provides modules capable of validating SSH authentication using private keys.
search ssh_login_pubkeyuse auxiliary/scanner/ssh/ssh_login_pubkeyshow optionsPost-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.
whoamiidhostnamesudo -lcat /etc/os-releaseImportant SSH files
Common SSH files include private and public keys, authorized_keys, known_hosts and the system SSH client and server configuration files.
~/.ssh/id_rsa~/.ssh/authorized_keys~/.ssh/known_hosts/etc/ssh/sshd_config/etc/ssh/ssh_configImportant 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.
Port 22
PermitRootLogin no
PasswordAuthentication yes
PubkeyAuthentication yesEnumeration 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.
nmap -sC -sV -p 22 <TARGET>nmap -p 22 --script ssh-auth-methods --script-args="ssh.user=<USERNAME>" <TARGET>ssh <USERNAME>@<TARGET>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.
Quick reference
These are the SSH enumeration and access commands worth remembering.
nmap -sV -p 22 <TARGET>nmap -sC -sV -p 22 <TARGET>nmap -p 22 --script ssh2-enum-algos <TARGET>nmap -p 22 --script ssh-hostkey <TARGET>ssh <USERNAME>@<TARGET>ssh -i <PRIVATE_KEY> <USERNAME>@<TARGET>chmod 600 <PRIVATE_KEY>scp <USERNAME>@<TARGET>:<REMOTE_FILE> .hydra -L <USERLIST> -P <PASSWORDLIST> ssh://<TARGET>use auxiliary/scanner/ssh/ssh_versionuse auxiliary/scanner/ssh/ssh_loginuse auxiliary/scanner/ssh/ssh_login_pubkey