Code:
#include <Winsock2.h>
#include <Windows.h>
#include <detours.h>
#pragma comment(lib, "detours.lib")
#pragma comment(lib, "ws2_32.lib")
typedef int (WINAPI* r_send)(SOCKET sock, char* buf, int len, int flags);
r_send osend;
typedef int (WINAPI* r_recv)(SOCKET sock, char* buf, int len, int flags);
r_recv orecv;
int WINAPI custom_send (SOCKET sock, char* buf, int len, int flags);
int WINAPI custom_recv (SOCKET sock, char* buf, int len, int flags);
SOCKET capSock;
BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved)
{
if (reason == DLL_PROCESS_ATTACH)
{
osend = (r_send) DetourFunction((PBYTE)&send, (PBYTE)&custom_send);
orecv = (r_recv) DetourFunction((PBYTE)&recv, (PBYTE)&custom_recv);
}
return TRUE;
}
int WINAPI custom_send(SOCKET sock, char* buf, int len, int flags)
{
capSock = sock;
return osend(sock, buf, len, flags);
}
int WINAPI custom_recv(SOCKET sock, char* buf, int len, int flags)
{
capSock = sock;
return orecv(sock, buf, len, flags);
}
I used the Microsoft Detours Library 1.5 which you can get
[Only registered and activated users can see links. Click Here To Register...]. So first include all the windows, winsock and detours stuff and link the libs. Next we make ourselves two cute little function pointer types and create two function pointers with them, one for send and one for recv. Those will be holding the copies of the original winsock functions send and recv which the library will create for us (detouring means writing a JMP command that will jump to our custom function at the starting address of the function you want to detour, due to this the original function would be lost, fortunately the library saves the original for us so the program doesn't get fucked up). custom_send and custom_recv will "replace" (not really, we will still call the original function) send and recv, in those we can do what we want with all the parameters (in this case, copy the socket the game uses into our socket). Next we call DetourFunction (if .dll gets attached, you will have to inject this .dll), it gets 2 parameters, 1) address of the original function, 2) address of the "replacing" function. It returns the saved function which we put in our function pointers. Of course, in our custom functions we have to call the original send and recv (saved in the function pointers) so our program won't crash.
Should work, didn't test it though. Not too hard eh?