This time. Defenses and other memory safety vulnerabilities. Everything you ve always wanted to know about gdb but were too afraid to ask

Similar documents
MEMORY SAFETY ATTACKS & DEFENSES

BUFFER OVERFLOW DEFENSES & COUNTERMEASURES

Software Security: Buffer Overflow Attacks

Return-orientated Programming

Required reading: StackGuard: Simple Stack Smash Protection for GCC

Software Security: Buffer Overflow Defenses and Miscellaneous

Software Security: Buffer Overflow Defenses

Lecture 08 Control-flow Hijacking Defenses

CSE 127: Computer Security Control Flow Hijacking. Kirill Levchenko

The Geometry of Innocent Flesh on the Bone

Buffer. This time. Security. overflows. Software. By investigating. We will begin. our 1st section: History. Memory layouts

Security and Privacy in Computer Systems. Lecture 5: Application Program Security

Software Security: Buffer Overflow Attacks (continued)

Memory safety, continued

CS 161 Computer Security

Biography. Background

Architecture-level Security Vulnerabilities

Exploits and gdb. Tutorial 5

Buffer overflow risks have been known for over 30 years. Is it still a problem? Try searching at to see.

So$ware Security (II): Buffer- overflow Defenses

Lecture 10 Return-oriented programming. Stephen Checkoway University of Illinois at Chicago Based on slides by Bailey, Brumley, and Miller

From Over ow to Shell

CSC 405 Computer Security Stack Canaries & ASLR

Secure Programming Lecture 5: Memory Corruption III (Countermeasures)

Software Security: Miscellaneous

Architecture-level Security Vulnerabilities. Julian Stecklina

The first Secure Programming Laboratory will be today! 3pm-6pm in Forrest Hill labs 1.B31, 1.B32.

Advanced Security for Systems Engineering VO 05: Advanced Attacks on Applications 2

CSE 565 Computer Security Fall 2018

Intro to x86 Binaries. From ASM to exploit

Linux Memory Layout. Lecture 6B Machine-Level Programming V: Miscellaneous Topics. Linux Memory Allocation. Text & Stack Example. Topics.

Return Oriented Programming

CS 161 Computer Security

2 Sadeghi, Davi TU Darmstadt 2012 Secure, Trusted, and Trustworthy Computing Chapter 6: Runtime Attacks

Writing Exploits. Nethemba s.r.o.

Beyond Stack Smashing: Recent Advances in Exploiting. Jonathan Pincus(MSR) and Brandon Baker (MS)

U23 - Binary Exploitation

Overflows, Injection, & Memory Safety

UMSSIA LECTURE I: SOFTWARE SECURITY

Secure Programming Lecture 6: Memory Corruption IV (Countermeasures)

String Oriented Programming Exploring Format String Attacks. Mathias Payer

Is stack overflow still a problem?

Memory safety, continued

CSc 466/566. Computer Security. 20 : Operating Systems Application Security

Q: Exploit Hardening Made Easy

CSE 565 Computer Security Fall 2018

The geometry of innocent flesh on the bone: Return-into-libc without function calls (on the x86) Hovav Shacham presented by: Fabian Fäßler

Robust Shell Code Return Oriented Programming and HeapSpray. Zhiqiang Lin

Module: Return-oriented Programming. Professor Trent Jaeger. CSE543 - Introduction to Computer and Network Security

Università Ca Foscari Venezia

CS 161 Computer Security. Week of January 22, 2018: GDB and x86 assembly

Module: Return-oriented Programming. Professor Trent Jaeger. CSE543 - Introduction to Computer and Network Security

Advanced Buffer Overflow

Hacking Blind BROP. Presented by: Brooke Stinnett. Article written by: Andrea Bittau, Adam Belay, Ali Mashtizadeh, David Mazie`res, Dan Boneh

CIS 551 / TCOM 401 Computer and Network Security. Spring 2007 Lecture 2

Countermeasures in Modern Operating Systems. Yves Younan, Vulnerability Research Team (VRT)

CSE 509: Computer Security

Software Security; Common Implementation Flaws

CSC 591 Systems Attacks and Defenses Return-into-libc & ROP

Memory Safety (cont d) Software Security

Return oriented programming

Hacking Blind. Andrea Bittau, Adam Belay, Ali Mashtizadeh, David Mazières, Dan Boneh. Stanford University

ROP It Like It s Hot!

Outline. Memory Exploit

CSE 127 Computer Security

Exploiting Stack Buffer Overflows Learning how blackhats smash the stack for fun and profit so we can prevent it

Betriebssysteme und Sicherheit Sicherheit. Buffer Overflows

Secure Programming I. Steven M. Bellovin September 28,

Software Security II: Memory Errors - Attacks & Defenses

Security Workshop HTS. LSE Team. February 3rd, 2016 EPITA / 40

idkwim in SecurityFirst 0x16 years old Linux system security researcher idkwim.tistory.com idkwim.linknow.

Advanced Buffer Overflow

20: Exploits and Containment

Introduction to Computer Systems , fall th Lecture, Sep. 28 th

Advanced Systems Security: Control-Flow Integrity

Software Security: Misc and Principles

CS 161 Computer Security

CNIT 127: Exploit Development. Ch 2: Stack Overflows in Linux

Memory Corruption Vulnerabilities, Part II

CSE 127 Computer Security

Return-Oriented Rootkits

Inject malicious code Call any library functions Modify the original code

Buffer overflows. Specific topics:

ANITA S SUPER AWESOME RECITATION SLIDES

Outline. Format string attack layout. Null pointer dereference

Buffer Overflow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Buffer Overflow. Jo, Heeseung

BUFFER OVERFLOW. Jo, Heeseung

CSE 127: Computer Security. Memory Integrity. Kirill Levchenko

CNIT 127: Exploit Development. Ch 1: Before you begin. Updated

Sungkyunkwan University

CMPSC 497: Midterm Review

Exercise 6: Buffer Overflow and return-into-libc Attacks

CS642: Computer Security

Outline. Heap meta-data. Non-control data overwrite

On The Effectiveness of Address-Space Randomization. H. Shacham, M. Page, B. Pfaff, E.-J. Goh, N. Modadugu, and D. Boneh Stanford University CCS 2004

Introduction to Operating Systems Prof. Chester Rebeiro Department of Computer Science and Engineering Indian Institute of Technology, Madras

Secure Systems Engineering

CSE 127 Computer Security

CS 645: Lecture 3 Software Vulnerabilities. Rachel Greenstadt July 3, 2013

Transcription:

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

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

Recall our challenges How can we make these even more difficult? 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)

Detecting overflows with canaries %eip Text... 00 00 00 00 %ebp %eip &arg1 buffer

Detecting overflows with canaries %eip Text... 00 00 00 00 %ebp %eip &arg1 buffer

Detecting overflows with canaries %eip Text... 00 00 00 00 02 8d e2 10 %ebp %eip &arg1 buffer canary

Detecting overflows with canaries %eip Text... 00 00 00 00 02 8d e2 10 nop %ebp nop nop %eip &arg1 0xbdf \x0f \x3c \x2f... buffer canary

Detecting overflows with canaries %eip Text... 00 00 00 00 02 8d e2 10 nop %ebp nop nop %eip &arg1 0xbdf \x0f \x3c \x2f... buffer canary

Detecting overflows with canaries %eip Not the expected value: abort Text... 00 00 00 00 02 8d e2 10 nop %ebp nop nop %eip &arg1 0xbdf \x0f \x3c \x2f... buffer canary

Detecting overflows with canaries %eip Not the expected value: abort Text... 00 00 00 00 02 8d e2 10 nop %ebp nop nop %eip &arg1 0xbdf \x0f \x3c \x2f... buffer canary What value should the canary have?

Canary values From StackGuard [Wagle & Cowan] 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

Recall our challenges How can we make these even more difficult? 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) More next time

Return-to-libc %eip padding good guess Text %ebp %eip 0xbdf &arg1... 00 00 00 00 nop nop nop \x0f \x3c \x2f... libc buffer nop sled malicious code

Return-to-libc %eip padding good guess Text %ebp %eip 0xbdf &arg1... 00 00 00 00 nop nop nop libc buffer nop sled

Return-to-libc %eip padding good guess Text libc %ebp %eip 0xbdf &arg1... 00 00 00 00 buffer

Return-to-libc %eip padding Text libc %ebp %eip &arg1... 00 00 00 00 buffer

Return-to-libc %eip padding Text libc %ebp %eip &arg1... 00 00 00 00 buffer... exec() printf()... /bin/sh... libc

Return-to-libc %eip padding known location Text libc %ebp %eip 0x17f &arg1... 00 00 00 00 buffer... exec() printf()... /bin/sh... libc

Return-to-libc %eip padding known location Text libc %ebp %eip 0x17f &arg1... 00 00 00 00 0x20d buffer... exec() printf()... /bin/sh... libc

Recall our challenges How can we make these even more difficult? 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) Basic idea: change the layout of the stack Slow to adopt Linux in 2005 Vista in 2007 (off by default for compatibility with older 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?

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

Last time We continued Buffer overflows By looking at Overflow Defenses and other memory safety vulnerabilities Finish overflow attacks & other vulnerabilities Overflow defenses Everything you ve always wanted to know about gdb but were too afraid to ask

This time Continuing with Software Security Writing & testing for Secure Code Required reading: StackGuard: Simple Stack Smash Protection for GCC Optional reading: Basic Integer Overflows Exploiting Format String Vulnerabilities http://nob.cs.ucdavis.edu/bishop/secprog/robust.html

Cat and mouse

Cat and mouse Defense: Make stack/heap non-executable to prevent injection of code

Cat and mouse Defense: Make stack/heap non-executable to prevent injection of code Attack response: Return to libc

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

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) or information leak (format string vulnerability: later today)

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) or information leak (format string vulnerability: later today) Defense: Avoid using libc code entirely and use code in the program text instead

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) or 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)

Return oriented programming (ROP)

Return-oriented Programming

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

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

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

Approach

Approach Gadgets are instruction groups that end with ret

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

Simple example equivalent to mov %edx, 5 %edx 0x00 0xffffffff

Simple example 0x17f: pop %edx ret Gadget equivalent to mov %edx, 5 Text %edx 0x00 0xffffffff

Simple example 0x17f: pop %edx ret Gadget equivalent to mov %edx, 5 0x00 Text next gadget 5 0x17f Instructions %edx 0xffffffff

Simple example (ret) 0x17f: pop %edx ret Gadget %eip equivalent to mov %edx, 5 program counter %esp 0x00 Text next gadget 5 0x17f Instructions %edx 0xffffffff

Simple example (ret) 0x17f: pop %edx ret %eip equivalent to mov %edx, 5 %esp Text next gadget 5 0x17f %edx 0x00 0xffffffff

Simple example (ret) 0x17f: pop %edx ret %eip equivalent to mov %edx, 5 %esp Text next gadget 5 0x17f %edx 5 0x00 0xffffffff

Simple example (ret) 0x17f: pop %edx ret %eip equivalent to mov %edx, 5 %esp Text next gadget 5 0x17f %edx 5 0x00 0xffffffff

Code sequence 0x17f: mov %eax, [%esp] mov %ebx, [%esp-8] mov [%ebx], %eax %eip %eax %ebx %esp Text 0x404 5 0x00 0x404 0xffffffff

Code sequence 0x17f: mov %eax, [%esp] mov %ebx, [%esp-8] mov [%ebx], %eax %eip %eax 5 %ebx %esp Text 0x404 5 0x00 0x404 0xffffffff

Code sequence 0x17f: mov %eax, [%esp] mov %ebx, [%esp-8] mov [%ebx], %eax %eip %eax 5 %ebx 0x404 %esp Text 0x404 5 0x00 0x404 0xffffffff

Code sequence 0x17f: mov %eax, [%esp] mov %ebx, [%esp-8] mov [%ebx], %eax %eip %eax %ebx 5 0x404 %esp Text 5 0x404 5 0x00 0x404 0xffffffff

Equivalent ROP sequence 0x17f: pop %eax ret 0x20d: pop %ebx ret 0x21a: mov [%ebx], %eax %eip %eax %ebx %esp Text 0x21a 0x404 0x20d 5 0x00 0x404 0xffffffff

Equivalent ROP sequence 0x17f: pop %eax ret 0x20d: pop %ebx ret 0x21a: mov [%ebx], %eax %eip %eax %ebx 5 %esp Text 0x21a 0x404 0x20d 5 0x00 0x404 0xffffffff

Equivalent ROP sequence 0x17f: pop %eax ret 0x20d: pop %ebx ret 0x21a: mov [%ebx], %eax %eip %eax %ebx 5 %esp Text 0x21a 0x404 0x20d 5 0x00 0x404 0xffffffff

Equivalent ROP sequence 0x17f: pop %eax ret 0x20d: pop %ebx ret 0x21a: mov [%ebx], %eax %eip %eax %ebx 5 0x404 %esp Text 0x21a 0x404 0x20d 5 0x00 0x404 0xffffffff

Equivalent ROP sequence 0x17f: pop %eax ret 0x20d: pop %ebx ret 0x21a: mov [%ebx], %eax %eip %eax %ebx 5 0x404 Text 0x21a 0x404 0x20d 5 0x00 0x404 0xffffffff

Equivalent ROP sequence 0x17f: pop %eax ret 0x20d: pop %ebx ret 0x21a: mov [%ebx], %eax %eip %eax %ebx 5 0x404 Text 5 0x21a 0x404 0x20d 5 0x00 0x404 0xffffffff

Image by Dino Dai Zovi

Whence the gadgets?

Whence the gadgets? How can we find gadgets to construct an exploit?

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

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?

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

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

Blind ROP

Blind ROP Defense: Randomizing the location of the code (by compiling for position independence) on a 64- bit machine makes attacks very difficult

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

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

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:

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

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

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/

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

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

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?

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!

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

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); }

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); }

Format string vulnerabilities

printf format strings int i = 10; printf( %d %p\n, i, &i);

printf format strings int i = 10; printf( %d %p\n, i, &i); 0x00000000 0xffffffff %ebp %eip &fmt 10 &i

printf format strings int i = 10; printf( %d %p\n, i, &i); 0x00000000 0xffffffff %ebp %eip &fmt 10 &i printf s stack frame

printf format strings int i = 10; printf( %d %p\n, i, &i); 0x00000000 0xffffffff %ebp %eip &fmt 10 &i printf s stack frame caller s stack frame

printf format strings int i = 10; printf( %d %p\n, i, &i); 0x00000000 0xffffffff %ebp %eip &fmt 10 &i printf s stack frame caller s stack frame

printf format strings int i = 10; printf( %d %p\n, i, &i); 0x00000000 0xffffffff %ebp %eip &fmt 10 &i printf s stack frame printf takes variable number of arguments caller s stack frame 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

printf format strings int i = 10; printf( %d %p\n, i, &i); 0x00000000 0xffffffff %ebp %eip &fmt 10 &i printf s stack frame printf takes variable number of arguments caller s stack frame 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

printf format strings int i = 10; printf( %d %p\n, i, &i); 0x00000000 0xffffffff %ebp %eip &fmt 10 &i printf s stack frame printf takes variable number of arguments caller s stack frame 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

printf format strings int i = 10; printf( %d %p\n, i, &i); 0x00000000 0xffffffff %ebp %eip &fmt 10 &i printf s stack frame printf takes variable number of arguments caller s stack frame 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

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

void vulnerable() { char buf[80]; if(fgets(buf, sizeof(buf), stdin)==null) return; printf(buf); } %d %x"

void vulnerable() { char buf[80]; if(fgets(buf, sizeof(buf), stdin)==null) return; printf(buf); } %d %x" 0x00000000 0xffffffff %ebp %eip &fmt caller s stack frame

void vulnerable() { char buf[80]; if(fgets(buf, sizeof(buf), stdin)==null) return; printf(buf); } %d %x" 0x00000000 0xffffffff %ebp %eip &fmt caller s stack frame

void vulnerable() { char buf[80]; if(fgets(buf, sizeof(buf), stdin)==null) return; printf(buf); } %d %x" 0x00000000 0xffffffff %ebp %eip &fmt caller s stack frame

Format string vulnerabilities

Format string vulnerabilities printf( 100% dml );

Format string vulnerabilities printf( 100% dml ); Prints stack entry 4 byes above saved %eip

Format string vulnerabilities printf( 100% dml ); Prints stack entry 4 byes above saved %eip printf( %s );

Format string vulnerabilities printf( 100% dml ); Prints stack entry 4 byes above saved %eip printf( %s ); Prints bytes pointed to by that stack entry

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 );

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

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 );

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

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! )

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

Format string prevalence 0.5 0.375 0.25 % of vulnerabilities that involve format string bugs 0.125 0 2002 2004 2006 2008 2010 2012 2014 2016 http://web.nvd.nist.gov/view/vuln/statistics

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); }

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);

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;

What s wrong with this code? #define BUF_SIZE 16 char buf[buf_size]; void vulnerable() { Negative 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;

What s wrong with this code? #define BUF_SIZE 16 char buf[buf_size]; void vulnerable() { Negative int len = read_int_from_network(); char *p = read_string_from_network(); Ok 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;

What s wrong with this code? #define BUF_SIZE 16 char buf[buf_size]; void vulnerable() { Negative int len = read_int_from_network(); char *p = read_string_from_network(); Ok if(len > BUF_SIZE) { printf( Too large\n ); return; } memcpy(buf, p, len); } Implicit cast to unsigned void *memcpy(void *dest, const void *src, size_t n); typedef unsigned int size_t;

Integer overflow vulnerabilities

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);...

What s wrong with this code? void vulnerable() { size_t len; char *buf; } HUGE len = read_int_from_network(); buf = malloc(len + 5); read(fd, buf, len);...

What s wrong with this code? void vulnerable() { size_t len; char *buf; } HUGE len = read_int_from_network(); buf = malloc(len + 5); read(fd, buf, len);... Wrap-around

What s wrong with this code? void vulnerable() { size_t len; char *buf; } HUGE len = read_int_from_network(); buf = malloc(len + 5); read(fd, buf, len);... Wrap-around Takeaway: You have to know the semantics of your programming language to avoid these errors

Integer overflow prevalence 2.5 2 % of vulnerabilities that involve integer overflows 1.5 1 0.5 0 2002 2004 2006 2008 2010 2012 2014 2016 http://web.nvd.nist.gov/view/vuln/statistics