How to create a dll in C for apihook?

0

I'm studying APIHook however I run into a problem, I can not create dlls in C, every time I try to create a series of errors in the functions as if they did not exist ... Even taking dlls done and pasting the code does not work, how to create a dll in C for APIHook? Here is the code that I tried, I'm trying to hook the messagebox but from a series of errors

int WINAPI My_MessageBox(HWND, LPCTSTR, LPCTSTR, UINT);

int * addr = (int *)MessageBoxW;
int * myaddr = (int *)My_MessageBox;
PDWORD pAddr = NULL;

unsigned __stdcall ThreadProc(void *param)
{
         // Hook API
         HMODULE hMod = GetModuleHandle(NULL);
         PIMAGE_DOS_HEADER pDosHeader = (PIMAGE_DOS_HEADER)hMod;
         PIMAGE_NT_HEADERS pNTHeaders = (PIMAGE_NT_HEADERS)((BYTE *)hMod + pDosHeader->e_lfanew);
         PIMAGE_OPTIONAL_HEADER pOptHeader = (PIMAGE_OPTIONAL_HEADER)&(pNTHeaders->OptionalHeader);

         PIMAGE_IMPORT_DESCRIPTOR pImportDescriptor = (PIMAGE_IMPORT_DESCRIPTOR)((BYTE *)hMod + pOptHeader->DataDirectory[1].VirtualAddress);

         while(pImportDescriptor->FirstThunk)
         {
               char * dllname = (char *)((BYTE *)hMod + pImportDescriptor->Name);

               PIMAGE_THUNK_DATA pThunkData = (PIMAGE_THUNK_DATA)((BYTE *)hMod + pImportDescriptor->OriginalFirstThunk);

               int no = 1;
               while(pThunkData->u1.Function)
               {
                     char *funname = (char *)((BYTE *)hMod + (DWORD)pThunkData->u1.AddressOfData + 2);
                     PDWORD lpAddr = (DWORD *)((BYTE *)hMod + (DWORD)pImportDescriptor->FirstThunk) + (no-1);

                     if((*lpAddr) == (DWORD)addr)
                     {
                                  DWORD dwOld;
                                  MEMORY_BASIC_INFORMATION mbi;
                                  VirtualQuery(lpAddr, &mbi, sizeof(mbi));
                                  VirtualProtect(lpAddr, sizeof(DWORD), PAGE_READWRITE, &dwOld);
                                  WriteProcessMemory(GetCurrentProcess(), lpAddr, &myaddr, sizeof(DWORD), NULL);
                                  pAddr = lpAddr;
                                  break;
                     }
                     no++;
                     pThunkData++;
               }
               pImportDescriptor++;
         }

         return 0;
}

BOOL APIENTRY DllMain (HINSTANCE hInst     /* Library instance handle. */ ,
                       DWORD reason        /* Reason this function is being called. */ ,
                       LPVOID reserved     /* Not used. */ )
{
                       //PDWORD OrigWriteFile;
                       //PDWORD MyWriteFile;
    switch (reason)
    {
      case DLL_PROCESS_ATTACH:
           _beginthreadex(NULL, 0, ThreadProc, NULL, NULL, NULL);
           MessageBox(NULL, "Testing", "Test", MB_OK);
        break;

      case DLL_PROCESS_DETACH:
        break;

      case DLL_THREAD_ATTACH:
        break;

      case DLL_THREAD_DETACH:
        break;
    }

    /* Returns TRUE on success, FALSE on failure */
    return TRUE;
}

int WINAPI My_MessageBox(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCation, UINT uType)
{
    MessageBox(NULL, "Detoured Messagebox call", "Test", MB_OK);
}

    
asked by anonymous 14.02.2018 / 12:12

0 answers