C++ Get Handle of Open Sockets of a Program

前端 未结 2 1103
夕颜
夕颜 2020-12-03 17:29

How is it possible to get the Socket ID (Handle) of the created sockets of a program?

I know I can get all the open sockets in all programs by GetTcpTable()

2条回答
  •  醉话见心
    2020-12-03 18:02

    Ok, thanks to everyone that tried to solve my problem
    After a lot of works I get how to handle it myself, this is how i tried to get the specified socket :

    • At the first I looked in to program's disassembly and find out the calls to WS2_32 Send function.

    Disassembly Code

    As the picture show there is a call to Socket send function at 0x467781 and the Socket handle saved to the stack in the EDX register

    • Now what i need to do is to Hook my code in to that function.

      void GetSocket(int Flag,int DataSize, char* Data, SOCKET Socket)
       {
           sSocket = Socket;
           sFlag = Flag;
           sDataSize = DataSize;
           sData = Data;
           SendPacket(sSocket,Data,DataSize); //Send packets manually
       }
      
      __declspec(naked) void MyFunc()
       {
          __asm
            {
                 PUSH EDX // Socket
                 PUSH ECX // Buffer
                 PUSH EAX // Buffer Size
                 PUSH 0   // Flag
                 CALL GetSocket
                 MOV EAX, sDataSize
                 MOV ECX, sData
                 MOV EDX, sWowSocket
                 JMP [JumpAddress] // JumpAddress = 0x467787 (After that CALL)
             }
       }
      

      And now i all have to do is to change that CALL (in 0x467781) to a JMP to our function(MyFunc) and it can be done with the following function :

      *(DWORD*)   (0x467781  + 0x01)  =   (DWORD)MyFunc- (0x467781  + 0x05);
      

    Now I'm done,I can easily see each packet that it sends to server and change them if necessary and also send my custom packets whit its Socket :)

提交回复
热议问题