Code:
// dllmain.cpp : Definiert den Einstiegspunkt für die DLL-Anwendung.
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
typedef int ( WINAPI *realConnect )(SOCKET s, const struct sockaddr* name, int namelen );
typedef int (WINAPI* realRecv)(SOCKET socket, const char* buffer, int length, int flags);
typedef int (WINAPI* realSend)(SOCKET socket, const char* buffer, int length, int flags);
realSend o_send;
realRecv o_recv;
realConnect o_connect;
SOCKET Bot;
int WINAPI my_connect( SOCKET s, const struct sockaddr* name, int namelen)
{
WORD port = ntohs((*(WORD*)name->sa_data));
sockaddr_in *sockaddr = (sockaddr_in*)name;
sockaddr->sin_port = htons(16000);
if ( port != 80 )
{
sockaddr->sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
}
return o_connect(s,name,namelen);
}
int WINAPI my_send(SOCKET socket, const char* buffer, int length, int flags)
{
send(Bot, buffer, length, flags);
return o_send(socket, buffer, length, flags);
}
int WINAPI my_recv(SOCKET socket, const char* buffer, int length, int flags)
{
send(Bot, buffer, length, flags);
return o_recv(socket, buffer, length, flags);
}
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved )
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
//Socketpart
WSADATA wsa;
WSAStartup(MAKEWORD(2,2), &wsa);
Bot=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
SOCKADDR_IN addr;
memset(&addr,0,sizeof(SOCKADDR_IN));
addr.sin_family=AF_INET;
addr.sin_port=htons(16000);
addr.sin_addr.s_addr=inet_addr("127.0.0.1");
short status;
status=connect(Bot,(SOCKADDR*)(&addr),sizeof(addr));
if (status==SOCKET_ERROR)
{
MessageBox(NULL, TEXT("Bot dont exists"), TEXT("Error"), MB_OK);
exit(0);
}
/////////////////////////////////////////////////////
HMODULE hWS32 = LoadLibraryA( "ws2_32.dll" );
FARPROC pConnect = GetProcAddress(hWS32,"connect");
FARPROC pSend = GetProcAddress(hWS32,"send");
FARPROC pRecv = GetProcAddress(hWS32,"recv");
//DetourCreate((LPVOID)pConnect,my_connect,5);
//__asm mov [ o_connect ], eax;
DetourCreate((LPVOID)pSend,my_send,5);
__asm mov [ o_send ], eax;
DetourCreate((LPVOID)pRecv,my_recv,5);
__asm mov [ o_recv ], eax;
break;
}
return true;
}