首先,我想做的是拦截iOS应用程序的任意标准C函数(例如fopen,read,write,malloc等)。

我有一个libtest.dylib与此代码:

typedef struct interpose_s {
    void *new_func;
    void *orig_func;
} interpose_t;


FILE *vg_fopen(const char * __restrict, const char * __restrict);

static const interpose_t interposing_functions[] \
__attribute__ ((section("__DATA, __interpose"))) = {
    { (void *)vg_fopen, (void *)fopen },
};

FILE *vg_fopen(const char * __restrict path, const char * __restrict mode) {
    printf("vg_fopen");
    return fopen(path, mode);
}

编译dylib之后,我转到主机iOS应用的二进制文件,并将LC_LOAD_DYLIB添加到LC_LOAD_COMMANDS列表的末尾,并将其指向@ executable_path / libtest.dylib。

我期望的是它将覆盖fopen的实现,并在调用fopen时显示“vg_fopen”。但是,我不明白,因此插入可能已失败。

我想知道可能是什么原因。这仅是为了学习目的而进行的内部开发,因此请不要提及其影响或警告我使用不当。

提前致谢。

最佳答案

dyld source:

// link any inserted libraries
// do this after linking main executable so that any dylibs pulled in by inserted
// dylibs (e.g. libSystem) will not be in front of dylibs the program uses
if ( sInsertedDylibCount > 0 ) {
    for(unsigned int i=0; i < sInsertedDylibCount; ++i) {
        ImageLoader* image = sAllImages[i+1];
        link(image, sEnv.DYLD_BIND_AT_LAUNCH, ImageLoader::RPathChain(NULL, NULL));
        // only INSERTED libraries can interpose
        image->registerInterposing();
    }
}

因此,没有,只有通过DYLD_INSERT_LIBRARIES插入的库才被应用。

10-08 08:28
查看更多