Advertisement
yorath

DLL injection

Sep 23rd, 2012
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.71 KB | None | 0 0
  1. //
  2. // DllTest.dll
  3. //
  4. #include <windows.h>
  5. #include <stdio.h>
  6.  
  7. #define DllExport extern "C" __declspec(dllexport)
  8.  
  9. void printMsg()
  10. {
  11.     FILE *fptr = NULL;
  12.     errno_t err = fopen_s(&fptr, "skelog.txt", "a+");
  13.     if (!err)
  14.     {
  15.         fprintf(fptr, "Process ID=(%d) loading Dll\n", GetCurrentProcessId());
  16.         fclose(fptr);
  17.     }
  18.  
  19. }
  20.  
  21. DllExport BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
  22. {
  23.     switch (fdwReason)
  24.     {
  25.     case DLL_PROCESS_ATTACH:
  26.         // printMsg();
  27.         MessageBox(NULL, TEXT("DLL Injection"), TEXT("Warning"), MB_OK);
  28.         break;
  29.     default:
  30.         break;
  31.     }
  32.     return TRUE;
  33. }
  34.  
  35. DllExport LRESULT CALLBACK MouseProc(int code, WPARAM wParam, LPARAM lParam)
  36. {
  37.     printMsg();
  38.     return CallNextHookEx(NULL, code, wParam, lParam);
  39. }
  40.  
  41. //
  42. // Test.exe
  43. //
  44. #include <windows.h>
  45.  
  46. int main()
  47. {
  48.     DWORD dwProcID = 7164;
  49.     WCHAR argumentBuffer[] = L"D:\\Documents\\Visual Studio 2012\\Projects\\Test\\Debug\\DllTest.dll";
  50.  
  51.     HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcID);
  52.     if (!hProc)
  53.         return -1;
  54.  
  55.     HMODULE hDll = GetModuleHandle(TEXT("Kernel32"));
  56.     if (!hDll)
  57.         return -1;
  58.    
  59.     LPTHREAD_START_ROUTINE loadLibraryAddress = (LPTHREAD_START_ROUTINE)GetProcAddress(hDll, "LoadLibraryW");
  60.     if (!loadLibraryAddress)
  61.         return -1;
  62.  
  63.     LPVOID lpBaseAddress = VirtualAllocEx(hProc, NULL, 256, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
  64.     if (!lpBaseAddress)
  65.         return -1;
  66.  
  67.     BOOL isValid = WriteProcessMemory(hProc, lpBaseAddress, argumentBuffer, sizeof(argumentBuffer), NULL);
  68.     if (!isValid)
  69.         return -1;
  70.  
  71.     HANDLE hThread = CreateRemoteThread(hProc, NULL, 0, loadLibraryAddress, lpBaseAddress, 0, NULL);
  72.     if (!hThread)
  73.     {
  74.         DWORD error = GetLastError();
  75.         return -1;
  76.     }
  77.  
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement