stdafx.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // stdafx.cpp : 只包括标准包含文件的源文件
  2. // WeChats.pch 将作为预编译头
  3. // stdafx.obj 将包含预编译类型信息
  4. #include "stdafx.h"
  5. HANDLE DuplicateHandleEx(DWORD pid, HANDLE h, DWORD flags)
  6. {
  7. HANDLE hHandle = NULL;
  8. HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
  9. if(hProc)
  10. {
  11. if(!DuplicateHandle(hProc,(HANDLE)h, GetCurrentProcess(),&hHandle, 0, FALSE, flags))
  12. {
  13. hHandle = NULL;
  14. }
  15. }
  16. CloseHandle(hProc);
  17. return hHandle;
  18. }
  19. int GetProcIds(LPWSTR Name, DWORD* Pids)
  20. {
  21. PROCESSENTRY32 pe32 = {sizeof(pe32)};
  22. int num = 0;
  23. HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  24. if(hSnap)
  25. {
  26. if(Process32First(hSnap, &pe32))
  27. {
  28. do {
  29. if(!wcsicmp(Name, pe32.szExeFile))
  30. {
  31. if(Pids)
  32. {
  33. Pids[num++] = pe32.th32ProcessID;
  34. }
  35. }
  36. } while(Process32Next(hSnap, &pe32));
  37. }
  38. CloseHandle(hSnap);
  39. }
  40. return num;
  41. }
  42. BOOL IsTargetPid(DWORD Pid, DWORD* Pids, int num)
  43. {
  44. for(int i=0; i<num; i++)
  45. {
  46. if(Pid == Pids[i])
  47. {
  48. return TRUE;
  49. }
  50. }
  51. return FALSE;
  52. }
  53. int PatchWeChat()
  54. {
  55. DWORD dwSize = 0;
  56. POBJECT_NAME_INFORMATION pNameInfo;
  57. POBJECT_NAME_INFORMATION pNameType;
  58. PVOID pbuffer = NULL;
  59. NTSTATUS Status;
  60. int nIndex = 0;
  61. DWORD dwFlags = 0;
  62. char szType[128] = {0};
  63. char szName[512] = {0};
  64. DWORD Pids[100] = {0};
  65. DWORD Num = GetProcIds(L"WeChat.exe", Pids);
  66. if(Num == 0)
  67. {
  68. return 0;
  69. }
  70. if(!ZwQuerySystemInformation)
  71. {
  72. goto Exit0;
  73. }
  74. pbuffer = VirtualAlloc(NULL, 0x1000, MEM_COMMIT, PAGE_READWRITE);
  75. if(!pbuffer)
  76. {
  77. goto Exit0;
  78. }
  79. Status = ZwQuerySystemInformation(SystemHandleInformation, pbuffer, 0x1000, &dwSize);
  80. if(!NT_SUCCESS(Status))
  81. {
  82. if (STATUS_INFO_LENGTH_MISMATCH != Status)
  83. {
  84. goto Exit0;
  85. }
  86. else
  87. {
  88. // 这里大家可以保证程序的正确性使用循环分配稍好
  89. if (NULL != pbuffer)
  90. {
  91. VirtualFree(pbuffer, 0, MEM_RELEASE);
  92. }
  93. if (dwSize*2 > 0x4000000) // MAXSIZE
  94. {
  95. goto Exit0;
  96. }
  97. pbuffer = VirtualAlloc(NULL, dwSize*2, MEM_COMMIT, PAGE_READWRITE);
  98. if(!pbuffer)
  99. {
  100. goto Exit0;
  101. }
  102. Status = ZwQuerySystemInformation(SystemHandleInformation, pbuffer, dwSize*2, NULL);
  103. if(!NT_SUCCESS(Status))
  104. {
  105. goto Exit0;
  106. }
  107. }
  108. }
  109. PSYSTEM_HANDLE_INFORMATION1 pHandleInfo = (PSYSTEM_HANDLE_INFORMATION1)pbuffer;
  110. for(nIndex = 0; nIndex < pHandleInfo->NumberOfHandles; nIndex++)
  111. {
  112. if(IsTargetPid(pHandleInfo->Handles[nIndex].UniqueProcessId, Pids, Num))
  113. {
  114. //
  115. HANDLE hHandle = DuplicateHandleEx(pHandleInfo->Handles[nIndex].UniqueProcessId,
  116. (HANDLE)pHandleInfo->Handles[nIndex].HandleValue,
  117. DUPLICATE_SAME_ACCESS
  118. );
  119. if(hHandle == NULL) continue;
  120. Status = NtQueryObject(hHandle, ObjectNameInformation, szName, 512, &dwFlags);
  121. if (!NT_SUCCESS(Status))
  122. {
  123. CloseHandle(hHandle);
  124. continue;
  125. }
  126. Status = NtQueryObject(hHandle, ObjectTypeInformation, szType, 128, &dwFlags);
  127. if (!NT_SUCCESS(Status))
  128. {
  129. CloseHandle(hHandle);
  130. continue;
  131. }
  132. pNameInfo = (POBJECT_NAME_INFORMATION)szName;
  133. pNameType = (POBJECT_NAME_INFORMATION)szType;
  134. WCHAR TypName[1024] = {0};
  135. WCHAR Name[1024] = {0};
  136. wcsncpy(TypName, (WCHAR*)pNameType->Name.Buffer, pNameType->Name.Length/2);
  137. wcsncpy(Name, (WCHAR*)pNameInfo->Name.Buffer, pNameInfo->Name.Length/2);
  138. // 匹配是否为需要关闭的句柄名称
  139. if (0 == wcscmp(TypName, L"Mutant"))
  140. {
  141. //WeChat_aj5r8jpxt_Instance_Identity_Mutex_Name
  142. //if (wcsstr(Name, L"_WeChat_App_Instance_Identity_Mutex_Name"))
  143. if (wcsstr(Name, L"_WeChat_") &&
  144. wcsstr(Name, L"_Instance_Identity_Mutex_Name"))
  145. {
  146. CloseHandle(hHandle);
  147. hHandle = DuplicateHandleEx(pHandleInfo->Handles[nIndex].UniqueProcessId,
  148. (HANDLE)pHandleInfo->Handles[nIndex].HandleValue,
  149. DUPLICATE_CLOSE_SOURCE
  150. );
  151. if(hHandle)
  152. {
  153. printf("+ Patch wechat success!\n");
  154. CloseHandle(hHandle);
  155. }
  156. else
  157. {
  158. printf("- Patch error: %d\n", GetLastError());
  159. }
  160. goto Exit0;
  161. }
  162. }
  163. CloseHandle(hHandle);
  164. }
  165. }
  166. Exit0:
  167. if (NULL != pbuffer)
  168. {
  169. VirtualFree(pbuffer, 0, MEM_RELEASE);
  170. }
  171. return 0;
  172. }