在 iOSGuard 侧产出可集成产物与接入文档,不修改 readoor 代码。 - defender/: anti_hook/anti_frida/anti_debug/anti_jailbreak 探针 + syscall 直发、 policy 分级层、poison 数据依赖毒化、dispatch/resolve/call_bridge 等子模块 - poison: 三副本冗余、无分支掩码;健康恒等、毒化破坏 key(替代 if-exit 处置) - policy: STRONG/MEDIUM/WEAK 分级,守误报底线,仅 DETECTED 参与、组合命中才毒化 - anti_jailbreak: IGSyscall 直发 svc 检测越狱文件(400)/沙盒逃逸(401),绕 libc hook - tools/build_defender_lib.sh: 一键交叉编译打包 libIGDefender.a(arm64) + umbrella 头 - docs/18_READOOR_DEFENDER_INTEGRATION.md: 精确到文件/bridging/替换点/代码片段 - docs/05-17 + evidence + tests + macho_guard/static_sdk_redirect 工具链 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
227 lines
10 KiB
C
227 lines
10 KiB
C
#include "IGArchiveGuard.h"
|
|
#include "../resolve/IGResolve.h"
|
|
#include "../anti_hook/IGAntiHook.h"
|
|
#include "../primitives/IGInline.h"
|
|
#include <errno.h>
|
|
#include <pthread.h>
|
|
#include <stdatomic.h>
|
|
#include <string.h>
|
|
#include <mach/mach.h>
|
|
#include <mach-o/dyld.h>
|
|
#include <mach-o/loader.h>
|
|
#include <mach-o/nlist.h>
|
|
|
|
/* 仅混淆常量,不作为秘密。SDK GOT 由 dyld 在独立镜像滑移后绑定。 */
|
|
static const unsigned char names[4][6] = {
|
|
{0x5a,0x53,0x4c,0x59,0x52}, {0x5a,0x4e,0x59,0x5d,0x58},
|
|
{0x5a,0x4b,0x4e,0x55,0x48,0x59}, {0x5a,0x5f,0x50,0x53,0x4f,0x59}
|
|
};
|
|
static pthread_once_t once = PTHREAD_ONCE_INIT;
|
|
static _Thread_local int resolving;
|
|
static _Atomic(uintptr_t) targets[4];
|
|
static uintptr_t expected[4], bindingSlots[4];
|
|
static unsigned char prefixes[4][16];
|
|
static IGStatus initial[4];
|
|
static int bindingReady;
|
|
#ifdef IG_OBSERVATION
|
|
static _Atomic(uint64_t) counters[12];
|
|
#define COUNT(i, k) atomic_fetch_add_explicit(&counters[(i)*3+(k)], 1, memory_order_relaxed)
|
|
#else
|
|
#define COUNT(i, k) ((void)0)
|
|
#endif
|
|
|
|
static int read_bytes(uintptr_t address, void *out, size_t size) {
|
|
vm_size_t copied = 0;
|
|
return vm_read_overwrite(mach_task_self(), address, size, (vm_address_t)out, &copied) == KERN_SUCCESS
|
|
&& copied == size;
|
|
}
|
|
|
|
/* 仅支持打包工具已审核的非认证 arm64 GOT;缺槽/重复槽均标为不可验证。 */
|
|
static int locate_bindings(void) {
|
|
unsigned found = 0, images = 0;
|
|
for (uint32_t index = 0; index < _dyld_image_count(); index++) {
|
|
const char *path = _dyld_get_image_name(index);
|
|
const char *suffix = "/SSZipArchive.framework/SSZipArchive";
|
|
size_t length = path ? ig_strlen(path) : 0, tail = ig_strlen(suffix);
|
|
if (length < tail || ig_strcmp(path + length - tail, suffix)) continue;
|
|
if (++images != 1) return 0;
|
|
const struct mach_header_64 *header = (const void *)_dyld_get_image_header(index);
|
|
if (!header || header->magic != MH_MAGIC_64 || header->cputype != CPU_TYPE_ARM64
|
|
|| (header->cpusubtype & 0xffffff) != 0) return 0;
|
|
uintptr_t slide = _dyld_get_image_vmaddr_slide(index);
|
|
const struct segment_command_64 *linkedit = NULL;
|
|
const struct symtab_command *symtab = NULL;
|
|
const struct dysymtab_command *dysymtab = NULL;
|
|
const uint8_t *command = (const void *)(header + 1);
|
|
const uint8_t *end = command + header->sizeofcmds;
|
|
for (uint32_t i = 0; i < header->ncmds; i++) {
|
|
if (command + sizeof(struct load_command) > end) return 0;
|
|
const struct load_command *lc = (const void *)command;
|
|
if (lc->cmdsize < 8 || lc->cmdsize % 8 || lc->cmdsize > (size_t)(end-command)) return 0;
|
|
if (lc->cmd == LC_SEGMENT_64) {
|
|
if (lc->cmdsize < sizeof(struct segment_command_64)) return 0;
|
|
const struct segment_command_64 *seg = (const void *)lc;
|
|
if (sizeof(*seg) + (uint64_t)seg->nsects * sizeof(struct section_64) != lc->cmdsize) return 0;
|
|
if (!ig_strcmp(seg->segname, SEG_LINKEDIT)) linkedit = seg;
|
|
} else if (lc->cmd == LC_SYMTAB && lc->cmdsize == sizeof(*symtab)) symtab = (const void *)lc;
|
|
else if (lc->cmd == LC_DYSYMTAB && lc->cmdsize == sizeof(*dysymtab)) dysymtab = (const void *)lc;
|
|
command += lc->cmdsize;
|
|
}
|
|
if (!linkedit || !symtab || !dysymtab || command != end) return 0;
|
|
uint64_t lo = linkedit->fileoff, hi = lo + linkedit->filesize;
|
|
if (hi < lo || symtab->symoff < lo || (uint64_t)symtab->symoff + (uint64_t)symtab->nsyms*16 > hi
|
|
|| symtab->stroff < lo || (uint64_t)symtab->stroff + symtab->strsize > hi
|
|
|| dysymtab->indirectsymoff < lo
|
|
|| (uint64_t)dysymtab->indirectsymoff + (uint64_t)dysymtab->nindirectsyms*4 > hi) return 0;
|
|
uintptr_t base = slide + linkedit->vmaddr - lo;
|
|
const struct nlist_64 *symbols = (const void *)(base + symtab->symoff);
|
|
const char *strings = (const void *)(base + symtab->stroff);
|
|
const uint32_t *indirect = (const void *)(base + dysymtab->indirectsymoff);
|
|
command = (const void *)(header + 1);
|
|
for (uint32_t i = 0; i < header->ncmds; i++) {
|
|
const struct load_command *lc = (const void *)command;
|
|
if (lc->cmd == LC_SEGMENT_64) {
|
|
const struct segment_command_64 *seg = (const void *)lc;
|
|
const struct section_64 *sections = (const void *)(seg + 1);
|
|
for (uint32_t j = 0; j < seg->nsects; j++) {
|
|
const struct section_64 *s = sections + j;
|
|
if ((s->flags & SECTION_TYPE) != S_NON_LAZY_SYMBOL_POINTERS) continue;
|
|
if (s->size % 8 || (uint64_t)s->reserved1 + s->size/8 > dysymtab->nindirectsyms) return 0;
|
|
for (uint64_t k = 0; k < s->size/8; k++) {
|
|
uint32_t symbol = indirect[s->reserved1 + k];
|
|
if (symbol & (INDIRECT_SYMBOL_LOCAL | INDIRECT_SYMBOL_ABS)) continue;
|
|
if (symbol >= symtab->nsyms) return 0;
|
|
uint32_t strx = symbols[symbol].n_un.n_strx;
|
|
if (strx >= symtab->strsize) return 0;
|
|
const char *name = strings + strx;
|
|
if (!memchr(name, 0, symtab->strsize-strx)) return 0;
|
|
for (unsigned n = 0; n < 4; n++) {
|
|
char expectedName[] = "_ig_a0";
|
|
expectedName[5] += n;
|
|
if (ig_strcmp(name, expectedName)) continue;
|
|
if (found & (1u << n)) return 0;
|
|
bindingSlots[n] = slide + s->addr + k*8;
|
|
found |= 1u << n;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
command += lc->cmdsize;
|
|
}
|
|
}
|
|
return found == 15 && images == 1;
|
|
}
|
|
|
|
static void initialize(void) {
|
|
bindingReady = locate_bindings();
|
|
for (unsigned i = 0; i < 4; i++) {
|
|
IGResolveResult result = ig_resolve_verified(NULL, names[i], i < 2 ? 5 : 6, NULL, NULL);
|
|
initial[i] = result.status;
|
|
if (result.status != IG_CLEAR || !result.address) continue;
|
|
if (!read_bytes((uintptr_t)result.address, prefixes[i], 16)) { initial[i] = IG_UNAVAILABLE; continue; }
|
|
expected[i] = (uintptr_t)result.address;
|
|
atomic_store_explicit(&targets[i], expected[i], memory_order_release);
|
|
}
|
|
}
|
|
|
|
static int prepare(void) {
|
|
if (resolving) return 0;
|
|
resolving = 1;
|
|
int result = pthread_once(&once, initialize);
|
|
resolving = 0;
|
|
return result == 0;
|
|
}
|
|
|
|
static IGStatus validate(unsigned index, uintptr_t *address) {
|
|
if (initial[index] != IG_CLEAR) return initial[index];
|
|
uintptr_t current = atomic_load_explicit(&targets[index], memory_order_acquire);
|
|
if (!current || current != expected[index]) return IG_DETECTED;
|
|
IGStatus status = ig_verify_function((void *)current, "archive-target", NULL, NULL);
|
|
if (status != IG_CLEAR) return status;
|
|
unsigned char bytes[16];
|
|
if (!read_bytes(current, bytes, 16)) return IG_UNAVAILABLE;
|
|
if (ig_memcmp(bytes, prefixes[index], 16)) return IG_DETECTED;
|
|
*address = current;
|
|
return IG_CLEAR;
|
|
}
|
|
|
|
static IGStatus check_bindings(void) {
|
|
#ifdef IG_ARCHIVE_TESTING
|
|
/* 宿主 ABI fixture 不装载 SSZipArchive;此豁免不能编入业务包。 */
|
|
return IG_CLEAR;
|
|
#else
|
|
if (!bindingReady) return IG_UNAVAILABLE;
|
|
uintptr_t correct[4] = {(uintptr_t)&ig_a0, (uintptr_t)&ig_a1, (uintptr_t)&ig_a2, (uintptr_t)&ig_a3};
|
|
for (unsigned i = 0; i < 4; i++) {
|
|
uintptr_t current;
|
|
if (!read_bytes(bindingSlots[i], ¤t, sizeof(current))) return IG_UNAVAILABLE;
|
|
if (current != correct[i]) return IG_DETECTED;
|
|
}
|
|
return IG_CLEAR;
|
|
#endif
|
|
}
|
|
|
|
static uintptr_t checked(unsigned index) {
|
|
COUNT(index, 0);
|
|
if (!prepare()) { COUNT(index, 2); errno = EDEADLK; return 0; }
|
|
uintptr_t address = 0;
|
|
IGStatus status = check_bindings();
|
|
if (status == IG_CLEAR) status = validate(index, &address);
|
|
if (status != IG_CLEAR) { COUNT(index, 2); errno = status == IG_DETECTED ? EACCES : ENOSYS; return 0; }
|
|
COUNT(index, 1);
|
|
return address;
|
|
}
|
|
|
|
__attribute__((noinline)) FILE *ig_a0(const char *path, const char *mode) {
|
|
int incoming = errno;
|
|
uintptr_t address = checked(0);
|
|
if (!address) return NULL;
|
|
errno = incoming;
|
|
return ((FILE *(*)(const char *, const char *))address)(path, mode);
|
|
}
|
|
__attribute__((noinline)) size_t ig_a1(void *ptr, size_t size, size_t count, FILE *stream) {
|
|
int incoming = errno;
|
|
uintptr_t address = checked(1);
|
|
if (!address) return 0;
|
|
errno = incoming;
|
|
return ((size_t (*)(void *, size_t, size_t, FILE *))address)(ptr, size, count, stream);
|
|
}
|
|
__attribute__((noinline)) size_t ig_a2(const void *ptr, size_t size, size_t count, FILE *stream) {
|
|
int incoming = errno;
|
|
uintptr_t address = checked(2);
|
|
if (!address) return 0;
|
|
errno = incoming;
|
|
return ((size_t (*)(const void *, size_t, size_t, FILE *))address)(ptr, size, count, stream);
|
|
}
|
|
__attribute__((noinline)) int ig_a3(FILE *stream) {
|
|
int incoming = errno;
|
|
uintptr_t address = checked(3);
|
|
if (!address) return EOF;
|
|
errno = incoming;
|
|
return ((int (*)(FILE *))address)(stream);
|
|
}
|
|
|
|
int ig_archive_check(void) {
|
|
int incoming = errno;
|
|
if (!prepare()) { errno = incoming; return -1; }
|
|
IGStatus status = check_bindings();
|
|
for (unsigned i = 0; i < 4; i++) {
|
|
uintptr_t address;
|
|
IGStatus current = validate(i, &address);
|
|
if (current == IG_DETECTED || (current == IG_UNAVAILABLE && status == IG_CLEAR)) status = current;
|
|
}
|
|
errno = incoming;
|
|
return status == IG_CLEAR ? 0 : status == IG_DETECTED ? 1 : -1;
|
|
}
|
|
#ifdef IG_OBSERVATION
|
|
uint64_t ig_archive_counter(unsigned index) {
|
|
return index < 12 ? atomic_load_explicit(&counters[index], memory_order_relaxed) : 0;
|
|
}
|
|
#endif
|
|
#ifdef IG_ARCHIVE_TESTING
|
|
void ig_archive_test_override(unsigned index, void *address) {
|
|
if (index < 4 && prepare()) atomic_store_explicit(&targets[index], (uintptr_t)address, memory_order_release);
|
|
}
|
|
void ig_archive_test_reentry(int enabled) { resolving = enabled; }
|
|
#endif
|