INJLIB.CPP
上传用户:nbcables
上传日期:2007-01-11
资源大小:1243k
文件大小:4k
源码类别:

钩子与API截获

开发平台:

Visual C++

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include "ProcessModule.h"
  4. #include "util.h"
  5. #ifdef WINNT
  6. int WINAPI InjectLib(DWORD process_id, char *lib_name)
  7. {
  8. PTHREAD_START_ROUTINE pfnRemote =(PTHREAD_START_ROUTINE)
  9. GetProcAddress(GetModuleHandle("Kernel32"), "LoadLibraryA");
  10. if(pfnRemote ==NULL)
  11. return -1;
  12. HANDLE hProcess =OpenProcess(PROCESS_ALL_ACCESS, FALSE, process_id);
  13. if(hProcess ==NULL)
  14. {
  15. WriteLog("InjectLib:OpenProcess %d failed!", process_id);
  16. return -1;
  17. }
  18. int mem_size =strlen(lib_name)+1;
  19. void *premote_mem =VirtualAllocEx(hProcess, NULL, mem_size, MEM_COMMIT, PAGE_READWRITE);
  20. if(premote_mem ==NULL)
  21. {
  22. CloseHandle(hProcess);
  23. return -1;
  24. }
  25. //if(hThread) SuspendThread(hThread);
  26. int ret =WriteProcessMemory(hProcess, premote_mem, lib_name, mem_size,NULL);
  27. if(ret ==STATUS_ACCESS_VIOLATION || ret ==false)
  28. {
  29. //if(hThread) ResumeThread(hThread);
  30. VirtualFreeEx(hProcess, premote_mem, 0, MEM_RELEASE);
  31. CloseHandle(hProcess); 
  32. return -1;
  33. }
  34. //if(hThread) ResumeThread(hThread);
  35. HANDLE hThread =CreateRemoteThread(hProcess, NULL, 0, 
  36. pfnRemote, premote_mem, 0, NULL);
  37. if(hThread ==NULL)
  38. {
  39. VirtualFreeEx(hProcess, premote_mem, 0, MEM_RELEASE);
  40. CloseHandle(hProcess);
  41. return -1;
  42. }
  43. //WriteLog2("c:\hookapi_debug.log", "CreateRemoteThread ok");
  44. WaitForSingleObject(hThread, INFINITE);
  45. VirtualFreeEx(hProcess, premote_mem, 0, MEM_RELEASE);
  46. CloseHandle(hProcess);
  47. CloseHandle(hThread);
  48. return 0;
  49. }
  50. int WINAPI InjectLib(HANDLE hProcess, char *lib_name)
  51. {
  52. PTHREAD_START_ROUTINE pfnRemote =(PTHREAD_START_ROUTINE)
  53. GetProcAddress(GetModuleHandle("Kernel32"), "LoadLibraryA");
  54. if(pfnRemote ==NULL)
  55. return -1;
  56. if(hProcess ==NULL)
  57. {
  58. WriteLog("InjectLib:OpenProcess %d failed!", hProcess);
  59. return -1;
  60. }
  61. int mem_size =strlen(lib_name)+1;
  62. void *premote_mem =VirtualAllocEx(hProcess, NULL, mem_size, MEM_COMMIT, PAGE_READWRITE);
  63. if(premote_mem ==NULL)
  64. {
  65. CloseHandle(hProcess);
  66. return -1;
  67. }
  68. //if(hThread) SuspendThread(hThread);
  69. int ret =WriteProcessMemory(hProcess, premote_mem, lib_name, mem_size,NULL);
  70. if(ret ==STATUS_ACCESS_VIOLATION || ret ==false)
  71. {
  72. //if(hThread) ResumeThread(hThread);
  73. VirtualFreeEx(hProcess, premote_mem, 0, MEM_RELEASE);
  74. return -1;
  75. }
  76. //if(hThread) ResumeThread(hThread);
  77. HANDLE hThread =CreateRemoteThread(hProcess, NULL, 0, 
  78. pfnRemote, premote_mem, 0, NULL);
  79. if(hThread ==NULL)
  80. {
  81. VirtualFreeEx(hProcess, premote_mem, 0, MEM_RELEASE);
  82. return -1;
  83. }
  84. //WriteLog2("c:\hookapi_debug.log", "CreateRemoteThread ok");
  85. WaitForSingleObject(hThread, INFINITE);
  86. VirtualFreeEx(hProcess, premote_mem, 0, MEM_RELEASE);
  87. CloseHandle(hThread);
  88. return 0;
  89. }
  90. int WINAPI EjectLib(DWORD process_id, char *lib_name)
  91. {
  92. PTHREAD_START_ROUTINE pfnRemote =(PTHREAD_START_ROUTINE)
  93. GetProcAddress(GetModuleHandle("Kernel32"), "FreeLibrary");
  94. if(pfnRemote ==NULL)
  95. {
  96. WriteLog("EjectLib:GetProcAddress FreeLibrary failed!");
  97. return -1;
  98. }
  99. HANDLE hProcess =OpenProcess(PROCESS_ALL_ACCESS, FALSE, process_id);
  100. if(hProcess ==NULL)
  101. {
  102. WriteLog("EjectLib:OpenProcess %d failed!", process_id);
  103. return -1;
  104. }
  105. CProcessModule pm;
  106. HANDLE hmod =pm.GetProcessModuleHandle(process_id, lib_name);
  107. if(hmod ==NULL)
  108. {
  109. CloseHandle(hProcess);
  110. //WriteLog("EjectLib(), not found lib:%s", lib_name);
  111. return 0;
  112. }
  113. //WriteLog("EjectLib(), found lib:%s, mod handle:0x%x", lib_name, hmod);
  114. HANDLE hThread =CreateRemoteThread(hProcess, NULL, 0, 
  115. pfnRemote, hmod, 0, NULL);
  116. if(hThread ==NULL)
  117. {
  118. WriteLog("EjectLib():CreateRemoteThread failed! process_id:%d", process_id);
  119. CloseHandle(hProcess);
  120. return -1;
  121. }
  122. WaitForSingleObject(hThread, INFINITE);
  123. CloseHandle(hProcess);
  124. CloseHandle(hThread);
  125. return 0;
  126. }
  127. #endif