State of Affairs
Challenge Description

This challenge involves exploiting a Terraform environment with restricted permissions to escalate privileges and retrieve the flag.
Table of Contents
Solution Overview
This challenge demonstrates a Terraform state file poisoning attack through a race condition:
Enumeration - Discover Terraform configuration files and cronjob behavior
Provider Analysis - Analyze installed Terraform providers and versions
Race Condition Identification - Find timing window before terraform files are initialized
State File Poisoning - Create malicious terraform.tfstate with code execution payload
Flag Retrieval - Execute command to copy flag with elevated privileges
Key Vulnerability: Race condition in cronjob allows injecting malicious state file that executes arbitrary commands via the statefile-rce provider technique.
Initial Analysis
Upon accessing the environment, we have limited privileges as the ctf user and need to escalate to access the flag.
File System Enumeration

Key findings:
The
ctfuser only has read permissions formain.tf,server.crtand.terraform.lock.hclLimited filesystem access suggests we need to find another attack vector
Terraform lock file is readable and may contain valuable information
Terraform Lock File Analysis
The Terraform lock file reveals the installed providers and their versions:
Provider Analysis:
From the Terraform lock file, we identified three installed providers:
registry.terraform.io/hashicorp/local- version 2.6.1 (latest)registry.terraform.io/hashicorp/time- version 0.9.2 (outdated, current: 0.13.1)registry.terraform.io/hashicorp/tls- version 4.1.0 (latest)
While the time provider is outdated, no known exploits exist for version 0.9.2.
Initial Terraform Commands:
Attempting to run terraform plan or terraform apply returns an error:

The error indicates we don't have sufficient permissions to read the required Terraform configuration files.
Cronjob Discovery
Using pspy to monitor processes, we discovered that supercronic is being used to execute scheduled tasks:

Crontab Contents:

Analysis:
The cronjob runs every minute
Executes
terraform initfollowed byterraform apply -auto-approveRuns as the
tfuserwith elevated privilegesLogs output to
/var/tmp/tfoutput.log
Note: We don't have write permissions to the crontab, so traditional cronjob privilege escalation won't work.
Terraform Output Analysis:
Since the cronjob runs every minute, we can examine the output log to understand what Terraform is doing:
Key Observations:
Terraform is managing TLS certificates and keys
Resources are being replaced every minute due to
replace_triggered_byThe state includes
time_static,tls_private_key,tls_self_signed_cert, andlocal_fileresourcesAll operations run with
tfuserprivileges
Temporary Files Discovery:
Examining the /tmp folder reveals Terraform state files:

A second terraform.tfstate file exists in .terraform/:
Permission Analysis:
All files and directories are owned by
tfuser:tfgroupWe cannot write or modify existing state files
This limits direct state file modification attacks
Identifying the Vulnerability
Race Condition Discovery
A critical behavior was discovered: Terraform files are not instantiated immediately when the environment spawns.
Observation:

When the instance first spawns:
No Terraform files exist in
/tmpNo provider plugins are installed
Files appear approximately 1 minute after spawn
This creates a race condition window where we can inject our own files
State File Code Execution
The race condition enables Terraform state file poisoning, a technique documented in HackTricks:

Attack Technique:
The terraform-provider-statefile-rce project demonstrates how malicious state files can achieve code execution:

When terraform init is executed with a compromised state file, Terraform will attempt to download and initialize providers referenced in the state, including malicious ones that execute arbitrary code during initialization.

Exploit Requirements:
Ability to create/modify a
terraform.tfstatefileterraform initmust be executed (satisfied by the cronjob)Malicious provider reference in the state file
Exploitation
Creating Malicious State File
The exploit payload uses a offensive-actions/statefile-rce provider that executes commands during initialization:
Payload Structure:
Command Explanation:
cp /home/tfuser/flag /tmp/flag- Copy the flag to a readable locationchmod 777 /tmp/flag- Make the flag readable by all users
Complete Malicious State File:
Exploiting the Race Condition
Attack Steps:
Restart the instance to reset the environment
Immediately execute the following command before the cronjob runs
Wait for the cronjob to execute
terraform init
Exploitation Command:
What this command does:
Decodes the base64-encoded malicious state file
Writes it to
/tmp/terraform.tfstateSets permissions to ensure it's readable by the cronjob
Execution:

Monitoring for Success:
Watch the /tmp directory for the flag file to appear:

Getting the Flag
After approximately one minute, the cronjob executes terraform init, which:
Reads our malicious state file
Attempts to initialize the fake
statefile-rceproviderExecutes our command with
tfuserprivilegesCopies the flag to
/tmp/flagwith full permissions
Success!

Summary:
Identified a race condition in Terraform initialization timing
Discovered cronjob running
terraform initandapplyevery minuteCrafted malicious state file using the
statefile-rceprovider techniqueExploited race condition window to inject poisoned state file before legitimate initialization
Achieved code execution as
tfuserto retrieve the flag
Flag: WIZ_CTF{B00tTh3St4t3_Trust_N0_Pr0v1d3r}
Last updated