コード例 #1
0
        internal static int repl_recv(IntPtr socket, IntPtr pBuffer, int len, int pFlags)
        {
            RecvHook.Suspend();

            var length = recv(socket, pBuffer, len, pFlags);

            if (length <= 0)
            {
                RecvHook.Continue();
                return(length);
            }

            var buffer = pBuffer.Copy(0, length);

            var packet = new Packet()
            {
                Channel     = PacketChannel.Recv,
                Buffer      = buffer,
                Source      = socket.GetSourceIPEndPoint(),
                Destination = socket.GetDestinationIPEndPoint(),
                Length      = length,
                Socket      = (int)socket
            };

            try {
                var response = OnReceivePacket(packet);

                if (!response.Filter)
                {
                    goto finish;
                }

                if (response.Buffer.FastSequenceEquals(buffer))
                {
                    goto finish;
                }

                NetworkDetourExtensions.WriteProcessMemory(Process.GetCurrentProcess().Handle, pBuffer, response.Buffer, (uint)response.Buffer.Length, out var bytesWritten);

                RecvHook.Continue();
                return(response.Buffer.Length);
            }
            catch { } // TODO: whenever an exception occurs, log the exception for the plugin.

finish:
            RecvHook.Continue();
            return(length);
        }
コード例 #2
0
        internal static int repl_send(IntPtr socket, IntPtr pBuffer, int len, int pFlags)
        {
            SendHook.Suspend();

            var buffer = pBuffer.Copy(0, len);
            var length = -1;

            var packet = new Packet()
            {
                Channel     = PacketChannel.Send,
                Buffer      = buffer,
                Source      = socket.GetSourceIPEndPoint(),
                Destination = socket.GetDestinationIPEndPoint(),
                Length      = len,
                Socket      = (int)socket
            };

            try {
                var response = OnReceivePacket(packet);

                if (!response.Filter)
                {
                    goto finish;
                }

                if (response.Buffer.FastSequenceEquals(buffer))
                {
                    goto finish;
                }

                var pinned_array     = GCHandle.Alloc(response.Buffer, GCHandleType.Pinned);
                var modified_pointer = pinned_array.AddrOfPinnedObject();

                length = send(socket, modified_pointer, response.Buffer.Length, pFlags);
                pinned_array.Free();
                return(length);
            }
            catch { } // TODO: whenever an exception occurs, log the exception for the plugin.

finish:
            length = send(socket, pBuffer, len, pFlags);
            SendHook.Continue();
            return(length);
        }
コード例 #3
0
        internal static int repl_wsa_recv(IntPtr socket, ref WSABuffer wsaBuffer, int bufferCount, out int bytesTransferred, ref int socketFlags, IntPtr overlapped, IntPtr completionRoutine)
        {
            WSARecvHook.Suspend();

            var length = WSARecv(socket, ref wsaBuffer, bufferCount, out bytesTransferred, ref socketFlags, overlapped, completionRoutine);
            var buffer = wsaBuffer.Pointer.Copy(0, wsaBuffer.Length);

            var packet = new Packet()
            {
                Channel     = PacketChannel.WSARecv,
                Buffer      = buffer,
                Source      = socket.GetSourceIPEndPoint(),
                Destination = socket.GetDestinationIPEndPoint(),
                Length      = length,
                Socket      = (int)socket
            };

            // TODO: allow modifying wsa_recv packets
            try { OnReceivePacket(packet); } catch { }

            WSARecvHook.Continue();
            return(length);
        }
コード例 #4
0
        internal static int repl_send_to(IntPtr socket, IntPtr pBuffer, int len, int socketFlags, byte[] socketAddress, int socketAddressSize)
        {
            SendToHook.Suspend();

            var length = sendto(socket, pBuffer, len, socketFlags, socketAddress, socketAddressSize);
            var buffer = pBuffer.Copy(0, length);

            var packet = new Packet()
            {
                Channel     = PacketChannel.SendTo,
                Buffer      = buffer,
                Source      = socket.GetSourceIPEndPoint(),
                Destination = socket.GetDestinationIPEndPoint(),
                Length      = length,
                Socket      = (int)socket
            };

            // TODO: allow modifying send_to packets
            try { OnReceivePacket(packet); } catch { }

            SendToHook.Continue();

            return(length);
        }