Skip to content

Blockchain CTF: where to practice smart contract hacking

A blockchain CTF is a Capture The Flag game in which the targets are smart contracts. You get vulnerable code deployed on a chain, and you win by making a contract do something its authors never intended, such as draining a vault or taking ownership. This page explains how these challenges work and lists the places where you can practice today, for free.

What makes a blockchain CTF different

In a classic CTF you look for a flag string hidden on a server. In a blockchain CTF the “flag” is usually a state on chain: a contract balance that reaches zero, an owner variable that holds your address, a boolean that flips to true. Everything you need is public. The source code is given to you, the storage of every contract can be read, and every transaction you send is final. The skill being tested is reading Solidity (and sometimes raw EVM bytecode) carefully enough to find the one assumption that does not hold.

Most challenges target the Ethereum Virtual Machine, so they also apply to EVM chains such as Arbitrum, Optimism or Polygon. A few events include other ecosystems: Paradigm CTF 2022, for instance, had Cairo challenges for StarkNet next to its Solidity ones.

How a challenge works: the Setup contract and isSolved()

Competitive blockchain CTFs such as Paradigm CTF follow a simple pattern. For each player the organizer starts a private chain (often a fork of Ethereum mainnet) and deploys a Setup contract. Its constructor deploys the vulnerable contracts, and it exposes a function called isSolved(). You receive an RPC URL, a funded private key and the Setup address. When isSolved() returns true, the server gives you the flag.

Here is the warm-up challenge from Paradigm CTF 2021, copied from its public repository. The target contract, Hello, has a solve() function that anyone can call, so the warm-up only checks that your tooling works:

pragma solidity 0.8.0;

import "./Hello.sol";

contract Setup {
    Hello public hello;

    constructor() {
        hello = new Hello();
    }

    function isSolved() public view returns (bool) {
        return hello.solved();
    }
}

Real challenges hide the win condition behind a bug. In the 2023 edition the contract is called Challenge rather than Setup, but the idea is the same: its isSolved()function holds the exact condition you have to reach. Read it first, because it tells you what “winning” means before you start looking for the bug.

With Foundry’s cast command-line tool, solving the warm-up looks like this:

# Values the challenge server gives you
export ETH_RPC_URL=<rpc url>
export PK=<your funded private key>
export SETUP=<setup contract address>

# 1. Find the target contract from the Setup contract
HELLO=$(cast call $SETUP "hello()(address)")

# 2. Exploit it (here the "exploit" is a single call)
cast send $HELLO "solve()" --private-key $PK

# 3. Check the win condition before asking for the flag
cast call $SETUP "isSolved()(bool)"   # true

For anything harder than one call you will write an attacker contract or a Foundry script, deploy it to the challenge chain and call it from your key. Always test the full exploit on a local fork first: on the challenge chain a wrong transaction that succeeds can leave the instance in a state you cannot recover from.

Where to practice

These are the resources most players start with. All of them are free and publish their source code.

Ethernaut (OpenZeppelin)

Ethernautis the usual first step. It is a browser game with about 40 levels, from “Hello Ethernaut” and “Fallback” to re-entrancy, delegatecall and storage layout puzzles. Each level gives you your own instance on a public testnet such as Sepolia: you click Get new instance, attack it from the browser console or your own scripts, then click Submit instance. The level contract checks whether you broke it. You need a wallet and a little testnet ETH from a faucet. The source is on GitHub, so you can also run the game against a local node.

Damn Vulnerable DeFi

Damn Vulnerable DeFi moves from single contracts to DeFi protocols: flash loans, price oracles, governance, lending pools, smart contract wallets, timelocks, upgradeable proxies. Version 4 has 18 challenges and runs locally with Foundry; two of them, Puppet V3 and Curvy Puppet, fork mainnet state and need an Ethereum RPC URL. Each challenge comes with a prompt, the contracts and a Foundry test. You write your exploit inside the test function and run forge test. If the test passes, you have solved it. The rules are short: act as the player account and do not change the initial or final conditions. The repository is maintained by The Red Guild.

Paradigm CTF archives

Paradigm ran a competitive CTF in 2021, 2022 and 2023 and published every challenge afterwards: 2021, 2022, 2023. These are much harder than Ethernaut and are the closest you can get to a live competition. Running them locally takes Docker; the 2023 challenges also need Paradigm’s CTF infrastructure repository. If you only want to study them, reading the Setup or Challenge contract and then the public write-ups works well too.

DeFiHackLabs

DeFiHackLabs is not a CTF, but it is the best bridge from puzzles to real incidents. It reproduces hundreds of real DeFi exploits as Foundry tests that fork the chain at a historical block around the attack, so you can run each hack and step through it. Its “Web3 Cybersecurity Academy” folder adds lessons on on-chain transaction debugging and on writing your own proof of concept.

Live competitions on CTFtime

CTFtimelists upcoming CTF events of every kind. Many general CTFs now include a blockchain category next to web, pwn and crypto, so check each event’s description. Playing live, even with a team that solves one blockchain challenge, teaches you how challenge servers, private chains and time pressure actually feel.

Tools you will use

Foundry is the standard toolkit, and both Damn Vulnerable DeFi and DeFiHackLabs are built on it. Install it with curl -L https://foundry.paradigm.xyz | bash followed by foundryup. It ships four tools:

  • forge builds, tests, fuzzes and deploys Solidity. Exploits are usually written as forge tests or scripts.
  • cast talks to a chain from the command line: read storage, call functions, send transactions, decode calldata.
  • anvil is a local Ethereum node. It can fork mainnet or a challenge chain so you can rehearse an exploit safely.
  • chisel is a Solidity REPL, handy for checking how an expression or an overflow behaves.

A sensible order

  1. Learn enough Solidity to read a contract line by line, including how msg.sender, tx.origin, delegatecall and storage slots work.
  2. Finish the Ethernaut levels. When you are stuck, read the contract again before reading a write-up.
  3. Install Foundry and work through Damn Vulnerable DeFi. This is where you learn flash loans and oracle manipulation.
  4. Reproduce a few real incidents from DeFiHackLabs and compare them with the CTF bugs you have already exploited.
  5. Attempt the Paradigm CTF archives, then join a live event from CTFtime.

Only attack contracts that you deployed yourself or that a challenge gives you. The same techniques used against a live protocol are theft. If you find a real bug, report it through the project’s security contact or bug bounty.