Logging into AWS EC2 instance without a PEM Key (2024)

Logging into AWS EC2 instance without a PEM Key (3)

If you are an AWS administrator then you must have faced the situation where you have lost or misplaced the AWS PEM key.

I also fell in the same situation sometime back for one of my EC2 instances that is managed by one my x colleague and then find a back door or you can say workaround for logging in.

This post is not a hacking post because I am not using any brute force to log in. You should have full control over your account to perform these steps.

Just follow these simple steps:

  1. Create an AWS S3 bucket and place this script under that with the name “sshd_config”. Make sure you have set the correct read permission(ACLs and Bucket policies).
# Package generated configuration file
# See the sshd_config(5) manpage for details
# What ports, IPs and protocols we listen for
Port 22
# Use these options to restrict which interfaces/protocols sshd will bind to
#ListenAddress ::
#ListenAddress 0.0.0.0
Protocol 2
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
#Privilege Separation is turned on for security
UsePrivilegeSeparation yes
# Lifetime and size of ephemeral version 1 server key
KeyRegenerationInterval 3600
ServerKeyBits 1024
# Logging
SyslogFacility AUTH
LogLevel INFO
# Authentication:
LoginGraceTime 120
PermitRootLogin yes
StrictModes yes
RSAAuthentication yes
PubkeyAuthentication yes
#AuthorizedKeysFile %h/.ssh/authorized_keys
# Don't read the user's ~/.rhosts and ~/.shosts files
IgnoreRhosts yes
# For this to work you will also need host keys in /etc/ssh_known_hosts
RhostsRSAAuthentication no
# similar for protocol version 2
HostbasedAuthentication no
# Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication
#IgnoreUserKnownHosts yes
# To enable empty passwords, change to yes (NOT RECOMMENDED)
PermitEmptyPasswords yes
# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication no
# Change to no to disable tunnelled clear text passwords
PasswordAuthentication yes
# Kerberos options
#KerberosAuthentication no
#KerberosGetAFSToken no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes
X11Forwarding yes
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
#UseLogin no
#MaxStartups 10:30:60
#Banner /etc/issue.net
# Allow client to pass locale environment variables
AcceptEnv LANG LC_*
Subsystem sftp /usr/lib/openssh/sftp-server
# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication. Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
UsePAM yes

This script is a simple ssh config script with just a simple change in configuration i.e, Enable password login.

2. Login to AWS console, go to IAM and create an IAM role for EC2 with policy to allow EC2 to perform a set of actions on S3 on your behalf.

3. The golden rule of DevOps, create a backup AMI form your currently running instance for the sake of disaster recovery.

4. Now stop the running instance or from the newly created AMI, create a new instance. Attach the IAM role and pass this script in the instance userdata:

#!/bin/bash
sudo apt-get update -y
sudo apt-get install python -y
sudo curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py"
sudo python get-pip.py - user
sudo apt install awscli -y
sudo aws s3 cp - region us-west-2 s3://login-issue/sshd_config /etc/ssh/sshd_config
sudo chmod 644 /etc/ssh/sshd_config
sudo service ssh restart
sudo echo -e "password\npassword" | passwd ubuntu

This init script is executed during instance startup perform the following actions:

  • Update the packages
  • Install Python
  • Install AWS CLI
  • Copy and replace the sshd_config script from S3 to EC2 instance
  • Change appropriate permission
  • Restart the SSH service
  • And finally, assign the password to the “ubuntu” user

In case you do not have a ubuntu user, change the name with the user name you had in your instance.

Just in case you do not want to modify the existing user, add a new user and put that user in “sudo” group. Make these changes in the script

sudo aws s3 cp - region us-west-2 s3://login-issue/adduser.sh /home/ubuntu/adduser.sh
sudo chmod 777 /home/ubuntu/adduser.sh
sudo sh /home/ubuntu/adduser.sh

Along with that, you need to create one more script and place that in the same S3 bucket with the name “adduser.sh

#!/bin/bash
sudo useradd -m vaibhav
sudo usermod -aG sudo vaibhav
sudo echo -e 'vaibhav\nvaibhav\n' | sudo passwd vaibhav

Once things are at the place, just start the instance or if you are creating the new instance complete the process.

On successful completion you can log in to the instance with the usual ssh login command:

ssh ubuntu@<public IP> or in case of new user ssh vaibhav@<public>

You can also check the instance system logs by clicking on Actions → Instance Settings → Get System log

Cheers! :)

Logging into AWS EC2 instance without a PEM Key (2024)
Top Articles
Latest Posts
Article information

Author: Otha Schamberger

Last Updated:

Views: 6464

Rating: 4.4 / 5 (75 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Otha Schamberger

Birthday: 1999-08-15

Address: Suite 490 606 Hammes Ferry, Carterhaven, IL 62290

Phone: +8557035444877

Job: Forward IT Agent

Hobby: Fishing, Flying, Jewelry making, Digital arts, Sand art, Parkour, tabletop games

Introduction: My name is Otha Schamberger, I am a vast, good, healthy, cheerful, energetic, gorgeous, magnificent person who loves writing and wants to share my knowledge and understanding with you.