CWxObject.cpp 4.4 KB

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