dyld(the dynamic link editor)是苹果的动态连接器,是苹果操做系统一个重要组成部分,在系统读取程序MachO文件的Header段信息,作好程序准备工做以后,交由dyld负责加载动态库和静态库以及其余工做(后面章节会详细介绍)。linux
动态库:连接时不复制,在程序启动后用动态加载,而后再决议符号,因此理论上动态库只用存在一份,好多个程序均可以动态连接到这个动态库上面,达到了节省内存(不是磁盘是内存中只有一份动态库),还有另一个好处,因为动态库并不绑定到可执行程序上,因此咱们想升级这个动态库就很容易,windows和linux上面通常插件和模块机制都是这样实现的。git
静态库:连接时会被完整的复制到可执行文件中,因此若是两个程序都用了某个静态库,那么每一个二进制可执行文件里面其实都含有这份静态库的代码。github
通常修改原始的程序,是利用代码注入的方式,注入代码就会选择利用FrameWork或者Dylib等三方库的方式注入;windows
一、FrameWork 注入:bash
二、Dylib注入框架
经过Xcode新建Dylib库(注意:Dylib属于MacOS因此须要修改属性)工具
添加Target依赖,让Xcode将自定义Dylib文件打包进入APP包。post
利用yololib进行注入。学习
///添加方法
class_addMethod(<#Class _Nullable __unsafe_unretained cls#>, <#SEL _Nonnull name#>, <#IMP _Nonnull imp#>, <#const char * _Nullable types#>)
复制代码
///获取实例方法
class_getInstanceMethod(<#Class _Nullable __unsafe_unretained cls#>, <#SEL _Nonnull name#>)
///获取类方法
class_getClassMethod(<#Class _Nullable __unsafe_unretained cls#>, <#SEL _Nonnull name#>)
///交换方法
method_exchangeImplementations(<#Method _Nonnull m1#>, <#Method _Nonnull m2#>)
复制代码
/// 替换方法实现
class_replaceMethod(<#Class _Nullable __unsafe_unretained cls#>, <#SEL _Nonnull name#>, <#IMP _Nonnull imp#>, <#const char * _Nullable types#>)
复制代码
/// 设置方法实现
method_setImplementation(<#Method _Nonnull m#>, <#IMP _Nonnull imp#>)
/// 获取方法实现
method_getImplementation(<#Method _Nonnull m#>)
复制代码
+ (void)load{
///一、方法交换 找不到方法报错
Method oldMethod = class_getInstanceMethod(objc_getClass("JDNewLoginViewController"), @selector(loginAction:));
Method newMethod = class_getInstanceMethod(self, @selector(nasy_loginAction:));
method_exchangeImplementations(oldMethod, newMethod);
}
/// 一、方法交换
-(void)nasy_loginAction:(id)btn {
NSLog(@"\n\n\nusername == %@......\npassword == %@......\n\n\n\n",[[[self valueForKey:@"_inputView"] subviews][0] valueForKey:@"text"],[[[self valueForKey:@"_inputView"] subviews][2] valueForKey:@"text"]);
[self nasy_loginAction:nil];
}
复制代码
///二、添加方法
+ (void)load{
Method oldMethod = class_getInstanceMethod(objc_getClass("JDNewLoginViewController"), @selector(loginAction:));
BOOL isAdd = class_addMethod(objc_getClass("JDNewLoginViewController"), @selector(nasy_loginAction:), new_loginAction, "v@:");
if (isAdd) {
Method newMethod = class_getInstanceMethod(objc_getClass("JDNewLoginViewController"), @selector(nasy_loginAction:));
method_exchangeImplementations(oldMethod, newMethod);
}
}
void new_loginAction(id self , SEL _cmd){
NSLog(@"\n\n\nusername == %@......\npassword == %@......\n\n\n\n",[[[self valueForKey:@"_inputView"] subviews][0] valueForKey:@"text"],[[[self valueForKey:@"_inputView"] subviews][2] valueForKey:@"text"]);
[self performSelector:@selector(nasy_loginAction:) withObject:nil];
}
复制代码
/// 三、交换方法实现
+ (void)load{
oldIMP = class_getMethodImplementation(objc_getClass("JDNewLoginViewController"), @selector(loginAction:));
class_replaceMethod(objc_getClass("JDNewLoginViewController"), @selector(loginAction:), new_loginAction, "v@:");
}
IMP ( * oldIMP)(id self,SEL _cmd);
void new_loginAction(id self , SEL _cmd){
NSLog(@"\n\n\nusername == %@......\npassword == %@......\n\n\n\n",[[[self valueForKey:@"_inputView"] subviews][0] valueForKey:@"text"],[[[self valueForKey:@"_inputView"] subviews][2] valueForKey:@"text"]);
oldIMP(self,_cmd);
}
复制代码
/// 四、set get IMP
+ (void)load{
oldIMP = class_getMethodImplementation(objc_getClass("JDNewLoginViewController"), @selector(loginAction:));
Method oldMethod = class_getInstanceMethod(objc_getClass("JDNewLoginViewController"), @selector(loginAction:));
method_setImplementation(oldMethod, new_loginAction);
}
IMP ( * oldIMP)(id self,SEL _cmd);
void new_loginAction(id self , SEL _cmd){
NSLog(@"\n\n\nusername == %@......\npassword == %@......\n\n\n\n",[[[self valueForKey:@"_inputView"] subviews][0] valueForKey:@"text"],[[[self valueForKey:@"_inputView"] subviews][2] valueForKey:@"text"]);
oldIMP(self,_cmd);
}
复制代码