/* * IGResolve.c — 加密名 + 无 dlsym Export Trie + 带校验的符号解析实现。 * * 复用 IGAntiHook 的 ig_verify_function 做「放行前校验」:从 Mach-O Export Trie 解析地址后, * 必须 provenance(提供方系统路径)+ 序言(非 inline-hook 蹦床)双过,否则返回 NULL。 */ #include "IGResolve.h" #include "../anti_hook/IGAntiHook.h" #include "../primitives/IGInline.h" #include /* _NSGetEnviron */ #include #include #ifndef LC_DYLD_EXPORTS_TRIE #define LC_DYLD_EXPORTS_TRIE (0x33 | LC_REQ_DYLD) #endif #if defined(__has_feature) # if __has_feature(ptrauth_calls) # include # define IG_RESOLVE_SIGN_FN(p) ptrauth_sign_unauthenticated((p), ptrauth_key_asia, 0) # endif #endif #ifndef IG_RESOLVE_SIGN_FN # define IG_RESOLVE_SIGN_FN(p) (p) #endif static int ig_is_system_image_path(const char *path) { if (!path) return 0; return ig_strncmp(path, "/usr/lib/", 9) == 0 || ig_strncmp(path, "/System/", 8) == 0 || ig_strncmp(path, "/private/preboot/Cryptexes/", 27) == 0; } static int ig_ends_with(const char *str, const char *suffix) { if (!str || !suffix) return 0; size_t slen = ig_strlen(str), tlen = ig_strlen(suffix); return slen >= tlen && ig_strcmp(str + slen - tlen, suffix) == 0; } static uint64_t ig_read_uleb128(const uint8_t **cursor, const uint8_t *end, int *ok) { uint64_t val = 0; int shift = 0; while (*cursor < end && shift < 64) { uint8_t byte = *(*cursor)++; val |= (uint64_t)(byte & 0x7fu) << shift; if (!(byte & 0x80u)) { *ok = 1; return val; } shift += 7; } *ok = 0; return 0; } static const uint8_t *ig_walk_export_trie(const uint8_t *start, const uint8_t *end, const char *sym) { const uint8_t *p = start; for (int step = 0; p < end && step < 512; ++step) { int ok = 0; uint64_t term_size = ig_read_uleb128(&p, end, &ok); if (!ok || term_size > (uint64_t)(end - p)) return NULL; if (*sym == '\0') return term_size ? p : NULL; const uint8_t *children = p + term_size; if (children >= end) return NULL; uint8_t count = *children++; p = children; int matched = 0; for (uint8_t i = 0; i < count; ++i) { const char *s = sym; const uint8_t *edge_start = p; while (p < end && *p != 0 && *s == (char)*p) { ++p; ++s; } if (p < end && *p == 0 && p != edge_start) { ++p; uint64_t next_off = ig_read_uleb128(&p, end, &ok); if (!ok || next_off >= (uint64_t)(end - start)) return NULL; sym = s; p = start + next_off; matched = 1; break; } while (p < end && *p != 0) ++p; if (p >= end) return NULL; ++p; (void)ig_read_uleb128(&p, end, &ok); if (!ok) return NULL; } if (!matched) return NULL; } return NULL; } static void *ig_lookup_concrete_export(const struct mach_header_64 *mh, intptr_t slide, const char *macho_sym) { if (!mh || mh->magic != MH_MAGIC_64) return NULL; const struct segment_command_64 *linkedit = NULL; uint32_t trie_off = 0, trie_size = 0; const uint8_t *cmd = (const uint8_t *)(mh + 1); const uint8_t *end_cmds = cmd + mh->sizeofcmds; for (uint32_t i = 0; i < mh->ncmds; ++i) { if (cmd + sizeof(struct load_command) > end_cmds) return NULL; const struct load_command *lc = (const struct load_command *)cmd; if (lc->cmdsize < 8 || lc->cmdsize % 8 || cmd + lc->cmdsize > end_cmds) return NULL; if (lc->cmd == LC_SEGMENT_64 && lc->cmdsize >= sizeof(struct segment_command_64)) { const struct segment_command_64 *seg = (const struct segment_command_64 *)lc; if (ig_strcmp(seg->segname, SEG_LINKEDIT) == 0) linkedit = seg; } else if (lc->cmd == LC_DYLD_EXPORTS_TRIE && lc->cmdsize >= sizeof(struct linkedit_data_command)) { const struct linkedit_data_command *d = (const struct linkedit_data_command *)lc; trie_off = d->dataoff; trie_size = d->datasize; } else if ((lc->cmd == LC_DYLD_INFO || lc->cmd == LC_DYLD_INFO_ONLY) && lc->cmdsize >= sizeof(struct dyld_info_command)) { const struct dyld_info_command *d = (const struct dyld_info_command *)lc; if (!trie_off) { trie_off = d->export_off; trie_size = d->export_size; } } cmd += lc->cmdsize; } if (!linkedit || !trie_off || !trie_size) return NULL; if (trie_off < linkedit->fileoff || trie_size > linkedit->filesize || (uint64_t)(trie_off - linkedit->fileoff) > linkedit->filesize - trie_size) { return NULL; } uintptr_t linkedit_base = (uintptr_t)slide + (uintptr_t)(linkedit->vmaddr - linkedit->fileoff); const uint8_t *start = (const uint8_t *)(linkedit_base + trie_off); const uint8_t *end = start + trie_size; const uint8_t *node = ig_walk_export_trie(start, end, macho_sym); if (!node) return NULL; int ok = 0; uint64_t flags = ig_read_uleb128(&node, end, &ok); if (!ok) return NULL; if (flags & EXPORT_SYMBOL_FLAGS_REEXPORT) return NULL; if ((flags & EXPORT_SYMBOL_FLAGS_KIND_MASK) != EXPORT_SYMBOL_FLAGS_KIND_REGULAR) return NULL; if (flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER) { (void)ig_read_uleb128(&node, end, &ok); if (!ok) return NULL; uint64_t resolver_off = ig_read_uleb128(&node, end, &ok); if (!ok) return NULL; void *(*resolver)(void) = (void *(*)(void))IG_RESOLVE_SIGN_FN((void *)((uintptr_t)mh + (uintptr_t)resolver_off)); return resolver(); } uint64_t sym_off = ig_read_uleb128(&node, end, &ok); if (!ok) return NULL; return IG_RESOLVE_SIGN_FN((void *)((uintptr_t)mh + (uintptr_t)sym_off)); } void *ig_resolve_export_default(const char *bare_name) { if (!bare_name || !bare_name[0]) return NULL; size_t len = ig_strlen(bare_name); if (len >= 254) return NULL; char macho_sym[256]; macho_sym[0] = '_'; ig_memcpy(macho_sym + 1, bare_name, len); macho_sym[1 + len] = '\0'; uint32_t count = _dyld_image_count(); /* 两阶段:Pass 0 检查非系统动态库抢占(跳过主程序和保护库自身),Pass 1 查系统镜像。 */ for (int pass = 0; pass < 2; ++pass) { for (uint32_t i = 0; i < count; ++i) { const struct mach_header_64 *mh = (const struct mach_header_64 *)_dyld_get_image_header(i); if (!mh || mh->magic != MH_MAGIC_64 || mh->filetype == MH_EXECUTE) continue; const char *path = _dyld_get_image_name(i); if (!path) continue; int sys = ig_is_system_image_path(path); if (pass == 0) { if (sys || ig_ends_with(path, "/RDCoreIO.framework/RDCoreIO")) continue; } else { if (!sys) continue; } void *addr = ig_lookup_concrete_export(mh, _dyld_get_image_vmaddr_slide(i), macho_sym); if (addr) { ig_wipe(macho_sym, sizeof(macho_sym)); return addr; } } } ig_wipe(macho_sym, sizeof(macho_sym)); return NULL; } void *ig_resolve_image_export(const char *image_suffix, const char *bare_name) { if (!image_suffix || !image_suffix[0] || !bare_name || !bare_name[0]) return NULL; size_t len = ig_strlen(bare_name); if (len >= 254) return NULL; char macho_sym[256]; macho_sym[0] = '_'; ig_memcpy(macho_sym + 1, bare_name, len); macho_sym[1 + len] = '\0'; const struct mach_header_64 *matched_mh = NULL; intptr_t matched_slide = 0; uint32_t matches = 0; uint32_t count = _dyld_image_count(); for (uint32_t i = 0; i < count; ++i) { const char *path = _dyld_get_image_name(i); if (!ig_ends_with(path, image_suffix)) continue; const struct mach_header_64 *mh = (const struct mach_header_64 *)_dyld_get_image_header(i); if (!mh || mh->magic != MH_MAGIC_64 || mh->filetype != MH_DYLIB) continue; matched_mh = mh; matched_slide = _dyld_get_image_vmaddr_slide(i); ++matches; } void *addr = (matches == 1) ? ig_lookup_concrete_export(matched_mh, matched_slide, macho_sym) : NULL; ig_wipe(macho_sym, sizeof(macho_sym)); return addr; } char *ig_decrypt(const unsigned char *enc, size_t len, char *out, size_t cap) { if (!out || cap==0) return NULL; out[0]='\0'; if (!enc || len==0 || len>=cap) return NULL; for (size_t i = 0; i < len; ++i) { out[i] = (char)(enc[i] ^ IG_ENC_KEY); if (out[i]=='\0') { ig_wipe(out,cap); return NULL; } } out[len] = '\0'; return out; } void ig_wipe(void *p, size_t n) { if (!p) return; volatile unsigned char *v = (volatile unsigned char *)p; while (n--) *v++ = 0; /* volatile:防编译器把清零 DCE 掉 */ } IGResolveResult ig_resolve_verified(void *handle, const unsigned char *enc_name, size_t sym_len, IGFindingSink sink, void *ctx) { char name[256]; IGResolveResult result={IG_UNAVAILABLE,NULL}; if (!ig_decrypt(enc_name, sym_len, name, sizeof(name))) return result; #ifdef dlsym extern void *dlsym(void *, const char *); void *addr = dlsym(handle ? handle : (void *)-2, name); #else (void)handle; void *addr = ig_resolve_export_default(name); #endif if (addr) { /* 放行前校验:provenance + inline-hook。不过即判被挟持,返回 NULL。 */ result.status=ig_verify_function(addr,name,sink,ctx); if (result.status==IG_CLEAR) result.address=addr; } else if (sink) { IGFinding f={0}; f.status=IG_UNAVAILABLE; f.reason=IG_REASON_SYMBOL_UNRESOLVED; sink(&f,ctx); } ig_wipe(name, sizeof(name)); /* 明文符号名用后清零,不驻留栈 */ return result; } void *ig_dlsym_verified(void *handle, const unsigned char *enc_name, size_t sym_len) { return ig_resolve_verified(handle,enc_name,sym_len,NULL,NULL).address; } const char *ig_getenv(const char *name) { char ***environment = _NSGetEnviron(); if (!environment || !*environment || !name || !name[0]) return NULL; char *const *envp = *environment; size_t nlen = ig_strlen(name); for (; *envp; ++envp) { const char *e = *envp; /* 匹配 "name=" 前缀 */ if (ig_strncmp(e, name, nlen) == 0 && e[nlen] == '=') { return e + nlen + 1; } } return NULL; }