Enable SDK Reentry

This commit is contained in:
Changhua 2022-08-13 23:33:37 +08:00
parent 30853e5c25
commit ab93568598
8 changed files with 72 additions and 45 deletions

View File

@ -1,9 +1,7 @@
// dllmain.cpp : 定义 DLL 应用程序的入口点。
#include "framework.h"
#include <rpc.h>
#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;
}
}

View File

@ -14,27 +14,28 @@
std::function<int(WxMessage_t)> 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<int(WxMessage_t)> &onMsg)
{
if (onMsg) {

View File

@ -1,5 +1,6 @@
EXPORTS
WxInitSDK
WxInitSDK
WxDestroySDK
WxSetTextMsgCb
WxSendTextMsg
WxGetMsgTypes

View File

@ -37,7 +37,8 @@ typedef map<int, wstring> MsgTypesMap_t;
typedef map<wstring, WxContact_t> ContactMap_t;
typedef vector<WxDbTable_t> DbTableVector_t;
int WxInitSDK();
int WxInitSDK();
int WxDestroySDK();
int WxSetTextMsgCb(const std::function<int(WxMessage_t)> &onMsg);
int WxSendTextMsg(wstring wxid, wstring at_wxid, wstring msg);
int WxSendImageMsg(wstring wxid, wstring path);

View File

@ -2,6 +2,7 @@
#include "framework.h"
#include <string.h>
#include <strsafe.h>
#include <tlhelp32.h>
#include <wchar.h>
#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 };

View File

@ -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);

View File

@ -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;
}

View File

@ -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); }