Bucket
Bucket
Difficulty: Medium
Classification: Official
Synopsis
Bucket is a medium difficulty Linux machine that features LocalStack which simulates a local AWS
environment. Web application is running on Apache server and the files are hosted on an open
S3 bucket which allows us dropping a malicious PHP file and thus gain a reverse shell. At user's
home directory we can find an unfinished project which utilizes DynamoDB for database.
Enumerating DynamoDB reveals credentials which can be reused to move laterally. An internal
application found to be running as root, which is exploited to gain root access.
Skills Required
Enumeration
Basic Knowledge of Linux
Skills Learned
S3
DynamoDB
PD4ML Exploitation
Enumeration
ports=$(nmap -p- --min-rate=1000 -T4 10.10.10.212 | grep ^[0-9] | cut -d '/' -f
1 | tr '\n' ',' | sed s/,$//)
nmap -p$ports -sV -sC 10.10.10.212
Nmap output reveals that the target server has ports 22 (OpenSSH) and 80 (Apache httpd) open.
Apache
Let's browse to port 80.
Apache service is redirecting us to bucket.htb domain. Let's add below entry to resolve the
domain to the server IP address.
This domain is hosting an advertising platform application but images of the application fail to
load. Lets view the page source.
The images of the application are loading from the adserver folder of a subdomain of
bucket.htb . Lets add this as well to our hosts file.
After refreshing the page, we see that the images are properly loading.
Foothold
We can now browse to the subdomain.
curl http://s3.bucket.htb/ | jq
It returns a JSON object with the status of running. This look like a generic API response and we
can check the response headers.
curl -v http://s3.bucket.htb/ | jq
This reveals some interesting headers. Let's search online for x-amz-version-id header.
S3
We see that this header is related to AWS Simple Storage Service (S3). Its an object storage
offering service which is specifically used for storing static files for a website or to have other
documents.
https://[bucketname].s3.domainname.com
https://s3-[region].domainname.com/[bucketname]
From this knowledge, we now aware that adserver folder path is a bucket name. We can use
aws command line tool to enumerate the files and folders inside a S3 bucket. We are going to
install the tool by issuing the following command.
By default AWS cli tool interacts with s3.amazonaws.com . Since we have another domain hosting
it, we can use --endpoint-url option to point the tool to another domain.
aws --endpoint-url=http://s3.bucket.htb s3 ls
AWS cli tool looks for ~/.aws/credentials file to locate the keys in order to authenticate to the
cloud services. Unfortunately we do not have any valid keys to authenticate with the service. Since
its a custom implementation of AWS services, we can use any random credentials to authenticate.
Let's configure the keys.
We can now list the contents inside S3 bucket.
aws --endpoint-url=http://s3.bucket.htb s3 ls
We find that adserver bucket is present. Let's list all contents inside this bucket.
We also notice that PHP is installed on the Apache server. We can execute the below commands
to upload another PHP file with code that can return a reverse shell.
By checking the folder permissions we notice that it has an Access Control List (ACL) set.
It seems that only roy and root users have permissions to this folder.
It is also possible to access the project folder.
DynamoDB
DynamoDB
It seems that the project is still under development. The code is trying to connect to the
DynamoDB service using an endpoint url. DynamoDB is a NoSQL database service that supports
key-value and document data structures.
mkdir /tmp/f
export HOME=/tmp/f
aws configure
We notice that it redirects to another hostname and to a different port. The port is not open
externally and it also redirects to shell/ . We visit the /shell/ directory.
This hosts a DynamoDB JavaScript Shell. DynamoDB is a NoSQL database service that supports
key-value and document data structures. We can refer το the AWS documentation to learn how to
enumerate information from DynamoDB using JavaScript. It is possible to list the tables using
below JavaScript code.
var params = {
};
dynamodb.listTables(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
We observe that the users table is present and we can view the contents inside this table.
var params = {
TableName: "users"
};
dynamodb.scan(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
We notice also three sets of credentials present in the table. It is worth spraying them on the SSH
service. We issue the below command to bruteforce the SSH service.
hydra -L /usr/share/wordlists/SecLists/Usernames/xato-net-10-million-
usernames.txt -P passwords.txt ssh://10.10.10.212
Using tool hydra we are able to spot valid credentials and then login as user roy to SSH.
Privilege Escalation
It is now possible to access the bucket-app folder.
ls -la /var/www/bucket-app
By exploring the Apache configuration files we found out that this application is running as root
user. Thus it is worth further enumerating the application.
This application is listening locally on port 8000. We can perform a port forward using SSH to
access the application.
<?php
require 'vendor/autoload.php';
use Aws\DynamoDb\DynamoDbClient;
if($_SERVER["REQUEST_METHOD"]==="POST") {
if($_POST["action"]==="get_alerts") {
date_default_timezone_set('America/New_York');
]);
file_put_contents('files/'.$name,$item["data"]);
}
passthru("java -Xmx512m -Djava.awt.headless=true -cp pd4ml_demo.jar
Pd4Cmd file:///var/www/bucket-app/files/$name 800 A4 -out files/result.pdf");
}
}
else
{
?>
The above code connects to DynamoDB service and performs a scan of the alerts table. It then
filters the content based on title . If there's a key that contains Ransomware title then it writes
its data value to a random html file inside files folder. Using the PD4ML utility it converts the
HTML contents to a PDF file. By checking the list of tables, we observe that there's no alerts
table present in the DynamoDB database.
Let's configure the AWS credentials and create the table by issuing the following command.
This creates a table called alerts with two attributes title and data . As application filters the
contents based on Ransomware title, it is possible to insert a record with a sample HTML code.
wget http://localhost:8000/files/result.pdf
We see that the PDF is indeed created with HTML that we provided in the database. Checking the
supported HTML tags for pd4ml reveals that we can also embed an external resource as PDF
attachment using the attachment tag.
<pd4ml:attachment src="http://pd4ml.com/i/logo.png" description="test"
icon="Paperclip"/>
We can now send POST request to generate the new PDF. Let's save the PDF and click on the
attachment.
The attack is successful and we can view the contents of /etc/passwd . It is also possible to list
the contents of directories with this vulnerability. Let's try to view the files inside /root directory.
We copy the SSH private key and by issuing the following commands it is possible to obtain the
root shell.