#include <windows.h>
#include <detours.h>
#include <stdio.h>
#pragma comment(lib, "detours.lib")
#pragma comment (lib, "ws2_32")
typedef int (WINAPI *connectTypedef) (SOCKET, sockaddr_in *, int);
connectTypedef connectOrg;
typedef struct hostent* (WINAPI *gethostbynameTypedef) (const char *);
gethostbynameTypedef gethostbynameOrg;
typedef unsigned long (WINAPI *inet_addrTypedef) (const char *);
inet_addrTypedef inet_addrOrg;
struct hostent* WINAPI gethostbynameHook(const char *name)
{
printf ("gethostbyname called with name as %s\n", name);
return gethostbynameOrg(name);
}
int WINAPI connectHook (SOCKET s, sockaddr_in *addr, int len)
{
printf ("Connecting to: %s:%u\n", inet_ntoa(addr->sin_addr), htons(addr->sin_port));
int ret = connectOrg (s, addr, len);
printf ("Return %i\n", ret);
if (ret == SOCKET_ERROR)
{
printf ("Socket Erorr: %u\n\n", WSAGetLastError());
}
return ret;
}
const char *LOCALHOST = "127.0.0.1";
unsigned long WINAPI inet_addrHook (const char *cp)
{
printf ("inet_addr called with IP: %s\n", cp);
return inet_addrOrg(cp);
}
BOOL WINAPI DllMain (HMODULE module, DWORD reason, LPVOID)
{
if (reason == DLL_PROCESS_ATTACH)
{
DisableThreadLibraryCalls (module);
AllocConsole();
freopen("CONOUT$", "w", stdout);
freopen("CONIN$", "r", stdin);
connectOrg = (connectTypedef)DetourFunction ((PBYTE)GetProcAddress(GetModuleHandleA("ws2_32"), "connect"), (PBYTE)connectHook);
gethostbynameOrg = (gethostbynameTypedef)DetourFunction ((PBYTE)GetProcAddress(GetModuleHandleA("ws2_32"), "gethostbyname"), (PBYTE)gethostbynameHook);
inet_addrOrg = (inet_addrTypedef)DetourFunction ((PBYTE)GetProcAddress(GetModuleHandleA("ws2_32"), "inet_addr"), (PBYTE)inet_addrHook);
}
return TRUE;
}