-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
394 lines (361 loc) · 16.6 KB
/
Main.cpp
File metadata and controls
394 lines (361 loc) · 16.6 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
#include <jni.h>
#include "universe.h"
#include <sys/mman.h>
#include <fstream>
#include <string>
#include <dlfcn.h>
#include <stdint.h>
#include <link.h>
#include <elf.h>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include "il2cpp-tabledefs.h"
#include <android/log.h>
#include <sys/stat.h>
#include <nlohmann/json.hpp>
#include <thread>
#include <chrono>
#include <iomanip>
#include <sstream>
using namespace std;
using json = nlohmann::json;
using namespace BNM;
using namespace BNM::Operators;
using namespace IL2CPP;
using namespace BNM::UnityEngine;
using namespace BNM::Structures::Mono;
using namespace BNM::Structures::Unity;
using namespace BNM::IL2CPP;
// Simple Logger Macros
#define LOG_TAG "DUMP"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
// Exported functions for IL2CPP
static const char* (*il2cpp_get_param_name_fn)(const MethodInfo*, uint32_t) = nullptr;
static void (*il2cpp_field_static_get_value_fn)(void*, void*) = nullptr;
static bool (*il2cpp_class_is_valuetype_fn)(const Il2CppClass *) = nullptr;
static uint16_t (*il2cpp_param_get_attrs_fn)(const Il2CppType*) = nullptr;
static void* (*il2cpp_domain_get_fn)() = nullptr;
static const Il2CppAssembly** (*il2cpp_domain_get_assemblies_fn)(const void* domain, size_t* size) = nullptr;
static const Il2CppImage* (*il2cpp_assembly_get_image_fn)(const Il2CppAssembly* assembly) = nullptr;
static const char* (*il2cpp_image_get_name_fn)(const Il2CppImage* image) = nullptr;
void* bnm_dlsym_finder(const char *name, void *userData) {
return dlsym(userData, name);
}
void InitIL2CPPExports(void* handle) {
if (il2cpp_get_param_name_fn) return;
il2cpp_get_param_name_fn = (const char* (*)(const MethodInfo*, uint32_t)) dlsym(handle, "il2cpp_method_get_param_name");
il2cpp_field_static_get_value_fn = (void (*)(void*, void*)) dlsym(handle, "il2cpp_field_static_get_value");
il2cpp_class_is_valuetype_fn = (bool (*)(const Il2CppClass *)) dlsym(handle, "il2cpp_class_is_valuetype");
il2cpp_param_get_attrs_fn = (uint16_t (*)(const Il2CppType*)) dlsym(handle, "il2cpp_param_get_attrs");
il2cpp_domain_get_fn = (void* (*)()) dlsym(handle, "il2cpp_domain_get");
il2cpp_domain_get_assemblies_fn = (const Il2CppAssembly** (*)(const void*, size_t*)) dlsym(handle, "il2cpp_domain_get_assemblies");
il2cpp_assembly_get_image_fn = (const Il2CppImage* (*)(const Il2CppAssembly*)) dlsym(handle, "il2cpp_assembly_get_image");
il2cpp_image_get_name_fn = (const char* (*)(const Il2CppImage*)) dlsym(handle, "il2cpp_image_get_name");
}
uintptr_t GetIL2CPPBase() {
static uintptr_t base = 0;
if (base == 0) {
Dl_info info;
auto adr = (BNM_PTR) BNM::Internal::GetIl2CppMethod(BNM_OBFUSCATE_TMP(BNM_IL2CPP_API_il2cpp_domain_get_assemblies));
if (adr && dladdr((void *) adr, &info)) {
base = reinterpret_cast<uintptr_t>(info.dli_fbase);
}
}
return base;
}
std::string ToHex(uintptr_t val) {
std::stringstream ss;
ss << "0x" << std::hex << std::uppercase << val;
return ss.str();
}
std::string GetTypeName(BNM::Class type) {
if (!type) return "unknown";
auto clazz = type.GetClass();
if (!clazz) return "unknown";
std::string name = clazz->name ? clazz->name : "unknown";
if (clazz->namespaze && std::string(clazz->namespaze) == "System") {
if (name == "Int32") return "int";
if (name == "Int64") return "long";
if (name == "Int16") return "short";
if (name == "UInt32") return "uint";
if (name == "UInt64") return "ulong";
if (name == "UInt16") return "ushort";
if (name == "Boolean") return "bool";
if (name == "String") return "string";
if (name == "Single") return "float";
if (name == "Double") return "double";
if (name == "Object") return "object";
if (name == "Void") return "void";
if (name == "Byte") return "byte";
if (name == "SByte") return "sbyte";
if (name == "Char") return "char";
if (name == "Decimal") return "decimal";
}
if (clazz->declaringType) return GetTypeName(clazz->declaringType) + "." + name;
if (clazz->namespaze && strlen(clazz->namespaze) > 0) return std::string(clazz->namespaze) + "." + name;
return name;
}
std::string GetAccess(uint32_t flags, bool isMethod) {
uint32_t access = flags & (isMethod ? METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK : FIELD_ATTRIBUTE_FIELD_ACCESS_MASK);
if (isMethod) {
switch (access) {
case METHOD_ATTRIBUTE_PRIVATE: return "private";
case METHOD_ATTRIBUTE_PUBLIC: return "public";
case METHOD_ATTRIBUTE_FAMILY: return "protected";
case METHOD_ATTRIBUTE_ASSEM: return "internal";
case METHOD_ATTRIBUTE_FAM_AND_ASSEM: return "private_protected";
case METHOD_ATTRIBUTE_FAM_OR_ASSEM: return "protected_internal";
default: return "private";
}
} else {
switch (access) {
case FIELD_ATTRIBUTE_PRIVATE: return "private";
case FIELD_ATTRIBUTE_PUBLIC: return "public";
case FIELD_ATTRIBUTE_FAMILY: return "protected";
case FIELD_ATTRIBUTE_ASSEMBLY: return "internal";
case FIELD_ATTRIBUTE_FAM_AND_ASSEM: return "private_protected";
case FIELD_ATTRIBUTE_FAM_OR_ASSEM: return "protected_internal";
default: return "private";
}
}
}
uintptr_t RVA_to_FileOffset(uintptr_t rva) {
if (rva == 0) return 0;
Dl_info info;
if (dladdr((void*)GetIL2CPPBase(), &info) == 0) return 0;
unsigned char *base = (unsigned char *)info.dli_fbase;
if (memcmp(base, ELFMAG, SELFMAG) != 0) return 0;
int elf_class = base[EI_CLASS];
if (elf_class == ELFCLASS64) {
Elf64_Ehdr *ehdr = (Elf64_Ehdr *)base;
Elf64_Phdr *phdr = (Elf64_Phdr *)(base + ehdr->e_phoff);
for (int i = 0; i < ehdr->e_phnum; i++) {
if (phdr[i].p_type == PT_LOAD && rva >= phdr[i].p_vaddr && rva < phdr[i].p_vaddr + phdr[i].p_memsz)
return (rva - phdr[i].p_vaddr) + phdr[i].p_offset;
}
} else if (elf_class == ELFCLASS32) {
Elf32_Ehdr *ehdr = (Elf32_Ehdr *)base;
Elf32_Phdr *phdr = (Elf32_Phdr *)(base + ehdr->e_phoff);
for (int i = 0; i < ehdr->e_phnum; i++) {
if (phdr[i].p_type == PT_LOAD && rva >= phdr[i].p_vaddr && rva < phdr[i].p_vaddr + phdr[i].p_memsz)
return (rva - phdr[i].p_vaddr) + phdr[i].p_offset;
}
}
return 0;
}
std::string CleanFileName(std::string name) {
std::string out = name;
std::replace(out.begin(), out.end(), '/', '_');
std::replace(out.begin(), out.end(), '\\', '_');
std::replace(out.begin(), out.end(), ':', '_');
return out;
}
void DumpSingleImageToJson(const std::string& dumpDir, Image img) {
if (!img || !img._data) return;
uintptr_t libBase = GetIL2CPPBase();
std::string dllName = img._data->name;
json root;
root["assembly"] = dllName;
root["classes"] = json::array();
for (auto &cls : img.GetClasses()) {
auto clazz = cls.GetClass();
if (clazz->declaringType) continue;
json classJson;
classJson["name"] = clazz->name;
classJson["fullName"] = GetTypeName(cls);
classJson["type"] = clazz->enumtype ? "enum" : (clazz->flags & TYPE_ATTRIBUTE_INTERFACE ? "interface" : (il2cpp_class_is_valuetype_fn && il2cpp_class_is_valuetype_fn(clazz) ? "struct" : "class"));
classJson["token"] = ToHex(clazz->token);
classJson["extends"] = cls.GetParent() ? GetTypeName(cls.GetParent()) : "System.Object";
classJson["fields"] = json::array();
for (auto &f : cls.GetFields(false)) {
auto *info = f._data;
json fJ;
fJ["name"] = info->name;
fJ["type"] = GetTypeName(BNM::Class(info->type));
fJ["access"] = GetAccess(info->type->attrs, false);
fJ["offset"] = ToHex(f.GetOffset());
fJ["token"] = ToHex(info->token);
fJ["isStatic"] = (info->type->attrs & FIELD_ATTRIBUTE_STATIC) != 0;
classJson["fields"].push_back(fJ);
}
classJson["methods"] = json::array();
for (auto &m : cls.GetMethods(false)) {
if (!m.IsValid()) continue;
auto *info = m.GetInfo();
json mJ;
mJ["name"] = info->name;
mJ["returns"] = GetTypeName(BNM::Class(info->return_type));
mJ["access"] = GetAccess(info->flags, true);
mJ["token"] = ToHex(info->token);
uintptr_t va = (uintptr_t)info->methodPointer;
uintptr_t rva = va ? va - libBase : 0;
mJ["va"] = ToHex(va);
mJ["rva"] = ToHex(rva);
mJ["offset"] = ToHex(RVA_to_FileOffset(rva));
mJ["params"] = json::array();
for (int i = 0; i < info->parameters_count; ++i) {
json pJ;
pJ["name"] = il2cpp_get_param_name_fn ? il2cpp_get_param_name_fn(info, i) : "arg" + std::to_string(i);
pJ["type"] = GetTypeName(BNM::Class(info->parameters[i]));
mJ["params"].push_back(pJ);
}
classJson["methods"].push_back(mJ);
}
root["classes"].push_back(classJson);
}
std::string path = dumpDir + "/" + CleanFileName(dllName) + "_Dump.json";
std::ofstream outFile(path);
if (outFile.is_open()) outFile << root.dump(4);
}
std::string GetMethodSignature(BNM::MethodBase method) {
if (!method.IsValid()) return "unknown()";
auto *info = method.GetInfo();
std::string modifiers;
uint16_t flags = info->flags;
modifiers += GetAccess(flags, true) + " ";
if (flags & METHOD_ATTRIBUTE_STATIC) modifiers += "static ";
if (flags & METHOD_ATTRIBUTE_ABSTRACT) {
modifiers += "abstract ";
if ((flags & METHOD_ATTRIBUTE_VTABLE_LAYOUT_MASK) == METHOD_ATTRIBUTE_REUSE_SLOT) modifiers += "override ";
} else if (flags & METHOD_ATTRIBUTE_VIRTUAL) {
if ((flags & METHOD_ATTRIBUTE_VTABLE_LAYOUT_MASK) == METHOD_ATTRIBUTE_NEW_SLOT) modifiers += "virtual ";
else modifiers += "override ";
}
std::string returnType = GetTypeName(BNM::Class(info->return_type));
if (info->return_type->byref) returnType = "ref " + returnType;
std::string params;
for (int i = 0; i < info->parameters_count; ++i) {
auto *paramType = info->parameters[i];
std::string pName = il2cpp_get_param_name_fn ? il2cpp_get_param_name_fn(info, i) : "arg" + std::to_string(i);
params += GetTypeName(BNM::Class(paramType)) + " " + pName;
if (i < info->parameters_count - 1) params += ", ";
}
return modifiers + returnType + " " + info->name + "(" + params + ")";
}
void DumpClassToFile(BNM::Class cls, std::ofstream &outFile, int indent = 0, bool isNested = false) {
if (!cls) return;
auto clazz = cls.GetClass();
if (!clazz) return;
std::string indentStr(indent, '\t');
std::string ns = clazz->namespaze ? clazz->namespaze : "";
if (!isNested && !ns.empty()) {
outFile << "namespace " << ns << " {" << std::endl;
indent++; indentStr = std::string(indent, '\t');
}
outFile << indentStr << "// VA: " << ToHex((uintptr_t)clazz) << " Token: " << ToHex(clazz->token) << std::endl;
if (clazz->enumtype) {
outFile << indentStr << "public enum " << clazz->name << std::endl;
outFile << indentStr << "{" << std::endl;
auto fields = cls.GetFields(false);
for (auto &f : fields) {
auto *info = f._data;
if (std::string(info->name) == "value__") continue;
int64_t val = 0;
if (il2cpp_field_static_get_value_fn) il2cpp_field_static_get_value_fn((void*)info, &val);
outFile << indentStr << "\t" << info->name << " = " << val << "," << std::endl;
}
outFile << indentStr << "}" << std::endl;
} else {
std::string kind = (clazz->flags & TYPE_ATTRIBUTE_INTERFACE) ? " interface " : (il2cpp_class_is_valuetype_fn && il2cpp_class_is_valuetype_fn(clazz) ? " struct " : " class ");
outFile << indentStr << "public" << kind << clazz->name;
if (cls.GetParent()) outFile << " : " << GetTypeName(cls.GetParent());
outFile << std::endl << indentStr << "{" << std::endl;
std::string cIndent = indentStr + "\t";
// Properties
auto properties = cls.GetProperties(false);
if (!properties.empty()) {
outFile << cIndent << "// Properties" << std::endl;
for (auto &p : properties) {
auto *info = p._data;
outFile << cIndent << "public " << GetTypeName(BNM::Class(info->get ? info->get->return_type : (info->set ? info->set->parameters[info->set->parameters_count - 1] : nullptr))) << " " << info->name << " { ";
if (info->get) outFile << "get; ";
if (info->set) outFile << "set; ";
outFile << "}" << std::endl;
}
outFile << std::endl;
}
for (auto &f : cls.GetFields(false)) {
auto *info = f._data;
outFile << cIndent << GetAccess(info->type->attrs, false) << " " << GetTypeName(BNM::Class(info->type)) << " " << info->name << "; // " << ToHex(f.GetOffset()) << std::endl;
}
for (auto &m : cls.GetMethods(false)) {
if (!m.IsValid()) continue;
auto *info = m.GetInfo();
uintptr_t va = (uintptr_t)info->methodPointer;
uintptr_t rva = va ? va - GetIL2CPPBase() : 0;
outFile << cIndent << GetMethodSignature(m) << "; // VA: " << ToHex(va) << " RVA: " << ToHex(rva) << std::endl;
}
if (clazz->nested_type_count > 0 && clazz->nestedTypes) {
for (int i = 0; i < clazz->nested_type_count; ++i) DumpClassToFile(BNM::Class(clazz->nestedTypes[i]), outFile, indent + 1, true);
}
outFile << indentStr << "}" << std::endl;
}
if (!isNested && !ns.empty()) outFile << "} // namespace " << ns << std::endl;
}
std::string GetPackageName() {
char buf[256]{0};
FILE *f = fopen("/proc/self/cmdline", "r");
if (f) { fread(buf, 1, sizeof(buf) - 1, f); fclose(f); }
return std::string(buf);
}
void start() {
std::thread([]() {
LOGI("Waiting 10s for game stability...");
std::this_thread::sleep_for(std::chrono::seconds(10));
std::string pkg = GetPackageName();
std::string dumpDir = "/storage/emulated/0/Android/data/" + pkg + "/files/dump";
mkdir(dumpDir.c_str(), 0777);
std::ofstream baseFile(dumpDir + "/base_address.txt");
baseFile << ToHex(GetIL2CPPBase());
baseFile.close();
auto images = BNM::Image::GetImages();
LOGI("Found %zu images. Starting full dump...", images.size());
for (size_t i = 0; i < images.size(); ++i) {
auto &img = images[i];
if (!img.IsValid() || !img._data) continue;
std::string dllName = img._data->name;
std::string safeName = CleanFileName(dllName);
LOGI("[%zu/%zu] Dumping %s...", i+1, images.size(), dllName.c_str());
// Dump to CS
std::string path = dumpDir + "/" + safeName + "_Dump.cs";
std::ofstream outFile(path);
for (auto &cls : img.GetClasses()) {
if (!cls.GetClass()->declaringType) DumpClassToFile(cls, outFile);
}
outFile.close();
// Dump to JSON
DumpSingleImageToJson(dumpDir, img);
}
LOGI("Full dump finished successfully!");
}).detach();
}
void maintenance_thread() {
LOGI("Maintenance thread started.");
void* handle = nullptr;
int retries = 0;
while (retries < 60) {
handle = dlopen("libil2cpp.so", RTLD_NOLOAD);
if (handle) {
InitIL2CPPExports(handle);
if (il2cpp_domain_get_fn && il2cpp_domain_get_fn() != nullptr) break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(500));
retries++;
}
if (handle) {
LOGI("libil2cpp.so found and initialized. Handle: %p", handle);
BNM::Loading::AddOnLoadedEvent(start);
BNM::Loading::SetMethodFinder(bnm_dlsym_finder, handle);
BNM::Loading::TrySetupByUsersFinder();
} else {
LOGE("Could not find libil2cpp.so after 30 seconds!");
}
}
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, [[maybe_unused]] void *reserved) {
LOGI("Dumper library loaded via JNI_OnLoad");
std::thread(maintenance_thread).detach();
return JNI_VERSION_1_6;
}