Skip to content
RCE Write-up Intermediate

RCE via EC2 IMDSv2 and Full Access to Sensitive S3

A deliberately vulnerable Flask app evaluated user input with eval() and fell back to subprocess for non-math input, allowing shell command execution. The attacker used this to query the EC2 metadata service, obtain IMDSv2 tokens and IAM role credentials, and access a sensitive S3 bucket containing root credentials.

medium.com

Published on medium.com

Before we get our hands dirty, let’s break down a couple of terms to make sure we’re all on the same page.

Revanth S, on medium.com

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

Impact

An attacker could execute commands on the EC2 instance, steal IAM role credentials, read a sensitive S3 bucket, and obtain organisation root credentials, potentially escalating access further.

What the attacker needed

Network access to the vulnerable app; IMDSv2 enabled on the EC2 instance; IAM role attached with S3 permissions

The fix

The app should avoid eval() and subprocess for user input, validate input strictly as arithmetic, and use a safe evaluation environment.

How it was found

  1. Enter a non-math expression such as whoami into the calculator input.
  2. Observe that the server executes it as a shell command.
  3. Request an IMDSv2 token from the metadata service.
  4. Use the token to retrieve the instance ID and IAM role name.
  5. Extract temporary security credentials for the role.
  6. Configure an AWS CLI profile with the credentials and list the sensitive S3 bucket.

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.

The web app let visitors type a maths expression, but it also ran anything else they typed as a command on the server. From there the attacker asked the cloud server for its temporary identity pass, used it to read a private storage bucket, and found even more powerful passwords inside.

What is RCE?

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.

Similar reports

View all

Same bug class and techniques, clearest first.

Remote code execution

The lesson behind this report.

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.