Reverse Shell To Your Amazon Aws ec2 Instance As Root Or Administrator By Injecting User Data
The article shows that an attacker with AWS credentials that include the ec2:modify-instance-attribute permission can inject a startup script into an EC2 instance's user-data, which runs as root or Administrator on the next boot, giving a reverse shell. The author demonstrates this on a Linux instance using ngrok and netcat.
Published on infosecwriteups.com
In this article, I will show you how you can use the Amazon AWS ‘aws ec2 modify-instance-attribute’ command to get reverse shell access from your AWS ec2 instance.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely overly broad AWS IAM permissions that allow a user to modify an instance's user-data, combined with user-data executing with high privileges at boot.
Impact
An attacker with the required AWS permissions could gain a root or Administrator shell on the EC2 instance, potentially leading to lateral movement within the AWS account.
What the attacker needed
Valid AWS API credentials; Permissions: ec2:start-instance, ec2:stop-instance…; Target EC2 instance with outbound Internet access
The fix
The article recommends reviewing and restricting IAM permissions, particularly ec2:stop-instance and ec2:modify-instance-attribute, and considering whether outbound Internet access is necessary.
How it was found
- Install and configure ngrok and the AWS CLI on the attacker machine.
- List EC2 instances to find the target instance ID.
- Start an ngrok TCP tunnel and a netcat listener.
- Create a shell script that connects back to the ngrok endpoint.
- Base64-encode the script and stop the target instance.
- Modify the instance's user-data with the encoded script, then start the instance to trigger the reverse shell.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
User-data is a script that an EC2 instance runs automatically when it starts, usually with high privileges. If someone has the right AWS permissions, they can change that script to run their own commands, such as opening a connection back to them. This article shows how that can lead to a remote shell on the server.
What is Misconfiguration?
Settings left unsafe: exposed panels, open storage buckets, debug modes, default credentials.