Example #1
0
        public override byte[] ParseIncomingSSL(int read, byte[] initialBuffer, SslStream stream, ClientContext context)
        {
            Stats.AddBytes(read, ByteType.Received);
            Stats.AddPacket(PacketType.Received);

            if (read != InitialBufferSize)
            {
                Logger.Log("Initial buffer incorrect size", Severity.Warning);
                return(null);
            }

            if (initialBuffer[0] == 0x02 && initialBuffer[1] == 0x07)
            {
                short int16 = BitConverter.ToInt16(initialBuffer, 2);
                if (Enum.TryParse(int16.ToString(), out PierceHeader header))
                {
                    int len = BitConverter.ToInt32(initialBuffer, 8);

                    //Logger.Log(header.ToString(), Severity.Info);
                    //Logger.Log("Length: " + len, Severity.Info);

                    var list = new List <byte>();
                    list.AddRange(initialBuffer);

                    var payload = new byte[len];

                    //int readback = stream.Read(payload, 0, len);

                    int bytesread = 0;
                    do
                    {
                        int readback = stream.Read(payload, bytesread, len - bytesread);
                        Stats.AddBytes(readback, ByteType.Received);
                        bytesread += readback;
                    } while (bytesread < len);


                    list.AddRange(payload);


                    if (bytesread != len)
                    {
                        Logger.Log("Full message not read or too much read!", Severity.Warning);
                        return(null);
                    }

                    var endbit  = new byte[4];
                    var endread = stream.Read(endbit, 0, 4);
                    Stats.AddBytes(endread, ByteType.Received);
                    if (endread != 4)
                    {
                        Logger.Log("End bit is wrong length!", Severity.Warning);
                        return(null);
                    }

                    list.AddRange(endbit);

                    PierceMessage message = PierceMessage.Parse(list.ToArray());

                    //Logger.Log("Message: " + message, Severity.Info);


                    //Logger.Log(BitConverter.ToString(payload), Severity.Info);

                    switch (message.Header)
                    {
                    case PierceHeader.Invalid:
                        Logger.Log("Invalid header received (0x00)", Severity.Warning);
                        return(null);

                    case PierceHeader.Handshake:
                        Logger.Log("Handshake received, version " + BitConverter.ToString(payload), Severity.Info);

                        uint id = (uint)new Random().Next();
                        while (RemoteClientList.ContainsKey(id))
                        {
                            id = (uint)new Random().Next();
                        }

                        if (!RemoteClientList.TryAdd(id, new RemoteClientInfo(id, context)))
                        {
                            throw new Exception("Conflicting key on creation, this is impossible");
                        }


                        var pierceMessage = new PierceMessage(PierceHeader.HandshakeOK);
                        pierceMessage.Payload = BitConverter.GetBytes(id);
                        byte[] bytes = pierceMessage.MakeParcel();
                        stream.Write(bytes);

                        // return back to server for stat collection
                        return(list.ToArray());

                    case PierceHeader.HandshakeOK:
                        break;

                    case PierceHeader.Message:
                        break;

                    case PierceHeader.ScreenShot:

                        break;

                    case PierceHeader.RemoteDeskRequest:
                        OnRemoteDeskRequestReceived(null);
                        break;

                    case PierceHeader.Socks5:
                        var sockParcel = SockParcel.DeSerialize(message.Payload);
                        if (RemoteClientList.ContainsKey(message.SenderId))
                        {
                            OnSockParcelReceived(RemoteClientList[message.SenderId], sockParcel);
                        }
                        else
                        {
                            Logger.Log(message.SenderId + " is an unrecognized Client ID", Severity.Warning);
                        }

                        break;

                    case PierceHeader.RoundTrip:

                        if (RemoteClientList.ContainsKey(message.SenderId))
                        {
                            OnRoundTripReceived(RemoteClientList[message.SenderId], message.Payload);
                        }
                        else
                        {
                            Logger.Log(message.SenderId + " is an unrecognized Client ID", Severity.Warning);
                        }

                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }
                else
                {
                    Logger.Log("Invalid header received", Severity.Warning);
                    return(null);
                }
            }
            else
            {
                Logger.Log("Invalid magic bytes received", Severity.Warning);
                return(null);
            }

            return(initialBuffer);
        }
Example #2
0
        private void ReadCallBack(IAsyncResult ar)
        {
            Stats.AddPacket(PacketType.Received);
            var state = (StateObject)ar.AsyncState;

            int bytesRead;

            try
            {
                bytesRead = state.ssl.EndRead(ar);
            }
            catch (IOException e)
            {
                // terminated connection

                Logger.Log("Connection terminated by server", Severity.Warning);
                Reconnect();
                return;
            }

            if (bytesRead == 0)
            {
                Logger.Log("0 bytes read, terminating", Severity.Warning);
                Reconnect();
                return;
            }

            Stats.AddBytes(bytesRead, ByteType.Received);
            state.ms.Write(state.buffer, 0, bytesRead);

            while (!PierceMessage.CheckMessageComplete(state.ms.ToArray()))
            {
                var readbuffer = new byte[1024];
                try
                {
                    bytesRead = state.ssl.Read(readbuffer, 0, readbuffer.Length);
                    Stats.AddBytes(bytesRead, ByteType.Received);
                    state.ms.Write(readbuffer, 0, bytesRead);
                }
                catch (IOException e)
                {
                    Logger.Log("Error while receiving PierceMessage: " + e.Message, Severity.Warning);
                    Reconnect();
                    return;
                }
                catch (SocketException e)
                {
                    Logger.Log("Error while receiving PierceMessage: " + e.Message, Severity.Warning);
                    Reconnect();
                    return;
                }
                catch (ObjectDisposedException e)
                {
                    Logger.Log("Error while receiving PierceMessage: " + e.Message, Severity.Warning);
                    Reconnect();
                    return;
                }
            }

            PierceMessage message = PierceMessage.Parse(state.ms.ToArray());

            //Logger.Log("Client rec: " + message, Severity.Info);

            if (string.IsNullOrEmpty(message.ParseError))
            {
                switch (message.Header)
                {
                case PierceHeader.HandshakeOK:
                    _id       = BitConverter.ToUInt32(message.Payload, 0);
                    Connected = true;
                    Logger.Log("Handshake OK, client id " + _id, Severity.Info);
                    break;

                case PierceHeader.RemoteDeskRequest:
                    // nein
                    break;

                case PierceHeader.ScreenShot:

                    break;


                case PierceHeader.Invalid:
                    break;

                case PierceHeader.Handshake:
                    break;

                case PierceHeader.Message:
                    break;

                case PierceHeader.Socks5:
                    var sockParcel = SockParcel.DeSerialize(message.Payload);
                    OnSockParcelReceived(sockParcel);
                    break;

                case PierceHeader.RoundTrip:
                    OnRoundTripReturn(message.Payload);
                    break;

                case PierceHeader.Object:
                    OnObjectReceived(message);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }


            // start listening again with new object
            StateObject newstate = new StateObject(state.ssl);

            state.ssl.BeginRead(newstate.buffer, 0, StateObject.BufferSize, ReadCallBack, newstate);
        }