From f2c017de104d58f874659c89a4d71f6aca0ff7aa Mon Sep 17 00:00:00 2001 From: Changhua Date: Fri, 31 Jan 2025 09:55:44 +0800 Subject: [PATCH] Refatoring --- WeChatFerry/spy/receive_msg.cpp | 4 +-- WeChatFerry/spy/send_msg.cpp | 4 +-- WeChatFerry/spy/user_info.cpp | 58 ++++++++++++++++++--------------- WeChatFerry/spy/user_info.h | 2 +- 4 files changed, 36 insertions(+), 32 deletions(-) diff --git a/WeChatFerry/spy/receive_msg.cpp b/WeChatFerry/spy/receive_msg.cpp index 4aa92e3..5becce8 100644 --- a/WeChatFerry/spy/receive_msg.cpp +++ b/WeChatFerry/spy/receive_msg.cpp @@ -49,10 +49,10 @@ QWORD MessageHandler::DispatchMsg(QWORD arg1, QWORD arg2) if (wxMsg.roomid.find("@chatroom") != std::string::npos) { wxMsg.is_group = true; - wxMsg.sender = wxMsg.is_self ? GetSelfWxid() : GetStringByWstrAddr(arg2 + OS_RECV_MSG_WXID); + wxMsg.sender = wxMsg.is_self ? user_info::get_self_wxid() : GetStringByWstrAddr(arg2 + OS_RECV_MSG_WXID); } else { wxMsg.is_group = false; - wxMsg.sender = wxMsg.is_self ? GetSelfWxid() : wxMsg.roomid; + wxMsg.sender = wxMsg.is_self ? user_info::get_self_wxid() : wxMsg.roomid; } } catch (const std::exception &e) { LOG_ERROR(GB2312ToUtf8(e.what())); diff --git a/WeChatFerry/spy/send_msg.cpp b/WeChatFerry/spy/send_msg.cpp index dca37f7..4e5bbdb 100644 --- a/WeChatFerry/spy/send_msg.cpp +++ b/WeChatFerry/spy/send_msg.cpp @@ -7,11 +7,11 @@ #include "log.hpp" #include "send_msg.h" #include "spy_types.h" +#include "user_info.h" #include "util.h" extern HANDLE g_hEvent; extern QWORD g_WeChatWinDllAddr; -extern string GetSelfWxid(); // Defined in spy.cpp #define SRTM_SIZE 0x3F0 @@ -263,7 +263,7 @@ void SendXmlMessage(string receiver, string xml, string path, QWORD type) WxString *pReceiver = NewWxStringFromStr(receiver); WxString *pXml = NewWxStringFromStr(xml); WxString *pPath = NewWxStringFromStr(path); - WxString *pSender = NewWxStringFromStr(GetSelfWxid()); + WxString *pSender = NewWxStringFromStr(user_info::get_self_wxid()); sendXmlMsg(pBuf, (QWORD)pSender, (QWORD)pReceiver, (QWORD)pXml, (QWORD)pPath, (QWORD)(&nullBuf), type, 0x4, sign, pBuf2); diff --git a/WeChatFerry/spy/user_info.cpp b/WeChatFerry/spy/user_info.cpp index 7566ff3..6cf95b7 100644 --- a/WeChatFerry/spy/user_info.cpp +++ b/WeChatFerry/spy/user_info.cpp @@ -6,17 +6,17 @@ #include "user_info.h" #include "util.h" -namespace user_info -{ - extern UINT64 g_WeChatWinDllAddr; +namespace user_info +{ #define OS_USER_HOME 0x5932770 #define OS_USER_WXID 0x595C270 #define OS_USER_NAME 0x595C3D8 #define OS_USER_MOBILE 0x595C318 -std::string get_home_path() + std::string + get_home_path() { static std::once_flag flag; static std::string home_path; @@ -29,28 +29,34 @@ std::string get_home_path() return home_path; } -std::optional get_self_wxid() +std::string get_self_wxid() { - UINT64 wxid_type = 0; - try { - 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); + static std::once_flag flag; + static std::string wxid; + + std::call_once(flag, [] { + UINT64 wxid_type = 0; + try { + wxid_type = GET_UINT64(g_WeChatWinDllAddr + OS_USER_WXID + 0x18); + if (wxid_type == 0xF) { + wxid = GET_STRING_FROM_P(g_WeChatWinDllAddr + OS_USER_WXID); + } else { + wxid = GET_STRING(g_WeChatWinDllAddr + OS_USER_WXID); + } + + } catch (...) { + LOG_ERROR("Failed to get wxid, type: {:#x}", wxid_type); + LOG_BUFFER(reinterpret_cast(g_WeChatWinDllAddr + OS_USER_WXID), 20); + wxid = "获取wxid失败"; } - } catch (...) { - LOG_ERROR("Failed to get wxid, type: {:#x}", wxid_type); - LOG_BUFFER(reinterpret_cast(g_WeChatWinDllAddr + OS_USER_WXID), 20); - return std::nullopt; - } + }); + return wxid; } UserInfo_t get_user_info() { UserInfo_t ui; - auto wxid = get_self_wxid(); - ui.wxid = wxid.value_or("unknown_wxid"); + ui.wxid = get_self_wxid(); 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) @@ -64,20 +70,18 @@ UserInfo_t get_user_info() 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"; - }); + return fill_response( + out, len, [](Response &rsp) { rsp.msg.str = (char *)get_self_wxid().c_str(); }); } 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(); + 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(); + rsp.msg.ui.home = (char *)ui.home.c_str(); }); } diff --git a/WeChatFerry/spy/user_info.h b/WeChatFerry/spy/user_info.h index a959b04..f248ba7 100644 --- a/WeChatFerry/spy/user_info.h +++ b/WeChatFerry/spy/user_info.h @@ -12,7 +12,7 @@ namespace user_info std::string get_home_path(); // 获取自身 wxid -std::optional get_self_wxid(); +std::string get_self_wxid(); // 获取用户信息 UserInfo_t get_user_info();