我有一个应用程序,它使用 Winsock 2.0 recv
函数,我可以通过 Redox Packet Editor 捕获输出,例如,它确认版本是 2.0。
我有这段代码来 Hook 函数:
#define _CRT_SECURE_NO_DEPRECATE
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <WinSock2.h>
#include <detours.h>
#include <stdio.h>
#pragma comment(lib, "ws2_32.lib")
FILE *pSendLogFile;
FILE *pRecvLogFile;
int (WINAPI *pSend)(SOCKET s, const char* buf, int len, int flags) = send;
int WINAPI MySend(SOCKET s, const char* buf, int len, int flags);
int (WINAPI *pRecv)(SOCKET s, char *buf, int len, int flags) = recv;
int WINAPI MyRecv(SOCKET s, char* buf, int len, int flags);
INT APIENTRY DllMain(HMODULE hDLL, DWORD Reason, LPVOID Reserved)
{
switch(Reason)
{
case DLL_PROCESS_ATTACH:
DisableThreadLibraryCalls(hDLL);
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)pSend, MySend);
if(DetourTransactionCommit() == NO_ERROR)
MessageBox(0,"send() detoured successfully","asd",MB_OK);
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)pRecv, MyRecv);
if(DetourTransactionCommit() == NO_ERROR)
MessageBox(0,"recv() detoured successfully","asd",MB_OK);
break;
case DLL_PROCESS_DETACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
break;
}
return TRUE;
}
int WINAPI MySend(SOCKET s, const char* buf, int len, int flags)
{
MessageBox(0,"sent","sent",MB_OK);
return pSend(s, buf, len, flags);
}
int WINAPI MyRecv(SOCKET s, char* buf, int len, int flags)
{
MessageBox(0,"recvd","recvd",MB_OK);
return pRecv(s, buf, len, flags);
}
对于 send
,一切正常,但对于 recv
我没有得到任何输出。我在另一个应用程序中尝试使用 1.1 版本的 Winsock,它工作正常。尝试 Hook WSARecv、WSARecvEx 但没有任何运气。
使用 WinAPIOverride32 检查应用程序,它清楚地表明它使用 recv
函数,并成功记录了使用情况。 Winsock 数据包编辑器也能很好地读取数据。
有什么想法吗?
最佳答案
您确定您挂接的是正确的 dll 吗?我会仔细检查程序实际使用了哪个 dll:WSOCK32.dll 或 ws2_32.dll。
编辑:
也许尝试这样的事情:
typedef int (WINAPI *SendPtr)(SOCKET s, const char* buf, int len, int flags);
HMODULE hLib = LoadLibrary("wsock32.dll");
SendPtr pSend = (SendPtr)GetProcAddress(hLib, "send");
然后将 pSend
与该值一起使用(recv 也是如此)。不要忘记最后调用 FreeLibrary。
如果您确定 dll 已经加载,那么最好使用 GetModuleHandle("wsock32.dll")
,因为在这种情况下您不必调用 FreeLibrary。
关于c++ - Winsock recv hooking with Detours,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10065903/