Heap Overflow Attacks.

Slides:



Advertisements
Similar presentations
Secure Coding in C and C++ Dynamic Memory Management
Advertisements

Smashing the Stack for Fun and Profit
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Dynamic Memory Allocation I Topics Basic representation and alignment (mainly for static memory allocation, main concepts carry over to dynamic memory.
An overview Secologic Treffen Martin Johns
Secure Coding in C and C++ Dynamic Memory Management Lecture 5 Acknowledgement: These slides are based on author Seacord’s original presentation.
Gabe Kanzelmeyer CS 450 4/14/10.  What is buffer overflow?  How memory is processed and the stack  The threat  Stack overrun attack  Dangers  Prevention.
Buffer Overflow. Process Memory Organization.
C and Data Structures Baojian Hua
Memory Layout C and Data Structures Baojian Hua
1 RISE: Randomization Techniques for Software Security Dawn Song CMU Joint work with Monica Chew (UC Berkeley)
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
Secure Coding in C and C++ Pointer Subterfuge Lecture 5 Acknowledgement: These slides are based on author Seacord’s original presentation.
Chapter 6 Buffer Overflow. Buffer Overflow occurs when the program overwrites data outside the bounds of allocated memory It was one of the first exploited.
Software attacks Lorenzo Dematté Software attacks Advanced buffer overflow: heap smashing.
Computer Science Detecting Memory Access Errors via Illegal Write Monitoring Ongoing Research by Emre Can Sezer.
Mitigation of Buffer Overflow Attacks
CNIT 127: Exploit Development Ch 4: Introduction to Format String Bugs.
Computer Security 2015 – Ymir Vigfusson. 2  We have talked extensively about stack overflows  But those are not as common anymore  Heap overflows 
Smashing the Stack Overview The Stack Region Buffer Overflow
CNIT 127: Exploit Development Ch 4: Introduction to Heap Overflows
What is exactly Exploit writing?  Writing a piece of code which is capable of exploit the vulnerability in the target software.
Worm enabling exploits Cyber Security Lab Spring ‘10.
C LANGUAGE Characteristics of C · Small size
CSE 351 Dynamic Memory Allocation 1. Dynamic Memory Dynamic memory is memory that is “requested” at run- time Solves two fundamental dilemmas: How can.
About Exploits Writing ABOUT EXPLOITS WRITING Gerardo Richarte 
Int main( ) { x = a(); } int a() { y = b(); } int b() { z = c(); } int c() { } 1.
VM: Chapter 7 Buffer Overflows. csci5233 computer security & integrity (VM: Ch. 7) 2 Outline Impact of buffer overflows What is a buffer overflow? Types.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
Heap Overflows. What is a Heap? malloc(), free(), realloc() Stores global variables Automatic memory allocation/deallocation Allocated at runtime Implemented.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Variables Bryce Boe 2012/09/05 CS32, Summer 2012 B.
Dynamic Allocation in C
Shellcode COSC 480 Presentation Alison Buben.
Buffer Overflow By Collin Donaldson.
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.
Stack and Heap Memory Stack resident variables include:
Introduction to Information Security
The Hardware/Software Interface CSE351 Winter 2013
CSC 495/583 Topics of Software Security Heap Exploitation (2)
CSC 495/583 Topics of Software Security Heap Exploitation
CSC 495/583 Topics of Software Security Stack Overflows (2)
CSC 495/583 Topics of Software Security Return-oriented programming
Secure Coding in C and C++ Pointer Subterfuge
Basic notes on pointers in C
Memory Management Chapter 10 11/24/2018 Crowley OS Chap. 10.
Dynamic Memory Allocation
Software Security Lesson Introduction
Format String.
CSC 495/583 Topics of Software Security Format String Bug (2) & Heap
Smashing the Stack for Fun and Profit
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2015.
The future of Software Security Dr. Si Chen
Virtual Memory: Systems CSCI 380: Operating Systems
Program Execution in Linux
Dynamic Memory A whole heap of fun….
Processes in Unix and Windows
Dynamic Memory And Objects
“Using the magic against the magician”
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2016.
Understanding and Preventing Buffer Overflow Attacks in Unix
Buddy Allocation CS 161: Lecture 5 2/11/19.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2013.
System and Cyber Security
Week 3: Format String Vulnerability
Return-to-libc Attacks
Presentation transcript:

Heap Overflow Attacks

What is a heap? Heap is a collection of variable-size memory chunks allocated by the program e.g., malloc(), free() in C, creating a new object in Java creating a new object in Java script Heap management system controls the allocation, de-allocation, and reclamation of memory chunks. To do this some meta data is necessary for book-keeping

Heap overflow attacks What if heap memory is corrupted? If a buffer is allocated on the heap and overflown, we could overwrite the heap meta data This can allow us to modify any memory location with any value of our chosen This could lead to running arbitrary code

Doug Lea’s malloc and free P == 1: previous chunk is in use P == 0: previous chunk is free Size includes the first two control words

Double-linked free chunk list struct chunk { int prev_size; int size; struct chunk *fd; struct chunk *bk; };

heap.c #define BUFSIZE 128 int main(int argc, char *argv[]) { char *a, *b, *c; if(argc < 2) { printf("Usage: %s <buffer>\n", argv[0]); exit(-1); } a = (char *) malloc(BUFSIZE); b = (char *) malloc(BUFSIZE+16); c = (char *) malloc(BUFSIZE+32); printf("address of a: %p\n", a); printf("address of b: %p\n", b); printf("address of c: %p\n", c); strcpy(b, "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"); strcpy(c, "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"); strcpy(a, argv[1]); free(a); free(b); free(c);

The heap in memory free(a) bin_forward bin_back prev_free_size=0 Size=0x88 1 char *a free(a) Data (AAAAAAAAAAAAAAAAAAAAA) prev_free_size=0 Size=0x98 1 char *b Data (BBBBBBBBBBBBBBBBBBBBBB) prev_free_size=0 Size=0xa8 1 char *c Data (CCCCCCCCCCCCCCCCCCCCC)

After free(a) free(a) free(b) bin_forward bin_back prev_free_size=0 Size=0x88 1 char *a free(a) forward back prev_free_size=0x88 Size=0x98 char *b free(b) Data (BBBBBBBBBBBBBBBBBBBBBB) prev_free_size=0 Size=0xa8 1 char *c Data (CCCCCCCCCCCCCCCCCCCCC)

Data (CCCCCCCCCCCCCCCCCCCCC) After free(b) bin_forward bin_back prev_free_size=0 Size=0x120 1 char *a free(a) forward back char *b free(b) prev_free_size=0x120 Size=136 char *c Data (CCCCCCCCCCCCCCCCCCCCC)

What if “b” was freed first? bin_forward bin_back prev_free_size=0 Size=0x88 1 char *a free(a) Data (AAAAAAAAAAAAAAAAAAAAA) prev_free_size=0 Size=0x98 1 char *b free(b) forward back prev_free_size=0x98 Size=0xa8 char *c Data (CCCCCCCCCCCCCCCCCCCCC)

Data (CCCCCCCCCCCCCCCCCCCCC) Then “a” was freed bin_forward bin_back prev_free_size=0 Size=0x120 1 char *a forward back char *b prev_free_size=0x120 Size=0xa8 char *c Data (CCCCCCCCCCCCCCCCCCCCC)

Key Observation When the chunk (A) to be free’ed is followed by a free chunk (B), chunk B will be unlinked from the free list, and A will be inserted into the free list. If the heap meta data in B has been corrupted, the unlink operation will be dangerous. Why?

Remove a chunk from the double-linked free list unlink(P) P->fd->bk = P->bk; P->bk->fd = P->fd; Will create some trouble for us P forward forward forward back back back If the “fd” pointer is corrupted, we can control which memory cell to be modified If the “bk” pointer is corrupted, we can control what value to put into the memory cell Overwrite the “forward” field with “where-12”. Overwrite the “back” field with “what”.

A heap overflow attack Overwrite a heap memory chunk by running past the allocated space. Create a fake heap structure that pretends to be a free chunk. When the current chunk is free()’d, an arbitrary memory overwrite will occur. We can control the “where” and “what” of the memory overwrite.

The “where” By modifying certain memory locations, we can modify the EIP and make it point to injected malicious code. The Global Offset Table (GOT) contains the entry addresses of dynamically linked library functions (e.g. printf, malloc, free, exit, strcpy, etc.) We can overwrite a GOT entry that will likely be used by the program $ objdump -R heap heap: file format elf32-i386 DYNAMIC RELOCATION RECORDS OFFSET TYPE VALUE 0804975c R_386_GLOB_DAT __gmon_start__ 08049744 R_386_JUMP_SLOT malloc 08049748 R_386_JUMP_SLOT __libc_start_main 0804974c R_386_JUMP_SLOT printf 08049750 R_386_JUMP_SLOT exit 08049754 R_386_JUMP_SLOT free 08049758 R_386_JUMP_SLOT strcpy

The “what” The “what” will be the address of the malicious code we injected into the buffer. But, P->bk->fd=P->fd will “puncture” our shellcode from bytes 8-12. Thus the shell code must “jump over” this hole.

Remove a chunk from the double-linked free list unlink(P) P->fd->bk = P->bk; P->bk->fd = P->fd; Will create some trouble for us P forward forward forward back back back If the “fd” pointer is corrupted, we can control which memory cell to be modified If the “bk” pointer is corrupted, we can control what value to put into the memory cell Overwrite the “forward” field with “where-12”. Overwrite the “back” field with “what”.

How to make free() think the chunk after “a” is free prev_free_size=0 Size=0x88 1 char *a Data (AAAAAAAAAAAAAAAAAAAAA) prev_free_size=0 Size=0x98 1 char * b Data (BBBBBBBBBBBBBBBBBBBBBB) prev_free_size=0x98 The second next chunk’s “size” word shall be an even number Size=0xa8 char *c Data (CCCCCCCCCCCCCCCCCCCCC)

Assembled heap-overflow payload buffer XXXX YYYY This word will be overwritten by the second part of unlink statement \x90\x90\x90\x90 nop, nop, jmp + 4 ZZZZ Shellcode… Shellcode… fake heap structure Padding 0xffffffff (-1) 0xffffffff (-1) GOT_entry - 12 buffer + 8