WeChatFerry/WeChatFerry/sdk/sdk.cpp

86 lines
1.9 KiB
C++
Raw Normal View History

2023-07-15 09:46:40 +08:00
#include "Shlwapi.h"
#include "framework.h"
#include <filesystem>
#include <process.h>
#include <tlhelp32.h>
#include "injector.h"
#include "sdk.h"
#include "util.h"
2024-04-20 07:11:15 +08:00
static BOOL injected = false;
2023-07-15 09:46:40 +08:00
static HANDLE wcProcess = NULL;
static HMODULE spyBase = NULL;
static WCHAR spyDllPath[MAX_PATH] = { 0 };
static int GetDllPath(bool debug, wchar_t *dllPath)
{
2024-04-17 00:35:43 +08:00
GetModuleFileName(GetModuleHandle(WCFSDKDLL), dllPath, MAX_PATH);
2024-04-16 19:42:40 +08:00
PathRemoveFileSpec(dllPath);
2023-07-15 09:46:40 +08:00
if (debug) {
2024-04-17 00:35:43 +08:00
PathAppend(dllPath, WCFSPYDLL_DEBUG);
2023-07-15 09:46:40 +08:00
} else {
2024-04-17 00:35:43 +08:00
PathAppend(dllPath, WCFSPYDLL);
2023-07-15 09:46:40 +08:00
}
2024-04-16 19:42:40 +08:00
if (!PathFileExists(dllPath)) {
MessageBox(NULL, dllPath, L"文件不存在", 0);
2023-07-15 09:46:40 +08:00
return ERROR_FILE_NOT_FOUND;
}
return 0;
}
int WxInitSDK(bool debug, int port)
{
int status = 0;
DWORD wcPid = 0;
status = GetDllPath(debug, spyDllPath);
if (status != 0) {
return status;
}
status = OpenWeChat(&wcPid);
if (status != 0) {
MessageBox(NULL, L"打开微信失败", L"WxInitSDK", 0);
return status;
}
Sleep(2000); // 等待微信打开
wcProcess = InjectDll(wcPid, spyDllPath, &spyBase);
if (wcProcess == NULL) {
MessageBox(NULL, L"注入失败", L"WxInitSDK", 0);
return -1;
}
PortPath_t pp = { 0 };
pp.port = port;
sprintf_s(pp.path, MAX_PATH, "%s", std::filesystem::current_path().string().c_str());
2024-04-17 00:35:43 +08:00
if (!CallDllFuncEx(wcProcess, spyDllPath, spyBase, "InitSpy", (LPVOID)&pp, sizeof(PortPath_t), NULL)) {
MessageBox(NULL, L"初始化失败", L"WxInitSDK", 0);
return -1;
2023-07-15 09:46:40 +08:00
}
2024-04-17 00:35:43 +08:00
2024-04-20 07:11:15 +08:00
injected = true;
2023-07-15 09:46:40 +08:00
return 0;
}
int WxDestroySDK()
{
2024-04-20 07:11:15 +08:00
if (!injected) {
2023-07-15 09:46:40 +08:00
return -1;
}
2024-04-20 07:11:15 +08:00
if (!CallDllFunc(wcProcess, spyDllPath, spyBase, "CleanupSpy", NULL)) {
return -2;
}
2023-07-15 09:46:40 +08:00
if (!EjectDll(wcProcess, spyBase)) {
2024-04-20 07:11:15 +08:00
return -3; // TODO: Unify error codes
2023-07-15 09:46:40 +08:00
}
return 0;
}