44
Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1

Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

Surveying Front-running attacks on Decentralized Exchanges

Mingcheng & Sean

1

Page 2: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

Goal

2

● Introduce a special kind of attck on decentralized exchange

● Try to propose a mitigation in the end

Page 3: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

What is this?

3

Page 4: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

Blockchain Introduction

blockchain is essentially a decentralized, distributed “database” (ledger) recording transactions, which cannot be altered retroactively without the alteration of all subsequent blocks.

● Distributed● Decentralized● Non-centralized Trust● Immutable

4

Page 5: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

Blockchain workflow:block and block generation

Blockchain consists of blocks.

5

Blockchain consists of blocks.

Each block consists of a head and a body.

- Body records transaction- Head includes the cryptographic hash (SHA256) of the body and the prior block

Page 6: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

Blockchain workflow:block and block generation

6

Head:Time: TimestamphashMerkleRoot: Hash of BodyhashPrevBlock: Hash of previous BlockBits: Target ValueNonce: Magic Number….

Body:Transactions...

Data

Page 7: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

Blockchain workflow:block and block generation

Head:Time: TimestamphashMerkleRoot: Hash of BodyhashPrevBlock: Hash of previous BlockBits: Target ValueNonce: Magic Number….

Body:Trasactions...

When SHA256(Head, Nonce++) < Bits (Target Max/Difficulty level)

Data

New Block

prev: H()

7

Page 8: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

prev: H()

Data Data

Blockchain workflow:block and block generation

Blockchain consists of blocks.

Each block consists of head and body.

- Body records transaction- Head includes the cryptographic hash (SHA256) of the body and the prior block

Block Miner can get block reward and operation fee.

- proof-of-work

8

Page 9: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

Blockchain workflow:broadcast and census

9

● If divergence, the chain has been seen by the majority will be validated forever (7 confirmation).

Page 10: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

Blockchain workflow:broadcast and census

10

● If divert, the chain has been seen by the majority will be validated forever.

● To keep consistent, bitcoin is designed to generate new block in average 10 mins by adjusting the difficulty level.

Page 11: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

Just ledger!!

Wasting powers/resource!!

Problems with bitcoin blockchain:

11

Page 12: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

Add More Flavors: Ethereum Virtual Machine and Smart Contract ● Lightweight computer programs executed on blockchain network without user

interaction when when certain conditions are made.○ When someone wants to get a particular task done in Ethereum they initiate a smart contract

with one or more peers.

● The EVM provides better deterministic, terminable and isolated environment for the smart contracts. (Like JVM)

○ EVM is turning complete

12

Page 13: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

The Etheruem Network

Smart Contract

blockchain

13

Page 14: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

Smart Contracts

blockchain

Smart Contracts

blockchain

Smart Contracts

blockchain

The Etheruem NetworkSmart Contracts

blockchain

Smart Contracts

blockchain

Smart Contracts

blockchain

Smart Contracts

blockchain

14

Page 15: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

● Gas is a unit that measures the amount of computational effort that it will take to execute certain operations.

○ Every line of code in Solidity requires a certain amount of gas to be executed.

Ethereum Gas and Fee

Gas Needed

● Gas Limit >= Gas needed● Gas Fee = Gas Limit * Gas Price (Gwei)● Gas Fee is the maximum profit code runner can get

15

Page 16: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

The Scale of Ethereum Network

● Ethereum is now the 2nd largest blockchain Network in th world

Market Cap = $ 20+ Billion

● Frequent Transactions

Data Collected from etherscan https://etherscan.io/

Page 17: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

ICO on Ethereum = Fundraising

Initial Coin OfferingSmart Contract

Public

DeveloperEther

ERC20 Tokens

Gather

Piles of Ether

=> Money

17

(ERC20 is a Protocol for Etheurm Cryptocurrencies, it allow direct interaction bewteen parties)

Page 18: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

Functionalities of ERC20 Tokens

18

● Toll: A token can act as a gateway to the Dapp.

● Value Exchange: Tokens can help create an internal economic

system within the application.

● Voting Rights: The tokens may also qualify the holders to have

certain voting rights.

Page 19: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

A Need for Exchange

TokenA TokenB

TokenC

Cryptocurrency Exchange

19

Page 20: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

Centralized Exchange

20

Page 21: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

Decentralized Exchange (DEXs)

Core Contract

1A for 2B__________________

Order Book

Matcher

The Exchange Smart Contracts

MakerTokenA

“I offer 1 TokenA for 2 TokenB”

___________________________

Taker

TokenB

“I take the TokenA with 2

TokenB”

Great Deal!

1A for 2B__________________

Proxy Contract1A for 2B

21

Page 22: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

Decentralized Exchange

Core Contract

AlgorithmicMatcher

Algorithmic Marketmaker

MakerTokenA

“I want to buy TokenB with TokenA”

Taker

TokenB

“I want to buy TokenA

with TokenB”

Proxy ContractA ⇔ B

With a Calculated Rate

22

Buy Order -> Price UPSell Order -> Price DOWN

Page 23: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

Algorithmic DEX Example - Bancor

23

Page 24: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

Industry Designs are more complexed

24

Kyber Network

“Kyber: An On-Chain Liquidity Protocol,” Apr. 22, 2019. https://files.kyber.network/Kyber_Protocol_22_April_v0.1.pdf (accessed Apr. 13, 2020).

Page 25: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

Transaction Visibility

25

Page 26: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

Transaction Delay

2 Block ConfirmationsGenerally Required at least 7 Confirmations

26

Page 27: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

Transaction Order is Not Guaranteed

Miner’s BlockConfirmed BlockConfirmed Block

TX1 TX2 TX3 TX4

TX5, 0.01 Gwei

Pending TX Queue

TX6, 0.03 Gwei

TX7, 0.02 Gwei

TX8, 0.15 GweiTX8 TX6

TX8 is more likely to be included in a block along the chain

27

Page 28: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

Profit Through Change TX Order

TX1: Buys 1000 TokenA

0.01 Gwei

Time

TokenA price goes up

Algorithmic Exchange

TX2: Buys 10 TokenA

0.02 Gwei

TX3: Sells 10 TokenA

0.009 Gwei

Frontrunning Attack

28

Page 29: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

Another Frontrunning Scenario

DEX A

Decentralized Exchange

Sell 1 for 5 ETH

Decentralized Exchange

Buy 1 for 6 ETH

TX: Buy 5 from A and Sell to B

0.2 Gwei

DEX B

Bot

Confirm TX

Pending TX0.1 Gwei

29

Page 30: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

Another Frontrunning Scenario

DEX A

Decentralized Exchange

Sell 1 for 5 ETH

Decentralized Exchange

Buy 1 for 6 ETH

TX: Buy 5 from A and Sell to B

0.2 Gwei

DEX B

Bot

Confirm TX

Pending TX0.1 Gwei

TX: Buy 5 from A and Sell to B

0.3 Gwei

Bot* 30

Page 31: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

Another Frontrunning Scenario

DEX A

Decentralized Exchange

Sell 1 for 5 ETH

Decentralized Exchange

Buy 1 for 6 ETH

TX...0.2 Gwei

DEX B

Bot

Confirm TX

Pending TX0.1 Gwei

TX...0.3 Gwei

Bot*

TX...0.4 Gwei

Bot** 31

Page 32: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

Another Frontrunning Scenario

DEX A

Decentralized Exchange

Sell 1 for 5 ETH

Decentralized Exchange

Buy 1 for 6 ETH

TX...0.2 Gwei

DEX B

Bot

Confirm TX

Pending TX0.1 Gwei

TX...0.3 Gwei

Bot*

TX...n Gwei

Bot**

...

Inefficiency to the blockchain

And Slows down the confirmation of other TXs

32

Page 33: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

Scale of Frontrunning Bots

Philip Daian, Steven Goldfeder, et al. “Flash Boys 2.0: Frontrunning, Transaction Reordering, and Consensus Instability in Decentralized Exchanges”. ArXiv abs/1904.05234 .2019.

33

Page 34: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

Scale of Frontrunning Bots

Philip Daian, Steven Goldfeder, et al. “Flash Boys 2.0: Frontrunning, Transaction Reordering, and Consensus Instability in Decentralized Exchanges”. ArXiv abs/1904.05234 .2019.

34

Page 35: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

Frontrunning Vulnerability

35

TX are visible to everyone

+

TX takes time to get confirmed = Frontrunning Attack!

+

Pending TX can be reordered.

E

!

Page 36: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

Mitigation - Reduce Visibility

36

Page 37: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

Mitigation - Encryption

Core Contract

1A for 2B__________________

Order Book

Matcher

The Exchange Smart Contracts

MakerTokenA

“I offer 1 TokenA for 2 TokenB”

Taker

TokenB

“I take the TokenA with

2 TokenB”

37

Page 38: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

Mitigation - Encryption

Core Contract

1A for 2B__________________

Order Book

Matcher

The Exchange Smart Contracts

MakerTokenA

“I offer 1 TokenA for 2 TokenB”

Taker

TokenB

“I take the TokenA with

2 TokenB”

38Key Map

Wallet Address

Key

Wallet Address Key

Page 39: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

Mitigation - Encryption

Core Contract

E for E__________________

Order Book

Trader

The Exchange Smart Contracts

MakerTokenA

“I offer ENCRYPT for ENCRYPT”

Taker

TokenB

“I take the ENCRYPT

with ENCRYPT

Matcher

E for E________________

Trusted Execution Environment(Inspired by Tesseart)

Trade Info

39

Key

Page 40: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

Countering Algorithmic Frontrunning

TX1: Buys 1000 TokenA

0.01 Gwei

Time

TokenA price goes up

Algorithmic Exchange

40

Page 41: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

Countering Algorithmic Frontrunning

TX1: Buys ENCRYPT

0.01 Gwei

Time

??? price goes up

Algorithmic Exchange

???

???

Create Transaction:

41

Page 42: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

DEX A

Decentralized Exchange

Sell 1 for 5 ETH

Decentralized Exchange

Buy 1 for 6 ETH

TX: Buy 5 from A and Sell to B

0.2 Gwei

DEX B

Bot

Confirm TX

Pending TX0.1 Gwei

42

Countering Frontrunning Bot

Page 43: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

Countering Frontrunning Bot

43

DEX A

Decentralized Exchange

Sell 1 for 5 ETH

Decentralized Exchange

Buy 1 for 6 ETH

TX: Buy E from A and Sell E to B

0.2 Gwei

DEX B

Bot

Confirm TX

Pending TX0.1 Gwei

Bot*

What is E and how many?

Page 44: Decentralized Exchanges Surveying Front-running attacks on · Surveying Front-running attacks on Decentralized Exchanges Mingcheng & Sean 1. Goal 2 ... Countering Frontrunning Bot

Our current progress

- Last semester, one of the research in the lab developed a blockchain network that allows cross-chain exchange between Bitcoin and Ethereum

- We got the task to survey current exchange, their vulnerabilities, and possible mitigations

- The lab group we work with are designing the protocl for communications between TEE and Blockchain Network.

44