Post

Local-PE-Injection

Local-PE-Injection

Local-PE-Injection

LocalPE-Injection.c

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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
/*
	PE Injection Steps:
		1. Allocate memory to hold injected PE file.
		2. Copy over the PE sections to the allocated memory
		3. Address the PE relocations
		3. Fix PE IAT (Import Address Table)
		4. Set correct permission for each PE section
		5. Execute the PE entry point
*/

#include <Windows.h>
#include <stdio.h>
#include <winternl.h>

/*
	Reads a file from disk into a heap-allocated buffer

	cFileName -> Name of the PE file to read
	ppAddress -> Receives a pointer to the heap buffer containing the file data
	pdwFileSize -> Receives the size of the file in bytes
*/
BOOL ReadFileDisk(_In_ LPCSTR cFileName, _Out_ PBYTE* ppAddress, _Out_ PDWORD pdwFileSize) {
	
	HANDLE hFile = INVALID_HANDLE_VALUE;
	PBYTE pBuffer = NULL;
	DWORD dwFileSize = 0x00, dwNumBytesRead = 0x00;

	// Verify parameters are filled
	if (!cFileName || !ppAddress || !pdwFileSize)
		return FALSE;


	*ppAddress = NULL;
	*pdwFileSize = 0;

	// Open a handle to the existing file with read access
	hFile = CreateFileA(cFileName, GENERIC_READ, 0x00, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if (hFile == INVALID_HANDLE_VALUE) {
		printf("[!] CreateFileA Failed: %d \n", GetLastError());
		goto _CleanUp;
	}

	// Get filesize of file to be read
	dwFileSize = GetFileSize(hFile, NULL);
	if (dwFileSize == INVALID_FILE_SIZE) {
		printf("[!] GetFileSize Failed: %d \n", GetLastError());
		goto _CleanUp;
	}

	// Allocate memory for the file
	pBuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwFileSize);
	if (!pBuffer) {
		printf("[!] HeapAlloc Failed: %d \n", GetLastError());
		goto _CleanUp;
	}

	// Reading file
	if (!ReadFile(hFile, pBuffer, dwFileSize, &dwNumBytesRead, NULL) || dwFileSize != dwNumBytesRead) {
		printf("[!] ReadFile Failed: %d \n", GetLastError());
		goto _CleanUp;
	}

	// Assign output parameters
	*ppAddress = pBuffer;
	*pdwFileSize = dwFileSize;


_CleanUp:
	if (hFile != INVALID_HANDLE_VALUE)
		CloseHandle(hFile);
	if (!*ppAddress && pBuffer)
		HeapFree(GetProcessHeap(), 0, pBuffer);
	return ((*ppAddress != NULL) && (*pdwFileSize != 0x00)) ? TRUE : FALSE;

}

/*
	PE header structure — populated by ParsePEStruct(). Caches pointers to all relevant PE headers and data directory entries, so callers don't have to recompute them from the raw buffer repeatedly.
*/

typedef struct _PE_HDRS
{
	PBYTE                    pFileBuffer;
	DWORD                    dwFileSize;

	PIMAGE_NT_HEADERS        pImgNtHdr;
	PIMAGE_SECTION_HEADER    pImgSecHdr;

	PIMAGE_DATA_DIRECTORY    pEntryImportDataDir;
	PIMAGE_DATA_DIRECTORY    pEntryBaseRelocDataDir;
	PIMAGE_DATA_DIRECTORY    pEntryTLSDataDir;
	PIMAGE_DATA_DIRECTORY    pEntryExceptionDataDir;
	PIMAGE_DATA_DIRECTORY    pEntryExportDataDir;

	BOOL                     bIsDLLFile;

} PE_HDR, *PPE_HDR;

/*
	Parses a raw PE file buffer and populates a PE_HDR structure with pointers to the relevant headers and data directory entries

	pPEHdr      -> Pointer to the PE_HDR structure to populate
	pFileBuffer -> Pointer to the raw PE file buffer in memory
	dwFileSize  -> Size of the raw PE file buffer (in bytes)
*/
BOOL ParsePEStruct(_Out_ PPE_HDR pPEHdr, _In_ PBYTE pFileBuffer, _In_ DWORD dwFileSize) {

	// Verify parameters are filled
	if (!pPEHdr || !pFileBuffer || !dwFileSize)
		return FALSE;

	// Assign function parameters to struct
	pPEHdr->pFileBuffer = pFileBuffer;
	pPEHdr->dwFileSize = dwFileSize;
	pPEHdr->pImgNtHdr = (PIMAGE_NT_HEADERS)(pFileBuffer + ((PIMAGE_DOS_HEADER)pFileBuffer)->e_lfanew);

	// Check if NT Header is valid
	if (pPEHdr->pImgNtHdr->Signature != IMAGE_NT_SIGNATURE)
		return FALSE;

	// Populate Elements to struct
	pPEHdr->bIsDLLFile = (pPEHdr->pImgNtHdr->FileHeader.Characteristics & IMAGE_FILE_DLL) ? TRUE : FALSE;
	pPEHdr->pImgSecHdr = IMAGE_FIRST_SECTION(pPEHdr->pImgNtHdr);
	pPEHdr->pEntryImportDataDir = &pPEHdr->pImgNtHdr->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT];
	pPEHdr->pEntryBaseRelocDataDir = &pPEHdr->pImgNtHdr->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC];
	pPEHdr->pEntryTLSDataDir = &pPEHdr->pImgNtHdr->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS];
	pPEHdr->pEntryExceptionDataDir = &pPEHdr->pImgNtHdr->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXCEPTION];
	pPEHdr->pEntryExportDataDir = &pPEHdr->pImgNtHdr->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT];

	return TRUE;
}

/* 
	Resolves all imported function addresses for a PE image and patches them into the IAT (Import Address Thunk / FirstThunk array).
	Walks the import descriptor table, loads each referenced DLL via LoadLibraryA and resolves each function either by ordinal or by name via GetProcAddress.

	pEntryImportDataDir -> Pointer to the import data directory entry from the PE optional header (DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT])
	pPeBaseAddr -> Base address of the PE image mapped into memory, all RVAs in the import descriptors are relative to this
*/
BOOL InitIAT(_In_ PIMAGE_DATA_DIRECTORY pEntryImportDataDir, _In_ PBYTE pPeBaseAddr) {

	// Check parameters filled
	if (!pEntryImportDataDir || !pPeBaseAddr)
		return FALSE;

	// Current import descriptor one entry per imported DLL
	PIMAGE_IMPORT_DESCRIPTOR pImgDescriptor = NULL;

	// Walk the import descriptor array
	for (SIZE_T i = 0; i < pEntryImportDataDir->Size; i += sizeof(IMAGE_IMPORT_DESCRIPTOR)) {
		
		// Resolve current import descriptor from its RVA
		pImgDescriptor = (PIMAGE_IMPORT_DESCRIPTOR)(pPeBaseAddr + pEntryImportDataDir->VirtualAddress + i);

		// A fully zeroed descriptor marks the end of the import descriptor table
		if (pImgDescriptor->OriginalFirstThunk == NULL && pImgDescriptor->FirstThunk == NULL)
			break;

		// OriginalFirstThunk (INT) is the unmodified import name table
	    // Some linkers omit it fall back to FirstThunk (IAT) in that case
		LPSTR cDllName = (LPSTR)(pPeBaseAddr + pImgDescriptor->Name);
		ULONG_PTR uOriginalFirstThunkRVA = pImgDescriptor->OriginalFirstThunk;
		ULONG_PTR uFirstThunkRVA = pImgDescriptor->FirstThunk;
		SIZE_T sImgThunkSize = 0x00;
		HMODULE hModule = NULL;

		// Load the DLL into the current process so we can resolve its export(s)
		hModule = LoadLibraryA(cDllName);
		if (!hModule) {
			printf("[!] LoadLibraryA Failed: %d \n", GetLastError());
			return FALSE;
		}

		// Walk the thunk arrays for this DLL one entry per imported function
		while (TRUE) {

			// Get pointers to the first thunk and original first thunk data
			PIMAGE_THUNK_DATA			pOriginalFirstThunk = (PIMAGE_THUNK_DATA)(pPeBaseAddr + uOriginalFirstThunkRVA + sImgThunkSize);
			PIMAGE_THUNK_DATA			pFirstThunk = (PIMAGE_THUNK_DATA)(pPeBaseAddr + uFirstThunkRVA + sImgThunkSize);
			PIMAGE_IMPORT_BY_NAME		pImgImportByName = NULL;
			ULONG_PTR					pFuncAddress = 0;

			// A zeroed thunk entry marks the end of this DLL's import list
			if (pOriginalFirstThunk->u1.Function == NULL && pFirstThunk->u1.Function == 0)
				break;
			
			// High bit of Ordinal field set function is imported by ordinal, not by name
			if (IMAGE_SNAP_BY_ORDINAL(pOriginalFirstThunk->u1.Ordinal)) {
				if (!(pFuncAddress = (ULONG_PTR)GetProcAddress(hModule, IMAGE_ORDINAL(pOriginalFirstThunk->u1.Ordinal)))) {
					printf("[!] Could Not Import !%s#%d \n", cDllName, (int)pOriginalFirstThunk->u1.Ordinal);
					FreeLibrary(hModule);
					return FALSE;
				}
			}

			// Ordinal flag not set resolve by function name via IMAGE_IMPORT_BY_NAME
			else {
				pImgImportByName = (PIMAGE_IMPORT_BY_NAME)(pPeBaseAddr + pOriginalFirstThunk->u1.AddressOfData);
				if (!(pFuncAddress = (ULONG_PTR)GetProcAddress(hModule, pImgImportByName->Name))) {
					printf("[!] Could Not Import !%s.%s \n", cDllName, pImgImportByName->Name);
					FreeLibrary(hModule);
					return FALSE;
				}
			}

			// Patch the IAT entry with the resolved runtime address of the function
			pFirstThunk->u1.Function = (ULONGLONG)pFuncAddress;

			// Advance by one thunk entry (sizeof IMAGE_THUNK_DATA = pointer size)
			sImgThunkSize += sizeof(IMAGE_THUNK_DATA);

		}
	}

	return TRUE;
}

typedef struct _BASE_RELOCATION_ENTRY {
	WORD	Offset : 12;  // Specifies where the base relocation is to be applied.
	WORD	Type : 4;   // Indicates the type of base relocation to be applied.
} BASE_RELOCATION_ENTRY, * PBASE_RELOCATION_ENTRY;

/*
	Reallocation is necsarry to all entries within each block, to adjust hardcoded address within an executable image when its loaded at an different from its preferred base address

	pEntryBaseRelocDataDir -> Pointer to data dir for the relocations of the PE image
	pPeBaseAddr -> Address of PE image currently loaded in memory
	pPrefAddr -> The preferred base address of the PE image
*/


BOOL ApplyReloc(_In_ PIMAGE_DATA_DIRECTORY pEntryBaseRelocDataDir, _In_ ULONG_PTR pPeBaseAddr, _In_ ULONG_PTR pPrefAddr) {

	// Verify parameters are filled
	if (!pEntryBaseRelocDataDir || !pPeBaseAddr || !pPrefAddr)
		return FALSE;

	// Pointer to the beginning of the base relocation block
	PIMAGE_BASE_RELOCATION pImgBaseRelocation = (pPeBaseAddr + pEntryBaseRelocDataDir->VirtualAddress);

	// The difference between the current PE image base address and its preferable base address
	ULONG_PTR uDeltaOffset = pPeBaseAddr - pPrefAddr;
	
	// Pointer to individual base relocation entries.
	PBASE_RELOCATION_ENTRY pBaseRelocEntry = NULL;

	// Iterate over all base relocation blocks inside the PE image
	while (pImgBaseRelocation->VirtualAddress) {

		// pointer to first entry of the current block
		pBaseRelocEntry = (PBASE_RELOCATION_ENTRY)(pImgBaseRelocation + 1);

		while ((PBYTE)pBaseRelocEntry != (PBYTE)pImgBaseRelocation + pImgBaseRelocation->SizeOfBlock) {
			// adjust relocation entry based on its relocation type
			switch (pBaseRelocEntry->Type) {
			case IMAGE_REL_BASED_DIR64:
				// Adjust a 64-bit field by the delta offset.
				*((ULONG_PTR*)(pPeBaseAddr + pImgBaseRelocation->VirtualAddress + pBaseRelocEntry->Offset)) += uDeltaOffset;
				break;
			case IMAGE_REL_BASED_HIGHLOW:
				// Adjust a 32-bit field by the delta offset.
				*((DWORD*)(pPeBaseAddr + pImgBaseRelocation->VirtualAddress + pBaseRelocEntry->Offset)) += (DWORD)uDeltaOffset;
				break;
			case IMAGE_REL_BASED_HIGH:
				// Adjust the high 16 bits of a 32-bit field.
				*((WORD*)(pPeBaseAddr + pImgBaseRelocation->VirtualAddress + pBaseRelocEntry->Offset)) += HIWORD(uDeltaOffset);
				break;
			case IMAGE_REL_BASED_LOW:
				// Adjust the low 16 bits of a 32-bit field.
				*((WORD*)(pPeBaseAddr + pImgBaseRelocation->VirtualAddress + pBaseRelocEntry->Offset)) += LOWORD(uDeltaOffset);
				break;
			case IMAGE_REL_BASED_ABSOLUTE:
				// No relocation is required.
				break;
			default:
				// Handle unknown relocation types.
				printf("[!] Unknown relocation type: %d | Offset: 0x%08X \n", pBaseRelocEntry->Type, pBaseRelocEntry->Offset);
				return FALSE;
			}
			pBaseRelocEntry++;

		}

		// Iterate over next realocation block
		pImgBaseRelocation = (PIMAGE_BASE_RELOCATION)pBaseRelocEntry;
	}

	return TRUE;
}

/*
	Applies correct memory protection to each section of a PE image loaded into memory
    The image is initially mapped with blanket RW permissions -- this function inspects IMAGE_SECTION_HEADER.Characteristics for each section and calls VirtualProtect to set the appropriate R/W/X combination

	pPeBaseAddr -> Base address of PE Image
	pImgNtHdr -> Pointer to NT Header struct
	pImgSecHdr -> Pointer to Section header
*/

BOOL FixMemPermissions(_In_ ULONG_PTR pPeBaseAddr, _In_ PIMAGE_NT_HEADERS pImgNtHdr, _In_ PIMAGE_SECTION_HEADER pImgSecHdr) {

	if (!pPeBaseAddr || !pImgNtHdr || !pImgSecHdr)
		return FALSE;

	// Loop through each section of the PE image.
	for (DWORD i = 0; i < pImgNtHdr->FileHeader.NumberOfSections; i++) {

		// Variables to store the new and old memory protections.
		DWORD	dwProtection = PAGE_NOACCESS, dwOldProtection = 0x00;

		// Skip sections with no raw data or no mapped address 
		if (!pImgSecHdr[i].SizeOfRawData || !pImgSecHdr[i].VirtualAddress)
			continue;

		// Determine memory protection based on section characteristics
		// These characteristics dictate whether the section is readable, writable, executable etc
		if (pImgSecHdr[i].Characteristics & IMAGE_SCN_MEM_WRITE)
			dwProtection = PAGE_WRITECOPY;

		if (pImgSecHdr[i].Characteristics & IMAGE_SCN_MEM_READ)
			dwProtection = PAGE_READONLY;

		if ((pImgSecHdr[i].Characteristics & IMAGE_SCN_MEM_WRITE) && (pImgSecHdr[i].Characteristics & IMAGE_SCN_MEM_READ))
			dwProtection = PAGE_READWRITE;

		if (pImgSecHdr[i].Characteristics & IMAGE_SCN_MEM_EXECUTE)
			dwProtection = PAGE_EXECUTE;

		if ((pImgSecHdr[i].Characteristics & IMAGE_SCN_MEM_EXECUTE) && (pImgSecHdr[i].Characteristics & IMAGE_SCN_MEM_WRITE))
			dwProtection = PAGE_EXECUTE_WRITECOPY;

		if ((pImgSecHdr[i].Characteristics & IMAGE_SCN_MEM_EXECUTE) && (pImgSecHdr[i].Characteristics & IMAGE_SCN_MEM_READ))
			dwProtection = PAGE_EXECUTE_READ;

		if ((pImgSecHdr[i].Characteristics & IMAGE_SCN_MEM_EXECUTE) && (pImgSecHdr[i].Characteristics & IMAGE_SCN_MEM_WRITE) && (pImgSecHdr[i].Characteristics & IMAGE_SCN_MEM_READ))
			dwProtection = PAGE_EXECUTE_READWRITE;

		// Apply the determined memory protection to the section
		if (!VirtualProtect((PVOID)(pPeBaseAddr + pImgSecHdr[i].VirtualAddress), pImgSecHdr[i].SizeOfRawData, dwProtection, &dwOldProtection)) {
			printf("[!] VirtualProtect Failed: %d \n", GetLastError());
			return FALSE;
		}
	}

	return TRUE;
}


/*
	Resolves a named export's address from a loaded PE image's export directory
	Walks AddressOfNames to match cFuncName, then uses the parallel ordinal array to index into AddressOfFunctions for the resolved address 

	pEntryExportDataDir -> Pointer to the export data directory entry
	pPeBaseAddr      -> Base address of the PE image in memory
	cFuncName           -> Name of the exported function to resolve
*/
PVOID GetDllFunctionAddrs(_In_ PIMAGE_DATA_DIRECTORY pEntryExportDataDir, _In_ ULONG_PTR pPeBaseAddr, _In_ LPCSTR cFuncName) {

	if (!pEntryExportDataDir || !pPeBaseAddr || !cFuncName)
		return NULL;

	// Nothing to search if the export directory is empty
	if (!pEntryExportDataDir->VirtualAddress || !pEntryExportDataDir->Size)
		return NULL;

	PIMAGE_EXPORT_DIRECTORY pImgExportDir = (PIMAGE_EXPORT_DIRECTORY)(pPeBaseAddr + pEntryExportDataDir->VirtualAddress);
	
	// Parallel arrays: name[i] and ordinal[i] map to the same export; ordinal[i] indexes into the function address array
	PDWORD                  pFunctionNameArray = (PDWORD)(pPeBaseAddr + pImgExportDir->AddressOfNames);
	PDWORD                  pFunctionAddrArray = (PDWORD)(pPeBaseAddr + pImgExportDir->AddressOfFunctions);
	PWORD                   pFunctionOrdinalArray = (PWORD)(pPeBaseAddr + pImgExportDir->AddressOfNameOrdinals);

	// Export directory bounds -- used to detect forwarded exports
	ULONG_PTR uExportDirStart = pPeBaseAddr + pEntryExportDataDir->VirtualAddress;
	ULONG_PTR uExportDirEnd = uExportDirStart + pEntryExportDataDir->Size;

	// Iterate named exports only 
	for (DWORD i = 0; i < pImgExportDir->NumberOfNames; i++) {
		CHAR* pFunctionName = (CHAR*)(pPeBaseAddr + pFunctionNameArray[i]);
		PVOID  pFunctionAddr = (PVOID)(pPeBaseAddr + pFunctionAddrArray[pFunctionOrdinalArray[i]]);

		if (strcmp(cFuncName, pFunctionName) != 0)
			continue;

		// If the address points inside the export directory, it's a forwarded export
		// (e.g. "NTDLL.RtlAllocateHeap") -- not resolving forwarders here
		if ((ULONG_PTR)pFunctionAddr >= uExportDirStart &&
			(ULONG_PTR)pFunctionAddr < uExportDirEnd) {
			printf("[!] Forwarded export skipped: %s -> %s\n", cFuncName, (CHAR*)pFunctionAddr);
			return NULL;
		}

		return pFunctionAddr;
	}

	return NULL;
}

// Executing DLL
typedef BOOL(WINAPI* DLLMAIN)(HINSTANCE, DWORD, LPVOID);

// Executing Exe
typedef BOOL(WINAPI* MAIN)();

// Forward Declaration
SIZE_T CharStringToWCharString(_Inout_ PWCHAR Destination, _In_ PCHAR Source, _In_ SIZE_T MaximumAllowed);


VOID FixArgs(_In_ OPTIONAL LPCSTR cArgs) {
	PRTL_USER_PROCESS_PARAMETERS pParam = ((PPEB)__readgsqword(0x60))->ProcessParameters;

	// Zero out the existing command line buffer before rebuilding it
	RtlSecureZeroMemory(pParam->CommandLine.Buffer, pParam->CommandLine.MaximumLength);

	if (cArgs) {
		WCHAR* wArgumentsToPass = NULL;
		WCHAR* wNewCommand = NULL;

		// Allocate buffer for wide-char conversion of cArgs (+ null terminator)
		SIZE_T cArgsLen = strlen(cArgs);
		SIZE_T wArgsByteLen = (cArgsLen + 1) * sizeof(WCHAR);

		wArgumentsToPass = (WCHAR*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, wArgsByteLen);
		if (!wArgumentsToPass) {
			printf("HeapAlloc Failed: %d \n", GetLastError());
			return;
		}

		// Convert cArgs from char to wchar
		CharStringToWCharString(wArgumentsToPass, cArgs, wArgsByteLen);

		// Allocate buffer for full command line: "<ImagePath>" <args> + null terminator
		// ImagePathName.Length is in bytes -- divide by sizeof(WCHAR) for char count
		// +4 for the two quote chars, one space, and one null terminator
		SIZE_T wNewCmdCharCount = (pParam->ImagePathName.Length / sizeof(WCHAR)) + wcslen(wArgumentsToPass) + 4;

		wNewCommand = (WCHAR*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, wNewCmdCharCount * sizeof(WCHAR));
		if (!wNewCommand) {
			printf("HeapAlloc Failed: %d \n", GetLastError());
			HeapFree(GetProcessHeap(), 0, wArgumentsToPass);  // don't leak on failure
			return;
		}

		// Build the full command line string
		wsprintfW(wNewCommand, L"\"%s\" %s", pParam->ImagePathName.Buffer, wArgumentsToPass);

		// Overwrite PEB command line with the new value
		lstrcpyW(pParam->CommandLine.Buffer, wNewCommand);

		// Length excludes null terminator, MaximumLength includes it
		pParam->CommandLine.Length = (USHORT)(wcslen(pParam->CommandLine.Buffer) * sizeof(WCHAR));
		pParam->CommandLine.MaximumLength = pParam->CommandLine.Length + sizeof(WCHAR);

		HeapFree(GetProcessHeap(), 0, wArgumentsToPass);
		HeapFree(GetProcessHeap(), 0, wNewCommand);
		return;
	}

	// No arguments provided -- overwrite command line with image path only
	lstrcpyW(pParam->CommandLine.Buffer, pParam->ImagePathName.Buffer);

	// Length excludes null terminator, MaximumLength includes it
	pParam->CommandLine.Length = (USHORT)(wcslen(pParam->CommandLine.Buffer) * sizeof(WCHAR));
	pParam->CommandLine.MaximumLength = pParam->CommandLine.Length + sizeof(WCHAR);
}

/*
	Local PE
	pPeHdr -> Pointer to PE_HDRS structure populated by ParsePEStruct()
	cExportedFuncName -> Name of exported function to be executed
	cArgs -> The command line arguments that are passed to PE image
*/
BOOL LocalPE_Injection(_In_ PPE_HDR pPeHdr, _In_ OPTIONAL LPCSTR cExportedFuncName, _In_ OPTIONAL LPCSTR cArgs) {
	// Check if parameter is not zero
	if (!pPeHdr)
		return FALSE;

	BOOL	bState = FALSE;
	PBYTE	pPeBaseAddr = NULL;
	PVOID	pEntryPoint = NULL;
	PVOID	pExportedFuncAddress = NULL;

	// Allocate memory for PE image
	pPeBaseAddr = VirtualAlloc(NULL, pPeHdr->pImgNtHdr->OptionalHeader.SizeOfImage, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
	if (!pPeBaseAddr) {
		printf("[!] VirtualAlloc Failed: %d\n", GetLastError());
		goto _CleanUp;
	}
	// Copy PE headers into the allocated region
	memcpy(pPeBaseAddr, pPeHdr->pFileBuffer, pPeHdr->pImgNtHdr->OptionalHeader.SizeOfHeaders);
	// Copy each section from the raw file buffer into its virtual address
	for (DWORD i = 0; i < pPeHdr->pImgNtHdr->FileHeader.NumberOfSections; i++) {
		if (!pPeHdr->pImgSecHdr[i].SizeOfRawData || !pPeHdr->pImgSecHdr[i].VirtualAddress)
			continue;
		memcpy(
			pPeBaseAddr + pPeHdr->pImgSecHdr[i].VirtualAddress,
			pPeHdr->pFileBuffer + pPeHdr->pImgSecHdr[i].PointerToRawData,
			pPeHdr->pImgSecHdr[i].SizeOfRawData
		);
	}
	// Update IAT
	if (!InitIAT(pPeHdr->pEntryImportDataDir, pPeBaseAddr))
		goto _CleanUp;
	
	// Apply relocation blocks
	if (!ApplyReloc(pPeHdr->pEntryBaseRelocDataDir, pPeBaseAddr, pPeHdr->pImgNtHdr->OptionalHeader.ImageBase))
		goto _CleanUp;
	
	// Apply correct memory permissions per section
	if (!FixMemPermissions(pPeBaseAddr, pPeHdr->pImgNtHdr, pPeHdr->pImgSecHdr))
		goto _CleanUp;
	
	// Fetch exported function address (if DLL and export name was provided)
	if (pPeHdr->pEntryExportDataDir->Size && pPeHdr->pEntryExportDataDir->VirtualAddress && cExportedFuncName)
		pExportedFuncAddress = GetDllFunctionAddrs(pPeHdr->pEntryExportDataDir, (ULONG_PTR)pPeBaseAddr, cExportedFuncName);
	
	// Fix user arguments if provided
	if (cArgs)
		FixArgs(cArgs);
	
	// Register exception handlers of the loaded PE (if present)
	if (pPeHdr->pEntryExceptionDataDir->Size) {
		// Retrieve the runtime function table from the exception directory
		PIMAGE_RUNTIME_FUNCTION_ENTRY pImgRuntimeFuncEntry = (PIMAGE_RUNTIME_FUNCTION_ENTRY)(pPeBaseAddr + pPeHdr->pEntryExceptionDataDir->VirtualAddress);
		// Register the function table so SEH unwinding works correctly
		if (!RtlAddFunctionTable(pImgRuntimeFuncEntry, (pPeHdr->pEntryExceptionDataDir->Size / sizeof(IMAGE_RUNTIME_FUNCTION_ENTRY)), (DWORD64)pPeBaseAddr)) {
			printf("[!] RtlAddFunctionTable Failed: %d\n", GetLastError());
			goto _CleanUp;
		}
	}
	
	// Execute TLS callbacks (if present)
	if (pPeHdr->pEntryTLSDataDir->Size) {
		// Retrieve the TLS directory from the loaded image
		PIMAGE_TLS_DIRECTORY pImgTlsDirectory = (PIMAGE_TLS_DIRECTORY)(pPeBaseAddr + pPeHdr->pEntryTLSDataDir->VirtualAddress);
		// Get the TLS callback array -- terminated by a NULL entry
		PIMAGE_TLS_CALLBACK* pImgTlsCallback = (PIMAGE_TLS_CALLBACK*)(pImgTlsDirectory->AddressOfCallBacks);
		// Invoke each TLS callback in order until NULL terminator is reached
		for (int i = 0; pImgTlsCallback[i] != NULL; i++) {
			// Third parameter is reserved and must be NULL per the TLS callback ABI
			pImgTlsCallback[i]((LPVOID)pPeBaseAddr, DLL_PROCESS_ATTACH, NULL);
		}
	}
	
	// Resolve entry point from the optional header
	pEntryPoint = (PVOID)(pPeBaseAddr + pPeHdr->pImgNtHdr->OptionalHeader.AddressOfEntryPoint);
	
	// If DLL -- call DllMain then optionally execute the exported function on a new thread
	if (pPeHdr->bIsDLLFile) {
		DLLMAIN	pDllMain = (DLLMAIN)pEntryPoint;
		HANDLE	hThread = NULL;
		// Execute DllMain with DLL_PROCESS_ATTACH
		pDllMain((HINSTANCE)pPeBaseAddr, DLL_PROCESS_ATTACH, NULL);
		// Execute exported function on a separate thread (if resolved)
		if (pExportedFuncAddress)
			hThread = CreateThread(NULL, 0x00, pExportedFuncAddress, NULL, 0x00, NULL);
		if (hThread)
			WaitForSingleObject(hThread, INFINITE);
		bState = TRUE;
	}
	
	// If EXE -- transfer execution directly to the entry point
	else {
		MAIN pMain = (MAIN)pEntryPoint;
		bState = pMain();
	}

_CleanUp:
	if (pPeBaseAddr && !bState)
		VirtualFree(pPeBaseAddr, 0, MEM_RELEASE);
	return bState;
}



/*
	Parses command line arguments and extracts the -pe, -fptr, and -parm flags

	argc      -> Argument count from main
	argv      -> Argument vector from main
	ppe_arg   -> Receives pointer to the -pe value (path to PE file)
	pfptr_arg -> Receives pointer to the -fptr value (exported function name)
	pparm_arg -> Receives pointer to the heap-allocated -parm string, or NULL if not provided
*/
BOOL HandleCmdLineArgs(int argc, char* argv[], char** ppe_arg, char** pfptr_arg, char** pparm_arg) {
	if (!argv || !ppe_arg || !pfptr_arg || !pparm_arg)
		return FALSE;

	char* pe_arg = NULL;
	char* fptr_arg = NULL;
	char* parm_arg = NULL;

	// Heap-allocated stack buffer
	char* parm_buffer = (char*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 1024 * 2);
	if (!parm_buffer)
		return FALSE;

	// Walk argv starting at 1 
	for (int i = 1; i < argc; i++) {
		if (strcmp(argv[i], "-pe") == 0 && i + 1 < argc) {
			pe_arg = argv[++i];
		}
		else if (strcmp(argv[i], "-fptr") == 0 && i + 1 < argc) {
			fptr_arg = argv[++i];
		}
		else if (strcmp(argv[i], "-parm") == 0 && i + 1 < argc) {
			parm_arg = parm_buffer;
			strcpy_s(parm_arg, 1024 * 2, argv[++i]);
			
			// Collect remaining argv tokens until the next flag or end of args
			while (i + 1 < argc && argv[i + 1][0] != '-') {
				strcat_s(parm_arg, 1024 * 2, " ");
				strcat_s(parm_arg, 1024 * 2, argv[++i]);
			}
		}
	}

	*ppe_arg = pe_arg;
	*pfptr_arg = fptr_arg;
	*pparm_arg = parm_arg;

	// Free the buffer if -parm was never encountered -- no point keeping it
	if (!parm_arg)
		HeapFree(GetProcessHeap(), 0, parm_buffer);

	return TRUE;
}


// Tanks to: VX-Underground
SIZE_T CharStringToWCharString(_Inout_ PWCHAR Destination, _In_ PCHAR Source, _In_ SIZE_T MaximumAllowed) {
	INT Length = (INT)MaximumAllowed;

	while (--Length >= 0)
	{
		if (!(*Destination++ = *Source++))
			return MaximumAllowed - Length - 1;
	}

	return MaximumAllowed - Length;
}

static inline LPCSTR GetFileName(_In_ LPCSTR cPath) {
	LPCSTR pSlash = strrchr(cPath, '\\');
	return pSlash ? pSlash + 1 : cPath;
}


int main(int argc, char* argv[]) {

	char* pe_arg = NULL;
	char* fptr_arg = NULL;
	char* parm_arg = NULL;

	// Parse command line flags into their respective output pointers
	HandleCmdLineArgs(argc, argv, &pe_arg, &fptr_arg, &parm_arg);

	LPCSTR cBinaryName = GetFileName(argv[0]);


	if (!pe_arg) {
		printf("[!] Usage: %s -pe <Input PE> -fptr <*optional*: Exported function> -parm <*optional*: Arguments to pass> \n", cBinaryName);
		printf("\t<i> e.g. %s -pe DllMsgBox.dll -fptr HelloWorld \n", cBinaryName);
		printf("\t<i> e.g. %s -pe DllMsgBox.dll \n", cBinaryName);
		printf("\t<i> e.g. %s -pe ExeMsgBox.exe \n", cBinaryName);
		printf("\t<i> e.g. %s -pe ExeArgs.exe -parm this is a command line arg \n", cBinaryName);
		printf("\n\n");
		
		return -1;
	}

	PBYTE		pFileBuffer = NULL;
	DWORD		dwFileSize = 0x00;
	PE_HDR		pPeHdrStruct = { 0 };

	// Read PE from disk into a heap buffer
	if (!ReadFileDisk(pe_arg, &pFileBuffer, &dwFileSize))
		return -1;

	// Parse raw buffer into PE_HDR structure
	if (!ParsePEStruct(&pPeHdrStruct, pFileBuffer, dwFileSize))
		return -1;
	
	// Map and execute the PE image in the current process
	LocalPE_Injection(&pPeHdrStruct, fptr_arg, parm_arg);

	return 0;
}
This post is licensed under CC BY 4.0 by the author.