views:

226

answers:

2

Hello. I am using Nektra's Deviare to hook winsock's send method calls. My ideia is to learn to also send messages through the same socket than the original application. So what I'm doing is when i detect the first call to send message, I save the socket id(the first argument of the send function), so I can use it later.

So here is my code:

    uint socket = 0;

[DllImport("Ws2_32.dll")]
private static extern int send(uint socket, string buf, int len, int flags);

void _proxy_OnFunctionCalled(DeviareTools.IProcess proc, DeviareParams.ICallInfo callInfo, Deviare.IRemoteCall rCall)
{
   socket = (uint)callInfo.Params.get_Item(0).Value;
}

and later

    int ret = send(socket, "ABC", 3, 0);
    MessageBox.Show(ret.ToString());

This last messagebox is poping up always -1. Why should be it?

Thanks

edit: calling WSAGetLastError() returns 2. Which I do not know what it means, as it doesn't seem to appear on msdn.

+1  A: 

Try doing

MessageBox.Show(socket.ToString());

as well and make sure your socket variable got set.

I believe error 2 is "file not found", which would suggest to me that you have an invalid socket id, but I could be wrong about that.

Eddie Deyo
It has some value, 1564, right now.
devoured elysium
Well I set socket value to 123 and it pops up the same error. Strange. So maybe it has something to do with the socket id not being valid? I don't get why.
devoured elysium
A: 

WinSock must be initialized, a Socket must be created, a connection must be made (unless your socket is a SOCK_DGRAM), so than you can send() anything. Besides that, API functions are meant to work with data from (char *) pointers, ensure that it is happening. Maybe "ABC" is being declared as a WIDECHAR array, a String object or something not a (char *) buffer.

There's a full exemple of a basic socket connection here in MSDN: http://msdn.microsoft.com/en-us/library/ms737625%28VS.85%29.aspx

Havenard
I think I do not have to initialize anything, remember I am using the ID of a socket that's already been created.
devoured elysium
Yes, but I think you are not getting the point. I am not asking how to send info with sockets, I am asking hwo to send info after I have the ID of the socket that I got hooking another application.
devoured elysium
It should work pretty fine, if it were a real and ready to to send data socket. Maybe you're doing something else wrong. I rather build my own methods to APIHook stuff, actually I've already made security filters and anti-cheating solutions for game servers without having access to their code, only using APIHook.
Havenard