Get socket handle to use for comparison later

358 Views Asked by At

I hooked some socket function of a game. Then can get the receive and sent data from sockets inside that game. The problem is: There are more then 1 socket.

How could I get the handle of the FIRST socket created? I hooked the function SOCKET, like this:

SOCKET GameMainSocket;

SOCKET _stdcall WSAAPI nSocket(int af,int type,int protocol)
{
     UnHookFunction("ws2_32.dll", "socket", KSocketHook);
     GameMainSocket = socket(af, type, protocol);
     HookFunction("ws2_32.dll", "socket", (LPVOID*) nSocket, KSocketHook);
     return GameMainSocket;
}

But then, later, when I try to compare it within hooked send and recv function, like this:

int __stdcall nSend(SOCKET s, const char *buf, int len,int flags)
{ 
  if (s = GameMainSocket)
  {
    // Allow send
  }
}

The code is just skipped and all the checks are true.

** My real quetion is: How could I identifie each socket created by an application?

Thanks in advance!

PROBLEM FULLY SOLVED.

My code now is:

if (s == GameMainSocket) 
{
// The magic goes here, encrypt packet with XOR (server does the same)
    char* buf2 = (char*) malloc (len);
    memcpy(buf2, buf, len);
    //buf2[0] = buf2[0];
    buf2[0] = buf2[0] ^ int("x") % 255;

}
1

There are 1 best solutions below

5
On BEST ANSWER
if (s = GameMainSocket)

is doing assignment which will return the assigned value, which will be true if it is not 0. Did you mean to do the following?

if (s == GameMainSocket)