Labs / Cloud Security / Data left in the open: buckets, keys & code

Lab 3 of 3 · Data left in the open: buckets, keys & code

Catch it in the code

A Terraform-style change declares a public bucket and a plaintext password; both are caught, fixed and applied.

Start this lab 5 steps

The idea

Cyrus reviews infrastructure changes at Kilnworth Energy, where the whole estate (every cloud resource the company runs) is built from code: a Terraform-style file that declares each bucket, database and role, and is applied to create the real resources. A change is on his desk this morning.

He reads it the way an attacker would, looking for a rule that opens a bucket to the whole internet and a secret typed straight into the file. Then comes the bad news: this change was already applied. So he confirms the exposure is live, fixes it in the code, applies the fix, and proves the bucket is private.

Two fields decide who gets in
Two fields decide who gets in
A secret goes wherever the file goes
A secret goes wherever the file goes
The bucket is public in the code
The bucket is public in the code
The file names the secret, the store holds it
The file names the secret, the store holds it
An edit is not an apply
An edit is not an apply

Steps

  1. Read the change before it ships

    Kilnworth builds its storage, databases and roles from one file of code, and applies it to create the real resources. A storage bucket is private by default: with nothing else declared, only the account's own callers reach it. What opens a bucket to strangers is a bucket policy, a rule attached to the bucket that names who may do what.

    Two fields in a policy statement decide everything. Principal is who the rule applies to, and a Principal of "*" means everyone, including an anonymous caller with no account at all. Action is what they may do, and s3:GetObject is download an object.

    So a statement that allows s3:GetObject to "*" hands the bucket's contents to the whole internet. The change lives at /opt/cloud/iac/kilnworth.tf.json. Read the bucket policy it declares with jq (a tool that prints one part of a JSON file) and look at those two fields on the reports bucket.

    operator:~# jq .resource.aws_s3_bucket_policy /opt/cloud/iac/kilnworth.tf.json
  2. And a secret in the code

    The same file declares the grid database. Infrastructure code does not stay on one laptop: it is committed to version control, cloned by every engineer, read by the CI system (the automation that applies it), and copied into logs along the way.

    So a secret written straight into the code is handed to everyone and everything that ever touches the repository. A password in a resource block is the clearest case. It sits in the declaration as plain text, travels with every clone, and stays in the repository's history even after someone deletes the line.

    Read the database resource and look at how its password field is set.

    operator:~# jq .resource.aws_db_instance /opt/cloud/iac/kilnworth.tf.json
  3. The exposure is already live

    A review usually happens before a change ships. This one did not: the change was already applied, so the live estate already matches the code. The public bucket policy is in force right now, and the reports bucket has been open the whole time.

    Confirm it the way an outsider would, with no inside access at all. From operator, point the aws CLI at the cloud's API with --endpoint-url (the address the CLI sends requests to; on real AWS you leave it out). Add --no-sign-request, which sends the request with no credentials, as an anonymous stranger on the internet would.

    First list the bucket, then download a report. If the applied policy grants s3:GetObject to everyone, both answer a caller who holds no key.

    operator:~# aws --endpoint-url http://10.0.1.20 s3 ls s3://kilnworth-reports --no-sign-request
    operator:~# aws --endpoint-url http://10.0.1.20 s3 cp s3://kilnworth-reports/grid-availability-2026-q3.txt - --no-sign-request
  4. Fix the code

    Fix it where it came from: the infrastructure file. The bucket is public only because of that one policy statement granting s3:GetObject to "*", so remove the public bucket policy. With no policy, the bucket is private again by default.

    Then deal with the password. Replacing the literal with ${var.grid_db_password} moves it out of this file into a variable. That fix is only real if the variable's value is fetched at deploy time from a secret store (AWS Secrets Manager, SSM Parameter Store, Vault).

    A value pasted into a checked-in .tfvars file is no safer.

    Terraform also writes whatever it resolves into its state file, so the state has to be protected too. And the edit cannot undo the past: the old password was already committed, so it must be rotated (changed).

    Write the corrected file, then read it back to confirm.

    operator:~# jq .resource /opt/cloud/iac/kilnworth.tf.json
  5. Apply it, and prove the bucket is private

    Editing the file changes what the code declares. The live estate keeps running the previously applied version until a deploy reads the new code, so a fix you have not applied protects nothing yet.

    Applying the corrected change makes the live estate match it: the public policy is gone, so the reports bucket becomes private. On a real team that is terraform apply. In this lab the apply step writes the new deployed state straight to the cloud API server, which has the same effect.

    Then run the outsider's listing one more time. The test that counts is the attacker's own command: the bucket must now refuse an anonymous caller with AccessDenied, the answer a private bucket gives any request that carries no credentials.

    operator:~# aws --endpoint-url http://10.0.1.20 s3 ls s3://kilnworth-reports --no-sign-request

Run these steps on real machines

Keep reading

Learn it on real machines.

Every lab is a small network of real Linux machines in your browser. Sign in with Google or email to start.

Start this lab