master canary forging by yuki koike - code blue 2015

55
Master Canary Forging A new exploitation method to bypass stack canaries

Upload: code-blue

Post on 15-Apr-2017

366 views

Category:

Software


4 download

TRANSCRIPT

Page 1: Master Canary Forging by Yuki Koike - CODE BLUE 2015

Master Canary ForgingA new exploitation method to bypass stack canaries

Page 2: Master Canary Forging by Yuki Koike - CODE BLUE 2015

Who am I?

● 小池 悠生(Koike Yuki)○ a 16-year-old student

● I had been fascinated with CTF○ DEF CON 2014 Finalist○ CODEGATE Junior 2015 Winner○ now focusing on real world bug hunting and

exploitation techniques

Page 3: Master Canary Forging by Yuki Koike - CODE BLUE 2015

Agenda

● Motivation● Stack Canary● Previous Bypass Techniques● Master Canary Forging● Evaluation and Countermeasures

Page 4: Master Canary Forging by Yuki Koike - CODE BLUE 2015

Agenda

● Motivation● Stack Canary● Previous Bypass Techniques● Master Canary Forging● Evaluation and Countermeasures

Page 5: Master Canary Forging by Yuki Koike - CODE BLUE 2015

Motivation

● I love ROP○ so I love Stack Based Buffer Overflows○ and hate Stack Canaries

● Stack Canaries can be strong protection○ It is worth finding ways to bypass them○ Are there any good methods?

Page 6: Master Canary Forging by Yuki Koike - CODE BLUE 2015

Agenda

● Motivation● Stack Canary● Previous Bypass Techniques● Master Canary Forging● Evaluation and Countermeasures

Page 7: Master Canary Forging by Yuki Koike - CODE BLUE 2015

Stack Canary

● For preventing BOF attacks○ Detect if the return address was overwritten

■ Kill the process if it has been tampered○ Design an “indicator”

■ The value of it should be changed before and after BOF occurred

Page 8: Master Canary Forging by Yuki Koike - CODE BLUE 2015

Stack Canary

return address

frame pointer

local variables

Page 9: Master Canary Forging by Yuki Koike - CODE BLUE 2015

● Append an “indicator” to a stack frame

Stack Canary

return address

frame pointer

canary 0xdeadbeef

local variables

Page 10: Master Canary Forging by Yuki Koike - CODE BLUE 2015

● When BOF occurs...

Stack Canary

canaryoverwritten

Page 11: Master Canary Forging by Yuki Koike - CODE BLUE 2015

● The attack will be detected since the value changed

Stack Canary

modified0x41414141

Page 12: Master Canary Forging by Yuki Koike - CODE BLUE 2015

● The attack will be detected since the value changed

Stack Canary

modified0x41414141

Not 0xdeadbeefAttack Detected

Page 13: Master Canary Forging by Yuki Koike - CODE BLUE 2015

Stack Canary

● For preventing BOF attacks○ Detect if the return address was overwritten

■ Kill the process if it has been tampered○ Design a “indicator”

■ The value of it should be changed before and after BOF occurred

Page 14: Master Canary Forging by Yuki Koike - CODE BLUE 2015

Stack Canary

● For preventing BOF attacks○ Detect if the return address was overwritten

■ Kill the process if it has been tampered○ Design a “indicator”

■ The value of it should be changed before and after BOF occurred● Can this be ensured??

Page 15: Master Canary Forging by Yuki Koike - CODE BLUE 2015

● The attack won’t be detected unless the value changed

Stack Canary

modified0xdeadbeef

Page 16: Master Canary Forging by Yuki Koike - CODE BLUE 2015

● The attack won’t be detected unless the value changed

Stack Canary

modified0xdeadbeef

return address

becomes any value

Page 17: Master Canary Forging by Yuki Koike - CODE BLUE 2015

● The attack won’t be detected unless the value changed

Stack Canary

⇒ACE(Arbitrary Code Execution)

Page 18: Master Canary Forging by Yuki Koike - CODE BLUE 2015

Stack Canary

● Types of Stack Canaries○ Random

■ hide the original value from attackers■ randomly generate values when the

program starts○ Terminator

■ should include something like ‘\0’.■ It is hard for attackers to fit the

overwritten value to the original value.

Page 19: Master Canary Forging by Yuki Koike - CODE BLUE 2015

Stack Canary

● Comparing a master canary and a canary on a stack

Page 20: Master Canary Forging by Yuki Koike - CODE BLUE 2015

Agenda

● Motivation● Stack Canary● Previous Bypass Techniques● Master Canary Forging● Evaluation and Countermeasures

Page 21: Master Canary Forging by Yuki Koike - CODE BLUE 2015

● ex1.c

method #1: avoid __stack_chk_fail

#include <stdio.h>void bof(int (*print)(const char *)) { char buf[16]; scanf("%s", buf); print(buf);}int main(void) { bof(puts);}

Page 22: Master Canary Forging by Yuki Koike - CODE BLUE 2015

● ex1.c

#include <stdio.h>void bof(int (*print)(const char *)) { char buf[16]; scanf("%s", buf); print(buf);}int main(void) { bof(puts);}

method #1: avoid __stack_chk_fail

return addressframe pointer

canary

local variables

arguments

Page 23: Master Canary Forging by Yuki Koike - CODE BLUE 2015

● ex1.c

method #1: avoid __stack_chk_fail

overwritten

arguments

#include <stdio.h>void bof(int (*print)(const char *)) { char buf[16]; scanf("%s", buf); print(buf);}int main(void) { bof(puts);}

Page 24: Master Canary Forging by Yuki Koike - CODE BLUE 2015

● ex1.c

method #1: avoid __stack_chk_fail

overwritten

arguments

#include <stdio.h>void bof(int (*print)(const char *)) { char buf[16]; scanf("%s", buf); print(buf);}int main(void) { bof(puts);} a function pointer && an argument

Page 25: Master Canary Forging by Yuki Koike - CODE BLUE 2015

● ex2.c

method #2: leak a canary

#include <stdio.h>

int main(void) { char buf[16]; scanf("%s", buf); printf(buf); fread(buf, sizeof(char), 32, stdin);}

Page 26: Master Canary Forging by Yuki Koike - CODE BLUE 2015

● ex2.c

method #2: leak a canary

#include <stdio.h>

int main(void) { char buf[16]; scanf("%s", buf); printf(buf); fread(buf, sizeof(char), 32, stdin);}

format string bug

Page 27: Master Canary Forging by Yuki Koike - CODE BLUE 2015

method #2: leak a canary

$ gdb ./ex2 -q(gdb) b 4Breakpoint 1 at 0x8048532: file ex2.c, line 4.(gdb) rBreakpoint 1, main () at ex2.c:44 scanf("%s", buf);(gdb) x/12xw $esp0xffffce60: 0xffffd129 0x0000002f 0x0804a000 0x080485e20xffffce70: 0x00000001 0xffffcf34 0xffffcf3c 0xf7e3539d0xffffce80: 0xf7faa3c4 0xf7ffd000 0x0804859b 0x48d09200(gdb) c%11$x48d09200

Page 28: Master Canary Forging by Yuki Koike - CODE BLUE 2015

● Where do canaries fail in these methods?○ method #1: avoid __stack_chk_fail

■ when detecting or terminating attacks○ method #2: leak a canary

■ the canary value on the stack

The essence of bypass methods

Page 29: Master Canary Forging by Yuki Koike - CODE BLUE 2015

● Where do canaries fail in these methods?○ method #1: avoid __stack_chk_fail

■ when detecting or terminating attacks○ method #2: leak a canary

■ the canary value on the stack○ method #3: overwrite the master canary

■ the original value(master canary)

The essence of bypass methods

Page 30: Master Canary Forging by Yuki Koike - CODE BLUE 2015

Agenda

● Motivation● Stack Canary● Previous Bypass Techniques● Master Canary Forging● Evaluation and Countermeasures

Page 31: Master Canary Forging by Yuki Koike - CODE BLUE 2015

● Following assumption:■ Linux Kernel 3.19■ glibc 2.21■ ASLR enabled

Master Canary Forging

Page 32: Master Canary Forging by Yuki Koike - CODE BLUE 2015

● Where is the master canary located?○ Let’s read glibc

Master Canary Forging

static voidsecurity_init (void){ /* Set up the stack checker's canary. */ uintptr_t stack_chk_guard = _dl_setup_stack_chk_guard (_dl_random);#ifdef THREAD_SET_STACK_GUARD THREAD_SET_STACK_GUARD (stack_chk_guard);#else __stack_chk_guard = stack_chk_guard;#endif

Page 33: Master Canary Forging by Yuki Koike - CODE BLUE 2015

● Where is the master canary located?○ Let’s read glibc

Master Canary Forging

static voidsecurity_init (void){ /* Set up the stack checker's canary. */ uintptr_t stack_chk_guard = _dl_setup_stack_chk_guard (_dl_random);#ifdef THREAD_SET_STACK_GUARD THREAD_SET_STACK_GUARD (stack_chk_guard);#else __stack_chk_guard = stack_chk_guard;#endif

Being assigned here

Page 34: Master Canary Forging by Yuki Koike - CODE BLUE 2015

● Where is the master canary located?○ Let’s read glibc

Master Canary Forging

static voidsecurity_init (void){ /* Set up the stack checker's canary. */ uintptr_t stack_chk_guard = _dl_setup_stack_chk_guard (_dl_random);#ifdef THREAD_SET_STACK_GUARD THREAD_SET_STACK_GUARD (stack_chk_guard);#else __stack_chk_guard = stack_chk_guard;#endif

Page 35: Master Canary Forging by Yuki Koike - CODE BLUE 2015

● Where is the master canary located?○ THREAD_SET_STACK_GUARD

■ defined in 7 architectures■ stores the canary in TLS(thread local storage)■ If not defined, the canary is stored in .bss

Master Canary Forging

Page 36: Master Canary Forging by Yuki Koike - CODE BLUE 2015

● To overwrite the master canary○ When it lies in .bss

■ It is just “Arbitrary Memory Write”

Master Canary Forging

Page 37: Master Canary Forging by Yuki Koike - CODE BLUE 2015

● To overwrite the master canary○ When it lies in .bss

■ It is just “Arbitrary Memory Write”○ Then, how about when it lies in TLS?

Master Canary Forging

Page 38: Master Canary Forging by Yuki Koike - CODE BLUE 2015

● To overwrite the master canary○ When it lies in .bss

■ It is just “Arbitrary Memory Write”○ Then, how about when it lies in TLS?

■ In the first place, where is TLS allocated?

Master Canary Forging

Page 39: Master Canary Forging by Yuki Koike - CODE BLUE 2015

● Where is TLS?○ Let’s read glibc

Master Canary Forging

void * internal_function _dl_allocate_tls_storage (void){ void *result; size_t size = GL(dl_tls_static_size);#if TLS_DTV_AT_TP size += (TLS_PRE_TCB_SIZE + GL(dl_tls_static_align) - 1) & ~(GL(dl_tls_static_align) - 1);#endif /* Allocate a correctly aligned chunk of memory. */ result = __libc_memalign (GL(dl_tls_static_align), size);

Page 40: Master Canary Forging by Yuki Koike - CODE BLUE 2015

● Where is TLS?○ _dl_allocate_tls_storage is responsible for allocation

■ Inside, __libc_memalign is called● __libc_memalign calls mmap

○ So in brief, TLS is created somewhere by mmap■ ASLR makes it difficult to overwrite that area

Master Canary Forging

Page 41: Master Canary Forging by Yuki Koike - CODE BLUE 2015

● One of the characterics of areas allocated by mmap:○ The areas are always adjacent to some region

Master Canary Forging

Page 42: Master Canary Forging by Yuki Koike - CODE BLUE 2015

● Mapped Area Based Buffer Overflow

Master Canary Forging

target area

Page 43: Master Canary Forging by Yuki Koike - CODE BLUE 2015

● Mapped Area Based Buffer Overflow○ create a new area by invoking mmap○ The new area and the target should be successive

Master Canary Forging

mapped area

target area

Page 44: Master Canary Forging by Yuki Koike - CODE BLUE 2015

● Mapped Area Based Buffer Overflow○ create a new area by invoking mmap○ The new area and the target should be successive○ cause BOF in the new area○ With enough size of BOF, the target area can be

overwritten

Master Canary Forging

overwritten

Page 45: Master Canary Forging by Yuki Koike - CODE BLUE 2015

● Mapped Area Based Buffer Overflow○ This seems to be able to overwrite the master canary○ Wait, can attackers invoke mmap?

Master Canary Forging

Page 46: Master Canary Forging by Yuki Koike - CODE BLUE 2015

● Mapped Area Based Buffer Overflow○ This seems to be able to overwrite the master canary○ Wait, can attackers invoke mmap?

■ YES

Master Canary Forging

Page 47: Master Canary Forging by Yuki Koike - CODE BLUE 2015

● Mapped Area Based Buffer Overflow○ This seems to be able to overwrite the master canary○ Wait, can attackers invoke mmap?

■ YES■ malloc

Master Canary Forging

Page 48: Master Canary Forging by Yuki Koike - CODE BLUE 2015

● Mapped Area Based Buffer Overflow○ This seems to be able to overwrite the master canary○ Wait, can attackers invoke mmap?

■ YES■ malloc■ “When allocating blocks of memory larger than

MMAP_THRESHOLD bytes, the glibc malloc() implementation allocates the memory as a private anonymous mapping using mmap(2).”

Master Canary Forging

Page 49: Master Canary Forging by Yuki Koike - CODE BLUE 2015

● Mapped Area Based Buffer Overflow○ following 2 conditions required:

■ Attackers can control allocation■ Heap Based BOF occurs

Master Canary Forging

Page 50: Master Canary Forging by Yuki Koike - CODE BLUE 2015

1. Overwrite the master canarya. When it is located in .bss

i. Use an “Arbitrary Memory Write”b. When it is located in TLS

i. Use a mapped area based BOF2. Cause a stack based BOF

Master Canary Forging

Page 51: Master Canary Forging by Yuki Koike - CODE BLUE 2015

Agenda

● Motivation● Stack Canary● Previous Bypass Techniques● Master Canary Forging● Evaluation and Countermeasures

Page 52: Master Canary Forging by Yuki Koike - CODE BLUE 2015

● Evaluation○ NOT so useful

■ It requires 2 types of vulnerabilities■ Heap Based BOF is usually sufficient for ACE

○ Mapped Area Based BOF itself is useful■ Sometimes a function pointer array is in TLS

Evaluation and Countermeasures

Page 53: Master Canary Forging by Yuki Koike - CODE BLUE 2015

● Countermeasures○ Use random XOR canaries

■ canary = master canary ^ stack pointer○ Establish a guard page

Evaluation and Countermeasures

Page 55: Master Canary Forging by Yuki Koike - CODE BLUE 2015

Thank you for listeningPlease ask me anything