✅Fallback
fallback() and receive()
Description
Look carefully at the contract's code below.
You will beat this level if
you claim ownership of the contract
you reduce its balance to 0
Things that might help
How to send ether when interacting with an ABI
How to send ether outside of the ABI
Converting to and from wei/ether units (see
help()
command)Fallback methods
Code Audit
When the contract is pushed to the chain, it sets the creator of this contract to be the owner
and sets the "contribution" of the owner to be 1000 ether. The contract defines a public contribute()
function to let users contribute ethers to the contract. If any user has greater contribution, this user becomes the new owner. The owner of this contract is able to call withdraw()
.
The contract implements a vulnerable receive()
function:
This is a special function that is called automatically when an user sends some ether to a contract without specifying anything in the data
field of the transaction.
If
data
field is non-empty, thenfallback()
function will be called; ifdata
field is empty, thenreceive()
function will be called.
Here we can just send some ether to the contract to warm up contributions[msg.sender]
and then send some ether again to fulfill the require()
statement. After that owner = msg.sender
will be triggered and we can call withdraw()
to steal all the money.
Solution
Contribute 1 wei to the contract:
Send 1 wei to the contract with an empty data
field in order to trigger the receive()
function:
Note that sendTransaction(<json_data>)
is a web.js function, not a function defined inside the contract.
Now we should be the owner of this contract. To verify:
Summary
You know the basics of how ether goes in and out of contracts, including the usage of the fallback method.
You've also learnt about OpenZeppelin's Ownable contract, and how it can be used to restrict the usage of some methods to a privileged address.
Move on to the next level when you're ready!
Last updated