forked from ohio813/injector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanualmap.cpp
More file actions
563 lines (482 loc) · 13.5 KB
/
Copy pathmanualmap.cpp
File metadata and controls
563 lines (482 loc) · 13.5 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
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
////////////////////////////////////////////////////////////////////////////////////////////
// loader: command-line interface dll injector
// Copyright (C) 2009-2011 Wadim E. <wdmegrv@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
////////////////////////////////////////////////////////////////////////////////////////////
#include "manualmap.h"
// Matt Pietrek's function
PIMAGE_SECTION_HEADER GetEnclosingSectionHeader(DWORD_PTR rva, PIMAGE_NT_HEADERS pNTHeader)
{
PIMAGE_SECTION_HEADER section = IMAGE_FIRST_SECTION(pNTHeader);
WORD nSection = 0;
for(nSection = 0; nSection < pNTHeader->FileHeader.NumberOfSections; nSection++, section++ )
{
// This 3 line idiocy is because Watcom's linker actually sets the
// Misc.VirtualSize field to 0. (!!! - Retards....!!!)
DWORD_PTR size = section->Misc.VirtualSize;
if(size == 0)
{
size = section->SizeOfRawData;
}
// Is the RVA within this section?
if( (rva >= section->VirtualAddress) && (rva < (section->VirtualAddress + size)) )
{
return section;
}
}
return 0;
}
// Matt Pietrek's function
LPVOID GetPtrFromRVA(DWORD_PTR rva, PIMAGE_NT_HEADERS pNTHeader, PBYTE imageBase)
{
PIMAGE_SECTION_HEADER section;
LONG_PTR delta;
section = GetEnclosingSectionHeader(rva, pNTHeader);
if(!section)
{
return 0;
}
delta = (LONG_PTR)( section->VirtualAddress - section->PointerToRawData );
return (LPVOID)( imageBase + rva - delta );
}
BOOL
FixIAT(
DWORD dwProcessId,
HANDLE hProcess,
PBYTE imageBase,
PIMAGE_NT_HEADERS pNtHeader,
PIMAGE_IMPORT_DESCRIPTOR pImgImpDesc
)
{
BOOL bRet = FALSE;
LPSTR lpModuleName = 0;
HMODULE hLocalModule = 0;
HMODULE hRemoteModule = 0;
WCHAR modulePath[MAX_PATH + 1] = {0};
WCHAR moduleNtPath[500 + 1] = {0};
WCHAR targetProcPath[MAX_PATH + 1] = {0};
WCHAR *pch = 0;
__try
{
//printf("Fixing Imports:\n");
// get target process path
if(!GetModuleFileNameExW(hProcess, (HMODULE)0, targetProcPath, MAX_PATH))
{
PRINT_ERROR_MSGA("Could not get path to target process.");
__leave;
}
pch = wcsrchr(targetProcPath, '\\');
if(pch)
{
targetProcPath[ pch - targetProcPath + 1 ] = (WCHAR)0;
}
if(!SetDllDirectoryW(targetProcPath))
{
PRINT_ERROR_MSGW(L"Could not set path to target process (%s).", targetProcPath);
__leave;
}
while((lpModuleName = (LPSTR)GetPtrFromRVA(pImgImpDesc->Name, pNtHeader, imageBase)))
{
PIMAGE_THUNK_DATA itd = 0;
//printf("module: %s\n", lpModuleName);
// ACHTUNG: LoadLibraryEx kann eine DLL nur anhand des Namen aus einem anderen
// Verzeichnis laden wie der Zielprozess!
hLocalModule = LoadLibraryExA(lpModuleName, 0, DONT_RESOLVE_DLL_REFERENCES);
if(!hLocalModule)
{
PRINT_ERROR_MSGA("Could not load module locally.");
__leave;
}
// get full path of module
if(!GetModuleFileNameW(hLocalModule, modulePath, MAX_PATH))
{
PRINT_ERROR_MSGA("Could not get path to module (%s).", lpModuleName);
__leave;
}
// get nt path
if(!GetFileNameNtW(modulePath, moduleNtPath, 500))
{
PRINT_ERROR_MSGA("Could not get the NT namespace path.");
__leave;
}
// Module already in process?
hRemoteModule = (HMODULE)ModuleInjectedW(hProcess, moduleNtPath);
if(!hRemoteModule)
{
if(!InjectLibraryW(dwProcessId, modulePath))
{
PRINT_ERROR_MSGW(L"Could not inject required module (%s).\n", modulePath);
__leave;
}
hRemoteModule = (HMODULE)ModuleInjectedW(hProcess, moduleNtPath);
}
itd = (PIMAGE_THUNK_DATA)GetPtrFromRVA(pImgImpDesc->FirstThunk, pNtHeader, imageBase);
while(itd->u1.AddressOfData)
{
IMAGE_IMPORT_BY_NAME *iibn =
(PIMAGE_IMPORT_BY_NAME)GetPtrFromRVA(itd->u1.AddressOfData, pNtHeader, imageBase);
itd->u1.Function = (DWORD_PTR)GetRemoteProcAddress(hProcess, hRemoteModule, (LPCSTR)iibn->Name);
//printf("Function: %s\n", (LPCSTR)iibn->Name);
itd++;
}
pImgImpDesc++;
}
bRet = TRUE;
}
__finally
{
if(hLocalModule)
{
FreeLibrary(hLocalModule);
}
}
return bRet;
}
BOOL
MapSections(
HANDLE hProcess,
LPVOID lpModuleBase,
PBYTE dllBin,
PIMAGE_NT_HEADERS pNTHeader
)
{
PIMAGE_SECTION_HEADER section = IMAGE_FIRST_SECTION(pNTHeader);
SIZE_T virtualSize = 0;
WORD nSection = 0;
for(nSection = 0; nSection < pNTHeader->FileHeader.NumberOfSections; nSection++)
{
LPVOID lpBaseAddress = (LPVOID)( (DWORD_PTR)lpModuleBase + section->VirtualAddress );
LPCVOID lpBuffer = (LPCVOID)( (DWORD_PTR)dllBin + section->PointerToRawData );
SIZE_T NumBytesWritten = 0;
PDWORD lpflOldProtect = 0;
if(!WriteProcessMemory(hProcess, lpBaseAddress, lpBuffer, section->SizeOfRawData, &NumBytesWritten) ||
NumBytesWritten != section->SizeOfRawData)
{
PRINT_ERROR_MSGA("Could not write to memory in remote process.");
return FALSE;
}
// next section header, calculate virtualSize of section header
virtualSize = section->VirtualAddress;
//printf("section: %s | %p | %x\n", section->Name, section->VirtualAddress, virtualSize);
section++;
if(section->VirtualAddress)
{
virtualSize = section->VirtualAddress - virtualSize;
}
/*
if(!VirtualProtectEx(hProcess, (LPVOID)( (DWORD_PTR)lpModuleBase + section->VirtualAddress ), virtualSize,
section->Characteristics & 0x00FFFFFF, lpflOldProtect))
{
PRINT_ERROR_MSGA("VirtualProtectEx failed.");
return FALSE;
}
*/
}
return TRUE;
}
BOOL
FixRelocations(
PBYTE dllBin,
LPVOID lpModuleBase,
PIMAGE_NT_HEADERS pNtHeader,
PIMAGE_BASE_RELOCATION pImgBaseReloc
)
{
LONG_PTR delta = (DWORD_PTR)lpModuleBase - pNtHeader->OptionalHeader.ImageBase;
SIZE_T relocationSize = pNtHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].Size;
WORD *pRelocData = 0;
//printf("FixRelocs:\n");
// image has no relocations
if(!pImgBaseReloc->SizeOfBlock)
{
//printf("Image has no relocations\n");
return TRUE;
}
do
{
PBYTE pRelocBase = (PBYTE)GetPtrFromRVA(pImgBaseReloc->VirtualAddress, pNtHeader, dllBin);
SIZE_T numRelocations = (pImgBaseReloc->SizeOfBlock - sizeof(IMAGE_BASE_RELOCATION)) / sizeof(WORD);
SIZE_T i = 0;
//printf("numRelocations: %d\n", numRelocations);
pRelocData = (WORD*)( (DWORD_PTR)pImgBaseReloc + sizeof(IMAGE_BASE_RELOCATION) );
// loop over all relocation entries
for(i = 0; i < numRelocations; i++, pRelocData++)
{
// Get reloc data
BYTE RelocType = *pRelocData >> 12;
WORD Offset = *pRelocData & 0xFFF;
switch(RelocType)
{
case IMAGE_REL_BASED_ABSOLUTE:
break;
case IMAGE_REL_BASED_HIGHLOW:
*(DWORD32*)(pRelocBase + Offset) += (DWORD32)delta;
break;
case IMAGE_REL_BASED_DIR64:
*(DWORD64*)(pRelocBase + Offset) += delta;
break;
default:
PRINT_ERROR_MSGA("Unsuppported relocation type.");
return FALSE;
}
}
pImgBaseReloc = (PIMAGE_BASE_RELOCATION)pRelocData;
} while( *(DWORD*)pRelocData );
return TRUE;
}
BOOL
CallTlsInitializers(
PBYTE imageBase,
PIMAGE_NT_HEADERS pNtHeader,
HANDLE hProcess,
HMODULE hModule,
DWORD fdwReason,
PIMAGE_TLS_DIRECTORY pImgTlsDir
)
{
DWORD_PTR pCallbacks = (DWORD_PTR)pImgTlsDir->AddressOfCallBacks;
if(pCallbacks)
{
while(TRUE)
{
SIZE_T NumBytesRead = 0;
LPVOID callback = 0;
if(!ReadProcessMemory(hProcess, (PVOID)pCallbacks, &callback, sizeof(LPVOID), &NumBytesRead) ||
NumBytesRead != sizeof(LPVOID))
{
PRINT_ERROR_MSGA("Could not read memory in remote process.");
return FALSE;
}
if(!callback) break;
RemoteDllMainCall(hProcess, callback, hModule, fdwReason, 0);
//printf("callback: %p\n", callback);
pCallbacks += sizeof(DWORD_PTR);
}
}
return TRUE;
}
BOOL
MapRemoteModuleW(
DWORD dwProcessId,
LPCWSTR lpModulePath
)
{
BOOL bRet = FALSE;
HANDLE hFile = 0;
DWORD fileSize = 0;
BYTE *dllBin = 0;
PIMAGE_NT_HEADERS nt_header = 0;
PIMAGE_DOS_HEADER dos_header = 0;
HANDLE hProcess = 0;
LPVOID lpModuleBase = 0;
PIMAGE_IMPORT_DESCRIPTOR pImgImpDesc = 0;
PIMAGE_BASE_RELOCATION pImgBaseReloc = 0;
PIMAGE_TLS_DIRECTORY pImgTlsDir = 0;
__try
{
// Get a handle for the target process.
hProcess = OpenProcess(
PROCESS_QUERY_INFORMATION | // Required by Alpha
PROCESS_CREATE_THREAD | // For CreateRemoteThread
PROCESS_VM_OPERATION | // For VirtualAllocEx/VirtualFreeEx
PROCESS_VM_WRITE | // For WriteProcessMemory
PROCESS_VM_READ,
FALSE,
dwProcessId);
if(!hProcess)
{
PRINT_ERROR_MSGA("Could not get handle to process (PID: 0x%X).", dwProcessId);
__leave;
}
hFile = CreateFileW(
lpModulePath,
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(hFile == INVALID_HANDLE_VALUE)
{
PRINT_ERROR_MSGA("CreateFileW failed.");
__leave;
}
if(GetFileAttributesW(lpModulePath) & FILE_ATTRIBUTE_COMPRESSED)
{
fileSize = GetCompressedFileSizeW(lpModulePath, NULL);
}
else
{
fileSize = GetFileSize(hFile, NULL);
}
if(fileSize == INVALID_FILE_SIZE)
{
PRINT_ERROR_MSGA("Could not get size of file.");
__leave;
}
dllBin = (BYTE*)malloc(fileSize);
{
DWORD NumBytesRead = 0;
if(!ReadFile(hFile, dllBin, fileSize, &NumBytesRead, FALSE))
{
PRINT_ERROR_MSGA("ReadFile failed.");
}
}
dos_header = (PIMAGE_DOS_HEADER)dllBin;
// Make sure we got a valid DOS header
if(dos_header->e_magic != IMAGE_DOS_SIGNATURE)
{
PRINT_ERROR_MSGA("Invalid DOS header.");
__leave;
}
// Get the real PE header from the DOS stub header
nt_header = (PIMAGE_NT_HEADERS)( (DWORD_PTR)dllBin +
dos_header->e_lfanew);
// Verify the PE header
if(nt_header->Signature != IMAGE_NT_SIGNATURE)
{
PRINT_ERROR_MSGA("Invalid PE header.");
__leave;
}
// Allocate space for the module in the remote process
lpModuleBase = VirtualAllocEx(
hProcess,
NULL,
nt_header->OptionalHeader.SizeOfImage,
MEM_COMMIT | MEM_RESERVE,
PAGE_EXECUTE_READWRITE);
if(!lpModuleBase)
{
PRINT_ERROR_MSGA("Could not allocate memory in remote process.");
__leave;
}
// fix imports
pImgImpDesc = (PIMAGE_IMPORT_DESCRIPTOR)GetPtrFromRVA(
nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress,
nt_header,
(PBYTE)dllBin);
if(nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].Size)
{
if(!FixIAT(dwProcessId, hProcess, (PBYTE)dllBin, nt_header, pImgImpDesc))
{
PRINT_ERROR_MSGA("@Fixing imports.");
__leave;
}
}
// fix relocs
pImgBaseReloc = (PIMAGE_BASE_RELOCATION)GetPtrFromRVA(
(DWORD)(nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress),
nt_header,
(PBYTE)dllBin);
if(nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].Size)
{
if(!FixRelocations(dllBin, lpModuleBase, nt_header, pImgBaseReloc))
{
PRINT_ERROR_MSGA("@Fixing relocations.");
__leave;
}
}
// Write the PE header into the remote process's memory space
{
SIZE_T NumBytesWritten = 0;
SIZE_T nSize = nt_header->FileHeader.SizeOfOptionalHeader +
sizeof(nt_header->FileHeader) +
sizeof(nt_header->Signature);
if(!WriteProcessMemory(hProcess, lpModuleBase, dllBin, nSize, &NumBytesWritten) ||
NumBytesWritten != nSize)
{
PRINT_ERROR_MSGA("Could not write to memory in remote process.");
__leave;
}
}
// Map the sections into the remote process(they need to be aligned
// along their virtual addresses)
if(!MapSections(hProcess, lpModuleBase, dllBin, nt_header))
{
PRINT_ERROR_MSGA("@Map sections.");
__leave;
}
// call all tls callbacks
//
pImgTlsDir = (PIMAGE_TLS_DIRECTORY)GetPtrFromRVA(
nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS].VirtualAddress,
nt_header,
(PBYTE)dllBin);
if(nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS].Size)
{
if(!CallTlsInitializers(dllBin, nt_header, hProcess, (HMODULE)lpModuleBase, DLL_PROCESS_ATTACH, pImgTlsDir))
{
PRINT_ERROR_MSGA("@Call TLS initializers.");
__leave;
}
}
// call entry point
if(!RemoteDllMainCall(
hProcess,
(LPVOID)( (DWORD_PTR)lpModuleBase + nt_header->OptionalHeader.AddressOfEntryPoint),
(HMODULE)lpModuleBase, 1, 0))
{
PRINT_ERROR_MSGA("@Call DllMain.");
__leave;
}
bRet = TRUE;
wprintf(L"Successfully injected (%s | PID: %x):\n\n"
L" AllocationBase:\t0x%p\n"
L" EntryPoint:\t\t0x%p\n"
L" SizeOfImage:\t\t0x%p\n"
L" CheckSum:\t\t0x%p\n",
lpModulePath,
dwProcessId,
lpModuleBase,
(DWORD_PTR)lpModuleBase + nt_header->OptionalHeader.AddressOfEntryPoint,
nt_header->OptionalHeader.SizeOfImage,
nt_header->OptionalHeader.CheckSum);
}
__finally
{
if(hFile)
{
CloseHandle(hFile);
}
if(dllBin)
{
free(dllBin);
}
if(hProcess)
{
CloseHandle(hProcess);
}
}
return bRet;
}
BOOL
MapRemoteModuleA(
DWORD dwProcessId,
LPCSTR lpModulePath
)
{
BOOL bRet = FALSE;
wchar_t *libpath = char_to_wchar_t(lpModulePath);
if(libpath == 0)
{
return bRet;
}
bRet = MapRemoteModuleW(dwProcessId, libpath);
if(libpath)
{
free(libpath);
}
return bRet;
}