Top Banner
Mac OS Rootkits Nanika & TT http://exploitspace.blogspot.com
70

VXCON 2012 - Advanced Mac OS Rootkit

Nov 11, 2014

Download

Documents

sttsai

In this presentation, we will start from basic details, and try to provide a comprehensive view of rootkit on Mac OS, including both user and kernel mode.

Not only introducing general rootkit techniques, we will also disclose new and more advanced rootkit tricks by digging into more kernel objects and data structures. Besides the advanced rootkit technique, a new way to anti-tracing (anti-dtrace) will be introduced as well.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: VXCON 2012 - Advanced Mac OS Rootkit

Mac OS RootkitsNanika & TT

http://exploitspace.blogspot.com

Page 2: VXCON 2012 - Advanced Mac OS Rootkit

About Us

Taiwan

Page 3: VXCON 2012 - Advanced Mac OS Rootkit

Nanika Pan

Trend Micro Staff Research Engineer Core Tech Department

Speech Black Hat USA 2011 / 2012 Syscan Singapore/Taipei/Hong Kong 08/10 Hacks in Taiwan Conference 05/06/07/09/10/12

Research Vulnerability discovery and analysis Exploit techniques Malware detection Mobile security

Page 4: VXCON 2012 - Advanced Mac OS Rootkit

Sung-ting Tsai (TT)

Trend Micro Leader of an advanced threat research team. Core Tech Department

Research New security technology Malicious document Malware auto-analyzing system (sandbox technologies) Malware detection System vulnerability and protection Mobile security

Speech Black Hat USA 2011 / 2012 Codegate 2012 Syscan 10’ / 12’ HITCon 08’

Page 5: VXCON 2012 - Advanced Mac OS Rootkit

warm-up

defaults write com.apple.Finder AppleShowAllFiles 1

Mac OS Activity Monitor (Utilities)

Page 6: VXCON 2012 - Advanced Mac OS Rootkit

plist file

Page 7: VXCON 2012 - Advanced Mac OS Rootkit
Page 8: VXCON 2012 - Advanced Mac OS Rootkit

/etc/rc.*

/etc/profile

/etc/bashrc

~/.bashrc

~/.profile

~/.login

/etc/inetd.conf

/etc/xinetd.d/

/etc/crontab

/etc/mach_init_per_user.d/

/etc/mach_init.d

Page 9: VXCON 2012 - Advanced Mac OS Rootkit

XPCServices

Page 10: VXCON 2012 - Advanced Mac OS Rootkit

/System/Library/LaunchAgents/

~/Library/LaunchAgents/

/System/Library/LaunchDaemons

~/Library/LaunchDaemons

~/Library/Preferences/com.apple.loginitems.plist

~/Library/StartupItems

/System/Library/StartupItems

Page 11: VXCON 2012 - Advanced Mac OS Rootkit

http://blog.mktime.com/archive/365.html

sudo defaults write com.apple.loginwindow LoginHook /path/to/login.sh

sudo defaults write com.apple.loginwindow LogoutHook /path/to/logout.sh

Page 13: VXCON 2012 - Advanced Mac OS Rootkit

get root

sudo

AuthorizationExecuteWithPrivileges

http://www.michaelvobrien.com/blog/2009/07/authorizationexecutewithprivileges-a-simple-example/

Page 14: VXCON 2012 - Advanced Mac OS Rootkit

Xcode build the file location

Page 15: VXCON 2012 - Advanced Mac OS Rootkit

Debug Argv

Page 16: VXCON 2012 - Advanced Mac OS Rootkit

gcc -framework CoreServices

AuthorizationExecuteWithPrivileges

Page 17: VXCON 2012 - Advanced Mac OS Rootkit

• let's go crazy hacking

Page 18: VXCON 2012 - Advanced Mac OS Rootkit

• Mac file format

• Lion vs Mountain Lion

• usermode aslr

• kernel aslr

• user rootkit

• user mode hook

• kernel rootkit

• syscall hook

• machtrap hook

• DKOM find process hide

• DKOM Advance process hide

• Anti Dtrace

Page 20: VXCON 2012 - Advanced Mac OS Rootkit

Header

Page 21: VXCON 2012 - Advanced Mac OS Rootkit

Entry Point

Page 22: VXCON 2012 - Advanced Mac OS Rootkit

LINKEDIT

Page 23: VXCON 2012 - Advanced Mac OS Rootkit

LC_DYLD_INFO

Page 24: VXCON 2012 - Advanced Mac OS Rootkit

_la_symbol_ptr Section Header

Page 25: VXCON 2012 - Advanced Mac OS Rootkit

_la_symbol_ptr

Page 26: VXCON 2012 - Advanced Mac OS Rootkit
Page 27: VXCON 2012 - Advanced Mac OS Rootkit

How to find Export Function Address

Page 28: VXCON 2012 - Advanced Mac OS Rootkit
Page 29: VXCON 2012 - Advanced Mac OS Rootkit

• SYMTable Address=LINKEDIT imagebase+(SYMTable Address-LINKEDIT FILEOffest)

• ex:string table->0x8fe5e000(0x3a344-0x35000)

Page 30: VXCON 2012 - Advanced Mac OS Rootkit
Page 32: VXCON 2012 - Advanced Mac OS Rootkit

GDB attach

Page 33: VXCON 2012 - Advanced Mac OS Rootkit
Page 34: VXCON 2012 - Advanced Mac OS Rootkit

GDB open

• NO ASLR

• http://reverse.put.as/2011/08/11/how-gdb-disables-aslr-in-mac-os-x-lion/

Page 35: VXCON 2012 - Advanced Mac OS Rootkit

ASLR usermode

• 10.7.x X64 mode main thread stack leak dyld image base

• 10.8.x random

Page 36: VXCON 2012 - Advanced Mac OS Rootkit

osx10.7.x

Page 37: VXCON 2012 - Advanced Mac OS Rootkit

Kernel ASLR

• mach_kernel 10.7.x no aslr

• File Function Addr = Mem Function Addr

• mach_kernel 10.8.x aslr

Page 38: VXCON 2012 - Advanced Mac OS Rootkit

Get ASLR Kernel base

• __asm__ volatile ("sidt %0": "=m" (idtr));

• idt table function address is always in kernel function address

• search MACHO Magic to find kernel base

Page 39: VXCON 2012 - Advanced Mac OS Rootkit

User mode hook

• DYLD_INSERT_LIBRARIES

• dynamic inject

Page 40: VXCON 2012 - Advanced Mac OS Rootkit

• LD_PRELOAD=./gethostname.dylib FAKE_HOSTNAME=foo hostname foo

• DYLD_FORCE_FLAT_NAMESPACE=1 DYLD_INSERT_LIBRARIES=lib_overrides.dylib overrides_test

Page 41: VXCON 2012 - Advanced Mac OS Rootkit

FILE * (*original_fopen) (const char *, const char *) =NULL;

FILE * fopen(const char * filename, const char * mode){    if (!original_fopen)       original_fopen = dlsym(RTLD_NEXT, "fopen");

    printf("== fopen: {%s,%s} ==\n", filename, mode);    FILE* f = original_fopen(filename, mode);    return f;}

gcc -Wall -o lib_overrides.dylib -dynamiclib lib_overrides.c

Page 42: VXCON 2012 - Advanced Mac OS Rootkit

int main(int argc, char const *argv[])

{    char hello[] = "hello world";        FILE *fp = fopen("hello.txt", "w");       if (fp) {        fwrite(hello, 1, strlen(hello), fp);        fclose(fp);    }

    return 0;}

gcc -Wall -o overrides_test overrides_test.c

Page 43: VXCON 2012 - Advanced Mac OS Rootkit

• if you do not see any event, don’t be surprised. Because this hook only changed the _la_symbol_ptr.

• http://tlrobinson.net/blog/category/gcc/

Page 44: VXCON 2012 - Advanced Mac OS Rootkit

• mach_port_t!remoteTask = 0;

• pid_t!targetProcess;

• task_for_pid( mach_task_self(), targetProcess, &remoteTask );

Dynamic inject

Page 45: VXCON 2012 - Advanced Mac OS Rootkit

• kern_return_t vm_read

(vm_task_t target_task,

vm_address_t address,

vm_size_t size,

size data_out,

target_task data_count);

• kern_return_t vm_write

(vm_task_t target_task,

vm_address_t address,

pointer_t data,

mach_msg_type_number_t data_count);

• 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);

Page 46: VXCON 2012 - Advanced Mac OS Rootkit

• kern_return_t thread_create_running

(" task_t parent_task," thread_state_flavor_t flavor," thread_state_t new_state," mach_msg_type_number_t new_stateCnt," thread_act_t *child_act);http://cansecwest.com/csw09/csw09-daizovi-miller.pdf

Page 47: VXCON 2012 - Advanced Mac OS Rootkit

why vmmap piduse task_for_pid to

attach another task (no root) ? Because it needs Apple Code Signature

Page 48: VXCON 2012 - Advanced Mac OS Rootkit

IDT with syscall

• idt[0x80] _idt64_unix_scall:ffffff80002d7480

• idt[0x81] _idt64_mach_scall:ffffff80002d74a0

• idt[0x82] _idt64_mdep_scall:ffffff80002d74c0

• idt[0x83] _idt64_diag_scall:ffffff80002d74e0

Page 49: VXCON 2012 - Advanced Mac OS Rootkit

syscall

• "_nsysent"

• 10.7.x no aslr

• 10.8.x _nsysent + offset 0x1c028

Page 50: VXCON 2012 - Advanced Mac OS Rootkit

mach trap

• “_mach_trap_table”

• 10.7.x = 10.8.x

• 10.8 add some new functions

Page 51: VXCON 2012 - Advanced Mac OS Rootkit

Dtrace

• base on dtrace

• execsnoop

• iosnoop

• opensnoop

• rwsnoop

Page 54: VXCON 2012 - Advanced Mac OS Rootkit
Page 55: VXCON 2012 - Advanced Mac OS Rootkit

Modify

• change one byte code

• 55 push rbp

• 48 89 e5 mov rbp,rsp

• 55 push rbp

• 0f 89 e5 lock mov rbp,rsp

Page 56: VXCON 2012 - Advanced Mac OS Rootkit

Handle Exception

• if (FBT_EXCEPTION_CODE == trapno && !IS_USER_TRAP(saved_state)) {

• _fbt_perfCallback

• _dtrace_invop

• http://www.opensource.apple.com/source/xnu/xnu-1456.1.26/bsd/dev/i386/fbt_x86.c

Page 57: VXCON 2012 - Advanced Mac OS Rootkit
Page 58: VXCON 2012 - Advanced Mac OS Rootkit

Anti Dtrace (FBT)

• Load kext

• find symbol _tempDTraceTrapHook

• tempDTraceTrapHook->fbt_perfCallback

• tempDTraceTrapHook->your function

• handle exception

• fix any dtrace modified byte code

Page 59: VXCON 2012 - Advanced Mac OS Rootkit

Process Hiding

• rubilyn rootkit

• http://www.nullsecurity.net/tools/backdoor/rubilyn-0.0.1.tar.gz

• Current DKOM => unlink p_list

• http://www.opensource.apple.com/source/xnu/xnu-1456.1.26/bsd/sys/proc_internal.h

Page 60: VXCON 2012 - Advanced Mac OS Rootkit

How to find hided process

• listing task can find hided process’ task

• http://www.opensource.apple.com/source/xnu/xnu-1456.1.26/osfmk/kern/task.h

Page 61: VXCON 2012 - Advanced Mac OS Rootkit
Page 62: VXCON 2012 - Advanced Mac OS Rootkit

Usermode list task for finding rubilyn rootkit

Page 63: VXCON 2012 - Advanced Mac OS Rootkit

DKOM unlink Task list

• this works on 10.7.x or 10.8.x

• demo

• install truehide.kext

• sudo kextload /System/Library/Extensions/truehide.kext

• sysctl -w debug.truehide.pid=?

Page 64: VXCON 2012 - Advanced Mac OS Rootkit

How to find unlinked Task list?

• In usermode, you can still find something interesting.

• Demo

Page 65: VXCON 2012 - Advanced Mac OS Rootkit

Defense

• check if any kext is loaded

• check if any task is attaching to another task

Page 67: VXCON 2012 - Advanced Mac OS Rootkit

• OSKext::loadExecutable()

• kxld_link_file(KXLDContext *context,u_char *file,u_long size,const char *name,void *callback_data,u_char **deps,u_int ndeps,u_char **_linked_object,kxld_addr_t *kmod_info_kern,u_char **_link_state,u_long *_link_state_size,u_char **_symbol_file __unused,u_long *_symbol_file_size __unused)

Page 68: VXCON 2012 - Advanced Mac OS Rootkit

monitor task_for_pid

Page 69: VXCON 2012 - Advanced Mac OS Rootkit

Summary• mach-o file format

• 10.7.x vs 10.8.x ASLR

• static inject | dynamic inject

• Dtrace and AntiDtrace

• detecting rootkit with proc struct unlink and advanced task unlink

• How to detect kext loading

Page 70: VXCON 2012 - Advanced Mac OS Rootkit

• Q&A