Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unprotected output switch to keepassxc-cli extract #2374

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 6 additions & 0 deletions src/cli/keepassxc-cli.1
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ Use special characters for the generated password. [Default: Disabled]
Use extended ASCII characters for the generated password. [Default: Disabled]


.SS "Extract options"

.IP "-u, --unprotected"
Extract the database with all the protected fields as unprotected.
This will extract passwords etc. as plaintext. [Default: Disabled]


.SH REPORTING BUGS
Bugs and feature requests can be reported on GitHub at https://github.com/keepassxreboot/keepassxc/issues.
Expand Down