Git Product home page Git Product logo

exploit-uniswap's Introduction

Exploiting an ERC777-token Uniswap Exchange

standard-readme compliant

Exploiting any Uniswap exchange that trades an ERC777 token by leveraging the reentrant microtrading attack vector

Table of Contents

Install

  1. Setup a Python virtual environment
$ pip3 install virtualenv
$ virtualenv -p python3 venv
  1. Activate virtual env & install Python dependencies (just Vyper)
$ source venv/bin/activate
$ pip install -r requirements.txt

If source venv/bin/activate does not work for you, try out with bash venv/bin/activate.

  1. Install NPM dependencies
$ npm install

Run

Once in the virtual environment (where Vyper must be installed), run

(venv)$ npm test

Exploit details

The proof of concept for the exploit is located in the test/uniswap.exploit.js file. It takes care of setting up the entire environment and running three test case scenarios.

The environment consists of:

The three test cases are:

  1. Legitimate trading with a single external sale: a user that holds tokens wants to operate on the exchange, to deposit tokens and receive ETH. This is done in a single transaction calling the tokenToEthSwapInput function. This is the regular use case for a Uniswap exchange.

  2. Legitimate trading with multiple external sales: same as case (1), but now the user submits multiple transactions instead of just 1. Therefore, this results in less profit than (1).

  3. Exploiting: the attacker deploys an attacker contract that will be in charge of operating in the exchange. The exploit is executed in a single transaction, reentering several times in the vulnerable function tokenToEthSwapInput by leveraging the ERC777 tokensToSend hook.

Why it works

By leveraging the tokensToSend hook, the attacker contract is called after receiving ETH (i.e. the exchange ETH balance has decreased) but before the token balance is modified (i.e. the exchange token balance has not decreased). As a consequence, reentering the vulnerable tokenToEthSwapInput will re-calculate the token-ETH exchage price, but this time with less ETH and same amount of tokens in reserves. Thus, the exchange will be buying the attacker tokens, paying in ETH, at a higher price than it should.

Learning resources

Disclaimer

This is a proof-of-concept exploit of an already public, disclosed and acknowledged vulnerability in Uniswap related to reentrancy attacks. Were that not the case, under no circumstances this proof-of-concept exploit would have been made public. Should you find any 0-day vulnerability in these contracts, please report directly to Uniswap.

exploit-uniswap's People

Contributors

tinchoabbate avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.