This Terraform project sets up a Capture The Flag (CTF) environment in AWS, consisting of a VPC, public and private subnets, an Internet Gateway, a Route Table, Security Groups, and EC2 instances.
- AWS CLI
- Terraform CLI (version 0.13+)
-
Clone this repository to your local machine.
-
Install the AWS CLI and configure your AWS credentials. For instructions, refer to the official AWS CLI documentation.
-
Install the Terraform CLI. For instructions, refer to the official Terraform documentation.
-
Create the S3 bucket for storing the Terraform state file:
aws s3api create-bucket --bucket "terraformstate<RandomNumbers>" --region "us-east-1"
Replace
<RandomNumbers>
with a random string of numbers as S3 requires all buckets to be a globally unique bucket name.Remember this bucket will remain after
Terraform Destroy
so you will need to manually remove if it's no longer needed. -
Create the DynamoDB table for state locking:
aws dynamodb create-table \ --table-name terraform-locks \ --attribute-definitions AttributeName=LockID,AttributeType=S \ --key-schema AttributeName=LockID,KeyType=HASH \ --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \ --region us-east-1
Remember this DynamoDB will remain after
Terraform Destroy
so you will need to manually remove if it's no longer needed.
Remember to replace <RandomNumbers>
in both the AWS CLI commands and the main.tf
file.
Current version already has this commented out, to use S3 and DynamoDB you'll need to uncomment the lines listed below.
Additionally, you can store and manage your terraform state locally. You can choose to not create an S3 bucket and DynamoDB table and simply comment out lines 1 - 9 in the main.tf file. Save the changes.
-
Initialize the Terraform working directory:
terraform init
-
Preview the changes to be applied:
terraform plan
-
Apply the changes:
terraform apply
Review the changes and type
yes
when prompted to create the resources. -
To connect to the public instances, use the SSH commands provided in the output:
chmod 400 ctf-key-pair-private.pem ssh -i 'ctf-key-pair-private.pem' ec2-user@<public-instance-dns>
Replace
<public-instance-dns>
with the public DNS of the desired instance. -
When you are done with the environment, destroy the resources to avoid unnecessary costs:
terraform destroy
Review the changes and type
yes
when prompted to destroy the resources.
This Terraform configuration creates an AWS infrastructure for a Capture the Flag (CTF) environment. The infrastructure consists of the following components:
-
VPC: A Virtual Private Cloud (VPC) is created to host the CTF environment. This VPC isolates the environment from other resources in your AWS account.
-
Subnets: Two subnets are created within the VPC:
- A public subnet that hosts instances accessible from the internet.
- A private subnet that hosts instances that are only accessible from the public subnet.
-
Internet Gateway: An Internet Gateway (IGW) is attached to the VPC, allowing internet access for resources within the public subnet.
-
Route Table: A custom route table is created and associated with the public subnet. It has a route that directs all internet-bound traffic to the Internet Gateway.
-
Security Groups: Two security groups are created to manage the ingress traffic to instances in the public and private subnets:
- The public security group allows SSH (port 22), HTTP (port 80), and HTTPS (port 443) traffic from any IP address.
- The private security group allows MySQL (port 3306) traffic from instances within the public security group.
-
EC2 Instances: A total of four Amazon EC2 instances are created:
- Three instances are created in the public subnet for participants to access and attempt to capture the flags.
- One instance is created in the private subnet, acting as a MySQL server. This instance can only be accessed from the instances in the public subnet.
-
Key Pair: An RSA key pair is generated for SSH access to the instances. The public key is imported into AWS, while the private key is saved locally and uploaded to an S3 bucket.
-
User Data: A user data script is provided for the private instance, which sets up the MySQL server, creates an admin user, and configures the database.
-
Tags: All resources are tagged with
CaptureTheFlag=Challenge
for easier identification and management. -
Terraform State: The Terraform state file is stored in an S3 bucket with a specified name pattern, and a DynamoDB table is used for state locking.
This script is designed to deploy a web server on an AWS EC2 instance and plant hidden files in random locations within the Linux file system.
- Updates the system packages using
yum update
. - Installs GCC and Apache HTTP Server (httpd) using
yum install
. - Creates an index.html file with a congratulatory message in the web server's root directory.
- Starts the HTTP server using
systemctl start
. - Enables the HTTP server to start on boot using
systemctl enable
. - Creates a C file (
temp.c
) and compiles it into a binary (netconfig
) using GCC. - Removes the temporary C file using
rm
. - Creates a text file (
CHANGELOG
) and a hidden file (.sysconfig
) with congratulatory messages. - Retrieves a list of root level directories (excluding system directories).
- Selects a random root level directory and a random subdirectory within it.
- Moves the files (
netconfig
,CHANGELOG
, and.sysconfig
) to the selected subdirectory. - Logs the location of each file in the
/var/log/flag_planting.log
file.
Please note that this script requires root permissions to perform the necessary operations and should be run on an EC2 instance with appropriate permissions.
Feel free to modify the script as per your requirements.