diff --git a/WeChatFerry/spy/rpc_server.cpp b/WeChatFerry/spy/rpc_server.cpp index 2192558..ab99536 100644 --- a/WeChatFerry/spy/rpc_server.cpp +++ b/WeChatFerry/spy/rpc_server.cpp @@ -75,7 +75,7 @@ static bool func_is_login(uint8_t *out, size_t *len) static bool func_get_self_wxid(uint8_t *out, size_t *len) { return FillResponse(Response_str_tag, out, len, [](Response &rsp) { - std::string wxid = GetSelfWxid(); + std::string wxid = user_info::get_self_wxid(); rsp.msg.str = wxid.empty() ? nullptr : (char *)wxid.c_str(); }); } @@ -83,7 +83,7 @@ static bool func_get_self_wxid(uint8_t *out, size_t *len) static bool func_get_user_info(uint8_t *out, size_t *len) { return FillResponse(Response_ui_tag, out, len, [](Response &rsp) { - UserInfo_t ui = GetUserInfo(); + UserInfo_t ui = user_info::get_user_info(); rsp.msg.ui.wxid = (char *)ui.wxid.c_str(); rsp.msg.ui.name = (char *)ui.name.c_str(); rsp.msg.ui.mobile = (char *)ui.mobile.c_str(); diff --git a/WeChatFerry/spy/user_info.cpp b/WeChatFerry/spy/user_info.cpp index a273592..7566ff3 100644 --- a/WeChatFerry/spy/user_info.cpp +++ b/WeChatFerry/spy/user_info.cpp @@ -1,7 +1,14 @@ -#include "user_info.h" +#include +#include + +#include "fill_response.h" #include "log.hpp" +#include "user_info.h" #include "util.h" +namespace user_info +{ + extern UINT64 g_WeChatWinDllAddr; #define OS_USER_HOME 0x5932770 @@ -9,50 +16,69 @@ extern UINT64 g_WeChatWinDllAddr; #define OS_USER_NAME 0x595C3D8 #define OS_USER_MOBILE 0x595C318 -static char home[MAX_PATH] = { 0 }; - -string GetHomePath() +std::string get_home_path() { - if (home[0] == 0) { - string path = Wstring2String(GET_WSTRING(g_WeChatWinDllAddr + OS_USER_HOME)) + "\\WeChat Files\\"; - strncpy_s(home, path.c_str(), path.size()); - } + static std::once_flag flag; + static std::string home_path; - return string(home); + std::call_once(flag, [] { + std::string path = Wstring2String(GET_WSTRING(g_WeChatWinDllAddr + OS_USER_HOME)) + "\\WeChat Files\\"; + home_path = std::filesystem::absolute(path).string(); + }); + + return home_path; } -string GetSelfWxid() +std::optional get_self_wxid() { - UINT64 wxidType = 0; + UINT64 wxid_type = 0; try { - wxidType = GET_UINT64(g_WeChatWinDllAddr + OS_USER_WXID + 0x18); - if (wxidType == 0xF) { + wxid_type = GET_UINT64(g_WeChatWinDllAddr + OS_USER_WXID + 0x18); + if (wxid_type == 0xF) { return GET_STRING_FROM_P(g_WeChatWinDllAddr + OS_USER_WXID); } else { return GET_STRING(g_WeChatWinDllAddr + OS_USER_WXID); } } catch (...) { - LOG_ERROR("wxid type: {:#x}", wxidType); - LOG_BUFFER((uint8_t *)(g_WeChatWinDllAddr + OS_USER_WXID), 20); - return "empty_wxid"; + LOG_ERROR("Failed to get wxid, type: {:#x}", wxid_type); + LOG_BUFFER(reinterpret_cast(g_WeChatWinDllAddr + OS_USER_WXID), 20); + return std::nullopt; } } -UserInfo_t GetUserInfo() +UserInfo_t get_user_info() { UserInfo_t ui; + auto wxid = get_self_wxid(); + ui.wxid = wxid.value_or("unknown_wxid"); - ui.wxid = GetSelfWxid(); - - UINT64 nameType = GET_UINT64(g_WeChatWinDllAddr + OS_USER_NAME + 0x18); - if (nameType == 0xF) { - ui.name = GET_STRING_FROM_P(g_WeChatWinDllAddr + OS_USER_NAME); - } else { // 0x1F - ui.name = GET_STRING(g_WeChatWinDllAddr + OS_USER_NAME); - } + UINT64 name_type = GET_UINT64(g_WeChatWinDllAddr + OS_USER_NAME + 0x18); + ui.name = (name_type == 0xF) ? GET_STRING_FROM_P(g_WeChatWinDllAddr + OS_USER_NAME) + : GET_STRING(g_WeChatWinDllAddr + OS_USER_NAME); ui.mobile = GET_STRING_FROM_P(g_WeChatWinDllAddr + OS_USER_MOBILE); - ui.home = GetHomePath(); + ui.home = get_home_path(); return ui; } + +bool rpc_get_self_wxid(uint8_t *out, size_t *len) +{ + return fill_response(out, len, [](Response &rsp) { + auto wxid = get_self_wxid(); + rsp.msg.str = wxid ? wxid->c_str() : "error"; + }); +} + +bool rpc_get_user_info(uint8_t *out, size_t *len) +{ + return fill_response(out, len, [](Response &rsp) { + UserInfo_t ui = get_user_info(); + rsp.msg.ui.wxid = ui.wxid.c_str(); + rsp.msg.ui.name = ui.name.c_str(); + rsp.msg.ui.mobile = ui.mobile.c_str(); + rsp.msg.ui.home = ui.home.c_str(); + }); +} + +} // namespace user_info diff --git a/WeChatFerry/spy/user_info.h b/WeChatFerry/spy/user_info.h index 8d70dc7..a959b04 100644 --- a/WeChatFerry/spy/user_info.h +++ b/WeChatFerry/spy/user_info.h @@ -1,12 +1,24 @@ #pragma once +#include #include #include "pb_types.h" -using namespace std; +namespace user_info +{ -string GetHomePath(); -string GetSelfWxid(); +// 获取 WeChat 数据存储路径 +std::string get_home_path(); -UserInfo_t GetUserInfo(); +// 获取自身 wxid +std::optional get_self_wxid(); + +// 获取用户信息 +UserInfo_t get_user_info(); + +// RPC 方法 +bool rpc_get_self_wxid(uint8_t *out, size_t *len); +bool rpc_get_user_info(uint8_t *out, size_t *len); + +} // namespace user_info