From ab935685987d26f46b27b9ecc67097ac8b8bed6d Mon Sep 17 00:00:00 2001 From: Changhua Date: Sat, 13 Aug 2022 23:33:37 +0800 Subject: [PATCH] Enable SDK Reentry --- SDK/dllmain.cpp | 8 +++---- SDK/sdk.cpp | 29 +++++++++++++++-------- SDK/sdk.def | 3 ++- SDK/sdk.h | 3 ++- SDK/util.cpp | 61 +++++++++++++++++++++++++++++++------------------ SDK/util.h | 3 --- Spy/dllmain.cpp | 4 ++-- Spy/spy.cpp | 6 ++++- 8 files changed, 72 insertions(+), 45 deletions(-) diff --git a/SDK/dllmain.cpp b/SDK/dllmain.cpp index 13cd500..a378c45 100644 --- a/SDK/dllmain.cpp +++ b/SDK/dllmain.cpp @@ -1,9 +1,7 @@ // dllmain.cpp : 定义 DLL 应用程序的入口点。 -#include "framework.h" -#include +#include "framework.h" -extern RPC_STATUS RpcConnectServer(); -extern RPC_STATUS RpcDisconnectServer(); +#include "sdk.h" BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { @@ -13,7 +11,7 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserv case DLL_THREAD_DETACH: break; case DLL_PROCESS_DETACH: { - RpcDisconnectServer(); + WxDestroySDK(); // 默认退出时清理 SDK break; } } diff --git a/SDK/sdk.cpp b/SDK/sdk.cpp index c049144..779f651 100644 --- a/SDK/sdk.cpp +++ b/SDK/sdk.cpp @@ -14,27 +14,28 @@ std::function g_cbReceiveTextMsg; +static DWORD WeChatPID = 0; +static WCHAR SpyDllPath[MAX_PATH] = { 0 }; + int WxInitSDK() { - unsigned long ulCode = 0; - DWORD status = 0; - DWORD pid = 0; - WCHAR DllPath[MAX_PATH] = { 0 }; + DWORD status = 0; + unsigned long ulCode = 0; - GetModuleFileName(GetModuleHandle(WECHATSDKDLL), DllPath, MAX_PATH); - PathRemoveFileSpec(DllPath); - PathAppend(DllPath, WECHATINJECTDLL); + GetModuleFileName(GetModuleHandle(WECHATSDKDLL), SpyDllPath, MAX_PATH); + PathRemoveFileSpec(SpyDllPath); + PathAppend(SpyDllPath, WECHATINJECTDLL); - if (!PathFileExists(DllPath)) { + if (!PathFileExists(SpyDllPath)) { return ERROR_FILE_NOT_FOUND; } - status = OpenWeChat(&pid); + status = OpenWeChat(&WeChatPID); if (status != 0) { return status; } Sleep(2000); // 等待微信打开 - if (!InjectDll(pid, DllPath)) { + if (!InjectDll(WeChatPID, SpyDllPath)) { return -1; } @@ -47,6 +48,14 @@ int WxInitSDK() return ERROR_SUCCESS; } +int WxDestroySDK() +{ + RpcDisconnectServer(); + EnjectDll(WeChatPID, SpyDllPath); + + return ERROR_SUCCESS; +} + int WxSetTextMsgCb(const std::function &onMsg) { if (onMsg) { diff --git a/SDK/sdk.def b/SDK/sdk.def index ed80cec..3456ef4 100644 --- a/SDK/sdk.def +++ b/SDK/sdk.def @@ -1,5 +1,6 @@ EXPORTS - WxInitSDK + WxInitSDK + WxDestroySDK WxSetTextMsgCb WxSendTextMsg WxGetMsgTypes diff --git a/SDK/sdk.h b/SDK/sdk.h index 8385482..7f798ba 100644 --- a/SDK/sdk.h +++ b/SDK/sdk.h @@ -37,7 +37,8 @@ typedef map MsgTypesMap_t; typedef map ContactMap_t; typedef vector DbTableVector_t; -int WxInitSDK(); +int WxInitSDK(); +int WxDestroySDK(); int WxSetTextMsgCb(const std::function &onMsg); int WxSendTextMsg(wstring wxid, wstring at_wxid, wstring msg); int WxSendImageMsg(wstring wxid, wstring path); diff --git a/SDK/util.cpp b/SDK/util.cpp index 5633e31..9e57a04 100644 --- a/SDK/util.cpp +++ b/SDK/util.cpp @@ -2,6 +2,7 @@ #include "framework.h" #include #include +#include #include #include "util.h" @@ -11,12 +12,7 @@ using namespace std; -int GetWeChatPath(wchar_t *path); -int GetWeChatWinDLLPath(wchar_t *path); -int GetWeChatVersion(wchar_t *version); -bool GetFileVersion(const wchar_t *filePath, wchar_t *version); - -int GetWeChatPath(wchar_t *path) +static int GetWeChatPath(wchar_t *path) { int ret = -1; HKEY hKey = NULL; @@ -45,7 +41,7 @@ __exit: return ERROR_SUCCESS; } -int GetWeChatWinDLLPath(wchar_t *path) +static int GetWeChatWinDLLPath(wchar_t *path) { int ret = GetWeChatPath(path); if (ret != ERROR_SUCCESS) { @@ -71,21 +67,7 @@ int GetWeChatWinDLLPath(wchar_t *path) return ret; } -int GetWeChatVersion(wchar_t *version) -{ - WCHAR Path[MAX_PATH] = { 0 }; - - int ret = GetWeChatWinDLLPath(Path); - if (ret != ERROR_SUCCESS) { - return ret; - } - - ret = GetFileVersion(Path, version); - - return ret; -} - -bool GetFileVersion(const wchar_t *filePath, wchar_t *version) +static bool GetFileVersion(const wchar_t *filePath, wchar_t *version) { if (wcslen(filePath) > 0 && PathFileExists(filePath)) { VS_FIXEDFILEINFO *pVerInfo = NULL; @@ -129,8 +111,43 @@ bool GetFileVersion(const wchar_t *filePath, wchar_t *version) return false; } +int GetWeChatVersion(wchar_t *version) +{ + WCHAR Path[MAX_PATH] = { 0 }; + + int ret = GetWeChatWinDLLPath(Path); + if (ret != ERROR_SUCCESS) { + return ret; + } + + ret = GetFileVersion(Path, version); + + return ret; +} + +static DWORD GetWeChatPid() +{ + DWORD pid = 0; + HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); + PROCESSENTRY32 pe32 = { sizeof(PROCESSENTRY32) }; + while (Process32Next(hSnapshot, &pe32)) { + wstring strProcess = pe32.szExeFile; + if (strProcess == WECHAREXE) { + pid = pe32.th32ProcessID; + break; + } + } + CloseHandle(hSnapshot); + return pid; +} + int OpenWeChat(DWORD *pid) { + *pid = GetWeChatPid(); + if (*pid) { + return ERROR_SUCCESS; + } + int ret = -1; STARTUPINFO si = { sizeof(si) }; WCHAR Path[MAX_PATH] = { 0 }; diff --git a/SDK/util.h b/SDK/util.h index 33e44e7..b8c4ba9 100644 --- a/SDK/util.h +++ b/SDK/util.h @@ -15,10 +15,7 @@ #define GET_WSTRING(addr) ((WCHAR *)(*(DWORD *)(addr))) int OpenWeChat(DWORD *pid); -int GetWeChatPath(wchar_t *path); -int GetWeChatWinDLLPath(wchar_t *path); int GetWeChatVersion(wchar_t *version); -bool GetFileVersion(const wchar_t *filePath, wchar_t *version); int GetWstringByAddress(DWORD address, wchar_t *buffer, DWORD buffer_size); BSTR GetBstrByAddress(DWORD address); void GetRpcMessage(WxMessage_t *wxMsg, RpcMessage_t rpcMsg); diff --git a/Spy/dllmain.cpp b/Spy/dllmain.cpp index 8af8120..b1fce62 100644 --- a/Spy/dllmain.cpp +++ b/Spy/dllmain.cpp @@ -6,7 +6,7 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserv { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: { - // MessageBox(NULL, L"InitSpy", L"DllMain", 0); + //MessageBox(NULL, L"InitSpy", L"DllMain", 0); InitSpy(hModule); break; } @@ -14,7 +14,7 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserv case DLL_THREAD_DETACH: break; case DLL_PROCESS_DETACH: { - // MessageBox(NULL, L"DestroySpy", L"DllMain", 0); + //MessageBox(NULL, L"DestroySpy", L"DllMain", 0); DestroySpy(); break; } diff --git a/Spy/spy.cpp b/Spy/spy.cpp index 21a73a9..a331cf2 100644 --- a/Spy/spy.cpp +++ b/Spy/spy.cpp @@ -35,6 +35,10 @@ void InitSpy(HMODULE hModule) } } -void DestroySpy() { RpcStopServer(); } +void DestroySpy() +{ + RpcStopServer(); + FreeLibrary((HMODULE)g_WeChatWinDllAddr); +} int IsLogin(void) { return (int)GET_DWORD(g_WeChatWinDllAddr + g_WxCalls.login); }