Software Security: Buffer Overflow Defenses

Similar documents
Software Security: Buffer Overflow Attacks (continued)

Software Security: Buffer Overflow Attacks

Software Security: Buffer Overflow Defenses and Miscellaneous

Software Security: Miscellaneous

Software Security (cont.): Defenses, Adv. Attacks, & More

Runtime Defenses against Memory Corruption

(Continue) Cryptography + (Back to) Software Security

Robust Shell Code Return Oriented Programming and HeapSpray. Zhiqiang Lin

CS 161 Computer Security

Outline. Memory Exploit

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

CS 161 Computer Security

CSE 565 Computer Security Fall 2018

CSE 127: Computer Security. Memory Integrity. Kirill Levchenko

Lecture 08 Control-flow Hijacking Defenses

Software Security II: Memory Errors - Attacks & Defenses

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

Software Vulnerabilities August 31, 2011 / CS261 Computer Security

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

Control Hijacking: Defenses

Cryptography [Symmetric Encryption]

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

CNIT 127: Exploit Development. Ch 14: Protection Mechanisms. Updated

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

BUFFER OVERFLOW DEFENSES & COUNTERMEASURES

Lecture 4 September Required reading materials for this class

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

Module: Program Vulnerabilities. Professor Trent Jaeger. CSE543 - Introduction to Computer and Network Security

Betriebssysteme und Sicherheit Sicherheit. Buffer Overflows

CMPSC 497 Buffer Overflow Vulnerabilities

CSE 127 Computer Security

Return-orientated Programming

Software Security: Misc and Principles

Spring'2016' Franziska'(Franzi)'Roesner''

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

Control Hijacking Attacks

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

CSE 127: Computer Security Control Flow Hijacking. Kirill Levchenko

Smashing the Buffer. Miroslav Štampar

Outline. Format string attack layout. Null pointer dereference

Buffer Overflow Attacks

Cryptography: Symmetric Encryption [continued]

1/31/2007 C. Edward Chow. CS591 Page 1

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

Cryptography: Symmetric Encryption (finish), Hash Functions, Message Authentication Codes

Software Security: Buffer Overflow Attacks and Beyond

CSE 509: Computer Security

Inject malicious code Call any library functions Modify the original code

Architecture-level Security Vulnerabilities

I run a Linux server, so we re secure

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

Is Exploitation Over? Bypassing Memory Protections in Windows 7

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

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

Modern Buffer Overflow Prevention Techniques: How they work and why they don t

Secure Software Development: Theory and Practice

String Oriented Programming Exploring Format String Attacks. Mathias Payer

Buffer Overflows Defending against arbitrary code insertion and execution

20: Exploits and Containment

Secure Programming I. Steven M. Bellovin September 28,

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

CS 161 Computer Security

Fundamentals of Linux Platform Security

CSCD 303 Fall Lecture 15 Buffer Overflows

Module: Program Vulnerabilities. Professor Trent Jaeger. CSE543 - Introduction to Computer and Network Security

CSE 127 Computer Security

Protection. Thierry Sans

CSE 127 Computer Security

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

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

Module: Program Vulnerabilities. Professor Trent Jaeger. CSE543 - Introduction to Computer and Network Security

Defending Computer Networks Lecture 4: Exploit Defenses. Stuart Staniford Adjunct Professor of Computer Science

Secure Programming Lecture 6: Memory Corruption IV (Countermeasures)

Buffer overflow prevention, and other attacks

Exploit Mitigation - PIE

Lecture 1: Buffer Overflows

Cryptography: Symmetric Encryption (finish), Hash Functions, Message Authentication Codes

Defeat Exploit Mitigation Heap Attacks. compass-security.com 1

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

BUFFER OVERFLOW. Jo, Heeseung

Buffer Overflow. Jo, Heeseung

Architecture-level Security Vulnerabilities. Julian Stecklina

Secure Systems Engineering

Memory Safety (cont d) Software Security

Buffer overflows. Specific topics:

C and C++: vulnerabilities, exploits and countermeasures

Web Security: Loose Ends

4. Jump to *RA 4. StackGuard 5. Execute code 5. Instruction Set Randomization 6. Make system call 6. System call Randomization

CSE 484 / CSE M 584: Computer Security and Privacy. Anonymity Mobile. Autumn Tadayoshi (Yoshi) Kohno

CSE 484 / CSE M 584: Computer Security and Privacy. Usable Security. Fall Franziska (Franzi) Roesner

The Geometry of Innocent Flesh on the Bone

CMSC 414 Computer and Network Security

Buffer Overflows: Attacks and Defenses for the Vulnerability of the Decade Review

Software Security: Buffer Overflow Attacks and Beyond

Module: Advanced Program Vulnerabilities and Defenses. Professor Trent Jaeger. CSE543 - Introduction to Computer and Network Security

CS161 Midterm 1 Review

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

Lab 2: Buffer Overflows

We will focus on Buffer overflow attacks SQL injections. See book for other examples

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

Transcription:

CSE 484 / CSE M 584: Computer Security and Privacy Software Security: Buffer Overflow Defenses Fall 2017 Franziska (Franzi) Roesner franzi@cs.washington.edu Thanks to Dan Boneh, Dieter Gollmann, Dan Halperin, Yoshi Kohno, Ada Lerner, John Manferdelli, John Mitchell, Vitaly Shmatikov, Bennet Yee, and many others for sample slides and materials...

Admin Please make sure you can access Lab 1 asap! Reminder: Lab 1 is much easier if you do the recommended reading (see course schedule for links): Smashing the Stack for Fun and Profit Exploiting Format String Vulnerabilities 10/9/17 CSE 484 / CSE M 584 - Fall 2017 2

Reminder: Printf Printf takes a variable number of arguments E.g., printf( Here s an int: %d, 10); Assumptions about input can lead to trouble E.g., printf(buf) when buf= Hello world versus when buf= Hello world %d Can be used to advance printf s internal stack pointer Can read memory E.g., printf( %x ) will print in hex format whatever printf s internal stack pointer is pointing to at the time Can write memory E.g., printf( Hello%n ); will write 5 to the memory location specified by whatever printf s internal SP is pointing to at the time 10/9/17 CSE 484 / CSE M 584 - Fall 2017 3

How Can We Attack This? foo() { char buf[ ]; strncpy(buf, readuntrustedinput(), sizeof(buf)); printf(buf); //vulnerable } If format string contains % then printf will expect to find arguments here Saved FP ret/ip &buf buf Saved FP ret/ip Caller s frame Printf s frame Foo s frame Addr 0xFF...F What should readuntrustedinput() return?? 10/9/17 CSE 484 / CSE M 584 - Fall 2017 4

Using %n to Overwrite Return Address This portion contains enough % symbols to advance printf s internal stack pointer Buffer with attacker-supplied input string attackstring%n, attack code &RET SFP RET Number of characters in attackstring must be equal to what? When %n happens, make sure the location under printf s stack pointer contains address of RET; %n will write the number of characters in attackstring into RET Return execution to this address C allows you to concisely specify the width to print, causing printf to pad by printing additional blank characters without reading anything else off the stack. Example: printf( %5d, 10) will print three spaces followed by the integer: 10 That is, %n will print 5, not 2. Key idea: do this 4 times with the right numbers to overwrite the return address byte-by-byte. (4x %n to write into &RET, &RET+1, &RET+2, &RET+3) 10/9/17 CSE 484 / CSE M 584 - Fall 2017 5

Buffer Overflow: Causes and Cures Typical memory exploit involves code injection Put malicious code at a predictable location in memory, usually masquerading as data Trick vulnerable program into passing control to it Possible defenses: 1. Prevent execution of untrusted code 2. Stack canaries 3. Encrypt pointers 4. Address space layout randomization 10/9/17 CSE 484 / CSE M 584 - Fall 2017 6

W-xor-X / DEP Mark all writeable memory locations as nonexecutable Example: Microsoft s Data Execution Prevention (DEP) This blocks (almost) all code injection exploits Hardware support AMD NX bit, Intel XD bit (in post-2004 CPUs) Makes memory page non-executable Widely deployed Windows (since XP SP2), Linux (via PaX patches), OS X (since 10.5) 10/9/17 CSE 484 / CSE M 584 - Fall 2017 7

What Does W-xor-X Not Prevent? Can still corrupt stack or function pointers or critical data on the heap As long as saved EIP points into existing code, W-xor-X protection will not block control transfer This is the basis of return-to-libc exploits Overwrite saved EIP with address of any library routine, arrange stack to look like arguments Does not look like a huge threat Attacker cannot execute arbitrary code 10/9/17 CSE 484 / CSE M 584 - Fall 2017 8

return-to-libc on Steroids Overwritten saved EIP need not point to the beginning of a library routine Any existing instruction in the code image is fine Will execute the sequence starting from this instruction What if instruction sequence contains RET? Execution will be transferred to where? Read the word pointed to by stack pointer (ESP) Guess what? Its value is under attacker s control! Use it as the new value for EIP Now control is transferred to an address of attacker s choice! Increment ESP to point to the next word on the stack 10/9/17 CSE 484 / CSE M 584 - Fall 2017 9

Chaining RETs for Fun and Profit Can chain together sequences ending in RET Krahmer, x86-64 buffer overflow exploits and the borrowed code chunks exploitation technique (2005) What is this good for? Answer [Shacham et al.]: everything Turing-complete language Build gadgets for load-store, arithmetic, logic, control flow, system calls Attack can perform arbitrary computation using no injected code at all return-oriented programming 10/9/17 CSE 484 / CSE M 584 - Fall 2017 10

Return-Oriented Programming 10/9/17 CSE 484 / CSE M 584 - Fall 2017 11

Run-Time Checking: StackGuard Embed canaries (stack cookies) in stack frames and verify their integrity prior to function return Any overflow of local variables will damage the canary buf canary sfp ret addr Frame of the calling function Top of stack Local variables Pointer to previous frame Return execution to this address 10/9/17 CSE 484 / CSE M 584 - Fall 2017 12

Run-Time Checking: StackGuard Embed canaries (stack cookies) in stack frames and verify their integrity prior to function return Any overflow of local variables will damage the canary buf canary sfp ret addr Frame of the calling function Top of stack Local variables Pointer to previous frame Return execution to this address Choose random canary string on program start Attacker can t guess what the value of canary will be Terminator canary: \0, newline, linefeed, EOF String functions like strcpy won t copy beyond \0 10/9/17 CSE 484 / CSE M 584 - Fall 2017 13

StackGuard Implementation StackGuard requires code recompilation Checking canary integrity prior to every function return causes a performance penalty For example, 8% for Apache Web server StackGuard can be defeated A single memory write where the attacker controls both the value and the destination is sufficient 10/9/17 CSE 484 / CSE M 584 - Fall 2017 14

Defeating StackGuard Suppose program contains strcpy(dst,buf) where attacker controls both dst and buf Example: dst is a local pointer variable buf dst canary sfp RET Return execution to this address BadPointer, attack code &RET canary sfp RET Overwrite destination of strcpy with RET position strcpy will copy BadPointer here 10/9/17 CSE 484 / CSE M 584 - Fall 2017 15

PointGuard Attack: overflow a function pointer so that it points to attack code Idea: encrypt all pointers while in memory Generate a random key when program is executed Each pointer is XORed with this key when loaded from memory to registers or stored back into memory Pointers cannot be overflowed while in registers Attacker cannot predict the target program s key Even if pointer is overwritten, after XORing with key it will dereference to a random memory address 10/9/17 CSE 484 / CSE M 584 - Fall 2017 16

[Cowan] Normal Pointer Dereference CPU 1. Fetch pointer value 2. Access data referenced by pointer Memory Pointer 0x1234 Data 0x1234 1. Fetch pointer value CPU 2. Access attack code referenced by corrupted pointer Memory Corrupted pointer 0x1234 0x1340 Data Attack code 0x1234 0x1340 10/9/17 CSE 484 / CSE M 584 - Fall 2017 17

[Cowan] PointGuard Dereference CPU 1. Fetch pointer value Decrypt 0x1234 2. Access data referenced by pointer Memory Encrypted pointer 0x7239 Data 0x1234 1. Fetch pointer value Decrypts to random value Decrypt CPU 0x9786 2. Access random address; segmentation fault and crash Memory Corrupted pointer 0x7239 0x1340 Data Attack code 0x1234 0x1340 0x9786 10/9/17 CSE 484 / CSE M 584 - Fall 2017 18

PointGuard Issues Must be very fast Pointer dereferences are very common Compiler issues Must encrypt and decrypt only pointers If compiler spills registers, unencrypted pointer values end up in memory and can be overwritten there Attacker should not be able to modify the key Store key in its own non-writable memory page PG d code doesn t mix well with normal code What if PG d code needs to pass a pointer to OS kernel? 10/9/17 CSE 484 / CSE M 584 - Fall 2017 19

ASLR: Address Space Randomization Map shared libraries to a random location in process memory Attacker does not know addresses of executable code Deployment (examples) Windows Vista: 8 bits of randomness for DLLs Linux (via PaX): 16 bits of randomness for libraries Even Android More effective on 64-bit architectures Other randomization methods Randomize system call ids or instruction set 10/9/17 CSE 484 / CSE M 584 - Fall 2017 20

Example: ASLR in Vista Booting Vista twice loads libraries into different locations: 10/9/17 CSE 484 / CSE M 584 - Fall 2017 21

ASLR Issues NOP slides and heap spraying to increase likelihood for custom code (e.g., on heap) Brute force attacks or memory disclosures to map out memory on the fly Disclosing a single address can reveal the location of all code within a library 10/9/17 CSE 484 / CSE M 584 - Fall 2017 22

Other Possible Solutions Use safe programming languages, e.g., Java What about legacy C code? (Though Java doesn t magically fix all security issues J) Static analysis of source code to find overflows Dynamic testing: fuzzing LibSafe: dynamically loaded library that intercepts calls to unsafe C functions and checks that there s enough space before doing copies Also doesn t prevent everything 10/9/17 CSE 484 / CSE M 584 - Fall 2017 23