c++ winsock hook

08/01/2013 04:11 tripa1#1
Hey everyone, in need of help, as ive tried and tried and tried, me being newbie still coming from autoit :p, I use this current code below to log sent packets from application, now how would I go about to send my own packet back to the send function
example: 02 45 00 21 00.... that begin a packet, I know I probably have to convert to ascii then send to my buffer? please help me :)


Code:
#include <cstdio>
#include <ctime>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <windows.h>
#include <detours.h>
#include "stdafx.h"
#pragma comment( lib, "Ws2_32.lib" )
#pragma comment( lib, "detours.lib" )
#pragma comment( lib, "Mswsock.lib" )

std::ofstream Logger;

std::string NowToString() {
    time_t rawtime;
    tm *timeinfo = new tm();
    char buffer[32];

    time( &rawtime );
    localtime_s( timeinfo, &rawtime );

    strftime( buffer, 32, "%m/%d/%Y %I:%M:%S %p", timeinfo );

    delete timeinfo;

    return std::string( buffer );
}

std::string TimeToString() {
    time_t rawtime;
    tm *timeinfo = new tm();
    char buffer[32];

    time( &rawtime );
    localtime_s( timeinfo, &rawtime );

    strftime( buffer, 32, "%I:%M:%S %p", timeinfo );

    delete timeinfo;

    return std::string( buffer );
}



void LogPacket( const char *buf, int len ) {
    Logger << "        0  1  2  3  4  5  6  7   8  9  A  B  C  D  E  F\n";
    Logger << "       -- -- -- -- -- -- -- --  -- -- -- -- -- -- -- --\n";
    Logger << "0000   ";

    for ( int i = 0; i < len; ++i ) {
    	if ( i != 0 && i % 16 == 0 ) {
    		Logger << "  ";

    		int line = ( i / 16 ) - 1;

    		for ( int j = 0; j < 16; ++j ) {
    			char c = buf[line * 16 + j];

    			if ( c >= 32 && c <= 126 ) {
    				Logger << c;
    			} else {
    				Logger << '.';
    			}
    		}

    		Logger << "\n" << std::hex << std::setw( 4 ) << std::setfill( '0' ) << i << std::dec << std::setw( 0 ) << "   ";
    	} else if ( i % 16 == 8 ) {
    		Logger << ' ';
    	}

    	Logger << std::hex << std::setw( 2 ) << std::setfill( '0' ) << ( int( buf[i] ) & 0xFF ) << ' ';
    	Logger << std::dec << std::setw( 0 );

    	if ( i == len - 1 ) {
    		int remaining = 16 - ( len % 16 );
    		int fill = ( remaining * 3 ) + 2;

    		if ( remaining >= 8 ) {
    			++fill;
    		}

    		for ( int j = 0; j < fill; ++j ) {
    			Logger << ' ';
    		}

    		int line = ( i - ( ( len % 16 ) - 1 ) ) / 16 ;

    		for ( int k = 0; k < ( len % 16 ); ++k ) {
    			char c = buf[line * 16 + k];

    			if ( c >= 32 && c <= 126 ) {
    				Logger << c;
    			} else {
    				Logger << '.';
    			}
    		}
    	}
    }

    Logger << "\n\n";
}


int ( WINAPI *Real_Send )( SOCKET s, const char *buf, int len, int flags ) = send;
int ( WINAPI *Real_Recv )( SOCKET s, char *buf, int len, int flags ) = recv;
int ( WINAPI *Real_RecvFrom )( SOCKET s, char *buf, int len, int flags, sockaddr *from, int *fromlen ) = recvfrom;
int ( WINAPI *Real_WSARecvEx )( SOCKET s, char *buf, int len, int *flags ) = WSARecvEx;

int WINAPI Mine_Send( SOCKET s, const char* buf, int len, int flags );
int WINAPI Mine_Recv( SOCKET s, char *buf, int len, int flags );
int WINAPI Mine_RecvFrom( SOCKET s, char *buf, int len, int flags, sockaddr *from, int *fromlen );
int WINAPI Mine_WSARecvEx( SOCKET s, char *buf, int len, int *flags );



int WINAPI Mine_Send( SOCKET s, const char *buf, int len, int flags ) {
    Logger << TimeToString() << ": Client -> Server (Length: " << len << " bytes)\n\n";
    LogPacket( buf, len );
    Logger << std::endl;

    return Real_Send( s, buf, len, flags );
}


int WINAPI Mine_Recv( SOCKET s, char *buf, int len, int flags ) {
    Logger << TimeToString() << ": Server -> Client (Length: " << len << " bytes)\n\n";
    LogPacket( buf, len );
    Logger << std::endl;

    return Real_Recv( s, buf, len, flags );
}

int WINAPI Mine_RecvFrom( SOCKET s, char *buf, int len, int flags, sockaddr *from, int *fromlen ) {
    Logger << TimeToString() << ": Server -> Client (Length: " << len << " bytes)*\n\n";
    LogPacket( buf, len );
    Logger << std::endl;

    return Real_RecvFrom( s, buf, len, flags, from, fromlen );
}

int WINAPI Mine_WSARecvEx( SOCKET s, char *buf, int len, int *flags ) {
    Logger << TimeToString() << ": Server -> Client (Length: " << len << " bytes)**\n\n";
    LogPacket( buf, len );
    Logger << std::endl;

    return Real_WSARecvEx( s, buf, len, flags );
}

BOOL WINAPI DllMain( HINSTANCE, DWORD dwReason, LPVOID ) {
    switch ( dwReason ) {
    	case DLL_PROCESS_ATTACH:
	MessageBox(NULL, "Attached", "From DLL", MB_OK);
    		Logger.open( "C:\\Packets.txt", std::ios::out | std::ios::app | std::ios::ate );
    		if ( Logger.tellp() > 0 ) {
    			Logger << "\n\n\n";
    		}
    		Logger << "##\n## Logging Started (" << NowToString() << ")\n##\n\n\n";
    		DetourTransactionBegin();
    		DetourUpdateThread( GetCurrentThread() );
    		DetourAttach( &(PVOID &)Real_Send, Mine_Send );
    		DetourAttach( &(PVOID &)Real_Recv, Mine_Recv );
    		DetourAttach( &(PVOID &)Real_RecvFrom, Mine_RecvFrom );
    		DetourAttach( &(PVOID &)Real_WSARecvEx, Mine_WSARecvEx );
    		DetourTransactionCommit();

    		break;

    	case DLL_PROCESS_DETACH:
	MessageBox(NULL, "DeAttached", "From DLL", MB_OK);
    		Logger << "##\n## Logging Stopped (" << NowToString() << ")\n##";
    		Logger.close();
    		DetourTransactionBegin();
    		DetourUpdateThread( GetCurrentThread() );
    		DetourDetach( &(PVOID &)Real_Send, Mine_Send );
    		DetourDetach( &(PVOID &)Real_Recv, Mine_Recv );
    		DetourDetach( &(PVOID &)Real_RecvFrom, Mine_RecvFrom );
    		DetourDetach( &(PVOID &)Real_WSARecvEx, Mine_WSARecvEx );
    		DetourTransactionCommit();

    		break;
    }

    return TRUE;
}
08/01/2013 11:00 phize#2
Call send with the right SOCKET argument.

You can use std::stringstream to convert your hex string to bytes, if that's what you're asking.
08/01/2013 11:38 tripa1#3
im new to all this, so if u care to explain please??? lol
example, I log a packet, saying hi in game which the packet = 08 03 68 69 00 which gives ascii of ..hi. {if that's what it even means}, but when try send the ..hi. in raw like this....
char *packet = "..hi.";
send(s, packet, strlen(packet), 0);
I get packet of 2e 2e 68 69 2e logged after I inject and game closes, which doesn't trigger the 08 03 68 69 00 that its suppose to be
I changed my arguments to
af = AF_INET
type = SOCK_STREAM
protocol = 0
which I know is correct, can someone please point me in the right direction, this game is really really basic but im still having trouble lol, can I send packet as is, or do I have to send the ..hi. style
thanks to who ever reposts
08/01/2013 12:12 phize#4
Oh boy...

You should probably read some C++ tutorials.
08/01/2013 12:28 tripa1#5
hey I know I probally should LOL, like I said, coming from autoit which is a completely different ball game, ive created a massive bot in autoit, if I can create a packet send func in C++ im going to convert now, was just trying to complete it at easy steps knowing ill get it working lol