Angels or demons: investigating and detecting decentralized financial traps on ethereum smart contracts

Jiachi Chen, Jiang Hu, Xin Xia, David Lo, John C. Grundy, Zhipeng Gao, Ting Chen

TL;DR

While Decentralized Finance (DeFi) promises a "trustless" financial system, many smart contracts contain intentional "backdoors" that allow developers to unfairly profit at the expense of users. This paper introduces DeFiDefender, a symbolic execution tool that identifies five specific "DeFi Contract Traps" (DCTs), revealing that over 52% of analyzed Ethereum DeFi contracts contain at least one such trap.

What Problem Does This Paper Address?

The core appeal of DeFi is its reliance on immutable, self-executing smart contracts that remove the need for centralized intermediaries. However, the authors argue that this creates a false sense of security. Unlike traditional software vulnerabilities—which are unintentional "errors" that anyone can exploit—DeFi Contract Traps (DCTs) are intentional code snippets that grant contract owners special privileges to manipulate the system.

These traps are often buried deep within bytecode or hidden in unexecuted parts of the source code, making them nearly invisible to the average user. The paper seeks to categorize these traps, understand their prevalence, and provide an automated way to detect them before users lose their assets.

Methods

The authors developed DeFiDefender, a tool designed to detect DCTs using a combination of symbolic execution and Abstract Syntax Tree (AST) analysis.

The tool's workflow is split based on the type of trap:

  1. Bytecode Analysis: For four of the five traps, DeFiDefender analyzes the contract's runtime and creation bytecode. It builds a Control Flow Graph (CFG) and uses symbolic execution to track storage slots and permission owners. It then applies predefined symbolic patterns to identify suspicious logic (e.g., a selfdestruct function reachable only by the owner).
  2. Source Code Analysis: For the "Forged Transfer" trap, the tool parses the Solidity source code into an AST to build a Contract Call Graph (CCG). This allows it to identify "dead code" subcontracts that contain promised functionalities (like withdrawal logic) that can never actually be executed.

DeFiDefender is optimized for speed, utilizing an updated version of the DefectChecker engine to support a wide range of Solidity compiler versions (v0.4.11 to v0.8.17).

Datasets

The study utilized two primary datasets:

  • Small-scale Dataset: 700 manually labeled DeFi smart contracts used to evaluate the tool's accuracy. This dataset was curated by independent developers with years of experience in smart contract security.
  • Large-scale Dataset: 20,679 real-world DeFi-related Ethereum smart contracts extracted from a pool of over 117,000 verified contracts on Etherscan (as of February 2022).

Key Insights and Ideas

  • DCTs vs. Vulnerabilities: This is the paper's most important conceptual contribution. A vulnerability is a bug; a DCT is a "feature" for the owner. While a vulnerability might hurt both the owner and the user, a DCT specifically benefits the owner at the user's expense.
  • The "Centralization" Paradox: Despite the "decentralized" label, the high prevalence of these traps suggests that many DeFi projects maintain centralized control. Owners can often change interest rates, drain funds, or destroy the contract at will.
  • The Five Traps:
    • Tricky Send: Deliberately failing to check the return value of a transfer, allowing the contract to "clear" a user's balance even if the money wasn't actually sent.
  • Super Storage Permission: Backdoors that let owners change critical variables (like interest rates) after deployment.
  • Super Transfer Permission: Functions that allow owners to drain the contract's Ether balance.
  • Selfdestruct Permission: The "rug pull" button; owners can kill the contract and pocket all remaining funds.
  • Forged Transfer: Misleading users by putting withdrawal logic in subcontracts that are never actually inherited or called by the main contract.

Key Results

  • High Efficiency: DeFiDefender is remarkably fast, requiring only 0.48 seconds on average to analyze a single contract.
  • Strong Accuracy: On the labeled dataset, the tool achieved an average accuracy of 98.17%, with a precision of 99.74% and a recall of 89.24%.
  • Widespread Prevalence: In the large-scale analysis of ~20k contracts, 52.13% contained at least one trap.
  • The Most Common Trap: "Super Storage Permission" was found in 50.56% of the contracts, highlighting how common it is for developers to retain the ability to manipulate contract states.

Limitations and Future Work

  • Intent vs. Functionality: The authors acknowledge that the presence of a trap doesn't guarantee malicious intent. Some "traps" might be intended as emergency "circuit breakers." Future work aims to analyze historical transactions to see if these traps were actually used maliciously.
  • Path Explosion: As smart contracts grow in complexity, symbolic execution faces the "path explosion" problem, where the number of possible execution paths becomes too large to process.
  • Source Code Dependency: The "Forged Transfer" trap currently requires source code for detection, which isn't always available on the blockchain.
  • Evasion: Now that these patterns are defined, malicious developers may find more sophisticated ways to obfuscate these traps to bypass detection tools.

Summary updated

Abstract

<title>Abstract</title> Decentralized Finance (DeFi) uses blockchain technologies to transform traditional financial activities into decentralized platforms that run without intermediaries and centralized institutions. Smart contracts are programs that run on the blockchain, and by utilizing smart contracts, developers can more easily develop DeFi applications. Some key features of smart contracts -- self-executed and immutability -- ensure the trustworthiness, transparency and efficiency of DeFi applications, and have led to a fast-growing DeFi market. However, misbehaving developers can add traps or backdoor code snippets to a smart contract, which are hard for contract users to discover. We call these code snippets in a DeFi smart contract as ''DeFi Contract Traps" (DCTs). In this paper, we identify five DeFi contract traps and introduce their behaviors, describe how attackers use them to make unfair profits, and analyse their prevalence in the Ethereum platform. We propose a symbolic execution tool, DeFiDefender, to detect such traps and use a manually labeled small-scale dataset that consists of 700 smart contracts to evaluate it. Our results show that our tool is not only highly effective but also highly efficient. DeFiDefender only needs 0.48s to analyze one DeFi smart contract and obtains a high average accuracy (98.17%), precision (99.74%), and recall (89.24%). Among the five DeFi contract traps introduced in this paper, four of them can be detected through contract bytecode without the need for source code. We also apply DeFiDefender to a large-scale dataset that consists of 20,679 real DeFi related Ethereum smart contracts. We found that 52.13% of these DeFi smart contracts contain at least one contract trap. Although a smart contract that contains contract traps is not necessarily malicious, our finding suggests that DeFi related contracts have many centralized issues in a zero-trust environment and in the absence of a trusted party.
BibTeX
@article{chen2024angels,
  author = {Jiachi Chen and Jiang Hu and Xin Xia and David Lo and John C. Grundy and Zhipeng Gao and Ting Chen},
  title = {{Angels or demons: investigating and detecting decentralized financial traps on ethereum smart contracts}},
  journal = {Autom. Softw. Eng.},
  year = {2024},
  doi = {10.1007/s10515-024-00459-4},
}
Figure