Skip to content

Commit 5c321b4

Browse files
修改bug与更新
1 parent dc75f14 commit 5c321b4

File tree

4 files changed

+82
-17
lines changed

4 files changed

+82
-17
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33

44
# 如何使用
55
~~先在目标手游的/data/data/packageName/files目录下创建test.txt文件,test.txt文件中的**每一行**均为要进行dump的程序集名称如Assembly-CSharp.dll,**注意这里创建以及写入test.txt文件务必在手机设备的环境上进行,否则可能会出现bug**。在创建/data/data/packageName/files/test.txt文件并写入要进行dump的程序集名称后即可~~
6-
通过任意ptrace注入器将该so模块注入至要dump的unity游戏进程中,主动调用dump将自动执行。可在logcat中使用Perfare进行过滤查看结果。如dump成功会在/data/data/packageName/files目录下生成dump.cs文件
6+
通过任意ptrace注入器将该so模块注入至要dump的unity游戏进程中,主动调用dump将自动执行。**最好是在游戏的相应逻辑触发后再进行注入,因为如果需要的类还未加载就不会被dump**可在logcat中使用Perfare进行过滤查看结果。如dump成功会在/data/data/packageName/files目录下生成test.cs以及test_method_info.txt文件
77

88
~~目标unity手游的所有程序集名称均可在其apk包的assets\bin\Data\ScriptingAssemblies.json中找到。~~
9-
推荐使用的ptrace注入器为[Android-Ptrace-Injector](https://github.com/reveny/Android-Ptrace-Injector)
9+
推荐使用的ptrace注入器为[Android-Ptrace-Injector](https://github.com/reveny/Android-Ptrace-Injector)[AndKittyInjector](https://github.com/MJx0/AndKittyInjector)
10+
11+
## 新增ida.py脚本
12+
使用ida加载libil2cpp.so后导入并执行ida.py脚本,再根据引导选择dump生成的test_method_info.txt文件即可重命名大部分函数。
1013

1114

1215
# 如何构建

app/src/main/cpp/hack.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include <fstream>
2020
#include <sstream>
2121

22+
char game_path[PATH_MAX];
23+
2224
void hack_start(const char *game_data_dir) {
2325
bool load = false;
2426
for (int i = 0; i < 10; i++) {
@@ -218,10 +220,9 @@ void start_dump(){
218220
buffer << file.rdbuf(); // 读取文件内容到 stringstream
219221
file.close();
220222

221-
char path[PATH_MAX];
222-
snprintf(path, PATH_MAX, "/data/data/%s",buffer.str().c_str());
223-
LOGI("game dir:%s\n", path);
224-
std::thread hack_thread(hack_start, path);
223+
snprintf(game_path, PATH_MAX, "/data/data/%s",buffer.str().c_str());
224+
LOGI("game dir:%s\n", game_path);
225+
std::thread hack_thread(hack_start, game_path);
225226
hack_thread.detach();
226227
}
227228
__attribute__((section(".init_array"))) void (*test_dump)() = start_dump;

app/src/main/cpp/il2cpp_dump.cpp

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626

2727
static uint64_t il2cpp_base = 0;
28+
std::stringstream method_info_outPut;
2829

2930
void init_il2cpp_api(void *handle) {
3031
#define DO_API(r, n, p) { \
@@ -102,6 +103,7 @@ std::string dump_method(Il2CppClass *klass) {
102103
if (method->methodPointer) {
103104
outPut << "\t// RVA: 0x";
104105
outPut << std::hex << (uint64_t) method->methodPointer - il2cpp_base;
106+
method_info_outPut << std::hex << (uint64_t) method->methodPointer - il2cpp_base;
105107
outPut << " VA: 0x";
106108
outPut << std::hex << (uint64_t) method->methodPointer;
107109
} else {
@@ -120,8 +122,12 @@ std::string dump_method(Il2CppClass *klass) {
120122
outPut << "ref ";
121123
}
122124
auto return_class = il2cpp_class_from_type(return_type);
123-
outPut << il2cpp_class_get_name(return_class) << " " << il2cpp_method_get_name(method)
125+
auto method_name = il2cpp_method_get_name(method);
126+
outPut << il2cpp_class_get_name(return_class) << " " << method_name
124127
<< "(";
128+
if(method->methodPointer){
129+
method_info_outPut << ":" << method_name << "\n";
130+
}
125131
auto param_count = il2cpp_method_get_param_count(method);
126132
for (int i = 0; i < param_count; ++i) {
127133
auto param = il2cpp_method_get_param(method, i);
@@ -420,9 +426,7 @@ void il2cpp_api_init(void *handle) {
420426

421427
void il2cpp_dump(const char *outDir) {
422428
LOGI("dumping...");
423-
std::stringstream imageOutput;
424429
std::vector<std::string> outPuts;
425-
426430
if (il2cpp_capture_memory_snapshot && il2cpp_free_captured_memory_snapshot) {
427431
auto memorySnapshot = il2cpp_capture_memory_snapshot();
428432
auto all_type_infos_count = memorySnapshot->metadata.typeCount;
@@ -447,18 +451,27 @@ void il2cpp_dump(const char *outDir) {
447451
LOGE("can not find il2cpp_capture_memory_snapshot!!!");
448452
}
449453

450-
451-
452454
LOGI("write dump file");
453-
auto outPath = std::string(outDir).append("/files/dump.cs");
455+
auto outPath = std::string(outDir).append("/files/test.cs");
454456
std::ofstream outStream(outPath);
455-
outStream << imageOutput.str();
456-
auto count = outPuts.size();
457-
for (int i = 0; i < count; ++i) {
458-
outStream << outPuts[i];
459-
}
457+
auto outPath2 = std::string(outDir).append("/files/test_method_info.txt");
458+
std::ofstream outStream2(outPath2);
460459

460+
if(outStream.is_open()){
461+
auto count = outPuts.size();
462+
for (int i = 0; i < count; ++i) {
463+
outStream << outPuts[i];
464+
}
465+
} else
466+
LOGE("can not open file:%s",outPath.c_str());
461467

462468
outStream.close();
469+
if (outStream2.is_open()){
470+
outStream2 << method_info_outPut.str();
471+
} else
472+
LOGE("can not open file:%s",outPath2.c_str());
473+
method_info_outPut.flush();
474+
method_info_outPut.clear();
475+
outStream2.close();
463476
LOGI("dump done!");
464477
}

ida.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# -*- coding: utf-8 -*-
2+
3+
def set_name(addr, name):
4+
new_name = name + '_' + hex(addr)
5+
idc.set_name(addr, new_name, SN_NOWARN | SN_NOCHECK)
6+
7+
def make_function(start, end):
8+
next_func = idc.get_next_func(start)
9+
if next_func < end:
10+
end = next_func
11+
if idc.get_func_attr(start, FUNCATTR_START) == start:
12+
ida_funcs.del_func(start)
13+
ida_funcs.add_func(start, end)
14+
15+
addresses = []
16+
fun_infos = []
17+
path = idaapi.ask_file(False, '*.txt', 'test_method_info.txt from PtraceIl2cppDumper')
18+
19+
20+
with open(path,"r",encoding="utf-8") as name_f:
21+
all_lines = name_f.readlines()
22+
23+
for line in all_lines:
24+
if line == "":
25+
break
26+
# 确保地址有效
27+
item = line.split(":")
28+
if item[0] == "":
29+
continue
30+
fun_name = item[1].replace("\n","")
31+
fun_addr = int(item[0],16)
32+
addresses.append(fun_addr)
33+
fun_infos.append((fun_addr,fun_name))
34+
35+
addresses.sort()
36+
37+
for index in range(len(addresses) - 1):
38+
start = addresses[index]
39+
end = addresses[index + 1]
40+
make_function(start, end)
41+
42+
for item in fun_infos:
43+
set_name(item[0], item[1])
44+
45+
46+
47+
print('Script finished!')
48+

0 commit comments

Comments
 (0)