Lecture 10 Return-oriented programming Stephen Checkoway - - PowerPoint PPT Presentation

lecture 10 return oriented programming
SMART_READER_LITE
LIVE PREVIEW

Lecture 10 Return-oriented programming Stephen Checkoway - - PowerPoint PPT Presentation

Lecture 10 Return-oriented programming Stephen Checkoway University of Illinois at Chicago Based on slides by Bailey, Brumley, and Miller ROP Overview Idea: We forge shellcode out of existing application logic gadgets Requirements:


slide-1
SLIDE 1

Lecture 10 – Return-oriented programming

Stephen Checkoway University of Illinois at Chicago Based on slides by Bailey, Brumley, and Miller

slide-2
SLIDE 2

ROP Overview

  • Idea: We forge shellcode out of existing application logic gadgets
  • Requirements:

vulnerability + gadgets + some unrandomized code

  • History:
  • No code randomized: Code injection
  • DEP enabled by default: ROP attacks using libc gadgets published 2007
  • ROP assemblers, compilers, shellcode generators
  • ASLR library load points: ROP attacks use .text segment gadgets
  • Today: all major OSes/compilers support position-independent executables

2

slide-3
SLIDE 3

3

Image by Dino Dai Zovi

slide-4
SLIDE 4

ROP Programming

  • 1. Disassemble code (library or program)
  • 2. Identify useful code sequences (usually ending in ret)
  • 3. Assemble the useful sequences into reusable gadgets*
  • 4. Assemble gadgets into desired shellcode

* Forming gadgets is mostly useful when constructing complicated return-oriented shellcode by hand

4

slide-5
SLIDE 5

A note on terminology

  • When ROP was invented in 2007
  • Sequences of code ending in ret were the basic building blocks
  • Multiple sequences and data are assembled into reusable gadgets
  • Subsequently
  • A gadget came to refer to any sequence of code ending in a ret
  • In 2010
  • ROP without returns (e.g., code sequences ending in call or jmp)
slide-6
SLIDE 6

There are many semantically equivalent ways to achieve the same net shellcode effect

6

slide-7
SLIDE 7

... v2 ... v1 a1: mov eax, [esp] a2: mov ebx, [esp+8] a3: mov [ebx], eax Implementation 1

Equivalence

7

Desired Logic Stack

Mem[v2] = v1

esp

slide-8
SLIDE 8

Constant store gadget

8

Desired Logic a5 v2 a3 v1 Stack

Mem[v2] = v1

a1: pop eax; a2: ret a3: pop ebx; a4: ret a5: mov [ebx], eax

Implementation 2

Suppose a5 and a3 on stack

esp

eax ebx eip v1 a1

slide-9
SLIDE 9

Constant store gadget

9

Desired Logic a5 v2 a3 v1 Stack

Mem[v2] = v1

a1: pop eax; a2: ret a3: pop ebx; a4: ret a5: mov [ebx], eax

Implementation 2

esp

eax ebx eip v1 a1 a3

slide-10
SLIDE 10

Constant store gadget

10

Desired Logic a5 v2 a3 v1 Stack

Mem[v2] = v1

a1: pop eax; a2: ret a3: pop ebx; a4: ret a5: mov [ebx], eax

Implementation 2

esp

eax ebx eip v1 a3 v2

slide-11
SLIDE 11

Constant store gadget

11

Desired Logic a5 v2 a3 v1 Stack

Mem[v2] = v1

a1: pop eax; a2: ret a3: pop ebx; a4: ret a5: mov [ebx], eax

Implementation 2

esp

eax ebx eip v1 a4 a5 v2

slide-12
SLIDE 12

Constant store gadget

12

Desired Logic a5 v2 a3 v1 Stack

Mem[v2] = v1

a1: pop eax; a2: ret a3: pop ebx; a4: ret a5: mov [ebx], eax

Implementation 2

esp

eax ebx eip v1 a5 v2

slide-13
SLIDE 13

Equivalence

13

Desired Logic a3 v2 a2 v1 Stack

Mem[v2] = v1

a1: mov eax, [esp] a2: mov ebx, [esp+8] a3: mov [ebx], eax Implementation 1 a1: pop eax; ret a2: pop ebx; ret a3: mov [ebx], eax Implementation 2

semantically equivalent

esp

slide-14
SLIDE 14

Return-Oriented Programming

  • Find needed instruction

gadgets at addresses a1, a2, and a3 in existing code

  • Overwrite stack to execute a1,

a2, and then a3

14

Desired Shellcode

Mem[v2] = v1

… argv argc return addr caller’s ebp buf (64 bytes) argv[1] buf %ebp %esp

slide-15
SLIDE 15

Return-Oriented Programming

15

Desired Shellcode

Mem[v2] = v1

… argv argc return addr caller’s ebp buf (64 bytes) argv[1] buf %ebp %esp a3 v2 a2 v1 a1

a1: pop eax; ret a2: pop ebx; ret a3: mov [ebx], eax

Desired store executed!

slide-16
SLIDE 16

What else can we do?

  • Depends on the code we have access to
  • Usually: Arbitrary Turing-complete behavior
  • Arithmetic
  • Logic
  • Conditionals and loops
  • Subroutines
  • Calling existing functions
  • System calls
  • Sometimes: More limited behavior
  • Often enough for straight-line code and system calls
slide-17
SLIDE 17

Comparing ROP to normal programming

Normal programming ROP Instruction pointer eip esp No-op nop ret Unconditional jump jmp address set esp to address of gadget Conditional jump jnz address set esp to address of gadget if some condition is met Variables memory and registers mostly memory Inter-instruction (inter-gadget) register and memory interaction minimal, mostly explicit; e.g., adding two registers only affects the destination register can be complex; e.g., adding two registers may involve modifying many registers which impacts other gadgets

slide-18
SLIDE 18

Return-oriented conditionals

  • Processors support instructions that conditionally change the PC
  • On x86
  • Jcc family: jz, jnz, jl, jle, etc. 33 in total
  • loop, loope, loopne
  • Based on condition codes mostly; and on ecx for some
  • On MIPS
  • beq, bne, blez, etc.
  • Based on comparison of registers
  • Processors generally don’t support for conditionally changing the

stack pointer (with some exceptions)

slide-19
SLIDE 19

We want conditional jumps

  • Unconditional jump addr
  • popl %eax

ret

  • movl %eax, %esp

ret

slide-20
SLIDE 20

We want conditional jumps

  • Unconditional jump addr
  • popl %eax

ret

  • movl %eax, %esp

ret

esp ... &next gadget addr

slide-21
SLIDE 21

We want conditional jump

  • Unconditional jump addr
  • popl %eax

ret

  • movl %eax, %esp

ret

esp ... &next gadget addr

slide-22
SLIDE 22

We want conditional jump

  • Unconditional jump addr
  • popl %eax

ret

  • movl %eax, %esp

ret

esp ... &next gadget addr eax

slide-23
SLIDE 23

We want conditional jumps

  • Unconditional jump addr
  • popl %eax

ret

  • movl %eax, %esp

ret

esp ... &next gadget addr eax

slide-24
SLIDE 24

We want conditional jumps

  • Unconditional jump addr
  • popl %eax

ret

  • movl %eax, %esp

ret

... &next gadget addr eax, esp

slide-25
SLIDE 25

We want conditional jumps

  • Unconditional jump addr
  • popl %eax

ret

  • movl %eax, %esp

ret

... &next gadget addr eax esp

slide-26
SLIDE 26

We want conditional jumps

  • Unconditional jump addr
  • popl %eax

ret

  • movl %eax, %esp

ret

  • Conditional jump addr, one way
  • Conditionally set a register to 0 or 0xffffffff
  • Perform a logical AND with the register and an offset
  • Add the result to esp
slide-27
SLIDE 27

Conditionally set a register to 0 or 0xffffffff

  • Compare registers eax and ebx and set ecx to
  • 0xffffffff if eax < ebx
  • 0 if eax >= ebx
  • Ideally we would find a sequence like

cmpl %ebx, %eax set carry flag cf according to eax - ebx sbbl %ecx, %ecx ecx ← ecx - ecx - cf; or ecx ← -cf ret

  • Unlikely to find this; instead look for cmp; ret and sbb; ret sequences
slide-28
SLIDE 28

Performing a logical AND with a constant

  • Pop the constant into a register using pop; ret
  • Use an and; ret sequence
slide-29
SLIDE 29

Updating the stack pointer

  • Use an add esp; ret sequence
slide-30
SLIDE 30

Putting it together

... &next gadget 37 addr 42

  • ffset

cmpl %ebx, %eax ret sbbl %ecx, %ecx ret popl %eax ret andl %ecx, %eax ret addl %eax, %esp ret popl %edx ret movl %eax, %esp ret

Conditional jump gadget Load constant in edx gadget Unconditional jump gadget

Useful instruction sequences

slide-31
SLIDE 31

... &next gadget 37 addr 42

  • ffset

Putting it together

cmpl %ebx, %eax ret sbbl %ecx, %ecx ret popl %eax ret andl %ecx, %eax ret addl %eax, %esp ret popl %edx ret movl %eax, %esp ret Register Value eax 10 ebx 20 ecx 108 edx 17 esp

slide-32
SLIDE 32

... &next gadget 37 addr 42

  • ffset

Putting it together

cmpl %ebx, %eax ret sbbl %ecx, %ecx ret popl %eax ret andl %ecx, %eax ret addl %eax, %esp ret popl %edx ret movl %eax, %esp ret Register Value eax 10 ebx 20 ecx 108 edx 17 esp

slide-33
SLIDE 33

... &next gadget 37 addr 42

  • ffset

Putting it together

cmpl %ebx, %eax ret sbbl %ecx, %ecx ret popl %eax ret andl %ecx, %eax ret addl %eax, %esp ret popl %edx ret movl %eax, %esp ret Register Value eax 10 ebx 20 ecx 108 edx 17 esp cf = 1

slide-34
SLIDE 34

... &next gadget 37 addr 42

  • ffset

Putting it together

cmpl %ebx, %eax ret sbbl %ecx, %ecx ret popl %eax ret andl %ecx, %eax ret addl %eax, %esp ret popl %edx ret movl %eax, %esp ret Register Value eax 10 ebx 20 ecx 108 edx 17 esp cf = 1

slide-35
SLIDE 35

... &next gadget 37 addr 42

  • ffset

Putting it together

cmpl %ebx, %eax ret sbbl %ecx, %ecx ret popl %eax ret andl %ecx, %eax ret addl %eax, %esp ret popl %edx ret movl %eax, %esp ret Register Value eax 10 ebx 20 ecx 0xffffffff edx 17 esp cf = 1

slide-36
SLIDE 36

... &next gadget 37 addr 42

  • ffset

Putting it together

cmpl %ebx, %eax ret sbbl %ecx, %ecx ret popl %eax ret andl %ecx, %eax ret addl %eax, %esp ret popl %edx ret movl %eax, %esp ret Register Value eax 10 ebx 20 ecx 0xffffffff edx 17 esp

slide-37
SLIDE 37

... &next gadget 37 addr 42

  • ffset

Putting it together

cmpl %ebx, %eax ret sbbl %ecx, %ecx ret popl %eax ret andl %ecx, %eax ret addl %eax, %esp ret popl %edx ret movl %eax, %esp ret Register Value eax 20 = offset ebx 20 ecx 0xffffffff edx 17 esp

slide-38
SLIDE 38

... &next gadget 37 addr 42

  • ffset

Putting it together

cmpl %ebx, %eax ret sbbl %ecx, %ecx ret popl %eax ret andl %ecx, %eax ret addl %eax, %esp ret popl %edx ret movl %eax, %esp ret Register Value eax 20 = offset ebx 20 ecx 0xffffffff edx 17 esp

slide-39
SLIDE 39

... &next gadget 37 addr 42

  • ffset

Putting it together

cmpl %ebx, %eax ret sbbl %ecx, %ecx ret popl %eax ret andl %ecx, %eax ret addl %eax, %esp ret popl %edx ret movl %eax, %esp ret Register Value eax 20 = offset ebx 20 ecx 0xffffffff edx 17 esp

slide-40
SLIDE 40

... &next gadget 37 addr 42

  • ffset

Putting it together

cmpl %ebx, %eax ret sbbl %ecx, %ecx ret popl %eax ret andl %ecx, %eax ret addl %eax, %esp ret popl %edx ret movl %eax, %esp ret Register Value eax 20 = offset ebx 20 ecx 0xffffffff edx 17 esp

slide-41
SLIDE 41

... &next gadget 37 addr 42

  • ffset

Putting it together

cmpl %ebx, %eax ret sbbl %ecx, %ecx ret popl %eax ret andl %ecx, %eax ret addl %eax, %esp ret popl %edx ret movl %eax, %esp ret Register Value eax 20 = offset ebx 20 ecx 0xffffffff edx 17 esp

slide-42
SLIDE 42

... &next gadget 37 addr 42

  • ffset

Putting it together

cmpl %ebx, %eax ret sbbl %ecx, %ecx ret popl %eax ret andl %ecx, %eax ret addl %eax, %esp ret popl %edx ret movl %eax, %esp ret Register Value eax 20 = offset ebx 20 ecx 0xffffffff edx 17 esp

slide-43
SLIDE 43

... &next gadget 37 addr 42

  • ffset

Putting it together

cmpl %ebx, %eax ret sbbl %ecx, %ecx ret popl %eax ret andl %ecx, %eax ret addl %eax, %esp ret popl %edx ret movl %eax, %esp ret Register Value eax 20 = offset ebx 20 ecx 0xffffffff edx 37 esp

slide-44
SLIDE 44

... &next gadget 37 addr 42

  • ffset

Putting it together

cmpl %ebx, %eax ret sbbl %ecx, %ecx ret popl %eax ret andl %ecx, %eax ret addl %eax, %esp ret popl %edx ret movl %eax, %esp ret Register Value eax 20 = offset ebx 20 ecx 0xffffffff edx 37 esp

slide-45
SLIDE 45

... &next gadget 37 addr 42

  • ffset

And again!

cmpl %ebx, %eax ret sbbl %ecx, %ecx ret popl %eax ret andl %ecx, %eax ret addl %eax, %esp ret popl %edx ret movl %eax, %esp ret Register Value eax 500 ebx 20 ecx 108 edx 17 esp

slide-46
SLIDE 46

... &next gadget 37 addr 42

  • ffset

And again!

cmpl %ebx, %eax ret sbbl %ecx, %ecx ret popl %eax ret andl %ecx, %eax ret addl %eax, %esp ret popl %edx ret movl %eax, %esp ret Register Value eax 500 ebx 20 ecx 108 edx 17 esp

slide-47
SLIDE 47

... &next gadget 37 addr 42

  • ffset

And again!

cmpl %ebx, %eax ret sbbl %ecx, %ecx ret popl %eax ret andl %ecx, %eax ret addl %eax, %esp ret popl %edx ret movl %eax, %esp ret Register Value eax 500 ebx 20 ecx 108 edx 17 esp cf = 0

slide-48
SLIDE 48

... &next gadget 37 addr 42

  • ffset

And again!

cmpl %ebx, %eax ret sbbl %ecx, %ecx ret popl %eax ret andl %ecx, %eax ret addl %eax, %esp ret popl %edx ret movl %eax, %esp ret Register Value eax 500 ebx 20 ecx 108 edx 17 esp cf = 0

slide-49
SLIDE 49

... &next gadget 37 addr 42

  • ffset

And again!

cmpl %ebx, %eax ret sbbl %ecx, %ecx ret popl %eax ret andl %ecx, %eax ret addl %eax, %esp ret popl %edx ret movl %eax, %esp ret Register Value eax 500 ebx 20 ecx edx 17 esp

slide-50
SLIDE 50

... &next gadget 37 addr 42

  • ffset

And again!

cmpl %ebx, %eax ret sbbl %ecx, %ecx ret popl %eax ret andl %ecx, %eax ret addl %eax, %esp ret popl %edx ret movl %eax, %esp ret Register Value eax 500 ebx 20 ecx edx 17 esp

slide-51
SLIDE 51

... &next gadget 37 addr 42

  • ffset

And again!

cmpl %ebx, %eax ret sbbl %ecx, %ecx ret popl %eax ret andl %ecx, %eax ret addl %eax, %esp ret popl %edx ret movl %eax, %esp ret Register Value eax 20 = offset ebx 20 ecx edx 17 esp

slide-52
SLIDE 52

... &next gadget 37 addr 42

  • ffset

And again!

cmpl %ebx, %eax ret sbbl %ecx, %ecx ret popl %eax ret andl %ecx, %eax ret addl %eax, %esp ret popl %edx ret movl %eax, %esp ret Register Value eax 20 = offset ebx 20 ecx edx 17 esp

slide-53
SLIDE 53

... &next gadget 37 addr 42

  • ffset

And again!

cmpl %ebx, %eax ret sbbl %ecx, %ecx ret popl %eax ret andl %ecx, %eax ret addl %eax, %esp ret popl %edx ret movl %eax, %esp ret Register Value eax ebx 20 ecx edx 17 esp

slide-54
SLIDE 54

... &next gadget 37 addr 42

  • ffset

And again!

cmpl %ebx, %eax ret sbbl %ecx, %ecx ret popl %eax ret andl %ecx, %eax ret addl %eax, %esp ret popl %edx ret movl %eax, %esp ret Register Value eax ebx 20 ecx edx 17 esp

slide-55
SLIDE 55

... &next gadget 37 addr 42

  • ffset

And again!

cmpl %ebx, %eax ret sbbl %ecx, %ecx ret popl %eax ret andl %ecx, %eax ret addl %eax, %esp ret popl %edx ret movl %eax, %esp ret Register Value eax ebx 20 ecx edx 17 esp

slide-56
SLIDE 56

... &next gadget 37 addr 42

  • ffset

And again!

cmpl %ebx, %eax ret sbbl %ecx, %ecx ret popl %eax ret andl %ecx, %eax ret addl %eax, %esp ret popl %edx ret movl %eax, %esp ret Register Value eax ebx 20 ecx edx 17 esp

slide-57
SLIDE 57

... &next gadget 37 addr 42

  • ffset

And again!

cmpl %ebx, %eax ret sbbl %ecx, %ecx ret popl %eax ret andl %ecx, %eax ret addl %eax, %esp ret popl %edx ret movl %eax, %esp ret Register Value eax ebx 20 ecx edx 42 esp

slide-58
SLIDE 58

... &next gadget 37 addr 42

  • ffset

And again!

cmpl %ebx, %eax ret sbbl %ecx, %ecx ret popl %eax ret andl %ecx, %eax ret addl %eax, %esp ret popl %edx ret movl %eax, %esp ret Register Value eax ebx 20 ecx edx 42 esp

slide-59
SLIDE 59

... &next gadget 37 addr 42

  • ffset

And again!

cmpl %ebx, %eax ret sbbl %ecx, %ecx ret popl %eax ret andl %ecx, %eax ret addl %eax, %esp ret popl %edx ret movl %eax, %esp ret Register Value eax addr ebx 20 ecx edx 42 esp

slide-60
SLIDE 60

... &next gadget 37 addr 42

  • ffset

And again!

cmpl %ebx, %eax ret sbbl %ecx, %ecx ret popl %eax ret andl %ecx, %eax ret addl %eax, %esp ret popl %edx ret movl %eax, %esp ret Register Value eax addr ebx 20 ecx edx 42 esp

slide-61
SLIDE 61

... &next gadget 37 addr 42

  • ffset

And again!

cmpl %ebx, %eax ret sbbl %ecx, %ecx ret popl %eax ret andl %ecx, %eax ret addl %eax, %esp ret popl %edx ret movl %eax, %esp ret Register Value eax addr ebx 20 ecx edx 42 esp

slide-62
SLIDE 62

... &next gadget 37 addr 42

  • ffset

And again!

cmpl %ebx, %eax ret sbbl %ecx, %ecx ret popl %eax ret andl %ecx, %eax ret addl %eax, %esp ret popl %edx ret movl %eax, %esp ret Register Value eax addr ebx 20 ecx edx 42 esp

slide-63
SLIDE 63

Compare

Register Value eax 10 ebx 20 ecx 108 edx 17 Register Value eax addr ebx 20 ecx edx 42 Register Value eax 500 ebx 20 ecx 108 edx 17 Register Value eax 20 ebx 20 ecx 0xffffffff edx 37 if (eax < ebx) edx = 37; else edx = 42;