- 构建期生成随包签名的 IGMethods.bin(仅主程序),运行时按镜像 UUID、 入口偏移与函数边界内指令摘要校验;导出审计复核清单与最终镜像一致 - 系统函数、调度与解包保护的代码前缀按 LC_FUNCTION_STARTS 限定在函数边界内 - 边界内识别 B / LDR+BR / ADRP+ADD+BR / MOVK+BR 蹦床并确认目标归属,避免系统尾调用误报 - 登记前 IMP 已落入匿名内存判为改写;清单读取改走 IG_dispatch_open - 函数边界长度按入口缓存;镜像未重新链接时复用基线;Debug 附加调试器时不按入口摘要判定改写 - 补充宿主测试;删除已过时的 README 与 docs 历史文档,保留 THIRD_PARTY_NOTICES
84 lines
3.2 KiB
Objective-C
84 lines
3.2 KiB
Objective-C
#import <Foundation/Foundation.h>
|
|
#import <dlfcn.h>
|
|
#import <objc/runtime.h>
|
|
#import <sys/mman.h>
|
|
#import <sys/ptrace.h>
|
|
#include "anti_hook/IGObjCBaseline.h"
|
|
|
|
// 优化器能把实例方法编译成一条合法的尾调用。
|
|
__attribute__((noinline)) static int readValue(void) {
|
|
static volatile int value = 7;
|
|
return value;
|
|
}
|
|
|
|
@interface IGManifestParent : NSObject
|
|
- (int)value;
|
|
+ (int)classValue;
|
|
- (int)other;
|
|
@end
|
|
@implementation IGManifestParent
|
|
- (int)value { return readValue(); }
|
|
+ (int)classValue { return readValue(); }
|
|
- (int)other { return 42; }
|
|
@end
|
|
@interface IGManifestChild : IGManifestParent
|
|
@end
|
|
@implementation IGManifestChild
|
|
@end
|
|
@interface IGManifestChild (Extra)
|
|
- (int)extra;
|
|
@end
|
|
@implementation IGManifestChild (Extra)
|
|
- (int)extra { return readValue(); }
|
|
@end
|
|
|
|
typedef struct { int changed, unavailable; } Counts;
|
|
static int collect(const IGFinding *finding, void *ctx) {
|
|
Counts *counts = ctx;
|
|
counts->changed += finding->status == IG_DETECTED;
|
|
counts->unavailable += finding->status == IG_UNAVAILABLE;
|
|
return 0;
|
|
}
|
|
|
|
int main(int argc, const char **argv) {
|
|
if (argc != 2) return 1;
|
|
// 强制 dyld 装入 Swift 类,与真实应用一致地通过运行时名称登记。
|
|
extern void ig_load_swift_fixture(void);
|
|
extern const char *ig_swift_nested_name(void);
|
|
extern const char *ig_swift_private_name(void);
|
|
ig_load_swift_fixture();
|
|
IGObjCWatch watches[] = {
|
|
{"IGManifestChild", "-value"}, {"IGManifestChild", "+classValue"},
|
|
{"IGManifestChild", "extra"}, {"Fixture.SwiftProbe", "swiftValue"},
|
|
{ig_swift_nested_name(), "nestedValue"}, {ig_swift_private_name(), "privateValue"},
|
|
};
|
|
Method method = class_getInstanceMethod(IGManifestParent.class, @selector(value));
|
|
IMP other = class_getMethodImplementation(IGManifestParent.class, @selector(other));
|
|
BOOL before = !strcmp(argv[1], "before"), after = !strcmp(argv[1], "after");
|
|
BOOL before_block = !strcmp(argv[1], "before-block"), before_anon = !strcmp(argv[1], "before-anon");
|
|
void *anon = NULL;
|
|
if (before) method_setImplementation(method, other);
|
|
if (before_block) {
|
|
IMP blk = imp_implementationWithBlock(^int(id self) { (void)self; return 99; });
|
|
method_setImplementation(method, blk);
|
|
}
|
|
if (before_anon) {
|
|
anon = mmap(NULL, 4096, PROT_READ, MAP_PRIVATE | MAP_ANON, -1, 0);
|
|
if (anon == MAP_FAILED) return 3;
|
|
Dl_info di;
|
|
if (dladdr(anon, &di)) return 4;
|
|
method_setImplementation(method, (IMP)anon);
|
|
}
|
|
if (ig_objc_baseline_capture(watches, 6) != 6) return 2;
|
|
if (after) method_setImplementation(method, other);
|
|
BOOL traced = !strcmp(argv[1], "different-bytes-traced");
|
|
if (traced && ptrace(PT_TRACE_ME, 0, NULL, 0) != 0) return 5;
|
|
Counts counts = {0};
|
|
ig_objc_baseline_check(collect, &counts);
|
|
if (anon) munmap(anon, 4096);
|
|
fprintf(stderr, "changed=%d unavailable=%d\n", counts.changed, counts.unavailable);
|
|
int expect_changed = before || before_block || before_anon || after || !strcmp(argv[1], "different-bytes");
|
|
return counts.changed != expect_changed ||
|
|
counts.unavailable != (!strcmp(argv[1], "unavailable") ? 6 : 0);
|
|
}
|