dlopen动态加载自定义dylib的时候,在加载完毕后会自动执行dylib的constructor函数,执行完后再返回,有没有办法在加载完还没执行constructor的时候hook这个dylib?因为要hook的函数在constructor里就执行了,如果等dlopen返回再hook就太迟了,函数已经执行了。。我试了fishhook用的_dyld_register_func_for_add_image方法不行,不知道是不是我用错了,或者有其他方法请大神赐教哈~
只是一个思路啊。
你看MachO里面有个mod_init段,里面就是constructor的函数地址。有没有可能Parse MachO获取这个地址后去掉让原来的modinit不执行,然后dlopen,做好hook以后手动执行。
EDIT: 我记得我当年搞IAPCrazy的去广告也是这个情况。 dyld callback是可行的,可能你的方法有问题? 贴个代码呢
直接
b ctorName
然后
c
应该就可以了
他要代码层注入
void add_callback(const struct mach_header *mh, intptr_t vmaddr_slide)
{
Dl_info info;
dladdr(mh, &info);
NSLog(@"dylib : %s",info.dli_fname);
%init;
MSHookFunction(((void*)MSFindSymbol(NULL,"_func")),(void*)repl_func,(void**)&orig_func);
}
static __attribute__((constructor)) void InitTweak()
{
_dyld_register_func_for_add_image(add_callback);
}
我这样做发现都没有hook到,_dyld_register_func_for_add_image函数是成功的,能打印出要加载的dylib名称,理论上这时候已经处于dylib加载完毕且还未执行constructor的时候,但是后面的MSHookFunction就是没hook上,换用fishhook也不行,它只能hook原生函数。。
楼主这个问题最后弄出来了吗?
Patch一下MachO就可以hook了, 没有别的解
好的 感谢张总 元旦快乐 ![]()
这样 有个问题,是你的 dylib 先加载,还是别的dylib 先加载
如果能决定,你的dylib 先加载,这种么问题
如果不能决定,是你的dylib还是别人的dylib先加载…
那么:
patch code 你值得拥有。
张总,还是麻烦你再解答一下,按你这个思路,我把目标A.dylib的constructor函数(形如_logosLocalCtor_17e62166)patch成其他函数地址了
于是在我自己编写的动态库中调用A的constructor函数,如下编写:(已经确保了我自己的动态库在A.dylib之后加载)
%ctor
{
%init;
MSImageRef image;
image = MSGetImageByName(“/Library/MobileSubstrate/DynamicLibraries/A.dylib”);
desConstructor = (void*(*)(int,char**,char**))MSFindSymbol(image,“_logosLocalCtor_17e62166”);
if(desConstructor){
(*desConstructor)(argc,argv,envp);
}else {
NSLog(@“nonooooo–”);
}
}
日志会输出:“nonooooo–” 并没有执行A.dylib的constructor函数
然后我把MSFindSymbol函数的第一个参数改为NULL,日志输出 “SubstituteLog: SubFindSymbol: ‘any image’ specified, which is incredibly slow - like, 2ms on a fast x86. I’m going to do it since it seems to be somewhat common, but you should be ashamed of yourself.” 意思是懒得给我找个函数符号
![]()
请教该如何在自己的动态库中调用A的那个constructor函数呢?有点懵了 ![]()
不需要用名称找符号执行,直接找段执行就行了
你理解了我的思路,但是你的实现还是错误的
何为“找段执行”? 本人小白啊 有没有更详细点的参考?
你想想你是怎么找到
这个的
系统是怎么判断这是constructor的?
