Advertisement
Guest User

Code injection hook - by namespace

a guest
Dec 5th, 2012
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. /*
  2. *               little sample for hooking LoadLibrary
  3. *               by namespace
  4. */
  5.  
  6. #include <windows.h>
  7. #include <detours.h>
  8.  
  9. #define WIN32_LEAN_AND_MEAN
  10.  
  11. #pragma comment(lib, "detours.lib")
  12.  
  13. typedef HRESULT (WINAPI *oLoadLibrary) (LPCSTR lpFile);
  14.  
  15. oLoadLibrary pLoadLibrary;
  16. DWORD dwLoadLibrary;
  17.  
  18. HRESULT WINAPI myLoadLibrary(LPCSTR lpFile)
  19. {
  20.     if (lpFile == "forbidden.dll")
  21.     {
  22.         SetLastError(0);
  23.  
  24.         return ERROR_SUCCESS;
  25.     }
  26.    
  27.     return pLoadLibrary(lpFile);
  28. }
  29.  
  30. DWORD APIENTRY MainThread(LPVOID lpArgs)
  31. {
  32.     HMODULE hKernel32 = NULL;
  33.  
  34.     do
  35.     {
  36.         hKernel32 = GetModuleHandle("kernel32.dll");
  37.         Sleep(200);
  38.     } while (!hKernel32);
  39.  
  40.     dwLoadLibrary = (DWORD) GetProcAddress(hKernel32, "LoadLibrary");
  41.  
  42.     if (dwLoadLibrary != 0) pLoadLibrary = (oLoadLibrary) DetourFunction((PBYTE) dwLoadLibrary, (PBYTE) myLoadLibrary);
  43.  
  44.     return TRUE;
  45. }
  46.  
  47. BOOL APIENTRY DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved)
  48. {
  49.     if (dwReason == DLL_PROCESS_ATTACH) CreateThread(NULL, NULL, NULL, &MainThread, NULL, NULL);
  50.  
  51.     return TRUE;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement