An attack on Terraform state and its pipeline, stopped at every step
State leak
Long-lived AWS keys sit in a leaked shell history. They open the state bucket, and terraform.tfstate holds the database password and API key in plain text.
Defense: Secrets are passed as write-only arguments (value_wo, OpenTofu 1.11 or later). SSM receives them on apply; the state file never does.
Rogue module
A pull request adds a “usage reporting” module and hands it both secrets. It re-sends them to the attacker’s collector on every apply, and || true keeps the pipeline green.
Defense: Stopped at review, not in code: the hardened tree never merged the module, and its apply adds nothing to the exfil log.
Lateral movement
The deploy role allows Action "*" on Resource "*" and trusts the static CI identity. With the leaked keys the attacker assumes it and reads the production secrets.
Defense: The hardened role reads this stack’s parameters and assets, nothing else. LocalStack does not enforce IAM, so this one is shown by the policy, not by a denied call.
State sabotage
The state bucket has no versioning, so one delete erases the shop’s map. The stack keeps running, but no plan can be built and nothing can be restored.
Defense: The hardened bucket is versioned. The same delete only leaves a marker; removing it brings the state back byte for byte.
What
A lab that breaks a Terraform pipeline, then hardens it. A compromised pipeline deploys a small shop to LocalStack. Four scripted acts take its secrets, pivot into production and erase its state. The hardened variant runs the same chain and stops every act.
Why
I wanted to see the attack, not just read the checklist. State files, CI roles and module sources are easy to overlook. Running the chain end to end shows which setting each step depends on, and which change stops it.
How
Two scenarios, one Makefile. make attack runs the four acts against the vulnerable tree; make defend applies the hardened tree and runs them again. Where LocalStack does not enforce a control, the check proves the defense by content and says so. Gates in scripts/check keep both scenarios honest, and a self-test plants violations to prove the gates fail.
Stack
- Infrastructure
- OpenTofu, S3 backend with a DynamoDB lock table
- AWS services
- S3, DynamoDB, IAM, STS, SSM Parameter Store, EC2
- Emulation
- LocalStack 4.14 community edition, Docker Compose
- Attack tooling
- Bash, AWS CLI, a Python collector on localhost
- Checks
- Gate scripts with a self-test, gitleaks