-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdll_init.cpp
More file actions
180 lines (137 loc) · 3.88 KB
/
dll_init.cpp
File metadata and controls
180 lines (137 loc) · 3.88 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
#include "pch.h"
#define DLL_EXPORT
#include "dll_init.h"
#include "kernel_helpers.h"
#include "tools.h"
#include "bionic/bionic_tls.h"
#include <vector>
#include <Psapi.h>
static bool s_libc_initialized = false;
typedef void(*__libc_init_tls_bridge_func)(void* args_raw);
extern "C" void** __get_tls();
int libil_offset = -0x22a038;
int libcxx_offset = -0x2e824;
int libart_offset = -0x956b8;
std::vector<ModuleRuntimeInfo> _modules;
void install_breakpoint(void* pcode)
{
DWORD oldProtect;
if (VirtualProtectFromApp(pcode, 8, PAGE_READWRITE, &oldProtect) != 0)
{
*(int*)pcode = 0xE1200070;
/*FlushInstructionCache(
GetCurrentProcess(),
pcode,
8
);*/
DWORD prevProt;
if (!VirtualProtectFromApp(pcode, 8, oldProtect, &prevProt))
{
DWORD err = GetLastError();
}
}
}
int __dll_init(ModuleInfo32* moduleInfo, uint32_t ul_reason_for_call)
{
LPVOID lpvData;
BOOL fIgnore;
char tmp[260];
GetModuleFileNameA((HMODULE)moduleInfo->moduleBase, tmp, sizeof(tmp));
DebugLog("__dll_init %s\n", tmp);
if (ul_reason_for_call == DLL_PROCESS_ATTACH)
{
// copy import functions pointers to GOT
uint32_t* ptr = moduleInfo->mapTableOffset;
int32_t c = moduleInfo->mapTableCount * 2 - 1;
while (c > 0)
{
uintptr_t* symbol = (uintptr_t*)(((uint8_t*)ptr[c--]) + moduleInfo->moduleBase);
uintptr_t* dest = (uintptr_t*)(((uint8_t*)ptr[c--]) + moduleInfo->moduleBase);
*dest = *symbol;
}
// libc should be always first
if (!s_libc_initialized)
{
HMODULE libc_handle = (HMODULE)moduleInfo->moduleBase;
__libc_init_tls_bridge_func __libc_init_tls_bridge = (__libc_init_tls_bridge_func)GetProcAddress(libc_handle, "__libc_init_tls_bridge");
if (__libc_init_tls_bridge != NULL)
{
__libc_init_tls_bridge(build_kernel_args());
}
else
{
__debugbreak();
}
s_libc_initialized = true;
SYSTEM_INFO si;
GetSystemInfo(&si);
/*CreateProcess()*/
}
/*if (strstr(tmp, "libc++.dll") != NULL)
{
//install_breakpoint((void*)(moduleInfo->moduleBase + 0x0068640 + libcxx_offset));
int addr = moduleInfo->moduleBase + 0x0068640 + libcxx_offset;
DebugLog("libc++: 0x%x\n", addr);
}
if (strstr(tmp, "libart.dll") != NULL)
{
//install_breakpoint((void*)(moduleInfo->moduleBase + 0x0068640 + libcxx_offset));
int addr = moduleInfo->moduleBase + 0x00EBCA0 + libart_offset;
DebugLog("libart: 0x%x\n", addr);
}*/
typedef void(*InitFunction)();
uintptr_t* preInitArray = (uintptr_t*)(moduleInfo->preInitArrayOffset + moduleInfo->moduleBase);
for (int i = 0; i < moduleInfo->preInitArrayCount; i++)
{
InitFunction fn = (InitFunction)(preInitArray[i] + moduleInfo->moduleBase);
DebugLog("Calling module preinit constructor 0x%x\n", fn);
fn();
}
uintptr_t* initArray = (uintptr_t*)(moduleInfo->initArrayOffset + moduleInfo->moduleBase);
for (int i = 0; i < moduleInfo->initArrayCount; i++)
{
InitFunction fn = (InitFunction)(initArray[i] + moduleInfo->moduleBase);
DebugLog("Calling module constructor 0x%x\n", fn);
fn();
}
ModuleRuntimeInfo mi;
mi.handle = moduleInfo->moduleBase;
MODULEINFO win_mi;
if (GetModuleInformation(
GetCurrentProcess(),
(HMODULE)mi.handle,
&win_mi,
sizeof(win_mi))
)
{
mi.moduleStart = win_mi.lpBaseOfDll;
mi.moduleEnd = (char*)win_mi.lpBaseOfDll + win_mi.SizeOfImage;
}
else
{
mi.moduleStart = 0;
mi.moduleEnd = 0;
}
mi.unwindIdxPtr = (void*)(moduleInfo->moduleBase + moduleInfo->unwindIdxOffset);
mi.unwindIdxSize = moduleInfo->unwindIdxSize;
_modules.push_back(mi);
}
return TRUE;
}
int get_module_name(void* ptr, char* name, int size)
{
for (auto &i : _modules)
{
if (ptr > i.moduleStart && ptr < i.moduleEnd)
{
GetModuleFileNameA((HMODULE)i.moduleStart, name, size);
const char* namestart = strrchr(name, '\\');
if (namestart != NULL)
{
strcpy_s(name, size, namestart+1);
}
return 1;
}
}
return 0;
}