Buffer Overflow overflows Defenses and other memory safety - - PowerPoint PPT Presentation

buffer
SMART_READER_LITE
LIVE PREVIEW

Buffer Overflow overflows Defenses and other memory safety - - PowerPoint PPT Presentation

This time We will continue By looking at Buffer Overflow overflows Defenses and other memory safety vulnerabilities Everything youve always wanted to know about gdb but were too afraid to ask Overflow defenses Other memory


slide-1
SLIDE 1

This time

  • Everything you’ve always wanted to know about

gdb but were too afraid to ask

  • Overflow defenses
  • Other memory safety vulnerabilities

Buffer

  • verflows

We will continue

and other memory safety vulnerabilities

By looking at

Overflow

Defenses

slide-2
SLIDE 2

gdb: your new best friend

i f i r x/<n> <addr> b <function>
 s Set a breakpoint at <function>
 step through execution (into calls) Examine <n> bytes of memory
 starting at address <addr> Show info about registers
 (%ebp, %eip, %esp, etc.) Show info about the current frame
 (prev. frame, locals/args, %ebp/%eip)

slide-3
SLIDE 3

Recall our challenges

  • Putting code into the memory (no zeroes)
  • Getting %eip to point to our code (dist buff to stored eip)
  • Finding the return address (guess the raw addr)

How can we make these even more difficult?

slide-4
SLIDE 4

Detecting overflows with canaries

00 00 00 00

buffer

Text

%eip

...

&arg1 %eip

%ebp

slide-5
SLIDE 5

Detecting overflows with canaries

00 00 00 00

buffer

Text

%eip

...

&arg1 %eip

%ebp

slide-6
SLIDE 6

Detecting overflows with canaries

00 00 00 00

buffer

Text

%eip

...

&arg1 %eip

%ebp

… 02 8d e2 10

canary

slide-7
SLIDE 7

Detecting overflows with canaries

00 00 00 00

buffer

Text

%eip

...

&arg1 %eip

%ebp

… 02 8d e2 10

canary

nop nop nop …

0xbdf

\x0f \x3c \x2f ...

slide-8
SLIDE 8

Detecting overflows with canaries

00 00 00 00

buffer

Text

%eip

...

&arg1 %eip

%ebp

… 02 8d e2 10

canary

nop nop nop …

0xbdf

\x0f \x3c \x2f ...

slide-9
SLIDE 9

Detecting overflows with canaries

00 00 00 00

buffer

Text

%eip

...

&arg1 %eip

%ebp

… 02 8d e2 10

canary

nop nop nop …

0xbdf

\x0f \x3c \x2f ...

Not the expected value: abort

slide-10
SLIDE 10

Detecting overflows with canaries

00 00 00 00

buffer

Text

%eip

...

&arg1 %eip

%ebp

… 02 8d e2 10

canary

nop nop nop …

0xbdf

\x0f \x3c \x2f ...

Not the expected value: abort What value should the canary have?

slide-11
SLIDE 11

Canary values

  • 1. Terminator canaries (CR, LF, NULL, -1)
  • Leverages the fact that scanf etc. don’t allow these
  • 2. Random canaries
  • Write a new random value @ each process start
  • Save the real value somewhere in memory
  • Must write-protect the stored value
  • 3. Random XOR canaries
  • Same as random canaries
  • But store canary XOR some control info, instead

From StackGuard [Wagle & Cowan]

slide-12
SLIDE 12

Recall our challenges

  • Putting code into the memory (no zeroes)
  • Option: Make this detectable with canaries

  • Getting %eip to point to our code(dist buff to stored eip)
  • Finding the return address (guess the raw addr)

How can we make these even more difficult? More next time…

slide-13
SLIDE 13

Return-to-libc

&arg1 %eip

%ebp

00 00 00 00

buffer

Text

%eip

...

nop nop nop …

nop sled

0xbdf

good
 guess padding

\x0f \x3c \x2f ...

malicious code libc

slide-14
SLIDE 14

Return-to-libc

&arg1 %eip

%ebp

00 00 00 00

buffer

Text

%eip

...

nop nop nop …

nop sled

0xbdf

good
 guess padding libc

slide-15
SLIDE 15

Return-to-libc

&arg1 %eip

%ebp

00 00 00 00

buffer

Text

%eip

...

0xbdf

good
 guess padding libc

slide-16
SLIDE 16

Return-to-libc

&arg1 %eip

%ebp

00 00 00 00

buffer

Text

%eip

...

padding libc

slide-17
SLIDE 17

Return-to-libc

&arg1 %eip

%ebp

00 00 00 00

buffer

Text

%eip

...

padding libc

exec()

... ...

printf()

...

“/bin/sh”

libc

slide-18
SLIDE 18

Return-to-libc

&arg1 %eip

%ebp

00 00 00 00

buffer

Text

%eip

...

padding

0x17f

known
 location libc

exec()

... ...

printf()

...

“/bin/sh”

libc

slide-19
SLIDE 19

Return-to-libc

&arg1 %eip

%ebp

00 00 00 00

buffer

Text

%eip

...

padding

0x17f

known
 location

0x20d

libc

exec()

... ...

printf()

...

“/bin/sh”

libc

slide-20
SLIDE 20

Recall our challenges

  • Putting code into the memory (no zeroes)
  • Option: Make this detectable with canaries

  • Getting %eip to point to our code (dist buff to stored eip)
  • Non-executable stack doesn’t work so well
  • Finding the return address (guess the raw addr)

How can we make these even more difficult?

slide-21
SLIDE 21

Address Space Layout Randomization (ASLR)

  • Basic idea: change the layout of the stack
  • Slow to adopt
  • Linux in 2005
  • Vista in 2007 (off by default for compatibility with
  • lder software
  • OS X in 2007 (for system libraries), 2011 for all apps
  • iOS 4.3 (2011)
  • Android 4.0
  • FreeBSD: no

How would you overcome this as an attacker?

slide-22
SLIDE 22

Overflow defenses summary

  • Putting code into the memory (no zeroes)
  • Option: Make this detectable with canaries

  • Getting %eip to point to our code (dist buff to stored eip)
  • Non-executable stack doesn’t work so well

  • Finding the return address (guess the raw addr)
  • Address Space Layout Randomization (ASLR)
  • Many systems slow to adopt; also, how could you get around this?
  • Good coding practices
slide-23
SLIDE 23

Last time

  • Finish overflow attacks & other vulnerabilities
  • Overflow defenses
  • Everything you’ve always wanted to know about

gdb but were too afraid to ask

Buffer

  • verflows

We continued

and other memory safety vulnerabilities

By looking at

Overflow

Defenses

slide-24
SLIDE 24

This time

Required reading:

“StackGuard: Simple Stack Smash Protection for GCC” Optional reading: “Basic Integer Overflows” “Exploiting Format String Vulnerabilities”

Secure


Code

Writing & testing for

Continuing with

Software

Security

http://nob.cs.ucdavis.edu/bishop/secprog/robust.html

slide-25
SLIDE 25

Cat and mouse

slide-26
SLIDE 26

Cat and mouse

  • Defense: Make stack/heap non-executable to prevent

injection of code

slide-27
SLIDE 27

Cat and mouse

  • Defense: Make stack/heap non-executable to prevent

injection of code

  • Attack response: Return to libc
slide-28
SLIDE 28

Cat and mouse

  • Defense: Make stack/heap non-executable to prevent

injection of code

  • Attack response: Return to libc
  • Defense: Hide the address of desired libc code or

return address using ASLR

slide-29
SLIDE 29

Cat and mouse

  • Defense: Make stack/heap non-executable to prevent

injection of code

  • Attack response: Return to libc
  • Defense: Hide the address of desired libc code or

return address using ASLR

  • Attack response: Brute force search (for 32-bit systems)
  • r information leak (format string vulnerability: later today)
slide-30
SLIDE 30

Cat and mouse

  • Defense: Make stack/heap non-executable to prevent

injection of code

  • Attack response: Return to libc
  • Defense: Hide the address of desired libc code or

return address using ASLR

  • Attack response: Brute force search (for 32-bit systems)
  • r information leak (format string vulnerability: later today)
  • Defense: Avoid using libc code entirely and use code in

the program text instead

slide-31
SLIDE 31

Cat and mouse

  • Defense: Make stack/heap non-executable to prevent

injection of code

  • Attack response: Return to libc
  • Defense: Hide the address of desired libc code or

return address using ASLR

  • Attack response: Brute force search (for 32-bit systems)
  • r information leak (format string vulnerability: later today)
  • Defense: Avoid using libc code entirely and use code in

the program text instead

  • Attack response: Construct needed functionality using

return oriented programming (ROP)

slide-32
SLIDE 32

Return oriented programming (ROP)

slide-33
SLIDE 33

Return-oriented Programming

slide-34
SLIDE 34

Return-oriented Programming

  • Introduced by Hovav Shacham in 2007
  • The Geometry of Innocent Flesh on the Bone:

Return-into-libc without Function Calls (on the x86), CCS’07

slide-35
SLIDE 35

Return-oriented Programming

  • Introduced by Hovav Shacham in 2007
  • The Geometry of Innocent Flesh on the Bone:

Return-into-libc without Function Calls (on the x86), CCS’07

  • Idea: rather than use a single (libc) function to run

your shellcode, string together pieces of existing code, called gadgets, to do it instead

slide-36
SLIDE 36

Return-oriented Programming

  • Introduced by Hovav Shacham in 2007
  • The Geometry of Innocent Flesh on the Bone:

Return-into-libc without Function Calls (on the x86), CCS’07

  • Idea: rather than use a single (libc) function to run

your shellcode, string together pieces of existing code, called gadgets, to do it instead

  • Challenges
  • Find the gadgets you need
  • String them together
slide-37
SLIDE 37

Approach

slide-38
SLIDE 38

Approach

  • Gadgets are instruction groups that end

with ret

slide-39
SLIDE 39

Approach

  • Gadgets are instruction groups that end

with ret

  • Stack serves as the code
  • %esp = program counter
  • Gadgets invoked via ret instruction
  • Gadgets get their arguments via pop,

etc.

  • Also on the stack
slide-40
SLIDE 40

Simple example

0xffffffff 0x00 mov %edx, 5

equivalent to

%edx

slide-41
SLIDE 41

Simple example

0x17f: pop %edx ret 0xffffffff 0x00 Text mov %edx, 5

equivalent to

%edx

Gadget

slide-42
SLIDE 42

Simple example

0x17f: pop %edx ret 5 0x17f 0xffffffff 0x00 Text mov %edx, 5 …

equivalent to

%edx

next gadget

Gadget “Instructions”

slide-43
SLIDE 43

Simple example

0x17f: pop %edx ret 5 0x17f 0xffffffff 0x00 Text mov %edx, 5 …

equivalent to

%eip

%edx

next gadget

%esp

Gadget “Instructions” “program counter”

(ret)

slide-44
SLIDE 44

Simple example

0x17f: pop %edx ret 5 0x17f 0xffffffff 0x00 Text mov %edx, 5 …

equivalent to

%eip

%edx

next gadget

%esp (ret)

slide-45
SLIDE 45

Simple example

0x17f: pop %edx ret 5 0x17f 0xffffffff 0x00 Text mov %edx, 5 …

equivalent to

%eip

%edx

5

next gadget

%esp (ret)

slide-46
SLIDE 46

Simple example

0x17f: pop %edx ret 5 0x17f 0xffffffff 0x00 Text mov %edx, 5 …

equivalent to

%eip

%edx

5

next gadget

%esp (ret)

slide-47
SLIDE 47

Code sequence

0xffffffff 0x00 0x404 … … 5 …

%eax %ebx

… %esp 0x17f: mov %eax, [%esp] mov %ebx, [%esp-8] mov [%ebx], %eax %eip 0x404 Text

slide-48
SLIDE 48

Code sequence

0xffffffff 0x00 0x404 … … 5 …

%eax %ebx

… %esp 0x17f: mov %eax, [%esp] mov %ebx, [%esp-8] mov [%ebx], %eax %eip 0x404 Text 5

slide-49
SLIDE 49

Code sequence

0xffffffff 0x00 0x404 … … 5 …

%eax %ebx

… %esp 0x17f: mov %eax, [%esp] mov %ebx, [%esp-8] mov [%ebx], %eax %eip 0x404 Text 5 0x404

slide-50
SLIDE 50

Code sequence

0xffffffff 0x00 0x404 … … 5 …

%eax %ebx

… %esp 0x17f: mov %eax, [%esp] mov %ebx, [%esp-8] mov [%ebx], %eax %eip 0x404 Text 5 0x404 5

slide-51
SLIDE 51

Equivalent ROP sequence

0xffffffff 0x00 0x404 0x20d 0x21a 5 …

%eax %ebx

… %esp 0x17f: pop %eax ret … 0x20d: pop %ebx ret … 0x21a: mov [%ebx], %eax %eip 0x404 Text

slide-52
SLIDE 52

Equivalent ROP sequence

0xffffffff 0x00 0x404 0x20d 0x21a 5 …

%eax %ebx

… %esp 0x17f: pop %eax ret … 0x20d: pop %ebx ret … 0x21a: mov [%ebx], %eax %eip 0x404 Text 5

slide-53
SLIDE 53

Equivalent ROP sequence

0xffffffff 0x00 0x404 0x20d 0x21a 5 …

%eax %ebx

… %esp 0x17f: pop %eax ret … 0x20d: pop %ebx ret … 0x21a: mov [%ebx], %eax %eip 0x404 Text 5

slide-54
SLIDE 54

Equivalent ROP sequence

0xffffffff 0x00 0x404 0x20d 0x21a 5 …

%eax %ebx

… %esp 0x17f: pop %eax ret … 0x20d: pop %ebx ret … 0x21a: mov [%ebx], %eax %eip 0x404 Text 5 0x404

slide-55
SLIDE 55

Equivalent ROP sequence

0xffffffff 0x00 0x404 0x20d 0x21a 5 …

%eax %ebx

… 0x17f: pop %eax ret … 0x20d: pop %ebx ret … 0x21a: mov [%ebx], %eax %eip 0x404 Text 5 0x404

slide-56
SLIDE 56

Equivalent ROP sequence

0xffffffff 0x00 0x404 0x20d 0x21a 5 …

%eax %ebx

… 0x17f: pop %eax ret … 0x20d: pop %ebx ret … 0x21a: mov [%ebx], %eax %eip 0x404 Text 5 0x404 5

slide-57
SLIDE 57

Image by Dino Dai Zovi

slide-58
SLIDE 58

Whence the gadgets?

slide-59
SLIDE 59

Whence the gadgets?

  • How can we find gadgets to construct an exploit?
slide-60
SLIDE 60

Whence the gadgets?

  • How can we find gadgets to construct an exploit?
  • Automate a search of the target binary for gadgets (look for ret

instructions, work backwards)

  • Cf. https://github.com/0vercl0k/rp
slide-61
SLIDE 61

Whence the gadgets?

  • How can we find gadgets to construct an exploit?
  • Automate a search of the target binary for gadgets (look for ret

instructions, work backwards)

  • Cf. https://github.com/0vercl0k/rp
  • Are there sufficient gadgets to do anything interesting?
slide-62
SLIDE 62

Whence the gadgets?

  • How can we find gadgets to construct an exploit?
  • Automate a search of the target binary for gadgets (look for ret

instructions, work backwards)

  • Cf. https://github.com/0vercl0k/rp
  • Are there sufficient gadgets to do anything interesting?
  • Yes: Shacham found that for significant codebases (e.g.,

libc), gadgets are Turing complete

  • Especially true on x86’s dense instruction set
slide-63
SLIDE 63

Whence the gadgets?

  • How can we find gadgets to construct an exploit?
  • Automate a search of the target binary for gadgets (look for ret

instructions, work backwards)

  • Cf. https://github.com/0vercl0k/rp
  • Are there sufficient gadgets to do anything interesting?
  • Yes: Shacham found that for significant codebases (e.g.,

libc), gadgets are Turing complete

  • Especially true on x86’s dense instruction set
  • Schwartz et al (USENIX Security ’11) have automated

gadget shellcode creation, though not needing/requiring Turing completeness

slide-64
SLIDE 64

Blind ROP

slide-65
SLIDE 65

Blind ROP

  • Defense: Randomizing the location of the code

(by compiling for position independence) on a 64- bit machine makes attacks very difficult

slide-66
SLIDE 66

Blind ROP

  • Defense: Randomizing the location of the code

(by compiling for position independence) on a 64- bit machine makes attacks very difficult

  • Recent, published attacks are often for 32-bit

versions of executables

slide-67
SLIDE 67

Blind ROP

  • Defense: Randomizing the location of the code

(by compiling for position independence) on a 64- bit machine makes attacks very difficult

  • Recent, published attacks are often for 32-bit

versions of executables

  • Attack response: Blind ROP
slide-68
SLIDE 68

Blind ROP

  • Defense: Randomizing the location of the code

(by compiling for position independence) on a 64- bit machine makes attacks very difficult

  • Recent, published attacks are often for 32-bit

versions of executables

  • Attack response: Blind ROP

If server restarts on a crash, but does not re-randomize:

slide-69
SLIDE 69

Blind ROP

  • Defense: Randomizing the location of the code

(by compiling for position independence) on a 64- bit machine makes attacks very difficult

  • Recent, published attacks are often for 32-bit

versions of executables

  • Attack response: Blind ROP

If server restarts on a crash, but does not re-randomize: 1.Read the stack to leak canaries and a return address

slide-70
SLIDE 70

Blind ROP

  • Defense: Randomizing the location of the code

(by compiling for position independence) on a 64- bit machine makes attacks very difficult

  • Recent, published attacks are often for 32-bit

versions of executables

  • Attack response: Blind ROP

If server restarts on a crash, but does not re-randomize: 1.Read the stack to leak canaries and a return address 2.Find gadgets (at run-time) to effect call to write

slide-71
SLIDE 71

Blind ROP

  • Defense: Randomizing the location of the code

(by compiling for position independence) on a 64- bit machine makes attacks very difficult

  • Recent, published attacks are often for 32-bit

versions of executables

  • Attack response: Blind ROP

If server restarts on a crash, but does not re-randomize: 1.Read the stack to leak canaries and a return address 2.Find gadgets (at run-time) to effect call to write 3.Dump binary to find gadgets for shellcode

http://www.scs.stanford.edu/brop/

slide-72
SLIDE 72

Defeat!

  • The blind ROP team was able to completely

automatically, only through remote interactions, develop a remote code exploit for nginx, a popular web server

slide-73
SLIDE 73

Defeat!

  • The blind ROP team was able to completely

automatically, only through remote interactions, develop a remote code exploit for nginx, a popular web server

  • The exploit was carried out on a 64-bit executable

with full stack canaries and randomization

slide-74
SLIDE 74

Defeat!

  • The blind ROP team was able to completely

automatically, only through remote interactions, develop a remote code exploit for nginx, a popular web server

  • The exploit was carried out on a 64-bit executable

with full stack canaries and randomization

  • Conclusion: give an inch, and they take a mile?
slide-75
SLIDE 75

Defeat!

  • The blind ROP team was able to completely

automatically, only through remote interactions, develop a remote code exploit for nginx, a popular web server

  • The exploit was carried out on a 64-bit executable

with full stack canaries and randomization

  • Conclusion: give an inch, and they take a mile?
  • Put another way: Memory safety is really useful!
slide-76
SLIDE 76

void safe() { char buf[80]; fgets(buf, 80, stdin); } void safer() { char buf[80]; fgets(buf, sizeof(buf), stdin); }

slide-77
SLIDE 77

void safe() { char buf[80]; fgets(buf, 80, stdin); } void safer() { char buf[80]; fgets(buf, sizeof(buf), stdin); } void vulnerable() { char buf[80]; if(fgets(buf, sizeof(buf), stdin)==NULL) return; printf(buf); }

slide-78
SLIDE 78

void safe() { char buf[80]; fgets(buf, 80, stdin); } void safer() { char buf[80]; fgets(buf, sizeof(buf), stdin); } void vulnerable() { char buf[80]; if(fgets(buf, sizeof(buf), stdin)==NULL) return; printf(buf); }

slide-79
SLIDE 79

Format string vulnerabilities

slide-80
SLIDE 80

printf format strings

int i = 10; printf(“%d %p\n”, i, &i);

slide-81
SLIDE 81

printf format strings

int i = 10; printf(“%d %p\n”, i, &i);

0xffffffff 0x00000000

&i 10 %eip %ebp &fmt …

slide-82
SLIDE 82

printf’s stack frame

printf format strings

int i = 10; printf(“%d %p\n”, i, &i);

0xffffffff 0x00000000

&i 10 %eip %ebp &fmt …

slide-83
SLIDE 83

caller’s
 stack frame printf’s stack frame

printf format strings

int i = 10; printf(“%d %p\n”, i, &i);

0xffffffff 0x00000000

&i 10 %eip %ebp &fmt …

slide-84
SLIDE 84

caller’s
 stack frame printf’s stack frame

printf format strings

int i = 10; printf(“%d %p\n”, i, &i);

0xffffffff 0x00000000

&i 10 %eip %ebp &fmt …

slide-85
SLIDE 85

caller’s
 stack frame printf’s stack frame

printf format strings

int i = 10; printf(“%d %p\n”, i, &i);

0xffffffff 0x00000000

&i 10 %eip %ebp &fmt …

  • printf takes variable number of arguments
  • printf pays no mind to where the stack frame “ends”
  • It presumes that you called it with (at least) as many arguments as

specified in the format string

slide-86
SLIDE 86

caller’s
 stack frame printf’s stack frame

printf format strings

int i = 10; printf(“%d %p\n”, i, &i);

0xffffffff 0x00000000

&i 10 %eip %ebp &fmt …

  • printf takes variable number of arguments
  • printf pays no mind to where the stack frame “ends”
  • It presumes that you called it with (at least) as many arguments as

specified in the format string

slide-87
SLIDE 87

caller’s
 stack frame printf’s stack frame

printf format strings

int i = 10; printf(“%d %p\n”, i, &i);

0xffffffff 0x00000000

&i 10 %eip %ebp &fmt …

  • printf takes variable number of arguments
  • printf pays no mind to where the stack frame “ends”
  • It presumes that you called it with (at least) as many arguments as

specified in the format string

slide-88
SLIDE 88

caller’s
 stack frame printf’s stack frame

printf format strings

int i = 10; printf(“%d %p\n”, i, &i);

0xffffffff 0x00000000

&i 10 %eip %ebp &fmt …

  • printf takes variable number of arguments
  • printf pays no mind to where the stack frame “ends”
  • It presumes that you called it with (at least) as many arguments as

specified in the format string

slide-89
SLIDE 89

void vulnerable() { char buf[80]; if(fgets(buf, sizeof(buf), stdin)==NULL) return; printf(buf); }

slide-90
SLIDE 90

void vulnerable() { char buf[80]; if(fgets(buf, sizeof(buf), stdin)==NULL) return; printf(buf); }

“%d %x"

slide-91
SLIDE 91

void vulnerable() { char buf[80]; if(fgets(buf, sizeof(buf), stdin)==NULL) return; printf(buf); }

caller’s
 stack frame 0xffffffff 0x00000000

%eip %ebp &fmt …

“%d %x"

slide-92
SLIDE 92

void vulnerable() { char buf[80]; if(fgets(buf, sizeof(buf), stdin)==NULL) return; printf(buf); }

caller’s
 stack frame 0xffffffff 0x00000000

%eip %ebp &fmt …

“%d %x"

slide-93
SLIDE 93

void vulnerable() { char buf[80]; if(fgets(buf, sizeof(buf), stdin)==NULL) return; printf(buf); }

caller’s
 stack frame 0xffffffff 0x00000000

%eip %ebp &fmt …

“%d %x"

slide-94
SLIDE 94

Format string vulnerabilities

slide-95
SLIDE 95

Format string vulnerabilities

  • printf(“100% dml”);
slide-96
SLIDE 96

Format string vulnerabilities

  • printf(“100% dml”);
  • Prints stack entry 4 byes above saved %eip
slide-97
SLIDE 97

Format string vulnerabilities

  • printf(“100% dml”);
  • Prints stack entry 4 byes above saved %eip
  • printf(“%s”);
slide-98
SLIDE 98

Format string vulnerabilities

  • printf(“100% dml”);
  • Prints stack entry 4 byes above saved %eip
  • printf(“%s”);
  • Prints bytes pointed to by that stack entry
slide-99
SLIDE 99

Format string vulnerabilities

  • printf(“100% dml”);
  • Prints stack entry 4 byes above saved %eip
  • printf(“%s”);
  • Prints bytes pointed to by that stack entry
  • printf(“%d %d %d %d …”);
slide-100
SLIDE 100

Format string vulnerabilities

  • printf(“100% dml”);
  • Prints stack entry 4 byes above saved %eip
  • printf(“%s”);
  • Prints bytes pointed to by that stack entry
  • printf(“%d %d %d %d …”);
  • Prints a series of stack entries as integers
slide-101
SLIDE 101

Format string vulnerabilities

  • printf(“100% dml”);
  • Prints stack entry 4 byes above saved %eip
  • printf(“%s”);
  • Prints bytes pointed to by that stack entry
  • printf(“%d %d %d %d …”);
  • Prints a series of stack entries as integers
  • printf(“%08x %08x %08x %08x …”);
slide-102
SLIDE 102

Format string vulnerabilities

  • printf(“100% dml”);
  • Prints stack entry 4 byes above saved %eip
  • printf(“%s”);
  • Prints bytes pointed to by that stack entry
  • printf(“%d %d %d %d …”);
  • Prints a series of stack entries as integers
  • printf(“%08x %08x %08x %08x …”);
  • Same, but nicely formatted hex
slide-103
SLIDE 103

Format string vulnerabilities

  • printf(“100% dml”);
  • Prints stack entry 4 byes above saved %eip
  • printf(“%s”);
  • Prints bytes pointed to by that stack entry
  • printf(“%d %d %d %d …”);
  • Prints a series of stack entries as integers
  • printf(“%08x %08x %08x %08x …”);
  • Same, but nicely formatted hex
  • printf(“100% no way!”)
slide-104
SLIDE 104

Format string vulnerabilities

  • printf(“100% dml”);
  • Prints stack entry 4 byes above saved %eip
  • printf(“%s”);
  • Prints bytes pointed to by that stack entry
  • printf(“%d %d %d %d …”);
  • Prints a series of stack entries as integers
  • printf(“%08x %08x %08x %08x …”);
  • Same, but nicely formatted hex
  • printf(“100% no way!”)
  • WRITES the number 3 to address pointed to by stack entry
slide-105
SLIDE 105

Format string prevalence

0.125 0.25 0.375 0.5 2002 2004 2006 2008 2010 2012 2014 2016

% of vulnerabilities that involve format string bugs

http://web.nvd.nist.gov/view/vuln/statistics

slide-106
SLIDE 106

What’s wrong with this code?

#define BUF_SIZE 16 char buf[BUF_SIZE]; void vulnerable() { int len = read_int_from_network(); char *p = read_string_from_network(); if(len > BUF_SIZE) { printf(“Too large\n”); return; } memcpy(buf, p, len); }

slide-107
SLIDE 107

What’s wrong with this code?

#define BUF_SIZE 16 char buf[BUF_SIZE]; void vulnerable() { int len = read_int_from_network(); char *p = read_string_from_network(); if(len > BUF_SIZE) { printf(“Too large\n”); return; } memcpy(buf, p, len); } void *memcpy(void *dest, const void *src, size_t n);

slide-108
SLIDE 108

What’s wrong with this code?

#define BUF_SIZE 16 char buf[BUF_SIZE]; void vulnerable() { int len = read_int_from_network(); char *p = read_string_from_network(); if(len > BUF_SIZE) { printf(“Too large\n”); return; } memcpy(buf, p, len); } void *memcpy(void *dest, const void *src, size_t n); typedef unsigned int size_t;

slide-109
SLIDE 109

What’s wrong with this code?

#define BUF_SIZE 16 char buf[BUF_SIZE]; void vulnerable() { int len = read_int_from_network(); char *p = read_string_from_network(); if(len > BUF_SIZE) { printf(“Too large\n”); return; } memcpy(buf, p, len); } void *memcpy(void *dest, const void *src, size_t n); typedef unsigned int size_t;

Negative

slide-110
SLIDE 110

What’s wrong with this code?

#define BUF_SIZE 16 char buf[BUF_SIZE]; void vulnerable() { int len = read_int_from_network(); char *p = read_string_from_network(); if(len > BUF_SIZE) { printf(“Too large\n”); return; } memcpy(buf, p, len); } void *memcpy(void *dest, const void *src, size_t n); typedef unsigned int size_t;

Ok Negative

slide-111
SLIDE 111

What’s wrong with this code?

#define BUF_SIZE 16 char buf[BUF_SIZE]; void vulnerable() { int len = read_int_from_network(); char *p = read_string_from_network(); if(len > BUF_SIZE) { printf(“Too large\n”); return; } memcpy(buf, p, len); } void *memcpy(void *dest, const void *src, size_t n); typedef unsigned int size_t;

Ok Negative Implicit cast to unsigned

slide-112
SLIDE 112

Integer overflow vulnerabilities

slide-113
SLIDE 113

What’s wrong with this code?

void vulnerable() { size_t len; char *buf; len = read_int_from_network(); buf = malloc(len + 5); read(fd, buf, len); ... }

slide-114
SLIDE 114

What’s wrong with this code?

void vulnerable() { size_t len; char *buf; len = read_int_from_network(); buf = malloc(len + 5); read(fd, buf, len); ... }

HUGE

slide-115
SLIDE 115

What’s wrong with this code?

void vulnerable() { size_t len; char *buf; len = read_int_from_network(); buf = malloc(len + 5); read(fd, buf, len); ... }

HUGE Wrap-around

slide-116
SLIDE 116

What’s wrong with this code?

void vulnerable() { size_t len; char *buf; len = read_int_from_network(); buf = malloc(len + 5); read(fd, buf, len); ... }

HUGE Wrap-around Takeaway: You have to know the semantics

  • f your programming language to avoid these errors
slide-117
SLIDE 117

Integer overflow prevalence

0.5 1 1.5 2 2.5 2002 2004 2006 2008 2010 2012 2014 2016

% of vulnerabilities that
 involve integer overflows

http://web.nvd.nist.gov/view/vuln/statistics