A reentrancy attack exploits the vulnerability in smart contracts when a function makes an external call to another contract before updating its own state. This allows the external contract, possibly malicious, to reenter the original function and repeat certain actions, like withdrawals, using the same state. Through such attacks, an attacker can possibly drain all the funds from a contract.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Solidity_Reentrancy {
mapping(address => uint) public balances;
function deposit() external payable {
balances[msg.sender] += msg.value;
}
function withdraw() external {
uint amount = balances[msg.sender];
require(amount > 0, "Insufficient balance");
// Vulnerability: Ether is sent before updating the user's balance, allowing reentrancy.
(bool success, ) = msg.sender.call{value: amount}("");
require(success, "Transfer failed");
// Update balance after sending Ether
balances[msg.sender] = 0;
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Solidity_Reentrancy {
mapping(address => uint) public balances;
function deposit() external payable {
balances[msg.sender] += msg.value;
}
function withdraw() external {
uint amount = balances[msg.sender];
require(amount > 0, "Insufficient balance");
// Fix: Update the user's balance before sending Ether
balances[msg.sender] = 0;
// Then send Ether
(bool success, ) = msg.sender.call{value: amount}("");
require(success, "Transfer failed");
}
}