TL;DR
This paper introduces Esim, a novel system for detecting similar Ethereum Virtual Machine (EVM) smart contracts by representing their bytecode as a Stable-Semantic Graph (SSG). Esim achieves state-of-the-art accuracy in similarity detection and significantly outperforms existing tools in real-world vulnerability searches, addressing critical security challenges in the rapidly expanding DeFi ecosystem.
What Problem Does This Paper Address?
The decentralized finance (DeFi) sector is booming, but this growth is accompanied by significant security risks. A major challenge is the prevalence of code reuse and the scarcity of open-source smart contracts. This makes it difficult to identify plagiarism and, more importantly, to detect and prevent the spread of vulnerable code. Traditional binary similarity detection methods, often based on instruction streams or control flow graphs (CFGs), struggle with EVM bytecode due to its low-level nature, heavily reused basic blocks, and the constant evolution of the Solidity compiler (Solc) versions. These factors obscure semantic meaning and disrupt traditional analysis techniques, necessitating a new approach tailored to EVM's unique characteristics.
Methods
Esim introduces a novel EVM bytecode representation called the Stable-Semantic Graph (SSG). The core idea behind SSG is to focus on "stable instructions" – those with high semantic relevance (like storage, log, call, and return operations) that are less susceptible to the code fragmentation caused by basic block reuse.
The SSG construction involves two main steps:
- SSG Builder: This module first constructs a Stable Control Flow Graph (SCFG) by identifying stable instructions and their control flow relationships, then integrates Stable Data Flow Graph (SDFG) information through backward taint analysis to capture data dependencies.
- SSG Embedding Generator: This module uses a heterogeneous graph neural network (GNN) with a Siamese architecture to learn vector embeddings for the SSGs. These embeddings capture the semantic essence of the bytecode, allowing for similarity comparisons using standard vector distance metrics.
Datasets
The authors curated three datasets for their evaluation:
- Dataset I: Used to evaluate the accuracy of SSG construction. It includes source code and bytecode for two well-known DeFi protocols (Uniswap V2 and USDT).
- Dataset II: Used for training the neural network and evaluating similarity detection. It comprises bytecode compiled from the same source code using multiple Solc versions, providing ground truth for similarity. This dataset contains 21,018 SSGs from the top 1,000 DeFi projects.
- Dataset III: A large-scale, real-world dataset used to evaluate Esim's efficiency and performance. It includes 2,675,573 distinct contracts deployed across six EVM-compatible chains (Ethereum, Optimism, BSC, Polygon, Arbitrum, Avalanche) between January 2023 and June 2024.
Key Insights and Ideas
- Graded Semantic Relevance of EVM Instructions: Not all EVM instructions are created equal. Instructions that interact with the blockchain's global state (storage, logs, calls) are more semantically significant and stable than those that only manipulate the stack. This insight is crucial for building a robust representation.
- Stability Against Basic Block Reuse: Instructions with high semantic relevance are less likely to be fragmented or heavily reused by the compiler. This stability makes them ideal anchors for building a consistent representation across different compiler versions and optimization levels.
- SSG Bridges Low-Level Bytecode and High-Level Semantics: By focusing on stable instructions and their relationships, SSG effectively captures the essential logic of EVM bytecode, overcoming the semantic loss inherent in low-level representations.
- Heterogeneous Graph Embeddings Capture Richness: Combining control flow and data flow information within a heterogeneous graph structure, and then embedding it using GNNs, allows Esim to retain a significant amount of semantic information for accurate similarity detection.
Key Results
- SSG Construction Accuracy: Esim achieves 100% F1-score for control flow and 95.16% F1-score for data flow in SSG construction.
- Similarity Detection Performance: Esim achieves a state-of-the-art AUC of 96.3%, outperforming traditional methods and baselines like FuzzHash (0.818 AUC) and Genius (0.837 AUC) by a significant margin. It also outperforms Eclone (0.94 AUC) despite Eclone's limitations.
- Efficiency: SSG generation for a single contract takes an average of 26 seconds, while embedding generation is negligible (0.06 seconds). Searching across a database of millions of contracts takes approximately 5 seconds.
- Real-World Vulnerability Search: In a case study analyzing DeFi attacks, Esim identified 382 similar vulnerable contracts with zero false positives, significantly outperforming Etherscan, which found only 255 contracts with a false positive rate exceeding 93.3%.
Limitations and Future Work
- Limitations: The paper acknowledges that while Esim is highly effective, its current focus is solely on EVM bytecode. The authors also note that the real-world impact of detected vulnerabilities is often realized after the attack, suggesting the need for real-time integration.
- Future Work: The authors propose extending their approach to other programming languages and scenarios by recognizing the differing significance of instructions. They also suggest integrating Esim with real-time attack detection systems to enable swift victim identification and preventive measures.
Summary updated