This is a Foundry's forge-based Ethernaut challenge solution. The code is written using the Forge Standard Library and you can test and validate the attack transactions.
Since each challenge has a different solidity version, instead of deploying the ethernaut contract, implemented it by creating and validating challenge instances directly through each challenge factory contract. The following two challenges have exceptions:
- Alien Codex
Spoiler warning
Since this challenge does not compile in solidity version 0.6 or later, loaded the bytecode compiled in version 0.5 directly into memory and created an instance using the create opcode. - Motorbike
Spoiler warning
The challenge is to verify that the contract instance is self-destructed, which is impossible to verify with a forge test because code deletion occurs after the transaction. Therefore, verified it by checking the balance.
Before we get started, you'll need to install foundry.
curl -L https://foundry.paradigm.xyz | bash
foundryup
Then, run the following commands:
git clone https://github.com/beaverx00/ethernaut-foundry.git
cd ethernaut-foundry
forge install
├── lib
│ ├── forge-std
│ ├── openzeppelin-contracts
│ └── openzeppelin-contracts-solc-0.6
├── src
│ ├── levels
│ │ ├── Reentrance.sol
│ │ └── ReentranceFactory.sol
│ └── attacks
│ └── ReentranceAttack.sol
└── test
└── Reentrance.t.sol
Using the Reentrance challenge as an example, the src
directory contains Reentrance.sol
, the challenge contract, and ReentranceFactory.sol
, which creates challenge instances and validates the results. The test directory contains the Reentrance.t.sol
file, which contains the test code to perform the attack and request validation. Finally, if you need a separate contract deployment for challenge solving, it exists under the attacks
directory.
All test contracts are named <Challenge>Test
(for example, ReentranceTest
) and the test function is named test_<Challenge>()
(e.g. test_Reentrance()
)
If you want to test the Reentrance
, you can do the following:
forge test --match-contract=Reentrance -vvvv
You can check emiited logs and stack traces during test. Further information, see Foundry Book