#include "IGDispatch.h" #include "../resolve/IGResolve.h" #include "../anti_hook/IGAntiHook.h" #include "../primitives/IGInline.h" #include #include #include #include #include #include #include #include /* 编码只减少常量区明文,不作为秘密或强加密。缓存与基线仍可被同进程攻击者修改。 */ enum { IG_DISPATCH_COUNT = 11 }; static const unsigned char kNames[IG_DISPATCH_COUNT][6] = { {0x4b,0x4e,0x55,0x48,0x59}, {0x5f,0x50,0x53,0x4f,0x59}, {0x4e,0x59,0x5d,0x58}, {0x53,0x4c,0x59,0x52}, {0x49,0x52,0x50,0x55,0x52,0x57}, {0x5b,0x59,0x48,0x4c,0x55,0x58}, {0x5a,0x53,0x4c,0x59,0x52}, {0x59,0x44,0x55,0x48}, {0x5a,0x53,0x4e,0x57}, {0x58,0x50,0x53,0x4c,0x59,0x52}, {0x58,0x50,0x4f,0x45,0x51}, }; static const size_t kLengths[IG_DISPATCH_COUNT] = {5,5,4,4,6,6,5,4,4,6,5}; #if IG_DIAGNOSTIC_STRINGS static const char *const kLabels[IG_DISPATCH_COUNT] = { "dispatch-write", "dispatch-close", "dispatch-read", "dispatch-open", "dispatch-unlink", "dispatch-getpid", "dispatch-fopen", "dispatch-exit", "dispatch-fork", "dispatch-dlopen", "dispatch-dlsym" }; #endif static pthread_once_t gOnce=PTHREAD_ONCE_INIT; static _Atomic(uintptr_t) gSlots[IG_DISPATCH_COUNT]; static uintptr_t gExpected[IG_DISPATCH_COUNT]; static unsigned char gPrefixes[IG_DISPATCH_COUNT][16]; static IGStatus gInitialStatus[IG_DISPATCH_COUNT]; static _Thread_local int gResolving; #ifdef IG_OBSERVATION static _Atomic(uint64_t) gAttempted[IG_DISPATCH_COUNT], gDispatched[IG_DISPATCH_COUNT]; #define IG_COUNT(slot,index) atomic_fetch_add_explicit(&(slot)[index],1,memory_order_relaxed) #else #define IG_COUNT(slot,index) ((void)0) #endif static int read_prefix(uintptr_t address, unsigned char out[16]) { vm_size_t copied=0; return vm_read_overwrite(mach_task_self(),address,16,(vm_address_t)out,&copied)==KERN_SUCCESS && copied==16; } static void initialize(void) { for (unsigned i=0;i