今天在用IDA看ios汇编的时候,函数准备hook,但是试了一上午都没有hook到这个函数,下面是截图:
下面是sub_51b128的截图:
我尝试hook sub_51b128,绕过它的实际过程,直接返回 1
下面是尝试的代码:
#include <mach/mach.h>
#include <mach-o/dyld.h>
#import <substrate.h>
int _module_base = 0;
int (*oldfun)(CFDateRef arg1, NSData * arg2, NSString * arg3, CFDateRef arg4);
int hook_fun(CFDateRef arg1, NSData * arg2, NSString * arg3, CFDateRef arg4)
{
NSLog(@" ################### hook sub_51b128");
//NSLog(@"参数1 %@", arg1);
//NSLog(@"参数2 %@", arg2);
//NSLog(@"参数3 %@", arg3);
//NSLog(@"参数4 %@", arg4);
return 1;
}
__attribute__((constructor)) void dylibMain()
{ _module_base = (int)_dyld_get_image_header(0);
if (_module_base == 0) {
NSLog(@"zmclover: get image header failed.");
} else {
NSLog(@"zmclover: get image hander success %d", _module_base);
_module_base = _module_base - 0x4000;
MSHookFunction((void *)((int (*)(CFDateRef arg1, NSData * arg2, NSString * arg3, CFDateRef arg4))(0x51b128)), (void*)hook_fun, (void**)&oldfun);
}
}
还有尝试返回的是bool 值:
#include <mach/mach.h>
#include <mach-o/dyld.h>
#import <substrate.h>
int _module_base = 0;
BOOL (*oldfun)(CFDateRef arg1, NSData * arg2, NSString * arg3, CFDateRef arg4);
BOOL hook_fun(CFDateRef arg1, NSData * arg2, NSString * arg3, CFDateRef arg4)
{
NSLog(@" ################### hook sub_51b128");
//NSLog(@"参数1 %@", arg1);
//NSLog(@"参数2 %@", arg2);
//NSLog(@"参数3 %@", arg3);
//NSLog(@"参数4 %@", arg4);
return YES;
}
__attribute__((constructor)) void dylibMain()
{ _module_base = (int)_dyld_get_image_header(0);
if (_module_base == 0) {
NSLog(@"zmclover: get image header failed.");
} else {
NSLog(@"zmclover: get image hander success %d", _module_base);
_module_base = _module_base - 0x4000;
MSHookFunction((void *)((BOOL (*)(CFDateRef arg1, NSData * arg2, NSString * arg3, CFDateRef arg4))(0x51b128)), (void*)hook_fun, (void**)&oldfun);
}
}
这里两种方式都会在我预期操作的时候被触发,触发的结果是导致程序崩溃退出,基于此我认为函数的地址是hook到了,导致程序崩溃退出的错误是:
Job appears to have crashed: Illegal instruction: 4
这个错我我的理解是:替换的函数,跟原函数在参数或者返回值上对不上,
我也尝试了这种形式的函数声明:
int(*oldfun)(void* arg1, void*arg2, void* arg3, void* arg4);
还是报同样的错误:
Job appears to have crashed: Illegal instruction: 4
我对mshookfunction的理解还很肤浅,使用也是照葫芦画瓢借鉴来的,
所以请指导一下,这种情况下该怎么hook这个函数~~