Skip to content

Chirag21/Ethernaut-Solutions-using-Foundry-Hardhat

Repository files navigation

Ethernaut Solutions [Foundry - Hardhat]

Never place private keys, secret phrases, or mnemonics in a .env file associated with real funds. Only put private keys in a .env file with ONLY testnet ETH, LINK, or other cryptocurrencies.


The Ethernaut is a Web3/Solidity based wargame inspired by overthewire, played in the Ethereum Virtual Machine. The game acts both as a tool for those interested in learning Ethereum and as a way to catalogue historical hacks as levels. Level contracts are taken from Ethernaut's repo.

You can read the Ethernaut CTF writeups here.

Getting Started

The repo includes Ethernaut solutions built with Hardhat and Foundry. The src/ folder contains the Ethernaut-level contracts. The tests for the exploits are present in the test/ folder. script and scripts folders contain the scripts that can be run on the testnet. These scripts carry out the exploitation. Note that the script/ folder contains scripts for Foundry, written in Solidity, while the scripts/ folder contains scripts for Hardhat, written in Typescript.

Install

git clone https://github.com/Chirag21/Ethernaut-solutions.git
  • Inside the project directory, run following commands:
pnpm install
forge install

Run tests

  • Using Foundry:-

    • forge test -vvvv --match-test [testFunctionName]
      

    eg. forge test -vvvv --match-test testFalloutContract

  • Using Hardhat

    • pnpm testhh [testFilePath]
      

    eg. pnpm testhh test/hardhat/2_fallout.test.ts

    Add --no-compile for fast compilation.


Spin up local node

Get private RPC url from Alchemy. This allows transactions to be broadcasted more quickly than the public RPC url. 

For local node, I prefer using Anvil. Anvil is a local testnet node shipped with Foundry. To start the node

anvil --chain-id 169

To fork the testnet

anvil --chain-id 169 --fork-url [RPCUrl] --fork-block-number [blockNumberToForkFrom]

Copy the Anvil private key and paste it into the .env file against "ANVIL_PRIVATE_KEY."


Run exploit scripts

Before running the exploit on testnet, first simulate it on the local forked node. Fork the testnet on local. Run script on local node

  • Using Foundry

    • forge script [pathOfTheScript] -vvvv --rpc-url localhost
      

    eg. forge script script/1_FallbackScript.sol -vvvv --rpc-url localhost

  • Using Hardhat

    • pnpm run [pathOfTheScript] --no-compile --network anvil
      

    eg. pnpm run scripts/2_fallout_exploit.ts --no-compile --network anvil

Execute the exploit on testnet (Remember to use "PRIVATE_KEY" from .env.)

  • Using Foundry

    • forge script [pathOfTheScript] -vvvv --rpc-url [rpcUrl]
      
    • Add rpc-url from foundry.toml

    eg. forge script [pathOfTheScript] -vvvv --rpc-url mumbai

  • Using Hardhat

    • pnpm run scripts/2_fallout_exploit.ts --network [networkName]
      
    • Add network from hardhat.config.ts

    eg. pnpm run scripts/2_fallout_exploit.ts --network mumbai


How to use the repo:-

  • Get the new instance of the level on the Ethernaut site. Copy the instance address and put it in the .env file. You can get the instance address by typing instance in the dev console on the Ethernaut site. Also, copy the block number of the transaction. We will need the block number for forking the testnet on the local test node.

  • Write the test for the exploit. For reference, look in the test folder. You can directly write the script by skipping the test.

  • Write the script that will run on the testnet. For reference, look in the script folder for Foundry or scripts folder for Hardhat.

  • For testing the script, fork testnet on local. You can use the Hardhat node or Anvil. Simulate the transaction on local node. Remember to use "ANVIL_PRIVATE_KEY" from .env.

  • Once the simulation is successful, run the test on the testnet network. After successful execution, go to the level page on Ethernaut and submit the instance. Remember to use "PRIVATE_KEY" from .env.

Never place private keys, secret phrases, or mnemonics in a .env file associated with real funds. Only put private keys in a .env file with ONLY testnet ETH, LINK, or other cryptocurrencies.