-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathinjection.cpp
More file actions
284 lines (222 loc) · 10 KB
/
Copy pathinjection.cpp
File metadata and controls
284 lines (222 loc) · 10 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
#include "injection.h"
#include "files.h"
#include "utilities.h"
#include <TlHelp32.h>
#include <DbgHelp.h>
#include <intrin.h>
#include <winnt.h>
namespace injection {
// standard
namespace standard {
HANDLE open_process(DWORD process_id, ULONG flags) {
return OpenProcess(flags, false, process_id);
}
int inject(HANDLE process_handle, const wchar_t* dll_path) {
void* dll_path_address = VirtualAllocEx(process_handle, NULL, sizeof(wchar_t) * (wcslen(dll_path) + 1), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (!dll_path_address)
return -1;
size_t bytes_written;
if (!WriteProcessMemory(process_handle, dll_path_address, dll_path, sizeof(wchar_t) * (wcslen(dll_path) + 1), &bytes_written))
return -2;
void* loadlibrary = GetProcAddress(GetModuleHandleA("kernel32.dll"), "LoadLibraryW");
HANDLE thread_handle = CreateRemoteThread(process_handle, NULL, 0, reinterpret_cast<LPTHREAD_START_ROUTINE>(loadlibrary), dll_path_address, 0, NULL);
if (!thread_handle)
return -3;
WaitForSingleObject(thread_handle, INFINITE);
return 1;
}
int inject(HANDLE process_handle, std::wstring dll_path) {
return inject(process_handle, dll_path.c_str());
}
}
// hook
namespace hook {
int inject(DWORD thread_id, const wchar_t* dll_path, const char* export_name) {
HMODULE dll = LoadLibraryExW(dll_path, NULL, DONT_RESOLVE_DLL_REFERENCES);
if (!dll)
return -1;
void* export_address = GetProcAddress(dll, export_name);
if (!export_address)
return -2;
char filename[260];
DWORD got_filename = GetModuleFileNameA(dll, filename, 260);
HHOOK hook = SetWindowsHookExA(WH_GETMESSAGE, reinterpret_cast<HOOKPROC>(export_address), dll, thread_id);
if (!hook) {
printf("%x\n", GetLastError());
return -3;
}
while (!PostThreadMessageA(thread_id, WM_NULL, NULL, NULL))
Sleep(100);
return 1;
}
int inject(DWORD thread_id, std::wstring dll_path, std::string export_name) {
return inject(thread_id, dll_path.c_str(), export_name.c_str());
}
}
// manual map
namespace manual_map {
byte thread_hijack_shell[] = {
0x51, // push rcx
0x50, // push rax
0x52, // push rdx
0x48, 0x83, 0xEC, 0x20, // sub rsp, 0x20
0x48, 0xB9, // movabs rcx, ->
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x48, 0xBA, // movabs rdx, ->
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x48, 0xB8, // movabs rax, ->
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xFF, 0xD0, // call rax
0x48, 0xBA, // movabs rdx, ->
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x48, 0x89, 0x54, 0x24, 0x18, // mov qword ptr [rsp + 0x18], rdx
0x48, 0x83, 0xC4, 0x20, // add rsp, 0x20
0x5A, // pop rdx
0x58, // pop rax
0x59, // pop rcx
0xFF, 0x64, 0x24, 0xE0 // jmp qword ptr [rsp - 0x20]
};
IMAGE_SECTION_HEADER* translate_raw_section(IMAGE_NT_HEADERS* nt, DWORD rva) {
auto section = IMAGE_FIRST_SECTION(nt);
for (auto i = 0; i < nt->FileHeader.NumberOfSections; ++i, ++section) {
if (rva >= section->VirtualAddress && rva < section->VirtualAddress + section->Misc.VirtualSize)
return section;
}
return NULL;
}
void* translate_raw(char* base, IMAGE_NT_HEADERS* nt, DWORD rva) {
auto section = translate_raw_section(nt, rva);
if (!section)
return NULL;
return base + section->PointerToRawData + (rva - section->VirtualAddress);
}
template <class F>
bool resolve_imports(char* base, IMAGE_NT_HEADERS* nt, F get_proc_address) {
auto rva = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
if (!rva)
return true;
auto import = reinterpret_cast<IMAGE_IMPORT_DESCRIPTOR*>(translate_raw(base, nt, rva));
if (!import)
return true;
for (; import->FirstThunk; ++import) {
auto module_name = reinterpret_cast<char*>(translate_raw(base, nt, import->Name));
if (!module_name)
break;
for (auto thunk = reinterpret_cast<PIMAGE_THUNK_DATA>(translate_raw(base, nt, import->FirstThunk)); thunk->u1.AddressOfData; ++thunk) {
auto by_name = reinterpret_cast<PIMAGE_IMPORT_BY_NAME>(translate_raw(base, nt, static_cast<DWORD>(thunk->u1.AddressOfData)));
thunk->u1.Function = get_proc_address(module_name, by_name->Name);
}
}
return true;
}
void resolve_relocations(char* base, IMAGE_NT_HEADERS* nt, byte* mapped) {
auto& base_relocation = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC];
if (!base_relocation.VirtualAddress)
return;
auto reloc = reinterpret_cast<IMAGE_BASE_RELOCATION*>(translate_raw(base, nt, base_relocation.VirtualAddress));
if (!reloc)
return;
for (auto current_size = 0UL; current_size < base_relocation.Size; ) {
auto count = (reloc->SizeOfBlock - sizeof(IMAGE_BASE_RELOCATION)) / sizeof(WORD);
auto rdata = reinterpret_cast<WORD*>(reinterpret_cast<byte*>(reloc) + sizeof(IMAGE_BASE_RELOCATION));
auto rbase = reinterpret_cast<byte*>(translate_raw(base, nt, reloc->VirtualAddress));
for (auto i = 0UL; i < count; ++i, ++rdata) {
auto data = *rdata;
auto type = data >> 12;
auto offset = data & 0xFFF;
if (type == IMAGE_REL_BASED_DIR64)
*reinterpret_cast<PBYTE*>(rbase + offset) += (mapped - reinterpret_cast<PBYTE>(nt->OptionalHeader.ImageBase));
}
current_size += reloc->SizeOfBlock;
reloc = reinterpret_cast<IMAGE_BASE_RELOCATION*>(rdata);
}
}
int inject(DWORD process_id, DWORD thread_id, const wchar_t* dll_path) {
auto buffer = files::load_binary(dll_path);
auto dll = new char[buffer.length()]; memcpy(dll, buffer.data(), buffer.length());
auto module = LoadLibraryExW(dll_path, NULL, DONT_RESOLVE_DLL_REFERENCES);
IMAGE_DOS_HEADER* dos = reinterpret_cast<IMAGE_DOS_HEADER*>(dll);
if (dos->e_magic != IMAGE_DOS_SIGNATURE)
return -1;
IMAGE_NT_HEADERS* nt = reinterpret_cast<IMAGE_NT_HEADERS*>(reinterpret_cast<uint64_t>(dll) + dos->e_lfanew);
if (nt->Signature != IMAGE_NT_SIGNATURE)
return -2;
/* write image to process */
HANDLE process = standard::open_process(process_id, PROCESS_ALL_ACCESS);
void* image = VirtualAllocEx(process, NULL, nt->OptionalHeader.SizeOfImage, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if (!image) {
CloseHandle(process);
return -3;
}
// resolve imports
if (!resolve_imports(dll, nt, [](const char* module_name, const char* function_name) { return reinterpret_cast<uint64_t>(GetProcAddress(LoadLibraryA(module_name), function_name)); }))
return -4;
// resolve relocations
resolve_relocations(dll, nt, reinterpret_cast<byte*>(image));
// copy headers to image
WriteProcessMemory(process, image, dll, nt->OptionalHeader.SizeOfHeaders, NULL);
// copy sections to image
IMAGE_SECTION_HEADER* section = IMAGE_FIRST_SECTION(nt);
for (size_t i = 0; i < nt->FileHeader.NumberOfSections; i++) {
WriteProcessMemory(process, reinterpret_cast<void*>(reinterpret_cast<uint64_t>(image) + section[i].VirtualAddress),
reinterpret_cast<void*>(reinterpret_cast<uint64_t>(dll) + section[i].PointerToRawData), section[i].SizeOfRawData, NULL);
}
/* initialize security cookie */
auto init_cookie = [&]() {
if (!nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG].VirtualAddress)
return 1;
auto load_cfg = reinterpret_cast<IMAGE_LOAD_CONFIG_DIRECTORY64*>(reinterpret_cast<uint64_t>(dll) + nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG].VirtualAddress);
uint64_t security_cookie = load_cfg->SecurityCookie;
if (!security_cookie)
return 1;
// taken from blackbone, credits to darth
FILETIME sys_time = {};
LARGE_INTEGER performance_count = { {} };
GetSystemTimeAsFileTime(&sys_time);
QueryPerformanceCounter(&performance_count);
uint64_t cookie = process_id ^ thread_id ^ reinterpret_cast<uintptr_t>(&security_cookie);
cookie ^= *reinterpret_cast<uint64_t*>(&sys_time);
cookie ^= (performance_count.QuadPart << 32) ^ performance_count.QuadPart;
cookie &= 0xFFFFFFFFFFFF;
if (cookie == 0x2B992DDFA232)
cookie++;
return WriteProcessMemory(process, reinterpret_cast<void*>(reinterpret_cast<uint64_t>(image) + (cookie - reinterpret_cast<uint64_t>(dll))), &cookie, sizeof(void*), NULL);
};
init_cookie();
/* execute image */
void* loader = VirtualAllocEx(process, NULL, sizeof(thread_hijack_shell), MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if (!loader)
return -5;
HANDLE thread = OpenThread(THREAD_ALL_ACCESS, false, thread_id);
auto shell = thread_hijack_shell;
memcpy(shell + 9, &image, sizeof(void*));
void* reason = reinterpret_cast<void*>(DLL_PROCESS_ATTACH);
memcpy(shell + 19, &reason, sizeof(void*));
//void* entry_point = reinterpret_cast<void*>(reinterpret_cast<uint64_t>(image) + (reinterpret_cast<char*>(GetProcAddress(module, "DllMain")) - reinterpret_cast<char*>(module)));
void* entry_point = reinterpret_cast<void*>(reinterpret_cast<uint64_t>(image) + nt->OptionalHeader.AddressOfEntryPoint);
memcpy(shell + 29, &entry_point, sizeof(void*));
CONTEXT ctx = {}; ctx.ContextFlags = CONTEXT_FULL;
if (SuspendThread(thread) == -1 || !GetThreadContext(thread, &ctx)) {
VirtualFreeEx(process, image, NULL, MEM_RELEASE); VirtualFreeEx(process, loader, NULL, MEM_RELEASE);
CloseHandle(process); CloseHandle(thread);
return -6;
}
memcpy(shell + 41, &ctx.Rip, sizeof(void*));
if (!WriteProcessMemory(process, loader, shell, sizeof(thread_hijack_shell), NULL)) {
VirtualFreeEx(process, image, NULL, MEM_RELEASE); VirtualFreeEx(process, loader, NULL, MEM_RELEASE);
CloseHandle(process); CloseHandle(thread);
return -7;
}
ctx.Rip = reinterpret_cast<uint64_t>(loader);
if (!SetThreadContext(thread, &ctx) || ResumeThread(thread) == -1) {
VirtualFreeEx(process, image, NULL, MEM_RELEASE); VirtualFreeEx(process, loader, NULL, MEM_RELEASE);
CloseHandle(process); CloseHandle(thread);
return -8;
}
return 1;
}
int inject(DWORD process_id, DWORD thread_id, std::wstring dll_path) {
return inject(process_id, thread_id, dll_path.c_str());
}
}
};