19 BOOL
nt_protect(LPVOID lpAddress, SIZE_T dwSize, DWORD flNewProtect, PDWORD lpflOldProtect)
23 "NtProtectVirtualMemory"
28 NTSTATUS(NTAPI *_NtProtectVirtualMemory)(
41 SIZE_T protect_size = dwSize;
42 NTSTATUS status = _NtProtectVirtualMemory(GetCurrentProcess(), &lpAddress, &protect_size, flNewProtect, lpflOldProtect);
57 this->
buffer =
new BYTE[patch_size];
60 memcpy(
buffer, patch_ptr, patch_size);
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;
89 std::cout <<
">>>>>>Replacing: " << func_name <<
" by: " << hook << std::endl;
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;
100 std::cout <<
">>>>>>Replacing DLL: " << lib_name_str <<
" by: " << name << std::endl;
112 0x48, 0xB8, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xEE, 0xFF,
115 const size_t hook64_size =
sizeof(hook_64);
117 std::cout <<
"[WARNING] Patching NTDLL is not allowed because of possible stability issues!\n";
120 DWORD oldProtect = 0;
123 PAGE_EXECUTE_READWRITE,
129 if (backup !=
nullptr) {
132 memcpy(hook_64 + 2, &new_offset,
sizeof(ULONGLONG));
133 memcpy(ptr, hook_64, hook64_size);
135 nt_protect((LPVOID)ptr, hook64_size, oldProtect, &oldProtect);
138 FlushInstructionCache(GetCurrentProcess(), ptr, hook64_size);
147 0xB8, 0xCC, 0xDD, 0xEE, 0xFF,
150 const size_t hook32_size =
sizeof(hook_32);
152 std::cout <<
"[WARNING] Patching NTDLL is not allowed because of possible stability issues!\n";
155 DWORD oldProtect = 0;
158 PAGE_EXECUTE_READWRITE,
164 if (backup !=
nullptr) {
167 memcpy(hook_32 + 1, &new_offset,
sizeof(DWORD));
168 memcpy(ptr, hook_32, hook32_size);
170 nt_protect((LPVOID)ptr, hook32_size, oldProtect, &oldProtect);
173 FlushInstructionCache(GetCurrentProcess(), ptr, hook32_size);
186inline long long int get_jmp_delta(ULONGLONG currVA,
int instrLen, ULONGLONG destVA)
188 long long int diff = destVA - (currVA + instrLen);
194 DWORD first_dw = delta >>
sizeof(DWORD) * 8;
198 const DWORD max_dword = DWORD(-1);
199 if (first_dw != max_dword) {
202 DWORD delta_dw = DWORD(delta);
203 if (delta_dw & 0x80000000) {
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);
221 std::cout <<
"Cannot replace the target: too big delta: " << std::hex << delta << std::endl;
226 DWORD delta_dw = DWORD(delta);
227 memcpy(patch_ptr + 1, &delta_dw,
sizeof(DWORD));
230 FlushInstructionCache(GetCurrentProcess(), patch_ptr + 1,
sizeof(DWORD));
bool makeBackup(BYTE *patch_ptr, size_t patch_size)
virtual FARPROC resolve_func(LPCSTR lib_name, LPCSTR func_name)
virtual FARPROC resolve_func(LPCSTR lib_name, LPCSTR func_name)
long long int get_jmp_delta(ULONGLONG currVA, int instrLen, ULONGLONG destVA)
bool is_valid_delta(long long int delta)
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)
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)
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)
bool replace_target(BYTE *ptr, ULONGLONG dest_addr)
size_t redirect_to_local32(void *ptr, DWORD new_offset, PatchBackup *backup=nullptr)
size_t redirect_to_local64(void *ptr, ULONGLONG new_offset, PatchBackup *backup=nullptr)
size_t redirect_to_local(void *ptr, void *new_function_ptr, PatchBackup *backup=nullptr)
Functions for retrieving process information from PEB.
Master include file, including everything else.