forked from vrolife/qiling-il2cpp-dump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump.cpp
437 lines (401 loc) · 14.7 KB
/
dump.cpp
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#include <stdio.h>
#include <unistd.h>
#include <dlfcn.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include "il2cpp-tabledefs.h"
#include "il2cpp-class.h"
#define DO_API(r, n, p) r (*n) p
#include "il2cpp-api-functions.h"
#undef DO_API
typedef void* (*dlsym_t)(void *handle, const char *symbol, const void* caller_addr);
int dump_fd = 2;
char buf[64 * 1024 * 1024];
size_t buf_pos = 0;
void _uprint(int fd, const char* format, ...) {
char buffer[4096];
va_list args;
va_start(args, format);
auto n = vsnprintf(buffer, 4096, format, args);
va_end(args);
if (fd == dump_fd) {
memcpy(&buf[buf_pos], buffer, n);
buf_pos += n;
} else {
write(fd, buffer, n);
}
}
#define uprint(...) _uprint(2, __VA_ARGS__)
uint64_t _il2cpp_base = 0;
dlsym_t _dlsym = 0;
void init_il2cpp_api() {
#define DO_API(r, n, p) { \
n = (r (*) p)_dlsym(NULL, #n, (void*)_il2cpp_base); \
if(!n) { \
uprint("api not found %s\n", #n); \
} \
}
#include "il2cpp-api-functions.h"
#undef DO_API
}
struct Dump {
const Dump& operator <<(const char* str) const {
_uprint(dump_fd, "%s", str);
return *this;
}
const Dump& operator <<(unsigned long val) const {
_uprint(dump_fd, "%lu", val);
return *this;
}
const Dump& operator <<(long val) const {
_uprint(dump_fd, "%ld", val);
return *this;
}
};
const Dump dump{};
void get_method_modifier(uint32_t flags) {
auto access = flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK;
switch (access) {
case METHOD_ATTRIBUTE_PRIVATE:
dump << "private ";
break;
case METHOD_ATTRIBUTE_PUBLIC:
dump << "public ";
break;
case METHOD_ATTRIBUTE_FAMILY:
dump << "protected ";
break;
case METHOD_ATTRIBUTE_ASSEM:
case METHOD_ATTRIBUTE_FAM_AND_ASSEM:
dump << "internal ";
break;
case METHOD_ATTRIBUTE_FAM_OR_ASSEM:
dump << "protected internal ";
break;
}
if (flags & METHOD_ATTRIBUTE_STATIC) {
dump << "static ";
}
if (flags & METHOD_ATTRIBUTE_ABSTRACT) {
dump << "abstract ";
if ((flags & METHOD_ATTRIBUTE_VTABLE_LAYOUT_MASK) == METHOD_ATTRIBUTE_REUSE_SLOT) {
dump << "override ";
}
} else if (flags & METHOD_ATTRIBUTE_FINAL) {
if ((flags & METHOD_ATTRIBUTE_VTABLE_LAYOUT_MASK) == METHOD_ATTRIBUTE_REUSE_SLOT) {
dump << "sealed override ";
}
} else if (flags & METHOD_ATTRIBUTE_VIRTUAL) {
if ((flags & METHOD_ATTRIBUTE_VTABLE_LAYOUT_MASK) == METHOD_ATTRIBUTE_NEW_SLOT) {
dump << "virtual ";
} else {
dump << "override ";
}
}
if (flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
dump << "extern ";
}
}
bool _il2cpp_type_is_byref(const Il2CppType *type) {
auto byref = type->byref;
if (il2cpp_type_is_byref) {
byref = il2cpp_type_is_byref(type);
}
return byref;
}
void dump_method(Il2CppClass *klass) {
dump << "\n\t// Methods\n";
void *iter = nullptr;
while (auto method = il2cpp_class_get_methods(klass, &iter)) {
//TODO attribute
if (method->methodPointer) {
dump << "\t// RVA: 0x";
// dump << (uint64_t) method->methodPointer - il2cpp_base;
_uprint(dump_fd, "%lx", (uint64_t) method->methodPointer - _il2cpp_base);
dump << " VA: 0x";
// dump << (uint64_t) method->methodPointer;
_uprint(dump_fd, "%lx", (uint64_t) method->methodPointer);
} else {
dump << "\t// RVA: 0x VA: 0x0";
}
/*if (method->slot != 65535) {
outPut << " Slot: " << std::dec << method->slot;
}*/
dump << "\n\t";
uint32_t iflags = 0;
auto flags = il2cpp_method_get_flags(method, &iflags);
get_method_modifier(flags);
//TODO genericContainerIndex
auto return_type = il2cpp_method_get_return_type(method);
if (_il2cpp_type_is_byref(return_type)) {
dump << "ref ";
}
auto return_class = il2cpp_class_from_type(return_type);
dump << il2cpp_class_get_name(return_class) << " " << il2cpp_method_get_name(method)
<< "(";
auto param_count = il2cpp_method_get_param_count(method);
for (int i = 0; i < param_count; ++i) {
auto param = il2cpp_method_get_param(method, i);
auto attrs = param->attrs;
if (_il2cpp_type_is_byref(param)) {
if (attrs & PARAM_ATTRIBUTE_OUT && !(attrs & PARAM_ATTRIBUTE_IN)) {
dump << "out ";
} else if (attrs & PARAM_ATTRIBUTE_IN && !(attrs & PARAM_ATTRIBUTE_OUT)) {
dump << "in ";
} else {
dump << "ref ";
}
} else {
if (attrs & PARAM_ATTRIBUTE_IN) {
dump << "[In] ";
}
if (attrs & PARAM_ATTRIBUTE_OUT) {
dump << "[Out] ";
}
}
auto parameter_class = il2cpp_class_from_type(param);
dump << il2cpp_class_get_name(parameter_class) << " "
<< il2cpp_method_get_param_name(method, i);
dump << ", ";
}
// if (param_count > 0) {
// outPut.seekp(-2, outPut.cur);
// }
dump << ") { }\n";
//TODO GenericInstMethod
}
}
void dump_property(Il2CppClass *klass) {
dump << "\n\t// Properties\n";
void *iter = nullptr;
while (auto prop_const = il2cpp_class_get_properties(klass, &iter)) {
//TODO attribute
auto prop = const_cast<PropertyInfo *>(prop_const);
auto get = il2cpp_property_get_get_method(prop);
auto set = il2cpp_property_get_set_method(prop);
auto prop_name = il2cpp_property_get_name(prop);
dump << "\t";
Il2CppClass *prop_class = nullptr;
uint32_t iflags = 0;
if (get) {
get_method_modifier(il2cpp_method_get_flags(get, &iflags));
prop_class = il2cpp_class_from_type(il2cpp_method_get_return_type(get));
} else if (set) {
get_method_modifier(il2cpp_method_get_flags(set, &iflags));
auto param = il2cpp_method_get_param(set, 0);
prop_class = il2cpp_class_from_type(param);
}
if (prop_class) {
dump << il2cpp_class_get_name(prop_class) << " " << prop_name << " { ";
if (get) {
dump << "get; ";
}
if (set) {
dump << "set; ";
}
dump << "}\n";
} else {
if (prop_name) {
dump << " // unknown property " << prop_name;
}
}
}
}
void dump_field(Il2CppClass *klass) {
dump << "\n\t// Fields\n";
auto is_enum = il2cpp_class_is_enum(klass);
void *iter = nullptr;
while (auto field = il2cpp_class_get_fields(klass, &iter)) {
//TODO attribute
dump << "\t";
auto attrs = il2cpp_field_get_flags(field);
auto access = attrs & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK;
switch (access) {
case FIELD_ATTRIBUTE_PRIVATE:
dump << "private ";
break;
case FIELD_ATTRIBUTE_PUBLIC:
dump << "public ";
break;
case FIELD_ATTRIBUTE_FAMILY:
dump << "protected ";
break;
case FIELD_ATTRIBUTE_ASSEMBLY:
case FIELD_ATTRIBUTE_FAM_AND_ASSEM:
dump << "internal ";
break;
case FIELD_ATTRIBUTE_FAM_OR_ASSEM:
dump << "protected internal ";
break;
}
if (attrs & FIELD_ATTRIBUTE_LITERAL) {
dump << "const ";
} else {
if (attrs & FIELD_ATTRIBUTE_STATIC) {
dump << "static ";
}
if (attrs & FIELD_ATTRIBUTE_INIT_ONLY) {
dump << "readonly ";
}
}
auto field_type = il2cpp_field_get_type(field);
auto field_class = il2cpp_class_from_type(field_type);
dump << il2cpp_class_get_name(field_class) << " " << il2cpp_field_get_name(field);
//TODO 获取构造函数初始化后的字段值
if (attrs & FIELD_ATTRIBUTE_LITERAL && is_enum) {
uint64_t val = 0;
il2cpp_field_static_get_value(field, &val);
dump << " = " << val;
}
dump << "; // 0x";
_uprint(dump_fd, "%lx", il2cpp_field_get_offset(field));
dump << "\n";
}
}
void dump_type(const Il2CppType *type) {
auto *klass = il2cpp_class_from_type(type);
dump << "\n// Namespace: " << il2cpp_class_get_namespace(klass) << "\n";
auto flags = il2cpp_class_get_flags(klass);
if (flags & TYPE_ATTRIBUTE_SERIALIZABLE) {
dump << "[Serializable]\n";
}
//TODO attribute
auto is_valuetype = il2cpp_class_is_valuetype(klass);
auto is_enum = il2cpp_class_is_enum(klass);
auto visibility = flags & TYPE_ATTRIBUTE_VISIBILITY_MASK;
switch (visibility) {
case TYPE_ATTRIBUTE_PUBLIC:
case TYPE_ATTRIBUTE_NESTED_PUBLIC:
dump << "public ";
break;
case TYPE_ATTRIBUTE_NOT_PUBLIC:
case TYPE_ATTRIBUTE_NESTED_FAM_AND_ASSEM:
case TYPE_ATTRIBUTE_NESTED_ASSEMBLY:
dump << "internal ";
break;
case TYPE_ATTRIBUTE_NESTED_PRIVATE:
dump << "private ";
break;
case TYPE_ATTRIBUTE_NESTED_FAMILY:
dump << "protected ";
break;
case TYPE_ATTRIBUTE_NESTED_FAM_OR_ASSEM:
dump << "protected internal ";
break;
}
if (flags & TYPE_ATTRIBUTE_ABSTRACT && flags & TYPE_ATTRIBUTE_SEALED) {
dump << "static ";
} else if (!(flags & TYPE_ATTRIBUTE_INTERFACE) && flags & TYPE_ATTRIBUTE_ABSTRACT) {
dump << "abstract ";
} else if (!is_valuetype && !is_enum && flags & TYPE_ATTRIBUTE_SEALED) {
dump << "sealed ";
}
if (flags & TYPE_ATTRIBUTE_INTERFACE) {
dump << "interface ";
} else if (is_enum) {
dump << "enum ";
} else if (is_valuetype) {
dump << "struct ";
} else {
dump << "class ";
}
dump << il2cpp_class_get_name(klass); //TODO genericContainerIndex
dump << " : ";
auto parent = il2cpp_class_get_parent(klass);
if (!is_valuetype && !is_enum && parent) {
auto parent_type = il2cpp_class_get_type(parent);
if (parent_type->type != IL2CPP_TYPE_OBJECT) {
dump << il2cpp_class_get_namespace(klass) << "." << il2cpp_class_get_name(parent) << ",";
}
}
void *iter = nullptr;
while (auto itf = il2cpp_class_get_interfaces(klass, &iter)) {
dump << (il2cpp_class_get_name(itf)) << ",";
}
dump << "\n{";
dump_field(klass);
dump_property(klass);
dump_method(klass);
//TODO EventInfo
dump << "}\n";
}
extern "C" void pthread_atfork() { }
extern "C" int atexit(void (*function)(void)) { return 0; }
extern "C"
int entry(dlsym_t dlsym, uintptr_t il2cpp_base)
{
_dlsym = dlsym;
_il2cpp_base = il2cpp_base;
uprint("payload dlsym %p\n", dlsym);
void* x = _dlsym(NULL, "", (void*)_il2cpp_base);
uprint("payload dlsym %p\n", dlsym);
dump_fd = open("/dump.cs", O_RDWR | O_CREAT, 0777);
init_il2cpp_api();
auto domain = il2cpp_domain_get();
il2cpp_thread_attach(domain);
uprint("ready\n");
size_t size;
auto assemblies = il2cpp_domain_get_assemblies(domain, &size);
for (int i = 0; i < size; ++i) {
auto image = il2cpp_assembly_get_image(assemblies[i]);
// imageOutput << "// Image " << i << ": " << il2cpp_image_get_name(image) << "\n";
uprint("// Image %d: %s\n", i, il2cpp_image_get_name(image));
}
auto corlib = il2cpp_get_corlib();
auto assemblyClass = il2cpp_class_from_name(corlib, "System.Reflection", "Assembly");
auto assemblyLoad = il2cpp_class_get_method_from_name(assemblyClass, "Load", 1);
auto assemblyGetTypes = il2cpp_class_get_method_from_name(assemblyClass, "GetTypes", 0);
if (assemblyLoad && assemblyLoad->methodPointer) {
uprint("Assembly::Load: %p\n", assemblyLoad->methodPointer);
} else {
uprint("miss Assembly::Load\n");
return 0;
}
if (assemblyGetTypes && assemblyGetTypes->methodPointer) {
uprint("Assembly::GetTypes: %p\n", assemblyGetTypes->methodPointer);
} else {
uprint("miss Assembly::GetTypes\n");
return 0;
}
typedef void *(*Assembly_Load_ftn)(void *, Il2CppString *, void *);
typedef Il2CppArray *(*Assembly_GetTypes_ftn)(void *, void *);
for (int i = 0; i < size; ++i) {
auto image = il2cpp_assembly_get_image(assemblies[i]);
// std::stringstream imageStr;
auto image_name = il2cpp_image_get_name(image);
// imageStr << "\n// Dll : " << image_name;
//LOGD("image name : %s", image->name);
// auto imageName = std::string(image_name);
// auto pos = imageName.rfind('.');
// auto imageNameNoExt = imageName.substr(0, pos);
char imageNameNoExt[128];
const char* end = strrchr(image_name, '.');
memcpy(imageNameNoExt, image_name, end - image_name);
imageNameNoExt[end - image_name] = 0;
auto assemblyFileName = il2cpp_string_new(imageNameNoExt);
uprint("Dump %s %d/%d\n", imageNameNoExt, i, size);
// auto reflectionAssembly = ((Assembly_Load_ftn) assemblyLoad->methodPointer)(nullptr,
// assemblyFileName,
// nullptr);
void* args[] = { assemblyFileName, NULL };
Il2CppException *exc = NULL;
auto* reflectionAssembly = il2cpp_runtime_invoke(assemblyLoad, assemblyClass, args, &exc);
auto reflectionTypes = ((Assembly_GetTypes_ftn) assemblyGetTypes->methodPointer)(
reflectionAssembly, nullptr);
auto items = reflectionTypes->vector;
for (int j = 0; j < reflectionTypes->max_length; ++j) {
auto klass = il2cpp_class_from_system_type((Il2CppReflectionType *) items[j]);
auto type = il2cpp_class_get_type(klass);
// uprint("[x] type name : %s\n", il2cpp_type_get_name(type));
dump_type(type);
// auto outPut = imageStr.str() + dump_type(type);
// outPuts.push_back(outPut);
}
}
write(dump_fd, buf, buf_pos);
close(dump_fd);
uprint("done...\n");
return 0;
}