I've got an application, which uses Winsock 2.0 recv
function, and I can catch the output by Redox Packet Editor for example, it confirms that version is 2.0.
I have this code to hook the function:
#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);
}
For send
, everything works perfectly, but I don't get any output for recv
. I tried in another application using 1.1 version of Winsock and it works ok. Tried to hook WSARecv, WSARecvEx without any luck.
Checked the app with WinAPIOverride32, it clearly says that it using recv
function, and successfuly logs the usage. Winsock Packet Editor is reading the data well too.
Any ideas?
Are you sure that you are hooking the correct dll? I'd double check which dll is actually used by the program: WSOCK32.dll or ws2_32.dll.
Edit:
Maybe try something like this:
typedef int (WINAPI *SendPtr)(SOCKET s, const char* buf, int len, int flags);
HMODULE hLib = LoadLibrary("wsock32.dll");
SendPtr pSend = (SendPtr)GetProcAddress(hLib, "send");
And then use pSend
with that value (same thing for recv). Don't forget to call FreeLibrary in the end.
If you are sure that the dll is already loaded, then it's probably better to use GetModuleHandle("wsock32.dll")
since you don't have to call FreeLibrary in that case.