Skip to content

Commit d4d4da8

Browse files
authored
Merge pull request #18 from mcbegamerxx954/main
Stability fixes
2 parents b4b85f6 + e02ff82 commit d4d4da8

File tree

2 files changed

+54
-17
lines changed

2 files changed

+54
-17
lines changed

src/Loader.cpp

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include "Util.h"
1111
#include "material.h"
1212

13-
1413
//==========================================================================================================================================
1514

1615
typedef struct _LoaderInvocationListener LoaderInvocationListener;
@@ -43,7 +42,7 @@ struct InvocationState {
4342
std::string* retstr;
4443
};
4544

46-
GumAddress minecraftpeBaseAddr;
45+
4746
GumAddress ResourcePackManager_ResourcePackManager;
4847
GumAddress AppPlatform_readAssetFile;
4948

@@ -59,14 +58,14 @@ void __attribute__((constructor)) init() {
5958

6059
gum_init();
6160

62-
minecraftpeBaseAddr = gum_module_find_base_address("libminecraftpe.so");
63-
64-
GumModuleMap* moduleMap = gum_module_map_new();
65-
const GumModuleDetails* minecraftpeDetails = gum_module_map_find(moduleMap, minecraftpeBaseAddr);
66-
61+
ModuleRange minecraftpeDetails = get_module_range();
62+
if (minecraftpeDetails.addr == 0 || minecraftpeDetails.len == 0) {
63+
// Failed initialization
64+
return;
65+
}
6766
#if __arm__
6867
//ResourcePackManager::ResourcePackManager
69-
ResourcePackManager_ResourcePackManager = FindSignatures(minecraftpeDetails,
68+
ResourcePackManager_ResourcePackManager = FindSignatures(&minecraftpeDetails,
7069
//1.20.50-1.20.81
7170
//"F0 B5 03 AF 2D E9 00 07 90 B0 05 46 AD 48 98 46 92 46 78 44 00 68 00 68 0F 90 08 69",
7271
//1.21.0-1.21.31
@@ -82,7 +81,7 @@ void __attribute__((constructor)) init() {
8281
);
8382

8483
//AppPlatform::readAssetFile
85-
AppPlatform_readAssetFile = FindSignatures(minecraftpeDetails,
84+
AppPlatform_readAssetFile = FindSignatures(&minecraftpeDetails,
8685
//1.20.50.21preview
8786
//"F0 B5 03 AF 4D F8 04 8D 9C B0 04 46 6E 48 78 44 00 68 00 68 1B 90 00 20 CD E9 08 00",
8887
//1.20.50-1.20.73
@@ -103,15 +102,15 @@ void __attribute__((constructor)) init() {
103102
}
104103
#elif __aarch64__
105104
//ResourcePackManager::ResourcePackManager
106-
ResourcePackManager_ResourcePackManager = FindSignatures(minecraftpeDetails,
105+
ResourcePackManager_ResourcePackManager = FindSignatures(&minecraftpeDetails,
107106
//1.20.50-1.21.50
108107
"FF 03 03 D1 FD 7B 07 A9 FD C3 01 91 F9 43 00 F9 F8 5F 09 A9 F6 57 0A A9 F4 4F 0B A9 59 D0 3B D5 F6 03 03 2A 28 17 40 F9 F5 03 02 AA F3 03 00 AA A8 83 1F F8 28 10 40 F9",
109108
//1.21.60.21preview
110109
"FF 83 02 D1 FD 7B 06 A9 FD 83 01 91 F8 5F 07 A9 F6 57 08 A9 F4 4F 09 A9 58 D0 3B D5 F6 03 03 2A 08 17 40 F9 F5 03 02 AA F3 03 00 AA A8 83 1F F8 28 10 40 F9 28 01 00 B4"
111110
);
112111

113112
//AppPlatform::readAssetFile
114-
AppPlatform_readAssetFile = FindSignatures(minecraftpeDetails,
113+
AppPlatform_readAssetFile = FindSignatures(&minecraftpeDetails,
115114
//1.20.50-1.20.73
116115
"FF 03 04 D1 FD 7B 0C A9 FD 03 03 91 FC 5F 0D A9 F6 57 0E A9 F4 4F 0F A9 57 D0 3B D5 F3 03 08 AA E8 16 40 F9 A8 83 1F F8 FF 7F 02 A9",
117116
//1.20.80-1.21.50
@@ -129,8 +128,6 @@ void __attribute__((constructor)) init() {
129128
printf("AppPlatform::readAssetFile not found\n");
130129
}
131130
#endif
132-
133-
g_object_unref(moduleMap);
134131

135132
interceptor = gum_interceptor_obtain();
136133
listener = (GumInvocationListener*) g_object_new(LOADER_TYPE_INVOCATION_LISTENER, NULL);

src/Util.h

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,53 @@
44
#include <vector>
55
#include <string>
66
#include "frida-gum.h"
7+
#include <bits/elf_common.h>
8+
#include <stddef.h>
9+
#include <stdint.h>
10+
#include <link.h>
11+
#include <string.h>
712

813
#define FindSignature FindSig
914
#define FindSignatures(moduleInfo, ...) FindSigs(moduleInfo, {__VA_ARGS__})
1015

11-
inline uintptr_t FindSig(const GumModuleDetails* moduleInfo, const std::string& signature) {
12-
uintptr_t moduleBase = (uintptr_t)moduleInfo->range->base_address;
13-
size_t moduleSize = moduleInfo->range->size;
16+
17+
struct ModuleRange {
18+
uintptr_t addr;
19+
size_t len;
20+
};
21+
22+
int callback(struct dl_phdr_info* dl_info, size_t _size, void* data) {
23+
// Skip if module is not minecraftpe
24+
if (strstr(dl_info->dlpi_name, "libminecraftpe") == NULL) {
25+
return 0;
26+
}
27+
28+
for (size_t i = 0; i < dl_info->dlpi_phnum; i++) {
29+
const ElfW(Phdr) phdr = dl_info->dlpi_phdr[i];
30+
// Check if we found a loadable and executable section
31+
// should be where the code is at
32+
if (phdr.p_type == PT_LOAD && phdr.p_flags & PF_X) {
33+
// Retrieve ModuleRange
34+
struct ModuleRange* mod_range = (struct ModuleRange*) data;
35+
mod_range->addr = dl_info->dlpi_addr + phdr.p_paddr;
36+
mod_range->len = phdr.p_memsz;
37+
// Stop dl_iterate_phdr
38+
return -1;
39+
}
40+
}
41+
return 0;
42+
}
43+
44+
/// Get module range of minecraftpe
45+
struct ModuleRange get_module_range() {
46+
struct ModuleRange mod_range = {0,0};
47+
dl_iterate_phdr(callback,(void*) &mod_range);
48+
return mod_range;
49+
}
50+
51+
inline uintptr_t FindSig(const ModuleRange* moduleInfo, const std::string& signature) {
52+
uintptr_t moduleBase = moduleInfo->addr;
53+
size_t moduleSize = moduleInfo->len;
1454

1555
std::vector<uint16_t> pattern;
1656
for (int i = 0; i < signature.size(); i++) {
@@ -53,7 +93,7 @@ inline uintptr_t FindSig(const GumModuleDetails* moduleInfo, const std::string&
5393
return 0;
5494
}
5595

56-
inline uintptr_t FindSigs(const GumModuleDetails* moduleInfo, const std::initializer_list<std::string>& signatures) {
96+
inline uintptr_t FindSigs(const ModuleRange* moduleInfo, const std::initializer_list<std::string>& signatures) {
5797
uintptr_t ptr = 0;
5898
for (auto& sig : signatures) {
5999
if ((ptr = FindSig(moduleInfo, sig))) {

0 commit comments

Comments
 (0)