libPeConv
A library to load, manipulate, dump PE files.
Loading...
Searching...
No Matches
hooks.cpp
Go to the documentation of this file.
1#include "peconv/hooks.h"
2#include "peconv.h"
3#include "peconv/peb_lookup.h"
4
5using namespace peconv;
6
7namespace peconv {
8
9 bool is_pointer_in_ntdll(LPVOID lpAddress)
10 {
11 HMODULE mod = peconv::get_module_via_peb(L"ntdll.dll");
12 size_t module_size = peconv::get_module_size_via_peb(mod);
13 if (peconv::validate_ptr(mod, module_size, lpAddress, sizeof(BYTE))) {
14 return true; //this address lies within NTDLL
15 }
16 return false;
17 }
18
19 BOOL nt_protect(LPVOID lpAddress, SIZE_T dwSize, DWORD flNewProtect, PDWORD lpflOldProtect)
20 {
21 FARPROC proc = peconv::get_exported_func(
22 peconv::get_module_via_peb(L"ntdll.dll"),
23 "NtProtectVirtualMemory"
24 );
25 if (!proc) {
26 return FALSE;
27 }
28 NTSTATUS(NTAPI *_NtProtectVirtualMemory)(
29 IN HANDLE,
30 IN OUT PVOID*,
31 IN OUT PSIZE_T,
32 IN DWORD,
33 OUT PDWORD) =
34 (NTSTATUS(NTAPI *)(
35 IN HANDLE,
36 IN OUT PVOID*,
37 IN OUT PSIZE_T,
38 IN DWORD,
39 OUT PDWORD)) proc;
40
41 SIZE_T protect_size = dwSize;
42 NTSTATUS status = _NtProtectVirtualMemory(GetCurrentProcess(), &lpAddress, &protect_size, flNewProtect, lpflOldProtect);
43 if (status != S_OK) {
44 return FALSE;
45 }
46 return TRUE;
47 }
48};
49
50bool PatchBackup::makeBackup(BYTE *patch_ptr, size_t patch_size)
51{
52 if (!patch_ptr) {
53 return false;
54 }
56 this->sourcePtr = patch_ptr;
57 this->buffer = new BYTE[patch_size];
58 this->bufferSize = patch_size;
59
60 memcpy(buffer, patch_ptr, patch_size);
61 return true;
62}
63
65{
66 if (!isBackup()) {
67 return false;
68 }
69 DWORD oldProtect = 0;
70 if (!nt_protect((LPVOID)sourcePtr, bufferSize, PAGE_EXECUTE_READWRITE, &oldProtect)) {
71 return false;
72 }
73 memcpy(sourcePtr, buffer, bufferSize);
74 nt_protect((LPVOID)sourcePtr, bufferSize, oldProtect, &oldProtect);
75
76 //flush cache:
77 FlushInstructionCache(GetCurrentProcess(), sourcePtr, bufferSize);
78 return true;
79}
80
81FARPROC peconv::hooking_func_resolver::resolve_func(LPCSTR lib_name, LPCSTR func_name)
82{
83 //the name may be ordinal rather than string, so check if it is a valid pointer:
84 if (!peconv::is_bad_read_ptr(func_name, 1)) {
85 std::map<std::string, FARPROC>::const_iterator itr = hooks_map.find(func_name);
86 if (itr != hooks_map.end()) {
87 FARPROC hook = itr->second;
88#ifdef _DEBUG
89 std::cout << ">>>>>>Replacing: " << func_name << " by: " << hook << std::endl;
90#endif
91 return hook;
92 }
93 }
94 // resolve eventual replacement DLLs
95 std::string lib_name_str = peconv::is_bad_read_ptr(lib_name, 1) ? "": lib_name;
96 std::map<std::string, std::string>::const_iterator itr2 = this->dll_replacements_map.find(lib_name_str);
97 if (itr2 != dll_replacements_map.end()) {
98 const std::string &name = itr2->second;
99#ifdef _DEBUG
100 std::cout << ">>>>>>Replacing DLL: " << lib_name_str << " by: " << name << std::endl;
101#endif
102 lib_name_str = name;
103 }
104 return peconv::default_func_resolver::resolve_func(lib_name_str.c_str(), func_name);
105}
106
107size_t peconv::redirect_to_local64(void *ptr, ULONGLONG new_offset, PatchBackup* backup)
108{
109 if (!ptr) return 0;
110
111 BYTE hook_64[] = {
112 0x48, 0xB8, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xEE, 0xFF, //movabs rax,FFEE998877665544
113 0xFF, 0xE0 //jmp rax
114 };
115 const size_t hook64_size = sizeof(hook_64);
116 if (is_pointer_in_ntdll(ptr)) {
117 std::cout << "[WARNING] Patching NTDLL is not allowed because of possible stability issues!\n";
118 return 0;
119 }
120 DWORD oldProtect = 0;
121 if (!nt_protect((LPVOID)ptr,
122 hook64_size,
123 PAGE_EXECUTE_READWRITE, //this must be executable if we are hooking kernel32.dll, because we are using VirtualProtect from kernel32 at the same time
124 &oldProtect))
125 {
126 return 0;
127 }
128
129 if (backup != nullptr) {
130 backup->makeBackup((BYTE*)ptr, hook64_size);
131 }
132 memcpy(hook_64 + 2, &new_offset, sizeof(ULONGLONG));
133 memcpy(ptr, hook_64, hook64_size);
134
135 nt_protect((LPVOID)ptr, hook64_size, oldProtect, &oldProtect);
136
137 //flush cache:
138 FlushInstructionCache(GetCurrentProcess(), ptr, hook64_size);
139 return hook64_size;
140}
141
142size_t peconv::redirect_to_local32(void *ptr, DWORD new_offset, PatchBackup* backup)
143{
144 if (!ptr) return 0;
145
146 BYTE hook_32[] = {
147 0xB8, 0xCC, 0xDD, 0xEE, 0xFF, // mov eax,FFEEDDCC
148 0xFF, 0xE0 //jmp eax
149 };
150 const size_t hook32_size = sizeof(hook_32);
151 if (is_pointer_in_ntdll(ptr)) {
152 std::cout << "[WARNING] Patching NTDLL is not allowed because of possible stability issues!\n";
153 return 0;
154 }
155 DWORD oldProtect = 0;
156 if (!nt_protect((LPVOID)ptr,
157 hook32_size,
158 PAGE_EXECUTE_READWRITE, //this must be executable if we are hooking kernel32.dll, because we are using VirtualProtect from kernel32 at the same time
159 &oldProtect))
160 {
161 return 0;
162 }
163
164 if (backup != nullptr) {
165 backup->makeBackup((BYTE*)ptr, hook32_size);
166 }
167 memcpy(hook_32 + 1, &new_offset, sizeof(DWORD));
168 memcpy(ptr, hook_32, hook32_size);
169
170 nt_protect((LPVOID)ptr, hook32_size, oldProtect, &oldProtect);
171
172 //flush cache:
173 FlushInstructionCache(GetCurrentProcess(), ptr, hook32_size);
174 return hook32_size;
175}
176
177size_t peconv::redirect_to_local(void *ptr, void* new_function_ptr, PatchBackup* backup)
178{
179#ifdef _WIN64
180 return peconv::redirect_to_local64(ptr, (ULONGLONG)new_function_ptr, backup);
181#else
182 return peconv::redirect_to_local32(ptr, (DWORD)new_function_ptr, backup);
183#endif
184}
185
186inline long long int get_jmp_delta(ULONGLONG currVA, int instrLen, ULONGLONG destVA)
187{
188 long long int diff = destVA - (currVA + instrLen);
189 return diff;
190}
191
192inline bool is_valid_delta(long long int delta)
193{
194 DWORD first_dw = delta >> sizeof(DWORD) * 8;
195 if (first_dw == 0) {
196 return true;
197 }
198 const DWORD max_dword = DWORD(-1);
199 if (first_dw != max_dword) {
200 return false;
201 }
202 DWORD delta_dw = DWORD(delta);
203 if (delta_dw & 0x80000000) {
204 return true;
205 }
206 //invalid, sign bit is missing
207 return false;
208}
209
210bool peconv::replace_target(BYTE *patch_ptr, ULONGLONG dest_addr)
211{
212 typedef enum {
213 OP_JMP = 0xE9,
214 OP_CALL_DWORD = 0xE8
215 } t_opcode;
216
217 if (patch_ptr[0] == OP_JMP || patch_ptr[0] == OP_CALL_DWORD) {
218 ULONGLONG delta = get_jmp_delta(ULONGLONG(patch_ptr), 5, dest_addr);
219 if (!is_valid_delta(delta)) {
220#ifdef _DEBUG
221 std::cout << "Cannot replace the target: too big delta: " << std::hex << delta << std::endl;
222#endif
223 //too big delta, cannot be saved in a DWORD
224 return false;
225 }
226 DWORD delta_dw = DWORD(delta);
227 memcpy(patch_ptr + 1, &delta_dw, sizeof(DWORD));
228
229 //flush cache:
230 FlushInstructionCache(GetCurrentProcess(), patch_ptr + 1, sizeof(DWORD));
231 return true;
232 }
233 return false;
234}
size_t bufferSize
Definition hooks.h:67
BYTE * sourcePtr
Definition hooks.h:69
void deleteBackup()
Definition hooks.h:38
bool makeBackup(BYTE *patch_ptr, size_t patch_size)
Definition hooks.cpp:50
bool isBackup()
Definition hooks.h:60
virtual FARPROC resolve_func(LPCSTR lib_name, LPCSTR func_name)
virtual FARPROC resolve_func(LPCSTR lib_name, LPCSTR func_name)
Definition hooks.cpp:81
long long int get_jmp_delta(ULONGLONG currVA, int instrLen, ULONGLONG destVA)
Definition hooks.cpp:186
bool is_valid_delta(long long int delta)
Definition hooks.cpp:192
Functions related to hooking the loaded PE. Reditecting/replacing a functions with another.
HMODULE get_module_via_peb(IN OPTIONAL LPCWSTR module_name=nullptr)
BOOL nt_protect(LPVOID lpAddress, SIZE_T dwSize, DWORD flNewProtect, PDWORD lpflOldProtect)
Definition hooks.cpp:19
bool validate_ptr(IN const void *buffer_bgn, IN size_t buffer_size, IN const void *field_bgn, IN size_t field_size)
bool is_pointer_in_ntdll(LPVOID lpAddress)
Definition hooks.cpp:9
FARPROC get_exported_func(PVOID modulePtr, LPCSTR wanted_name)
size_t get_module_size_via_peb(IN OPTIONAL HMODULE hModule=nullptr)
bool is_bad_read_ptr(LPCVOID areaStart, SIZE_T areaSize)
Definition util.cpp:150
bool replace_target(BYTE *ptr, ULONGLONG dest_addr)
Definition hooks.cpp:210
size_t redirect_to_local32(void *ptr, DWORD new_offset, PatchBackup *backup=nullptr)
Definition hooks.cpp:142
size_t redirect_to_local64(void *ptr, ULONGLONG new_offset, PatchBackup *backup=nullptr)
Definition hooks.cpp:107
size_t redirect_to_local(void *ptr, void *new_function_ptr, PatchBackup *backup=nullptr)
Definition hooks.cpp:177
Functions for retrieving process information from PEB.
Master include file, including everything else.