1

I just want to get the password out of my software KeePass.

After using the code from an old question here Link to the question, im getting this error message:

S1061 'IEnumerable<<anonymous type: string Group, string Title, string Username, string Password>>' does not contain a definition for 'Dump' and no accessible extension method 'Dump' accepting a first argument of type 'IEnumerable<<anonymous type: string Group, string Title, string Username, string Password>>' could be found (are you missing a using directive or an assembly reference?) KeePasso C:\Users\prusinma\source\repos\KeePasso\KeePasso\Program.cs 36 Active

This is the code im using:

using System.Linq;
using KeePassLib;
using KeePassLib.Keys;
using KeePassLib.Serialization;

namespace KeePasso
{
    class Program
    {
        static void Main()
        {

            var dbpath = @"\\xxx\Home_VIE\xxx\Desktop\KeePassDatabase\Database.kdbx";
            var keypath = @"\\xxx\Home_VIE\xxx\Desktop\KeePassDatabase\Database.key";
            var masterpw = "1234abcd";

            var ioConnInfo = new IOConnectionInfo { Path = dbpath };
            var compKey = new CompositeKey();
            compKey.AddUserKey(new KcpPassword(masterpw));
            compKey.AddUserKey(new KcpKeyFile(IOConnectionInfo.FromPath(keypath))); // Keyfile

            var db = new PwDatabase();
            db.Open(ioConnInfo, compKey, null);

            var kpdata = from entry in db.RootGroup.GetEntries(true)
                         select new
                         {
                             Group = entry.ParentGroup.Name,
                             Title = entry.Strings.ReadSafe("Title"),
                             Username = entry.Strings.ReadSafe("UserName"),
                             Password = entry.Strings.ReadSafe("Password"),
                         };

            kpdata.Dump(); // this is how Linqpad outputs stuff
            db.Close();
        }
    }
}

In the last rows in the code, there is a red underline at Dump. Which displays the same error message i shared above.

I was already trying to find similiar questions and in most of them they were related to the type. But as i can see, all the datas/entries in Title, Username and Password are strings.

Would appreciate if someone could help me here out. Id also be open for a other solution how to read out the password from the database.

Thanks!

Beardy
  • 163
  • 1
  • 15

2 Answers2

1

Since kpdata is collection of anonimous types, which has overriden ToString (and if entry.Strings.ReadSafe returns string or some type with "correctly" overriden ToString method) you can just use Console.WriteLine on it:

Console.WriteLine(string.Join(Environment.NewLine, kpdata));; // instead of kpdata.Dump();

Otherwise you will need to find a way to import LINQPad's Dump method into your project or just use some json serialization library to convert object to string.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • Thanks. I already tried to simply output the entries with WriteLine. But my Output doesnt look correct `System.Linq.Enumerable+SelectEnumerableIterator`2[KeePassLib.PwEntry,<>f__AnonymousType0`4[System.String,System.String,System.String,System.String]]` – Beardy Aug 17 '21 at 10:21
  • @Beardy, sorry, missed that it is collection. Than you can use something like `Console.WriteLine(string.Join(Environment.NewLine, kpdata));` – Guru Stron Aug 17 '21 at 10:23
  • Oh wow. It actually worked!!! I was never expecting someone giving a solution that fast! Can you give me maybe an example, how to get only the password and store it in a variable? – Beardy Aug 17 '21 at 10:26
  • @Beardy - you have a collection so theoretically there can be multiple passwords - in this case which one of them do you need? – Guru Stron Aug 17 '21 at 10:29
  • The collection/database has 3 rows called Title, Username and Password. Lets say the Titles name is Azkaban. Then i want to get the password from Title Azkaban and store it in a variable – Beardy Aug 17 '21 at 10:32
  • @Beardy I would recommend to get familiar with [LINQ](https://csharp.net-tutorials.com/linq/linq-query-syntax-vs-method-syntax/). Something like `kpdata.Where(d => d.Title == ....).FirstOrDefault()?.Password` should do the trick. – Guru Stron Aug 17 '21 at 10:47
  • Was hoping to skip that LINQ part haha. But ill have a look! Thanks so so so so much for your help =) – Beardy Aug 17 '21 at 10:52
  • `var pw = kpdata.Where(d => d.Title == "Azkaban").FirstOrDefault()?.Password;` and then `Console.WriteLine(pw.ToString());` works ;) – Beardy Aug 17 '21 at 11:01
0

Cast it to object!

//But This works!
((object)d).Dump();

// as does this)
(d as Object).Dump()
Smith
  • 5,765
  • 17
  • 102
  • 161