DeFi Security I: Smart Contract Vulnerabilities
The $3.8 Billion Problem
On August 10, 2021, Poly Network—a cross-chain bridge protocol—was hacked. The attacker exploited a single smart contract vulnerability and drained $611 million across three blockchains. It was the largest DeFi hack in history at that time.
The vulnerability? A simple access control flaw: the attacker called a privileged function that should have been restricted to administrators. Within hours, the entire treasury was gone.
But here's the shocking part: The hacker returned the funds. They later claimed it was a "white hat" operation to expose vulnerabilities. The crypto world breathed a sigh of relief, but the incident exposed an uncomfortable truth:
DeFi is a hacker's paradise.
The numbers tell the story:
DeFi losses by year:
2020: $162 million stolen (8 major hacks)
2021: $1.8 billion stolen (23 major hacks)
2022: $3.1 billion stolen (163 exploits)
2023: $1.8 billion stolen (231 exploits)
2024: $1.2 billion+ stolen (YTD)
Total (2020-2024): $8+ billion
Average hack: $15 million
Median hack: $2.5 million
Largest single hack: $611M (Poly Network)
Why DeFi is uniquely vulnerable:
1. Code is law (no exceptions):
Traditional finance:
Bug detected → Transaction reversed
Hack occurs → Funds frozen
Mistake made → Bank reverses it
DeFi:
Bug detected → Already exploited
Hack occurs → Funds gone forever
Mistake made → Loss is permanent
Smart contracts execute exactly as written
No "customer service" to call
No "undo" button
2. Money + code = maximum incentive:
Traditional software bugs:
Impact: Website crash, data loss, inconvenience
Attacker gain: Reputation damage, data theft
DeFi bugs:
Impact: Immediate financial loss
Attacker gain: Direct monetary profit
Incentive: Maximum (take millions instantly)
Result: Every line of code under attack
Billions at stake
Professional hackers targeting
3. Transparent targets:
All code is open source (verifiable)
All TVL is visible (know exactly how much to steal)
All transactions are public (can test attacks on-chain)
Attackers can:
- Read entire codebase
- Simulate attacks off-chain
- See exact vulnerability
- Calculate exact profit
- Execute atomically (all or nothing)
Result: Attackers have massive advantage
4. Composability = complexity:
DeFi protocols interact with each other
Bug in one protocol affects all that depend on it
Cascading failures possible
Attack surface grows exponentially
Example:
Protocol A uses Protocol B for oracle
Protocol B has bug
Attacker exploits B to manipulate oracle
All protocols using B's oracle compromised
5. Immutability = no patches:
Traditional software: Deploy patch, update all instances
Smart contracts: Immutable once deployed
If bug found:
- Cannot be patched (code is permanent)
- Must deploy new contract (lose TVL)
- Or use upgrade proxy (adds complexity, centralization)
- Meanwhile: Attackers can exploit anytime
This lesson explores the most common and costly smart contract vulnerabilities—the mistakes that have cost billions and continue to plague DeFi:
What we'll cover:
- Reentrancy: The DAO hack and how recursive calls steal funds
- Integer overflow/underflow: When math breaks
- Access control: Who can call what (and why it matters)
- Logic errors: Flawed assumptions and edge cases
- Flash loan attacks: Borrowing millions to manipulate markets
- Oracle manipulation: Exploiting price feeds
- Front-running and MEV exploitation
- Best practices and security patterns
Real hacks we'll analyze:
- The DAO (2016): $60M stolen via reentrancy
- Parity Multisig (2017): $150M frozen forever
- bZx (2020): $1M stolen via flash loan manipulation
- Harvest Finance (2020): $34M stolen via flash loan + oracle
- Cream Finance (2021): $130M stolen via reentrancy
- Poly Network (2021): $611M stolen via access control
Understanding these vulnerabilities isn't just academic—it's essential survival knowledge for anyone building or using DeFi. Every protocol is under constant attack. Every bug will be found. And the cost of mistakes is measured in millions.
Let's explore how attackers exploit smart contracts—and how defenders can protect against them.