Skip to content

Commit 78df8f1

Browse files
committed
[BUGFIX] Fixed missing system check before applying the patch. Flush cache after
1 parent 900a22a commit 78df8f1

File tree

3 files changed

+26
-20
lines changed

3 files changed

+26
-20
lines changed

run_pe/main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
#include <tchar.h>
44
#include "run_pe.h"
55

6-
LPCTSTR version = TEXT("0.1.9");
6+
LPCTSTR version = TEXT("0.2");
7+
78
bool g_PatchRequired = false;
89

910
bool isWindows1124H2OrLater()

run_pe/patch_ntdll.cpp

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,27 @@ bool patch_NtManageHotPatch32(HANDLE hProcess)
1717
if (!_NtManageHotPatch) {
1818
return false;
1919
}
20-
ULONG_PTR stub_ptr = (ULONG_PTR)_NtManageHotPatch;
20+
LPVOID stub_ptr = (LPVOID)_NtManageHotPatch;
2121

22-
if (!VirtualProtectEx(hProcess, (LPVOID)stub_ptr, stub_size, PAGE_READWRITE, &oldProtect)) {
22+
if (!VirtualProtectEx(hProcess, stub_ptr, stub_size, PAGE_READWRITE, &oldProtect)) {
2323
return false;
2424
}
2525
BYTE stub_buffer_orig[stub_size] = { 0 };
2626
SIZE_T out_bytes = 0;
27-
if (!ReadProcessMemory(hProcess, (LPVOID)stub_ptr, stub_buffer_orig, stub_size, &out_bytes) || out_bytes != stub_size) {
27+
if (!ReadProcessMemory(hProcess, stub_ptr, stub_buffer_orig, stub_size, &out_bytes) || out_bytes != stub_size) {
2828
return false;
2929
}
3030
// confirm it is a valid syscall stub:
3131
if (stub_buffer_orig[0] != 0xB8) {
3232
return false;
3333
}
34-
if (!WriteProcessMemory(hProcess, (LPVOID)stub_ptr, hotpatch_patch, sizeof(hotpatch_patch), &out_bytes) || out_bytes != sizeof(hotpatch_patch)) {
34+
if (!WriteProcessMemory(hProcess, stub_ptr, hotpatch_patch, sizeof(hotpatch_patch), &out_bytes) || out_bytes != sizeof(hotpatch_patch)) {
3535
return false;
3636
}
37-
if (!VirtualProtectEx(hProcess, (LPVOID)stub_ptr, stub_size, oldProtect, &oldProtect)) {
37+
if (!VirtualProtectEx(hProcess, stub_ptr, stub_size, oldProtect, &oldProtect)) {
3838
return false;
3939
}
40+
FlushInstructionCache(hProcess, stub_ptr, sizeof(hotpatch_patch));
4041
return true;
4142
}
4243

@@ -65,26 +66,27 @@ bool patch_NtManageHotPatch64(HANDLE hProcess)
6566
if (!_NtManageHotPatch) {
6667
return false;
6768
}
68-
ULONG_PTR stub_ptr = (ULONG_PTR)_NtManageHotPatch;
69+
LPVOID stub_ptr = (LPVOID)_NtManageHotPatch;
6970

70-
if (!VirtualProtectEx(hProcess, (LPVOID)stub_ptr, stub_size, PAGE_READWRITE, &oldProtect)) {
71+
if (!VirtualProtectEx(hProcess, stub_ptr, stub_size, PAGE_READWRITE, &oldProtect)) {
7172
return false;
7273
}
7374
BYTE stub_buffer_orig[stub_size] = { 0 };
7475
SIZE_T out_bytes = 0;
75-
if (!ReadProcessMemory(hProcess, (LPVOID)stub_ptr, stub_buffer_orig, stub_size, &out_bytes) || out_bytes != stub_size) {
76+
if (!ReadProcessMemory(hProcess, stub_ptr, stub_buffer_orig, stub_size, &out_bytes) || out_bytes != stub_size) {
7677
return false;
7778
}
7879
// confirm it is a valid syscall stub:
7980
if (::memcmp(stub_buffer_orig, syscall_fill_pattern, syscall_pattern_start) != 0) {
8081
return false;
8182
}
82-
if (!WriteProcessMemory(hProcess, (LPVOID)stub_ptr, hotpatch_patch, sizeof(hotpatch_patch), &out_bytes) || out_bytes != sizeof(hotpatch_patch)) {
83+
if (!WriteProcessMemory(hProcess, stub_ptr, hotpatch_patch, sizeof(hotpatch_patch), &out_bytes) || out_bytes != sizeof(hotpatch_patch)) {
8384
return false;
8485
}
85-
if (!VirtualProtectEx(hProcess, (LPVOID)stub_ptr, stub_size, oldProtect, &oldProtect)) {
86+
if (!VirtualProtectEx(hProcess, stub_ptr, stub_size, oldProtect, &oldProtect)) {
8687
return false;
8788
}
89+
FlushInstructionCache(hProcess, stub_ptr, sizeof(hotpatch_patch));
8890
return true;
8991
}
9092

@@ -104,9 +106,9 @@ bool patch_ZwQueryVirtualMemory(HANDLE hProcess, LPVOID module_ptr)
104106
if (!_ZwQueryVirtualMemory || _ZwQueryVirtualMemory < pos) {
105107
return false;
106108
}
107-
ULONG_PTR stub_ptr = (ULONG_PTR)_ZwQueryVirtualMemory - pos;
109+
LPVOID stub_ptr = (LPVOID)((ULONG_PTR)_ZwQueryVirtualMemory - pos);
108110

109-
if (!VirtualProtectEx(hProcess, (LPVOID)stub_ptr, stub_size, PAGE_READWRITE, &oldProtect)) {
111+
if (!VirtualProtectEx(hProcess, stub_ptr, stub_size, PAGE_READWRITE, &oldProtect)) {
110112
return false;
111113
}
112114
LPVOID patch_space = VirtualAllocEx(hProcess, 0, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
@@ -115,7 +117,7 @@ bool patch_ZwQueryVirtualMemory(HANDLE hProcess, LPVOID module_ptr)
115117
}
116118
BYTE stub_buffer_orig[stub_size] = { 0 };
117119
SIZE_T out_bytes = 0;
118-
if (!ReadProcessMemory(hProcess, (LPVOID)stub_ptr, stub_buffer_orig, stub_size, &out_bytes) || out_bytes != stub_size) {
120+
if (!ReadProcessMemory(hProcess, stub_ptr, stub_buffer_orig, stub_size, &out_bytes) || out_bytes != stub_size) {
119121
return false;
120122
}
121123
const BYTE nop_pattern[] = {0x0F, 0x1F, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00};
@@ -169,18 +171,19 @@ bool patch_ZwQueryVirtualMemory(HANDLE hProcess, LPVOID module_ptr)
169171

170172
const SIZE_T trampoline_full_size = stub_size + pos + syscall_pattern_full + sizeof(jump_to_contnue);
171173

172-
if (!WriteProcessMemory(hProcess, (LPVOID)stub_ptr, stub_buffer_patched, stub_size, &out_bytes) || out_bytes != stub_size) {
174+
if (!WriteProcessMemory(hProcess, stub_ptr, stub_buffer_patched, stub_size, &out_bytes) || out_bytes != stub_size) {
173175
return false;
174176
}
175-
if (!VirtualProtectEx(hProcess, (LPVOID)stub_ptr, stub_size, oldProtect, &oldProtect)) {
177+
if (!VirtualProtectEx(hProcess, stub_ptr, stub_size, oldProtect, &oldProtect)) {
176178
return false;
177179
}
178-
if (!WriteProcessMemory(hProcess, (LPVOID)patch_space, stub_buffer_trampoline, trampoline_full_size, &out_bytes) || out_bytes != trampoline_full_size) {
180+
if (!WriteProcessMemory(hProcess, patch_space, stub_buffer_trampoline, trampoline_full_size, &out_bytes) || out_bytes != trampoline_full_size) {
179181
return false;
180182
}
181-
if (!VirtualProtectEx(hProcess, (LPVOID)patch_space, stub_size, PAGE_EXECUTE_READ, &oldProtect)) {
183+
if (!VirtualProtectEx(hProcess, patch_space, stub_size, PAGE_EXECUTE_READ, &oldProtect)) {
182184
return false;
183185
}
186+
FlushInstructionCache(hProcess, stub_ptr, stub_size);
184187
return true;
185188
#endif
186189
}

run_pe/run_pe.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,13 @@ bool run_pe(IN LPCTSTR payloadPath, IN LPCTSTR targetPath, IN LPCTSTR cmdLine)
310310
free_pe_buffer(loaded_pe, payloadImageSize);
311311
return false;
312312
}
313+
if (g_PatchRequired) {
313314
#ifndef _WIN64
314-
patch_NtManageHotPatch32(pi.hProcess);
315+
patch_NtManageHotPatch32(pi.hProcess);
315316
#else
316-
patch_NtManageHotPatch64(pi.hProcess);
317+
patch_NtManageHotPatch64(pi.hProcess);
317318
#endif
319+
}
318320
//3. Perform the actual RunPE:
319321
bool isOk = _run_pe(loaded_pe, payloadImageSize, pi, is32bit_payload);
320322
//4. Cleanup:

0 commit comments

Comments
 (0)