-
Notifications
You must be signed in to change notification settings - Fork 11
/
Compat.m
160 lines (120 loc) · 4.27 KB
/
Compat.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#import <HookKit/Compat.h>
#import <HookKit/Core.h>
#import <HookKit/Hook.h>
#import <HookKit/Module.h>
@implementation HKSubstitutor {
NSMutableArray<__kindof HookKitHook *>* batchHooks;
__kindof HookKitModule* module;
}
@synthesize types, batching;
- (instancetype)init {
if((self = [super init])) {
batchHooks = [NSMutableArray new];
module = nil;
types = 0;
}
return self;
}
- (void)initLibraries {
HookKitCore* core = [HookKitCore sharedInstance];
module = [core defaultModule];
if(types != 0) {
NSArray* typeinfo = [[self class] getSubstitutorTypeInfo:types];
if([typeinfo count]) {
module = [core getModuleWithIdentifier:[typeinfo[0] objectForKey:@"id"]];
}
}
}
+ (hookkit_lib_t)getAvailableSubstitutorTypes {
hookkit_lib_t types = 0;
HookKitCore* core = [HookKitCore sharedInstance];
hookkit_lib_t type = 1;
NSArray* module_infos = [core getModuleInfo];
for(__unused NSDictionary* module_info in module_infos) {
types |= type;
type = type * 2;
}
return types;
}
+ (NSArray<NSDictionary *> *)getSubstitutorTypeInfo:(hookkit_lib_t)types {
NSMutableArray* result = [NSMutableArray new];
HookKitCore* core = [HookKitCore sharedInstance];
hookkit_lib_t type = 1;
NSArray* module_infos = [core getModuleInfo];
for(NSDictionary* module_info in module_infos) {
if((types & type) == type) {
[result addObject:@{
@"id" : module_info[@"Identifier"],
@"name" : module_info[@"Description"],
@"type" : @(type)
}];
}
type = type * 2;
}
return [result copy];
}
+ (instancetype)substitutorWithTypes:(hookkit_lib_t)types {
HKSubstitutor* substitutor = [self new];
[substitutor setTypes:types];
[substitutor initLibraries];
return substitutor;
}
+ (instancetype)defaultSubstitutor {
static HKSubstitutor* defaultSubstitutor = nil;
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^{
defaultSubstitutor = [self new];
[defaultSubstitutor initLibraries];
});
return defaultSubstitutor;
}
- (hookkit_status_t)hookMessageInClass:(Class)objcClass withSelector:(SEL)selector withReplacement:(void *)replacement outOldPtr:(void **)old_ptr {
HookKitClassHook* hook = [HookKitClassHook hook:objcClass selector:selector replacement:replacement orig:old_ptr];
if(batching) {
[batchHooks addObject:hook];
return HK_OK;
}
return [module executeHook:hook] ? HK_OK : HK_ERR_NOT_SUPPORTED;
}
- (hookkit_status_t)hookFunction:(void *)function withReplacement:(void *)replacement outOldPtr:(void **)old_ptr {
HookKitFunctionHook* hook = [HookKitFunctionHook hook:function replacement:replacement orig:old_ptr];
if(batching) {
[batchHooks addObject:hook];
return HK_OK;
}
return [module executeHook:hook] ? HK_OK : HK_ERR_NOT_SUPPORTED;
}
- (hookkit_status_t)hookMemory:(void *)target withData:(const void *)data size:(size_t)size {
HookKitMemoryHook* hook = [HookKitMemoryHook hook:target data:data size:size];
if(batching) {
[batchHooks addObject:hook];
return HK_OK;
}
return [module executeHook:hook] ? HK_OK : HK_ERR_NOT_SUPPORTED;
}
- (HKImageRef)openImage:(NSString *)path {
return (HKImageRef)[module openImageWithPath:path];
}
- (void)closeImage:(HKImageRef)image {
[module closeImage:(hookkit_image_t)image];
}
- (hookkit_status_t)findSymbolsInImage:(HKImageRef)image symbolNames:(NSArray<NSString *> *)symbolNames outSymbols:(NSArray<NSValue *> **)outSymbols {
NSMutableArray* outSyms = [NSMutableArray new];
for(NSString* symbolName in symbolNames) {
[outSyms addObject:[NSValue valueWithPointer:[self findSymbolInImage:image symbolName:symbolName]]];
}
*outSymbols = [outSyms copy];
return HK_OK;
}
- (void *)findSymbolInImage:(HKImageRef)image symbolName:(NSString *)symbolName {
return [module findSymbolName:symbolName inImage:(hookkit_image_t)image];
}
- (hookkit_status_t)executeHooks {
int result = [module executeHooks:batchHooks];
[batchHooks removeAllObjects];
return result ? HK_OK : HK_ERR;
}
- (int)getLibErrno:(hookkit_lib_t *)outType {
return 0;
}
@end