DFveloper/cyber-security-bucket / blockchain-security__blockchain-security-realtime-scenarios.json
DFveloper's picture
download
raw
4.36 kB
[
{
"id": "blockchain-001",
"scenario": "A smart contract auditor discovers a reentrancy vulnerability in a DeFi protocol.",
"instruction": "Analyze the following smart contract vulnerability and provide remediation steps.",
"input": "Contract: DeFiBank.sol\nPlatform: Ethereum Mainnet\nTVL: $10,000,000\n\nVulnerable Function:\n```solidity\nfunction withdraw(uint256 amount) public {\n require(balances[msg.sender] >= amount, \"Insufficient balance\");\n \n // Vulnerable: External call before state update\n (bool success, ) = msg.sender.call{value: amount}(\"\");\n require(success, \"Transfer failed\");\n \n // State update after external call\n balances[msg.sender] -= amount;\n}\n```\n\nAttack Transaction:\nFrom: 0xAttacker\nTo: DeFiBank contract\nValue: 1 ETH\n\nReentrancy Attack:\n```solidity\ncontract Attacker {\n DeFiBank public bank;\n uint256 public count = 0;\n \n function attack() external payable {\n bank.deposit{value: 1 ether}();\n bank.withdraw(1 ether);\n }\n \n receive() external payable {\n if (count < 10) {\n count++;\n bank.withdraw(1 ether);\n }\n }\n}\n```\n\nTransaction trace:\n1. Attacker deposits 1 ETH\n2. Attacker calls withdraw(1 ETH)\n3. Contract sends 1 ETH to attacker\n4. Attacker's receive() re-enters withdraw()\n5. Repeats 10 times\n6. Total stolen: 10 ETH (9 ETH profit)\n\nSlither Analysis:\n$ slither DeFiBank.sol\n\nReentrancy in DeFiBank.withdraw (DeFiBank.sol#15-21):\nExternal calls:\n- (bool success) = msg.sender.call{value: amount}(\"\") (DeFiBank.sol#17)\nState variables written after the call(s):\n- balances[msg.sender] -= amount (DeFiBank.sol#20)\n\nReference: https://swcregistry.io/docs/SWC-107",
"output": "## Smart Contract Vulnerability - Reentrancy\n\n### Vulnerability Details\n- **Type**: Reentrancy\n- **SWC**: SWC-107\n- **Impact**: Fund theft\n- **Severity**: CRITICAL\n\n### Vulnerable Code\n```solidity\nfunction withdraw(uint256 amount) public {\n require(balances[msg.sender] >= amount, \"Insufficient balance\");\n \n // VULNERABLE: External call before state update\n (bool success, ) = msg.sender.call{value: amount}(\"\");\n require(success, \"Transfer failed\");\n \n // State update happens AFTER external call\n balances[msg.sender] -= amount;\n}\n```\n\n### Attack Flow\n```\n1. Attacker deposits 1 ETH\n balances[attacker] = 1 ETH\n\n2. Attacker calls withdraw(1 ETH)\n - Balance check: 1 ETH >= 1 ETH ✓\n - External call: Send 1 ETH to attacker\n - Attacker's receive() triggers\n - Re-enters withdraw()\n - Balance check: 1 ETH >= 1 ETH ✓ (not updated yet!)\n - External call: Send 1 ETH again\n - Repeats 10 times\n - Final state update: balances[attacker] = 0\n\n3. Result: Attacker stole 10 ETH with 1 ETH deposit\n```\n\n### Fix\n```solidity\nfunction withdraw(uint256 amount) public {\n require(balances[msg.sender] >= amount, \"Insufficient balance\");\n \n // Update state BEFORE external call\n balances[msg.sender] -= amount;\n \n // External call after state update\n (bool success, ) = msg.sender.call{value: amount}(\"\");\n require(success, \"Transfer failed\");\n}\n```\n\n### Alternative: ReentrancyGuard\n```solidity\nimport \"@openzeppelin/contracts/security/ReentrancyGuard.sol\";\n\ncontract DeFiBank is ReentrancyGuard {\n function withdraw(uint256 amount) public nonReentrant {\n require(balances[msg.sender] >= amount, \"Insufficient balance\");\n balances[msg.sender] -= amount;\n (bool success, ) = msg.sender.call{value: amount}(\"\");\n require(success, \"Transfer failed\");\n }\n}\n```\n\n### Impact\n- **Fund Theft**: 10 ETH stolen ($25,000)\n- **TVL at Risk**: $10,000,000\n- **Reputation**: Protocol trust destroyed\n\n### Prevention\n- Use Checks-Effects-Interactions pattern\n- Implement ReentrancyGuard\n- Use Slither for static analysis\n- Conduct formal verification\n- Professional audit before deployment\n\n### Tools\n- Slither\n- Mythril\n- Echidna\n- Certora",
"metadata": {
"category": "blockchain-security",
"vulnerability": "reentrancy",
"swc": "SWC-107",
"tools": ["Slither", "Mythril", "Echidna"],
"severity": "critical",
"platform": "ethereum"
}
}
]

Xet Storage Details

Size:
4.36 kB
·
Xet hash:
8a6e150e111c0ca73fac8568fd856f8b2d7f10089ea5e4f11d37871300f7e379

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.