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.
Steps
-
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.
Principalis who the rule applies to, and aPrincipalof"*"means everyone, including an anonymous caller with no account at all.Actionis what they may do, ands3:GetObjectis download an object.So a statement that allows
s3:GetObjectto"*"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 withjq(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
-
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
passwordfield is set.operator:~# jq .resource.aws_db_instance /opt/cloud/iac/kilnworth.tf.json
-
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 theawsCLI 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:GetObjectto 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
-
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:GetObjectto"*", 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
.tfvarsfile 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
-
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