@@ -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}
0 commit comments