lookahead

An ML-based framework for DeFi attack detection (based on adversarial contracts)

https://github.com/zju-abclab/lookahead

Science Score: 57.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
    Found 4 DOI reference(s) in README
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (15.6%) to scientific vocabulary

Keywords

blockchain defi ethereum security web3
Last synced: 6 months ago · JSON representation ·

Repository

An ML-based framework for DeFi attack detection (based on adversarial contracts)

Basic Info
  • Host: GitHub
  • Owner: zju-abclab
  • Language: Python
  • Default Branch: main
  • Homepage:
  • Size: 12.4 MB
Statistics
  • Stars: 4
  • Watchers: 2
  • Forks: 0
  • Open Issues: 0
  • Releases: 0
Topics
blockchain defi ethereum security web3
Created 10 months ago · Last pushed 7 months ago
Metadata Files
Readme Citation

README.md

LookAhead: Preventing DeFi Attacks via Unveiling Adversarial Contracts

This repository contains the source code for LookAhead, an effective and efficient ML-based framework for DeFi attack detection (based on adversarial contracts).


Code Structure

decompiler

This folder contains custom-designed plugins for Gigahorse, a binary lifter and analysis framework for Ethereum smart contracts. We introduce various extra functional modules to be integrated with the Gigahorse toolkit.

The core functionality is to launch a web server (gigahorse_web_server.py), which is responsible for handling contract data uploads and running Gigahorse tools to perform contract data analysis.

model_eval

This folder contains code for evaluating the classifier and transformer models employed by LookAhead.

We evaluate the datasets, generate prediction results, measure execution time, and assess the performance of the models.

online_test

This folder contains the logic implemented for conducting online testing to measure the real-world performance of the LookAhead system.

  • Before performing the detection, we first train local models (logic located under training).
  • For detection, we use contract_feature_handler.py to extract contract features and classification_evaluator.py to load the trained models and produce predictions. An example usage is demonstrated in evaluate.ipynb. (logic located under detection)

We provide a report file, listing all true positive adversarial contracts detected during both experiment periods described in the paper.

[!NOTE] Addresses marked with a * were originally identified as true positives by our research team, but may be questionable after rechecking them post-publication. We include them here for transparency, but recommend treating them with caution.


Dataset

Our dataset includes benign and adversarial contracts and their associated features and is available in the format of SQLite database files under dataset folder. It also includes a manually curated dataset of address labels that classify the sources of funds.

Benign contracts are collected from Google BigQuery based on unique user interactions during the period from June 1, 2022 to June 30, 2024. The SQL queries used for retrieving addresses and calculating unique user interactions are available under dataset/sql.


Usage

LookAhead has been fully tested on Ubuntu 20.04.

Configure Environment

bash conda env create --name lookahead -f environment.yml conda activate lookahead

You will also need to install Gigahorse. For information regarding the Gigahorse project, see: https://github.com/nevillegrech/gigahorse-toolchain.

Make sure to install the Souffle addons required by Gigahorse: ```bash

builds all, sets libfunctors.so as a link to libsoufflenum.so

cd decompiler/gigahorse-toolchain/souffle-addon && make WORD_SIZE=$(souffle --version | sed -n 3p | cut -c12,13) ```

Environment Variables

Create a dotfile named .env and fill in the required API keys to be used as environment variables: ETHERSCAN_APIKEY=YOUR_ETHERSCAN_API_KEY ALCHEMY_APIKEY=YOUR_ALCHEMY_API_KEY BSC_QUICKNODE_APIKEY=YOUR_BSC_QUICK_NODE_API_KEY

Download Feature Dataset

Due to the file size limit of Git, we make our contract feature dataset files available via an alternative storage service, download them by running the following: bash cd dataset wget https://static.ouorz.com/features.db

Train and Evaluate Classifiers

We propose the following classifiers and a transformer model to be trained and evaluated: - XGBoost - Logistic Regression - Decision Tree - Random Forest - Support Vector Machine - K-Nearest Neighbor

bash python -W ignore evaluate_all.py

Run Tests

To run online_test, follow these steps:

Train local classifier models (trained models are saved under online_test/models)

bash cd online_test/training python -W ignore train.py

Start running a Gigahorse decompiler web server in the background:

bash cd decompiler python gigahorse_web_server.py

Perform detection tests using the Jupyter Notebook online_test/detection/evaluate_single_address.ipynb.


Citation

Published version available at: https://doi.org/10.1145/3729353.

bibtex @article{ren2025lookahead, title={LookAhead: Preventing DeFi Attacks via Unveiling Adversarial Contracts}, author={Ren, Shoupeng and He, Lipeng and Tu, Tianyu and Wu, Di and Liu, Jian and Ren, Kui and Chen, Chun}, journal={Proceedings of the ACM on Software Engineering}, volume={2}, number={FSE}, year={2025}, publisher={ACM New York, NY} }

Owner

  • Name: zju-abclab
  • Login: zju-abclab
  • Kind: organization

Citation (CITATION.bib)

@article{ren2025lookahead,
  title={LookAhead: Preventing DeFi Attacks via Unveiling Adversarial Contracts},
  author={Ren, Shoupeng and He, Lipeng and Tu, Tianyu and Wu, Di and Liu, Jian and Ren, Kui and Chen, Chun},
  journal={Proceedings of the ACM on Software Engineering},
  volume={2},
  number={FSE},
  year={2025},
  publisher={ACM New York, NY}
}

GitHub Events

Total
  • Watch event: 3
  • Push event: 1
  • Public event: 1
Last Year
  • Watch event: 3
  • Push event: 1
  • Public event: 1

Dependencies

environment.yml pypi
  • absl-py ==2.1.0
  • aiohappyeyeballs ==2.6.1
  • aiohttp ==3.11.18
  • aiosignal ==1.3.2
  • annotated-types ==0.7.0
  • astunparse ==1.6.3
  • attrs ==25.3.0
  • bitarray ==3.3.1
  • blinker ==1.9.0
  • catboost ==1.2.8
  • certifi ==2024.7.4
  • charset-normalizer ==3.3.2
  • ckzg ==2.1.1
  • click ==8.1.8
  • contourpy ==1.2.1
  • cycler ==0.12.1
  • cytoolz ==1.0.1
  • eth-abi ==5.2.0
  • eth-account ==0.13.7
  • eth-hash ==0.7.1
  • eth-keyfile ==0.8.1
  • eth-keys ==0.7.0
  • eth-rlp ==2.2.0
  • eth-typing ==5.2.1
  • eth-utils ==5.3.0
  • flask ==3.1.0
  • flatbuffers ==24.3.25
  • fonttools ==4.53.1
  • frozenlist ==1.6.0
  • gast ==0.6.0
  • google-pasta ==0.2.0
  • graphviz ==0.20.3
  • grpcio ==1.65.5
  • h5py ==3.11.0
  • hexbytes ==1.3.0
  • idna ==3.7
  • itsdangerous ==2.2.0
  • jinja2 ==3.1.6
  • keras ==3.5.0
  • kiwisolver ==1.4.5
  • libclang ==18.1.1
  • lightgbm ==4.6.0
  • markdown ==3.7
  • markdown-it-py ==3.0.0
  • markupsafe ==2.1.5
  • matplotlib ==3.9.2
  • mdurl ==0.1.2
  • ml-dtypes ==0.4.0
  • multidict ==6.4.3
  • namex ==0.0.8
  • narwhals ==1.35.0
  • nvidia-cublas-cu12 ==12.3.4.1
  • nvidia-cuda-cupti-cu12 ==12.3.101
  • nvidia-cuda-nvcc-cu12 ==12.3.107
  • nvidia-cuda-nvrtc-cu12 ==12.3.107
  • nvidia-cuda-runtime-cu12 ==12.3.101
  • nvidia-cudnn-cu12 ==8.9.7.29
  • nvidia-cufft-cu12 ==11.0.12.1
  • nvidia-curand-cu12 ==10.3.4.107
  • nvidia-cusolver-cu12 ==11.5.4.101
  • nvidia-cusparse-cu12 ==12.2.0.103
  • nvidia-nccl-cu12 ==2.19.3
  • nvidia-nvjitlink-cu12 ==12.3.101
  • opt-einsum ==3.3.0
  • optree ==0.12.1
  • parsimonious ==0.10.0
  • pillow ==10.4.0
  • plotly ==6.0.1
  • propcache ==0.3.1
  • protobuf ==4.25.4
  • pycryptodome ==3.22.0
  • pydantic ==2.11.3
  • pydantic-core ==2.33.1
  • pyparsing ==3.1.2
  • python-dateutil ==2.9.0.post0
  • python-dotenv ==1.1.0
  • pyunormalize ==16.0.0
  • regex ==2024.11.6
  • requests ==2.32.3
  • rich ==13.7.1
  • rlp ==4.1.0
  • scikit-learn ==1.5.1
  • scipy ==1.14.0
  • tensorboard ==2.17.1
  • tensorboard-data-server ==0.7.2
  • tensorflow ==2.17.0
  • tensorrt ==10.3.0
  • tensorrt-cu12 ==10.3.0
  • tensorrt-cu12-bindings ==10.3.0
  • tensorrt-cu12-libs ==10.3.0
  • termcolor ==2.4.0
  • threadpoolctl ==3.5.0
  • toolz ==1.0.0
  • tqdm ==4.67.1
  • types-requests ==2.32.0.20250328
  • typing-inspection ==0.4.0
  • urllib3 ==2.2.2
  • web3 ==7.10.0
  • websockets ==15.0.1
  • werkzeug ==3.1.3
  • wrapt ==1.16.0
  • xgboost ==2.1.4
  • yarl ==1.20.0