Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions aws/backupchallenge/.terraform-docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
formatter: "" # this is required

version: ""

header-from: main.tf
footer-from: ""

sections:
hide:
- requirements
- providers
- modules
show: []

hide-all: false # deprecated in v0.13.0, removed in v0.15.0
show-all: true # deprecated in v0.13.0, removed in v0.15.0

content: ""

output:
file: ""
mode: inject
template: |-
<!-- BEGIN_TF_DOCS -->
{{ .Content }}
<!-- END_TF_DOCS -->

output-values:
enabled: false
from: ""

sort:
enabled: true
by: name

settings:
anchor: true
color: true
default: true
description: false
escape: true
hide-empty: false
html: true
indent: 2
lockfile: true
read-comments: true
required: true
sensitive: true
type: true
27 changes: 27 additions & 0 deletions aws/backupchallenge/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

79 changes: 79 additions & 0 deletions aws/backupchallenge/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Backup challenge bucket (challenge 78)

This folder provisions the S3 bucket for challenge 78 of WrongSecrets: a "backup" bucket that is readable by anybody and that holds a dedicated, non-production ed25519 private key.

The public exposure is kept to the minimum the challenge needs:

- anonymous `s3:GetObject` on the single key object only,
- no public `s3:ListBucket`, no public write and no public delete,
- ACLs are disabled through `BucketOwnerEnforced`, while the public policy is attached through `aws_s3_bucket_public_access_block` with `block_public_acls` and `ignore_public_acls` kept enabled.

## Pre-requisites

- Terraform CLI - [Installation](https://developer.hashicorp.com/terraform/install)
- AWS CLI - [Installation](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html), configured with credentials that may create S3 buckets.

## Usage

1. Create the bucket:

```bash
cd aws/backupchallenge
terraform init
terraform apply
```

S3 bucket names are globally unique, while this setup requests the name `backupchallenge`. If that name is already taken in S3, add a suffix (for example your AWS account id):

```bash
terraform apply -var="bucket_suffix=$(aws sts get-caller-identity --query Account --output text)"
```

2. Upload the fixture key with the AWS CLI:

```bash
./upload-backup-key.sh
./upload-backup-key.sh backupchallenge-123456789012
```

The script uploads `ed25519_backup_key`, a dedicated non-production key which only exists for this exercise, to `s3://<bucket>/backups/id_ed25519`. It never grants write or delete access to anybody.

3. Let the application validate against the very same key. The challenge reads it from the `challenge78_backup_key` property, so set that property (for instance through the `CHALLENGE78_BACKUP_KEY` environment variable or a mounted secret) to the contents of `ed25519_backup_key`. The value must never be hardcoded in the java sources.

Please note that the target AWS account must not enforce the account level Block Public Access setting for public bucket policies, otherwise Terraform will refuse to attach the read-only policy.

## Cleanup

```bash
terraform destroy
```

<!-- BEGIN_TF_DOCS -->
## Resources

| Name | Type |
|------|------|
| [aws_s3_bucket.backup](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket) | resource |
| [aws_s3_bucket_ownership_controls.backup](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_ownership_controls) | resource |
| [aws_s3_bucket_policy.public_read_backup_key](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_policy) | resource |
| [aws_s3_bucket_public_access_block.backup](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_public_access_block) | resource |
| [aws_s3_bucket_server_side_encryption_configuration.backup](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_server_side_encryption_configuration) | resource |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_bucket_name"></a> [bucket\_name](#input\_bucket\_name) | Name of the S3 backup bucket which holds the ed25519 private key of challenge 78 | `string` | `"backupchallenge"` | no |
| <a name="input_bucket_suffix"></a> [bucket\_suffix](#input\_bucket\_suffix) | Optional suffix appended to the bucket name, needed when the plain bucket name is already taken as S3 bucket names are globally unique | `string` | `""` | no |
| <a name="input_object_key"></a> [object\_key](#input\_object\_key) | Key of the object in the backup bucket which holds the ed25519 private key | `string` | `"backups/id_ed25519"` | no |
| <a name="input_region"></a> [region](#input\_region) | The AWS region to use | `string` | `"eu-west-1"` | no |
| <a name="input_tags"></a> [tags](#input\_tags) | Tags to apply to the resources | `map(string)` | <pre>{<br/> "Application": "wrongsecrets"<br/>}</pre> | no |

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_backup_key_object_arn"></a> [backup\_key\_object\_arn](#output\_backup\_key\_object\_arn) | ARN of the object which holds the ed25519 private key |
| <a name="output_backup_key_object_url"></a> [backup\_key\_object\_url](#output\_backup\_key\_object\_url) | Unauthenticated HTTPS URL which serves the ed25519 private key |
| <a name="output_bucket_name"></a> [bucket\_name](#output\_bucket\_name) | Name of the publicly readable backup bucket |
<!-- END_TF_DOCS -->
8 changes: 8 additions & 0 deletions aws/backupchallenge/ed25519_backup_key
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
QyNTUxOQAAACAZ9gkVL6FP7qrQWJ7WrvzXq/kyzO3E/jee+u6VlnAbtgAAALi5qk7UuapO
1AAAAAtzc2gtZWQyNTUxOQAAACAZ9gkVL6FP7qrQWJ7WrvzXq/kyzO3E/jee+u6VlnAbtg
AAAEAAHamenpbsC/dbPA8t6CdFffza8GXKCQ+yrWnN+ynFdRn2CRUvoU/uqtBYntau/Ner
+TLM7cT+N5767pWWcBu2AAAAMHdyb25nc2VjcmV0cy1jaGFsbGVuZ2UtNzUtbm9uLXByb2
R1Y3Rpb24tZml4dHVyZQECAwQF
-----END OPENSSH PRIVATE KEY-----
61 changes: 61 additions & 0 deletions aws/backupchallenge/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
provider "aws" {
region = var.region

default_tags {
tags = var.tags
}
}

locals {
bucket_name = var.bucket_suffix == "" ? var.bucket_name : "${var.bucket_name}-${var.bucket_suffix}"
}

resource "aws_s3_bucket" "backup" {
bucket = local.bucket_name
force_destroy = true
}

resource "aws_s3_bucket_ownership_controls" "backup" {
bucket = aws_s3_bucket.backup.id

rule {
object_ownership = "BucketOwnerEnforced"
}
}

resource "aws_s3_bucket_server_side_encryption_configuration" "backup" {
bucket = aws_s3_bucket.backup.id

rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}

resource "aws_s3_bucket_public_access_block" "backup" {
bucket = aws_s3_bucket.backup.id
block_public_acls = true
ignore_public_acls = true
block_public_policy = false
restrict_public_buckets = false
}

resource "aws_s3_bucket_policy" "public_read_backup_key" {
bucket = aws_s3_bucket.backup.id

depends_on = [aws_s3_bucket_public_access_block.backup]

policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Sid = "AllowAnonymousReadOfBackupKey"
Effect = "Allow"
Principal = "*"
Action = "s3:GetObject"
Resource = "${aws_s3_bucket.backup.arn}/${var.object_key}"
}
]
})
}
14 changes: 14 additions & 0 deletions aws/backupchallenge/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
output "bucket_name" {
description = "Name of the publicly readable backup bucket"
value = aws_s3_bucket.backup.id
}

output "backup_key_object_arn" {
description = "ARN of the object which holds the ed25519 private key"
value = "${aws_s3_bucket.backup.arn}/${var.object_key}"
}

output "backup_key_object_url" {
description = "Unauthenticated HTTPS URL which serves the ed25519 private key"
value = "https://${aws_s3_bucket.backup.bucket}.s3.${var.region}.amazonaws.com/${var.object_key}"
}
23 changes: 23 additions & 0 deletions aws/backupchallenge/upload-backup-key.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash
set -euo pipefail

BUCKET_NAME="${1:-backupchallenge}"
OBJECT_KEY="${2:-backups/id_ed25519}"
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
KEY_FILE="${SCRIPT_DIR}/ed25519_backup_key"

if [[ ! -f "${KEY_FILE}" ]]; then
echo "Missing fixture: ${KEY_FILE}" >&2
exit 1
fi

aws s3 cp --only-show-errors "${KEY_FILE}" "s3://${BUCKET_NAME}/${OBJECT_KEY}"

ETAG="$(aws s3api head-object \
--bucket "${BUCKET_NAME}" \
--key "${OBJECT_KEY}" \
--query ETag \
--output text)"

echo "Uploaded ${KEY_FILE##*/} to s3://${BUCKET_NAME}/${OBJECT_KEY} (ETag: ${ETAG})"
echo "Participants can fetch it without credentials via https://${BUCKET_NAME}.s3.amazonaws.com/${OBJECT_KEY}"
31 changes: 31 additions & 0 deletions aws/backupchallenge/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
variable "region" {
description = "The AWS region to use"
type = string
default = "eu-west-1"
}

variable "bucket_name" {
description = "Name of the S3 backup bucket which holds the ed25519 private key of challenge 78"
type = string
default = "backupchallenge"
}

variable "bucket_suffix" {
description = "Optional suffix appended to the bucket name, needed when the plain bucket name is already taken as S3 bucket names are globally unique"
type = string
default = ""
}

variable "object_key" {
description = "Key of the object in the backup bucket which holds the ed25519 private key"
type = string
default = "backups/id_ed25519"
}

variable "tags" {
description = "Tags to apply to the resources"
type = map(string)
default = {
"Application" = "wrongsecrets"
}
}
10 changes: 10 additions & 0 deletions aws/backupchallenge/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
terraform {
required_version = "~> 1.6, >= 1.6"

required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 6.34"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.owasp.wrongsecrets.challenges.cloud;

import com.google.common.base.Strings;
import org.owasp.wrongsecrets.challenges.Challenge;
import org.owasp.wrongsecrets.challenges.Spoiler;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
* Cloud challenge in which the participant has to retrieve an ed25519 private key from a
* deliberately public S3 backup bucket.
*
* <p>The application never holds the key as a literal in the sources: the expected value is
* injected through the {@code challenge78_backup_key} property, which the deployment fills with the
* contents of the very same fixture that is uploaded to the bucket. The key itself is only ever
* handed out by the bucket, never by this application.
*
* <p>Because a multi line key loses its line breaks while being pasted into the answer box, the
* comparison ignores line breaks, tabs and spaces on both sides.
*
* <p>See <a href="https://github.com/OWASP/wrongsecrets/issues/982">issue 982</a>.
*/
@Component
public class Challenge78 implements Challenge {

private static final String UNCONFIGURED_VALUE = "if_you_see_this_please_use_AWS_Setup";

private final String configuredKey;
private String expectedKey;

/**
* Cloud challenge in which the participant has to retrieve an ed25519 private key from a
* deliberately public S3 backup bucket.
*
* @param configuredKey the key which was uploaded to the backup bucket, injected by the
* deployment so that this class never contains the answer itself
*/
public Challenge78(@Value("${challenge78_backup_key}") String configuredKey) {
this.configuredKey = configuredKey;
}

@Override
public Spoiler spoiler() {
return new Spoiler(getExpectedKey());
}

@Override
public boolean answerCorrect(String answer) {
if (Strings.isNullOrEmpty(answer)) {
return false;
}
return withoutWhitespace(answer).equals(withoutWhitespace(getExpectedKey()));
}

private String getExpectedKey() {
if (expectedKey == null) {
expectedKey =
Strings.isNullOrEmpty(configuredKey) ? UNCONFIGURED_VALUE : configuredKey.strip();
}
return expectedKey;
}

private String withoutWhitespace(String value) {
return value.replace("\r", "").replace("\n", "").replace("\t", "").replace(" ", "");
}
}
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ challenge_acht_ctf_to_provide_to_host_value=not_set
challenge_acht_ctf_host_value=not_set
challenge_sixty_seven_ctf_to_provide_to_host_value=not_set
challenge67_cloud_log_secret=not_set
challenge78_backup_key=if_you_see_this_please_use_AWS_Setup
CTF_SERVER_ADDRESS=not_set
reason_enabled=true
plainText13=This is not the secret
Expand Down
9 changes: 9 additions & 0 deletions src/main/resources/explanations/challenge78.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
=== The backup bucket that forgot to lock its door

Teams regularly copy "backups" to object storage: configuration archives, database dumps, and the key material that happened to be lying around on a build machine. For this exercise a small backup set was copied to an S3 bucket of the account you are running in.

That bucket is readable by anybody. Listing the bucket is blocked, so you cannot simply browse its contents, but everybody who knows (or guesses) where the objects live can download them without any credentials at all.

One of the objects in there is an ed25519 private key. It is a dedicated, non-production key which was created for this exercise only, but it is a real key and it is exactly the kind of material you never want to find in a backup.

Find the bucket, download the key and paste the contents of the key file into the answer box below.
Loading
Loading