在使用Logos进行hook时,如果你遇到“Unknown type name”的错误,通常是因为Logos不能直接识别中文类名。为了绕过这个限制,你可以使用Objective-C运行时函数来获取类和方法,并进行动态hook。以下是一个示例,演示如何使用运行时函数来hook中文类名的方法。
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
#import <substrate.h>
// 定义hook函数
void hook_中文方法名(id self, SEL _cmd) {
NSLog(@"Before calling 中文方法名");
// 获取原方法
Class class = object_getClass(self);
SEL selector = NSSelectorFromString(@"中文方法名");
Method method = class_getInstanceMethod(class, selector);
void (*orig_func)(id, SEL) = (void (*)(id, SEL))method_getImplementation(method);
// 调用原方法
orig_func(self, _cmd);
NSLog(@"After calling 中文方法名");
}
// 在应用启动时执行hook
%ctor {
@autoreleasepool {
// 获取目标类
Class targetClass = objc_getClass("中文.ViewController");
// 获取目标方法
SEL targetSelector = NSSelectorFromString(@"中文方法名");
Method targetMethod = class_getInstanceMethod(targetClass, targetSelector);
// 替换方法实现
if (targetMethod) {
MSHookMessageEx(targetClass, targetSelector, (IMP)hook_中文方法名, NULL);
}
NSLog(@"Tweak loaded");
}
}
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
#import <substrate.h>
// 定义hook函数
void hook_中文方法名(id self, SEL _cmd, id arg) {
NSLog(@"Before calling 中文方法名: with arg: %@", arg);
// 获取原方法
Class class = object_getClass(self);
SEL selector = NSSelectorFromString(@"中文方法名:");
Method method = class_getInstanceMethod(class, selector);
void (*orig_func)(id, SEL, id) = (void (*)(id, SEL, id))method_getImplementation(method);
// 调用原方法
orig_func(self, _cmd, arg);
NSLog(@"After calling 中文方法名: with arg: %@", arg);
}
// 在应用启动时执行hook
%ctor {
@autoreleasepool {
// 获取目标类
Class targetClass = objc_getClass("中文_ViewController");
// 获取目标方法
SEL targetSelector = NSSelectorFromString(@"中文方法名:");
Method targetMethod = class_getInstanceMethod(targetClass, targetSelector);
// 替换方法实现
if (targetMethod) {
MSHookMessageEx(targetClass, targetSelector, (IMP)hook_中文方法名, NULL);
}
NSLog(@"Tweak loaded");
}
}
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
#import <substrate.h>
// 定义hook函数
void hook_中文方法名(id self, SEL _cmd, id param1, id param2) {
NSLog(@"Before calling 中文方法名:param1:param2: with params: %@, %@", param1, param2);
// 获取原方法
Class class = object_getClass(self);
SEL selector = NSSelectorFromString(@"中文方法名:param1:param2:");
Method method = class_getInstanceMethod(class, selector);
void (*orig_func)(id, SEL, id, id) = (void (*)(id, SEL, id, id))method_getImplementation(method);
// 调用原方法
orig_func(self, _cmd, param1, param2);
NSLog(@"After calling 中文方法名:param1:param2: with params: %@, %@", param1, param2);
}
// 在应用启动时执行hook
%ctor {
@autoreleasepool {
// 获取目标类
Class targetClass = objc_getClass("中文_ViewController");
// 获取目标方法
SEL targetSelector = NSSelectorFromString(@"中文方法名:param1:param2:");
Method targetMethod = class_getInstanceMethod(targetClass, targetSelector);
// 替换方法实现
if (targetMethod) {
MSHookMessageEx(targetClass, targetSelector, (IMP)hook_中文方法名, NULL);
}
NSLog(@"Tweak loaded");
}
}
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
#import <substrate.h>
// 定义原始函数指针
id (*orig_中文方法名)(id self, SEL _cmd, id param1, id param2, id param3);
// 定义hook函数
id hook_中文方法名(id self, SEL _cmd, id param1, id param2, id param3) {
NSLog(@"Before calling 中文方法名:param1:param2: with params: %@, %@, %@", param1, param2, param3);
// 调用原方法
id returnValue = orig_中文方法名(self, _cmd, param1, param2, param3);
NSLog(@"After calling 中文方法名:param1:param2: with params: %@, %@, %@, return value: %@", param1, param2, param3, returnValue);
return returnValue;
}
// 在应用启动时执行hook
%ctor {
@autoreleasepool {
// 获取目标类
Class targetClass = objc_getClass("中文方法名.ViewController");
// 获取目标方法
SEL targetSelector = NSSelectorFromString(@"中文方法名:param1:param2:");
Method targetMethod = class_getInstanceMethod(targetClass, targetSelector);
// 替换方法实现
if (targetMethod) {
MSHookMessageEx(targetClass, targetSelector, (IMP)hook_中文方法名, (IMP *)&orig_中文方法名);
}
NSLog(@"Tweak loaded");
}
}