Back to blog
SecurityMarch 12, 2026·6 min read

How Smart Contracts Get Hacked

The most common smart contract vulnerabilities — reentrancy attacks, integer overflow, and access control flaws — explained with real examples like the DAO hack.

#Smart Contract#Security#Reentrancy#DAO Hack#Hacking

"Code Is Law" — But What If the Code Has a Bug?

One of blockchain's core philosophies is "Code is Law." Smart contracts execute automatically according to code-defined rules, with no intermediary and no possibility of arbitrary modification.

But this philosophy is a double-edged sword. If the code has a bug, exploiting that bug is also "executing according to the code." The attacker hasn't broken a contract — they've simply triggered execution in a way the developer didn't intend.

What makes smart contracts particularly attractive targets? Three things: the code is immutable once deployed (you can't patch it quietly), the code is publicly readable by anyone (attackers can study it at leisure), and contracts hold real monetary value (there's a direct financial reward for finding flaws).

To date, smart contract exploits have drained tens of billions of dollars from the ecosystem.

The DAO Hack (2016) — $60 Million

The first major smart contract hack, and the event that changed Ethereum's history.

Background: The DAO was an Ethereum-based decentralized investment fund in 2016. It raised the largest crowdfund in Ethereum history at the time — roughly $150 million worth of ETH.

Vulnerability: Reentrancy Attack

Simplified, the withdrawal code looked like this:

// Vulnerable code (simplified)
function withdraw(uint amount) public {
    require(balance[msg.sender] >= amount);
    // Step 1: Send the funds
    msg.sender.call{value: amount}("");
    // Step 2: Update the balance  ← the problem
    balance[msg.sender] -= amount;
}

The attacker deployed a malicious contract that, upon receiving ETH in step 1, immediately called withdraw again before step 2 ever executed. The balance hadn't been decremented yet, so the check in require passed again. This loop repeated hundreds of times, draining $60 million in ETH.

Aftermath: The Ethereum community made an extraordinary decision: hard fork the blockchain to reverse the hack as if it never happened — voluntarily breaking the "immutable" principle. Those who disagreed and kept the original chain created Ethereum Classic (ETC), which exists to this day.

Integer Overflow and Underflow

Before Solidity 0.8, numbers had no automatic overflow protection.

uint8 x = 255;
x += 1; // x becomes 0, not 256 (before Solidity 0.8)

In 2018, the BEC token contract was exploited using integer overflow, allowing the attacker to mint an astronomically large number of tokens out of thin air and crash the token's price to near zero.

Defense: Use Solidity 0.8+ (which has built-in overflow checks) or the OpenZeppelin SafeMath library for older contracts.

Access Control Flaws

When privileged functions lack proper access checks, attackers can assume administrative control.

Poly Network (2021) — $611 million: The largest single DeFi hack ever. Poly Network was a cross-chain bridge, and an attacker found a flaw in the contract's permission verification logic. By calling a specific function in a particular sequence, the attacker was able to designate their own address as the contract's administrator — then use that authority to withdraw all assets.

Remarkably, the attacker returned all the funds three days later, claiming they wanted to expose the vulnerability. The most expensive white-hat hack in history.

Defense: Use OpenZeppelin's Ownable pattern, always apply onlyOwner or role-based access modifiers to sensitive functions.

Flash Loan Attacks

Flash loans are a DeFi primitive that lets you borrow any amount of assets with no collateral — as long as you repay within the same transaction. This enables legitimate arbitrage but also powerful exploits.

The attack pattern:

  1. Borrow a massive amount (e.g., $100M) in a single transaction
  2. Use the funds to manipulate a price oracle or drain an imbalanced pool
  3. Profit from the manipulation
  4. Repay the flash loan — all in one atomic transaction

If anything fails, the entire transaction reverts. If it succeeds, the attacker walks away with the profit.

The 2020 bZx attack used flash loans to manipulate oracle prices on a thinly traded pair, netting millions of dollars with zero upfront capital.

How to Reduce Smart Contract Risk

For developers:

  • Follow the Checks-Effects-Interactions pattern (update state before calling external contracts)
  • Use established, audited libraries like OpenZeppelin rather than writing everything from scratch
  • Commission multiple security audits from different firms
  • Run a bug bounty program through platforms like Immunefi

For users:

  • Prefer protocols with long track records and large TVL — bugs tend to be discovered and fixed over time
  • Be cautious during the first weeks after a new protocol launches — that's when most exploits occur
  • Verify that a project has been audited by reputable firms, and check when the audit was done relative to the current code

Is Exploiting a Bug "Legal" Under "Code Is Law"?

This is the philosophical question the blockchain community continues to wrestle with.

During the DAO hack, the attacker argued: "I simply executed the contract as its code permitted." Technically, this was accurate.

But the Ethereum community judged based on intent. Draining funds in a way the designers clearly never intended was treated as theft. They chose to hard fork and reverse it.

This episode demonstrated that blockchain is not a purely mechanical system where "code is absolute law" — it ultimately depends on human community consensus. Decentralization has limits, and those limits are social as much as technical.


To explore smart contract vulnerabilities hands-on, the Security interactive module lets you simulate reentrancy attacks and flash loan exploits.

Want to experience it yourself?

Try ChainLearn's interactive modules to simulate the concepts directly.

Start Learning