Hello guys acctually i Hooked the Send and Recv fuctions but kinda just 2 or 3 packets works fine like login , charserver when try to go MapServer it's crash analysing the packet it's are empty kinda how the Buffer is writed in another previous packet or something else. the game packets dont have encryption. will share my code here to see what can be doing wrong.
Code:
#include <cstdio>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <string>
#include <windows.h>
#include "detours.h"
//#include "WinSock2.h"
// DBO int __stdcall recv(SOCKET s, char *buf, int len, int flags);
// DBO int __stdcall send(SOCKET s, const char *buf, int len, int flags);
#pragma comment( lib, "Ws2_32.lib" )
#pragma comment( lib, "detours.lib" )
PVOID Original_WinSock_Send_Function = (PVOID)0x579414;
PVOID Original_WinSock_Recv_Function = (PVOID)0x579418;
int (WINAPI *Original_Send)(SOCKET s, const char *buf, int len, int flags) = NULL;//send;
int (WINAPI *Original_Recv)(SOCKET s, char *buf, int len, int flags) = NULL;//recv;
int WINAPI Hooked_Send(SOCKET s, const char* buf, int len, int flags);
int WINAPI Hooked_Recv(SOCKET s, char *buf, int len, int flags);
char dp[64000] = { 0 };
char * packet_to_text(char* buf, int len)
{
int c, c2, c3, c4;
c = c2 = c3 = c4 = 0;
sprintf(&dp[c2++], "\n");
for (c = 0; c<len; c++)
{
if (c3 == 16)
{
for (; c4<c; c4++)
if (buf[c4] >= 0x20)
dp[c2++] = buf[c4];
else
dp[c2++] = 0x002E;
c3 = 0;
sprintf(&dp[c2++], "\n");
}
if ((c == 0) || !(c % 16))
{
sprintf(&dp[c2], "(%04X) ", c);
c2 += 7;
}
sprintf(&dp[c2], "%02X ", buf[c]);
c2 += 3;
c3++;
}
if (len % 16)
{
c3 = len;
while (c3 % 16)
{
sprintf(&dp[c2], " ");
c2 += 3;
c3++;
}
}
for (; c4<c; c4++)
if (buf[c4] >= 0x20)
dp[c2++] = buf[c4];
else
dp[c2++] = 0x2E;
sprintf(&dp[c2++], "\n");
dp[c2] = 0;
return (char*)&dp[0];
}
void WriteLog(const char* fl, const char* fmt, ...)
{
va_list args; // you using unicode or ascii? ascii
char text[4096];
char logbuf[4096];
char buf[4096];
SYSTEMTIME rawtime;
FILE *fp;
GetLocalTime(&rawtime);
va_start(args, fmt);
vsprintf(text, fmt, args);
va_end(args);
strcat(text, "\n");
sprintf(&logbuf[0], "log\\%s%02u%02u%04u.log", fl, rawtime.wMonth, rawtime.wDay, rawtime.wYear);
sprintf(&buf[0], "[%02u-%02u-%u, %02u:%02u:%02u] %s", rawtime.wMonth, rawtime.wDay, rawtime.wYear,
rawtime.wHour, rawtime.wMinute, rawtime.wSecond, text);
fp = fopen(&logbuf[0], "a");
if (!fp)
{
return;
}
else
{
fprintf(fp, buf);
fclose(fp);
}
}
int WINAPI Hooked_Recv(SOCKET s, char *buf, int len, int flags)
{
//My recv function
char temp[40] = { 0 };
//sprintf(&temp[0], "Buffer Pointer: 0x%04x", buf);
//MessageBox(NULL, temp, temp, 0);
WriteLog("packet" /* File Name will be packet_datestamp.log in a folder called log */, packet_to_text(buf, len));
return Original_Recv(s, buf, len, flags);
}
// now i just need to compile this into DLL ?
int WINAPI Hooked_Send(SOCKET s, const char *buf, int len, int flags) {
//My send function
return Original_Send(s, buf, len, flags);
}
BOOL WINAPI DllMain(HINSTANCE, DWORD dwReason, LPVOID) {
switch (dwReason) {
case DLL_PROCESS_ATTACH:
AllocConsole();
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
Original_Send = (int (WINAPI *)(SOCKET s, const char *buf, int len, int flags))DetourFindFunction("wsock32.dll", "send");
Original_Recv = (int (WINAPI *)(SOCKET s, char *buf, int len, int flags))DetourFindFunction("wsock32.dll", "recv");
if (!Original_Send) MessageBox(NULL, "Send", "Broke", 0);
if (!Original_Recv) MessageBox(NULL, "Recv", "Broke", 0);
//DetourAttach(&(PVOID &)Original_WinSock_Send_Function, Hooked_WinSock_Send_Function);
DetourAttach(&(PVOID &)Original_Send, Hooked_Send);
//DetourAttach(&(PVOID &)Original_WinSock_Recv_Function, Hooked_WinSock_Recv_Function);
DetourAttach(&(PVOID &)Original_Recv, Hooked_Recv);
DetourTransactionCommit();
break;
case DLL_PROCESS_DETACH:
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach(&(PVOID &)Original_Send, Hooked_Send);
DetourDetach(&(PVOID &)Original_Recv, Hooked_Recv);
DetourTransactionCommit();
break;
}
return TRUE;
}