Level 4 · Money and marketsThe gold rush#295

How DeFi protocols get hacked: five doors into the strongbox, and the code is only one

DeFi protocols lose money through five main doors: a bug in the code, a manipulated price feed, stolen admin keys, a captured vote and a tampered website. Euler Finance lost about $197 million to a code flaw in March 2023; BadgerDAO users lost about $120 million to a tampered front end in December 2021.

Outlaws in the boomtown rarely blew the strongbox open. Dynamite was loud. It was easier to bribe the man with the key, swap the scales at the assay office, or put up a fake sign pointing miners to the wrong window.

DeFi strongboxes get robbed the same way. Sometimes the lock itself is faulty. More often, the robbers come through a side door nobody was watching.

Here are the five doors, and two robberies that used two of them.

What it is

An exploit is a way of taking funds from a protocol that its builders did not intend, using its own rules or the systems around it. The common doors are:

  • Code bugs: the contract does something its authors didn't mean. "Smart contract risks" covers the basics.
  • Oracle manipulation: the price feed is pushed off, so the contract lends or pays against a false price ("Oracles explained").
  • Key compromise: whoever holds the admin or bridge keys is hacked or turns ("Why bridges get hacked").
  • Governance capture: someone gathers enough votes to pass a harmful proposal, as Beanstalk learned ("Governance tokens and DAOs").
  • Front-end attacks: the website is tampered with, so users sign something harmful while the contracts stay untouched.

Even the cover sellers sort losses this way. Nexus Mutual's Protocol Cover pays for code bugs, oracle failure and governance takeovers, but excludes "user interface or website errors" and "private key security breaches" (Nexus Mutual, Protocol Cover terms).

How it works: Euler, the faulty lock

Euler Finance was a lending protocol on Ethereum. On 13 March 2023, starting at 8:50:59 UTC, an attacker took about $197 million (Euler Labs).

The flaw sat in a function called donateToReserves. It had been added to fix a different bug reported through Euler's bug bounty, audited, and deployed by July 2022. In Euler's own words, "the function lacked a health check".

The attack, using flash-loaned funds, in three steps:

  • Build a large borrowed position with the borrowed funds.
  • Donate collateral to the reserves, which the missing check allowed, leaving the position under water.
  • Liquidate yourself and collect the liquidation bonus, which was larger than what the donation cost.

No key stolen, no vote bought. The contract followed its rules exactly. The rules had a gap.

The ending is unusual. Euler offered a 10% bounty and announced a $1 million reward for information. The attacker sent funds back in stages from 18 March and returned the last of it on 3 April 2023, more than $177 million by The Block's count (The Block). Because ether had risen, Euler says, about $240 million in assets came back. Most robbers don't return the gold. Don't plan on it.

How it works: BadgerDAO, the fake sign

On 2 December 2021, users of BadgerDAO, a DeFi yield protocol, lost about $120 million. The contracts were fine. The website wasn't.

When users made ordinary deposits or claimed rewards, the site slipped in extra token approvals that let the attacker's address move their tokens, according to Rekt's reconstruction (Rekt). Badger later blamed "a maliciously injected snippet" served through a compromised Cloudflare API key that its engineers had not created (Badger, via CoinDesk).

The users signed. The contracts obeyed. The sign at the assay office had simply been repainted. The approvals themselves are explained in the article on token approvals.

Why it matters

Audits look at code. Euler had been audited many times, and its fix had been audited too. Badger's loss happened in a place no smart contract audit looks at.

For users, the door matters because it decides what can protect you. A code bug can hit even the most careful depositor. A front-end attack only works if you sign what you didn't read.

Where people get it wrong

  • "Audited means safe." Euler was audited, and so was the fix that broke it.
  • "If the contracts are fine, I'm fine." Badger's contracts were fine.
  • "Hacks are always about code." Keys, votes, prices and websites are doors too.
  • "Hacked funds come back." Euler's did. That is the exception, not the rule.

Count the doors before you deposit

Before you deposit, ask five questions, one per door. Is the code audited, and has it changed since? Where does it get its prices? Who holds the admin keys? Can a vote change the rules, and how fast? And are you using the official site, checking every approval it asks for?

You can't fix the lock. You can stop walking through the fake door.

Frequently asked questions

How do DeFi protocols get hacked?

Mostly through five routes: bugs in the smart contract code, manipulated price oracles, stolen or misused admin keys, governance votes captured by an attacker, and tampered websites that trick users into signing harmful transactions.

What happened in the Euler Finance hack?

On 13 March 2023, an attacker used a flaw in Euler's donateToReserves function, which lacked a health check, to drain about $197 million. The attacker returned the funds in stages, with the last transfers on 3 April 2023.

What is a front-end attack in DeFi?

A front-end attack tampers with the website or app users interact with, rather than the smart contracts. In the BadgerDAO case of December 2021, the site added malicious token approvals to normal transactions, and about $120 million was taken.

Does a smart contract audit prevent hacks?

No. An audit lowers the risk of code bugs but cannot guarantee there are none, and it does not cover keys, websites or governance. Euler Finance had been audited repeatedly and was still exploited.

Can I get my money back after a DeFi hack?

Sometimes, but rarely. Euler's attacker returned the funds after negotiations, and some users had cover that paid out. In most hacks the funds are never recovered.

Sources

  1. Euler Labs, War and peace: behind the scenes of Euler's $240M exploit recovery (10 January 2024)
  2. The Block, Euler hacker returns $31 million, marking end to 'recoverable funds' in DeFi exploit (4 April 2023)
  3. Rekt, Badger: rekt (December 2021)
  4. CoinDesk, BadgerDAO reveals details of how it was hacked for $120M (10 December 2021)
  5. Nexus Mutual, Protocol Cover terms and conditions (IPFS)

Read next.