2

when i inject this dll to program, the program crash when meet function memcpy.

I want to hook function memcpy with detours. Please Help.

#include <windows.h>
#include<iostream>
#include "detours\detours.h"

#pragma comment( lib, "msvcrt.lib" )
#pragma comment( lib, "detours.lib" )
void * (__cdecl *memcpy_o)(void *dest, const void *src, size_t count);


void* __cdecl Mine_Memcpy(void *dest, const void *src, size_t count) {
    char cislo[24]; // just big enough
    void* asd = &memcpy;
    sprintf(cislo,"0x%08x", &asd);

    MessageBoxA(0, cislo, cislo, 0);

    return memcpy_o(dest, src, count);

}


BOOL WINAPI DllMain(HINSTANCE, DWORD dwReason, LPVOID) {

    HANDLE memcpy_get = GetProcAddress(GetModuleHandleA("msvcrt"), "memcpy");
    switch (dwReason) {
    case DLL_PROCESS_ATTACH:
        DetourTransactionBegin();
        DetourUpdateThread(GetCurrentThread());
        memcpy_o = (void * (__cdecl *)(void *dest, const void *src, size_t count))DetourAttach(&(PVOID&)memcpy_get, Mine_Memcpy);
        DetourTransactionCommit();
        break;

    case DLL_PROCESS_DETACH:
        DetourTransactionBegin();
        DetourUpdateThread(GetCurrentThread());
        DetourDetach(&(PVOID &)memcpy_o, Mine_Memcpy);
        DetourTransactionCommit();
        break;
    }

    return TRUE;
}

I inject this dll to program. when the program what to use mecpy it stop working please help, where is the mistake ?

lukas kiss
  • 381
  • 2
  • 15
  • 2
    Why not just edit your original question instead of asking a new one? http://stackoverflow.com/questions/33488648/detours-hook-memcpy-not-working – MrEricSir Nov 03 '15 at 22:35
  • `char cislo[24]; // just big enough` Why is it "just big enough"? What if it is 1 byte off and now you are overwriting memory? Stop being on a knife's edge and just make sure it is big enough by using a value that is sure to be "big enough" `char cislo[50];`. – PaulMcKenzie Nov 03 '15 at 22:41
  • i change it to char cislo[100]; but it still stop working when the program go to hooked memcpy function. – lukas kiss Nov 04 '15 at 16:29
  • when i inject this dll to program and program call memcpy function. The program stop working. – lukas kiss Nov 04 '15 at 16:31
  • What if `MessageBoxA` uses `memcpy`? And the `sprintf` (better to use `snprintf` here) prints the address of `asd` on stack, not the address of `memcpy` as probably desired. And to print a pointer `%p` is usually used. Good luck! – vlp Nov 05 '15 at 21:57
  • Thanks man i will change it and try it – lukas kiss Nov 08 '15 at 16:10
  • I change it, and i aslo delte everithing in fuctoin but it still crash. – lukas kiss Nov 12 '15 at 20:08

0 Answers0