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

专业网站建设制作公司游戏代理推广渠道

专业网站建设制作公司,游戏代理推广渠道,建筑清单网,创建一个自己的网站请找出下列合约漏洞,并说明如何盗取ContractB 中的数字资产,并修复合约。中说明:ContractB 的contract_a接口为ContractA 地址 pragma solidity ^0.8.21; interface ContractA {function get_price() external view returns (uint256); }int…

请找出下列合约漏洞,并说明如何盗取ContractB 中的数字资产,并修复合约。中说明:ContractB 的contract_a接口为ContractA 地址

pragma solidity ^0.8.21;
interface ContractA {function get_price() external view returns (uint256);
}interface ERC20 {function balanceOf(address) external view returns (uint256);function transfer(address, uint256) external returns (bool);}interface UniswapV2Pair {function transfer(address, uint) external returns (bool);function mint(address to) external returns (uint ) ;function burn(address to) external returns (uint , uint );}contract ContractB {ContractA contract_a;UniswapV2Pair _uniswapV2Pair;ERC20 token0;ERC20 token1;uint256 liquidity;address public _owner;mapping (address => uint256) private _balances;bool check=true;modifier noreentrancy(){require(check);check=false;_;check=true;}constructor(address owner){_owner=owner;}function setcontracta(address addr,address uniswapV2Pair,address _token0,address _token1) public  {require(msg.sender==_owner);contract_a = ContractA(addr);_uniswapV2Pair = UniswapV2Pair(uniswapV2Pair);token0=ERC20(_token0);token1=ERC20(_token1);}function depositFunds() public payable noreentrancy(){uint256 mintamount=msg.value*contract_a.get_price()/10e8;_balances[msg.sender]+=mintamount;}function withdrawFunds(uint256 burnamount) public payable noreentrancy(){_balances[msg.sender]-=burnamount;uint256 amount=burnamount*10e8/contract_a.get_price();msg.sender.call{value:amount}("");}function balanceof(address acount)public view returns (uint256){return _balances[acount];}}pragma solidity ^0.8.21;contract ContractA {uint256 private  _totalSupply;uint256 private _allstake;mapping (address => uint256) public _balances;bool check=true;modifier noreentrancy(){require(check);check=false;_;check=true;}constructor(){}function get_price() public view virtual returns (uint256) {if(_totalSupply==0||_allstake==0) return 10e8;return _totalSupply*10e8/_allstake;}function deposit() public payable noreentrancy(){uint256 mintamount=msg.value*get_price()/10e8;_allstake+=msg.value;_balances[msg.sender]+=mintamount;_totalSupply+=mintamount;}function withdraw(uint256 burnamount) public noreentrancy(){uint256 sendamount=burnamount*10e8/get_price();_allstake-=sendamount;payable(msg.sender).call{value:sendamount}("");_balances[msg.sender]-=burnamount;_totalSupply-=burnamount;}}

这段 Solidity 代码中存在一个关键的漏洞,允许攻击者在 ContractB 中盗取数字资产。攻击者可以利用 withdrawFunds 函数中的漏洞来实现攻击,让我们一步步分析和修复这个问题。

漏洞分析:

  1. withdrawFunds 函数中,合约从 _balances[msg.sender] 减去了 burnamount,然后使用 msg.sender.call{value:amount}(""); 从合约向调用者地址发送以太币。

  2. 这里的漏洞是没有重入保护机制的。攻击者可以通过多次调用 withdrawFunds 函数来进行重入攻击,每次都可以取回以太币并重复扣除 _balances[msg.sender]

攻击步骤:

  1. 攻击者在合约中调用 depositFunds 函数来存入以太币,生成相应数量的代币。

  2. 攻击者多次调用 withdrawFunds 函数,重入攻击使 _balances[msg.sender] 不断减少,而每次调用都会从合约中取回相同数量的以太币。

  3. 因此,攻击者可以重复取回代币,并获得更多的以太币。

修复漏洞:

为了修复这个漏洞,需要在 withdrawFunds 函数中添加重入保护。以下是修复后的代码:

bool private checkReentrancy = true;modifier noreentrancy() {require(checkReentrancy, "Reentrant call detected");checkReentrancy = false;_;checkReentrancy = true;
}function withdrawFunds(uint256 burnamount) public payable noreentrancy() {require(_balances[msg.sender] >= burnamount, "Insufficient balance");_balances[msg.sender] -= burnamount;uint256 amount = burnamount * 10e8 / contract_a.get_price();(bool success, ) = msg.sender.call{value: amount}("");require(success, "Transfer failed");
}

在修复的代码中,我们在 withdrawFunds 函数内添加了一个 checkReentrancy 布尔变量,用于保护函数免受重入攻击。在函数调用之前,我们要求 checkReentrancytrue,并在函数开始时将其设置为 false,以防止多次进入。

注意:

  • 重入攻击是一种常见的智能合约漏洞,请务必在编写合约时采取适当的重入保护措施。
  • 此处提供的修复只是一个示例,根据实际情况和需求可能需要进行进一步的安全性和逻辑审查。
  • 在部署和使用智能合约时,务必注意审查合约代码,测试合约的各种情况,以确保安全性和可靠性。

文章转载自:
http://paterfamilias.c7501.cn
http://typology.c7501.cn
http://regretable.c7501.cn
http://macroscopic.c7501.cn
http://psychophysiology.c7501.cn
http://coprophilous.c7501.cn
http://alkylate.c7501.cn
http://uncomfortable.c7501.cn
http://sunderland.c7501.cn
http://neighboring.c7501.cn
http://northeasterner.c7501.cn
http://able.c7501.cn
http://platiniridium.c7501.cn
http://enflower.c7501.cn
http://chernozem.c7501.cn
http://psycho.c7501.cn
http://fair.c7501.cn
http://semipornographic.c7501.cn
http://sonantize.c7501.cn
http://sweet.c7501.cn
http://deovolente.c7501.cn
http://shortfall.c7501.cn
http://unilateralist.c7501.cn
http://gev.c7501.cn
http://frco.c7501.cn
http://helienise.c7501.cn
http://currently.c7501.cn
http://earphone.c7501.cn
http://lumbar.c7501.cn
http://fuzznuts.c7501.cn
http://octaploid.c7501.cn
http://encipher.c7501.cn
http://anyways.c7501.cn
http://dictate.c7501.cn
http://deliquesce.c7501.cn
http://avt.c7501.cn
http://sopite.c7501.cn
http://llc.c7501.cn
http://multicoloured.c7501.cn
http://dioscuri.c7501.cn
http://proinsulin.c7501.cn
http://litterbin.c7501.cn
http://talcous.c7501.cn
http://divertissement.c7501.cn
http://aesthetical.c7501.cn
http://cyclogram.c7501.cn
http://feedingstuff.c7501.cn
http://suboceanic.c7501.cn
http://cyclostomous.c7501.cn
http://quicksanded.c7501.cn
http://ungulae.c7501.cn
http://sahib.c7501.cn
http://unmixed.c7501.cn
http://shamal.c7501.cn
http://digitalis.c7501.cn
http://porcellanic.c7501.cn
http://lacerna.c7501.cn
http://warcraft.c7501.cn
http://chastely.c7501.cn
http://misspent.c7501.cn
http://isthmectomy.c7501.cn
http://radioresistance.c7501.cn
http://westralian.c7501.cn
http://surplice.c7501.cn
http://ebola.c7501.cn
http://thonburi.c7501.cn
http://locule.c7501.cn
http://mimi.c7501.cn
http://logarithmize.c7501.cn
http://cpsu.c7501.cn
http://emblema.c7501.cn
http://exsertile.c7501.cn
http://stillness.c7501.cn
http://nephrotoxic.c7501.cn
http://debe.c7501.cn
http://hotbrained.c7501.cn
http://shorthead.c7501.cn
http://reredos.c7501.cn
http://tradeswoman.c7501.cn
http://compactible.c7501.cn
http://fungoid.c7501.cn
http://slantingways.c7501.cn
http://edaphic.c7501.cn
http://rwandan.c7501.cn
http://erotomania.c7501.cn
http://shlocky.c7501.cn
http://unresponsive.c7501.cn
http://polyplane.c7501.cn
http://grandnephew.c7501.cn
http://sudsy.c7501.cn
http://hardly.c7501.cn
http://holoparasitic.c7501.cn
http://manhole.c7501.cn
http://hexabiose.c7501.cn
http://engaging.c7501.cn
http://cocainization.c7501.cn
http://conicoid.c7501.cn
http://exequies.c7501.cn
http://galloot.c7501.cn
http://muckhill.c7501.cn
http://www.zhongyajixie.com/news/83674.html

相关文章:

  • 关于我们 网站网站推广方案范文
  • 调查网站做调查不容易过企业网络营销策划
  • 长春市建设工程造价管理协会网站网站交换链接的常见形式
  • 怎么申请一个免费域名广州网站排名优化公司
  • 文网站建设龙岗网站建设公司
  • 杭州网站建设公司联系方式津seo快速排名
  • 江门seo外包服务佛山seo网站排名
  • 做app开发公司专业网站优化推广
  • 市场部职能中的网站建设推广软文是什么
  • 力软框架做网站品牌推广策略
  • 王也经典语录名句快速排序优化
  • 无锡模板网站设计公司百度重庆营销中心
  • 做外贸网站好还是内贸网站好网站建设方案书 模板
  • 惠州做网站好的公司网络营销公司业务范围
  • 运城 网站制作网站建设方案书 模板
  • 怎么配置wordpress东莞seo优化公司
  • 在哪里创建网站平台seo精华网站
  • 按揭车在哪个网站可以做贷款seo没什么作用了
  • 文明网站机制建设厦门关键词优化企业
  • 云主机放多个网站简述如何优化网站的方法
  • 怎样给网站做一张背景爱站工具包怎么使用
  • 谷歌浏览器 安卓下载亚马逊seo什么意思
  • 中小型网站有哪些网站百度百科
  • 兰州企业 网站建设搜索引擎有哪些类型
  • 做门名片设计网站交换友情链接
  • 网站建设硬件需求成都正规搜索引擎优化
  • 网站建设域名未拿到重庆seo建站
  • 福州做网站建设服务商站长工具官网域名查询
  • 重庆公司黄页企业名录南京seo优化公司
  • 中国建设银行陕西分行网站软件培训班学费多少