-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Expand file tree
/
Copy pathil2cpp_dump.cpp
More file actions
449 lines (432 loc) · 15.9 KB
/
Copy pathil2cpp_dump.cpp
File metadata and controls
449 lines (432 loc) · 15.9 KB
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
438
439
440
441
442
443
444
445
446
447
448
449
//
// Created by Perfare on 2020/7/4.
//
#include "il2cpp_dump.h"
#include <dlfcn.h>
#include <cstdlib>
#include <cstring>
#include <cinttypes>
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#include "log.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
static void *il2cpp_handle = nullptr;
static uint64_t il2cpp_base = 0;
void init_il2cpp_api() {
#define DO_API(r, n, p) n = (r (*) p)dlsym(il2cpp_handle, #n)
#include "il2cpp-api-functions.h"
#undef DO_API
}
uint64_t get_module_base(const char *module_name) {
uint64_t addr = 0;
char line[1024];
uint64_t start = 0;
uint64_t end = 0;
char flags[5];
char path[PATH_MAX];
FILE *fp = fopen("/proc/self/maps", "r");
if (fp != nullptr) {
while (fgets(line, sizeof(line), fp)) {
strcpy(path, "");
sscanf(line, "%" PRIx64"-%" PRIx64" %s %*" PRIx64" %*x:%*x %*u %s\n", &start, &end,
flags, path);
#if defined(__aarch64__)
if (strstr(flags, "x") == 0) //TODO
continue;
#endif
if (strstr(path, module_name)) {
addr = start;
break;
}
}
fclose(fp);
}
return addr;
}
std::string get_method_modifier(uint32_t flags) {
std::stringstream outPut;
auto access = flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK;
switch (access) {
case METHOD_ATTRIBUTE_PRIVATE:
outPut << "private ";
break;
case METHOD_ATTRIBUTE_PUBLIC:
outPut << "public ";
break;
case METHOD_ATTRIBUTE_FAMILY:
outPut << "protected ";
break;
case METHOD_ATTRIBUTE_ASSEM:
case METHOD_ATTRIBUTE_FAM_AND_ASSEM:
outPut << "internal ";
break;
case METHOD_ATTRIBUTE_FAM_OR_ASSEM:
outPut << "protected internal ";
break;
}
if (flags & METHOD_ATTRIBUTE_STATIC) {
outPut << "static ";
}
if (flags & METHOD_ATTRIBUTE_ABSTRACT) {
outPut << "abstract ";
if ((flags & METHOD_ATTRIBUTE_VTABLE_LAYOUT_MASK) == METHOD_ATTRIBUTE_REUSE_SLOT) {
outPut << "override ";
}
} else if (flags & METHOD_ATTRIBUTE_FINAL) {
if ((flags & METHOD_ATTRIBUTE_VTABLE_LAYOUT_MASK) == METHOD_ATTRIBUTE_REUSE_SLOT) {
outPut << "sealed override ";
}
} else if (flags & METHOD_ATTRIBUTE_VIRTUAL) {
if ((flags & METHOD_ATTRIBUTE_VTABLE_LAYOUT_MASK) == METHOD_ATTRIBUTE_NEW_SLOT) {
outPut << "virtual ";
} else {
outPut << "override ";
}
}
if (flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
outPut << "extern ";
}
return outPut.str();
}
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;
}
std::string dump_method(Il2CppClass *klass) {
std::stringstream outPut;
outPut << "\n\t// Methods\n";
void *iter = nullptr;
while (auto method = il2cpp_class_get_methods(klass, &iter)) {
//TODO attribute
if (method->methodPointer) {
outPut << "\t// RVA: 0x";
outPut << std::hex << (uint64_t) method->methodPointer - il2cpp_base;
outPut << " VA: 0x";
outPut << std::hex << (uint64_t) method->methodPointer;
} else {
outPut << "\t// RVA: 0x VA: 0x0";
}
/*if (method->slot != 65535) {
outPut << " Slot: " << std::dec << method->slot;
}*/
outPut << "\n\t";
uint32_t iflags = 0;
auto flags = il2cpp_method_get_flags(method, &iflags);
outPut << get_method_modifier(flags);
//TODO genericContainerIndex
auto return_type = il2cpp_method_get_return_type(method);
if (_il2cpp_type_is_byref(return_type)) {
outPut << "ref ";
}
auto return_class = il2cpp_class_from_type(return_type);
outPut << 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)) {
outPut << "out ";
} else if (attrs & PARAM_ATTRIBUTE_IN && !(attrs & PARAM_ATTRIBUTE_OUT)) {
outPut << "in ";
} else {
outPut << "ref ";
}
} else {
if (attrs & PARAM_ATTRIBUTE_IN) {
outPut << "[In] ";
}
if (attrs & PARAM_ATTRIBUTE_OUT) {
outPut << "[Out] ";
}
}
auto parameter_class = il2cpp_class_from_type(param);
outPut << il2cpp_class_get_name(parameter_class) << " "
<< il2cpp_method_get_param_name(method, i);
outPut << ", ";
}
if (param_count > 0) {
outPut.seekp(-2, outPut.cur);
}
outPut << ") { }\n";
//TODO GenericInstMethod
}
return outPut.str();
}
std::string dump_property(Il2CppClass *klass) {
std::stringstream outPut;
outPut << "\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);
outPut << "\t";
Il2CppClass *prop_class = nullptr;
uint32_t iflags = 0;
if (get) {
outPut << get_method_modifier(il2cpp_method_get_flags(get, &iflags));
prop_class = il2cpp_class_from_type(il2cpp_method_get_return_type(get));
} else if (set) {
outPut << 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) {
outPut << il2cpp_class_get_name(prop_class) << " " << prop_name << " { ";
if (get) {
outPut << "get; ";
}
if (set) {
outPut << "set; ";
}
outPut << "}\n";
} else {
if (prop_name) {
outPut << " // unknown property " << prop_name;
}
}
}
return outPut.str();
}
std::string dump_field(Il2CppClass *klass) {
std::stringstream outPut;
outPut << "\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
outPut << "\t";
auto attrs = il2cpp_field_get_flags(field);
auto access = attrs & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK;
switch (access) {
case FIELD_ATTRIBUTE_PRIVATE:
outPut << "private ";
break;
case FIELD_ATTRIBUTE_PUBLIC:
outPut << "public ";
break;
case FIELD_ATTRIBUTE_FAMILY:
outPut << "protected ";
break;
case FIELD_ATTRIBUTE_ASSEMBLY:
case FIELD_ATTRIBUTE_FAM_AND_ASSEM:
outPut << "internal ";
break;
case FIELD_ATTRIBUTE_FAM_OR_ASSEM:
outPut << "protected internal ";
break;
}
if (attrs & FIELD_ATTRIBUTE_LITERAL) {
outPut << "const ";
} else {
if (attrs & FIELD_ATTRIBUTE_STATIC) {
outPut << "static ";
}
if (attrs & FIELD_ATTRIBUTE_INIT_ONLY) {
outPut << "readonly ";
}
}
auto field_type = il2cpp_field_get_type(field);
auto field_class = il2cpp_class_from_type(field_type);
outPut << 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);
outPut << " = " << std::dec << val;
}
outPut << "; // 0x" << std::hex << il2cpp_field_get_offset(field) << "\n";
}
return outPut.str();
}
std::string dump_type(const Il2CppType *type) {
std::stringstream outPut;
auto *klass = il2cpp_class_from_type(type);
outPut << "\n// Namespace: " << il2cpp_class_get_namespace(klass) << "\n";
auto flags = il2cpp_class_get_flags(klass);
if (flags & TYPE_ATTRIBUTE_SERIALIZABLE) {
outPut << "[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:
outPut << "public ";
break;
case TYPE_ATTRIBUTE_NOT_PUBLIC:
case TYPE_ATTRIBUTE_NESTED_FAM_AND_ASSEM:
case TYPE_ATTRIBUTE_NESTED_ASSEMBLY:
outPut << "internal ";
break;
case TYPE_ATTRIBUTE_NESTED_PRIVATE:
outPut << "private ";
break;
case TYPE_ATTRIBUTE_NESTED_FAMILY:
outPut << "protected ";
break;
case TYPE_ATTRIBUTE_NESTED_FAM_OR_ASSEM:
outPut << "protected internal ";
break;
}
if (flags & TYPE_ATTRIBUTE_ABSTRACT && flags & TYPE_ATTRIBUTE_SEALED) {
outPut << "static ";
} else if (!(flags & TYPE_ATTRIBUTE_INTERFACE) && flags & TYPE_ATTRIBUTE_ABSTRACT) {
outPut << "abstract ";
} else if (!is_valuetype && !is_enum && flags & TYPE_ATTRIBUTE_SEALED) {
outPut << "sealed ";
}
if (flags & TYPE_ATTRIBUTE_INTERFACE) {
outPut << "interface ";
} else if (is_enum) {
outPut << "enum ";
} else if (is_valuetype) {
outPut << "struct ";
} else {
outPut << "class ";
}
outPut << il2cpp_class_get_name(klass); //TODO genericContainerIndex
std::vector<std::string> extends;
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) {
extends.emplace_back(il2cpp_class_get_name(parent));
}
}
void *iter = nullptr;
while (auto itf = il2cpp_class_get_interfaces(klass, &iter)) {
extends.emplace_back(il2cpp_class_get_name(itf));
}
if (!extends.empty()) {
outPut << " : " << extends[0];
for (int i = 1; i < extends.size(); ++i) {
outPut << ", " << extends[i];
}
}
outPut << "\n{";
outPut << dump_field(klass);
outPut << dump_property(klass);
outPut << dump_method(klass);
//TODO EventInfo
outPut << "}\n";
return outPut.str();
}
void il2cpp_dump(void *handle, char *outDir) {
//initialize
LOGI("il2cpp_handle: %p", handle);
il2cpp_handle = handle;
init_il2cpp_api();
if (il2cpp_domain_get_assemblies) {
Dl_info dlInfo;
if (dladdr((void *) il2cpp_domain_get_assemblies, &dlInfo)) {
il2cpp_base = reinterpret_cast<uint64_t>(dlInfo.dli_fbase);
} else {
LOGW("dladdr error, using get_module_base.");
il2cpp_base = get_module_base("libil2cpp.so");
}
LOGI("il2cpp_base: %" PRIx64"", il2cpp_base);
} else {
LOGE("Failed to initialize il2cpp api.");
return;
}
auto domain = il2cpp_domain_get();
il2cpp_thread_attach(domain);
//start dump
LOGI("dumping...");
size_t size;
auto assemblies = il2cpp_domain_get_assemblies(domain, &size);
std::stringstream imageOutput;
for (int i = 0; i < size; ++i) {
auto image = il2cpp_assembly_get_image(assemblies[i]);
imageOutput << "// Image " << i << ": " << il2cpp_image_get_name(image) << "\n";
}
std::vector<std::string> outPuts;
if (il2cpp_image_get_class) {
LOGI("Version greater than 2018.3");
//使用il2cpp_image_get_class
for (int i = 0; i < size; ++i) {
auto image = il2cpp_assembly_get_image(assemblies[i]);
std::stringstream imageStr;
imageStr << "\n// Dll : " << il2cpp_image_get_name(image);
auto classCount = il2cpp_image_get_class_count(image);
for (int j = 0; j < classCount; ++j) {
auto klass = il2cpp_image_get_class(image, j);
auto type = il2cpp_class_get_type(const_cast<Il2CppClass *>(klass));
//LOGD("type name : %s", il2cpp_type_get_name(type));
auto outPut = imageStr.str() + dump_type(type);
outPuts.push_back(outPut);
}
}
} else {
LOGI("Version less than 2018.3");
//使用反射
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) {
LOGI("Assembly::Load: %p", assemblyLoad->methodPointer);
} else {
LOGI("miss Assembly::Load");
return;
}
if (assemblyGetTypes && assemblyGetTypes->methodPointer) {
LOGI("Assembly::GetTypes: %p", assemblyGetTypes->methodPointer);
} else {
LOGI("miss Assembly::GetTypes");
return;
}
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);
auto assemblyFileName = il2cpp_string_new(imageNameNoExt.c_str());
auto reflectionAssembly = ((Assembly_Load_ftn) assemblyLoad->methodPointer)(nullptr,
assemblyFileName,
nullptr);
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);
//LOGD("type name : %s", il2cpp_type_get_name(type));
auto outPut = imageStr.str() + dump_type(type);
outPuts.push_back(outPut);
}
}
}
LOGI("write dump file");
auto outPath = std::string(outDir).append("/files/dump.cs");
std::ofstream outStream(outPath);
outStream << imageOutput.str();
auto count = outPuts.size();
for (int i = 0; i < count; ++i) {
outStream << outPuts[i];
}
outStream.close();
LOGI("dump done!");
}