/// <summary>
        /// Import credentials from the "Login Data" file.
        /// </summary>
        /// <exception cref="FileNotFoundException">Thrown when the database is not present.</exception>
        /// <param name="param">The parameters for the import</param>
        public override void ImportCredentials(ImportParameter param)
        {
            var currentProfilePath = !string.IsNullOrEmpty(param.Profile) ? Path.Combine(ProfilePath, param.Profile) : ProfilePath;

            if (!Directory.Exists(currentProfilePath))
            {
                throw new FileNotFoundException(currentProfilePath);
            }

            var loginDataPath = Path.Combine(currentProfilePath, "Login Data");

            if (!File.Exists(loginDataPath))
            {
                throw new FileNotFoundException(loginDataPath);
            }

            using (var db = new DBHandler(loginDataPath))
            {
                DataTable dt;
                db.Query(out dt, "SELECT origin_url, username_value, password_value FROM logins");

                foreach (var row in dt.AsEnumerable())
                {
                    param.Database.CreateWebsiteEntry(
                        param.Group,
                        row["origin_url"] as string,
                        row["username_value"] as string,
                        Encoding.UTF8.GetString(Cryptography.DecryptUserData(row["password_value"] as byte[])),
                        param.ExtractTitle,
                        param.ExtractIcon,
                        param.Logger
                        );
                }
            }
        }
        /// <summary>
        /// Enumerates the HTTP Basic Authentication passwords stored in the credential store.
        /// </summary>
        private IEnumerable <Tuple <string, string, string> > ReadCredentialStorePasswords()
        {
            int    count;
            IntPtr credentials = IntPtr.Zero;

            try
            {
                if (CredEnumerate(null, 0, out count, out credentials))
                {
                    var entropy = new byte[]
                    {
                        0x84, 0x01, 0x88, 0x01, 0x94, 0x01, 0xC8, 0x00,
                        0xE0, 0x00, 0xD8, 0x00, 0xE4, 0x00, 0x98, 0x01,
                        0xB4, 0x00, 0xE4, 0x00, 0x88, 0x01, 0xD0, 0x00,
                        0xDC, 0x00, 0xB4, 0x00, 0xD0, 0x00, 0x8C, 0x01,
                        0x90, 0x01, 0xE4, 0x00, 0xB4, 0x00, 0x84, 0x01,
                        0xCC, 0x00, 0xD4, 0x00, 0xE0, 0x00, 0xB4, 0x00,
                        0x8C, 0x01, 0xC8, 0x00, 0xC8, 0x00, 0xE4, 0x00,
                        0xC0, 0x00, 0xD0, 0x00, 0x90, 0x01, 0x88, 0x01,
                        0x84, 0x01, 0xDC, 0x00, 0x98, 0x01, 0xDC, 0x00,
                        0x00, 0x00
                    };

                    for (int i = 0; i < count; i++)
                    {
                        var credPtr = Marshal.ReadIntPtr(credentials, i * Marshal.SizeOf(typeof(IntPtr)));
                        var cred    = (Credential)Marshal.PtrToStructure(credPtr, typeof(Credential));
                        if (cred.Type == CredentialType.Generic && cred.CredentialBlobSize > 0)
                        {
                            var blob = new byte[cred.CredentialBlobSize];
                            Marshal.Copy(cred.CredentialBlob, blob, 0, blob.Length);

                            var data = Cryptography.DecryptUserData(blob, entropy);
                            if (data.Length > 0)
                            {
                                var credStr     = Encoding.Unicode.GetString(data).TrimEnd('\0');
                                var splitOffset = credStr.IndexOf(':');
                                if (splitOffset != -1)
                                {
                                    yield return(Tuple.Create(
                                                     cred.TargetName,
                                                     credStr.Substring(0, splitOffset),
                                                     credStr.Substring(splitOffset + 1)
                                                     ));
                                }
                            }
                        }
                    }
                }
            }
            finally
            {
                if (credentials != IntPtr.Zero)
                {
                    CredFree(credentials);
                }
            }
        }
        /// <summary>
        /// Import credentials from the "Login Data" file.
        /// </summary>
        /// <exception cref="FileNotFoundException">Thrown when the database is not present.</exception>
        /// <param name="param">The parameters for the import</param>
        public override void ImportCredentials(ImportParameter param)
        {
            var currentProfilePath = !string.IsNullOrEmpty(param.CustomProfilePath)
                                ? param.CustomProfilePath
                                : !string.IsNullOrEmpty(param.Profile)
                                        ? Path.Combine(ProfilePath, param.Profile)
                                        : ProfilePath;

            if (!Directory.Exists(currentProfilePath))
            {
                throw new ProfileNotFoundException(currentProfilePath);
            }

            var loginDataPath = Path.Combine(currentProfilePath, "Login Data");

            if (!File.Exists(loginDataPath))
            {
                throw new ProfileNotFoundException(loginDataPath);
            }

            try
            {
                using (var db = new DBHandler(loginDataPath))
                {
                    DataTable dt;
                    db.Query(out dt, "SELECT origin_url, username_value, password_value, date_created FROM logins");

                    foreach (var row in dt.AsEnumerable())
                    {
                        var date = DateUtils.FromChromiumTime((long)row["date_created"]);

                        var entry = new EntryInfo
                        {
                            Hostname = row["origin_url"] as string,
                            Username = row["username_value"] as string,
                            Password = Encoding.UTF8.GetString(Cryptography.DecryptUserData(row["password_value"] as byte[])),
                            Created  = date,
                            Modified = date
                        };

                        param.Database.CreateWebsiteEntry(
                            param.Group,
                            entry,
                            param.CreationSettings,
                            param.Logger
                            );
                    }
                }
            }
            catch (DbException ex)
            {
                throw new Exception(string.Format("Error while using the browsers login database. It may help to close all running instances of the browser.\n\n{0}", StrUtil.FormatException(ex)), ex);
            }
        }
Пример #4
0
        /// <summary>
        /// Enumerates the auto complete passwords stored in the registry.
        /// </summary>
        private IEnumerable <EntryInfo> ReadRegistryPasswords()
        {
            var hashToUrl = new Dictionary <string, string>();

            using (var sha1 = new SHA1Managed())
            {
                foreach (var url in GetHistoryItems())
                {
                    var hash = sha1.ComputeHash(Encoding.Unicode.GetBytes(url));

                    var sb = new StringBuilder();

                    byte checksum = 0;
                    foreach (var x in hash)
                    {
                        checksum += x;

                        sb.AppendFormat("{0:X2}", x);
                    }
                    sb.AppendFormat("{0:X2}", checksum);

                    hashToUrl[sb.ToString()] = url;
                }
            }

            var storage2 = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Internet Explorer\IntelliForms\Storage2");

            if (storage2 != null)
            {
                foreach (var hash in storage2.GetValueNames())
                {
                    string url;
                    if (hashToUrl.TryGetValue(hash, out url))
                    {
                        var data = storage2.GetValue(hash, null) as byte[];
                        if (data != null)
                        {
                            data = Cryptography.DecryptUserData(data, Encoding.Unicode.GetBytes(url));
                            if (data.Length > 0)
                            {
                                var autoCompleteHeader = FromBinaryData <IEAutoCompleteSecretHeader>(data, 0);
                                if (data.Length >= autoCompleteHeader.Size + autoCompleteHeader.SecretInfoSize + autoCompleteHeader.SecretSize)
                                {
                                    var totalSecrets = autoCompleteHeader.IESecretHeader.TotalSecrets / 2;

                                    var offset       = Marshal.SizeOf(typeof(IEAutoCompleteSecretHeader));
                                    var secretOffset = autoCompleteHeader.Size + autoCompleteHeader.SecretInfoSize;

                                    for (var i = 0; i < autoCompleteHeader.IESecretHeader.TotalSecrets; i += 2)
                                    {
                                        var entry    = FromBinaryData <SecretEntry>(data, offset);
                                        var username = BytePtrToStringUni(data, secretOffset + entry.Offset);

                                        offset += Marshal.SizeOf(typeof(SecretEntry));

                                        entry = FromBinaryData <SecretEntry>(data, offset);
                                        var password = BytePtrToStringUni(data, secretOffset + entry.Offset);

                                        offset += Marshal.SizeOf(typeof(SecretEntry));

                                        yield return(new EntryInfo
                                        {
                                            Hostname = url.Trim('\0').Trim(),
                                            Username = username,
                                            Password = password,
                                            Created = DateTime.Now,
                                            Modified = DateTime.Now
                                        });
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }