Reentrancy Attack, One of The Most Destructive Attacks in the Solidity Smart Contract

NEFTURE SECURITY I Blockchain Security
Dissecting Web3
Published in
3 min readJan 31, 2023

$80M were lost by Fei Protocol in 2022 due to a Reentrancy Attack!

This quite unique type of crypto-heist is a battle to the death (not really) between 2 smart contracts ⚔️

Here’s an accessible & quick decrypt of the hack that precipitated Ethereum Hard Fork.

In short & simple, a Reentrancy Attack happens when a hacker builds a malicious smart contract that allows him to repeatedly withdraw ETH from a smart contract, until all the funds get stolen.

Here’s how that can happens.

INTRINSIC VULNERABILITIES

1️⃣ External Calls

A smart contract can communicate with an EXTERNAL existing deployed smart contract by “calling it”, it’s called external calls.

Example: DEX interact with other smart contracts through traded tokens.

The Issue?

  • On Ethereum, transfers of value works as such: users and smart contracts accounts are treated the same, they can both “make a call” to a smart contract
  • A deployed smart contract has an address. And one only needs its functions and public variables to be able to call it (= interact with it)
  • Smart contracts can possibly call “untrusted smart contracts”. These type of smart contract can execute a malicious code that will permit a reentrancy attack?

How so?

By hijacking the control flow

2️⃣ The Check-Interaction-Effects Pattern (CIEP)

Check-Interaction-Effects is a code pattern.

It allows in a case of a request for a withdrawal:

> [CHECK] To verify if the demand of withdrawal is valid

> [INTERACTION] To allow the withdrawal of funds

> [EFFECTS] To update their “internal” state, aka the balance

Issue?

Due to Ethereum’s existing fallback functions, the CIEP is subjected to re-entrancy vulnerabilities.

The CIEP control flow can be hijacked at the INTERACTION stage by a fallback function which will, instead of letting the next stage EFFECT (=the internal update) takes place, basically stop the process on its track and allow the hacker to repeatedly call the withdraw function.

TL;DR: the SC is stuck in a withdraw loop until all funds are siphonned.

The RUNDOWN

To summarize, a hacker will create a malicious smart contract that will call the target’s withdrawal function in the smart contract.

Before the internal update post withdrawal can be performed, the fallback function in the malicious smart contract is activated.

Interrupting the program flow halfway, and repeatedly calling the withdraw function; until nothing is left.

At its core, this hack is pretty simple, right?

Many have been done since Reentracy Attacks first made a big-bang appearance in 2016 with THE DAO $60M hack that precipitated the hard fork that created Ethereum Classic (ETC).

But be it single function, cross-function or cross-contract reentrancy attacks, they keep & will keep plaguing web3 as long as good practices are not implemented in building smart contracts, and audits, that could spot these vulnerabilities, stay missing.

About us

Nefture is a WEB3 Cybersecurity Company that keeps your wallet safe with our Metamask Extension. Register for the beta here!

We also allow brands to tap into web3 through 360° support on their blockchain project:

We specialize in blockchain technologies to make your project come to life and cybersecurity to completely secure your web3 journey: from building Smart Contracts, Audits, Minting websites, Dapps, Discord Audits and Securing,… to Security Breaches Investigation and Management!

Start your web3 journey with us: https://agency.nefture.com/

--

--

NEFTURE SECURITY I Blockchain Security
Dissecting Web3

Nefture secures crypto assets by detecting and mitigating malicious activities and system failures. - nefture.com