我有一个应用程序,它使用Winsock 2.0 recv
函数,我可以通过Redox Packet Editor捕获输出,例如,它确认版本是2.0。Winsock recv hooking Detours
我有这样的代码挂钩函数:
#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的另一个应用程序,它工作正常。试图挂钩WSARecv,WSARecvEx没有任何运气。
使用WinAPIOverride32检查了应用程序,它清楚地表明它使用了recv
函数,并成功记录了使用情况。 Winsock Packet Editor也正在读取数据。
任何想法?
int WINAPI MyRecv(SOCKET s, char* buf, int len, int flags)
{
fopen_s(&pRecvLogFile, "C:\\RecvLog.txt", "a+");
fprintf(pRecvLogFile, "%s\n", buf);
fclose(pRecvLogFile);
return pRecv(s, buf, len, flags); //you need to call recv first
}
,而不是做这样的事情:
我的建议是:写一个LSP(Layered Service Provider)。修改其中一个LSP样本将使您获得更多,并且可能比挂钩方式更具包容性。将其设想为TDI驱动程序和朋友的替代用户模式。 – 0xC0000022L 2012-04-09 00:17:06
该LSP的任何文章或示例代码?我希望它不那么复杂。 – methyl 2012-04-09 08:34:12
当然,这个大部分仍然有效:https://www.microsoft.com/msj/0599/layeredservice/layeredservice.aspx,当然还有:http://msdn.microsoft.com/en-us/ library/windows/desktop/bb513664(v = vs.85).aspx – 0xC0000022L 2012-04-09 11:50:07