# State Breach

> A four-stage attack on Terraform state and its CI/CD pipeline, and the countermeasure that stops each step.

- Type: Side project by Maximilian Mitterbauer
- Stack: Terraform, Docker, AWS S3, IAM, SSM
- Source: https://github.com/max-vie/state-breach
- Page: https://www.mitmax.at/projects/state-breach/

The text below is quoted from the project page, where Maximilian Mitterbauer writes in the first person.

## 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.

## Attack chain and the defense for each step

- LEAK, 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 (WRITE-ONLY): 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 PR, 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 (MERGE GATE): Stopped at review, not in code: the hardened tree never merged the module, and its apply adds nothing to the exfil log.
- PIVOT, 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 (SCOPED ROLE): 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.
- SABOTAGE, 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 (VERSIONING): The hardened bucket is versioned. The same delete only leaves a marker; removing it brings the state back byte for byte.

## Stack in detail

- 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

Read the source: https://github.com/max-vie/state-breach. The repository is public. It runs against LocalStack in Docker, and every credential in it is fake.
