首页WIN7问题iOS逆向 ---- Hook方法及原理OC篇

iOS逆向 ---- Hook方法及原理OC篇

时间2022-10-23 18:15:03发布分享专员分类WIN7问题浏览83

👇👇关注后回复 “进群” runtime error ,拉你进程序员交流群👇👇

iOS逆向 ---- Hook方法及原理OC篇

作者丨奔跑的不将就

来源丨掘金

链接:

一、Hook简介

Hook英文意思为钩子,顾名思义就是像钩子一样改变程序原有的运行流程runtime error

比如一段程序的执行流程是 A --> B --> C,现在我们在 A 和 B 之间插入一段代码或者直接改变 B ,这样程序原有的执行流程就发生了改变runtime error 。如下图所示:

iOS逆向 ---- Hook方法及原理OC篇

Hook的这种思想在平时的iOS开发中也有比较多的应用场景,比如在我们实现埋点功能时,经常会用到的 Aspects 库runtime error 。今天,我们就来探索一下对OC方法中的Hook及其原理。

二、方法交换原理

iOS逆向中,如果我们希望对一个OC方法进行Hook,主要应用了OC的运行时特性,在程序运行时动态改变方法的实现runtime error

在OC中,用于实现运行时特性的是一套基于C/C++以及汇编的API,即我们常说的Runtimeruntime error

runtime error 我们Hook过程中用到的主要是一下几个方法:

1、方法交换

OBJC_EXPORT void

method_exchangeImplementations(Method _Nonnull m1, Method _Nonnull m2)

OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

2、替换方法OBJC_EXPORT IMP _Nullableclass_replaceMethod(Class _Nullable cls, SEL _Nonnull name, IMP _Nonnull imp,const char * _Nullable types)OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

3、setIMP & getIMPOBJC_EXPORT IMP _Nonnullmethod_setImplementation(Method _Nonnull m, IMP _Nonnull imp)OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

OBJC_EXPORT IMP _Nullableclass_getMethodImplementation(Class _Nullable cls, SEL _Nonnull name)OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

在平时的开发中runtime error ,我们也会用到这几个方法,但它们是怎么实现方法交换/替换的呢?

观察这几个方法的参数runtime error ,我们发现主要有 Class、SEL、IMP、Method 这几种类型,我们分别来看一下

1、Class 一个 objc_class 类型的结构体指针runtime error ,用于说明对象是哪个类;

2、Method 一个 objc_method 类型的结构体指针runtime error ,用于定义一个方法,在objc源码中定义如下:

iOS逆向 ---- Hook方法及原理OC篇

3、SEL 可以发现在 Method 的定义中,也能看见SELruntime error 。我们看下在苹果官方文档中如何定义这种类型

iOS逆向 ---- Hook方法及原理OC篇

4、IMP 同样在 Method 的定义中也能看到runtime error ,在苹果官方文档的定义如下:

iOS逆向 ---- Hook方法及原理OC篇

我们发现其实上诉方法都包含 SEL 和 IMP 这两种类型的信息runtime error

根据上文中苹果文档中的介绍runtime error 我们可以发现

SEL: Method selectors are used to represent the name of a method at runtime. A method selector is a C string that has been registered (or “mapped“) with the Objective-C runtime.

IMP: This data type is a pointer to the start of the function that implements the method. This function uses standard C calling conventions as implemented for the current CPU architecture. The first argument is a pointer to self (that is, the memory for the particular instance of this class, or, for a class method, a pointer to the metaclass). The second argument is the method selector. The method arguments follow.

SEL是一个C String,用于表示一个方法的名称,IMP是一个方法实现首地址,默认有两个参数 self 和 _cmdruntime error 。其实SEL和IMP的关系我们可以类比一本书的目录,SEL就是目录中的内容标题,IMP是后面的页码。

一个方法调用时,通过SEL找到对应的IMP,进而找到方法的实现runtime error 。如下图:

我们在Hook一个OC方法时runtime error ,只需要改变其SEL所指向的IMP时,就可以实现方法的交换的目的,或者使用class_replaceMethod 和 method_setImplementation 改变一个类原有方法的实现,原理如下图:

三、总结

逆向中,Hook一个OC方法其实就是改变其SEL所指向的IMP,从而找到另一个实现地址,执行另一个方法实现runtime error

但这种方法的局限在于,其只能针对OC方法进行Hook,对于C函数则无法Hookruntime error

在后续的文章中,我们将探索如何Hook一个C函数以及fishhook库的使用及原理,请持续关注runtime error

-End-

最近有一些小伙伴runtime error ,让我帮忙找一些 面试题 资料,于是我翻遍了收藏的 5T 资料后,汇总整理出来,可以说是程序员面试必备! 所有资料都整理到网盘了,欢迎下载!

面试题】即可获取

在看点这里好文分享给更多人↓↓

爱资源吧版权声明:以上文中内容来自网络,如有侵权请联系删除,谢谢。

方法及HookiOS方法及iOSruntime error
【沙发管家】电视盒子的几大隐藏实用功能:你都知道吗? 2022 THUBA DAO 夏季黑客松获胜项目梳理