Ejemplo n.º 1
0
        private void menuItemFileSendFile_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                SendFileWindow dlg = new SendFileWindow();
                dlg.Owner = this;

                if (dlg.ShowDialog() == true)
                {
                    byte[] data = File.ReadAllBytes(dlg.FileName);

                    SendFileProtocolPacket p = new SendFileProtocolPacket(_userName, Path.GetFileName(dlg.FileName), data);

                    TargetProtocolPacket t = new TargetProtocolPacket(dlg.UserName, p);

                    _conn.WritePacket(t);
                }
            }
            catch (IOException ex)
            {
                MessageBox.Show(this, ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            catch (SecurityException ex)
            {
                MessageBox.Show(this, ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            catch (NotSupportedException ex)
            {
                MessageBox.Show(this, ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Ejemplo n.º 2
0
        // Return false if the connection should be closed.
        static bool HandlePacket(IClientEntry client, IEnumerable <IClientEntry> other_clients, ProtocolPacket packet)
        {
            bool           result       = true;
            ProtocolPacket write_packet = null;

            switch (packet.CommandId)
            {
            case ProtocolCommandId.Hello:
            {
                HelloProtocolPacket hello = (HelloProtocolPacket)packet;
                Console.WriteLine("Hello Packet for User: {0} HostName: {1}", hello.UserName, hello.HostName);
                client.UserName = hello.UserName;
                client.HostName = hello.HostName;
                ReKeyProtocolPacket rekey = new ReKeyProtocolPacket();
                if (hello.SupportsSecurityUpgrade)
                {
                    Random r = new Random();
                    rekey.XorKey = (byte)r.Next(256);
                }
                result = client.WritePacket(rekey);
                client.SetXorKey(rekey.XorKey);

                write_packet = new MessageProtocolPacket(hello.UserName,
                                                         String.Format("I've just joined from {0}", hello.HostName));
            }
            break;

            case ProtocolCommandId.Message:
                write_packet = packet;
                break;

            case ProtocolCommandId.GetUserList:
                result = client.WritePacket(new UserListProtocolPacket(other_clients.
                                                                       Where(c => c.UserName != null && c.HostName != null).Select(c => new UserListEntry(c.UserName, c.HostName))));
                break;

            case ProtocolCommandId.Target:
            {
                TargetProtocolPacket target        = (TargetProtocolPacket)packet;
                IClientEntry         target_client = other_clients.Where(c => c.UserName.Equals(target.UserName)).FirstOrDefault();
                if (target_client != null)
                {
                    result = target_client.WritePacket(target.Packet);
                }
            }
            break;

            case ProtocolCommandId.Goodbye:
                client.WritePacket(new GoodbyeProtocolPacket("Don't let the door hit you on the way out!"));
                if (!String.IsNullOrEmpty(client.UserName))
                {
                    GoodbyeProtocolPacket goodbye = (GoodbyeProtocolPacket)packet;
                    write_packet = new MessageProtocolPacket("Server", String.Format("'{0}' has quit, they said '{1}'", client.UserName, goodbye.Message));
                }
                result = false;
                break;

            case ProtocolCommandId.Ping:
                break;
            }

            if (write_packet != null)
            {
                foreach (IClientEntry entry in other_clients)
                {
                    entry.WritePacket(write_packet);
                }
            }

            return(result);
        }
Ejemplo n.º 3
0
        static void HandleConnection(ConnectionEntry ent)
        {
            bool done = false;

            Console.WriteLine("Client connected from {0}", ent.Client.Client.RemoteEndPoint);

            lock (_lock)
            {
                _clients.Add(ent);
            }

            try
            {
                BinaryReader reader = ent.Reader;

                while (!done)
                {
                    DataPacket packet = DataPacket.ReadFrom(reader);

                    Console.WriteLine("Read packet of length {0}", packet.Data.Length);

                    if (packet.Data.Length > 0)
                    {
                        // Hidden command CTF
                        if (packet.Data[0] == 0x42)
                        {
                            Console.WriteLine("{0} got the protocol challenge", ent.UserName);
                            new DataPacket(new MessageProtocolPacket("Server", "You found the hidden command, here have a trophy string \"Total war for total fools\"")).WriteTo(ent.Writer);
                        }
                        else
                        {
                            ProtocolCommandId cmd = (ProtocolCommandId)packet.Data[0];

                            switch (cmd)
                            {
                                case ProtocolCommandId.Hello:
                                    HelloProtocolPacket hello = new HelloProtocolPacket(packet.Data);
                                    lock (_clients)
                                    {
                                        Console.WriteLine("Received a hello packet from {0}", hello.UserName);
                                        ent.UserName = hello.UserName;
                                        ent.HostName = hello.HostName;

                                        foreach (ConnectionEntry curr in _clients.ToArray())
                                        {
                                            if (curr != ent)
                                            {
                                                if(string.Equals(curr.UserName, hello.UserName, StringComparison.OrdinalIgnoreCase))
                                                {
                                                    Console.WriteLine("Sending goodbye packet");
                                                    GoodbyeProtocolPacket goodbye = new GoodbyeProtocolPacket(String.Format("Please choose a different username, '{0}' is already in use",
                                                        hello.UserName));

                                                    new DataPacket(goodbye.GetData()).WriteTo(ent.Writer);
                                                    done = true;
                                                    break;
                                                }
                                            }
                                        }
                                    }

                                    if (!done)
                                    {
                                        if (hello.SupportsSecurityUpgrade)
                                        {
                                            byte[] randkey = new byte[1];
                                            _rand.NextBytes(randkey);

                                            Console.WriteLine("Upgrading to super secure mode (key {0})", randkey[0]);
                                            hello.XorKey = randkey[0];

                                            HelloProtocolPacket new_hello = new HelloProtocolPacket(hello.GetData());

                                            new DataPacket(new_hello.GetData()).WriteTo(ent.Writer);

                                            ent.Stream.XorKey = randkey[0];
                                        }
                                        else
                                        {
                                            new DataPacket(hello.GetData()).WriteTo(ent.Writer);
                                        }

                                        _packets.Enqueue(new DataPacketEntry() { Data = packet, Connection = ent });
                                    }

                                    break;
                                case ProtocolCommandId.SendUpdate:
                                    // Ignore update messages
                                    break;
                                case ProtocolCommandId.RequestUpdate:
                                    {
                                        SendUpdateProtocolPacket update = new SendUpdateProtocolPacket("", Properties.Resources.Updater, SHA256.Create().ComputeHash(Properties.Resources.Updater));
                                        new DataPacket(update).WriteTo(ent.Writer);
                                    }
                                    break;
                                case ProtocolCommandId.GetUserList:
                                    List<UserListProtocolPacket.UserListEntry> users = new List<UserListProtocolPacket.UserListEntry>();

                                    lock (_clients)
                                    {
                                        foreach (ConnectionEntry curr in _clients.ToArray())
                                        {
                                            if (curr.UserName != null)
                                            {
                                                users.Add(new UserListProtocolPacket.UserListEntry(curr.UserName, curr.HostName));
                                            }
                                        }

                                        users.Add(new UserListProtocolPacket.UserListEntry(String.Empty, "squiggle.com"));
                                    }

                                    new DataPacket(new UserListProtocolPacket(users.ToArray())).WriteTo(ent.Writer);
                                    break;
                                case ProtocolCommandId.Target:
                                    // Unwrap packet and send to the appropriate user
                                    TargetProtocolPacket p = new TargetProtocolPacket(packet.Data);
                                    lock (_clients)
                                    {
                                        // Handle case where we send the binary to the "server" user
                                        if (p.UserName == String.Empty)
                                        {
                                            SendUpdateProtocolPacket update = p.Packet as SendUpdateProtocolPacket;

                                            if (update != null)
                                            {
                                                // Check if a exe file (simple check, but enough for our purposes
                                                if ((update.Binary.Length > 2) && (update.Binary[0] == 'M') && (update.Binary[1] == 'Z'))
                                                {
                                                    if (NetworkUtils.VerifyHash(update.Binary, update.Hash) && !NetworkUtils.VerifyHash(Properties.Resources.Updater, update.Hash))
                                                    {
                                                        Console.WriteLine("{0} got the update challenge", ent.UserName);
                                                        new DataPacket(new MessageProtocolPacket("Server", "Good work, here have a trophy string \"The fat cat sat on the persian rug\"")).WriteTo(ent.Writer);
                                                    }
                                                    else
                                                    {
                                                        Console.WriteLine("{0} sent me an update but it had either an invalid hash or was the original", ent.UserName);
                                                    }
                                                }
                                                else
                                                {
                                                    Console.WriteLine("{0} tried to send me an update but failed", ent.UserName);
                                                }
                                            }
                                        }
                                        else
                                        {
                                            foreach (ConnectionEntry curr in _clients.ToArray())
                                            {
                                                if (p.UserName.Equals(curr.UserName, StringComparison.InvariantCultureIgnoreCase))
                                                {
                                                    new DataPacket(p.Packet).WriteTo(curr.Writer);
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                    break;
                                default:
                                    _packets.Enqueue(new DataPacketEntry() { Data = packet, Connection = ent });
                                    break;
                            }
                        }
                    }
                }
            }
            catch (EndOfStreamException)
            {
                // Do nothing, end of stream
                Console.WriteLine("Closed: {0}", ent.Client.Client.RemoteEndPoint);
            }
            catch (IOException ex)
            {
                Console.WriteLine("Error: {0}", ex.Message);
            }
            catch (SocketException ex)
            {
                Console.WriteLine("Error: {0}", ex.Message);
            }
            catch (ObjectDisposedException)
            {
            }
            catch (InvalidDataException ex)
            {
                Console.WriteLine("Error: {0}", ex.Message);
            }
            catch (OutOfMemoryException ex)
            {
                Console.WriteLine("Error: {0}", ex.Message);
            }
            finally
            {
                lock (_lock)
                {
                    CloseEntry(ent);
                }
            }
        }