Skip to content

Omed0/Foundry-CTF-Template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ› οΈ Foundry CTF Template

A minimal and reusable Foundry project template for Ethereum CTF challenges following the Setup + Target pattern.

This template includes sample contracts, a Setup.sol wrapper, an optional Attacker.sol helper, and a Solidity script Solve.s.sol to automate the exploit process.


πŸ“ Project Layout

.
β”œβ”€β”€ .env
β”œβ”€β”€ .gitignore
β”œβ”€β”€ docker-compose.yml
β”œβ”€β”€ entrypoint.sh
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ foundry.toml
β”œβ”€β”€ remappings.txt
β”œβ”€β”€ README.md
β”œβ”€β”€ src
β”‚   β”œβ”€β”€ Attacker.sol
β”‚   β”œβ”€β”€ Creature.sol
β”‚   └── Setup.sol
└── script
    └── Solve.s.sol

πŸ“ Environment Setup

.env – Configuration

PRIVATE_KEY=0x…      # Your EOA private key
SETUP_ADDRESS=0x…    # Deployed Setup contract address
CHAIN_ID=31337       # Chain ID of the network
RPC_URL=https://…    # RPC endpoint for the chain
FOUNDRY_DISABLE_NIGHTLY_WARNING=true

Load it with:

source .env        # Linux/macOS
.\.env             # Windows PowerShell

.gitignore

out/
cache/
lib/
.env

βš’οΈ Foundry Configuration

foundry.toml

[profile.default]
solc_version = "0.8.15"
src = "src"
out = "out"
lib = "lib"
remappings = ["forge-std/=lib/forge-std/src/"]

remappings.txt

forge-std/=lib/forge-std/src/

πŸš€ Getting Started (Without Docker)

  1. Clone the template
git clone https://github.com/you/ctf-foundry-template.git
cd ctf-foundry-template
  1. Install Foundry
curl -L https://foundry.paradigm.xyz | bash
foundryup
  1. Install Dependencies
forge install foundry-rs/forge-std
  1. Setup Environment
cp .env.example .env
# Then edit your values inside .env
  1. Compile Contracts
source .env
forge clean
forge build
  1. Run the Exploit
forge script script/Solve.s.sol:Solve \
  --rpc-url $RPC_URL \
  --private-key $PRIVATE_KEY \
 # --chain-id $CHAIN_ID \
  --broadcast \
  -vvvv
  1. Fetch the Flag

: in HacktheBox sometime after solving challenge u can get a flag like this

curl http://<CTF-HOST>/flag

: Docker Ways is complex i recommend use the above style


🐳 Docker Support

πŸ“¦ Dockerfile

# 1. Base + Prereqs
FROM ghcr.io/foundry-rs/foundry:latest AS builder
USER root
WORKDIR /app

COPY foundry.toml remappings.txt ./
RUN mkdir -p lib && \
    git clone --depth 1 https://github.com/foundry-rs/forge-std lib/forge-std

COPY . .
RUN forge build --optimize

# 2. Runtime
FROM ghcr.io/foundry-rs/foundry:latest
WORKDIR /ctf

COPY --from=builder /app/lib ./lib
COPY --from=builder /app/out ./out
COPY --from=builder /app/src ./src
COPY --from=builder /app/script ./script
COPY entrypoint.sh ./

USER root
RUN chmod +x entrypoint.sh
USER forge

ENTRYPOINT ["./entrypoint.sh"]

entrypoint.sh

#!/usr/bin/env bash
set -e

if [ "$1" = "shell" ]; then
    echo "πŸ› οΈ  Starting interactive shell..."
    exec bash
fi

echo "πŸš€ Running solver script..."
forge script script/Solve.s.sol:Solve \
    --rpc-url "$RPC_URL" \
    --private-key "$PRIVATE_KEY" \
    --chain-id "$CHAIN_ID" \
    --broadcast \
    -vvvv

echo "βœ… Script execution completed successfully."

Make it executable:

chmod +x entrypoint.sh

Build and Run with Docker

# Build image
docker build -t ctf-template .

# Run solver script
docker run --rm -e RPC_URL=$RPC_URL \
                 -e PRIVATE_KEY=$PRIVATE_KEY \
                 -e CHAIN_ID=$CHAIN_ID \
                 ctf-template

# Start a shell
docker run --rm -it -e RPC_URL=$RPC_URL \
                      -e PRIVATE_KEY=$PRIVATE_KEY \
                      -e CHAIN_ID=$CHAIN_ID \
                      ctf-template shell

πŸ‹ Docker Compose

docker-compose.yml

version: "3.8"
services:
  ctf-template:
    build: .
    env_file:
      - .env
    volumes:
      - ./:/ctf
    command: ["${COMMAND:-}"]

Usage

# Build image
docker-compose build

# Run solver
docker-compose up --abort-on-container-exit

# Start a shell
COMMAND=shell docker-compose run --rm ctf-template

πŸ”§ Modifying the Template for a New CTF

  1. Replace Creature.sol with your vulnerable target.
  2. Update Setup.sol with the correct logic and deployed target.
  3. Modify Solve.s.sol to reflect the new logic.
  4. Optionally add helpers like Attacker.sol.
  5. Use .env to plug in dynamic values.

πŸ“š References

This README generated by AI


🧠 Tip: Fork this template for each challenge. Keep it clean. Hack smart.

About

Foundry CTF Template - for CTF er challenge template u can reuse it

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors