WeChatFerry/sdk/sdk.cpp

126 lines
3.2 KiB
C++
Raw Normal View History

2023-02-25 01:53:02 +08:00
#include "Shlwapi.h"
2021-02-12 23:21:57 +08:00
#include "framework.h"
2023-05-21 20:29:21 +08:00
#include <filesystem>
2021-02-12 23:21:57 +08:00
#include <process.h>
#include <tlhelp32.h>
#include "injector.h"
#include "sdk.h"
#include "util.h"
2023-02-25 02:11:04 +08:00
#define WCF_LOCK ".wcf.lock"
static bool debugMode = false;
2022-10-16 22:14:06 +08:00
static HANDLE wcProcess = NULL;
static HMODULE spyBase = NULL;
2022-10-15 20:25:42 +08:00
static WCHAR spyDllPath[MAX_PATH] = { 0 };
2022-08-13 23:33:37 +08:00
2023-02-25 02:11:04 +08:00
static int GetDllPath(bool debug, wchar_t *dllPath)
2021-02-12 23:21:57 +08:00
{
2022-10-15 20:25:42 +08:00
GetModuleFileName(GetModuleHandle(WECHATSDKDLL), spyDllPath, MAX_PATH);
PathRemoveFileSpec(spyDllPath);
2023-02-25 01:53:02 +08:00
if (debug) {
PathAppend(spyDllPath, WECHATINJECTDLL_DEBUG);
} else {
PathAppend(spyDllPath, WECHATINJECTDLL);
}
2022-10-15 20:25:42 +08:00
if (!PathFileExists(spyDllPath)) {
2023-05-21 20:29:21 +08:00
MessageBox(NULL, spyDllPath, L"文件不存在", 0);
2021-02-12 23:21:57 +08:00
return ERROR_FILE_NOT_FOUND;
}
2023-02-25 02:11:04 +08:00
return 0;
}
2023-04-08 22:52:35 +08:00
int WxInitSDK(bool debug, int port)
2023-02-25 02:11:04 +08:00
{
int status = 0;
DWORD wcPid = 0;
status = GetDllPath(debug, spyDllPath);
if (status != 0) {
return status;
}
2022-10-15 20:25:42 +08:00
status = OpenWeChat(&wcPid);
2021-02-12 23:21:57 +08:00
if (status != 0) {
2023-05-21 20:29:21 +08:00
MessageBox(NULL, L"打开微信失败", L"WxInitSDK", 0);
2021-02-12 23:21:57 +08:00
return status;
}
2022-08-20 15:15:04 +08:00
2022-08-06 16:49:37 +08:00
Sleep(2000); // 等待微信打开
2022-10-15 20:25:42 +08:00
wcProcess = InjectDll(wcPid, spyDllPath, &spyBase);
if (wcProcess == NULL) {
2023-05-21 20:29:21 +08:00
MessageBox(NULL, L"注入失败", L"WxInitSDK", 0);
2021-02-12 23:21:57 +08:00
return -1;
}
2023-05-21 20:29:21 +08:00
PortPath_t pp = { 0 };
pp.port = port;
sprintf_s(pp.path, MAX_PATH, "%s", std::filesystem::current_path().string().c_str());
if (!CallDllFuncEx(wcProcess, spyDllPath, spyBase, "InitSpy", (LPVOID)&pp, sizeof(PortPath_t), NULL)) {
MessageBox(NULL, L"初始化失败", L"WxInitSDK", 0);
2022-08-20 15:15:04 +08:00
return -1;
}
2023-02-25 02:11:04 +08:00
#ifdef WCF
FILE *fd = fopen(WCF_LOCK, "wb");
if (fd == NULL) {
2023-05-21 20:29:21 +08:00
MessageBox(NULL, L"无法打开lock文件", L"WxInitSDK", 0);
2023-02-25 02:11:04 +08:00
return -2;
}
fwrite((uint8_t *)&debug, sizeof(debug), 1, fd);
fwrite((uint8_t *)&spyBase, sizeof(spyBase), 1, fd);
fclose(fd);
#endif
2023-02-25 02:11:04 +08:00
debugMode = debug;
2022-10-15 20:25:42 +08:00
return 0;
2021-02-12 23:21:57 +08:00
}
2022-08-13 23:33:37 +08:00
int WxDestroySDK()
{
2023-02-25 02:11:04 +08:00
int status = 0;
#ifdef WCF
bool debug;
DWORD pid = GetWeChatPid();
if (pid == 0) {
2023-05-21 20:29:21 +08:00
MessageBox(NULL, L"微信未运行", L"WxDestroySDK", 0);
2023-02-25 02:11:04 +08:00
return status;
}
wcProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
if (wcProcess == NULL) {
2023-05-21 20:29:21 +08:00
MessageBox(NULL, L"微信未运行", L"WxDestroySDK", 0);
2023-02-25 02:11:04 +08:00
return -1;
}
FILE *fd = fopen(WCF_LOCK, "rb");
if (fd == NULL) {
2023-05-21 20:29:21 +08:00
MessageBox(NULL, L"无法打开lock文件", L"WxDestroySDK", 0);
2023-02-25 02:11:04 +08:00
return -2;
}
fread((uint8_t *)&debug, sizeof(debug), 1, fd);
fread((uint8_t *)&spyBase, sizeof(spyBase), 1, fd);
fclose(fd);
status = GetDllPath(debug, spyDllPath);
#else
status = GetDllPath(debugMode, spyDllPath);
#endif
if (status != 0) {
return status;
}
2023-04-08 18:58:10 +08:00
if (!CallDllFunc(wcProcess, spyDllPath, spyBase, "CleanupSpy", NULL, NULL)) {
2021-02-12 23:21:57 +08:00
return -1;
}
2022-10-15 20:25:42 +08:00
if (!EjectDll(wcProcess, spyBase)) {
return -1; // TODO: Unify error codes
2021-02-12 23:21:57 +08:00
}
2023-05-21 20:29:21 +08:00
2021-02-12 23:21:57 +08:00
return 0;
}