david brumley carnegie mellon university credit: some slides from ed schwartz

43
David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

Upload: nyla-pawl

Post on 14-Dec-2015

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

David BrumleyCarnegie Mellon University

Credit: Some slides from Ed Schwartz

Page 2: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

2

Control Flow Hijack: Always control + computation

computation + control

shellcode (aka payload) padding &buf

Return-oriented programming (ROP): shellcode without code injection

Page 3: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

3

Motivation: Return-to-libc Attack

Overwrite return address with address of libc function• setup fake return address

and argument(s)• ret will “call” libc function

No injected code!

argv

argc

return addr

caller’s ebp

buf(64 bytes)

argv[1]

buf

%ebp

%esp

ptr to “/bin/sh”

&system

ret transfers control to system, which finds arguments on stack

Page 4: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

4

Attack Surface: Linux

2/1/2012

Randomized

Stack

Heap

Unrandomized

Program Image

Libc

Page 5: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

5

Attack Surface: Windows

2/1/2012

Randomized

Stack

Heap

Unrandomized

Program Image

Libc

Page 6: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

6

How do we exploit DEP-defended systems where the text section isn’t randomized?

Page 7: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

7

argv

argc

return addr

caller’s ebp

buf(64 bytes)

argv[1]

buf

%ebp

%esp

ptr to “/bin/sh”

&system

What if we don’t know the absolute address to “/bin/sh”?

(objdump gives addresses, but we don’t know ASLR constants)

Find an instruction sequence, aka gadget, to calculate the

address at exploit time.

Page 8: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

8

argv

argc

return addr

caller’s ebp

buf“/bin/sh”

argv[1]

buf

gadgets to compute

ptr to “/bin/sh”

&system

Idea!Get a copy of ESP to calculate address of “/bin/sh” on randomized stack.

This overcomes ASLR because ASLR only protects against knowing absolute addresses.

Computed “/bin/sh”

Writes

Page 9: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

9

Return Oriented Programming Techniques

1. Return chaining2. Semantic equivalence3. ROP on Windows

Page 10: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

10

Return Chaining• Suppose we want to call 2

functions in our exploit: foo(arg1, arg2) bar(arg3, arg4)

• Stack unwinds up• First function returns into

code to advance stack pointer– e.g., pop; pop; ret

arg4

arg3

&(pop-pop-ret)

bar

arg2

arg1

&(pop-pop-ret)

foo

Overwritten ret addr

What does this do?

Page 11: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

11

Return Chaining

• When foo is executing, &pop-pop-ret is at the saved EIP slot.

• When foo returns, it executes pop-pop-ret to clear up arg1 (pop), arg2 (pop), and transfer control to bar (ret)

arg4

arg3

&(pop-pop-ret)

bar

arg2

arg1

&(pop-pop-ret)

foo

Page 12: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

12

There are many semantically equivalent

ways to achieve the same net shellcode effect

Page 13: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

13

...

v2

...

v1

a1: mov eax, [esp]

a2: mov ebx, [esp+8]

a3: mov [ebx], eax

Implementation 1

Equivalence

Desired Logic

Stack

Mem[v2] = v1

esp

Page 14: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

14

Gadgets

Desired Logic

a5

v2

a3

v1

Stack

Mem[v2] = v1

a1: pop eax;

a2: ret

a3: pop ebx;

a4: ret

a5: mov [ebx], eaxImplementation 2

Suppose a5 and a3 on

stackesp

eax

ebx

eip

v1

a1

Page 15: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

15

Gadgets

Desired Logic

a5

v2

a3

v1

Stack

Mem[v2] = v1

a1: pop eax;

a2: ret

a3: pop ebx;

a4: ret

a5: mov [ebx], eaxImplementation 2

esp

eax

ebx

eip

v1

a1a3

Page 16: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

16

Gadgets

Desired Logic

a5

v2

a3

v1

Stack

Mem[v2] = v1

a1: pop eax;

a2: ret

a3: pop ebx;

a4: ret

a5: mov [ebx], eaxImplementation 2

esp

eax

ebx

eip

v1

a3

v2

Page 17: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

17

Gadgets

Desired Logic

a5

v2

a3

v1

Stack

Mem[v2] = v1

a1: pop eax;

a2: ret

a3: pop ebx;

a4: ret

a5: mov [ebx], eaxImplementation 2

esp

eax

ebx

eip

v1

a4a5

v2

Page 18: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

18

Gadgets

Desired Logic

a5

v2

a3

v1

Stack

Mem[v2] = v1

a1: pop eax;

a2: ret

a3: pop ebx;

a4: ret

a5: mov [ebx], eaxImplementation 2

esp

eax

ebx

eip

v1

a5

v2

Gadget 1

Gadget 2

Page 19: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

19

Equivalence

Desired Logic

a3

v2

a2

v1

Stack

Mem[v2] = v1

a1: mov eax, [esp]

a2: mov ebx, [esp+8]

a3: mov [ebx], eaxImplementation 1

a1: pop eax; ret

a2: pop ebx; ret

a3: mov [ebx], eaxImplementation 2

semantically equivalent

esp

“Gadgets”

Page 20: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

20

Gadgets• A gadget is a set of instructions for carrying out

a semantic action– mov, add, etc.

• Gadgets typically have a number of instructions– One instruction = native instruction set– More instructions = synthesize <- ROP

• Gadgets in ROP generally (but not always) end in return

Page 21: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

21

ROP Programming

1. Disassemble code2. Identify useful code

sequences as gadgets3. Assemble gadgets into

desired shellcode

Page 22: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

22Image by Dino Dai Zovi

Page 23: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

23

ROP Overview

Idea: We forge shell code out of existing application logic gadgets

Requirements: vulnerability + gadgets + some unrandomized code(we need to know the addresses of gadgets)

Page 24: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

24

Return-Oriented Programming (ROP)

• Find needed instruction gadgets at addresses a1, a2, and a3 in existing code

• Overwrite stack to execute a1, a2, and then a3

Desired Shellcode

Mem[v2] = v1

argv

argc

return addr

caller’s ebp

buf(64 bytes)

argv[1]

buf

%ebp

%esp

Page 25: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

25

Return-Oriented Programming (ROP)

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!

Page 26: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

26

Quizvoid foo(char *input){ char buf[512]; ... strcpy (buf, input); return;}a1: add eax, 0x80; pop %ebp; ret

a2: pop %eax; ret

Draw a stack diagram and

ROP exploit to pop a value

0xBBBBBBBBinto eax and

add 0x80.

Known Gadget

s

ret instr

Page 27: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

27

Quizvoid foo(char *input){ char buf[512]; ... strcpy (buf, input); return;}a1: add eax, 0x80; pop %ebp; ret

a2: pop %eax; ret

<data for pop ebp>

a1

0xBBBBBBBB

a2

saved ret

saved ebp

buf

AAAAA ... a2 0xBBBBBBBB a1

Overwrite buf

gadget 1 + data

gadget 2

Page 28: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

28

ROPing WindowsLPVOID WINAPI VirtualProtect(

LPVOID lpAddress, // dynamically determined base addr to pages to change

SIZE_T dwSize, // size of the region in bytesDWORD DWORD flNewProtect, // 0x40 =

EXECUTE_READWRITEDWORD flProtect // A ptr to a variable for prev. arg

);

VirtualProtect() can un-DEP a memory region

Page 29: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

29

VirtualProtect DiagramflProtect

(a ptr to mem)

flNewProtect(static)

dwSize(dynamic)

lpAddress(dynamic)

addr of your shellcode to

unprotect

&VirtualProtect

Craft lpAddress

Craft dwSize

LPVOID WINAPI VirtualProtect(LPVOID lpAddress, SIZE_T dwSize, DWORD DWORD flNewProtect, DWORD flProtect

);

Page 30: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

30

ROPing Windows: An Example Exploit (pre-Win 8)Shellcode

Padding/NOPS

7. Change value of ESP back to where pointer to VirtualProtect is, then ret

6. Gadget to overwrite placeholder for Param 4

5. Gadget to overwrite placeholder for Param 3 with value

4. Gadget to overwrite placeholder for Param 2 with value

3. Gadget to overwrite placeholder for Param 1 with value (Pointer to shellcode = saved ESP + offset)

• lpAddress placeholder: base addr to pages to change• dwSize placeholder: size of the region in bytes• flNewProtect placeholder: EXECUTE_READWRITE• flProtect: A ptr to a variable for prev. argPointer to VirtualProtect (static) and space for params:

2. gadgets to get stack pointer and save it to a register (push %esp; pop %eax; ret) & jump below the parameters (add esp, offset; ret)

From https://www.corelan.be/index.php/2010/06/16/exploit-writing-tutorial-part-10-chaining-dep-with-rop-the-rubikstm-cube/

Gadgets to run shellcode (not shown)

esp

1. Stack Pivot

Page 31: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

31

Stack PivotsPointing esp to controlled data

Fact: Functions often access arguments with respect to esp

Defn: A stack pivot redirects esp at attacker-controlled data

Example: Attacker controls heap data pointed to be ESI. One stack pivot may be: xchg esi, esp; retNow esp points to the attacker-controlled data.

Page 32: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

32

Other References

Thorough introduction:https://www.corelan.be/index.php/2010/06/16/exploit-writing-tutorial-part-10-chaining-dep-with-rop-the-rubikstm-cube/

Adopting to Win8:http://vulnfactory.org/blog/2011/09/21/defeating-windows-8-rop-mitigation/

Page 33: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

33

Disassembling Code

Page 34: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

34

Recall: Execution Model

ProcessMemory

Stack

Heap

Processor

Fetch, decode, execute

read and write

CodeEIP

Page 35: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

Disassemblyuser@box:~/l2$ objdump -d ./file...00000000 <even_sum>: 0: 55 push %ebp 1: 89 e5 mov %esp,%ebp 3: 83 ec 10 sub $0x10,%esp 6: 8b 45 0c mov 0xc(%ebp),%eax 9: 03 45 08 add 0x8(%ebp),%eax c: 03 45 10 add 0x10(%ebp),%eax f: 89 45 fc mov %eax,0xfffffffc(%ebp) 12: 8b 45 fc mov 0xfffffffc(%ebp),%eax 15: 83 e0 01 and $0x1,%eax 18: 84 c0 test %al,%al 1a: 74 03 je 1f <even_sum+0x1f> 1c: ff 45 fc incl 0xfffffffc(%ebp) 1f: 8b 45 fc mov 0xfffffffc(%ebp),%eax 22: c9 leave 23: c3 ret

Address

Executable instructions

Disassemble

Page 36: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

36

Linear-Sweep Disassembly

DisassemblerEIP

0x55 0x89 0xe5 0x83 0xec 0x10 ... 0xc9

Executable Instructions

Algorithm:1. Decode Instruction2. Advance EIP by len

push ebp

Page 37: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

37

Linear-Sweep Disassembly

DisassemblerEIP

0x55 0x89 0xe5 0x83 0xec 0x10 ... 0xc9

Executable Instructions

push ebp... push ebpmov %esp, %ebp

Page 38: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

38

Linear-Sweep Disassembly

DisassemblerEIP

0x55 0x89 0xe5 0x83 0xec 0x10 ... 0xc9

Executable Instructions

push ebppush ebpmov %esp, %ebp

Algorithm:1. Decode Instruction2. Advance EIP by len

Note we don’t follow jumps: we just increment

by instruction length

Page 39: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

39

Disassemble from any address

0x55 0x89 0xe5 0x83 0xec 0x10 ... 0xc9

push ebp

NormalExecutionmov %esp, %ebp

DisassemblerEIP

It’s perfectly valid to start disassembling from any address.

All byte sequences will have a unique disassembly

Page 40: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

40

Recursive Descent

• Follow jumps and returns instead of linear sweep

• Undecidable: indirect jumps– Where does jmp *eax go?

Page 41: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

41

ROP Programming

1. Disassemble code2. Identify useful code

sequences ending in ret as gadgets

3. Assemble gadgets into desired shellcode

Disassemble all sequences

ending in ret

Page 42: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

42

ROP: Shacham et al.

1. Disassemble code2. Identify useful code

sequences as gadgets ending in ret

3. Assemble gadgets into desired shellcode

Automatic

Manual

Then Q came along and automated

Page 43: David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz

43

Questions?