From 7771483f17fa2fce6c498195017f22d2b0b4b38b Mon Sep 17 00:00:00 2001 From: bhushan4work Date: Sun, 11 Oct 2026 14:36:50 +0530 Subject: [PATCH] feat: add challenge 78 with public S3 backup bucket (#982) --- aws/backupchallenge/.terraform-docs.yml | 49 +++++++++ aws/backupchallenge/.terraform.lock.hcl | 27 +++++ aws/backupchallenge/README.md | 79 +++++++++++++ aws/backupchallenge/ed25519_backup_key | 8 ++ aws/backupchallenge/main.tf | 61 ++++++++++ aws/backupchallenge/outputs.tf | 14 +++ aws/backupchallenge/upload-backup-key.sh | 23 ++++ aws/backupchallenge/variables.tf | 31 ++++++ aws/backupchallenge/versions.tf | 10 ++ .../challenges/cloud/Challenge78.java | 66 +++++++++++ src/main/resources/application.properties | 1 + .../resources/explanations/challenge78.adoc | 9 ++ .../explanations/challenge78_hint.adoc | 14 +++ .../explanations/challenge78_reason.adoc | 20 ++++ .../wrong-secrets-configuration.yaml | 13 +++ .../cloud/Challenge78RegistrationTest.java | 50 +++++++++ .../challenges/cloud/Challenge78Test.java | 104 ++++++++++++++++++ ...esControllerWithPresetCloudValuesTest.java | 2 +- 18 files changed, 580 insertions(+), 1 deletion(-) create mode 100644 aws/backupchallenge/.terraform-docs.yml create mode 100644 aws/backupchallenge/.terraform.lock.hcl create mode 100644 aws/backupchallenge/README.md create mode 100644 aws/backupchallenge/ed25519_backup_key create mode 100644 aws/backupchallenge/main.tf create mode 100644 aws/backupchallenge/outputs.tf create mode 100755 aws/backupchallenge/upload-backup-key.sh create mode 100644 aws/backupchallenge/variables.tf create mode 100644 aws/backupchallenge/versions.tf create mode 100644 src/main/java/org/owasp/wrongsecrets/challenges/cloud/Challenge78.java create mode 100644 src/main/resources/explanations/challenge78.adoc create mode 100644 src/main/resources/explanations/challenge78_hint.adoc create mode 100644 src/main/resources/explanations/challenge78_reason.adoc create mode 100644 src/test/java/org/owasp/wrongsecrets/challenges/cloud/Challenge78RegistrationTest.java create mode 100644 src/test/java/org/owasp/wrongsecrets/challenges/cloud/Challenge78Test.java diff --git a/aws/backupchallenge/.terraform-docs.yml b/aws/backupchallenge/.terraform-docs.yml new file mode 100644 index 0000000000..af5b15f746 --- /dev/null +++ b/aws/backupchallenge/.terraform-docs.yml @@ -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: |- + + {{ .Content }} + + +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 diff --git a/aws/backupchallenge/.terraform.lock.hcl b/aws/backupchallenge/.terraform.lock.hcl new file mode 100644 index 0000000000..c1a39a11cd --- /dev/null +++ b/aws/backupchallenge/.terraform.lock.hcl @@ -0,0 +1,27 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/aws" { + version = "6.68.0" + constraints = "~> 6.34" + hashes = [ + "h1:79hHQtjGSCiXBZK+VDiNUdI2SLXbNaHf+CHySvKFnvA=", + "h1:rB2Amjxr32AZWeWsDMQyU61xmW8IvSAUXhv4ogvx9l4=", + "zh:064ed007a33a22d8367db88b18d56a4dd066f07f95b1a4d80ffa2b6ee803f6f0", + "zh:20ede4bb730b55023ad426ed13b1b4b295fac4bcc2baa4fb63d2932aa15d760d", + "zh:490cffbc315ba21f740158a6b4195e7707ec2ee7ed357a47bb90ed18466f01a2", + "zh:7840197f73f53d676373c9aeabccd79c61c6da6e4aa1ed840e57ca46c1894126", + "zh:889b15a5c2f7fb168c04c846e75932a6b2ceb7967c3bfa0a0e24be16cdee7969", + "zh:96ba7624b6f0e0a1fa5477b0838194be919d815d30ce7f97e04d4520cca4c00b", + "zh:9b12af85486a96aedd8d7984b0ff811a4b42e3d88dad1a3fb4c0b580d04fa425", + "zh:9b41c2afe2fec1a1880f9427b3ca611abfe6643b41942556a83cd7daae55acf1", + "zh:a83e9d46de040fa7bc56f7aa53bdeb8d7078bac65d9155b07022fd75723308b8", + "zh:aa5cfdd2faf8a2848c72c13ca88786b857fd397aa713967b8b1f9610f2c76a0b", + "zh:abd449fbf38cf828b8b17bc8540752733dd861160504a45b411e72e1792c758c", + "zh:b2511842cb6b23036c72babbaf12b4984835bb2824f938882330b51c68bdaecd", + "zh:c11e8860c4d611dda93da2e5be27c9bd2599da6ad320f3d12ccc714ffbe6f100", + "zh:e4252ceda8641bfe85ad7710b428f994c080bde6c55043ab47a08015432cebdc", + "zh:f292ad91c97ac7354a04a698f346c2a0c3866d9170ca45dda6a6bfab71180be0", + "zh:f4d293cd0e9562bfa1ba32a1fe5c3652c6a32b951e44081158737d4075696045", + ] +} diff --git a/aws/backupchallenge/README.md b/aws/backupchallenge/README.md new file mode 100644 index 0000000000..358bb1e4ab --- /dev/null +++ b/aws/backupchallenge/README.md @@ -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:///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 +``` + + +## 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 | +|------|-------------|------|---------|:--------:| +| [bucket\_name](#input\_bucket\_name) | Name of the S3 backup bucket which holds the ed25519 private key of challenge 78 | `string` | `"backupchallenge"` | no | +| [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 | +| [object\_key](#input\_object\_key) | Key of the object in the backup bucket which holds the ed25519 private key | `string` | `"backups/id_ed25519"` | no | +| [region](#input\_region) | The AWS region to use | `string` | `"eu-west-1"` | no | +| [tags](#input\_tags) | Tags to apply to the resources | `map(string)` |
{
"Application": "wrongsecrets"
}
| no | + +## Outputs + +| Name | Description | +|------|-------------| +| [backup\_key\_object\_arn](#output\_backup\_key\_object\_arn) | ARN of the object which holds the ed25519 private key | +| [backup\_key\_object\_url](#output\_backup\_key\_object\_url) | Unauthenticated HTTPS URL which serves the ed25519 private key | +| [bucket\_name](#output\_bucket\_name) | Name of the publicly readable backup bucket | + diff --git a/aws/backupchallenge/ed25519_backup_key b/aws/backupchallenge/ed25519_backup_key new file mode 100644 index 0000000000..5ceb0fd2bb --- /dev/null +++ b/aws/backupchallenge/ed25519_backup_key @@ -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----- diff --git a/aws/backupchallenge/main.tf b/aws/backupchallenge/main.tf new file mode 100644 index 0000000000..c21ee512b3 --- /dev/null +++ b/aws/backupchallenge/main.tf @@ -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}" + } + ] + }) +} diff --git a/aws/backupchallenge/outputs.tf b/aws/backupchallenge/outputs.tf new file mode 100644 index 0000000000..cfcb82635a --- /dev/null +++ b/aws/backupchallenge/outputs.tf @@ -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}" +} diff --git a/aws/backupchallenge/upload-backup-key.sh b/aws/backupchallenge/upload-backup-key.sh new file mode 100755 index 0000000000..c7cc4e569f --- /dev/null +++ b/aws/backupchallenge/upload-backup-key.sh @@ -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}" diff --git a/aws/backupchallenge/variables.tf b/aws/backupchallenge/variables.tf new file mode 100644 index 0000000000..b4aca2f437 --- /dev/null +++ b/aws/backupchallenge/variables.tf @@ -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" + } +} diff --git a/aws/backupchallenge/versions.tf b/aws/backupchallenge/versions.tf new file mode 100644 index 0000000000..47438d0f2b --- /dev/null +++ b/aws/backupchallenge/versions.tf @@ -0,0 +1,10 @@ +terraform { + required_version = "~> 1.6, >= 1.6" + + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 6.34" + } + } +} diff --git a/src/main/java/org/owasp/wrongsecrets/challenges/cloud/Challenge78.java b/src/main/java/org/owasp/wrongsecrets/challenges/cloud/Challenge78.java new file mode 100644 index 0000000000..9713f405f2 --- /dev/null +++ b/src/main/java/org/owasp/wrongsecrets/challenges/cloud/Challenge78.java @@ -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. + * + *

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. + * + *

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. + * + *

See issue 982. + */ +@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(" ", ""); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index ce36e24380..e931198711 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -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 diff --git a/src/main/resources/explanations/challenge78.adoc b/src/main/resources/explanations/challenge78.adoc new file mode 100644 index 0000000000..ae629ca276 --- /dev/null +++ b/src/main/resources/explanations/challenge78.adoc @@ -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. diff --git a/src/main/resources/explanations/challenge78_hint.adoc b/src/main/resources/explanations/challenge78_hint.adoc new file mode 100644 index 0000000000..a46e268108 --- /dev/null +++ b/src/main/resources/explanations/challenge78_hint.adoc @@ -0,0 +1,14 @@ +You can solve this challenge by the following steps: + +1. Find the backup bucket. It is named `backupchallenge`, and because S3 bucket names are globally unique your deployment may carry a short suffix (for example the AWS account id), so look for `backupchallenge-` as well. +2. Download the key object. Listing the bucket is deliberately not part of the public exposure, but the object itself can be fetched anonymously: ++ +[source,shell] +---- +curl https://backupchallenge.s3.eu-west-1.amazonaws.com/backups/id_ed25519 +---- ++ +The path style variant works too: `https://s3.eu-west-1.amazonaws.com/backupchallenge/backups/id_ed25519`. +3. Paste the complete contents of the key file into the answer box and submit. Line breaks, tabs and spaces are ignored while checking, so it does not matter when your browser or terminal flattens the pasted key into a single line. + +Not seeing the object? Then the operators used a suffix on the bucket name, so replace `backupchallenge` in the URL with the name the bucket actually got. diff --git a/src/main/resources/explanations/challenge78_reason.adoc b/src/main/resources/explanations/challenge78_reason.adoc new file mode 100644 index 0000000000..eca887c143 --- /dev/null +++ b/src/main/resources/explanations/challenge78_reason.adoc @@ -0,0 +1,20 @@ +*Why a world readable backup bucket is a problem* + +A bucket policy with `Principal: "*"` and `s3:GetObject` turns every object in that bucket into a public web page. The data is then no longer protected by IAM, by the network, or by the fact that "nobody knows it is there": it is protected by nothing at all. + +Why this goes wrong so often: + +- bucket names are a shared namespace. Names are global, predictable and easy to brute force, and public bucket names are collected in wordlists and found by scanners that continuously probe object storage for open buckets. Obscurity is not a control. +- backups are a worst case secret store. A backup of a build machine, a deployment folder or a configuration directory tends to contain exactly the key material you did not bother to remove: ssh keys, cloud credentials, tokens. One readable object can be the full compromise of whatever that key can reach. +- the exposure was meant to be prevented by infrastructure as code, and it silently was not. If the deny policy never made it into the plan, every environment that was applied since then shares the same hole. Drift and unreviewed changes are how "private by default" buckets end up public. +- server side encryption does not help here. The object is decrypted for whoever is allowed to read it, and in this case that is everybody. + +What to do instead: + +- switch on Block Public Access at the account level, so a single bucket policy or ACL cannot make data public by accident. +- keep object ownership at `BucketOwnerEnforced` (ACLs disabled) and never grant object permissions to `Principal: "*"`; access should always be granted to a specific role, user or service principal. +- keep backups out of key material altogether. Keys belong in a secrets manager or a dedicated key store, backups belong in a separate account with restricted access. +- if temporary read access is needed, use short lived pre-signed URLs or a workload role instead of a public policy, and enable CloudTrail data events plus access logging so you can see who fetched the object. +- if a key did end up in a bucket like this, treat it as a compromise: rotate the key, review what it could reach and check the access logs before you delete anything. + +In this exercise the key is a dedicated non-production key, but the bucket and the misconfigured policy are very real. diff --git a/src/main/resources/wrong-secrets-configuration.yaml b/src/main/resources/wrong-secrets-configuration.yaml index 76af63a442..96ac4f5154 100644 --- a/src/main/resources/wrong-secrets-configuration.yaml +++ b/src/main/resources/wrong-secrets-configuration.yaml @@ -1145,3 +1145,16 @@ configurations: category: *ci_cd ctf: enabled: true + + - name: Challenge 78 + short-name: "challenge-78" + sources: + - class-name: "org.owasp.wrongsecrets.challenges.cloud.Challenge78" + explanation: "explanations/challenge78.adoc" + hint: "explanations/challenge78_hint.adoc" + reason: "explanations/challenge78_reason.adoc" + environments: [ *aws ] + difficulty: *normal + category: *terraform + ctf: + enabled: true diff --git a/src/test/java/org/owasp/wrongsecrets/challenges/cloud/Challenge78RegistrationTest.java b/src/test/java/org/owasp/wrongsecrets/challenges/cloud/Challenge78RegistrationTest.java new file mode 100644 index 0000000000..a746e83a0b --- /dev/null +++ b/src/test/java/org/owasp/wrongsecrets/challenges/cloud/Challenge78RegistrationTest.java @@ -0,0 +1,50 @@ +package org.owasp.wrongsecrets.challenges.cloud; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; +import org.owasp.wrongsecrets.Challenges; +import org.owasp.wrongsecrets.definitions.Environment; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class Challenge78RegistrationTest { + + @Autowired private Challenges challenges; + + @Test + void challenge78ShouldBeRegisteredForTheAwsEnvironment() { + var definition = challenges.findByShortName("challenge-78").orElseThrow(); + + assertThat(challenges.getChallenge(definition)).hasSize(1); + assertThat(challenges.getChallenge(definition).getFirst()).isInstanceOf(Challenge78.class); + assertThat(definition.supportedEnvironments()) + .extracting(Environment::name) + .containsExactly("aws"); + assertThat(definition.difficulty().difficulty()).isEqualTo("normal"); + assertThat(definition.category().category()).isEqualTo("Terraform"); + assertThat(definition.ctf().enabled()).isTrue(); + } + + @Test + void challenge78ShouldShipExplanationHintAndReasonWithoutTheAnswer() { + var definition = challenges.findByShortName("challenge-78").orElseThrow(); + var challenge = challenges.getChallenge(definition).getFirst(); + var solution = challenge.spoiler().solution(); + var source = definition.sources().getFirst(); + + var explanation = source.explanation().contents().get(); + var hint = source.hint().contents().get(); + var reason = source.reason().contents().get(); + + assertThat(explanation).isNotBlank(); + assertThat(hint).isNotBlank(); + assertThat(reason).isNotBlank(); + assertThat(explanation).doesNotContain(solution); + assertThat(hint).doesNotContain(solution); + assertThat(reason).doesNotContain(solution); + assertThat(challenge.answerCorrect(solution)).isTrue(); + assertThat(challenge.answerCorrect("not-the-private-key")).isFalse(); + } +} diff --git a/src/test/java/org/owasp/wrongsecrets/challenges/cloud/Challenge78Test.java b/src/test/java/org/owasp/wrongsecrets/challenges/cloud/Challenge78Test.java new file mode 100644 index 0000000000..9aeae34a2a --- /dev/null +++ b/src/test/java/org/owasp/wrongsecrets/challenges/cloud/Challenge78Test.java @@ -0,0 +1,104 @@ +package org.owasp.wrongsecrets.challenges.cloud; + +import static org.assertj.core.api.Assertions.assertThat; + +import ch.qos.logback.classic.Logger; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.AppenderBase; +import java.security.KeyPairGenerator; +import java.security.NoSuchAlgorithmException; +import java.util.ArrayList; +import java.util.Base64; +import java.util.List; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.slf4j.LoggerFactory; + +class Challenge78Test { + + private Logger challengeLogger; + private CapturingAppender appender; + + @BeforeEach + void attachAppender() { + challengeLogger = (Logger) LoggerFactory.getLogger(Challenge78.class); + appender = new CapturingAppender(); + appender.setContext(challengeLogger.getLoggerContext()); + appender.start(); + challengeLogger.addAppender(appender); + } + + @AfterEach + void detachAppender() { + challengeLogger.detachAppender(appender); + appender.stop(); + } + + @Test + void rightAnswerShouldSolveChallenge() throws Exception { + var challenge = new Challenge78(newEd25519PrivateKey()); + var solution = challenge.spoiler().solution(); + + assertThat(challenge.answerCorrect(solution)).isTrue(); + assertThat(challenge.spoiler().solution()).isEqualTo(solution); + assertThat(challenge.answerCorrect("not-the-private-key")).isFalse(); + assertThat(challenge.answerCorrect("")).isFalse(); + assertThat(challenge.answerCorrect(null)).isFalse(); + } + + @Test + void pastedKeyWithoutLineBreaksShouldSolveChallenge() throws Exception { + var challenge = new Challenge78(newEd25519PrivateKey()); + var solution = challenge.spoiler().solution(); + + assertThat(challenge.answerCorrect(solution.replace("\n", "").replace("\r", ""))).isTrue(); + assertThat(challenge.answerCorrect(solution.replace("\n", "\r\n"))).isTrue(); + } + + @Test + void truncatedKeyShouldNotSolveChallenge() throws Exception { + var challenge = new Challenge78(newEd25519PrivateKey()); + var solution = challenge.spoiler().solution(); + + assertThat(challenge.answerCorrect(solution.substring(0, solution.length() / 2))).isFalse(); + } + + @Test + void unconfiguredChallengeShouldReturnAnAnswerButAcceptNoEmptyInput() { + var challenge = new Challenge78(""); + + assertThat(challenge.spoiler().solution()).isNotBlank(); + assertThat(challenge.answerCorrect("")).isFalse(); + assertThat(challenge.answerCorrect(null)).isFalse(); + } + + @Test + void answerShouldNeverBeWrittenToTheLogs() throws Exception { + var challenge = new Challenge78(newEd25519PrivateKey()); + var solution = challenge.spoiler().solution(); + + assertThat(challenge.answerCorrect(solution)).isTrue(); + assertThat(appender.messages).noneMatch(message -> message.contains(solution)); + } + + private static String newEd25519PrivateKey() throws NoSuchAlgorithmException { + var keyPair = KeyPairGenerator.getInstance("Ed25519").generateKeyPair(); + var encoded = Base64.getEncoder().encodeToString(keyPair.getPrivate().getEncoded()); + var pem = new StringBuilder("-----BEGIN PRIVATE KEY-----"); + for (var offset = 0; offset < encoded.length(); offset += 64) { + pem.append('\n').append(encoded, offset, Math.min(offset + 64, encoded.length())); + } + return pem.append('\n').append("-----END PRIVATE KEY-----\n").toString(); + } + + private static final class CapturingAppender extends AppenderBase { + + private final List messages = new ArrayList<>(); + + @Override + protected void append(ILoggingEvent event) { + messages.add(event.getFormattedMessage()); + } + } +} diff --git a/src/test/java/org/owasp/wrongsecrets/challenges/cloud/ChallengesControllerWithPresetCloudValuesTest.java b/src/test/java/org/owasp/wrongsecrets/challenges/cloud/ChallengesControllerWithPresetCloudValuesTest.java index a1149933a4..adbefb960a 100644 --- a/src/test/java/org/owasp/wrongsecrets/challenges/cloud/ChallengesControllerWithPresetCloudValuesTest.java +++ b/src/test/java/org/owasp/wrongsecrets/challenges/cloud/ChallengesControllerWithPresetCloudValuesTest.java @@ -54,7 +54,7 @@ void shouldSpoilExercises() throws Exception { void shouldNotShowDisabledChallengeAnywhere() throws Exception { for (var challenge : challenges.getChallengeDefinitions()) { var shortname = challenge.name().shortName(); - if (shortname.contains("46")) { + if (shortname.contains("46") || shortname.contains("78")) { continue; } mvc.perform(get("/challenge/%s".formatted(challenge.name().shortName())))