You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: program-analysis/echidna/Exercise-7.md
+14-60Lines changed: 14 additions & 60 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,9 +15,8 @@ Join the team on Slack at: https://empireslacking.herokuapp.com/ #ethereum
15
15
- clone the repo via `git clone https://github.com/tinchoabbate/damn-vulnerable-defi -b v2.0.0`, and
16
16
- install the dependencies via `yarn install`.
17
17
2. To run Echidna on these contracts you must comment out the `dependencyCompiler` section in `hardhat.config.js`. Otherwise, the project will not compile with [`crytic-compile`](https://github.com/crytic/crytic-compile). See the example provided [here](./exercises/exercise7/example.hardhat.config.ts).
18
-
3. For this exercise we will be using Etheno to deploy the `SideEntranceLenderPool` contract. You can find more about Etheno [here](./end-to-end-testing.md).
19
-
4. Analyze the `before` function in `test/side-entrance/side-entrance.challenge.js` to identify what initial setup needs to be done.
20
-
5. Create a contract called `E2E` to be used for the end-to-end testing by Echidna.
18
+
3. Analyze the `before` function in `test/side-entrance/side-entrance.challenge.js` to identify what initial setup needs to be done.
19
+
4. Create a contract to be used for the property testing by Echidna.
21
20
22
21
No skeleton will be provided for this exercise.
23
22
@@ -39,72 +38,27 @@ This solution can be found in [exercises/exercise7/solution.sol](./exercises/exe
The goal of the side entrance challenge is to realize that the contract's accounting of its ETH balance is misconfigured. `balanceBefore` is used to track the balance of the contract before the flash loan BUT `address(this).balance` is used to track the balance of the contract after the flash loan. Thus, you can use the deposit function to repay your flash loan while still maintaining that the contract's total balance of ETH has not changed (i.e. `address(this).balance >= balanceBefore`). Since the ETH that was deposited is now owned by you, you can now also withdraw it and drain all the funds from the contract.
42
-
43
-
We instruct Echidna to do a flashloan. Using the `setEnableWithdraw` and `setEnableDeposit` Echidna will search for function(s) to call inside the flashloan callback to try and break the `testPoolBalance` property.
44
-
45
-
At some point Echidna will identify that if (1) `deposit` is used to pay back the flash loan and (2) `withdraw` is called right after, the `testPoolBalance` property breaks.
46
-
47
-
To use Etheno, you can use an example deployment script like the one below via Hardhat:
pool =awaitSideEntranceLenderPoolFactory.deploy();
63
-
awaitpool.deployed();
64
-
console.log(`pool address ${pool.address}`);
65
41
66
-
awaitthis.pool.deposit({ value:ETHER_IN_POOL });
42
+
In order for Echidna to be able to interact with the `SideEntranceLenderPool`, it has to be deployed first. However, deploying and funding it from the contract to be used by Echidna won't work, as the funding transaction's `msg.sender` is the contract itself. This means that the owner of the funds is the Echidna contract and therefore it can remove the funds by calling `withdraw()`, without the need for the exploit.
67
43
68
-
}
44
+
To prevent that issue, a simple factory contract has to be created to deploy the pool without setting the Echidna property testing contract as the owner of the funds. This factory has a public function that deploys a `SideEntranceLenderPool`, funds it with the given amount, and return its address. Now, since the Echidna testing contract is not the owner of the funds, it cannot call `withdraw()` to empty the pool.
69
45
70
-
main()
71
-
.then(() =>process.exit(0))
72
-
.catch((error) => {
73
-
console.error(error);
74
-
process.exit(1);
75
-
});
76
-
```
77
-
Make sure to add a localhost network to be able to deploy to Etheno. Example for Hardhat:
In another shell run the following hardhat command:
91
-
```shell
92
-
npx hardhat run scripts/deploy.js --network localhost
93
-
```
46
+
Now that the challenge is set up as intended, we proceed to solve it by instructing Echidna to do a flashloan. Using the `setEnableWithdraw` and `setEnableDeposit` Echidna will search for function(s) to call inside the flashloan callback to try and break the `testPoolBalance` property.
94
47
95
-
And then your shell command works fine.
48
+
At some point Echidna will identify that if (1) `deposit` is used to pay back the flash loan and (2) `withdraw` is called right after, the `testPoolBalance` property breaks.
96
49
97
-
Don't forget to copy the initialization JSON file (`init.json`) from Etheno to your fuzzing environment!
0 commit comments