Beispiel #1
0
        /// <summary>
        /// generates a keepass xml file by directly accessing firefoxes passwords
        /// designed to be used in a thread
        /// uses the thread
        /// </summary>
        private void GenerateUsingFirefox()
        {
            try
            {
                InternetAccessor internetAccessor = new InternetAccessor();

                FirefoxProfile profile = new FirefoxProfile(this.ProfilePath);

                profile.Login(this.FirefoxMasterPassword);

                FirefoxSignonsFile signonsFile = profile.GetSignonsFile(this.FirefoxMasterPassword);

                // the group to store the passwords

                // the output xml document
                XmlDocument keePassDocument = new XmlDocument();

                XmlElement keePassRootElement = keePassDocument.CreateElement("pwlist");
                keePassDocument.AppendChild(keePassRootElement);

                int current = 0;
                int max     = signonsFile.SignonSites.Count;
                // loop each input password and generate the output password
                foreach (FirefoxSignonSite signonSite in signonsFile.SignonSites)
                {
                    current++;

                    string siteTitle = null;

                    if (GetTitles)
                    {
                        siteTitle = internetAccessor.ScrapeTitle(signonSite.Site);
                    }

                    foreach (FirefoxSignon signon in signonSite.Signons)
                    {
                        string title = siteTitle == null ? signonSite.Site : siteTitle;

                        if (!String.IsNullOrEmpty(signon.LoginFormDomain))
                        {
                            title = signon.LoginFormDomain;
                        }

                        string host = null;
                        try
                        {
                            Uri uri = new Uri(signonSite.Site);
                            host = uri.Host;
                        }
                        catch { }

                        if (GetTitles)
                        {
                            // get the pages title

                            string internetTitle = null;

                            if (String.IsNullOrEmpty(signon.LoginFormDomain) || signon.LoginFormDomain == signonSite.Site)
                            {
                                internetTitle = siteTitle;
                            }
                            else
                            {
                                internetTitle = internetAccessor.ScrapeTitle(signon.LoginFormDomain);
                            }

                            if (!String.IsNullOrEmpty(internetTitle))
                            {
                                title = internetTitle;
                            }
                        }

                        string notes = String.Empty;

                        if (checkBoxIncludeImportNotes.Checked)
                        {
                            notes += "Imported from FireFox by the Web Site Advantage Firefox To KeePass" + Environment.NewLine;
                        }

                        if (GenerateAutoType)
                        {
                            if (this.GetTitles)
                            {
                                notes += Environment.NewLine +
                                         "Auto-Type: " + KeePassUtilities.AutoTypeSequence() + Environment.NewLine +
                                         (String.IsNullOrEmpty(host) ? String.Empty : "Auto-Type-Window: *" + host + "*" + Environment.NewLine) +
                                         "Auto-Type-Window: " + KeePassUtilities.AutoTypeWindow(title) + Environment.NewLine;
                            }
                            else
                            {
                                if (!String.IsNullOrEmpty(host))
                                {
                                    notes += Environment.NewLine +
                                             "Auto-Type: " + KeePassUtilities.AutoTypeSequence() + Environment.NewLine +
                                             "Auto-Type-Window: *" + host + "*" + Environment.NewLine;
                                }
                            }
                        }


                        string now = XmlConvert.ToString(DateTime.Now, "yyyy-MM-ddTHH:mm:ss");

                        // create xml

                        XmlElement keePassEntryElement = keePassDocument.CreateElement("pwentry");
                        keePassRootElement.AppendChild(keePassEntryElement);

                        XmlElement keePassGroupElement = keePassDocument.CreateElement("group");
                        keePassEntryElement.AppendChild(keePassGroupElement);
                        keePassGroupElement.InnerText = GroupName;

                        // set the group the password gets stored in
                        if (!String.IsNullOrEmpty(GroupName))
                        {
                            keePassGroupElement.SetAttribute("tree", GroupName);
                        }

                        XmlElement keePassTitleElement = keePassDocument.CreateElement("title");
                        keePassEntryElement.AppendChild(keePassTitleElement);
                        keePassTitleElement.InnerText = title;

                        XmlElement keePassUserNameElement = keePassDocument.CreateElement("username");
                        keePassEntryElement.AppendChild(keePassUserNameElement);
                        keePassUserNameElement.InnerText = signon.UserName;

                        XmlElement keePassURLElement = keePassDocument.CreateElement("url");
                        keePassEntryElement.AppendChild(keePassURLElement);
                        keePassURLElement.InnerText = signonSite.Site;

                        XmlElement keePassPasswordElement = keePassDocument.CreateElement("password");
                        keePassEntryElement.AppendChild(keePassPasswordElement);
                        keePassPasswordElement.InnerText = signon.Password;

                        if (!String.IsNullOrEmpty(notes))
                        {
                            // put other stuff in the notes
                            XmlElement keePassNotesElement = keePassDocument.CreateElement("notes");
                            keePassEntryElement.AppendChild(keePassNotesElement);

                            //	keePassNotesElement.InnerText =

                            XmlCDataSection cd = keePassDocument.CreateCDataSection(notes);
                            keePassNotesElement.AppendChild(cd);
                        }

                        XmlElement keePassUuidElement = keePassDocument.CreateElement("uuid");
                        keePassEntryElement.AppendChild(keePassUuidElement);
                        keePassUuidElement.InnerText = Guid.NewGuid().ToString().Replace("-", "").ToLower();                         // condensed guid

                        XmlElement keePassImageElement = keePassDocument.CreateElement("image");
                        keePassEntryElement.AppendChild(keePassImageElement);
                        keePassImageElement.InnerText = IconId.ToString();


                        XmlElement keePassCreatedElement = keePassDocument.CreateElement("creationtime");
                        keePassEntryElement.AppendChild(keePassCreatedElement);
                        keePassCreatedElement.InnerText = now;

                        XmlElement keePassModifiedElement = keePassDocument.CreateElement("lastmodtime");
                        keePassEntryElement.AppendChild(keePassModifiedElement);
                        keePassModifiedElement.InnerText = now;

                        XmlElement keePassAccessedElement = keePassDocument.CreateElement("lastaccesstime");
                        keePassEntryElement.AppendChild(keePassAccessedElement);
                        keePassAccessedElement.InnerText = now;

                        // so it does not expire
                        XmlElement keePassExpiresElement = keePassDocument.CreateElement("expiretime");
                        keePassEntryElement.AppendChild(keePassExpiresElement);
                        keePassExpiresElement.SetAttribute("expires", "false");
                        keePassExpiresElement.InnerText = "2999-12-28T23:59:59";

                        LogProgress((int)((double)(current) * 100 / (double)max));
                    }
                }


                // save the xml. this way the encoding header is included...
                XmlTextWriter writer = new XmlTextWriter(KeePassFile, Encoding.UTF8);
                try
                {
                    writer.Formatting = Formatting.Indented;
                    keePassDocument.Save(writer);
                }
                finally
                {
                    writer.Close();
                }

                this.ThreadFinished(null);
            }
            catch (Exception ex)
            {
                this.ThreadFinished(ex);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Create the keepass file using an exported firefox file
        /// </summary>
        private void GenerateUsingExport()
        {
            try
            {
                InternetAccessor internetAccessor = new InternetAccessor();

                // load in the firefox xml document
                XmlDocument fireFoxDocument = new XmlDocument();
                FileStream  fileStream      = File.Open(FirefoxFile, FileMode.Open, FileAccess.Read);
                try
                {
                    XmlReaderSettings settings = new XmlReaderSettings();
                    settings.CheckCharacters = false;
                    settings.ValidationType  = ValidationType.None;

                    XmlReader reader = XmlTextReader.Create(fileStream, settings);

                    try
                    {
                        fireFoxDocument.Load(reader);
                    }
                    finally
                    {
                        reader.Close();
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("Failed to load the password file. " + Environment.NewLine +
                                        "This may be due to the presence of foreign characters in the data. " + Environment.NewLine +
                                        "Please check the website for help" + Environment.NewLine + Environment.NewLine + ex.Message, ex);
                }
                finally
                {
                    fileStream.Close();
                }


                // get a list of each password node
                XmlNodeList fireFoxEntryNodes = fireFoxDocument.SelectNodes("xml/entries/entry");

                // the output xml document
                XmlDocument keePassDocument = new XmlDocument();

                XmlElement keePassRootElement = keePassDocument.CreateElement("pwlist");
                keePassDocument.AppendChild(keePassRootElement);


                int current = 0;
                int max     = fireFoxEntryNodes.Count;
                // loop each input password and generate the output password
                foreach (XmlElement fireFoxEntryElement in fireFoxEntryNodes)
                {
                    current++;

                    string title = fireFoxEntryElement.SelectSingleNode("@host").InnerText;

                    string url = title;

                    string formSubmitURL = fireFoxEntryElement.SelectSingleNode("@formSubmitURL").InnerText;

                    if (!String.IsNullOrEmpty(formSubmitURL))
                    {
                        url = formSubmitURL;
                    }

                    string host = url;
                    try
                    {
                        Uri uri = new Uri(url);
                        host = uri.Host;
                    }
                    catch { }

                    string internetTitle = null;

                    if (GetTitles)
                    {
                        // get the pages title
                        try
                        {
                            internetTitle = internetAccessor.ScrapeTitle(url);

                            if (!String.IsNullOrEmpty(internetTitle))
                            {
                                title = internetTitle;
                            }
                        }
                        catch (Exception ex)
                        {
                            // some issue!
                            KeePassUtilities.LogException(ex);
                        }
                    }

                    string notes = String.Empty;

                    if (checkBoxIncludeImportNotes.Checked)
                    {
                        notes +=
                            "Imported from FireFox by the Web Site Advantage Firefox To KeePass Importer" + Environment.NewLine + Environment.NewLine +
                            "UserNameField=" + fireFoxEntryElement.SelectSingleNode("@userFieldName").InnerText + System.Environment.NewLine +
                            "PasswordField=" + fireFoxEntryElement.SelectSingleNode("@passFieldName").InnerText + System.Environment.NewLine +
                            "LoginFormDomain=" + fireFoxEntryElement.SelectSingleNode("@formSubmitURL").InnerText + System.Environment.NewLine;
                    }

                    if (GenerateAutoType)
                    {
                        if (!String.IsNullOrEmpty(internetTitle))
                        {
                            notes += Environment.NewLine +
                                     "Auto-Type: " + KeePassUtilities.AutoTypeSequence() + Environment.NewLine +
                                     "Auto-Type-Window: *" + host + "*" + Environment.NewLine +
                                     "Auto-Type-Window: " + KeePassUtilities.AutoTypeWindow(internetTitle) + Environment.NewLine;
                        }
                        else
                        {
                            notes += Environment.NewLine +
                                     "Auto-Type: " + KeePassUtilities.AutoTypeSequence() + Environment.NewLine +
                                     "Auto-Type-Window: *" + host + "*" + Environment.NewLine;
                        }
                    }

                    string now = XmlConvert.ToString(DateTime.Now, "yyyy-MM-ddTHH:mm:ss");

                    // create the xml

                    XmlElement keePassEntryElement = keePassDocument.CreateElement("pwentry");
                    keePassRootElement.AppendChild(keePassEntryElement);

                    XmlElement keePassGroupElement = keePassDocument.CreateElement("group");
                    keePassEntryElement.AppendChild(keePassGroupElement);
                    keePassGroupElement.InnerText = GroupName;

                    // set the group the password gets stored in
                    if (!String.IsNullOrEmpty(GroupName))
                    {
                        keePassGroupElement.SetAttribute("tree", GroupName);
                    }

                    XmlElement keePassTitleElement = keePassDocument.CreateElement("title");
                    keePassEntryElement.AppendChild(keePassTitleElement);
                    keePassTitleElement.InnerText = title;

                    XmlElement keePassUserNameElement = keePassDocument.CreateElement("username");
                    keePassEntryElement.AppendChild(keePassUserNameElement);
                    keePassUserNameElement.InnerText = fireFoxEntryElement.SelectSingleNode("@user").InnerText;

                    XmlElement keePassURLElement = keePassDocument.CreateElement("url");
                    keePassEntryElement.AppendChild(keePassURLElement);
                    keePassURLElement.InnerText = fireFoxEntryElement.SelectSingleNode("@host").InnerText;


                    XmlElement keePassPasswordElement = keePassDocument.CreateElement("password");
                    keePassEntryElement.AppendChild(keePassPasswordElement);
                    keePassPasswordElement.InnerText = fireFoxEntryElement.SelectSingleNode("@password").InnerText;

                    // put other stuff in the notes
                    if (!String.IsNullOrEmpty(notes))
                    {
                        XmlElement keePassNotesElement = keePassDocument.CreateElement("notes");
                        keePassEntryElement.AppendChild(keePassNotesElement);

                        XmlCDataSection cd = keePassDocument.CreateCDataSection(notes);
                        keePassNotesElement.AppendChild(cd);
                    }

                    XmlElement keePassUuidElement = keePassDocument.CreateElement("uuid");
                    keePassEntryElement.AppendChild(keePassUuidElement);
                    keePassUuidElement.InnerText = Guid.NewGuid().ToString().Replace("-", "").ToLower();                     // condensed guid

                    XmlElement keePassImageElement = keePassDocument.CreateElement("image");
                    keePassEntryElement.AppendChild(keePassImageElement);
                    keePassImageElement.InnerText = IconId.ToString();

                    XmlElement keePassCreatedElement = keePassDocument.CreateElement("creationtime");
                    keePassEntryElement.AppendChild(keePassCreatedElement);
                    keePassCreatedElement.InnerText = now;

                    XmlElement keePassModifiedElement = keePassDocument.CreateElement("lastmodtime");
                    keePassEntryElement.AppendChild(keePassModifiedElement);
                    keePassModifiedElement.InnerText = now;

                    XmlElement keePassAccessedElement = keePassDocument.CreateElement("lastaccesstime");
                    keePassEntryElement.AppendChild(keePassAccessedElement);
                    keePassAccessedElement.InnerText = now;

                    // so it does not expire
                    XmlElement keePassExpiresElement = keePassDocument.CreateElement("expiretime");
                    keePassEntryElement.AppendChild(keePassExpiresElement);
                    keePassExpiresElement.SetAttribute("expires", "false");
                    keePassExpiresElement.InnerText = "2999-12-28T23:59:59";

                    LogProgress((int)((double)(current) * 100 / (double)max));
                }

                XmlTextWriter writer = new XmlTextWriter(KeePassFile, Encoding.UTF8);
                try
                {
                    writer.Formatting = Formatting.Indented;
                    keePassDocument.Save(writer);
                }
                finally
                {
                    writer.Close();
                }

                this.ThreadFinished(null);
            }
            catch (Exception ex)
            {
                this.ThreadFinished(ex);
            }
        }