PART 3 : EXPLOITING THE DAPP

This article delves into the intricacies of exploiting a common vulnerability in smart contracts, known as a reentrancy attack, using a pirate-themed DApp called “Reentrant Pirate” as our example.

The “Reentrant Pirate” DApp is designed to simulate a treasure hunt game, requiring participants to pay an entry fee of 2 Ether to join. Players can leave the game at any time, albeit with an early exit fee of 1 Ether deducted from their deposit. While the concept appears straightforward, the contract harbors a significant vulnerability that can be exploited to drain its funds.

In this article, we will explore how this reentrancy vulnerability works and demonstrate the steps to exploit it. This example underscores the importance of secure coding practices in the development of smart contracts.


UNDERSTANDING THE CONTRACTS

VULNERABLE CONTRACT : VulnerablePirateAdventure.sol

The VulnerablePirateAdventure.sol contract simulates a pirate-themed adventure game on the Ethereum blockchain. Players can join the adventure by paying an entry fee and leave at any time, albeit with an early exit fee. Here’s a detailed breakdown of its components and functionality:

  • State Variables
    • pirates: A mapping that stores the amount deposited by each player.
    • entryFee: A constant that represents the entry fee to join the adventure (2 Ether).
    • earlyExitFee: A constant that represents the fee for leaving the adventure early (1 Ether).
  • joinAdventure( ) Function
    • Players join the adventure by sending at least 2 Ether.
    • If the player sends more than 2 Ether, the excess amount is refunded.
    • An event LogReceived is emitted to log the transaction.
  • claimTreasure( ) Function:
    • Allows players to withdraw their initial deposit.
    • Checks if the player has any deposit.
    • Sets the player’s deposit to 0 before making the external call to prevent reentrancy attacks.
  • leaveAdventure( ) Function:
    • Allows players to leave the adventure early, deducting an early exit fee of 1 Ether from their deposit.
    • The vulnerable part is that the external call to send Ether back to the user is made before updating the state, allowing a reentrancy attack.
  • getContractBalance( ) Function:
    • Returns the current balance of the contract.
ATTACK CONTRACT : MaliciousContract.sol

The MaliciousContract.sol contract is designed to exploit the reentrancy vulnerability in the VulnerablePirateAdventure.sol contract. Here’s a detailed breakdown of its components and functionality:

  • State Variables:
    • vulnerableContract: An instance of the vulnerable contract.
    • owner: The address of the owner of the malicious contract.
  • Constructor:
    • Initializes the vulnerableContract with the address of the deployed vulnerable contract.
    • Sets the owner of the malicious contract.
  • attack Function:
    • Allows the attacker to send 2 Ether to the vulnerable contract and join the adventure.
    • Immediately calls the leaveAdventure function to exploit the reentrancy vulnerability.
  • receive Function:
    • A fallback function that is triggered when the contract receives Ether.
    • If the vulnerable contract’s balance is sufficient, it repeatedly calls the leaveAdventure function.
    • Otherwise, it transfers the accumulated Ether back to the owner, effectively draining the vulnerable contract’s funds.

STEPS TO EXPLOIT

STEP 1: Confirm PirateContract has balance
  • Go to the Dapp and click on “View Contract Balance” button and see if you have enough ETH in the smart contract, if not just re-enter the Treasure Hunt with different accounts
STEP 2: Downloading and Installing Foundry
  • FOR WINDOWS : Download the exe files from here, and save it in your exploit folder
  • FOR LINUX : Download the binary files from here, and save it in your exploit folder
STEP 3: Testing Foundry tools like Cast.exe and Forge.exe
  • By running the following command check if you can interact with Ganache blockchain with foundry framework
STEP 4: Deploy the Malicious Contract
  • Now, we will deploy the MaliciousContract which will exploit the reentrancy vulnerability.
  • Note : All the tools of foundry should be in a folder with both the contract copies.
  • Confirm deployment and note down the “Deployed to” address
STEP 5: Initial balance check of Smart Contracts
  • We check the balances of the smart contract.
STEP 6: Execute the Attack
  • Next, we will execute the attack by calling the attack function of the malicious contract.
  • Now Observe the Re-entrancy Attack
STEP 7: Final balance check of Smart Contracts
  • Finally, we check the balances of the smart contract again to confirm the exploit.
STEP 8: Calling the Withdraw( ) Function
  • Now we call the withdraw function to transfer all the fund from malicious smart contract to attackers wallet

CONCLUSION

In this tutorial, we’ve walked through the detailed process of exploiting a reentrancy vulnerability in the “Reentrant Pirate” DApp. By deploying and interacting with the vulnerable contract, we demonstrated how an attacker can drain funds using a carefully crafted malicious contract. This practical exercise highlights a critical security flaw that has significant implications for decentralized applications on the Ethereum blockchain.

Reentrancy attacks occur when an external contract call is made before the state of the calling contract is updated. This vulnerability can allow malicious actors to repeatedly call the external contract in a loop, extracting funds with each iteration before the initial contract can finalize its state. As seen in our example, this can lead to substantial financial losses and undermine the trust in decentralized systems.

Leave a Reply

Your email address will not be published. Required fields are marked *