university of illinois urbana-champaign erays: reverse ......yi zhou, deepak kumar, surya bakshi,...

86
Erays: Reverse Engineering Ethereum’s Opaque Smart Contracts Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign 1

Upload: others

Post on 21-May-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

Erays: Reverse Engineering Ethereum’s Opaque Smart Contracts

Yi Zhou, Deepak Kumar, Surya Bakshi,Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

1

Page 2: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

Introduction:Ethereum

2

Page 3: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

Introduction:Ethereum Smart Contracts

● Computer programs on the blockchain

● Written in high level language (Solidity)

● Executed in the Ethereum Virtual Machine (EVM)

3

Page 4: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

Solidity Code

contract dummy {uint s;

function foo(uint a) public returns (uint) {while (a < s) {

if (a > 10) {a += 1;

} else {a += 2;

}}return a;

}} 4

Page 5: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

Compiled Contract

608060405260043610603e5763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416632fbebd3881146043575b600080fd5b348015604e57600080fd5b506058600435606a565b60408051918252519081900360200190f35b60005b600054821015609357600a821115608857600182019150608f565b6002820191505b606d565b50905600a165627a7a7230582095826fc9f61669f3d0fe36966d60c64042dec36a23ac89e6b4ebe1752f2c7ca00029

5

Page 6: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

EVM Bytecode

PUSH1 0x80PUSH1 0x40 MSTORE PUSH1 0x04 CALLDATASIZE LT PUSH1 0x3e JUMPI PUSH4 0xffffffff PUSH29 0x0100000000000000000000000000000000000000000000000000000000 PUSH1 0x00 CALLDATALOAD ... 6

Page 7: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

● EVM bytecode is not easily understandable

● High level source code is not always available

● Contract functionality remains opaque/proprietary

Problem:Opaque/proprietary contracts

7

Page 8: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

● Total Count: 1,024,886

● Unique Count: 34,328 Ecosystem:How many contracts are there?

8

Page 9: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

● 10,387 Solidity Source Files Collected (from Etherscan)

● 35 Versions (v0.1.3 to v0.4.19) of Solidity Compilers Used

● 88,426 Unique Binaries Compiled

How many contracts are opaque/proprietary?

Ecosystem:

9

Page 10: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

Ecosystem: Measuring Opacity

Contracts

Total 1,024,886

Unique 34,328 (100.0%)

Unique Transparent 7,734 (22.5%)

Unique Opaque 26,594 (77.5%)

10

Page 11: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

Ecosystem: Measuring Opacity

Contracts

Total 1,024,886

Unique 34,328 (100.0%)

Unique Transparent 7,734 (22.5%)

Unique Opaque 26,594 (77.5%)

11

Page 12: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

12

Erays

Page 13: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

Erays: System Design

1

Control Flow Graph

Recovery

2

Lifting

3

Optimization

4

Aggregation

5

Control Flow Structure Recovery

13

Page 14: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

● Identify basic block boundaries ... JUMPDEST PUSH1 0x0 JUMPDEST PUSH1 0x0 SLOAD DUP3 LT ISZERO PUSH1 0x93 JUMPI ...

Control Flow Graph Recovery

141

Page 15: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

● Identify basic block boundaries

Control Flow Graph Recovery

... JUMPDEST PUSH1 0x0 JUMPDEST PUSH1 0x0 SLOAD DUP3 LT ISZERO PUSH1 0x93 JUMPI ...

151

Page 16: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

● Identify basic block boundaries

● Organize basic blocks into a CFG

○ Emulate the contract using a stack model

○ Explore the contract in a manner similar to Depth First Search

○ Record stack images at each block entrance

Control Flow Graph Recovery

161

Page 17: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

JUMPDEST...

...PUSH1 0x88JUMPI

Control Flow Graph Recovery

...PUSH1 0x8fJUMP

...PUSH1 0x93JUMPI

...return

...

...PUSH1 0x6dJUMP

17

Page 18: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

JUMPDEST...

...PUSH1 0x88JUMPI

Control Flow Graph Recovery

...PUSH1 0x8fJUMP

...PUSH1 0x93JUMPI

...return

...

...PUSH1 0x6dJUMP

18

Page 19: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

JUMPDEST...

...PUSH1 0x88JUMPI

Control Flow Graph Recovery

...PUSH1 0x8fJUMP

...PUSH1 0x93JUMPI

...return

...

...PUSH1 0x6dJUMP

19

Page 20: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

JUMPDEST...

...PUSH1 0x88JUMPI

Control Flow Graph Recovery

...PUSH1 0x8fJUMP

...PUSH1 0x93JUMPI

...return

...

...PUSH1 0x6dJUMP

20

Page 21: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

JUMPDEST...

...PUSH1 0x88JUMPI

Control Flow Graph Recovery

...PUSH1 0x8fJUMP

...PUSH1 0x93JUMPI

...return

...

...PUSH1 0x6dJUMP

21

Page 22: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

JUMPDEST...

...PUSH1 0x88JUMPI

Control Flow Graph Recovery

...PUSH1 0x8fJUMP

...PUSH1 0x93JUMPI

...return

...

...PUSH1 0x6dJUMP

22

Page 23: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

JUMPDEST...

...PUSH1 0x88JUMPI

Control Flow Graph Recovery

...PUSH1 0x8fJUMP

...PUSH1 0x93JUMPI

...return

...

...PUSH1 0x6dJUMP

23

Page 24: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

JUMPDEST...

...PUSH1 0x88JUMPI

Control Flow Graph Recovery

...PUSH1 0x8fJUMP

...PUSH1 0x93JUMPI

...return

...

...PUSH1 0x6dJUMP

24

Page 25: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

JUMPDEST...

...PUSH1 0x88JUMPI

Control Flow Graph Recovery

...PUSH1 0x8fJUMP

...PUSH1 0x93JUMPI

...return

...

...PUSH1 0x6dJUMP

25

Page 26: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

● Convert stack-based operations into register-based representation (R. Vallee-Rai 1999)

○ Map stack slots to registers

Lifting: Stack-based to Register-based

...

$s2

$s1

$s0

261 2

Page 27: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

● Convert stack-based operations into register-based representation (R. Vallee-Rai 1999)

○ Map stack slots to registers

○ Assign registers to each bytecode (using stack height)

Lifting: Stack-based to Register-based

ADD

$s2 0x2

$s1 0x3

$s0 0xb2

271 2

Page 28: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

● Convert stack-based operations into register-based representation (R. Vallee-Rai 1999)

○ Map stack slots to registers

○ Assign registers to each bytecode (using stack height)

Lifting: Stack-based to Register-based

ADD

$s2 0x2

$s1 0x3

$s0 0xb2

281 2

Page 29: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

● Convert stack-based operations into register-based representation (R. Vallee-Rai 1999)

○ Map stack slots to registers

○ Assign registers to each bytecode (using stack height)

Lifting: Stack-based to Register-based

ADD

$s2 0x2 + 0x3

$s1

$s0 0xb2

291 2

Page 30: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

● Convert stack-based operations into register-based representation (R. Vallee-Rai 1999)

○ Map stack slots to registers

○ Assign registers to each bytecode (using stack height)

Lifting: Stack-based to Register-based

ADD

$s2 0x5

$s1

$s0 0xb2

301 2

Page 31: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

● Convert stack-based operations into register-based representation (R. Vallee-Rai 1999)

○ Map stack slots to registers

○ Assign registers to each bytecode (using stack height)

Lifting: Stack-based to Register-based

ADD

$s2

$s1 0x5

$s0 0xb2

311 2

Page 32: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

● Convert stack-based operations into register-based representation (R. Vallee-Rai 1999)

○ Map stack slots to registers

○ Assign registers to each bytecode (using stack height)

Lifting: Stack-based to Register-based

ADD $s1, $s2, $s1

$s2

$s1 0x5

$s0 0xb2

321 2

Page 33: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

● Convert stack-based operations into register-based representation (R. Vallee-Rai 1999)

○ Map stack slots to registers

○ Assign registers to each bytecode (using stack height)

Lifting: Stack-based to Register-based

PUSH1 0x0 SLOAD DUP3 LT ISZERO PUSH1 0x93 JUMPI

331 2

Page 34: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

● Convert stack-based operations into register-based representation (R. Vallee-Rai 1999)

○ Map stack slots to registers

○ Assign registers to each bytecode (using stack height)

Lifting: Stack-based to Register-based

MOVE $s4, 0x0 SLOAD $s4, [$s4] MOVE $s5, $s2 LT $s4, $s5, $s4 ISZERO $s4, $s4 MOVE $s5, 0x93 JUMPI $s5, $s4

341 2

Page 35: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

● Convert stack-based operations into register-based representation (R. Vallee-Rai 1999)

● Introduce new instructions

Lifting: Stack-based to Register-based

351 2

Page 36: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

● Convert stack-based operations into register-based representation (R. Vallee-Rai 1999)

● Introduce new instructions

○ INTCALL, INTRET

○ MOVE

○ ASSERT

○ NEQ, GEQ, LEQ, SL, SR

Lifting: Stack-based to Register-based

361 2

Page 37: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

● Global optimizations (1973 G. Kildall)

Optimization: Removing Redundancy

MOVE $s4, 0x0 SLOAD $s4, [$s4] MOVE $s5, $s2 LT $s4, $s5, $s4 ISZERO $s4, $s4 MOVE $s5, 0x93 JUMPI $s5, $s4

371 2 3

Page 38: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

● Global optimizations (1973 G. Kildall)

○ Constant propagation

Optimization: Removing Redundancy

MOVE $s4, 0x0 SLOAD $s4, [0x0] MOVE $s5, $s2 LT $s4, $s5, $s4 ISZERO $s4, $s4 MOVE $s5, 0x93 JUMPI 0x93, $s4

381 2 3

Page 39: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

● Global optimizations (1973 G. Kildall)

○ Constant propagation

○ Copy propagation

Optimization: Removing Redundancy

MOVE $s4, 0x0 SLOAD $s4, [0x0] MOVE $s5, $s2 LT $s4, $s2, $s4 ISZERO $s4, $s4 MOVE $s5, 0x93 JUMPI 0x93, $s4

391 2 3

Page 40: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

● Global optimizations (1973 G. Kildall)

○ Constant propagation

○ Copy propagation

○ Dead code elimination

Optimization: Removing Redundancy

-- SLOAD $s4, [0x0] -- LT $s4, $s2, $s4 ISZERO $s4, $s4 -- JUMPI 0x93, $s4

401 2 3

Page 41: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

● Global optimizations (1973 G. Kildall)

○ Constant propagation

○ Copy propagation

○ Dead code elimination

● Local optimizations

Optimization: Removing Redundancy

-- SLOAD $s4, [0x0] -- LT $s4, $s2, $s4 ISZERO $s4, $s4 -- JUMPI 0x93, $s4

411 2 3

Page 42: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

● Global optimizations (1973 G. Kildall)

○ Constant propagation

○ Copy propagation

○ Dead code elimination

● Local optimizations

Optimization: Removing Redundancy

-- SLOAD $s4, [0x0] -- -- GEQ $s4, $s2, $s4 -- JUMPI 0x93, $s4

421 2 3

Page 43: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

● Global optimizations (1973 G. Kildall)

○ Constant propagation

○ Copy propagation

○ Dead code elimination

● Local optimizations

Optimization: Removing Redundancy

SLOAD $s4, [0x0] GEQ $s4, $s2, $s4 JUMPI 0x93, $s4

431 2 3

Page 44: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

● Convert register-based instructions into three address form

Aggregation: Condensing the Output

SLOAD $s4, [0x0] GEQ $s4, $s2, $s4 JUMPI 0x93, $s4

441 2 3 4

Page 45: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

● Convert register-based instructions into three address form

Aggregation: Condensing the Output

$s4 = S[0x0] $s4 = $s2 ≥ $s4 if ($s4) goto 0x93

451 2 3 4

Page 46: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

● Convert register-based instructions into three address form

● Aggregate instructions into nested expressions (R. Vallee-Rai 1999)

Aggregation: Condensing the Output

$s4 = S[0x0] $s4 = $s2 ≥ $s4 if ($s4) goto 0x93

461 2 3 4

Page 47: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

● Convert register-based instructions into three address form

● Aggregate instructions into nested expressions (R. Vallee-Rai 1999)

Aggregation: Condensing the Output

-- $s4 = $s2 ≥ S[0x0] if ($s4) goto 0x93

471 2 3 4

Page 48: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

● Convert register-based instructions into three address form

● Aggregate instructions into nested expressions (R. Vallee-Rai 1999)

Aggregation: Condensing the Output

-- -- if ($s2 ≥ S[0x0]) goto 0x93

481 2 3 4

Page 49: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

● Convert register-based instructions into three address form

● Aggregate instructions into nested expressions (R. Vallee-Rai 1999)

Aggregation: Condensing the Output

if ($s2 ≥ S[0x0]) goto 0x93

491 2 3 4

Page 50: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

Control Flow Structure Recovery

● Separate each public function subgraph

● Use structural analysis (M. Sharir 1980)

○ Match subgraphs to control constructs (while, if then else)

○ Collapse matched subgraphs

501 2 3 4 5

Page 51: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

ASSERT(0 == msg.value)$s2 = C[0x4]

if ($s2 <= 0xa) goto 0x88

Control Flow Structure Recovery

$s2 = 0x1 + $s2goto 0x8f

if ($s2 >= S[0x0]) goto 0x93

M[$m] = $s2RETURN($m, 0x20)

$s2 = 0x2 + $s2

goto 0x6d

51

Page 52: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

ASSERT(0 == msg.value)$s2 = C[0x4]

if ($s2 <= 0xa) {$s2 = 0x2 + $s2

} else {$s2 = 0x1 + $s2

}

Control Flow Structure Recovery

if ($s2 >= S[0x0]) goto 0x93

M[$m] = $s2RETURN($m, 0x20)

goto 0x6d

52

Page 53: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

ASSERT(0 == msg.value)$s2 = C[0x4]

if ($s2 <= 0xa) {$s2 = 0x2 + $s2

} else {$s2 = 0x1 + $s2

}goto 0x6d

Control Flow Structure Recovery

if ($s2 >= S[0x0]) goto 0x93

M[$m] = $s2RETURN($m, 0x20)

53

Page 54: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

ASSERT(0 == msg.value)$s2 = C[0x4]

Control Flow Structure Recovery

while (0x1) {if ($s2 >= S[0x0])

breakif ($s2 <= 0xa) {

$s2 = 0x2 + $s2} else {

$s2 = 0x1 + $s2}

}

M[$m] = $s2RETURN($m, 0x20)

54

Page 55: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

Control Flow Structure Recovery

ASSERT(0 == msg.value)$s2 = C[0x4]while (0x1) {

if ($s2 >= S[0x0])break

if ($s2 <= 0xa) {$s2 = 0x2 + $s2

} else {$s2 = 0x1 + $s2

}}M[$m] = $s2RETURN($m, 0x20)

55

Page 56: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

● Construct test cases using historical transactions

● Leverage Geth to generate the expected transaction output

● “Execute” our representation and compare the output

Validation

56

Page 57: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

● Construct test cases using historical transactions

● Leverage Geth to generate the expected transaction output

● “Execute” our representation and compare the output

Validation

Transactions

Total 15,855 (100.0 %)

57

Page 58: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

● Construct test cases using historical transactions

● Leverage Geth to generate the expected transaction output

● “Execute” our representation and compare the output

Validation

Transactions

Total 15,855 (100.0 %)

Success 15,345 (96.8%)

58

Page 59: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

● Construct test cases using historical transactions

● Leverage Geth to generate the expected transaction output

● “Execute” our representation and compare the output

Validation

Transactions

Total 15,855 (100.0 %)

Success 15,345 (96.8%)

Failures 510 (3.2%)

59

Page 60: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

● Construct test cases using historical transactions

● Leverage Geth to generate the expected transaction output

● “Execute” our representation and compare the output

Validation

Transactions

Total 15,855 (100.0 %)

Success 15,345 (96.8%)

Failures 510 (3.2%)

Construction Failures 196 (1.2%)

60

Page 61: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

● Construct test cases using historical transactions

● Leverage Geth to generate the expected transaction output

● “Execute” our representation and compare the output

Validation

Transactions

Total 15,855 (100.0 %)

Success 15,345 (96.8%)

Failures 510 (3.2%)

Construction Failures 196 (1.2%)

Comparison Failures 314 (2.0%)

61

Page 62: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

Use Case

62

Page 63: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

Erays: Function Fuzzy Hash

Binary X

Function A

Function B

Function C

63

Page 64: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

Erays: Function Fuzzy Hash

Binary X

Function A

Hash A0x746f7563...

Function B

Function C

Compute Fuzzy Hash

64

Page 65: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

Erays: Function Fuzzy Hash

Binary X

Function A

Hash A0x746f7563...

Function B

Function C

Hash B0x6865646d...

Hash C0x79737061...

65

Page 66: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

Erays: Code Sharing

Binary X

Function A

Hash A0x746f7563...

Function B

Function C

Hash B0x6865646d...

Hash C0x79737061...

Hash D0x67686574...

Binary Y

Function B

Function D

66

Page 67: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

Case Studies

67

Page 68: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

Case Study: High Value Contracts

● Look for opaque contracts with large Ether balance ~ $590M

● Multi-signature wallets likely used by the Gemini exchange

Multi-Signature Wallet: signature scheme requiring k-of-N signatures.

● Security best practice for large sums of money

68

Page 69: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

Case Study: High Value Contracts

● Look for opaque contracts with large Ether balance ~ $590M / 3 contracts

● Multi-signature wallets likely used by the Gemini exchange

● Interesting, time-dependent withdrawal policies

69

Multi-Signature Wallet: signature scheme requiring k-of-N signatures.

● Security best practice for large sums of money

Page 70: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

Time Dependency Hazard

● Found block.timestamp used in contract

● Erays reveals it is used to control the delay of withdrawal requests

● Useful auditing tool, even for opaque contracts

70

Page 71: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

Case Study: Duplicate Contracts

● Look for opaque contracts with the most instances

● Exchange user wallets○ Poloniex: ~350,000 contracts○ Yunbi: ~90,000 contracts

● A different approach to handling user funds

71

Page 72: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

Case Study: EtherDelta Arbitrage

● Decentralized token exchanges (DEX) operate entirely on-chain○ Etherdelta

Page 73: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

Case Study: EtherDelta Arbitrage

● Decentralized token exchanges (DEX) operate entirely on-chain○ Etherdelta

● Evidence of arbitrageurs

ArbitrageurBehavior

DEX

1. Buy @0.009

2. Sell @0.01

Page 74: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

Case Study: EtherDelta Arbitrage

● Decentralized token exchanges (DEX) operate entirely on-chain○ Etherdelta

● Evidence of arbitrageurs

● Executing a buy/sell mismatch for a profit

ArbitrageurBehavior

DEX

1. Buy @0.009

2. Sell @0.01

Page 75: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

Case Study: EtherDelta Arbitrage Bots

● Arbitrageurs must publish gadgets to facilitate arbitrage

● Create functions to validate the order and new trade

● Implement atomic batch trades (or fail)

ArbitrageurBehavior

1. Buy @0.009

2. Sell @0.01

Gadg. DEX

Buy/SellTrades

Assert or revert both

trades

Page 76: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

Case Study: CryptoKitties

76

Page 77: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

77

Page 78: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

Case Study: CryptoKitties

● On-chain game code is published with source code

● Game mechanism well understood

78

Page 79: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

Case Study: CryptoKitties

● Developers who know the algorithm aren’t allowed to play the game!

79

Page 80: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

Case Study: CryptoKitties

● Developers who know the algorithm aren’t allowed to play the game!

● So obviously we had to target this function

80

Page 81: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

Case Study: CryptoKitties

● The block hash is used to inject random mutations into genes and to select a parent for a gene

81

256-bits

…. 1234

Randomness(block hash)

234345

Matron Sire

Child2345

Page 82: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

Case Study: CryptoKitties

● The block hash is used to inject random mutations into genes and to select a parent for a gene

● Found a more effective breeding strategy

82

256-bits

…. 1234

Randomness(block hash)

234345

Matron Sire

Child2345

Page 83: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

Case Study: CryptoKitties

● The block hash is used to inject random mutations into genes and to select a parent for a gene

● Found a more effective breeding strategy

● Don’t rely on security through obscurity!

83

256-bits

…. 1234

Randomness(block hash)

234345

Matron Sire

Child2345

Page 84: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

Conclusion

● Ethereum smart contract ecosystem is largely opaque○ ~ 1M contracts, 34K unique, 77.5% unique opaque

84

Page 85: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

Conclusion

● Ethereum smart contract ecosystem is largely opaque○ ~ 1M contracts, 34K unique, 77.5% unique opaque

● Erays converts EVM bytecode into higher level representations○ https://github.com/teamnsrg/erays○ [email protected]

85

Page 86: University of Illinois Urbana-Champaign Erays: Reverse ......Yi Zhou, Deepak Kumar, Surya Bakshi, Joshua Mason, Andrew Miller, Michael Bailey University of Illinois Urbana-Champaign

Conclusion

● Ethereum smart contract ecosystem is largely opaque○ ~ 1M contracts, 34K unique, 77.5% unique opaque

● Erays converts EVM bytecode into higher level representations○ https://github.com/teamnsrg/erays○ [email protected]

● The utility of Erays is demonstrated in several case studies○ High value wallets, exchange user wallets, arbitrage bots, CryptoKitties secret

algorithm

86