Everybody be cool, this is a roppery! Vincenzo Iozzo - - PowerPoint PPT Presentation

everybody be cool this is a roppery
SMART_READER_LITE
LIVE PREVIEW

Everybody be cool, this is a roppery! Vincenzo Iozzo - - PowerPoint PPT Presentation

Everybody be cool, this is a roppery! Vincenzo Iozzo (vincenzo.iozzo@zynamics.com) zynamics GmbH ( @ y ) y Tim Kornau (tim.kornau@zynamics.com) zynamics GmbH Ralf Philipp Weinmann (ralf philipp.weinmann@uni.lu) Universit du Luxembourg


slide-1
SLIDE 1

Everybody be cool, this is a roppery!

Vincenzo Iozzo (vincenzo.iozzo@zynamics.com) zynamics GmbH ( @ y ) y Tim Kornau (tim.kornau@zynamics.com) zynamics GmbH Ralf‐Philipp Weinmann (ralf‐philipp.weinmann@uni.lu) Université du Luxembourg BlackHat Vegas 2010

slide-2
SLIDE 2

Overview

1.Introduction 2.Gentle overview 3 Finding gadgets 3.Finding gadgets 4.Compile gadgets 5.Some fancy demos 6.Further work 6.Further work

slide-3
SLIDE 3

Introduction

Exploitation with non‐ bl h executable pages is not much fun

slide-4
SLIDE 4

But we have funny ideas

Exploitation with non‐ bl h executable pages is not much fun.. Unless you use “return‐ y

  • riented programming”
slide-5
SLIDE 5

Gentle introduction

slide-6
SLIDE 6

But life is hard

Code signing Code signing S db i Sandboxing ROP We were lucky!

slide-7
SLIDE 7

Code Signing

Used to make sure that only signed Used to make sure that only signed (Apple verified) binaries can be executed

  • If a page has write permissions it can’t

If a page has write permissions it can t have executable permissions N t bl th h

  • No executable pages on the heap
  • Only signed pages can be executed
slide-8
SLIDE 8

ROP

Instructions Variables for the gadget return sequence Instructions Address of the next gadget Variables for the gadget return sequence Instructions Address of the next gadget Variables for the gadget return sequence Instructions g g Address of the next gadget Variables for the gadget Instructions return sequence Variables for the gadget Address of the next gadget Instruction sequences within the attacked binary Attacker controlled memory

slide-9
SLIDE 9

ROP ‐ Workflow

  • 1. Find the gadgets
  • 2. Chain them to form a payload

p y 3 h l d

  • 3. Test the payload on your target
slide-10
SLIDE 10

Finding Gadgets Overview

1.Goal definition 2.Motivation 3 Strategy 3.Strategy 4.Algorithms 5.Results 6.Further improvement 6.Further improvement

slide-11
SLIDE 11

Goal definition

Build an algorithm which is bl f l d capable of locating gadgets within a given binary g y automatically without major side effects side effects.

slide-12
SLIDE 12

Motivation I

Little spirits need access to a wide range of devices. Because what is a device without a spirit?

slide-13
SLIDE 13

Motivation II

We want to be able to execute our code:

  • in the presents of non‐executable protection (AKA

NX bit)

  • when code signing of binaries is enabled.
  • but we do not aim at ASLR.
slide-14
SLIDE 14

Strategy I

  • Build a program from parts of another program
  • These parts are named gadgets

p g g

  • A gadget is a sequence of (useable) instructions
  • Gadgets must be combinable

g

  • end in a “free‐branch”
  • Gadgets must provide a useful operation

g p p

  • for example A + B
slide-15
SLIDE 15

Strategy II

  • The subset of useful gadgets must be locatable in

The subset of useful gadgets must be locatable in the set of all gadgets

  • Only the “simplest” gadget for an operation

Only the simplest gadget for an operation should be used

  • Side effects of gadgets must be near to zero to

Side effects of gadgets must be near to zero to avoid destroying results of previous executed code sequences. sequences.

  • Use the REIL meta language to be platform

independent. independent.

slide-16
SLIDE 16

Strategy III

A small introduction to the REIL meta language

  • small RISC instruction set (17 instructions)

small RISC instruction set (17 instructions)

  • Arithmetic instructions (ADD, SUB, MUL, DIV, MOD, BSH)
  • Bitwise instructions (AND, OR, XOR)

L i l i t ti (BISZ JCC)

  • Logical instructions (BISZ, JCC)
  • Data transfer instructions (LDM, STM, STR)
  • Other instructions (NOP, UNDEF, UNKN)
  • register machine

li i d b f i

  • unlimited number of temp registers
  • side effect free

i fl i i 64Bi

  • no exceptions, floating point, 64Bit, ..
slide-17
SLIDE 17

Algorithms

  • Stage I → Collect data from the binary
  • Stage II → Merge the collected data
  • Stage III → Locate useful gadgets in merged data
slide-18
SLIDE 18

Algorithms stage I (I)

Goal of the stage I algorithms:

  • Collect data from the binary
  • Collect data from the binary
  • 1. Extract expression trees from native

instructions instructions

  • 2. Extract path information

A

+

B D R0 15 D C R0 15 E

slide-19
SLIDE 19

Algorithms stage I (II)

Details of the stage I algorithms: 1 Expression tree extraction

  • 1. Expression tree extraction
  • Handlers for each possible REIL instruction

1 Most of the handlers are simple transformations

  • 1. Most of the handlers are simple transformations
  • 2. STM and JCC need to be treated specially
  • 2. Path extraction
  • Path is extracted in reverse control flow order

+

* * *

BISZ

OP

COND

OP

COND

slide-20
SLIDE 20

Algorithms stage II (I)

Goal of the stage II algorithms:

  • Merge the collected data from stage I
  • Merge the collected data from stage I
  • 1. Combine the expression trees for single

native instructions along a path native instructions along a path

  • 2. Determine jump conditions on the path

3 Simplify the result

  • 3. Simplify the result
slide-21
SLIDE 21

Algorithms stage II (II)

Details of the stage II algorithms:

  • Combine the expression trees for single native
  • Combine the expression trees for single native

instructions along a path

1 0 00000001 ADD R0 R1 R2

  • 1. 0x00000001 ADD R0, R1, R2
  • 2. 0x00000002 STR R0, R4
  • 3. 0x00000003 LDMFD SP! {R4,LR}
  • 4. 0x00000004 BX LR
slide-22
SLIDE 22

Algorithms stage II (III)

Details of the stage II algorithms:

  • Determine jump conditions on the path:
  • Determine jump conditions on the path:

Z FLAG MUST BE FALSE

  • 1. 0x00000001 SOME INSTRUCTION
  • 2. 0x00000002 BEQ 0xADDRESS
  • 3. 0x00000003 SOME INSTRUCTION
  • 4. 0x00000004 SOME INSTRUCTION

Generate condition tree

  • Simplify the result:
  • 4. 0x00000004 SOME INSTRUCTION

Simplify the result:

R0 = ((((((R2+4)+4)+4)+4) OR 0) AND 0xFFFFFFFF) R0 = R2+16 R0 R2+16

slide-23
SLIDE 23

Algorithms stage III (I)

Goal of the stage III algorithms:

  • Search for useful gadgets in the merged data
  • Search for useful gadgets in the merged data

− Use a tree match handler for each

  • peration
  • peration.
  • Select the simplest gadget for each operation

Select the simplest gadget for each operation − Use a complexity value to determine the gadget which is least complex (side‐ gadget which is least complex. (side‐ effects)

slide-24
SLIDE 24

Algorithms stage III (II)

Details of the stage III algorithms:

  • Search for useful gadgets in the merged data
  • Search for useful gadgets in the merged data

Trees of a ad et andidate Trees of a gadget candidate are compared to the tree of a specific operation. Can you spot the match ?

slide-25
SLIDE 25

Algorithms stage III (III)

Details of the stage III algorithms:

  • Select the simplest gadget for each operation
  • Select the simplest gadget for each operation

There are in most cases more instruction more instruction sequences which provide a specific

  • peration. The overall

complexity of all trees is used to determine which gadget is the simplest simplest.

slide-26
SLIDE 26

Results of gadget finding

  • Algorithms for automatic return‐oriented

programming gadget search are possible programming gadget search are possible.

  • The described algorithms automatically find the

necessary parts to build the return‐oriented necessary parts to build the return oriented program.

  • Searching for gadgets is not only platform but also

Searching for gadgets is not only platform but also very compiler dependent.

slide-27
SLIDE 27

So what is next

After automatic gadget extraction we need a simple and effective way we need a simple and effective way to combine them.

slide-28
SLIDE 28

Chaining gadgets

slide-29
SLIDE 29

Chaining gadgets

… by hand is like playing Tetris

With very ugly blocks Each gadget set defines custom ISA

g g

We have better scores that at...

slide-30
SLIDE 30

Chaining gadgets

slide-31
SLIDE 31

Chaining gadgets

Hence we have decided to Hence we have decided to bring in some help...

slide-32
SLIDE 32

The Wolf

A ROP compiler for gadget

t ith id ff t sets with side‐effects

Very basic language Allows for easy ROPperies on

ARM devices ARM devices

slide-33
SLIDE 33

Living with side‐effects

“allowread”: specifies readable memory

p y ranges

“allowcorrupt”: expendable memory allowcorrupt : expendable memory

ranges [corruption may occur here]

[corruption may occur here] protect: registers must stay invariant

[ d l l d d]

[SP and PC implicitly guarded]

slide-34
SLIDE 34

Statements

(multi‐)assignment

( ) g

Conditional goto statement Call statement (calling lib functions) Call statement (calling lib functions) Data definitions

Labels for data/code

Labels for data/code

slide-35
SLIDE 35

Multi‐assignment

Example from PWN2OWN payload: p p y

(r0 r1 r2) << | (mem[sockloc] sin SIZE SIN) (r0, r1, r2) <<_| (mem[sockloc], sin, SIZE_SIN)

targets memory read constant targets memory read constant assignment operator data reference

slide-36
SLIDE 36

Loops

l b l( l l )

define label for conditional jump

label(clear_loop) r1 = 256 (mem[r0], r2, r1) << | (0, (3*r1) & 255, r1-1)

conditional jump

( [ ], , ) _| ( , ( ) , ) r0 = r0+4 gotoifnz(r1, clear_loop)

RHS may contain arithmetic logical RHS may contain arithmetic‐logical calculations:

{ * / % ^ | & } {+,‐,*,/, %, ^, |, &, <<, >>}

slide-37
SLIDE 37

Hired help: STP

  • Mr. Wolf is a high‐level problem solver:

he likes to delegate

Menial work: let someone else do it In this case STP [Simple Theorem Prover] [Simple Theorem Prover]

slide-38
SLIDE 38

What is STP?

Constraint solver for problems involving bit‐

p g vectors and arrays

Open‐source written by Vijay Ganesh Open source, written by Vijay Ganesh Used for model‐checking, theorem proving,

EXE etc EXE, etc.

Gives Boolean answer whether formula is

f bl f satisfiable & assignment if it is

slide-39
SLIDE 39

STP formulae

Just a bunch of assertions in QF ABV _ Simple example:

BITVECTOR(4)

Simple example:

x0 : BITVECTOR(4); ... x9 : BITVECTOR(4);

ASSERT (BVPLUS(4 BVMULT(4 x0 0hex6) 0hex0 0hex0

ASSERT (BVPLUS(4,BVMULT(4,x0, 0hex6), 0hex0, 0hex0,

  • BVMULT(4,x3, 0hex7), BVMULT(4,x4, 0hex4),
  • BVMULT(4,x5, 0hex6), BVMULT(4,x6, 0hex4),
  • 0hex0 0hex0 BVMULT(4 x9 0hex8) 0hex0) = 0hex7);
  • 0hex0, 0hex0, BVMULT(4,x9, 0hex8),0hex0) 0hex7);
slide-40
SLIDE 40

High‐level algorithm

For multi‐assignments:

1

Find all gadgets assigning to targets For multi‐assignments:

1.

Find all gadgets assigning to targets

2.

Verify constraints for each (protect/memread/memcorrupt) (protect/memread/memcorrupt)

3.

Find all gadgets for expressions on RHS Ch i i d

4.

Chain expression gadgets

5.

Connect LHS and RHS

slide-41
SLIDE 41

Notes on chaining algorithm

Chaining for arithmetic/logical expressions

g / g p may use registers/memory locations for temporary results temporary results

Multi‐assignments give us freedom

Algorithm sometimes may fail because

Algorithm sometimes may fail because

constraints cannot be satisfied [insufficient d ] gadgets]

slide-42
SLIDE 42

K got the payload, now?

You could test it on a jailbroken phone You could test it on a jailbroken phone

  • Does not match reality!
  • No code signing for instance

No code signing for instance

  • Still an option if exploit reliability is not

i your primary concern

slide-43
SLIDE 43

K got the payload, now?

You could test it on a developer phone You could test it on a developer phone

  • Have a small application to reproduce a

“ROP scenario” ROP scenario

  • Depending on the application you’re

t ti th db li i diff t targeting the sandbox policy is different

  • Still closer to reality
slide-44
SLIDE 44

Simple plan

  • Allocate a buffer on the heap
  • Fill the buffer with the shellcode
  • Fill the buffer with the shellcode
  • Point the stack pointer to the beginning
  • f the stack
  • Execute the payload

Execute the payload

  • Restore
slide-45
SLIDE 45

Future work

  • Port to other platforms (eg: x86)
  • Abstract language to describe gadgets
  • Abstract language to describe gadgets
  • Try to avoid “un‐decidable” constraints
  • Make it more flexible to help when

ASLR is in place ASLR is in place

slide-46
SLIDE 46

Thanks for your time

Questions?