Injection.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include "StdAfx.h"
  2. #include "Injection.h"
  3. CInjection::CInjection()
  4. {
  5. }
  6. CInjection::CInjection(DWORD dwPid, LPCTSTR lpDynamicLibraryPath)
  7. :m_dwInjectPID(dwPid),
  8. m_hInjectProcess(NULL),
  9. m_lpInjectData(NULL),
  10. m_lpEjectData(NULL),
  11. m_hInjectThread(NULL),
  12. m_hEjectThread(NULL),
  13. m_dwPathLen(0)
  14. {
  15. ASSERT(dwPid!=0);
  16. ASSERT(lpDynamicLibraryPath!=NULL);
  17. memset(m_szDllPath, 0, sizeof(m_szDllPath));
  18. _tcscpy_s(m_szDllPath,lpDynamicLibraryPath);
  19. m_hInjectProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, m_dwInjectPID);
  20. if ( m_hInjectProcess == NULL)
  21. {
  22. WriteTextLog(_T("打开WeChat.exe进程失败"));
  23. }
  24. //m_hInjectProcess = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_VM_OPERATION | PROCESS_VM_WRITE, FALSE, m_dwInjectPID);
  25. }
  26. CInjection::~CInjection(void)
  27. {
  28. // 卸载dll;
  29. EjectDynamicLibrary();
  30. // 释放所有资源;
  31. if (m_hInjectThread)
  32. CloseHandle(m_hInjectThread);
  33. m_hInjectThread = NULL;
  34. if (m_hEjectThread)
  35. CloseHandle(m_hEjectThread);
  36. m_hEjectThread = NULL;
  37. if (m_lpInjectData)
  38. VirtualFreeEx(m_hInjectProcess, m_lpInjectData, m_dwPathLen, MEM_RELEASE);
  39. m_lpInjectData = NULL;
  40. if (m_lpEjectData)
  41. VirtualFreeEx(m_hInjectProcess, m_lpEjectData, m_dwPathLen, MEM_RELEASE);
  42. m_lpEjectData = NULL;
  43. if (m_hInjectProcess)
  44. CloseHandle(m_hInjectProcess);
  45. m_hInjectProcess = NULL;
  46. }
  47. void CInjection::setInjectionObj(DWORD dwPid, LPCTSTR lpDynamicLibraryPath)
  48. {
  49. ASSERT(dwPid != 0);
  50. ASSERT(lpDynamicLibraryPath != NULL);
  51. m_dwInjectPID = dwPid;
  52. memset(m_szDllPath, 0, sizeof(m_szDllPath));
  53. _tcscpy_s(m_szDllPath, lpDynamicLibraryPath);
  54. m_hInjectProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, m_dwInjectPID);
  55. if (m_hInjectProcess == NULL)
  56. {
  57. WriteTextLog(_T("打开WeChat.exe进程失败"));
  58. }
  59. //m_hInjectProcess = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_VM_OPERATION | PROCESS_VM_WRITE, FALSE, m_dwInjectPID);
  60. }
  61. BOOL CInjection::InjectDynamicLibrary()
  62. {
  63. ASSERT(m_hInjectProcess!=NULL);
  64. m_dwPathLen = _tcslen(m_szDllPath)*sizeof(TCHAR)+1;
  65. m_lpInjectData = VirtualAllocEx(m_hInjectProcess,NULL, m_dwPathLen, MEM_COMMIT, PAGE_READWRITE);
  66. if (NULL == m_lpInjectData)
  67. {
  68. WriteTextLog(_T("创建WeChat.exe进程虚拟内存失败"));
  69. return FALSE;
  70. }
  71. if (WriteProcessMemory(m_hInjectProcess, m_lpInjectData, m_szDllPath, m_dwPathLen, NULL) == 0)
  72. {
  73. // 注意:MEM_RELEASE释放时第三参数一定要为0,请查看MSDN;
  74. VirtualFreeEx(m_hInjectProcess, m_lpInjectData, 0, MEM_RELEASE);
  75. return FALSE;
  76. }
  77. HMODULE hk32 = GetModuleHandle(_T("kernel32.dll"));
  78. // 注意:微信使用的是W版本;
  79. LPVOID lpAddr = GetProcAddress(hk32,"LoadLibraryW");
  80. m_hInjectThread = CreateRemoteThread(m_hInjectProcess, NULL, 0, (LPTHREAD_START_ROUTINE)lpAddr, m_lpInjectData, 0, NULL);
  81. if (NULL == m_hInjectThread)
  82. {
  83. // 注意:MEM_RELEASE释放时第三参数一定要为0,请查看MSDN;
  84. VirtualFreeEx(m_hInjectProcess, m_lpInjectData, 0, MEM_RELEASE);
  85. return FALSE;
  86. }
  87. WaitForSingleObject(m_hInjectThread, INFINITE);
  88. if (m_hInjectThread)
  89. CloseHandle(m_hInjectThread);
  90. m_hInjectThread = NULL;
  91. /* 注入成功后,不能释放内存否则微信会挂;
  92. if (m_lpInjectData != NULL)
  93. VirtualFreeEx(m_hInjectProcess, m_lpInjectData, 0, MEM_RELEASE);
  94. */
  95. return TRUE;
  96. }
  97. BOOL CInjection::EjectDynamicLibrary()
  98. {
  99. if(m_hInjectProcess==NULL)
  100. return TRUE;
  101. // 获取模块句柄;
  102. HANDLE hModule = FindModuleEx(m_szDllPath, m_dwInjectPID);
  103. if (hModule == NULL )
  104. {
  105. WriteTextLog(_T("获取WeChat.exe进程模块hook.dll失败"));
  106. return FALSE;
  107. }
  108. LPVOID lpAddr = GetProcAddress(GetModuleHandle(_T("kernel32.dll")), "FreeLibraryAndExitThread");//FreeLibraryAndExitThread//FreeLibrary
  109. if (lpAddr == NULL )
  110. {
  111. WriteTextLog(_T("获取kernel32.dll中的FreeLibraryAndExitThread失败"));
  112. return FALSE;
  113. }
  114. m_hEjectThread = CreateRemoteThread(m_hInjectProcess, NULL, 0, (LPTHREAD_START_ROUTINE)lpAddr, hModule, 0, NULL);
  115. if ( m_hEjectThread == NULL )
  116. {
  117. WriteTextLog(_T("创建WeChat.exe远程线程(FreeLibraryAndExitThread)失败"));
  118. return FALSE;
  119. }
  120. WaitForSingleObject(m_hEjectThread, INFINITE);
  121. if (m_hEjectThread)
  122. CloseHandle(m_hEjectThread);
  123. m_hEjectThread = NULL;
  124. return TRUE;
  125. }