Solodit 🟪 Profile picture
Nov 30, 2023 8 tweets 4 min read Read on X
Ethereum’s only hard fork happened because of a reentrancy attack.

When the first DAO was launched in 2016, it held 14% of the entire Ethereum value.

Today, 4 out of 20+ hacks in 2023 have been reentrancy attacks.

What are they and how to prevent them? 👇 How do re-entrancy attacks happen?
1/ What is a reentrancy attack?

When a hacker "reenters" a function to do something malicious.

A hacker typically exploits a callback function (like a “fallback” function or “onERC721Received’) to maliciously "reenter" the victim’s contract and execute a malicious transaction.
2/ How do these work?

A user and a malicious contract both deposit funds into a contract.

The malicious contract calls on the victim’s withdraw function and triggers a fallback function.

This fallback function then allows the attacker to continuously drain the victim’s assets. How do reentrancy attacks work?
3/ An example:

1. "Bank" contract holds 10 ETH & updates state every 24h
2. Hacker opens an account, deposits 1 ETH
3. Hacker withdraws 1 ETH hourly
4. Because accounting is only updated after 24h, hacker is able to do this 24 times
5. Hacker drains bank treasury
Bank contract
Attacker contract
4/ 3 ways to prevent them:

a) Reentrancy Guards

Using a reentrancy guard ensures the attacker can’t make more than one function run at a time.

OpenZeppelin’s ReentrancyGuard.sol has become an industry standard: github.com/OpenZeppelin/o…
Contract using a reentrancy guard
5/ b) Checks, Effects, Interaction Pattern (CEI)

The effects/changes in the state variables of the contract should be carried out before any interactions with another contract.

For ex, checking the balance of the customer before allowing a withdrawal. Checks, Effects, Interaction Pattern code example
6/ c) Security review

Although audits are not the cure to all exploits, getting one decreases exponentially the likelihood of an attack happening.

Consider reaching out to @CyfrinAudits for a competitive or private audit of your codebase to find vectors like this and others. Graph showing audited vs unaudited protocols and their hacks
@CyfrinAudits If you enjoyed this thread:

1. Follow me @SoloditOfficial for more of these
2. RT the tweet below to share this thread with your audience
3. Subscribe to Cyfrin's security newsletter here: cyfrin.io/newsletter

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Solodit 🟪

Solodit 🟪 Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @SoloditOfficial

Nov 24, 2023
In 2022, DeFi protocols lost $403.2 million in 40+ oracle manipulation attacks.

Oracle manipulations are one of the most common attacks in crypto.

But what are they and how do they happen? 👇 Image
1/ 📃 First, what is an oracle?

An oracle bridges blockchains to external systems (like APIs or other off/on-chain data).

They enable smart contracts to execute actions based upon inputs and outputs from the real world. Image
2/ 🤔 What does this have to do with DeFi?

Most DeFi protocols require the price of assets onchain in order to run their contracts - for ex, to decide when to liquidate a user’s loan.

Pricing is fetched from oracles, who expose the asset's value so contracts build on top of it.
Read 12 tweets
Nov 23, 2023
In 2022, DeFi protocols lost $403.2 million in 40+ oracle manipulation attacks.

Oracle manipulations are one of the most common attacks in crypto.

But what are they and how do they happen? 👇 @Chainalysis report on oracle manipulation stolen assets throughout the years
📃 First, what is an oracle?

An oracle bridges blockchains to external systems (like APIs or other off/on-chain data).

They enable smart contracts to execute actions based upon inputs and outputs from the real world. @Chainlink image explaining oracles
🤔 What does this have to do with DeFi?

Most DeFi protocols require the price of assets onchain in order to run their contracts - for ex, to decide when to liquidate a user’s loan.

Pricing is fetched from oracles, who expose the asset's value so contracts build on top of it.
Read 11 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(