Software attacks Lorenzo Dematté Software attacks Advanced buffer overflow: heap smashing.

Slides:



Advertisements
Similar presentations
Windows Heap Exploitation (Win2KSP0 through WinXPSP2)
Advertisements

Windows Heap Overflows David Litchfield. Windows Heap Overflows Introduction This presentation will examine how to exploit heap based buffer overflows.
Part IV: Memory Management
Dynamic Memory Management
Defenses. Preventing hijacking attacks 1. Fix bugs: – Audit software Automated tools: Coverity, Prefast/Prefix. – Rewrite software in a type safe languange.
Memory management.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
ITEC 352 Lecture 27 Memory(4). Review Questions? Cache control –L1/L2  Main memory example –Formulas for hits.
Andrew Roths Fermin J. Serna MSRC Engineering and MSEC Science Microsoft Corporation.
DIEHARDER: SECURING THE HEAP. Previously in DieHard…  Increase Reliability by random positioning of data  Replicated Execution detects invalid memory.
Breno de MedeirosFlorida State University Fall 2005 Buffer overflow and stack smashing attacks Principles of application software security.
Memory/Storage Architecture Lab Computer Architecture Virtual Memory.
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
Beyond Stack Smashing: Recent Advances in Exploiting Buffer Overruns Jonathan Pincus Microsoft Research Brandon Baker Microsoft Carl Hartung CSCI 7143:
CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Memory Management - 2 CS 342 – Operating Systems Ibrahim Korpeoglu Bilkent.
Memory Management (continued) CS-3013 C-term Memory Management CS-3013 Operating Systems C-term 2008 (Slides include materials from Operating System.
Memory Management 1 CS502 Spring 2006 Memory Management CS-502 Spring 2006.
CS-3013 & CS-502, Summer 2006 Memory Management1 CS-3013 & CS-502 Summer 2006.
Computer Organization and Architecture
1 Memory Management in Representative Operating Systems.
EECE476: Computer Architecture Lecture 27: Virtual Memory, TLBs, and Caches Chapter 7 The University of British ColumbiaEECE 476© 2005 Guy Lemieux.
Memory Allocation CS Introduction to Operating Systems.
Rensselaer Polytechnic Institute CSC 432 – Operating Systems David Goldschmidt, Ph.D.
Operating System Chapter 7. Memory Management Lynn Choi School of Electrical Engineering.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Reliable Windows Heap Exploits
Chapter 3.5 Memory and I/O Systems. 2 Memory Management Memory problems are one of the leading causes of bugs in programs (60-80%) MUCH worse in languages.
Computer Science Detecting Memory Access Errors via Illegal Write Monitoring Ongoing Research by Emre Can Sezer.
Mitigation of Buffer Overflow Attacks
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
Windows Memory Architecture 井民全製作. A Process ’ s Virtual Address Space Every Process has its own private virtual address 32-bits processes  4 GB address.
MICHALIS POLYCHRONAKIS(COLUMBIA UNIVERSITY,USA), KOSTAS G. ANAGNOSTAKIS(NIOMETRICS, SINGAPORE), EVANGELOS P. MARKATOS(FORTH-ICS, GREECE) ACSAC,2010 Comprehensive.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
1 Memory Management Basics. 2 Program P Basic Memory Management Concepts Address spaces Physical address space — The address space supported by the hardware.
Smashing the Stack Overview The Stack Region Buffer Overflow
Exploitation possibilities of memory related vulnerabilities
CNIT 127: Exploit Development Ch 4: Introduction to Heap Overflows
CNIT 127: Exploit Development Ch 8: Windows Overflows Part 2.
Reliable Windows Heap Exploits Matt Conover & Oded Horovitz.
Eusecwest March 2,  Memory corruption vulnerability exposure can be mitigated through memory hardening practices  OS vendors have a unique opportunity.
Buffer overflow and stack smashing attacks Principles of application software security.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Virtual Memory Implementation.
CSE 351 Dynamic Memory Allocation 1. Dynamic Memory Dynamic memory is memory that is “requested” at run- time Solves two fundamental dilemmas: How can.
CNIT 127: Exploit Development Ch 8: Windows Overflows Part 1.
COMP091 – Operating Systems 1 Memory Management. Memory Management Terms Physical address –Actual address as seen by memory unit Logical address –Address.
Memory Management. 2 How to create a process? On Unix systems, executable read by loader Compiler: generates one object file per source file Linker: combines.
VM: Chapter 7 Buffer Overflows. csci5233 computer security & integrity (VM: Ch. 7) 2 Outline Impact of buffer overflows What is a buffer overflow? Types.
Beyond Stack Smashing: Recent Advances In Exploiting Buffer Overruns Jonathan Pincus and Brandon Baker Microsoft Researchers IEEE Security and.
Virtual Memory 1 Computer Organization II © McQuain Virtual Memory Use main memory as a “cache” for secondary (disk) storage – Managed jointly.
Introduction to Information Security
CS703 - Advanced Operating Systems
Paging COMP 755.
Dynamic Memory Allocation
Optimizing Malloc and Free
Advanced Buffer Overflow: Pointer subterfuge
CS399 New Beginnings Jonathan Walpole.
Virtual Memory CSCI 380: Operating Systems Lecture #7 -- Review and Lab Suggestions William Killian.
CSC 495/583 Topics of Software Security Format String Bug (2) & Heap
CSE 451: Operating Systems Autumn 2005 Memory Management
Operating System Chapter 7. Memory Management
CSE451 Virtual Memory Paging Autumn 2002
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
CSCI 380: Operating Systems William Killian
Computer Architecture
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
Virtual Memory Use main memory as a “cache” for secondary (disk) storage Managed jointly by CPU hardware and the operating system (OS) Programs share main.
COMP755 Advanced Operating Systems
Windows Heap Overflows
CS444/544 Operating Systems II Virtual Memory
Presentation transcript:

Software attacks Lorenzo Dematté Software attacks Advanced buffer overflow: heap smashing

Software attacks Lorenzo Dematté Heap overflow void func(void* arg, int len) { char* ptr = malloc(100); memcpy(ptr, arg, len); //buffer overflow if len> } Overflows on the heap can be exploited as well as stack overflows However, this is a data-overwrite attack. How can we use it to inject an arc (redirect program control flow) and allow arbitrary code execution? We don’t have RETs on the heap! => This is possible due to arbitrary memory writing as we’ll see later

Software attacks Lorenzo Dematté Heap overflow One paper on secure coding suggested to solve the problem of stack overflows moving the buffers to heap! This is not the solution: exploiting a heap overflow is harder, but feasible. See: –Heap Buffer Overrun in JPEG Processing (GDI+) Allows Code Execution (MS04-028) (it’s sufficient to preview image in explorer – IE) gdiplus.dll –Windows RPC DCOM Long Filename Heap Overflow Exploit (MS03-039) rpcss.exe

Software attacks Lorenzo Dematté Heap overflow They are much more architecture dependent than stack overflows Heap is a more complex architecture – its layout is influenced by the OS and by the C- C++ runtime We are going to explore some possible exploits for Win32 A lot of exploits for linux-gcc exist as well (w00w00 on heap) <- room for a seminary!

Software attacks Lorenzo Dematté The Heap API The Virtual Memory Manager allocates and deallocates with the granularity of one page (4k) Process Address Space 0x x7fffffff VirtualAlloc(lpAddress, 2, AllocationType, flProtect ); Only 2 bytes A whole page (4k) is allocated Even the stack region grows and shrink in pages (done automatically by the OS)

Software attacks Lorenzo Dematté The Heap API We need a more flexible structure!! We want not to waste space and have allocations of few bytes char* buff = malloc(2 * sizeof(char)); and of MBs void* buff = malloc( ); This is done using the structure called Heap API for the heap is provided by the C runtime library (malloc, free, delete, new) In Win32, Heap API is provided by the OS too!

Software attacks Lorenzo Dematté The Heap API Kernel32.dll GlobalAllocLocalAllocHeapAlloc RtlAllocateHeap mallocnew msvcrt.dll ntdll.dll GlobalFreeLocalFreeHeapFree RtlFreeHeap freedelete msvcrt.dll ntdll.dll Kernel32.dll

Software attacks Lorenzo Dematté The Heap structures In order to handle different size, and cope well with problems (fragmentation) the Heap has a complex structure It uses several algorithms too: –Allocation algorithm (different strategies for different size) –Free algorithm –Coalesce (fusion of 2 adiacent free segments)

Software attacks Lorenzo Dematté The Heap structures PEB Default Heap 0x0010Default Heap 0x0080Heaps Count 0x0090Heap List 0x x This is the structure that holds Process properties. Its location is fixed at 7ffdf000 (or go via TEB -> PEB) mov eax, dword ptr fs:[18] mov eax, dword ptr[eax+0x30] So we can easily access to the Heap address. This will be useful later!

Software attacks Lorenzo Dematté The Heap structures Segments Look aside Table Segment Table Free Lists Table Free list usage bit map Virtual Allocation list typedef struct _LIST_ENTRY { struct _LIST_ENTRY *Flink; struct _LIST_ENTRY *Blink; } LIST_ENTRY;

Software attacks Lorenzo Dematté The Heap structures Management Structures When a heap is first created there are two pointers that point to the first free block set in FreeList[0]. Assuming the heap base address is 0x then first available block can be found at 0x x (FreeList[0].Flink) = 0x (First Free Block) 0x C (FreeList[0].Blink) = 0x (First Free Block) 0x (First Free Block) = 0x (FreeList[0]) 0x C (First Free Block+4) = 0x (FreeList[0]) The heap management structures reside in the heap!

Software attacks Lorenzo Dematté The Heap structures When an allocation occurs these pointers are updated accordingly. As more allocations and frees occur these pointers are continually updated and in this fashion allocated blocks are tracked in a doubly linked list. When a heap based buffer is overflowed the control information is overwritten When the buffer (allocated block) is freed and it comes to updating the pointers in the FreeList array there’s going to be an access violation

Software attacks Lorenzo Dematté The Heap structures Access violation / Memory overwrite Observe the following code: mov dword ptr [ecx],eax If we own both EAX and ECX we have an arbitrary DWORD overwrite. We can overwrite the data at any 32bit address with a 32bit value of our choosing. In RtlHeapFree we have such a line of code!!

Software attacks Lorenzo Dematté Heap Structures this The instructions to remove an entry from a double linked list are: prev_chunk->FLink = next_chunk next_chunk->BLink = prev_chunk Where next_chunk is this->FLink and prev_chunk is this- >BLink

Software attacks Lorenzo Dematté Heap smashing If we assume that prev_chunk->FLink is loaded in ECX (and since prev_chunk->FLink == &Prev_chunk + 0 in ECX is prev_chunk ) And that next_chunk is in EAX… If we build a fake header with prev_chunk and next_chunk of our choiche we have (from the previous code) mov dword ptr [ecx],eax EAX (Address A) written in the address pointed by ECX (Address B) Arbitrary memory overwrite will happen on free of our faked chunk!

Software attacks Lorenzo Dematté Heap smashing ntdll!RtlpCoalesceFreeBlocks+0x2fb: 784ac8d4 call ntdll!RtlpUpdateIndexRemoveBlock (784624a3) 784ac8d9 mov ecx,[edi+0xc] 784ac8dc mov eax,[edi+0x8] 784ac8df cmp eax,ecx 784ac8e1 mov [ecx],eax ds:0023:f1f1f1f1=???????? 784ac8e3 mov [eax+0x4],ecx In fact, two heap algorithms (Free of very large (I.e. virtually allocated) blocks and coalesce) adjust the list in this way!!

Software attacks Lorenzo Dematté The Heap structures Previous chunk size Self Size Segment Index Flags Unused bytes Tag index (Debug) – Busy 02 – Extra present 04 – Fill pattern 08 – Virtual Alloc 10 – Last entry 20 – FFU1 40 – FFU2 80 – Don ’ t coalesce

Software attacks Lorenzo Dematté The Heap structures Free chunk structure – 16 Bytes Previous chunk size Self Size Segment Index Flags Unused bytes Tag index (Debug) Next chunkPrevious chunk

Software attacks Lorenzo Dematté Heap smashing Exploit: fake a freed buffer Utilize coalescing algorithms of the heap Arbitrary overwrite happens when either the overflowed buffer gets freed (usually guaranteed) or when the buffer AFTER the faked buffer gets freed Previous chunk Size < 40 0x40 Address A Address B 40 – FFU2 This is overwritten on the old control structure (overflow) Our buffer

Software attacks Lorenzo Dematté The Heap structures Virtually Allocated chunk structure – 32 Bytes Previous chunk size Self Size Segment Index Flags Unused bytes Tag index (Debug) Next chunkPrevious chunk Commit sizeReserve size

Software attacks Lorenzo Dematté Heap smashing Exploit: fake a virtual allocated block Arbitrary memory overwrite will happen when the buffer we faked is freed (the one next to the overflowed buffer) < 0x40 | 9 Address AAddress B 01 – Busy 08 – Virtual Alloc This is written at the end of our buffer This is overwritten on the old control structure (overflow)

Software attacks Lorenzo Dematté Heap smashing: coalesce algo Buffer already freedBuffer removed from free list This buffer is just freed Buffer placed back in free list AB C B.BLink.FLink (A.FLink) = B.Flink (C) B.FLink.Blink (C.BLink) = B.Blink (A) *(B.Blink) = B.Flink => *AddressB = AddressA Arbitrary memory overwrite!

Software attacks Lorenzo Dematté Heap smashing: repairing the heap Many of the Windows API calls use the default process heap. After the overflow the heap is corrupt, so there will be surely an access violation. We then repair the heap following Litchfield’s method: we reset the heap making it “appear” as if it is a fresh new heap.

Software attacks Lorenzo Dematté Heap smashing: repairing the heap Get a pointer to the Thread Information Block at fs:[18] Get a pointer to the Process Environment Block from the TEB. Get a pointer to the default process heap from the PEB We now have a pointer to the heap. Read the TotalFreeSize dword of the heap structure (at offset 0x28) Write this to our heap control structure. In the heap control structure, also set the flags to 0x14 (first segment) and and the next 2 bytes to 0 At heap_base+0x178 we have FreeLists[0]. Set FreeLists[0].Flink and write edx into FreeLists[0].Blink to this Finally set the pointers at the end of our block to point to FreeLists[0]

Software attacks Lorenzo Dematté Arbitrary memory write So, as in the data pointer overwrite, we have an arbitrary DWORD memory overwrite This can be used to overwrite particular function pointers (Exception handlers VEH + SEH, atexit(), stack cookie exception handler, Lock and Win32k function pointers in PEB) These arguments will be place for a seminar by one of you! =)