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
10 {
14 return true; //this address lies within NTDLL
15 }
16 return false;
17 }
18
48};
49
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;
71 return false;
72 }
75
76 //flush cache:
78 return true;
79}
80
82{
83 //the name may be ordinal rather than string, so check if it is a valid pointer:
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
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
103 }
105}
106
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);
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,
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));
134
136
137 //flush cache:
139 return hook64_size;
140}
141
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);
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,
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));
169
171
172 //flush cache:
174 return hook32_size;
175}
176
185
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 }
203 if (delta_dw & 0x80000000) {
204 return true;
205 }
206 //invalid, sign bit is missing
207 return false;
208}
209
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) {
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 }
227 memcpy(patch_ptr + 1, &delta_dw, sizeof(DWORD));
228
229 //flush cache:
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
bool parse_delayed_desc(BYTE *modulePtr, const size_t moduleSize, const ULONGLONG img_base, LPSTR lib_name, const T_FIELD ordinal_flag, IMAGE_DELAYLOAD_DESCRIPTOR *desc, peconv::t_function_resolver *func_resolver)
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.