Summarized by Nhat-Minh Nguyen (@nmnguyen)
TLDR
- Smart contracts are increasingly used with blockchain systems for high-value applications. It is highly desired to ensure the quality of smart contract source code before they are deployed. A DAO hack, realizing a vulnerability, stole 3.6 million Ether by exploiting the fallback function in the code that was exposed to reentrancy.
- In this paper, we propose a new tool with a new method for representing smart contracts as specialized graphs and learning their patterns automatically via graph neural networks on a large scale to detect vulnerabilities at both line-level and contract-level accuracy.
- We have deployed the MANDO-GURU web app for visualizing specialized interactive graphs and highlighted vulnerabilities that help users to double-check their smart contracts easier.
Citation
Hoang H. Nguyen, Nhat-Minh Nguyen, Hong-Phuc Doan, Zahra Ahmadi, Thanh-Nam Doan, and Lingxiao Jiang. 2022. MANDO-GURU: Vulnerability Detection for Smart Contract Source Code by Heterogeneous Graph Embeddings. In Proceedings of the ACM Joint European Software Engineering Conference and Symposium on the Foundations of Software Engineering (ESEC/FSE’22), Singapore, 14 - 18 November, 2022.
Preprint: https://hoanghnguyen.com/assets/pdf/nguyen2022fse.pdf
Core Research Question
- What are the performances of our models compared to several state-of-the-art baselines on contract-level vulnerability classification?
- What are the performances of our models on line-level vulnerability detection?
Background
- Smart contracts are self-executing lines of code with the terms of an agreement between buyer and seller automatically verified and executed via a computer network contained therein exists across a distributed, decentralized blockchain network.
- Control-flow graph (CFG) is a representation of all paths that might be traversed through a program during its execution.
- Call graph (CG) represents calling relationships between subroutines in a computer program.
- Graph Neural Network is a class of artificial neural networks for processing data that can be represented as graphs.
Summary
- Smart contracts are increasingly used with blockchain systems for high-value applications. It is highly desired to ensure the quality of smart contract source code before deployment.
- More and more individual developers or industry practitioners can develop Decentralized Applications (DApps). However, previous research has shown that many real-world smart contracts deployed on blockchains have serious vulnerabilities, for example, the DAO attack and the Parity attack.
- The DAO attack exploits a recursive call vulnerability to transfer one-third of the DAO funds to a malicious account (worth about USD 50 million)
- The Parity attack exploits a vulnerability in the library contract to steal over 150,000 ETH from a malicious account (worth about USD 30 million).
Method
MANDO-GURU contains three main components: Backend, RESTful APIs, and Frontend.
The Backend plays a vital role with several core sub-components such as heterogeneous representation for the generated graphs from input smart contracts, heterogeneous graph fusion, custom multi-metapaths extraction, heterogeneous graph neural network, and vulnerability detections in coarse-grained and fine-grained levels.
- Heterogeneous representation: We utilized Slither tool to generate basic CFGs and CGs from smart contract input and then convert them into heterogeneous forms.
- Heterogeneous contract graph is a fusion of heterogeneous CFGs and CGs which enrich information for learning. Accordingly, the heterogeneous CG edges of the smart contract act as bridges to link the discrete heterogeneous CFGs of the smart contract functions into a global fused graph.
- Multi-metapaths extraction: pre-defining all possible metapaths with any length according to all possible node types and edge types is a challenge, it would lead to an exponential explosion of meta-paths. Besides, the order of these node types can change dynamically depending on the input contracts’ structures. In order to address the problem of exploding and changing metapaths, our method focuses on length-2 metapaths through reflective connections between adjacent nodes to extract multiple metapaths contracts’ structures.
- Heterogeneous graph neural network: we separated our detection into 2 phases: Coarse-Grained Detection and Fine-Grained Detection.
- Coarse-Grained Detection: This phase classifies if a smart contract contains a vulnerability. We embedded to represent each input smart contract, and train the MLP to predict clean or vulnerable contracts. This classification assists in reducing the search space by filtering out those clean contracts and reducing noisy data before the second phase of fine-grained vulnerability detection at the line level.
- Fine-Grained Detection: we apply node classification on the node embeddings of their Heterogeneous Contract Graph to identify the nodes that may contain vulnerabilities, which correspond to statements or lines of code and allow us to detect the locations of the vulnerabilities at the fine-grained line level in smart contract source code.
Results
The first table is our best model, which improved buggy f1 score compared with the original Heterogeneous GNN (metapath2vec) and the best of 3 original Homogeneous GNNs (GCN, LINE, node2vec)
Access Control | Arithmetic | Denial of Service | Front Running | Reentrancy | Time Manipulation | Unchecked Low Level Calls | |
---|---|---|---|---|---|---|---|
Heterogeneous GNN | 62.90% | 56.46% | 55.17% | 63.40% | 61.79% | 66.29% | 55.22% |
Homogeneous GNNs | 62.63% | 58.59% | 60.12% | 64.77% | 66.23% | 66.65% | 61.69% |
MANDO-GURU | 71.19% | 66.85% | 89.15% | 89.86% | 76.09% | 87.71% | 72.08% |
The second table is our best model comparing with the best of 6 conventional tools (Securify, Mythril, Slither, Manticore, Smartcheck, Oyente) and original Heterogeneous GNN (metapath2vec) and the best of 3 original Homogeneous GNNs (GCN, LINE, node2vec).
Access Control | Arithmetic | Denial of Service | Front Running | Reentrancy | Time Manipulation | Unchecked Low Level Calls | |
---|---|---|---|---|---|---|---|
Conventional Detection Tools | 34.0% | 73.0% | 52.0% | 63.0% | 23.0% | 44.0% | 14.0% |
Heterogeneous GNN | 35.46% | 68.70% | 60.64% | 80.65% | 71.66% | 67.51% | 26.06% |
Homogeneous GNNs | 53.59% | 68.61% | 64.06% | 83.06% | 74.78% | 70.76% | 38.13% |
MANDO-GURU | 80.93% | 84.35% | 82.12% | 90.51% | 86.40% | 90.29% | 84.81% |
Discussion and Key Takeaways
- In this paper, we proposed a new method, based on multi-level graph embeddings of control-flow graphs and call graphs of Solidity smart contracts, to train more accurate vulnerability detection models that can identify vulnerabilities in smart contracts at fine-grained line level and contract level of granularity.
- MANDO-GURU can significantly improve many other vulnerability detection techniques by up to 24% in terms of the F1-score at the contract level.
- MANDO-GURU significantly improves the traditional code analysis-based techniques by up to 63.4%.
- By using our online tool, developers can check whether their contracts have any vulnerabilities and explore some statistics to have a glance into the details of a smart contract.
- Our tool is publicly available at GitHub - MANDO-Project/ge-sc-machine: MANDO-GURU, a deep graph learning-based tool, aims to accurately detect vulnerabilities in smart contract source code at both coarse-grained contract-level and fine-grained line-level.. A test version is currently deployed at http://mandoguru.com, and a demo video of our tool is available at Mando-Guru introduction - YouTube.
- However, our supervised learning approach requires a labeled dataset which is a challenge in programming analysis. It’s time-consuming to annotate bugs in source code.
Applicability
- Our method is a valuable complement to other vulnerability detection techniques and contributes to smart contract security.
- Furthermore, We can also adapt our method to cases where only compiled smart contract bytecode is available without source code to expand.
- Our approach also fits other programming languages as long as they can be represented in a graph form.