WeChatFerry/sdk/sdk.cpp

78 lines
2.0 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"
#include <process.h>
#include <tlhelp32.h>
#include "injector.h"
2022-10-15 20:25:42 +08:00
#include "log.h"
2021-02-12 23:21:57 +08:00
#include "sdk.h"
#include "util.h"
2022-10-16 22:14:06 +08:00
static DWORD wcPid = 0;
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 01:53:02 +08:00
int WxInitSDK(bool debug)
2021-02-12 23:21:57 +08:00
{
2022-10-15 20:25:42 +08:00
int status = 0;
InitLogger();
LOG_INFO("WxInitSDK.");
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-02-25 01:53:02 +08:00
LOG_ERROR("DLL does not exists: {}.", Wstring2String(spyDllPath));
2021-02-12 23:21:57 +08:00
return ERROR_FILE_NOT_FOUND;
}
2022-10-15 20:25:42 +08:00
status = OpenWeChat(&wcPid);
2021-02-12 23:21:57 +08:00
if (status != 0) {
2022-10-15 20:25:42 +08:00
LOG_ERROR("OpenWeChat failed: {}.", status);
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) {
LOG_ERROR("Failed to Inject DLL into WeChat.");
2021-02-12 23:21:57 +08:00
return -1;
}
2022-10-15 20:25:42 +08:00
if (!CallDllFunc(wcProcess, spyDllPath, spyBase, "InitSpy", NULL)) {
LOG_ERROR("Failed to InitSpy.");
2022-08-20 15:15:04 +08:00
return -1;
}
#if 0
2022-08-20 15:15:04 +08:00
do {
2022-10-15 20:25:42 +08:00
if (!CallDllFunc(wcProcess, spyDllPath, spyBase, "IsLogin", (DWORD *)&status)) {
LOG_ERROR("Failed to check login status.");
return -1;
2022-08-20 15:15:04 +08:00
}
2022-08-07 16:21:28 +08:00
Sleep(1000);
2022-10-15 20:25:42 +08:00
} while (status == 0);
#endif
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()
{
2022-10-15 20:25:42 +08:00
LOG_INFO("WxDestroySDK");
if (!CallDllFunc(wcProcess, spyDllPath, spyBase, "CleanupSpy", NULL)) {
LOG_ERROR("Failed to CleanupSpy.");
2021-02-12 23:21:57 +08:00
return -1;
}
2022-10-15 20:25:42 +08:00
if (!EjectDll(wcProcess, spyBase)) {
LOG_ERROR("Failed to Eject DLL.");
return -1; // TODO: Unify error codes
2021-02-12 23:21:57 +08:00
}
return 0;
}