Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > C/C++
You last visited: Today at 20:39

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



c++ winsock hook

Discussion on c++ winsock hook within the C/C++ forum part of the Coders Den category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Feb 2010
Posts: 6
Received Thanks: 0
c++ winsock hook

Hey everyone, in need of help, as ive tried and tried and tried, me being newbie still coming from autoit , 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;
}
tripa1 is offline  
Old 08/01/2013, 11:00   #2
 
elite*gold: 0
Join Date: Sep 2006
Posts: 774
Received Thanks: 8,580
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.
phize is offline  
Old 08/01/2013, 11:38   #3
 
elite*gold: 0
Join Date: Feb 2010
Posts: 6
Received Thanks: 0
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
tripa1 is offline  
Old 08/01/2013, 12:12   #4
 
elite*gold: 0
Join Date: Sep 2006
Posts: 774
Received Thanks: 8,580
Oh boy...

You should probably read some C++ tutorials.
phize is offline  
Old 08/01/2013, 12:28   #5
 
elite*gold: 0
Join Date: Feb 2010
Posts: 6
Received Thanks: 0
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
tripa1 is offline  
Reply


Similar Threads Similar Threads
Winsock send Hook crasht Programm?
07/14/2013 - C/C++ - 18 Replies
Hey, Habe vorhin ein Hook zusammengeschraubt womit ich mir bisschen die Pakete von Netzwerk Programmen anschauen wollte. In Counterstrike funktioniert das ganze ohne große Probleme, bei Firefox oder WoW schmiert es aber mit ner Acces Violation ab.. hab ich da irgendwas übersehen im Code? Versuch schon seit gut ner Stunde rum, bekomms aber nicht gebacken.. mittlerweile schaut der Code auch schon bissel wüsst aus vom rumprobieren.. :rolleyes: #include <windows.h> #include <iostream>...
Winsock send Hook Problem
08/08/2011 - General Coding - 20 Replies
Huhu, Ich würde gerne die send(...) Mehtode hooken, um das Socket abfangen zu können, damit ich danach eigene Pakete verschicken kann. Das Problem besteht darin, dass sobald ich die dll injecte(z.b in firefox) und ein paket versende, einmal die MessageBox erscheint, das send() aufgerufen wurde und danach das Programm abstürtzt. Zum hooken benutze ich microsoft detours 1.5 und arbeite unter win 7 64bit. Die dll compile ich als 32bit und injecte sie auch in einen 32bit prozess. Würde mich...
(Winsock hook) replace/filter packet bytes
11/18/2010 - General Coding - 3 Replies
Hi guys Please, can someone explain me or help me about my winsock hook?! I need to change the first and second bytes of the packet I've received... example: I'm receiving the packet:
[VB6]Winsock Packet Sending Program & [C++] ws_32 Hook&DLL
06/07/2009 - C/C++ - 1 Replies
Hi.. im trying to create a program that send packet to the server.. someone told me to hook my program using the ws_32 hook and dll can someone give me or teach me how to do these codes? the client i need to attach to is "KhanClient.exe" i hope you can help me..
[C++] winsock ws_32.dll hook
06/05/2009 - C/C++ - 5 Replies
i am trying to make a hack for a online game. but i don't know how to hook my program to the process name : khanclient.exe can someone help me? this is my current code in VB2008 the point in this is i want to click a button and send a packet to server from client. i hope you get what i mean. here is a screenshot:



All times are GMT +1. The time now is 20:40.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.