DaVilka Опубликовано 5 апреля, 2016 Поделиться Опубликовано 5 апреля, 2016 По видео кодера я сделал длл, которая хукает fункцию, все вроде бы хукается, но при возвращении управления клиент критует, ошибок я не заметил, прошу помощи, компилятор visual studio 12 Скрытый текст #include <Windows.h> #define BEA_ENGINE_STATIC #define BEA_USE_STDCALL #include "BeaEngine\BeaEngine.h" #pragma comment (lib, "BeaEngine\\BeaEngine.lib") class HookHelper { public: inline static unsigned int CalculateJump_X86(unsigned char* from, unsigned char* to) { return 0 -(from - to) - 5;} inline static void ChangeProtection(void* ptr, size_t size, DWORD protection) { VirtualProtect(ptr, size, protection, &protection); } inline static void* Alloc(size_t size, DWORD protection) { return VirtualAlloc(0, size, MEM_RESERVE | MEM_COMMIT, protection); } static void HookFunction(unsigned char* origFuncPtr, unsigned char* hookFuncPtr); static void HookFunctionWithTrampoline(unsigned char* origFuncPtr, unsigned char* hookFuncPtr, unsigned char*& trampoline); }; void HookHelper::HookFunction(unsigned char* origFuncPtr, unsigned char* hookFuncPtr) { DISASM disasm = {0}; disasm.EIP = reinterpret_cast<size_t> (origFuncPtr); unsigned char totalLen = 0; unsigned char jmp_bytes[5] = {0xE9}; signed char delta = 0; while (totalLen < 5) { unsigned char currentLen = Disasm(&disasm); totalLen += currentLen; disasm.EIP += currentLen; } delta = totalLen - 5; (*(unsigned int*)(jmp_bytes + 1)) = HookHelper::CalculateJump_X86(origFuncPtr, hookFuncPtr); DWORD protection = PAGE_EXECUTE_READWRITE; HookHelper::ChangeProtection(origFuncPtr, totalLen + delta, protection); memcpy(origFuncPtr, jmp_bytes, 5); if (delta) memset(origFuncPtr, 0x90, delta); HookHelper::ChangeProtection(origFuncPtr, totalLen + delta, protection); } void HookHelper::HookFunctionWithTrampoline(unsigned char* origFuncPtr, unsigned char* hookFuncPtr, unsigned char*& trampoline) { DISASM disasm = {0}; disasm.EIP = reinterpret_cast<size_t> (origFuncPtr); unsigned char totalLen = 0; unsigned char jmp_bytes[5] = {0xE9}; signed char delta = 0; while (totalLen < 5) { unsigned char currentLen = Disasm(&disasm); totalLen += currentLen; disasm.EIP += currentLen; } delta = totalLen - 5; trampoline = reinterpret_cast<unsigned char*> (HookHelper::Alloc(totalLen + 5, PAGE_EXECUTE_READWRITE)); (*(unsigned int*)(jmp_bytes + 1)) = HookHelper::CalculateJump_X86(trampoline + totalLen, origFuncPtr + 5 + delta); memcpy(trampoline, origFuncPtr, totalLen); memcpy(trampoline + totalLen, jmp_bytes, 5); (*(unsigned int*)(jmp_bytes + 1)) = HookHelper::CalculateJump_X86(origFuncPtr, hookFuncPtr); DWORD protection = PAGE_EXECUTE_READWRITE; HookHelper::ChangeProtection(origFuncPtr, totalLen + delta, protection); memcpy(origFuncPtr, jmp_bytes, 5); if (delta) memset(origFuncPtr + 5, 0x90, delta); HookHelper::ChangeProtection(origFuncPtr, totalLen + delta, protection); } Скрытый текст #include <iostream> #include "HookHelper.h" #include <Windows.h> typedef int (__stdcall * MsgBox) (int,int *,unsigned long,unsigned long,unsigned long,unsigned long); MsgBox MsgBoxA = nullptr; unsigned char* trampoline = nullptr; int __stdcall MessageBoxA_Hook2(int id0, int * id1, unsigned long id2, unsigned long id3, unsigned long id4, unsigned long id5) { MessageBoxW(NULL, L"≈сть контакт", L"Good", MB_OK | MB_ICONINFORMATION); return ((MsgBox)trampoline)(id0, id1, id2, id3, id4, id5); //return 0; } DWORD WINAPI hook_thead (LPVOID) { MsgBoxA = reinterpret_cast<MsgBox>( GetProcAddress(GetModuleHandleA("engine.dll"), "?RequestEnterWorldPacket@UNetworkHandler@@UAEHHPAHKKKK@Z")); HookHelper::HookFunctionWithTrampoline(reinterpret_cast<unsigned char*> (MsgBoxA), reinterpret_cast<unsigned char*> (&MessageBoxA_Hook2), trampoline); return 0; } DWORD APIENTRY DllMain (HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: CreateThread (NULL, NULL, hook_thead, NULL, NULL, NULL); case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return true; } Ссылка на комментарий Поделиться на другие сайты Поделиться
partoftheworlD Опубликовано 6 апреля, 2016 Поделиться Опубликовано 6 апреля, 2016 (изменено) unsigned char* trampoline = nullptr; Попробуй закоментить эту строку , а вообще лучше отладчиком пройдись, чтобы найти проблему. По коду trampoline обнуляется в main и используешь уже 0, вместо расчета обратного прыжка trampoline = reinterpret_cast<unsigned char*> (HookHelper::Alloc(totalLen + 5, PAGE_EXECUTE_READWRITE)); и скорее всего не там восстанавливается функция. Изменено 6 апреля, 2016 пользователем partoftheworlD Ссылка на комментарий Поделиться на другие сайты Поделиться
DaVilka Опубликовано 8 апреля, 2016 Автор Поделиться Опубликовано 8 апреля, 2016 В 06.04.2016в22:16, partoftheworlD сказал: unsigned char* trampoline = nullptr; Попробуй закоментить эту строку , а вообще лучше отладчиком пройдись, чтобы найти проблему. По коду trampoline обнуляется в main и используешь уже 0, вместо расчета обратного прыжка trampoline = reinterpret_cast<unsigned char*> (HookHelper::Alloc(totalLen + 5, PAGE_EXECUTE_READWRITE)); и скорее всего не там восстанавливается функция. а разве здесь unsigned char* trampoline = nullptr; она не просто объявляется? а здесь return ((MsgBox)trampoline)(id0, id1, id2, id3, id4, id5); не присваивается значение, а здесь HookHelper::HookFunctionWithTrampoline(reinterpret_cast<unsigned char*> (MsgBoxA), reinterpret_cast<unsigned char*> (&MessageBoxA_Hook2), trampoline); уже присвоенное значение передается передается параметром в HookHelper::HookFunctionWithTrampoline третьим параметром ? если закомментировать unsigned char* trampoline = nullptr; то trampoline не определен Оо Ссылка на комментарий Поделиться на другие сайты Поделиться
Рекомендуемые сообщения