風 水. Heap Feng Shui in JavaScript. Alexander Sotirov. asotirov@determina.com



Similar documents
Bypassing Memory Protections: The Future of Exploitation

Bypassing Browser Memory Protections in Windows Vista

MWR InfoSecurity Advisory. Interwoven Worksite ActiveX Control Remote Code Execution. 10 th March Contents

Hotpatching and the Rise of Third-Party Patches

Custom Penetration Testing

Bypassing Windows Hardware-enforced Data Execution Prevention

esrever gnireenigne tfosorcim seiranib

Format string exploitation on windows Using Immunity Debugger / Python. By Abysssec Inc

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 20: Stack Frames 7 March 08

CVE Adobe Flash Player Integer Overflow Vulnerability Analysis

CORE SECURITY. Exploiting Adobe Flash Player in the era of Control Flow Guard. Francisco Falcon Black Hat Europe 2015 November 12-13, 2015

Software Vulnerabilities

Software Vulnerability Exploitation Trends. Exploring the impact of software mitigations on patterns of vulnerability exploitation

Exploiting nginx chunked overflow bug, the undisclosed attack vector

The V8 JavaScript Engine

I Control Your Code Attack Vectors Through the Eyes of Software-based Fault Isolation. Mathias Payer, ETH Zurich

Attacking Obfuscated Code with IDA Pro. Chris Eagle

Lecture 10: Dynamic Memory Allocation 1: Into the jaws of malloc()

Advanced IBM AIX Heap Exploitation. Tim Shelton V.P. Research & Development HAWK Network Defense, Inc. tshelton@hawkdefense.com

Return-oriented programming without returns

An Analysis of Address Space Layout Randomization on Windows Vista

Heap-based Buffer Overflow Vulnerability in Adobe Flash Player

Windows XP SP3 Registry Handling Buffer Overflow

Off-by-One exploitation tutorial

WLSI Windows Local Shellcode Injection. Cesar Cerrudo Argeniss (

Hacking Techniques & Intrusion Detection. Ali Al-Shemery arabnix [at] gmail

Stack Overflows. Mitchell Adair

Software security. Buffer overflow attacks SQL injections. Lecture 11 EIT060 Computer Security

Hydra. Advanced x86 polymorphic engine. Incorporates existing techniques and introduces new ones in one package. All but one feature OS-independent

Attacking x86 Windows Binaries by Jump Oriented Programming

Turn the Page: Why now is the time to migrate off Windows Server 2003

Introduction. Figure 1 Schema of DarunGrim2

Defense in Depth: Protecting Against Zero-Day Attacks

Stitching the Gadgets On the Ineffectiveness of Coarse-Grained Control-Flow Integrity Protection

Segmentation and Fragmentation

Where s the FEEB? The Effectiveness of Instruction Set Randomization

Reverse Engineering and Computer Security

Attacking Host Intrusion Prevention Systems. Eugene Tsyrklevich

Application-Specific Attacks: Leveraging the ActionScript Virtual Machine

MSc Computer Science Dissertation

Introduction to Information Security

Memory Allocation. Static Allocation. Dynamic Allocation. Memory Management. Dynamic Allocation. Dynamic Storage Allocation

ERNW Newsletter 51 / September 2015

HTC Windows Phone 7 Arbitrary Read/Write of Kernel Memory 10/11/2011

Internet Architecture and Philosophy

Computer Organization and Architecture

Diving into a Silverlight Exploit and Shellcode - Analysis and Techniques

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

The AVR Microcontroller and C Compiler Co-Design Dr. Gaute Myklebust ATMEL Corporation ATMEL Development Center, Trondheim, Norway

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Cache Configuration Reference

SYMANTEC ADVANCED THREAT RESEARCH. An Analysis of Address Space Layout Randomization on Windows Vista

Bypassing Anti- Virus Scanners

W4118: segmentation and paging. Instructor: Junfeng Yang

Persist It Using and Abusing Microsoft s Fix It Patches

Virtual Memory. How is it possible for each process to have contiguous addresses and so many of them? A System Using Virtual Addressing

Government Girls Polytechnic, Bilaspur

Unix Security Technologies. Pete Markowsky <peterm[at] ccs.neu.edu>

Abysssec Research. 1) Advisory information. 2) Vulnerable version

1 The Java Virtual Machine

Project: Simulated Encrypted File System (SEFS)

Kernel Pool Exploitation on Windows 7

MASTERTAG DEVELOPER GUIDE

Sources: On the Web: Slides will be available on:

Buffer Overflows. Code Security: Buffer Overflows. Buffer Overflows are everywhere. 13 Buffer Overflow 12 Nov 2015

Universal XSS via IE8s XSS Filters

Introduction to Data Structures

Introduction to Reverse Engineering

Criteria for web application security check. Version

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

Out Of Control: Overcoming Control-Flow Integrity

TitanMist: Your First Step to Reversing Nirvana TitanMist. mist.reversinglabs.com

Securing software by enforcing data-flow integrity

Reversing C++ Paul Vincent Sabanal. Mark Vincent Yason

Attacking MongoDB. Firstov Mihail

Software Fingerprinting for Automated Malicious Code Analysis

The Beast is Resting in Your Memory On Return-Oriented Programming Attacks and Mitigation Techniques To appear at USENIX Security & BlackHat USA, 2014

How to Sandbox IIS Automatically without 0 False Positive and Negative

Jonathan Worthington Scarborough Linux User Group

Physical Data Organization

Lecture 26: Obfuscation

Ajax Development with ASP.NET 2.0

Buffer Overflows. Security 2011

Yandex.Widgets Quick start

EMET 4.0 PKI MITIGATION. Neil Sikka DefCon 21

Fighting malware on your own

Project 2: Web Security Pitfalls

Sandy. The Malicious Exploit Analysis. Static Analysis and Dynamic exploit analysis. Garage4Hackers

picojava TM : A Hardware Implementation of the Java Virtual Machine

SoK: Eternal War in Memory

CHAPTER 4 System Exploitation

Internals of Windows Memory Management (not only) for Malware Analysis

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

& Data Processing 2. Exercise 3: Memory Management. Dipl.-Ing. Bogdan Marin. Universität Duisburg-Essen

Dynamic Taint Analysis for Automatic Detection, Analysis, and Signature Generation of Exploits on Commodity Software

Hacking your perimeter. Social-Engineering. Not everyone needs to use zero. David Kennedy (ReL1K) Twitter: Dave_ReL1K

Transcription:

風 水 Heap Feng Shui in JavaScript Alexander Sotirov asotirov@determina.com Black Hat Europe 2007

Introduction What is Heap Feng Shui? the ancient art of arranging heap blocks in order to redirect the program control flow to the shellcode Heap Feng Shui in JavaScript precise application data overwrites reliable browser exploitation

Overview State of the art in browser exploitation Internet Explorer heap internals HeapLib JavaScript library Heap manipulation Mitigation

Part I State of the art in browser exploitation

Stack overflows Very hard to exploit in most cases: Target return address SEH frame local variables Protection stack cookies (/GS flag) SafeSEH exception handler table local variable reordering in the Visual C++ compiler

Heap overflows Generic heap exploitation is also difficult: Target doubly-linked list of free chunks heap chunk header lookaside linked list safe unlinking Protection 8-bit header cookie in XP, XOR of the header data in Vista removed in Vista

What's left? Non-array stack overflows very rare Use of uninitialized variables stack variables use after free Application data on the heap application specific memory allocators function pointers C++ object pointers

WebView setslice exploit Uses heap spraying to fill the browser heap with shellcode Overwrites application data in the previous heap chunk Multiple attempts until it either hits an object pointer, or crashes

Heap spraying Developed by Blazde and SkyLined, used by most browser exploits since 2004. var x = new Array(); // Fill 200MB of memory with copies of the // NOP slide and shellcode for (var i = 0; i < 200; i++) { x[i] = nop + shellcode; }

Normal heap layout 300 MB used memory: free memory: 200 MB 100 MB 0 MB

After heap spraying 300 MB used memory: free memory: shellcode: 200 MB shellcode 100 MB Address 0x0C0C0C0C is very likely to contain shellcode. 0 MB

Function pointer overwrite 1. Spray the heap with 200MB of shellcode 2. Overwrite a function pointer with 0x0C0C0C0C 3. Call the function pointer Function pointer 0x0C0C0C0C Shellcode at 0x0C0C0C0C nop slide shellcode

Object pointer overwrite 1. Spray the heap with 200MB of shellcode, using byte 0xC as a nop slide 2. Overwrite an object pointer with 0x0C0C0C0C 3. Call a virtual function of the object Fake object at 0x0C0C0C0C vtable pointer Fake vtable at 0x0C0C0C0C virtual func +0 virtual func +4 Shellcode at 0x0C0C0C0C nop slide shellcode

Unreliable exploitation Heap spraying is a great technique, but the setslice exploit is still not reliable Overwriting application data requires a specific layout of heap chunks We need to control the heap state

Part II Heap Feng Shui

Heap Feng Shui The heap allocator is deterministic Specific sequences of allocations and frees can be used to control the layout used: free:

Heap Feng Shui The heap allocator is deterministic Specific sequences of allocations and frees can be used to control the layout used: free: our data: We allocate two 4KB blocks

Heap Feng Shui The heap allocator is deterministic Specific sequences of allocations and frees can be used to control the layout used: free: our data: We free the first 4KB block

Heap Feng Shui The heap allocator is deterministic Specific sequences of allocations and frees can be used to control the layout used: free: our data: The application allocates a 4KB block and reuses our data

Heap Feng Shui The heap allocator is deterministic Specific sequences of allocations and frees can be used to control the layout used: free: our data: We just exploited an uninitialized data vulnerability

Heap Feng Shui in JavaScript We want to set the heap state before triggering a vulnerability Heap spraying proves that JavaScript can access the system heap We need a way to allocate and free blocks of an arbitrary size

Part III Internet Explorer heap internals

Internet Explorer heap usage JavaScript runtime MSHTML engine ActiveX objects objects strings JavaScript heap Default process heap Dedicated Dedicated Dedicated heaps heaps heaps

JavaScript strings The string "AAAA" is stored as: string size string data null terminator 4 bytes length / 2 bytes 2 bytes 08 00 00 00 41 00 41 00 41 00 41 00 00 00 We can calculate its size in bytes with: bytes = len * 2 + 6 len = (bytes - 6) / 2

String allocation var str1 = "AAAAAAAAAA"; // no allocation // allocates a 10 character string var str2 = str1.substr(0, 10); // allocates a 20 character string var str3 = str1 + str2;

String garbage collection Mark-and-sweep algorithm, frees all unreferenced objects Triggered by a number of heuristics Explicitly by the CollectGarbage() call in Internet Explorer

JavaScript alloc and free var padding = "AAAAAAAAAAAAAAAAAAAAAAAAAAAA " var str; function alloc(bytes) { } str = padding.substr(0, (bytes-6)/2); function free() { } str = null; CollectGarbage(); alloc(0x10000); // allocate 64KB memory block free(); // free memory block

OLEAUT32 allocator Not all string allocations and frees reach the system memory allocator custom memory allocator in OLEAUT32 caching of free memory blocks 4 bins for blocks of different sizes up to 6 free blocks stored in each bin

OLEAUT32 alloc function bin = the right bin for the requested size if (bin not empty) find a block in the bin > requested size if (found) return block else return sysalloc(size) else return sysalloc(size)

OLEAUT32 free function bin = the right bin for the block size if (bin not full) add block to bin else find the smallest block in the bin if (smallest block < new block) sysfree(smallest block) add new block to bin else sysfree(new block)

Bypassing the cache Our freed blocks will go into the cache Freeing 6 maximum sized blocks for each bin will push all smaller blocks out Allocating the 6 blocks again will leave the cache empty When the cache is empty, allocations will come from the system heap

Plunger Technique 1. Allocate 6 maximum size blocks 2. Allocate our blocks 3. Free our blocks 4. Free 6 maximum size blocks 5. Allocate 6 maximum size blocks OLEAUT32 cache maximum size blocks empty

Plunger Technique 1. Allocate 6 maximum size blocks 2. Allocate our blocks 3. Free our blocks 4. Free 6 maximum size blocks 5. Allocate 6 maximum size blocks OLEAUT32 cache maximum size blocks our blocks empty

Plunger Technique 1. Allocate 6 maximum size blocks 2. Allocate our blocks 3. Free our blocks 4. Free 6 maximum size blocks 5. Allocate 6 maximum size blocks OLEAUT32 cache maximum size blocks our blocks

Plunger Technique 1. Allocate 6 maximum size blocks 2. Allocate our blocks 3. Free our blocks 4. Free 6 maximum size blocks 5. Allocate 6 maximum size blocks OLEAUT32 cache maximum size blocks free blocks

Plunger Technique 1. Allocate 6 maximum size blocks 2. Allocate our blocks 3. Free our blocks 4. Free 6 maximum size blocks 5. Allocate 6 maximum size blocks OLEAUT32 cache maximum size blocks free blocks empty

Part IV HeapLib - JavaScript heap manipulation library

Introducing HeapLib Supports Internet Explorer 5-7 Object oriented API Functions for: heap logging and debugging allocation and freeing of blocks with arbitrary size and contents high-level heap manipulation function (not yet supported on Vista)

Hello world! <script src="heaplib.js"></script> <script> var heap = new heaplib.ie(); heap.gc(); heap.debugheap(true); heap.alloc(512); heap.alloc("bbbbb", "foo"); heap.free("foo"); heap.debugheap(false); </script>

HeapLib Demo

Part V Windows Heap Manipulation

Windows Heap Overview Heap Pre-Vista FreeList[0] FreeList[1] FreeList[127] 1024 2080 8192 8 1016 1016 Lookaside Lookaside Table Lookaside[0] Lookaside[1] Lookaside[126] 8 8 1016

Free Algorithm if size >= 512KB free with VirtualFree return if size < 1KB and lookaside not full add to lookaside list return coalesce block with free blocks around it if size < 1KB add to FreeList[size/8] else add to FreeList[0]

Allocate Algorithm if size >= 512KB alloc with VirtualAlloc return if size < 1KB if lookaside not empty return a block from the lookaside if FreeList[size/8] not empty return a block from FreeList[size/8] if FreeList[0] not empty return a block from FreeList[0] allocate more memory with VirtualAlloc

Defragmenting the heap To allocate two consecutive blocks, we need to defragment the heap. for (var i = 0; i < 1000; i++) heap.alloc(0x2010); used: free:

Defragmenting the heap To allocate two consecutive blocks, we need to defragment the heap. for (var i = 0; i < 1000; i++) heap.alloc(0x2010); used: free: our blocks:

Putting a block on the FreeList To put a block on the free list, we need to ensure that it is not coalesced. heap.alloc(0x2010, "foo"); heap.alloc(0x2010); heap.alloc(0x2010, "foo"); heap.free("foo"); used: free: our blocks:

Putting a block on the FreeList To put a block on the free list, we need to ensure that it is not coalesced. heap.alloc(0x2010, "foo"); heap.alloc(0x2010); heap.alloc(0x2010, "foo"); heap.free("foo"); used: free: our blocks:

Emptying the lookaside To empty the lookaside, allocate enough blocks of the same size. for (var i = 0; i < 100; i++) heap.alloc(512);

Freeing to the lookaside To put a block on the lookaside, empty it and free the block. for (var i = 0; i < 100; i++) heap.alloc(512); heap.alloc(512, "foo"); heap.free("foo");

Object pointer overwrite The lookaside linked list can be used to exploit object pointer overwrites without heap spraying. 1. Empty the lookaside 2. Build a fake vtable block 3. Free the fake vtable to the lookaside 4. Overwrite an object pointer with the address of the lookaside head 5. Call a virtual function of the object

Object pointer overwrite mov ecx, dword ptr [eax] ; get the vtable address push eax ; push the 'this' pointer call dword ptr [ecx+08h] ; call virtual func Lookaside head (fake object) vtable pointer Free block (fake vtable) NULL jmp short +4 virtual func +8 shellcode jmp ecx NULL disassembles as two sub [eax], al instructions

Exploit Demo

Mitigation Heap isolation Non-determinism in the heap allocator

Questions? asotirov@determina.com