When a web application SSRF causes the cloud to rain credentials & more

tl;dr - This blog post reviews an interesting Server-Side Request Forgery (SSRF) technique against applications that are in cloud environments when combined with overly permissive user accounts. We were able to successfully apply this technique to Amazon Web Services (AWS) hosted web applications. This discovery highlights the importance of assessing AWS configurations and user privileges to mitigate the impacts from such attacks.

This work is born out of both lab based research and real-world use against live applications. The demos provided in this blog post are from lab-based recreations of issues that were originally discovered in production.

Background

An application had a facility for its customers (anybody could register freely) to upload their files. However, these files were stored in Amazon Simple Storage Service (Amazon S3). Our goal was to access other users’ files but it was hard to guess the bucket names and the directories.

Shortly after starting the first penetration test, we found an interesting SSRF vulnerability which allowed us to send HTTP requests to internal or restricted resources and view their responses.

We thought perhaps we could enumerate open ports and proxy our request to other sites using the vulnerable website among others. We reported it immediately to our client and they asked us to see how far we could go while they worked on the fix.

Ability to query internal AWS metadata

As the application was located at Amazon Elastic Compute Cloud (EC2), it was possible to use SSRF to query internal AWS data [1]. This capability was what we needed in order to see if we could locate credentials in order to access Amazon S3 directly.

The following HTTP request shows an example where the user data is retrieved from the Amazon EC2 environment. This is taken from a lab based recreation:

By looking at the downloaded script file containing credentials, you are able to see that this account only needed access to the ‘DescribeTags’ action. This is the ‘describe-tags’ operation in Amazon CLI.

The identified Amazon EC2 account on the server could access a number of resources that were not required for its operation. The account was granted the following EC2 AWS permissions which were excessive for it operation. This list was obtained by looping through the available commands with their right arguments plus the ‘dry-run’ option flag and analysing their response [2].

Finding sensitive data

At this point, we were interested to see if we could find any sensitive data using the “describe-instance-attribute” operation. However, we needed a list of all instance IDs in order to pass them as an argument.

The following command was used to capture the names of all instance-ids:

Obviously, however, obtaining credentials that we already had was not very interesting and we had to go through a long list of base64 texts in order to analyse them all.

We found silver, next we aimed for gold

Decoding and reading the retrieved attributes finally paid off! We managed to obtain some sensitive data including other AWS credentials, database and SMTP credentials and JWT secret key.

While we could already do various things using the data and credentials we had obtained, our goal was to gain access to Amazon S3.

Exploring Amazon S3

Among the captured AWS credentials, we found a user that had access to Amazon S3. By using this account we were able to run the following command and get the list of all S3 buckets that were using it:

> aws s3 ls --profile ourDifferentUser

There was even more sensitive data than we had expected. This was because our target had hosted their log files, application source code, static websites, backup files, as well as customers’ files in their Amazon S3 and our new user could access them all. The sample result for the above command was as follows:

Exploring S3 with a Graphical User Interface (GUI)

In order to make it easier for ourselves, we then used the S3 Browser [3] to access the repositories by using the credentials we had obtained. This meant it was trivial to upload new files or download and modify the existing files.

The impact

We were now in a position (from a web application point of view) to embed an arbitrary JavaScript code within the existing static files and perform actions against web users. However, as the logs and application source code were also accessible, the world was our oyster.

At this point, we had reached our goal and we had little time left! A curious reader might see that even a remote code execution on the web server was not far from our reach, given the fact we had all sorts of credentials, application files and log files.

Our recommendation for the client

SSRF

The destination of the provided URL should be examined on the server-side in order to ensure that its IP address does not point at any internal resources. Any requests that do not meet this requirement should be logged, with a suitable error message being returned to the user.

It is important to note that blacklisting the ‘169.254.169.254’ IP address on the web application is not an appropriate solution, however, it can be used as an indication of an active attack. If the web application only verifies the URL without resolving the IP address, an attacker can bypass it by setting an external domain’s DNS record to ‘169.254.169.254’ or use other IP address formats. Additionally, attackers may redirect an innocent looking URL to this IP address if the application follows the redirections.

Based on our target and its usage, a better approach would be to create a verification mechanism to ensure that the destination website belongs to the user. For example, this can be verified by adding a special text to the DNS entry or a text file that should be uploaded to the root of the destination website.

The request responses should not be displayed (if possible) to reduce risks of data exfiltration.

AWS user with excessive permissions

As user data is not protected by cryptographic methods, it is important to ensure that sensitive data is not stored in it.

If AWS CLI commands need to be executed at boot time then IAM roles should be assigned to each EC2 instance that requires it. Each of these roles will be assigned an IAM policy with permissions set to only allow the necessary commands (i.e. only the ‘describe-tags’ operation in our case).

All users should be examined to ensure that they only have access to the minimum resources needed for their operation.

Apart from fixing these issues, it is highly recommended that AWS log files are reviewed in order to detect any potential breaches, access violations or data tampering that may have occur. This is due to the simplicity and nature of these vulnerabilities.

Lesson learned

The control failure that allowed us to gain access to the target’s Amazon S3 was lack of AWS configuration security review having taken place.

Although SSRF vulnerabilities in this type of situation are important issues, the impact of an exploit could have been minimised if we had been unable to obtain the AWS credentials and if the user had not been given excessive permissions.

How NCC Group can help

NCC Group performs AWS security configuration reviews as part of its assurance services. We have also developed the Scout2 tool which is open source and lets AWS administrators assess their environment's security posture [4]. We are able to deliver a training course on how to carry out AWS security configuration review assessments. If you are interested in understanding more about the course and how we can deliver it for your team, do not hesitate to get in contact. Please contact your account manager or email response@nccgroup.trust or your account manager.