Forcefully Send Ether with selfdestruct | Hack Solidity (0.6)

Поделиться
HTML-код
  • Опубликовано: 8 ноя 2024

Комментарии • 45

  • @BarziniNwa
    @BarziniNwa 2 года назад +2

    Reading through “Mastering Ethereum” and watching your videos for further clarification is gold 👌🏼 thank you

  • @Morais479
    @Morais479 2 года назад +2

    Great video! One of the best channels for solidity if not the best!

  • @salem232
    @salem232 4 года назад +5

    😍 thanks for the video!! Very clear!! And breaking the game by wasting 5 Ether ?!!!! Truly self destructive!!! 😬

    • @smartcontractprogrammer
      @smartcontractprogrammer  4 года назад +8

      Actually you can take all of the 7 ether, using a special function called create2. Here is how it works
      create2 is used to deploy a contract at the same address (even contracts that were previously deleted)
      1. deploy Attack contract using create2
      2. selfdestruct to force send Ether. this will delete Attack contract
      3. use create2 to redeploy the Attack contract
      4. Attack is the winner and can claim the 7 ethers

    • @salem232
      @salem232 4 года назад +2

      Smart Contract Programmer 😯 !!! Wow, that sounds like hacking!!! Will you cover create2 later ?

    • @dashemanija
      @dashemanija 3 года назад +3

      Breaking the game was possible with only 0.1 eth, in fact with every amount different from one because sum would never going to be == 7

    • @jagadishk5827
      @jagadishk5827 2 года назад +1

      @@smartcontractprogrammer how is the attack winner in step 4 ?

    • @raviojhayt
      @raviojhayt 2 года назад

      @@smartcontractprogrammer Another way would be to send 4 Ether when calling attack, which makes the Game ther to 6. Then quickly deposit the 1 Ether to the game like a normal player. to become the winner :)

  • @smartcontractprogrammer
    @smartcontractprogrammer  4 года назад +3

    Overview of the exploit 0:42
    Contract vulnerable to the exploit 1:54
    Preventative technique 6:50

    • @MehranHydary
      @MehranHydary 3 года назад +1

      After creating the uint public balance variable - we would have to update the claimReward + getBalance functions too right?

    • @smartcontractprogrammer
      @smartcontractprogrammer  3 года назад +3


      claimReward()
      after Ether is forcefully sent, so address(this).balance >= balance, user will receive more Ether than balance when calling claimReward().
      this does not hurt the winner, so you as a smart contract coder can choose to reward the winner with balance or address(this).balance

  • @福瑞黃
    @福瑞黃 2 года назад

    Great! Very smart! I have question, In 3:35 line 49, you set the address of target as payable, does it means that we can change the address of another contract from non-payable to payable? It seems unreasonable?

  • @broli450
    @broli450 2 года назад +1

    What happen if you sent 3 wei ? The contract will be blocked too because you verifiy that its strictly equal to 7 eth ?

  • @dogcare5376
    @dogcare5376 3 года назад

    Awesome review,so amazing

  • @_chonkov
    @_chonkov 2 года назад +1

    What if the target was 7 ether, 3 ether were already sent, we make a selfdestruct in a contract that had another 3 ether and immediately deposit one more...3 easily stolen ether🤔

  • @neoanderson1865
    @neoanderson1865 2 года назад +1

    I was wondering if you could clarify something for me. Before the attack happens the EtherGame balance is 2 ether, the attack function is called forcefully sending 5 ether, making the balance = 7 ether. That would mean the attacker would be the winner no? The require(balance

    • @smartcontractprogrammer
      @smartcontractprogrammer  2 года назад

      Game requires each deposit to be exactly 1 ETH. Under normal circumstance, 7th depositor will win.
      However this rule can be broken by forcefully sending more than 1 ETH and immediately win the game

    • @ErhanTezcan
      @ErhanTezcan 2 года назад +4

      The attacker's 5 eth was sent forcefully via selfdestruct, not via the deposit function as intended; therefore, it did not go through the if condition that would set the winner.
      I believe if the game is not active enough, you could forcefully send 4 ether and then immediately send the remaining 1 ether to win the game on your demand. The caveat there would be that there is a probability some other player deposited just after your attack has finished. (someone else also suggested this in the comments)
      To be more reliable, you could write an attack with 2 contracts for that: say there is N ether remaining to win the game, and you have contract X with 1 ether and Y with N-1 ether. You write an attack function in X such that it calls an external function in your Y contract which will cause Y to selfdestruct and send its N-1 to the target, and right after that the next line in X attack will be to deposit 1 ether to the target, thereby winning the game.
      The game could also defend against this by only allowing EOA's to play, and disallow contracts (i.e. requiring that msg. sender == tx. origin)

    • @neoanderson1865
      @neoanderson1865 2 года назад +2

      ​@@ErhanTezcan Thank you for taking the time to reply. This makes much more sense now. For some reason I was thinking the eth that was forcefully sent was done through the deposit function instead of selfdestruct

  • @andrewpan3047
    @andrewpan3047 2 года назад

    hi, the question is not how to break the contract but how to retrieve all the ethers after Yves sent all 5 ethers

  • @totalingarc
    @totalingarc 3 года назад

    kool video, thank you

  • @leojamescharles181
    @leojamescharles181 3 года назад +1

    I want to delete the contract itself.
    selfdestruct(0x0);
    But solidity says this does "Invalid implicit conversion"
    can you please tell me how to selfdestruct the contract ?

  • @akif409
    @akif409 4 года назад

    You are the best!

  • @allaboutdata512
    @allaboutdata512 2 года назад

    great vid thanks. Please use dark theme. my eyes are hurting badly after competing the playlist

  • @shashanksingh4708
    @shashanksingh4708 Год назад

    how do i test this using hardhat ?

  • @ongmorel5098
    @ongmorel5098 3 года назад +1

    Nice video but a people has just to directly send 5 ether to the contract without calling a function and the contract will be broken

    • @smartcontractprogrammer
      @smartcontractprogrammer  3 года назад +4

      People cannot directly send Ether because the contract doesn't have a payable fallback function.
      That is why selfdestruct is used to forcefully send ETH and break the contract

    • @ongmorel5098
      @ongmorel5098 3 года назад

      @@smartcontractprogrammer thks!

  • @QuickstickD
    @QuickstickD 3 года назад

    Is the self destruct function how people rug projects?

  • @fahadaslamtanoli
    @fahadaslamtanoli 10 месяцев назад

    can attacker can update the balance variable

  • @cryptonut4498
    @cryptonut4498 3 года назад

    Is the ether stuck in the contract forever? How can someone get it out?

  • @RobloxGamer-g9n
    @RobloxGamer-g9n 4 года назад

    Cool!!!

  • @baddepakaprasad1565
    @baddepakaprasad1565 4 года назад

    Hi..
    when i am trying to execute ethereum smart contracts by using Remix and the warning dispalys like and also code cannot be executed . plz solve this issue.
    Warning: SPDX license identifier not provided in source file. Before publishing, consider adding a comment containing "SPDX-License-Identifier: " to each source file. Use "SPDX-License-Identifier: UNLICENSED" for non-open-source code. Please see spdx.org for more information.

    • @smartcontractprogrammer
      @smartcontractprogrammer  4 года назад +1

      Warning can be fixed if you put this up at the first line
      // SPDX-License-Identifier: MIT
      What error are you getting?