Register for your free account! | Forgot your password?

Go Back   elitepvpers > Popular Games > Silkroad Online > SRO Coding Corner
You last visited: Today at 07:24

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

Advertisement



C++ hook wsarecv and wsasend

Discussion on C++ hook wsarecv and wsasend within the SRO Coding Corner forum part of the Silkroad Online category.

Reply
 
Old   #1
 
vitalka's Avatar
 
elite*gold: 0
Join Date: May 2007
Posts: 160
Received Thanks: 23
C++ hook wsarecv and wsasend

Hey guys i dont know why my source doesnt work. Whats the mistake?


Code:
#include "stdio.h"
#include "winsock2.h"
#include "windows.h"
#include <iostream>
#include <commctrl.h>
#include <time.h>

using namespace std;

#pragma comment(lib, "ws2_32.lib")

typedef int (WINAPI* t_WSARecv)(SOCKET,LPWSABUF,DWORD,LPDWORD,LPDWORD,LPWSAOVERLAPPED,LPWSAOVERLAPPED_COMPLETION_ROUTINE);
typedef int (WINAPI* t_WSASend)(SOCKET,LPWSABUF,DWORD,LPDWORD,DWORD,LPWSAOVERLAPPED,LPWSAOVERLAPPED_COMPLETION_ROUTINE );

t_WSASend o_WSASend;
t_WSARecv o_WSARecv;


void *DetourFunction(BYTE *src, const BYTE *dst, const int len) // credits to gamedeception
{
	BYTE *jmp = (BYTE*)malloc(len+5);
	DWORD dwback;
	VirtualProtect(src, len, PAGE_READWRITE, &dwback);
	memcpy(jmp, src, len); jmp += len;
	jmp[0] = 0xE9;
	*(DWORD*)(jmp+1) = (DWORD)(src+len - jmp) - 5;
	src[0] = 0xE9;
	*(DWORD*)(src+1) = (DWORD)(dst - src) - 5;
	VirtualProtect(src, len, dwback, &dwback);
	return (jmp-len);
}


int WINAPI hook_WSARecv(SOCKET s,LPWSABUF lpBuffers,DWORD dwBufferCount,LPDWORD lpNumberOfBytesRecvd,LPDWORD lpFlags,LPWSAOVERLAPPED lpOverlapped,LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
{
	MessageBox(HWND_DESKTOP,L"Blabla",L"Titel",MB_OK);
	return o_WSARecv(s,lpBuffers,dwBufferCount,lpNumberOfBytesRecvd,lpFlags,lpOverlapped,lpCompletionRoutine);
}

int WINAPI hook_WSASend(SOCKET s,LPWSABUF lpBuffers,DWORD dwBufferCount,LPDWORD lpNumberOfBytesSent,DWORD dwFlags,LPWSAOVERLAPPED lpOverlapped,LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
{
	MessageBox(HWND_DESKTOP,L"Blabla",L"Titel",MB_OK);
	return o_WSASend(s,lpBuffers,dwBufferCount,lpNumberOfBytesSent,dwFlags,lpOverlapped,lpCompletionRoutine);
}

BOOL APIENTRY DllMain(HMODULE hModule, DWORD Ergebnis, LPVOID lpReserved)
{
    UNREFERENCED_PARAMETER(lpReserved);
  switch(Ergebnis)
  {
  case DLL_PROCESS_ATTACH:

        DisableThreadLibraryCalls(hModule);

		o_WSASend  = (t_WSASend)DetourFunction((PBYTE)GetProcAddress(GetModuleHandle(L"ws2_32.dll"), "WSASend"), (PBYTE)hook_WSASend,5);
        o_WSARecv  = (t_WSARecv)DetourFunction((PBYTE)GetProcAddress(GetModuleHandle(L"ws2_32.dll"), "WSARecv"), (PBYTE)hook_WSARecv,5);
          
		break;
  }
    return true;
}
vitalka is offline  
Old 01/21/2012, 22:47   #2

 
elite*gold: 260
Join Date: Aug 2008
Posts: 560
Received Thanks: 3,780
You don't really want to hook WSASend/WSARecv in Silkroad. Silkroad uses , which means the data you actually send and receive will most likely be processed later, rather than at the time of the API call.

To properly process the API results, you'd have to also hook and process and possibly , keeping track of the lpOverlapped parameter so you know which event is a read/write/something else.

You should really just detour connect and detour to a proxy, either external or internal (via an injected dll) so you don't have to mess with this stuff. That's been the way to go about things since 2006, so no sense in not doing it.

If you still want to hook raw send/recv functions, you need to be hooking client code and not the WSASend/WSARecv API calls. Otherwise, between threading issues, non-networking code using the same mechanics, you will have a hard time getting something that will always give the correct results.
pushedx is offline  
Old 01/22/2012, 11:37   #3
 
vitalka's Avatar
 
elite*gold: 0
Join Date: May 2007
Posts: 160
Received Thanks: 23
Its not really for Silkroad i dont want to code anymore for Silkroad, but in this section are some people who have much knowledge.
So the hook works for now, but the send funktion working only time per hook so after one send i get a windows error, but why?

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;
SOCKADDR_IN addr;


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);
		memset(&addr,0,sizeof(SOCKADDR_IN)); // zuerst alles auf 0 setzten 
        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;
}
vitalka is offline  
Old 01/23/2012, 08:13   #4
 
lesderid's Avatar
 
elite*gold: 0
Join Date: Dec 2007
Posts: 2,400
Received Thanks: 1,517
Quote:
Originally Posted by vitalka View Post
Its not really for Silkroad i dont want to code anymore for Silkroad, but in this section are some people who have much knowledge.
So the hook works for now, but the send funktion working only time per hook so after one send i get a windows error, but why?

Code:
//...
Use this:
Code:
int WINAPI my_send(SOCKET socket, const char* buffer, int length, int flags) 
{
	o_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) 
{
	o_send(Bot, buffer, length, flags); //And shouldn't this be o_recv?
	return o_recv(socket, buffer, length, flags);
}
lesderid is offline  
Thanks
1 User
Reply


Similar Threads Similar Threads
[Help] WSARecv Hook
08/26/2011 - General Coding - 8 Replies
Ich hooke mit Microsoft's Detour Library die Funktion WSASend & WSARecv. Die Packet-Auswertung erfolgt bei WSASend ohne Probleme, aber bei WSARecv kommen oft nur 0'en an. Weiß jemand vielleicht warum?
<26.06.11> Chico™ Public Hook, //Invisible//Chams /Stamina/Mini Hook/NO CRASH VERSION
07/08/2011 - WarRock Hacks, Bots, Cheats & Exploits - 46 Replies
26.06.11 Hack Released 26.06.11 SuperNoSpread Problem Fixxen Status= Not in Hack Hej Com, leider hab ich eine schlechte nachricht xP^^ aus egendeinen Grund verursacht bei mir SuperNoSpread einen Direkten Crash sobald man in game ist, deshalb habe ich es entfernt O,o der Hack ist tortzdem nett und wird euch gefallen, sobald ich mehr addys habe kommen mehrere funktionen noch dazu ;) http://img4.fotos-hochladen.net/uploads/public24e lfvtrw1u.png STOP! VOR DEM DOWNLOAD! Mit Dem...
WSAsend/WSABUF
08/14/2009 - General Coding - 2 Replies
hiho =) i'm reversing a mmo which uses WSAsend to send it's packets WSASend Function (Windows) as you can see the parameter lpBuffers holds the adress to an array of WSABUF structs WSABUF Structure (Windows) ..which contains length and data i'm trying to read the data of wsabuf -> but this differs from my parallel collected packet in WPE ->only the size of the packet is correct, the rest is rubbish =/



All times are GMT +1. The time now is 07:25.


Powered by vBulletin®
Copyright ©2000 - 2026, 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 ©2026 elitepvpers All Rights Reserved.