Skip to content

Commit

Permalink
Add an option for unprotected database extraction in CLI, resolves ke…
Browse files Browse the repository at this point in the history
  • Loading branch information
Z1ni committed Oct 8, 2018
1 parent 4ff63c2 commit 2f90888
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/cli/Extract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@

#include "Extract.h"

#include <QBuffer>
#include <QCommandLineParser>
#include <QFile>
#include <QTextStream>

#include "cli/Utils.h"
#include "core/Database.h"
#include "format/KdbxXmlWriter.h"
#include "format/KeePass2Reader.h"
#include "keys/CompositeKey.h"
#include "keys/FileKey.h"
Expand Down Expand Up @@ -53,7 +55,11 @@ int Extract::execute(const QStringList& arguments)
<< "key-file",
QObject::tr("Key file of the database."),
QObject::tr("path"));
QCommandLineOption unprotectedOpt(QStringList() << "u"
<< "unprotected",
QObject::tr("Output unprotected data. This WILL show your passwords in PLAINTEXT!"));
parser.addOption(keyFile);
parser.addOption(unprotectedOpt);
parser.process(arguments);

const QStringList args = parser.positionalArguments();
Expand Down Expand Up @@ -92,6 +98,8 @@ int Extract::execute(const QStringList& arguments)
compositeKey->addKey(fileKey);
}

bool outputUnprotected = parser.isSet(unprotectedOpt);

QString databaseFilename = args.at(0);
QFile dbFile(databaseFilename);
if (!dbFile.exists()) {
Expand All @@ -104,11 +112,26 @@ int Extract::execute(const QStringList& arguments)
}

KeePass2Reader reader;
KdbxXmlWriter writer(KeePass2::FILE_VERSION_4);

reader.setSaveXml(true);
Database* db = reader.readDatabase(&dbFile, compositeKey);
delete db;

QByteArray xmlData = reader.reader()->xmlData();
QByteArray xmlData;

if (outputUnprotected) {
QBuffer xmlWriteBuffer;

xmlWriteBuffer.open(QIODevice::WriteOnly);
writer.writeDatabase(&xmlWriteBuffer, db);
xmlWriteBuffer.close();

xmlData = xmlWriteBuffer.data();
} else {
xmlData = reader.reader()->xmlData();
}

delete db;

if (reader.hasError()) {
if (xmlData.isEmpty()) {
Expand All @@ -117,6 +140,9 @@ int Extract::execute(const QStringList& arguments)
qWarning("Error while parsing the database:\n%s\n", qPrintable(reader.errorString()));
}
return EXIT_FAILURE;
} else if (writer.hasError()) {
qCritical("Error while writing the database XML:\n%s", qPrintable(writer.errorString()));
return EXIT_FAILURE;
}

out << xmlData.constData() << "\n";
Expand Down

0 comments on commit 2f90888

Please sign in to comment.