-
Notifications
You must be signed in to change notification settings - Fork 167
/
hook_objc_msgSend.m
268 lines (235 loc) · 10.1 KB
/
hook_objc_msgSend.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/**
* Copyright 2017 jmpews
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hookzz.h"
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#import <objc/message.h>
#import <mach-o/dyld.h>
#import <dlfcn.h>
struct section_64 *zz_macho_get_section_64_via_name(struct mach_header_64 *header, char *sect_name);
zpointer zz_macho_get_section_64_address_via_name(struct mach_header_64 *header, char *sect_name);
struct segment_command_64 *zz_macho_get_segment_64_via_name(struct mach_header_64 *header, char *segment_name);
void sprintfArg(char *fd, RegState *rs, int index, char *type_name);
Class zz_macho_object_get_class(id object_addr);
zpointer log_sel_start_addr = 0;
zpointer log_sel_end_addr = 0;
zpointer log_class_start_addr = 0;
zpointer log_class_end_addr = 0;
char decollators[128] = {0};
int LOG_ALL_SEL = 0;
int LOG_ALL_CLASS = 0;
@interface HookZz : NSObject
@end
@implementation HookZz
+ (void)load {
const struct mach_header *header = _dyld_get_image_header(0);
struct segment_command_64 *seg_cmd_64_text = zz_macho_get_segment_64_via_name((struct mach_header_64 *)header, (char *)"__TEXT");
zsize slide = (zaddr)header - (zaddr)seg_cmd_64_text->vmaddr;
struct section_64 *sect_64_1 = zz_macho_get_section_64_via_name((struct mach_header_64 *)header, (char *)"__objc_methname");
log_sel_start_addr = slide + (zaddr)sect_64_1->addr;
log_sel_end_addr = log_sel_start_addr + sect_64_1->size;
struct section_64 *sect_64_2 = zz_macho_get_section_64_via_name((struct mach_header_64 *)header, (char *)"__objc_data");
log_class_start_addr = slide + (zaddr)sect_64_2->addr;
log_class_end_addr = log_class_start_addr + sect_64_2->size;
[self hook_objc_msgSend];
}
void objc_msgSend_pre_call(RegState *rs, ThreadStack *threadstack, CallStack *callstack) {
char *sel_name = (char *)rs->general.regs.x1;
// No More Work Here!!! it will be slow.
if(LOG_ALL_SEL || (sel_name > log_sel_start_addr && sel_name < log_sel_end_addr)) {
// bad code! correct-ref: https://github.com/DavidGoldman/InspectiveC/blob/299cef1c40e8a165c697f97bcd317c5cfa55c4ba/logging.mm#L27
void *object_addr = (void *)rs->general.regs.x0;
void *class_addr = zz_macho_object_get_class((id)object_addr);
if(!class_addr)
return;
void *super_class_addr = class_getSuperclass(class_addr);
// KVO 2333
if(LOG_ALL_CLASS || ((object_addr > log_class_start_addr && object_addr < log_class_end_addr) || (class_addr > log_class_start_addr && class_addr < log_class_end_addr) || (super_class_addr > log_class_start_addr && super_class_addr < log_class_end_addr))) {
memset(decollators, 45, 128);
if(threadstack->size * 3 >= 128)
return;
decollators[threadstack->size * 3] = '\0';
// char *class_name = class_getName(object_addr);
char *class_name = object_getClassName(object_addr);
unsigned int class_name_length = strlen(class_name);
#if 0
// check View
if(class_name_length >= 4 && !strcmp((class_name + class_name_length - 4), "View")) {
printf(@"thread-id: %ld | %s [%s %s]", threadstack->thread_id, decollators, class_name, sel_name);
}
#endif
#if 0
printf("thread-id: %ld | %s [%s %s]\n", threadstack->thread_id, decollators, class_name, sel_name);
#endif
#if 1
// check ViewController
if(class_name_length >= 14 && !strcmp((class_name + class_name_length - 14), "ViewController")) {
printf("thread-id: %ld | %s [%s %s]\n", threadstack->thread_id, decollators, class_name, sel_name);
}
#endif
#if 0
// check ViewController with parse parameters (ref readme.md)
if(class_name_length >= 14 && !strcmp((class_name + class_name_length - 14), "ViewController")) {
Method method = class_getInstanceMethod(class_addr, sel_name);
int num_args = method_getNumberOfArguments(method);
char method_name[128] = {0};
char sel_name_tmp[128] = {0};
char *x;
char *y;
x = sel_name_tmp;
strcpy(sel_name_tmp, sel_name);
if(!strchr(x, ':')) {
printf("thread-id: %ld | %s [%s %s]\n", threadstack->thread_id, decollators, class_name, sel_name_tmp);
return;
}
for (int i=2; strchr(x, ':') && i < num_args; i++) {
y = strchr(x, ':');
*y = '\0';
char *type_name = method_copyArgumentType(method, i);
sprintf(method_name + strlen(method_name), "%s:", x);
sprintfArg(method_name + strlen(method_name), rs, i, type_name);
x = y + 1;
}
printf("thread-id: %ld | %s [%s %s]\n", threadstack->thread_id, decollators, class_name, method_name);
}
#endif
}
}
}
void ojbc_msgSend_post_call(RegState *rs, ThreadStack *threadstack, CallStack *callstack) {
}
+ (void)hook_objc_msgSend {
ZzBuildHook((void *)objc_msgSend, NULL, NULL, objc_msgSend_pre_call, ojbc_msgSend_post_call);
ZzEnableHook((void *)objc_msgSend);
}
@end
Class zz_macho_object_get_class(id object_addr) {
if(!object_addr)
return NULL;
#if 0
if(object_isClass(object_addr)) {
return object_addr;
} else {
return object_getClass(object_addr);
}
#elif 1
return object_getClass(object_addr);
#elif 0
Class kind = object_getClass(object_addr);
if (class_isMetaClass(kind))
return object_addr;
return kind;
#endif
}
void sprintfArg(char *fd, RegState *rs, int index, char *type_name) {
if(index > 8) {
sprintf(fd, "%s", "unknown");
return;
}
switch(*type_name) {
case '#':
case '@': {
sprintf(fd, "<class:%s>", object_getClassName((void *)rs->general.x[index]));
} break;
case '*': {
sprintf(fd, "<char *:%s>", (char *)(rs->general.x[index]));
} break;
case 'B':
case 'c':
case 'C':
case 's':
case 'S':
case 'i':
case 'I':
case 'l':
case 'L': {
sprintf(fd, "<num:%ld>", (long long)rs->general.x[index]);
} break;
default: {
sprintf(fd, "<%s>", "unknown");
};
}
}
struct section_64 *
zz_macho_get_section_64_via_name(struct mach_header_64 *header,
char *sect_name) {
struct load_command *load_cmd;
struct segment_command_64 *seg_cmd_64;
struct section_64 *sect_64;
load_cmd = (struct load_command *)((zaddr)header + sizeof(struct mach_header_64));
for (zsize i = 0; i < header->ncmds;
i++, load_cmd = (struct load_command *)((zaddr)load_cmd + load_cmd->cmdsize)) {
if (load_cmd->cmd == LC_SEGMENT_64) {
seg_cmd_64 = (struct segment_command_64 *)load_cmd;
sect_64 = (struct section_64 *)((zaddr)seg_cmd_64 +
sizeof(struct segment_command_64));
for (zsize j = 0; j < seg_cmd_64->nsects;
j++, sect_64 = (struct section_64 *)((zaddr)sect_64 + sizeof(struct section_64))) {
if (!strcmp(sect_64->sectname, sect_name)) {
return sect_64;
}
}
}
}
return NULL;
}
zpointer zz_macho_get_section_64_address_via_name(struct mach_header_64 *header,
char *sect_name) {
struct load_command *load_cmd;
struct segment_command_64 *seg_cmd_64;
struct section_64 *sect_64;
zsize slide, linkEditBase;
load_cmd = (struct load_command *)((zaddr)header + sizeof(struct mach_header_64));
for (zsize i = 0; i < header->ncmds;
i++, load_cmd = (struct load_command *)((zaddr)load_cmd + load_cmd->cmdsize)) {
if (load_cmd->cmd == LC_SEGMENT_64) {
seg_cmd_64 = (struct segment_command_64 *)load_cmd;
if ( (seg_cmd_64->fileoff == 0) && (seg_cmd_64->filesize != 0) ) {
slide = (uintptr_t)header - seg_cmd_64->vmaddr;
}
if ( strcmp(seg_cmd_64->segname, "__LINKEDIT") == 0 ) {
linkEditBase = seg_cmd_64->vmaddr - seg_cmd_64->fileoff + slide;
}
sect_64 = (struct section_64 *)((zaddr)seg_cmd_64 +
sizeof(struct segment_command_64));
for (zsize j = 0; j < seg_cmd_64->nsects;
j++, sect_64 = (struct section_64 *)((zaddr)sect_64 + sizeof(struct section_64))) {
if (!strcmp(sect_64->sectname, sect_name)) {
return (zpointer)(sect_64->addr + slide);
}
}
}
}
return NULL;
}
struct segment_command_64 *
zz_macho_get_segment_64_via_name(struct mach_header_64 *header,
char *segment_name) {
struct load_command *load_cmd;
struct segment_command_64 *seg_cmd_64;
struct section_64 *sect_64;
load_cmd = (struct load_command *)((zaddr)header + sizeof(struct mach_header_64));
for (zsize i = 0; i < header->ncmds;
i++, load_cmd = (struct load_command *)((zaddr)load_cmd + load_cmd->cmdsize)) {
if (load_cmd->cmd == LC_SEGMENT_64) {
seg_cmd_64 = (struct segment_command_64 *)load_cmd;
if(!strcmp(seg_cmd_64->segname, segment_name)) {
return seg_cmd_64;
}
}
}
return NULL;
}