CSC 495/583 Topics of Software Security Heap Exploitation

Slides:



Advertisements
Similar presentations
Dynamic Memory Management
Advertisements

Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
User-Level Memory Management in Linux Programming
Week 7 - Friday.  What did we talk about last time?  Allocating 2D arrays.
DIEHARDER: SECURING THE HEAP. Previously in DieHard…  Increase Reliability by random positioning of data  Replicated Execution detects invalid memory.
Security & Exploitation
Stacks and HeapsCS-3013 A-term A Short Digression on Stacks and Heaps CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.
Beyond Stack Smashing: Recent Advances in Exploiting Buffer Overruns Jonathan Pincus Microsoft Research Brandon Baker Microsoft Carl Hartung CSCI 7143:
Digression on Stack and Heaps CS-502 (EMC) Fall A Short Digression on Stacks and Heaps CS-502, Operating Systems Fall 2009 (EMC) (Slides include.
Netprog: Buffer Overflow1 Buffer Overflow Exploits Taken shamelessly from: netprog/overflow.ppt.
Stacks and HeapsCS-502 Fall A Short Digression Stacks and Heaps CS-502, Operating Systems Fall 2007 (Slides include materials from Operating System.
Software bugs (and security implications). Software security Code can have perfect design and algorithm, but still have implementation vulnerabilities.
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
Computer Science Detecting Memory Access Errors via Illegal Write Monitoring Ongoing Research by Emre Can Sezer.
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
Week 9 Part 1 Kyle Dewey. Overview Dynamic allocation continued Heap versus stack Memory-related bugs Exam #2.
University of Washington Today Finished up virtual memory On to memory allocation Lab 3 grades up HW 4 up later today. Lab 5 out (this afternoon): time.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
Lecture 9: Buffer Ovefflows and ROP EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014,
What is exactly Exploit writing?  Writing a piece of code which is capable of exploit the vulnerability in the target software.
CNIT 127: Exploit Development Ch 8: Windows Overflows Part 2.
Buffer overflow and stack smashing attacks Principles of application software security.
Group 9. Exploiting Software The exploitation of software is one of the main ways that a users computer can be broken into. It involves exploiting the.
VM: Chapter 7 Buffer Overflows. csci5233 computer security & integrity (VM: Ch. 7) 2 Outline Impact of buffer overflows What is a buffer overflow? Types.
Protecting C and C++ programs from current and future code injection attacks Yves Younan, Wouter Joosen and Frank Piessens DistriNet Department of Computer.
Software Security. Bugs Most software has bugs Some bugs cause security vulnerabilities Incorrect processing of security related data Incorrect processing.
Variables Bryce Boe 2012/09/05 CS32, Summer 2012 B.
CSE 220 – C Programming malloc, calloc, realloc.
Heap Overflow Attacks.
Shellcode COSC 480 Presentation Alison Buben.
Refs: rootshell, antionline, your favorite hacker site…
Mitigation against Buffer Overflow Attacks
Buffer Overflow Buffer overflows are possible because C doesn’t check array boundaries Buffer overflows are dangerous because buffers for user input are.
Dynamic Storage Allocation
Memory allocation & parameter passing
Protecting Memory What is there to protect in memory?
Protecting Memory What is there to protect in memory?
CSC 495/583 Topics of Software Security Heap Exploitation (2)
Protecting Memory What is there to protect in memory?
Lesson One – Creating a thread
ENEE150 Discussion 07 Section 0101 Adam Wang.
Dynamic Memory Allocation
CSC 495/583 Topics of Software Security Stack Overflows (2)
Day 03 Introduction to C.
Checking Memory Management
CSCI206 - Computer Organization & Programming
The Hardware/Software Interface CSE351 Winter 2013
Clear1 and Clear2 clear1(int array[], int size) { int i; for (i = 0; i < size; i += 1) array[i] = 0; } clear2(int *array, int size) {
High Coverage Detection of Input-Related Security Faults
Optimizing Malloc and Free
Object Oriented Programming COP3330 / CGS5409
Memory Management III: Perils and pitfalls Mar 13, 2001
Dynamic Memory Allocation
Memory Allocation CS 217.
Software Security Lesson Introduction
CSC 495/583 Topics of Software Security Format String Bug (2) & Heap
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
The future of Software Security Dr. Si Chen
Arrays an array of 5 ints is filled with 3,2,4,1,7
Dynamic Memory A whole heap of fun….
COP 3330 Object-oriented Programming in C++
CSE 153 Design of Operating Systems Winter 2019
Understanding and Preventing Buffer Overflow Attacks in Unix
Pointers, Dynamic Data, and Reference Types
Week 7 - Friday CS222.
SPL – PS2 C++ Memory Handling.
CSE 303 Concepts and Tools for Software Development
Return-to-libc Attacks
Presentation transcript:

CSC 495/583 Topics of Software Security Heap Exploitation Class11 CSC 495/583 Topics of Software Security Heap Exploitation Dr. Si Chen (schen@wcupa.edu)

Heap Overview Heap Exploitation Overview Heap Overflows Use After Free Heap Spraying Metadata Corruption

It’s just another segment in runtime memory The Heap 0x00000000 Runtime Memory Libraries (libc) ELF Executable .text segment .data segment Heap Stack It’s just another segment in runtime memory 0xFFFFFFFF

Basics of Dynamic Memory int main() { char * buffer = NULL; /* allocate a 0x100 byte buffer */ buffer = malloc(0x100); /* read input and print it */ fgets(stdin, buffer, 0x100); printf(“Hello %s!\n”, buffer); /* destroy our dynamically allocated buffer */ free(buffer); return 0; }

Heap vs Stack Heap Dynamic memory allocations at runtime Objects, big buffers, structs, persistence, larger things Slower, Manual Done by the programmer malloc/calloc/recalloc/free new/delete Stack Fixed memory allocations known at compile time Local variables, return addresses, function args Fast, Automatic Done by the compiler Abstracts away any concept of allocating/de-allocating

Tons of different heap implementations dlmalloc ptmalloc tcmalloc jemalloc nedmalloc Hoard Some applications even create their own heap implementations!

Sizes.c

Sizes.c How many bytes on the heap are your malloc chunks really taking up??

unsigned int * buffer = NULL; buffer = malloc(0x100); Heap Chunks unsigned int * buffer = NULL; buffer = malloc(0x100); //Out comes a heap chunk Heap Chunk Previous Chunk Size (4 bytes) Chunk Size (4 bytes) Data (8 + (n / 8)*8 bytes) Flags *(buffer-2) *(buffer-1) *buffer

Heap Chunk Previous Chunk Size Chunk Size Heap Chunks Size of previous chunk (if prev chunk is free) Chunk Size Size of entire chunk including overhead Heap Chunk Previous Chunk Size (4 bytes) Chunk Size (4 bytes) Data (8 + (n / 8)*8 bytes) Flags *(buffer-2) *(buffer-1) *buffer

Heap Chunk Data Heap Chunks – Your newly allocated memory / ptr returned by malloc Heap Chunk Previous Chunk Size (4 bytes) Chunk Size (4 bytes) Data (8 + (n / 8)*8 bytes) Flags *(buffer-2) *(buffer-1) *buffer

Heap Chunk Flags Heap Chunks – Because of byte alignment, the lower 3 bits of the chunk size field would always be zero. Instead they are used for flag bits. 0x01 PREV_INUSE – set when previous chunk is in use 0x02 IS_MMAPPED – set if chunk was obtained with mmap() 0x04 NON_MAIN_ARENA – set if chunk belongs to a thread arena Heap Chunk Previous Chunk Size (4 bytes) Chunk Size (4 bytes) Data (8 + (n / 8)*8 bytes) Flags *(buffer-2) *(buffer-1) *buffer

Pseudo Memory Map Runtime Memory Runtime Memory MBE - 04/07/2015 1313 0x00000000 – Start of memory 0x00000000 – Start of memory Runtime Memory Libraries (libc) ELF Executable .text segment .data segment Heap Stack Runtime Memory Libraries (libc) ELF Executable .text segment .data segment Heap Stack 0x08048000 – Start of .text Segment 0x08048000 – Start of .text Segment 0xb7ff0000 – Top of heap 0xbfff0000 – Top of stack 0xFFFFFFFF – End of memory MBE - 04/07/2015 Heap Exploitation 1313

Heap Allocations Runtime Memory Heap Segment 0x00000000 Libraries (libc) ELF Executable .text segment .data segment Heap Stack Heap Segment ---------------------------------> Grows towards higher memory Previous Chunk Size Chunk Size Data 0xFFFFFFFF

Heap Allocations Runtime Memory Heap Segment 0x00000000 Libraries (libc) ELF Executable .text segment .data segment Heap Stack Heap Segment ---------------------------------> Grows towards higher memory Previous Chunk Size Chunk Size Data 0xFFFFFFFF

Heap Allocations Runtime Memory Heap Segment 0x00000000 Libraries (libc) ELF Executable .text segment .data segment Heap Stack Heap Segment ---------------------------------> Grows towards higher memory Previous Chunk Size Chunk Size Data 0xFFFFFFFF

Heap grows DOWN towards higher memory Segment Growth Heap grows DOWN towards higher memory Heap Segment higher memory Grows towards ----------> Stack grows UP towards lower memory Grows towards lower memory <--------- Stack Segment

Segment Growth Runtime Memory Libraries (libc) ELF Executable .text segment .data segment Heap Stack Heap Segment higher memory Grows towards ----------> Grows towards lower memory <--------- 0xFFFFFFFF Stack Segment

Heap grows DOWN towards higher memory Segment Growth Heap grows DOWN towards higher memory Heap Segment higher memory Grows towards ----------> Stack grows UP towards lower memory Any ideas why? – Probably historical reasons, gave low memory systems more room to fluctuate Grows towards lower memory <--------- Stack Segment

Heap chunks exist in two states Heap Chunks – In Use Heap chunks exist in two states in use (malloc’d) free’d Heap Chunk Previous Chunk Size (4 bytes) Chunk Size (4 bytes) Data (8 + (n / 8)*8 bytes) Flags *(buffer-2) *(buffer-1) *buffer

free(buffer); Heap Chunk (freed) Forward Pointer Backwards Pointer Heap Chunks – Freed free(buffer); Forward Pointer A pointer to the next freed chunk Backwards Pointer A pointer to the previous freed chunk Heap Chunk (freed) Previous Chunk Size (4 bytes) Chunk Size (4 bytes) FD (4 bytes) BK (4 bytes) Flags *(buffer-2) *(buffer-1) *buffer *(buffer+1)

From Glibc 2.7.0 Source (malloc.c)

Heap Overview Heap Exploitation Overview Heap Overflows Use After Free Heap Spraying Metadata Corruption

Heap Overflows Runtime Memory Heap Segment 0x00000000 Libraries (libc) ELF Executable .text segment .data segment Heap Stack Heap Segment ---------------------------------> Grows towards higher memory Previous Chunk Size Chunk Size Data 0xFFFFFFFF

Heap Overflows Buffer overflows are basically the same on the heap as they are on the stack 0x00000000 Runtime Memory Libraries (libc) ELF Executable .text segment .data segment Heap Stack Heap Segment ---------------------------------> Grows towards higher memory Previous Chunk Size Chunk Size AAAAAAAAAAAAAA AAAAAAAAAAAAAA AAAAAAAAAAAAAA AAAAAAAAAAAAAA AAAAAAAAAAAAAA … heap overflow Data Previous Chunk Size Chunk Size Data 0xFFFFFFFF

Heap Overflows In the real world, lots of cool and complex things like objects/structs end up on the heap Anything that handles the data you just corrupted is now viable attack surface in the application It’s common to put function pointers in structs which generally are malloc’d on the heap Overwrite a function pointer on the heap, and force a codepath to call that object’s function!

Use After Free Use After Free (UAF) A class of vulnerability where data on the heap is freed, but a leftover reference or ‘dangling pointer’ is used by the code as if the data were still valid Most popular in Web Browsers, complex programs Also known as UAF

Use After Free (UAF) Runtime Memory Heap Segment 0x00000000 Libraries (libc) ELF Executable .text segment .data segment Heap Stack Heap Segment ---------------------------------> Grows towards higher memory Previous Chunk Size Chunk Size Data pointer 0xFFFFFFFF

Use After Free (UAF) free()’d Runtime Memory Heap Segment 0x00000000 Libraries (libc) ELF Executable .text segment .data segment Heap Stack Heap Segment ---------------------------------> Grows towards higher memory Previous Chunk Size Chunk Size Data pointer free()’d 0xFFFFFFFF

Use After Free (UAF) free()’d free()’d Runtime Memory Heap Segment 0x00000000 Runtime Memory Libraries (libc) ELF Executable .text segment .data segment Heap Stack Heap Segment ---------------------------------> Grows towards higher memory Previous Chunk Size Chunk Size Data free()’d ??? free()’d 0xFFFFFFFF

Use After Free (UAF) Runtime Memory Heap Segment 0x00000000 Libraries (libc) ELF Executable .text segment .data segment Heap Stack Heap Segment ---------------------------------> Grows towards higher memory Previous Chunk Size Chunk Size Data pointer dangling 0xFFFFFFFF

Use After Free (UAF) Dangling Pointer A left over pointer in your code that references free’d data and is prone to be re-used As the memory it’s pointing at was freed, there’s no guarantees on what data is there now Also known as stale pointer, wild pointer

Use After Free (UAF) Runtime Memory Heap Segment 0x00000000 Libraries (libc) ELF Executable .text segment .data segment Heap Stack Heap Segment ---------------------------------> Grows towards higher memory Previous Chunk Size Chunk Size Data pointer dangling 0xFFFFFFFF

Use After Free (UAF) Runtime Memory Heap Segment Libraries (libc) ELF Executable .text segment .data segment Heap Stack Heap Segment ---------------------------------> Grows towards higher memory Previous Chunk Size Chunk Size Data Previous Chunk Size malloc() Chunk Size pointer Newly allocated data 0xFFFFFFFF dangling

Use After Free (UAF) Runtime Memory Heap Segment Libraries (libc) ELF Executable .text segment .data segment Heap Stack Heap Segment ---------------------------------> Grows towards higher memory Previous Chunk Size Chunk Size Data Previous Chunk Size malloc() fgets() ... Chunk Size AAAAAAAAAAAAAA AAAAAAAAAAAAAA AAAAAAAAAAAAAA AAAAAAAAAAAAAA AAAAAAAAAAAAAA AAAAAAAAAAAAAA pointer dangling

Use After Free (UAF) To exploit a UAF, you usually have to allocate a different type of object over the one you just freed struct toystr { void (* message)(char *); char buffer[20]; }; struct person { int favorite_num; int age; char name[16]; };

Use After Free You actually don’t need any form of memory corruption to leverage a use after free It’s simply an implementation issue – pointer mismanagement

Heap Spraying Heap Spraying A technique used to increase exploit reliability, by filling the heap with large chunks of data relevant to the exploit you’re trying to land It can assist with bypassing ASLR A heap spray is not a vulnerability or security flaw

Heap Spray in Action Runtime Memory filler = “AAAAAAAAAAAAA...”; 0x00000000 – Start of memory 0x08048000 – .text Segment in ELF 0x09104000 – Top of heap filler = “AAAAAAAAAAAAA...”; for(i = 0; i < 3000; i++) { temp = malloc(1000000); memcpy(temp, filler, 1000000); } Runtime Memory Libraries (libc) ELF Executable Heap Stack 0xbfff0000 – Top of stack 0xFFFFFFFF – End of memory

Heap Spray in Action Runtime Memory filler = “AAAAAAAAAAAAA...”; 0x00000000 – Start of memory 0x08048000 – .text Segment in ELF 0x09104000 – Top of heap filler = “AAAAAAAAAAAAA...”; for(i = 0; i < 3000; i++) { temp = malloc(1000000); memcpy(temp, filler, 1000000); } 0xbbe09e00 – bottom of heap 0xbfff0000 – Top of stack 0xFFFFFFFF – End of memory Runtime Memory Libraries (libc) ELF Executable Heap AAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAA Stack 3GB of AAAAAAAAA’s

Heap Spraying in the Wild Generally found in browser exploits, rare in CTF and wargames but still something you should be aware of Usually heap sprays are done in something like javascript placed on a malicious html page memory = new Array(); for(i = 0; i < 0x100; i++) memory[i] = ROPNOP + ROP;

On 32bit systems your address space is at maximum 4GB (232 bytes) Heap Spraying on 32bit On 32bit systems your address space is at maximum 4GB (232 bytes) Spray 3GB of A’s onto the heap? – Note: It’s unlikely you would ever need to spray 3GB of anything as heap locations can be somewhat predictable, even with ASLR

On 64bit heap spraying can’t really be used to bypass ASLR Heap Spraying on 64bit On 64bit heap spraying can’t really be used to bypass ASLR – Good luck spraying anywhere near 264 bytes (spoiler: that’s ~18446744 terabytes) Targeted sprays are still useful in scenarios that you have a partial heap ptr overwrite or need to do some heap grooming

Heap Spray Payloads Pretty common to spray some critical value for your exploit, fake objects, or ROP chains

Q & A