hook

22 篇文章 0 订阅

上面只是介绍一种hook的使用方法,这里进行详细的介绍。

MobileHooker 用于替换覆盖系统的方法,这个过程被称为Hooking(挂钩). 将使用到3API

IMP MSHookMessage(Class class, SEL selector, IMP replacement, const char* prefix); // prefix should be NULL.

void MSHookMessageEx(Class class, SEL selector, IMP replacement, IMP *result);

void MSHookFunction(void* function, void* replacement, void** p_original);

 

MSHookMessage() Objective-C中替换implementation[class Selector] 通过替换 并返回原来的执行。挂钩一个类的方法,

MSHookemessage(EX)中调用Objc_getMetaClass得到提供的元数据和类检索,例如下面的注释。这种动态替换是Objective-c的一个功能使用method_setImplementationMSHookMmessage()是线程不安全的,不赞成使用,赞成MSHookMessageEx()

 

MSHookFunction() 很像 MSHookMessage() 但是适合于c/c++ 的函数。MSHookFunctin() 将写入指令调入指定替换函数,并接会分配一些字节在内存地址,相当于原始切出指令并且跳入原始的挂钩方法。由于iPhone系统默认的内存页不能同时写和执行,一个内核补丁必须申请MSHookFunction() 工作.

截止MobileSubstrate的最新版本,MSHookMessage() 也需要一个内核补丁 调用关闭的挂钩的所有正确方法。

 

代码示例:

使用 MSHookfunction:

static void (*original_CFShow)(CFTypeRef obj);  // a function pointer to store the original CFShow().

void replaced_CFShow(CFTypeRef obj) {           // our replacement of CFShow().

  printf("Calling original CFShow(%p)...", obj);

  original_CFShow(obj);                         // calls the original CFShow.

  printf(" done.\n");

}

...

// hook CFShow to our own implementation.

MSHookFunction(CFShow, replaced_CFShow, &original_CFShow);

// From now on any call to CFShow will pass through replaced_CFShow first.

...

CFShow(CFSTR("test"));

 

使用 MSHookMessageEX:

static IMP original_UIView_setFrame_;

void replaced_UIView_setFrame_(UIView* self, SEL _cmd, CGRect frame) {  // Note the implicit self and _cmd parameters are needed explicitly here.

  CGRect originalFrame = self.frame;

  NSLog("Changing frame of %p from %@ to %@", self, NSStringFromCGRect(originalFrame), NSStringFromCGRect(frame));

  original_UIView_setFrame_(self, _cmd, frame);    // Remember to pass self and _cmd.

}

...

MSHookMessageEx([UIView class]@selector(setFrame:)(IMP)replaced_UIView_setFrame_,(IMP *)&original_UIView_setFrame_);

...

myView.frame = CGRectMake(00100100);

 

注意,如果你挂钩一个类的方法,有必须放一个元类在类参数中。

MSHookMessageEx(objc_getMetaClass("UIView")@selector(commitAnimations), replaced_UIView_commitAnimations, (IMP *)&original_UIView_commitAnimations);

 

 

这里的MSHookMessageEx或者MSHookMessage方法等同于 %hook %end的方法 建议使用%hook %end 对,比较方便和简洁。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值