CMPSC 497 More on Overflow Vulnerabilities Trent Jaeger Systems - - PowerPoint PPT Presentation

cmpsc 497 more on overflow vulnerabilities
SMART_READER_LITE
LIVE PREVIEW

CMPSC 497 More on Overflow Vulnerabilities Trent Jaeger Systems - - PowerPoint PPT Presentation

Systems and Internet Infrastructure Security Network and Security Research Center Department of Computer Science and Engineering Pennsylvania State University, University Park PA CMPSC 497 More on Overflow Vulnerabilities Trent Jaeger


slide-1
SLIDE 1

Systems and Internet Infrastructure Security (SIIS) Laboratory Page

Systems and Internet Infrastructure Security

Network and Security Research Center Department of Computer Science and Engineering Pennsylvania State University, University Park PA

1

CMPSC 497 More on Overflow Vulnerabilities

Trent Jaeger Systems and Internet Infrastructure Security (SIIS) Lab Computer Science and Engineering Department Pennsylvania State University

slide-2
SLIDE 2

Systems and Internet Infrastructure Security (SIIS) Laboratory Page 2

Overflow Vulnerabilities

  • Despite knowledge of buffer overflows for over

40 years, they have not been eliminated

  • This is partly due to the wide variety of exploit
  • ptions
  • Variety of targets: can exploit more than return

addresses – any code addresses or data

  • Variety of uses: can exploit on read and write
  • Variety of exploits: can inject or reuse code
  • Variety of workarounds: current defenses are

incomplete

slide-3
SLIDE 3

Systems and Internet Infrastructure Security (SIIS) Laboratory Page 3

Available Defenses

  • Available defenses do not prevent all options
  • Stackguard: A canary before return address on stack
  • DEP or W xor X: Stack memory is not executable
  • This combination of defenses prevents the classic

buffer overflow attack, but there are many options

  • Plus, both defenses may be disabled by other

attacks

slide-4
SLIDE 4

Systems and Internet Infrastructure Security (SIIS) Laboratory Page 4

StackGuard Limitations

  • Obvious limitation: only protects the return address
  • What about other local variables?

int authenticated = 0; char packet[1000]; while (!authenticated) { PacketRead(packet); if (Authenticate(packet)) authenticated = 1; } if (authenticated) ProcessPacket(packet);

slide-5
SLIDE 5

Systems and Internet Infrastructure Security (SIIS) Laboratory Page

  • Packet overflows overwrite the authenticated value

Function Initialization: Stacks

stack frame CANARY

  • ld ebp

authenticated

packet ret

slide-6
SLIDE 6

Systems and Internet Infrastructure Security (SIIS) Laboratory Page 6

StackGuard Limitations

  • Obvious limitation: only protects the return address
  • What is the exploit?

int authenticated = 0; char packet[1000]; while (!authenticated) { PacketRead(packet); if (Authenticate(packet)) authenticated = 1; } if (authenticated) ProcessPacket(packet);

slide-7
SLIDE 7

Systems and Internet Infrastructure Security (SIIS) Laboratory Page 7

StackGuard Limitations

  • Obvious limitation: only protects the return address
  • What about other local variables?
  • Of course, there are also other data on the stack that

may also require protection

  • Code addresses – function pointers
  • Data that impacts control flow – as in example
  • Data that may impact operation of program
  • Any ways to address this limitation?
slide-8
SLIDE 8

Systems and Internet Infrastructure Security (SIIS) Laboratory Page 8

StackGuard Limitations

  • Big limitation: Disclosure attacks
  • By performing a buffer “overread”
  • Example is the famous Heartbleed attack against SSL
  • Why is this a problem for Stackguard canaries?

char packet[10]; … // suppose len is adversary controlled strncpy(buf, packet, len); send(fd, buf, len2);

slide-9
SLIDE 9

Systems and Internet Infrastructure Security (SIIS) Laboratory Page

  • Big limitation: Disclosure attacks
  • By performing a buffer “overread”
  • Example is the famous Heartbleed

attack against SSL

  • Why is this a problem for Stackguard?

char packet[10]; … // suppose len is adversary controlled strncpy(buf, packet, len); send(fd, buf, len);

9

StackGuard Limitations

previous stack frame arg ret addr canary

  • ld ebp

packet

slide-10
SLIDE 10

Systems and Internet Infrastructure Security (SIIS) Laboratory Page 10

StackGuard Limitations

  • Big limitation: disclosure attacks
  • By performing a buffer “overread”
  • One may extract the canary values by reading beyond

the end of stack buffers

  • Which would enable the adversary to learn the

(supposedly secret) canary value

  • How would you exploit this?
slide-11
SLIDE 11

Systems and Internet Infrastructure Security (SIIS) Laboratory Page 11

DEP Limitations

  • Big limitation: code injection is not necessary to

construct adversary-controlled exploit code

  • Code reuse attacks
slide-12
SLIDE 12

Systems and Internet Infrastructure Security (SIIS) Laboratory Page 12

  • Vs. Injecting Shell Code

stack frame for main 2 1 ret execve (“/bin/sh”)

  • Remember this exploit
  • The adversary’s goal is

to get execve to run to generate a command shell

  • To do this the adversary

uses execve from libc – i.e., reuses code that is already there

slide-13
SLIDE 13

Systems and Internet Infrastructure Security (SIIS) Laboratory Page 13

Code Reuse

  • How can we invoke execve without code injection?
slide-14
SLIDE 14

Systems and Internet Infrastructure Security (SIIS) Laboratory Page 14

Code Reuse

  • How can we invoke execve without code injection?
  • Call execve directly from return value
  • The difference is subtle, but significant
  • In the original exploit, we wrote the address of execve

into buffer on the stack and modified return address to start executing at buffer

  • I.e., we are executing in the stack memory region
  • Instead, we can modify the return address to point to

execve directly, so we continue to execute code

  • Reusing available code to do what the adversary wants
slide-15
SLIDE 15

Systems and Internet Infrastructure Security (SIIS) Laboratory Page

  • How can we invoke execve without code injection?
  • Use the code directly
  • The difference is subtle, but significant

15

Code Reuse

stack frame for main 2 1 ret execve (“/bin/sh”) stack frame for main execve buffer “/bin/sh” 2

slide-16
SLIDE 16

Systems and Internet Infrastructure Security (SIIS) Laboratory Page 16

Code Reuse

  • How would we use code reuse to disable DEP?
  • Goal is to allow execution of stack memory
slide-17
SLIDE 17

Systems and Internet Infrastructure Security (SIIS) Laboratory Page 17

Code Reuse

  • How would we use code reuse to disable DEP?
  • Goal is to allow execution of stack memory
  • There’s a system call for that

int mprotect(void *addr, size_t len, int prot);

  • Sets protection for region of memory starting at address
  • Invoke this system call to allow execution on stack and

then start executing from the injected code

slide-18
SLIDE 18

Systems and Internet Infrastructure Security (SIIS) Laboratory Page 18

Heap Overflows

  • Another region of memory that may be vulnerable

to overflows is heap memory

  • A buffer overflow of a buffer allocated on the heap is

called a heap overflow

int authenticated = 0; char *packet = (char *)malloc(1000); while (!authenticated) { PacketRead(packet); if (Authenticate(packet)) authenticated = 1; } if (authenticated) ProcessPacket(packet);

slide-19
SLIDE 19

Systems and Internet Infrastructure Security (SIIS) Laboratory Page 19

Morris Worm

  • Robert Morris, a 23 doctoral student from Cornell
  • Wrote a small (99 line) program
  • Launched on November 3, 1988
  • Simply disabled the Internet
  • Used a buffer overflow in a program called fingerd
  • To get adversary-controlled code running
  • Then spread to other hosts – cracked passwords

and leverage open LAN configurations

  • Covered its tracks (set is own process name to sh,

prevented accurate cores, re-forked itself)

CSE543 - Introduction to Computer and Network Security Page

Heap Overflows

  • Overflows on heap also possible
  • “Classical” heap overflow

corrupts metadata

  • Heap metadata maintains chunk

size, previous and next pointers, ...

  • Heap metadata is inline with

heap data

  • And waits for heap management

functions (malloc, free) to write corrupted metadata to target locations

X

char *packet = malloc(1000) ptr[1000] = ‘M’;

slide-20
SLIDE 20

Systems and Internet Infrastructure Security (SIIS) Laboratory Page

Process Address Space

  • Text: static code
  • Data: also called heap
  • static variables
  • dynamically allocated data

(malloc, new)

  • Stack: program

execution stacks

Text Data Stack

lower memory address higher memory address

CSE543 - Introduction to Computer and Network Security Page

Heap Overflows

  • http://www.sans.edu/student-files/presentations/heap_overflows_notes.pdf

X

  • Heap allocators maintain a doubly-linked list of allocated

and free chunks

  • malloc() and free() modify this list
slide-21
SLIDE 21

Systems and Internet Infrastructure Security (SIIS) Laboratory Page

Program Stack

  • For implementing procedure calls and returns
  • Keep track of program execution and state by

storing

  • local variables
  • arguments to the called procedure (callee)
  • return address of the calling procedure (caller)
  • ...

CSE543 - Introduction to Computer and Network Security Page

Heap Overflows

  • free() removes a chunk from allocated list
  • By overflowing chunk2, attacker controls bk and fd
  • Controls both where and what data is written!
  • Arbitrarily change memory (e.g., function pointers)

X

chunk2->bk->fd = chunk2->fd chunk2->fd->bk = chunk2->bk chunk2->bk->fd = chunk2->fd chunk2->fd->bk = chunk2->bk

slide-22
SLIDE 22

Systems and Internet Infrastructure Security (SIIS) Laboratory Page

Program Stack

*Slide by Robert Seacord

CSE543 - Introduction to Computer and Network Security Page

Heap Overflows

  • By overflowing chunk2, attacker controls bk and fd
  • Controls both where and what data is written!
  • Assign chunk2->fd to value to want to write
  • Assign chunk2->bk to address X (where you want to write)
  • Less an offset of the fd field in the structure
  • Free() removes a chunk from allocated list
  • What’s the result?

X

chunk2->bk->fd = chunk2->fd chunk2->fd->bk = chunk2->bk

slide-23
SLIDE 23

Systems and Internet Infrastructure Security (SIIS) Laboratory Page

Stack Frames

  • Stack grows from high mem to low mem addresses
  • The stack pointer points to the top of the stack
  • ESP in Intel architectures
  • The frame pointer points to the end of the current

frame

  • also called the base pointer
  • EBP in Intel architectures
  • The stack is modified during
  • function calls, function initializations, returning from

a function

CSE543 - Introduction to Computer and Network Security Page

Heap Overflows

  • By overflowing chunk2, attacker controls bk and fd
  • Controls both where and what data is written!
  • Assign chunk2->fd to value to want to write
  • Assign chunk2->bk to address X (where you want to write)
  • Less an offset of the fd field in the structure
  • Free() removes a chunk from allocated list
  • What’s the result?
  • Change a memory address to a new pointer value (in data)

X

chunk2->bk->fd = chunk2->fd addrX->fd = value chunk2->fd->bk = chunk2->bk value->bk = addrX

slide-24
SLIDE 24

Systems and Internet Infrastructure Security (SIIS) Laboratory Page

Defenses

  • What would you suggest for defenses to prevent

heap overflows?

  • Heap canaries?
  • DEP or W xor X?
slide-25
SLIDE 25

Systems and Internet Infrastructure Security (SIIS) Laboratory Page

Defenses

  • What would you suggest for defenses to prevent

heap overflows?

  • Heap canaries?
  • Heap canaries would be numerous
  • And prone to disclosure attacks still
  • DEP?
  • Prevents you from assigning a code pointer to an

address directly – value = addr will fail

  • But can replace function pointers, cause they are data
  • Code reuse is still possible through heap overflows
slide-26
SLIDE 26

Systems and Internet Infrastructure Security (SIIS) Laboratory Page 26

Buffer Overflows

  • A buffer overflow occurs when data is written
  • utside of the boundaries of the memory allocated

to a particular data structure

  • Happens when buffer boundaries are neglected

and unchecked

  • Can be exploited to modify (memory after buffer)
  • Stack: return address, local variables, function pointers,

etc.

  • Heap: data structures and metadata (next time)

CSE543 - Introduction to Computer and Network Security Page

Heap Overflow Defenses

  • Separate data and metadata
  • e.g., OpenBSD’s allocator (Variation of PHKmalloc)
  • Sanity checks during heap management
  • Added to GNU libc 2.3.5
  • Randomization
  • Q. What are analogous defenses for stack overflows?

X

free(chunk2) --> assert(chunk2->fd->bk == chunk2) assert(chunk2->bk->fd == chunk2)

slide-27
SLIDE 27

Systems and Internet Infrastructure Security (SIIS) Laboratory Page 28

Code Reuse in General

  • Code reuse attacks can be employed more

generally to enable adversaries to execute existing code under their control

  • Termed “return-oriented attacks”
  • by Hovav Shacham and his colleagues
  • Next few slides are Prof Shacham’s
slide-28
SLIDE 28

Systems and Internet Infrastructure Security (SIIS) Laboratory Page 29

Code Reuse in General

  • Code reuse attacks can be employed more

generally to enable adversaries to execute existing code under their control

  • Termed “return-oriented attacks”

CSE543 - Introduction to Computer and Network Security Page

Return-Oriented Programming

X

slide-29
SLIDE 29

Systems and Internet Infrastructure Security (SIIS) Laboratory Page 30

Code Reuse in General

  • Code reuse attacks can be employed more

generally to enable adversaries to execute existing code under their control

  • Termed “return-oriented attacks”

CSE543 - Introduction to Computer and Network Security Page

ROP Thesis

X

slide-30
SLIDE 30

Systems and Internet Infrastructure Security (SIIS) Laboratory Page 31

Code Reuse in General

  • Code reuse attacks can be employed more

generally to enable adversaries to execute existing code under their control

  • Termed “return-oriented attacks”

CSE543 - Introduction to Computer and Network Security Page

ROP vs return-to-libc

X

slide-31
SLIDE 31

Systems and Internet Infrastructure Security (SIIS) Laboratory Page 32

Code Reuse in General

  • Code reuse attacks can be employed more

generally to enable adversaries to execute existing code under their control

  • Termed “return-oriented attacks”

CSE543 - Introduction to Computer and Network Security Page

Machine Instructions

X

slide-32
SLIDE 32

Systems and Internet Infrastructure Security (SIIS) Laboratory Page 33

Code Reuse in General

  • Code reuse attacks can be employed more

generally to enable adversaries to execute existing code under their control

  • Termed “return-oriented attacks”

CSE543 - Introduction to Computer and Network Security Page

ROP Execution

X

slide-33
SLIDE 33

Systems and Internet Infrastructure Security (SIIS) Laboratory Page 34

Code Reuse in General

  • Code reuse attacks can be employed more

generally to enable adversaries to execute existing code under their control

  • Termed “return-oriented attacks”

ROP Example

  • Use ESP as program counter

– E.g., Store 5 at address 0x8048000 (without introducing new code)

%eax = %ebx = 0x8048000 = Registers Memory Code Stack

G1 5 jmp G2 Return Address

buf

0x8048000 jump G3

. . .

pop %eax ret pop %ebx ret movl %eax, (%ebx) ret

G2: G2 G3 G1: G3:

slide-34
SLIDE 34

Systems and Internet Infrastructure Security (SIIS) Laboratory Page 35

Code Reuse in General

  • Code reuse attacks can be employed more

generally to enable adversaries to execute existing code under their control

  • Termed “return-oriented attacks”

ROP Example

  • Use ESP as program counter

– E.g., Store 5 at address 0x8048000 (without introducing new code)

%eax = %ebx = 0x8048000 = Registers Memory Code Stack

G1 5 jmp G2 Return Address

buf

0x8048000 jump G3

. . .

pop %eax ret pop %ebx ret movl %eax, (%ebx) ret

G2: G2 G3 G1: G3:

5

slide-35
SLIDE 35

Systems and Internet Infrastructure Security (SIIS) Laboratory Page 36

Code Reuse in General

  • Code reuse attacks can be employed more

generally to enable adversaries to execute existing code under their control

  • Termed “return-oriented attacks”

ROP Example

  • Use ESP as program counter

– E.g., Store 5 at address 0x8048000 (without introducing new code)

%eax = %ebx = 0x8048000 = Registers Memory Code Stack

G1 5 jmp G2 Return Address

buf

0x8048000 jump G3

. . .

pop %eax ret pop %ebx ret movl %eax, (%ebx) ret

G2: G2 G3 G1: G3:

5

slide-36
SLIDE 36

Systems and Internet Infrastructure Security (SIIS) Laboratory Page 37

Code Reuse in General

  • Code reuse attacks can be employed more

generally to enable adversaries to execute existing code under their control

  • Termed “return-oriented attacks”

ROP Example

  • Use ESP as program counter

– E.g., Store 5 at address 0x8048000 (without introducing new code)

%eax = %ebx = 0x8048000 = Registers Memory Code Stack

G1 5 jmp G2 Return Address

buf

0x8048000 jump G3

. . .

pop %eax ret pop %ebx ret movl %eax, (%ebx) ret

G2: G2 G3 G1: G3:

5 0x8048000

slide-37
SLIDE 37

Systems and Internet Infrastructure Security (SIIS) Laboratory Page 38

Code Reuse in General

  • Code reuse attacks can be employed more

generally to enable adversaries to execute existing code under their control

  • Termed “return-oriented attacks”

ROP Example

  • Use ESP as program counter

– E.g., Store 5 at address 0x8048000 (without introducing new code)

%eax = %ebx = 0x8048000 = Registers Memory Code Stack

G1 5 jmp G2 Return Address

buf

0x8048000 jump G3

. . .

pop %eax ret pop %ebx ret movl %eax, (%ebx) ret

G2: G2 G3 G1: G3:

5 0x8048000

ROP Example

  • Use ESP as program counter

– E.g., Store 5 at address 0x8048000 (without introducing new code)

%eax = %ebx = 0x8048000 = Registers Memory Code Stack

G1 5 jmp G2 Return Address

buf

0x8048000 jump G3

. . .

pop %eax ret pop %ebx ret movl %eax, (%ebx) ret

G2: G2 G3 G1: G3:

5 0x8048000

slide-38
SLIDE 38

Systems and Internet Infrastructure Security (SIIS) Laboratory Page 39

Code Reuse in General

  • Code reuse attacks can be employed more

generally to enable adversaries to execute existing code under their control

  • Termed “return-oriented attacks”

ROP Example

  • Use ESP as program counter

– E.g., Store 5 at address 0x8048000 (without introducing new code)

%eax = %ebx = 0x8048000 = Registers Memory Code Stack

G1 5 jmp G2 Return Address

buf

0x8048000 jump G3

. . .

pop %eax ret pop %ebx ret movl %eax, (%ebx) ret

G2: G2 G3 G1: G3:

5 0x8048000

slide-39
SLIDE 39

Systems and Internet Infrastructure Security (SIIS) Laboratory Page 40

Code Reuse in General

  • Code reuse attacks can be employed more

generally to enable adversaries to execute existing code under their control

  • Termed “return-oriented attacks”

ROP Example

  • Use ESP as program counter

– E.g., Store 5 at address 0x8048000 (without introducing new code)

%eax = %ebx = 0x8048000 = Registers Memory Code Stack

G1 5 jmp G2 Return Address

buf

0x8048000 jump G3

. . .

pop %eax ret pop %ebx ret movl %eax, (%ebx) ret

G2: G2 G3 G1: G3:

5 0x8048000 5

slide-40
SLIDE 40

Systems and Internet Infrastructure Security (SIIS) Laboratory Page 41

Code Reuse in General

  • Code reuse attacks can be employed more

generally to enable adversaries to execute existing code under their control

  • Termed “return-oriented attacks”

CSE543 - Introduction to Computer and Network Security Page

Building ROP Functionality

X

slide-41
SLIDE 41

Systems and Internet Infrastructure Security (SIIS) Laboratory Page 42

Code Reuse in General

  • Code reuse attacks can be employed more

generally to enable adversaries to execute existing code under their control

  • Termed “return-oriented attacks”

CSE543 - Introduction to Computer and Network Security Page

Building ROP Functionality

X

slide-42
SLIDE 42

Systems and Internet Infrastructure Security (SIIS) Laboratory Page 43

Code Reuse in General

  • Code reuse attacks can be employed more

generally to enable adversaries to execute existing code under their control

  • Termed “return-oriented attacks”

CSE543 - Introduction to Computer and Network Security Page

Building ROP Functionality

X

slide-43
SLIDE 43

Systems and Internet Infrastructure Security (SIIS) Laboratory Page 44

Return-oriented Programming

  • What can we do with return-oriented

programming?

  • Anything any other program can do
  • How do we know?
slide-44
SLIDE 44

Systems and Internet Infrastructure Security (SIIS) Laboratory Page 45

Return-oriented Programming

  • What can we do with return-oriented

programming?

  • Anything any other program can do
  • How do we know? Turing completeness
  • A language is Turing complete if it has (loosely)
  • Conditional branching
  • Can change memory arbitrarily
  • Both are possible in ROP
slide-45
SLIDE 45

Systems and Internet Infrastructure Security (SIIS) Laboratory Page 46

Take Away

  • Buffer overflows provide a wide variety of options

for adversaries, depending on the software flaw

  • The result is that defenses that have become

widely available are only effective against a fraction

  • f the possible exploit options
  • And can be disabled by some types of exploits
  • We discussed variants of the classic buffer
  • verflow attack
  • Disclosure attacks, core reuse attacks, heap overflows,

and return-oriented attacks