crashinject.cpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. /*
  5. * Given a PID, this program attempts to inject a DLL into the process
  6. * with that PID. The DLL it attempts to inject, "crashinjectdll.dll",
  7. * must exist alongside this exe. The DLL will then crash the process.
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <windows.h>
  13. int main(int argc, char** argv)
  14. {
  15. if (argc != 2) {
  16. fprintf(stderr, "Usage: crashinject <PID>\n");
  17. return 1;
  18. }
  19. int pid = atoi(argv[1]);
  20. if (pid <= 0) {
  21. fprintf(stderr, "Usage: crashinject <PID>\n");
  22. return 1;
  23. }
  24. // find our DLL to inject
  25. wchar_t filename[_MAX_PATH];
  26. if (GetModuleFileNameW(nullptr, filename,
  27. sizeof(filename) / sizeof(wchar_t)) == 0)
  28. return 1;
  29. wchar_t* slash = wcsrchr(filename, L'\\');
  30. if (slash == nullptr)
  31. return 1;
  32. slash++;
  33. wcscpy(slash, L"crashinjectdll.dll");
  34. // now find our target process
  35. HANDLE targetProc = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION,
  36. FALSE,
  37. pid);
  38. if (targetProc == nullptr) {
  39. fprintf(stderr, "Error %d opening target process\n", GetLastError());
  40. return 1;
  41. }
  42. /*
  43. * This is sort of insane, but we're implementing a technique described here:
  44. * http://www.codeproject.com/KB/threads/winspy.aspx#section_2
  45. *
  46. * The gist is to use CreateRemoteThread to create a thread in the other
  47. * process, but cheat and make the thread function kernel32!LoadLibrary,
  48. * so that the only remote data we have to pass to the other process
  49. * is the path to the library we want to load. The library we're loading
  50. * will then do its dirty work inside the other process.
  51. */
  52. HMODULE hKernel32 = GetModuleHandleW(L"Kernel32");
  53. // allocate some memory to hold the path in the remote process
  54. void* pLibRemote = VirtualAllocEx(targetProc, nullptr, sizeof(filename),
  55. MEM_COMMIT, PAGE_READWRITE);
  56. if (pLibRemote == nullptr) {
  57. fprintf(stderr, "Error %d in VirtualAllocEx\n", GetLastError());
  58. CloseHandle(targetProc);
  59. return 1;
  60. }
  61. if (!WriteProcessMemory(targetProc, pLibRemote, (void*)filename,
  62. sizeof(filename), nullptr)) {
  63. fprintf(stderr, "Error %d in WriteProcessMemory\n", GetLastError());
  64. VirtualFreeEx(targetProc, pLibRemote, sizeof(filename), MEM_RELEASE);
  65. CloseHandle(targetProc);
  66. return 1;
  67. }
  68. // Now create a thread in the target process that will load our DLL
  69. HANDLE hThread = CreateRemoteThread(
  70. targetProc, nullptr, 0,
  71. (LPTHREAD_START_ROUTINE)GetProcAddress(hKernel32,
  72. "LoadLibraryW"),
  73. pLibRemote, 0, nullptr);
  74. if (hThread == nullptr) {
  75. fprintf(stderr, "Error %d in CreateRemoteThread\n", GetLastError());
  76. VirtualFreeEx(targetProc, pLibRemote, sizeof(filename), MEM_RELEASE);
  77. CloseHandle(targetProc);
  78. return 1;
  79. }
  80. WaitForSingleObject(hThread, INFINITE);
  81. // Cleanup, not that it's going to matter at this point
  82. CloseHandle(hThread);
  83. VirtualFreeEx(targetProc, pLibRemote, sizeof(filename), MEM_RELEASE);
  84. CloseHandle(targetProc);
  85. return 0;
  86. }