增加注释和打印

This commit is contained in:
LuChao 2024-06-01 14:56:02 +08:00
parent d4d2267305
commit 35a7aae3d0
2 changed files with 193 additions and 99 deletions

View File

@ -1,92 +1,160 @@
#include <WinSock2.h> #include <WinSock2.h>
#include "hook.h" #include "hook.h"
#include "client_socket.h" #include "client_socket.h"
#include "config.h" #include "config.h"
#include "detours.h" #include "detours.h"
#include "http_client.h" #include "http_client.h"
#include "nlohmann/json.hpp" #include "nlohmann/json.hpp"
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"
// #include "thread_pool.h" #include "thread_pool.h"
#include "utils.h" #include "utils.h"
#include "wxutils.h" #include "wxutils.h"
namespace hook { #include <iostream>
void SendHttpMsgCallback(PTP_CALLBACK_INSTANCE instance, PVOID context,
PTP_WORK Work) {
InnerMessageStruct *msg = (InnerMessageStruct *)context;
if (msg == NULL) {
SPDLOG_INFO("http msg is null");
return;
}
std::unique_ptr<InnerMessageStruct> sms(msg);
nlohmann::json j_msg = nlohmann::json::parse(
msg->buffer, msg->buffer + msg->length, nullptr, false);
if (j_msg.is_discarded()) {
return;
}
std::string jstr = j_msg.dump() + "\n";
std::string url = wxhelper::Config::GetInstance().GetRecvHttpUrl();
int timeout = wxhelper::Config::GetInstance().GetRecvHttpTimeout();
http::HttpClient client{url, timeout};
client.SendRequest(jstr);
}
namespace hook
{
/**
* @brief HTTP
* @param instance
* @param context
* @param Work
*/
void SendHttpMsgCallback(PTP_CALLBACK_INSTANCE instance, PVOID context, PTP_WORK Work)
{
wxhelper::wxutils::print_utf8_to_console("SendHttpMsgCallback called"); // 调试打印
void SendTcpMsgCallback(PTP_CALLBACK_INSTANCE instance, PVOID context, SPDLOG_INFO("SendHttpMsgCallback called"); // 调试打印
PTP_WORK Work) {
InnerMessageStruct *msg = (InnerMessageStruct *)context; InnerMessageStruct *msg = (InnerMessageStruct *)context;
if (msg == NULL) { if (msg == NULL)
SPDLOG_INFO("add work:msg is null"); {
return; wxhelper::wxutils::print_utf8_to_console("httpMsg is null"); // 调试打印
SPDLOG_INFO("httpMsg is null");
return;
}
std::unique_ptr<InnerMessageStruct> sms(msg);
nlohmann::json j_msg = nlohmann::json::parse(
msg->buffer, msg->buffer + msg->length, nullptr, false);
if (j_msg.is_discarded())
{
wxhelper::wxutils::print_utf8_to_console("jsonMsg discarded"); // 调试打印
SPDLOG_INFO("jsonMsg discarded"); // 调试打印
return;
}
std::string jstr = j_msg.dump() + "\n";
std::string url = wxhelper::Config::GetInstance().GetRecvHttpUrl();
int timeout = wxhelper::Config::GetInstance().GetRecvHttpTimeout();
http::HttpClient client{url, timeout};
client.SendRequest(jstr);
} }
std::unique_ptr<InnerMessageStruct> sms(msg);
nlohmann::json j_msg = nlohmann::json::parse( /**
msg->buffer, msg->buffer + msg->length, nullptr, false); * @brief TCP
if (j_msg.is_discarded()) { * @param instance
return; * @param context
* @param Work
*/
void SendTcpMsgCallback(PTP_CALLBACK_INSTANCE instance, PVOID context, PTP_WORK Work)
{
wxhelper::wxutils::print_utf8_to_console("SendTcpMsgCallback called"); // 调试打印
SPDLOG_INFO("SendTcpMsgCallback called"); // 调试打印
InnerMessageStruct *msg = (InnerMessageStruct *)context;
if (msg == NULL)
{
wxhelper::wxutils::print_utf8_to_console("add work:msg is null"); // 调试打印
SPDLOG_INFO("add work:msg is null");
return;
}
std::unique_ptr<InnerMessageStruct> sms(msg);
nlohmann::json j_msg = nlohmann::json::parse(
msg->buffer, msg->buffer + msg->length, nullptr, false);
if (j_msg.is_discarded())
{
wxhelper::wxutils::print_utf8_to_console("jsonMsg discarded"); // 调试打印
SPDLOG_INFO("jsonMsg discarded"); // 调试打印
return;
}
std::string jstr = j_msg.dump() + "\n";
// wxhelper::wxutils::print_utf8_to_console("SendTcpMsgCallback jstr:" + jstr);
wxhelper::wxutils::print_utf8_to_console("SendTcpMsgCallback jstr return JSON");
std::string ip = wxhelper::Config::GetInstance().GetRecvTcpIp();
int port = wxhelper::Config::GetInstance().GetRecvTcpPort();
wxhelper::TcpClient client{ip, port};
client.SendAndCloseSocket(jstr);
} }
std::string jstr = j_msg.dump() + "\n";
std::string ip = wxhelper::Config::GetInstance().GetRecvTcpIp();
int port = wxhelper::Config::GetInstance().GetRecvTcpPort();
wxhelper::TcpClient client{ip, port};
client.SendAndCloseSocket(jstr);
}
/**
* @brief
BaseHook::BaseHook(void *origin, void *detour) * @param origin
: origin_(origin), detour_(detour) {} * @param detour
*/
int BaseHook::Hook() { BaseHook::BaseHook(void *origin, void *detour)
if (hook_flag_) { : origin_(origin), detour_(detour)
SPDLOG_INFO("hook already called"); {
return 2; wxhelper::wxutils::print_utf8_to_console("BaseHook created with origin"); // 调试打印
// 调试打印
SPDLOG_INFO("BaseHook created with origin: {} and detour: {}", origin, detour); // 调试打印
} }
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach((PVOID *)origin_, detour_);
LONG ret = DetourTransactionCommit();
if (ret == NO_ERROR) {
hook_flag_ = true;
}
return ret;
}
int BaseHook::Unhook() { /**
if (!hook_flag_) { * @brief
SPDLOG_INFO("hook already called"); * @return
return NO_ERROR; */
} int BaseHook::Hook()
UINT64 base = wxhelper::wxutils::GetWeChatWinBase(); {
DetourTransactionBegin(); wxhelper::wxutils::print_utf8_to_console("Hook called"); // 调试打印
DetourUpdateThread(GetCurrentThread()); SPDLOG_INFO("Hook called"); // 调试打印
DetourDetach((PVOID *)origin_, detour_); if (hook_flag_)
LONG ret = DetourTransactionCommit(); {
if (ret == NO_ERROR) { // std::cout << "hook already called" << std::endl; // 调试打印
hook_flag_ = false; wxhelper::wxutils::print_utf8_to_console("hook already called"); // 调试打印
}
return ret;
}
} // namespace hook SPDLOG_INFO("hook already called");
return 2;
}
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach((PVOID *)origin_, detour_);
LONG ret = DetourTransactionCommit();
if (ret == NO_ERROR)
{
hook_flag_ = true;
}
return ret;
}
/**
* @brief
* @return
*/
int BaseHook::Unhook()
{
wxhelper::wxutils::print_utf8_to_console("Unhook called"); // 调试打印
SPDLOG_INFO("Unhook called"); // 调试打印
if (!hook_flag_)
{
wxhelper::wxutils::print_utf8_to_console("hook already called"); // 调试打印
SPDLOG_INFO("hook already called");
return NO_ERROR;
}
UINT64 base = wxhelper::wxutils::GetWeChatWinBase();
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach((PVOID *)origin_, detour_);
LONG ret = DetourTransactionCommit();
if (ret == NO_ERROR)
{
hook_flag_ = false;
}
return ret;
}
} // namespace hook

View File

@ -4,35 +4,61 @@
#include <stdint.h> #include <stdint.h>
namespace hook { namespace hook {
struct InnerMessageStruct { /**
char* buffer; * @struct InnerMessageStruct InnerMessageStruct
int64_t length; */
~InnerMessageStruct() { struct InnerMessageStruct {
if (this->buffer != NULL) { char* buffer;
delete[] this->buffer; int64_t length;
this->buffer = NULL; // 析构函数,用于释放缓冲区内存
} ~InnerMessageStruct() {
} if (this->buffer!= NULL) {
}; delete[] this->buffer;
this->buffer = NULL;
}
}
};
void SendHttpMsgCallback(PTP_CALLBACK_INSTANCE instance, PVOID context, /**
PTP_WORK Work); * @brief HTTP
* @param instance
* @param context
* @param Work
*/
void SendHttpMsgCallback(PTP_CALLBACK_INSTANCE instance, PVOID context, PTP_WORK Work);
void SendTcpMsgCallback(PTP_CALLBACK_INSTANCE instance, PVOID context, /**
PTP_WORK Work); * @brief TCP
class BaseHook { * @param instance
public: * @param context
BaseHook():hook_flag_(false),origin_(nullptr),detour_(nullptr){} * @param Work
BaseHook(void* origin, void* detour); */
int Hook(); void SendTcpMsgCallback(PTP_CALLBACK_INSTANCE instance, PVOID context, PTP_WORK Work);
int Unhook();
virtual ~BaseHook() {}
protected: /**
bool hook_flag_; * @class BaseHook BaseHook
void* origin_; */
void* detour_; class BaseHook {
}; public:
// 构造函数
BaseHook():hook_flag_(false),origin_(nullptr),detour_(nullptr){}
// 构造函数,接收原始函数和替换函数的指针
BaseHook(void* origin, void* detour);
// 钩子函数,用于安装钩子
int Hook();
// 卸载钩子函数
int Unhook();
// 虚析构函数
virtual ~BaseHook() {}
protected:
// 钩子标志
bool hook_flag_;
// 原始函数指针
void* origin_;
// 替换函数指针
void* detour_;
};
} // namespace hook } // namespace hook
#endif #endif