forked from opa334/libundirect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibundirect_dynamic.h
229 lines (209 loc) · 8.5 KB
/
libundirect_dynamic.h
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright (c) 2020-2022 Lars Fröder
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
#import <dlfcn.h>
#import "substrate.h"
#import <libhooker.h>
#import <roothide.h>
#define LIBUNDIRECT_PATH jbroot("/usr/lib/libundirect.dylib")
#ifdef __cplusplus
extern "C" {
#endif
// dynamic header for when you don't want to link against libundirect
// for documentation, check out the non-dynamic header
typedef enum
{
OPTION_DO_NOT_SEEK_BACK = 1 << 0
} libundirect_find_options_t;
__attribute__((unused))
static void libundirect_MSHookMessageEx(Class _class, SEL message, IMP hook, IMP *old)
{
static void (*impl_libundirect_MSHookMessageEx)(Class, SEL, IMP, IMP *);
if(!impl_libundirect_MSHookMessageEx)
{
void* handle = dlopen(LIBUNDIRECT_PATH, RTLD_LAZY);
impl_libundirect_MSHookMessageEx = (void (*)(Class, SEL, IMP, IMP *))dlsym(handle, "libundirect_MSHookMessageEx");
}
if(impl_libundirect_MSHookMessageEx)
{
impl_libundirect_MSHookMessageEx(_class, message, hook, old);
}
else
{
MSHookMessageEx(_class, message, hook, old);
}
}
__attribute__((unused))
static void libundirect_rebind(void* directPtr, Class _class, SEL selector, const char* format)
{
static void (*impl_libundirect_rebind)(void*, Class, SEL, const char*);
if(!impl_libundirect_rebind)
{
void* handle = dlopen(LIBUNDIRECT_PATH, RTLD_LAZY);
impl_libundirect_rebind = (void (*)(void*, Class, SEL, const char*))dlsym(handle, "libundirect_rebind");
}
if(impl_libundirect_rebind)
{
impl_libundirect_rebind(directPtr, _class, selector, format);
}
}
__attribute__((unused))
static void* libundirect_seek_back(void* startPtr, unsigned char toByte, unsigned int maxSearch)
{
static void* (*impl_libundirect_seek_back)(void*, unsigned char, unsigned int);
if(!impl_libundirect_seek_back)
{
void* handle = dlopen(LIBUNDIRECT_PATH, RTLD_LAZY);
impl_libundirect_seek_back = (void* (*)(void*, unsigned char, unsigned int))dlsym(handle, "libundirect_seek_back");
}
if(impl_libundirect_seek_back)
{
return impl_libundirect_seek_back(startPtr, toByte, maxSearch);
}
return NULL;
}
__attribute__((unused))
static void* libundirect_find_with_options_and_mask(NSString* imageName, unsigned char* bytesToSearch, unsigned char* byteMask, size_t byteCount, unsigned char startByte, unsigned int seekbackMax, libundirect_find_options_t options)
{
static void* (*impl_libundirect_find_with_options_and_mask)(NSString*, unsigned char*, unsigned char*, size_t, unsigned char, unsigned int, libundirect_find_options_t);
if(!impl_libundirect_find_with_options_and_mask)
{
void* handle = dlopen(LIBUNDIRECT_PATH, RTLD_LAZY);
impl_libundirect_find_with_options_and_mask = (void* (*)(NSString*, unsigned char*, unsigned char*, size_t, unsigned char, unsigned int, libundirect_find_options_t))dlsym(handle, "libundirect_find_with_options_and_mask");
}
if(impl_libundirect_find_with_options_and_mask)
{
return impl_libundirect_find_with_options_and_mask(imageName, bytesToSearch, byteMask, byteCount, startByte, seekbackMax, options);
}
return NULL;
}
__attribute__((unused))
static void* libundirect_find_with_options(NSString* imageName, unsigned char* bytesToSearch, size_t byteCount, unsigned char startByte, unsigned int seekbackMax, libundirect_find_options_t options)
{
static void* (*impl_libundirect_find_with_options)(NSString*, unsigned char*, size_t, unsigned char, unsigned int, libundirect_find_options_t);
if(!impl_libundirect_find_with_options)
{
void* handle = dlopen(LIBUNDIRECT_PATH, RTLD_LAZY);
impl_libundirect_find_with_options = (void* (*)(NSString*, unsigned char*, size_t, unsigned char, unsigned int, libundirect_find_options_t))dlsym(handle, "libundirect_find_with_options");
}
if(impl_libundirect_find_with_options)
{
return impl_libundirect_find_with_options(imageName, bytesToSearch, byteCount, startByte, seekbackMax, options);
}
return NULL;
}
__attribute__((unused))
static void* libundirect_find(NSString* imageName, unsigned char* bytesToSearch, size_t byteCount, unsigned char startByte)
{
static void* (*impl_libundirect_find)(NSString*, unsigned char*, size_t, unsigned char);
if(!impl_libundirect_find)
{
void* handle = dlopen(LIBUNDIRECT_PATH, RTLD_LAZY);
impl_libundirect_find = (void* (*)(NSString*, unsigned char*, size_t, unsigned char))dlsym(handle, "libundirect_find");
}
if(impl_libundirect_find)
{
return impl_libundirect_find(imageName, bytesToSearch, byteCount, startByte);
}
return NULL;
}
__attribute__((unused))
static void* libundirect_dsc_find(NSString* imageName, Class _class, SEL selector)
{
static void* (*impl_libundirect_dsc_find)(NSString*, Class, SEL);
if(!impl_libundirect_dsc_find)
{
void* handle = dlopen(LIBUNDIRECT_PATH, RTLD_LAZY);
impl_libundirect_dsc_find = (void* (*)(NSString*, Class, SEL))dlsym(handle, "libundirect_dsc_find");
}
if(impl_libundirect_dsc_find)
{
return impl_libundirect_dsc_find(imageName, _class, selector);
}
return NULL;
}
__attribute__((unused))
static void libundirect_dsc_rebind(NSString* imageName, Class _class, SEL selector, const char* format)
{
static void (*impl_libundirect_dsc_rebind)(NSString*, Class, SEL, const char*);
if(!impl_libundirect_dsc_rebind)
{
void* handle = dlopen(LIBUNDIRECT_PATH, RTLD_LAZY);
impl_libundirect_dsc_rebind = (void (*)(NSString*, Class, SEL, const char*))dlsym(handle, "libundirect_dsc_rebind");
}
if(impl_libundirect_dsc_rebind)
{
return impl_libundirect_dsc_rebind(imageName, _class, selector, format);
}
}
__attribute__((unused))
static NSArray* libundirect_failedSelectors()
{
static NSArray* (*impl_libundirect_failedSelectors)();
if(!impl_libundirect_failedSelectors)
{
void* handle = dlopen(LIBUNDIRECT_PATH, RTLD_LAZY);
impl_libundirect_failedSelectors = (NSArray* (*)(void))dlsym(handle, "libundirect_failedSelectors");
}
if(impl_libundirect_failedSelectors)
{
return impl_libundirect_failedSelectors();
}
return (NSArray*)nil;
}
__attribute__((unused))
static void libundirect_startBatchHooks(void)
{
static void (*impl_libundirect_startBatchHooks)();
if(!impl_libundirect_startBatchHooks)
{
void* handle = dlopen(LIBUNDIRECT_PATH, RTLD_LAZY);
impl_libundirect_startBatchHooks = (void (*)(void))dlsym(handle, "libundirect_startBatchHooks");
}
if(impl_libundirect_startBatchHooks)
{
impl_libundirect_startBatchHooks();
}
}
__attribute__((unused))
static void libundirect_applyBatchHooksAndAdditional(const struct LHFunctionHook* additionalHooks, NSUInteger additionalCount)
{
static void (*impl_libundirect_applyBatchHooksAndAdditional)(const struct LHFunctionHook*, NSUInteger);
if(!impl_libundirect_applyBatchHooksAndAdditional)
{
void* handle = dlopen(LIBUNDIRECT_PATH, RTLD_LAZY);
impl_libundirect_applyBatchHooksAndAdditional = (void (*)(const struct LHFunctionHook*, NSUInteger))dlsym(handle, "libundirect_applyBatchHooksAndAdditional");
}
if(impl_libundirect_applyBatchHooksAndAdditional)
{
impl_libundirect_applyBatchHooksAndAdditional(additionalHooks, additionalCount);
}
}
__attribute__((unused))
static void libundirect_applyBatchHooks()
{
static void (*impl_libundirect_applyBatchHooks)();
if(!impl_libundirect_applyBatchHooks)
{
void* handle = dlopen(LIBUNDIRECT_PATH, RTLD_LAZY);
impl_libundirect_applyBatchHooks = (void (*)(void))dlsym(handle, "libundirect_failedSelectors");
}
if(impl_libundirect_applyBatchHooks)
{
return impl_libundirect_applyBatchHooks();
}
}
#ifdef __cplusplus
}
#endif
// macros to readd setters and getters for ivars, these can't be hooked as the application still calls the original direct getters and setters
// mainly useful to get existing code to just work without having to change everything to use the ivar instead
// can only be used from xmi files
#define LIBUNDIRECT_CLASS_ADD_GETTER(classname, type, ivarname, gettername) %hook classname %new - (type)gettername { return [self valueForKey:[NSString stringWithUTF8String:#ivarname]]; } %end
#define LIBUNDIRECT_CLASS_ADD_SETTER(classname, type, ivarname, settername) %hook classname %new - (void)settername:(type)toset { [self setValue:toset forKey:[NSString stringWithUTF8String:#ivarname]]; } %end