Hashtag Web3 Logo

Advanced Solidity Exploits

9 min
advanced

Moving Beyond Reentrancy

Every new Solidity developer learns about the Reentrancy attack. But modern DeFi protocols are complex ecosystems of interacting contracts. The most devastating hacks today don't usually involve basic syntax errors; they involve economic exploits and systemic logic flaws.

Here is how auditors look for advanced exploits.

Flash Loan Attack Flow 1. Borrow $50M Flash Loan 2. Manipulate Pump DEX price 3. Exploit Borrow vs inflated 4. Repay Return loan 5. Profit Keep $40M 💰 🛡️ Fix: Use Chainlink oracles, not single DEX pool prices

1. Flash Loans and Oracle Manipulation

A Flash Loan is a uniquely Web3 concept. Smart contracts (like Aave) allow a user to borrow tens of millions of dollars with zero collateral, provided the entire loan is paid back within the exact same transaction block. If the loan isn't paid back by the last line of the transaction, the entire thing reverts as if it never happened.

Hackers don't steal the flash loan. They weaponize the capital to break other protocols.

The Exploit:

  1. Borrow: Hacker flash-borrows $50M USDC.
  2. Manipulate: They dump the $50M USDC into a small Curve pool to buy a low-cap token (let's call it $JUNK). The massive buy order artificially inflates the price of $JUNK by 10,000%.
  3. Exploit: The hacker goes to a Lending Protocol that uses that exact Curve pool as its "Price Oracle" (its source of truth for prices). The hacker deposits their $JUNK token. The Lending Protocol looks at the Curve pool, sees that $JUNK is supposedly worth a fortune, and allows the hacker to borrow $40M of real ETH against it.
  4. Repay: The hacker repays the $50M flash loan (plus a tiny fee).
  5. The Result: The hacker walks away with $40M in ETH. The Lending Protocol is left holding worthless $JUNK tokens, creating massive bad debt.

The Fix: Protocols must never use a single DEX pool as a price oracle. They must use decentralized oracle networks like Chainlink, which aggregate prices from across the entire market (both centralized and decentralized exchanges), making them too expensive to manipulate.

2. Uninitialized Proxies

Smart contracts are immutable. To fix bugs, developers use a "Proxy Pattern." The users interact with Contract A (the Proxy), which holds the money. Contract A forwards the logic to Contract B (the Implementation). If Contract B has a bug, the admin tells Contract A to point to a new Contract C.

Because of how this architecture works, you cannot use a standard constructor() function to set up the admin. You must use an initialize() function.

The Exploit:

If a developer deploys the Implementation contract but forgets to call initialize(), the contract is left wide open. A hacker can spot this on the blockchain, call initialize() themselves, and set themselves as the owner. They can then command the contract to destroy itself or steal funds.

The Fix: Developers must always call initialize() in the exact same deployment script, and the initialize() function must contain a modifier ensuring it can only be called once.

3. Signature Replay Attacks

To save users gas money, protocols often ask users to "sign a message" with their wallet off-chain, rather than executing an on-chain transaction. The protocol then submits this signature to the smart contract to execute an action (like a trade on OpenSea).

The Exploit:

If the smart contract isn't coded securely, a hacker can take a signature you generated yesterday (e.g., "Transfer 1 ETH to Bob") and submit it to the contract again today. If the contract doesn't track that the signature was already used, it will transfer another 1 ETH to Bob.

if the signature doesn't specify the blockchain, a hacker could take a signature meant for Ethereum and execute it on Polygon (where you might also have funds).

The Fix: Smart contracts must implement EIP-712 standards. Signatures must include a nonce (a unique number that increments, so older signatures are invalid) and the chain ID (so a signature for Ethereum cannot be replayed on Arbitrum).

Key takeaways

  • Flash loans allow anyone to execute capital-intensive attacks without needing any actual money.
  • Price oracles are the weakest link in DeFi. Relying on a single on-chain pool is highly vulnerable to manipulation.
  • Upgradeable contracts (Proxies) require careful initialization to prevent hackers from hijacking ownership.
  • Cryptographic signatures must include nonces and chain IDs to prevent replay attacks.

Quiz: Advanced Solidity Exploits

1 / 5

What makes a Flash Loan unique compared to traditional loans?