Example #1
0
        /// <summary>
        /// Decrypts/Encrypts all account files (depending on DPAPI setting)
        /// </summary>
        /// <returns></returns>
        public bool UpdateDPAPI()
        {
            credMan = CredManifest.GetManifest();
            if (UseMaFiles)
            {
                string maDir = GetExecutableDir() + "/maFiles/";
                for (int i = 0; i < Entries.Count; i++)
                {
                    ManifestEntry entry    = Entries[i];
                    string        filename = maDir + entry.FileName;
                    if (!File.Exists(filename))
                    {
                        continue;
                    }

                    string fileContents = File.ReadAllText(filename);

                    fileContents = Encryptor.DPAPIUnprotect(fileContents, Encryptor.AccountEntropy);

                    string toWriteFileContents = fileContents;

                    if (UseDPAPI == true)
                    {
                        toWriteFileContents = Encryptor.DPAPIProtect(toWriteFileContents, Encryptor.AccountEntropy);
                    }

                    File.WriteAllText(filename, toWriteFileContents);
                    if (UseWindowsFileEncryption)
                    {
                        File.Encrypt(filename);
                    }
                    else
                    {
                        File.Decrypt(filename);
                    }
                }
            }
            else
            {
                foreach (CredManifestEntry entry in credMan.Entries)
                {
                    string fileContents = entry.Contents;

                    string toWriteFileContents = Encryptor.DPAPIUnprotect(fileContents, Encryptor.AccountEntropy);

                    if (UseDPAPI)
                    {
                        toWriteFileContents = Encryptor.DPAPIProtect(toWriteFileContents, Encryptor.AccountEntropy);
                    }

                    entry.Contents = toWriteFileContents;
                }
                credMan.Save();
            }

            Save();

            return(true);
        }
Example #2
0
        public bool Save()
        {
            string maDir       = GetExecutableDir() + @"\maFiles\";
            string filename    = maDir + "manifest.json";
            bool   notFirstRun = true;

            if (!Directory.Exists(maDir))
            {
                try
                {
                    notFirstRun = false;
                    Directory.CreateDirectory(maDir);
                }
                catch (Exception)
                {
                    return(false);
                }
            }
            try
            {
                try
                {
                    if (!UseMaFiles && Entries.Count > 0 && credMan.Entries.Count < Entries.Count) // Move the accounts over to CredMan
                    {
                        credMan = CredManifest.GetManifest();
                        if (!credMan.ImportAccounts())
                        {
                            UseMaFiles = true;
                        }
                    }
                    else if (UseMaFiles && notFirstRun)
                    {
                        credMan = CredManifest.GetManifest();
                        if (credMan.Entries.Count > 0 && Entries.Count < credMan.Entries.Count)
                        {
                            if (!credMan.ExportAccounts())
                            {
                                UseMaFiles = false;
                            }
                        }
                    }
                }
                catch { }

                if (UseDPAPI)
                {
                    File.WriteAllText(filename, Encryptor.DPAPIProtect(JsonConvert.SerializeObject(this)));
                }
                else
                {
                    File.WriteAllText(filename, JsonConvert.SerializeObject(this));
                }

                if (UseWindowsFileEncryption)
                {
                    File.Encrypt(filename);
                }
                else
                {
                    File.Decrypt(filename);
                }
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Example #3
0
        public bool SaveAccount(SteamGuardAccount account, bool encrypt, SecureString passkey = null)
        {
            if (encrypt && passkey != null && passkey.Length >= 1)
            {
                return(false);
            }
            if (!encrypt && this.Encrypted)
            {
                return(false);
            }
            if (account == null)
            {
                return(false);
            }

            if (UseMaFiles)
            {
                string salt        = null;
                string iV          = null;
                string jsonAccount = JsonConvert.SerializeObject(account);

                if (encrypt)
                {
                    salt = Encryptor.GetRandomSalt();
                    iV   = Encryptor.GetInitializationVector();
                    string encrypted = "Encrypted" + Encryptor.EncryptData(passkey, salt, iV, jsonAccount);
                    if (encrypted == null)
                    {
                        return(false);
                    }
                    jsonAccount = encrypted;
                }

                if (UseDPAPI)
                {
                    jsonAccount = Encryptor.DPAPIProtect(jsonAccount, Encryptor.AccountEntropy);
                }

                string maDir    = GetExecutableDir() + "/maFiles/";
                string filename = account.Session.SteamID.ToString() + ".maFile";

                ManifestEntry newEntry = new ManifestEntry()
                {
                    SteamID  = account.Session.SteamID,
                    IV       = iV,
                    Salt     = salt,
                    FileName = filename,
                };

                bool foundExistingEntry = false;
                for (int i = 0; i < this.Entries.Count; i++)
                {
                    if (Entries[i].SteamID == account.Session.SteamID)
                    {
                        Entries[i]         = newEntry;
                        foundExistingEntry = true;
                        break;
                    }
                }

                if (!foundExistingEntry)
                {
                    Entries.Add(newEntry);
                }

                bool wasEncrypted = Encrypted;
                Encrypted = encrypt || Encrypted;

                if (!Save())
                {
                    Encrypted = wasEncrypted;
                    return(false);
                }

                try
                {
                    File.WriteAllText(maDir + filename, jsonAccount);
                    if (UseWindowsFileEncryption)
                    {
                        File.Encrypt(maDir + filename);
                    }
                    else
                    {
                        File.Decrypt(maDir + filename);
                    }
                    return(true);
                }
                catch (Exception)
                {
                    return(false);
                }
            }
            else
            {
                try
                {
                    credMan = CredManifest.GetManifest();
                    string salt = null; string iV = null;
                    string jsonAccount   = JsonConvert.SerializeObject(account);
                    bool   fileEncrypted = false;

                    if (encrypt)
                    {
                        salt = Encryptor.GetRandomSalt();
                        iV   = Encryptor.GetInitializationVector();
                        string encrypted = "Encrypted" + Encryptor.EncryptData(passkey, salt, iV, jsonAccount);
                        if (encrypted == null)
                        {
                            return(false);
                        }
                        jsonAccount   = encrypted;
                        fileEncrypted = true;
                    }

                    if (UseDPAPI)
                    {
                        jsonAccount = Encryptor.DPAPIProtect(jsonAccount, Encryptor.AccountEntropy);
                    }

                    CredManifestEntry newEntry = new CredManifestEntry()
                    {
                        SteamID   = account.Session.SteamID,
                        IV        = iV,
                        Salt      = salt,
                        Contents  = jsonAccount,
                        Encrypted = fileEncrypted,
                    };
                    bool foundExistingEntry = false;
                    for (int i = 0; i < credMan.Entries.Count; i++)
                    {
                        if (credMan.Entries[i].SteamID == account.Session.SteamID)
                        {
                            credMan.Entries[i] = newEntry;
                            foundExistingEntry = true;
                            break;
                        }
                    }

                    if (!foundExistingEntry)
                    {
                        credMan.Entries.Add(newEntry);
                    }

                    credMan.Save();

                    bool wasEncrypted = Encrypted;
                    Encrypted = encrypt || Encrypted;

                    if (!Save())
                    {
                        Encrypted = wasEncrypted;
                        return(false);
                    }
                    return(true);
                }
                catch (Exception) { return(false); }
            }
        }
Example #4
0
        public bool ChangeEncryptionKey(SecureString oldKey, SecureString newKey)
        {
            if (Encrypted)
            {
                if (!VerifyPasskey(oldKey))
                {
                    return(false);
                }
            }
            bool toEncrypt = newKey != null;

            credMan = CredManifest.GetManifest();
            if (UseMaFiles)
            {
                string maDir = GetExecutableDir() + "/maFiles/";
                for (int i = 0; i < Entries.Count; i++)
                {
                    ManifestEntry entry    = Entries[i];
                    string        filename = maDir + entry.FileName;
                    if (!File.Exists(filename))
                    {
                        continue;
                    }

                    string fileContents = File.ReadAllText(filename);

                    fileContents = Encryptor.DPAPIUnprotect(fileContents, Encryptor.AccountEntropy);

                    if (fileContents.StartsWith("Encrypted"))
                    {
                        fileContents = Encryptor.DecryptData(oldKey, entry.Salt, entry.IV, fileContents.Remove(0, 9));
                    }

                    string newSalt             = null;
                    string newIV               = null;
                    string toWriteFileContents = fileContents;

                    if (toEncrypt)
                    {
                        newSalt             = Encryptor.GetRandomSalt();
                        newIV               = Encryptor.GetInitializationVector();
                        toWriteFileContents = "Encrypted" + Encryptor.EncryptData(newKey, newSalt, newIV, fileContents);
                    }

                    if (UseDPAPI)
                    {
                        toWriteFileContents = Encryptor.DPAPIProtect(toWriteFileContents, Encryptor.AccountEntropy);
                    }


                    File.WriteAllText(filename, toWriteFileContents);
                    if (UseWindowsFileEncryption)
                    {
                        File.Encrypt(filename);
                    }
                    else
                    {
                        File.Decrypt(filename);
                    }
                    entry.IV   = newIV;
                    entry.Salt = newSalt;
                }
            }
            else
            {
                foreach (CredManifestEntry entry in credMan.Entries)
                {
                    string fileContents = entry.Contents;

                    fileContents = Encryptor.DPAPIUnprotect(fileContents, Encryptor.AccountEntropy);

                    if (fileContents.StartsWith("Encrypted"))
                    {
                        fileContents = Encryptor.DecryptData(oldKey, entry.Salt, entry.IV, fileContents.Remove(0, 9));
                    }

                    string newSalt             = null;
                    string newIV               = null;
                    string toWriteFileContents = fileContents;

                    if (toEncrypt)
                    {
                        newSalt             = Encryptor.GetRandomSalt();
                        newIV               = Encryptor.GetInitializationVector();
                        toWriteFileContents = "Encrypted" + Encryptor.EncryptData(newKey, newSalt, newIV, fileContents);
                    }
                    else
                    {
                        entry.Encrypted = false;
                    }

                    if (UseDPAPI)
                    {
                        toWriteFileContents = Encryptor.DPAPIProtect(toWriteFileContents, Encryptor.AccountEntropy);
                    }

                    entry.Contents = toWriteFileContents;
                    entry.IV       = newIV;
                    entry.Salt     = newSalt;
                }
                credMan.Key = newKey;
                credMan.Save();
            }

            Encrypted = toEncrypt;

            Save();
            return(true);
        }
Example #5
0
        public bool Save()
        {
            if (_originalCred != null)
            {
                try
                {
                    try // To delete the current stuff, you have to know the old stuff...
                    {
                        new PasswordVault().Remove(new PasswordCredential("SteamAuthenticator", "Storage", Encryptor.DPAPIUnprotect(_originalCred)));
                    }
                    catch (Exception)
                    {
                        //    System.Windows.MessageBox.Show("Failed to remove existing manifest!", "CredManifest");
                    }
                }
                catch (Exception)
                {
                    return(false);
                }
            }

            if (!Manifest.RememberPasskey)
            {
                UnsecureKey = ""; // 'Flush' it from memory then save the manifest. Note: unless the key gets resubmitted to CredManifest, it wont know the key until a program restart
            }
            else
            {
                Key = Key; // This refreshes the unsecure and secure copy
            }
            _originalCred = JsonConvert.SerializeObject(this);
            if (Manifest.UseDPAPI)
            {
                new PasswordVault().Add(new PasswordCredential("SteamAuthenticator", "Storage", Encryptor.DPAPIProtect(_originalCred)));
            }
            else
            {
                new PasswordVault().Add(new PasswordCredential("SteamAuthenticator", "Storage", _originalCred));
            }
            return(true);
        }