How Server-Side Request Forgery Can Lead to Full Cloud Compromise – and What You Can Do About It
Server-Side Request Forgery (SSRF) is a powerful exploit that enables attackers to trick a vulnerable server into making arbitrary HTTP requests on their behalf. While some view SSRF as merely a method to force the server into sending HTTP requests to 3rd party addresses, its true potential lies in accessing cloud metadata endpoints—a practice that can lead to the leakage of temporary credentials and, ultimately, a full cloud account takeover. In other words, SSRF, much like Remote Code Execution (RCE), SQL Injection, and password issues, represents one of the “holy grails” in security: once an attacker lands a successful exploit, the impact can be devastating. Not only can a single weakness compromise a customer’s application and infrastructure, but it can also trigger a domino effect by leaking access credentials that expose even more sensitive systems.
In this article, we’ll explore how SSRF can escalate from simple internal network reconnaissance to a complete takeover of cloud environments like AWS and Azure, among others. We’ll explain what metadata endpoints are—using AWS and Azure as prime examples—and discuss why they are both necessary for cloud operations and high-value targets for attackers. Whether you’re new to the concept or looking to deepen your understanding, this discussion will provide a comprehensive overview of SSRF and its far-reaching implications in today’s security landscape.
In this article, we’ll discuss:
Server-Side Request Forgery occurs when an application accepts user input (e.g., a URL) and fetches data from that URL on behalf of the user—without sufficient validation. This behavior lets attackers craft malicious requests to internal services that are normally inaccessible from the outside.
An SSRF goal is to access internal endpoints, such as the AWS or Azure instance metadata service. By querying these endpoints, attackers can obtain temporary access tokens or credentials used by the instance, often leading to further compromise within the cloud environment.
Further Reading:
In modern cloud environments, instances (like AWS EC2, Azure VMs, or GCP VMs) frequently need to perform cloud operations, such as retrieving files from storage buckets or accessing secrets. Rather than embedding long-lived credentials in each instance, cloud providers offer “metadata endpoints” at a fixed address (commonly 169.254.169.254
).
These endpoints allow an instance to retrieve temporary credentials and configurations. This approach reduces the risk of exposing static credentials but also creates a high-value target. If attackers can trick the server into making SSRF requests to these endpoints and return a response, they can steal these temporary credentials and impersonate the instance’s account.
Example Endpoints:
http://169.254.169.254/latest/meta-data/
http://169.254.169.254/metadata/instance?api-version=2021-02-01
(requires the header Metadata:true
)Note: Many other cloud providers also have similar metadata endpoints.
Let’s focus on AWS as an example. Once you obtain credentials via SSRF (e.g., from EC2 instance metadata), you should test what permissions you have—and see if you can escalate to a more privileged role or service. Even if the instance IAM role is “limited,” you might find creative ways to escalate.
Below are some AWS services to investigate after obtaining credentials:
Tip: Always enumerate your target’s cloud resources thoroughly. A seemingly “low-permission” credential could still expose misconfigurations that lead to privilege escalation.
Code Example to Check Permissions and Loot Staff from the Account
This Bash script, designed to run on Kali Linux or Kali WSL, automatically installs the necessary prerequisites. It supports multiple accounts using data from a local .\credentials
file. The script checks which permissions an IAM account has and dumps information from the cloud account. The output is stored in the local folder, timestamped for your convenience. This script has been tested on multiple accounts.
IMDSv2 Access Example
Below is an example Bash script that retrieves AWS instance metadata using IMDSv2. It illustrates how to interact with the IMDSv2 endpoint:
- #!/bin/bash
- # Step 1: Retrieve IMDSv2 Token
- TOKEN=$(curl -s -X PUT -H "X-aws-ec2-metadata-token-ttl-seconds: 21600" \
- http://169.254.169.254/latest/api/token)
- # Step 2: Validate Token Retrieval
- if [ -z "$TOKEN" ]; then
- echo "Error: Unable to retrieve IMDSv2 token. Exiting."
- exit 1
- fi
- # Step 3: Recursive Function to Fetch Metadata
- fetch_metadata() {
- local path=$1
- local metadata=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" \
- "http://169.254.169.254/latest/meta-data/${path}")
- # Process each line of the metadata
- for item in $metadata; do
- if [[ $item == */ ]]; then
- # If the item ends with '/', it's a subpath. Call the function recursively.
- fetch_metadata "${path}${item}"
- else
- # Otherwise, fetch and print the value.
- value=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" \
- "http://169.254.169.254/latest/meta-data/${path}${item}")
- echo "${path}${item}: $value"
- fi
- done
- }
- # Step 4: Start Recursive Fetching
- echo "Recursively fetching all metadata from IMDSv2..."
- fetch_metadata ""
169.254.x.x
and other internal IP ranges at the application layer. If possible, avoid returning the raw content of backend requests to the user.SSRF is more than just a neat trick to force the server into connecting to arbitrary client-provided URLs — it’s a full-fledged risk that can cascade into a complete cloud takeover when metadata endpoints and misconfigurations collide. By understanding how SSRF works, limiting instance permissions, and enforcing best practices, you can significantly reduce the chances of a devastating compromise.
Key Takeaways