strict mode bruteforcing

This commit is contained in:
Syping 2020-10-25 00:24:45 +02:00
parent 386da45ce2
commit aa3efb449f
4 changed files with 58 additions and 42 deletions

View file

@ -19,7 +19,7 @@
#include <QTextStream> #include <QTextStream>
#include "brutethread.h" #include "brutethread.h"
brutethread::brutethread(const QByteArray &fileContent, quint64 length, QVector<checksum*> checksum_vector, QVector<QByteArray> checksums) : checksum_vector(checksum_vector), checksums(checksums), fileContent(fileContent), length(length) {} brutethread::brutethread(const QByteArray &fileContent, quint64 length, QVector<checksum*> checksum_vector, QVector<QByteArray> checksums, bool strictMatch) : checksum_vector(checksum_vector), checksums(checksums), fileContent(fileContent), strictMatch(strictMatch), length(length) {}
void brutethread::run() void brutethread::run()
{ {
@ -29,7 +29,11 @@ void brutethread::run()
const QByteArray content = fileContent.mid(seek, length); const QByteArray content = fileContent.mid(seek, length);
const QByteArray generatedHash = generator->generateChecksum(content).toHex(); const QByteArray generatedHash = generator->generateChecksum(content).toHex();
for (const QByteArray &hash : checksums) { for (const QByteArray &hash : checksums) {
if (generatedHash.left(hash.length()) == hash) { if (!strictMatch && generatedHash.left(hash.length()) == hash) {
QTextStream(stdout) << "MATCH: " << generator->formatName() << " Checksum " << hash << " Start " << seek << " End " << seek+length << Qt::endl;
emit matched();
}
else if (strictMatch && generatedHash.length() == hash.length() && generatedHash == hash) {
QTextStream(stdout) << "MATCH: " << generator->formatName() << " Checksum " << hash << " Start " << seek << " End " << seek+length << Qt::endl; QTextStream(stdout) << "MATCH: " << generator->formatName() << " Checksum " << hash << " Start " << seek << " End " << seek+length << Qt::endl;
emit matched(); emit matched();
} }

View file

@ -33,13 +33,14 @@ class brutethread : public QThread
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit brutethread(const QByteArray &fileContent, quint64 length, QVector<checksum*> checksum_vector, QVector<QByteArray> checksums); explicit brutethread(const QByteArray &fileContent, quint64 length, QVector<checksum*> checksum_vector, QVector<QByteArray> checksums, bool strictMatch);
void run(); void run();
private: private:
QVector<checksum*> checksum_vector; QVector<checksum*> checksum_vector;
QVector<QByteArray> checksums; QVector<QByteArray> checksums;
QByteArray fileContent; QByteArray fileContent;
bool strictMatch;
quint64 length; quint64 length;
signals: signals:

View file

@ -32,7 +32,7 @@ int main(int argc, char *argv[])
{ {
QCoreApplication a(argc, argv); QCoreApplication a(argc, argv);
a.setApplicationName("checkbrute"); a.setApplicationName("checkbrute");
a.setApplicationVersion("0.1"); a.setApplicationVersion("0.1.1");
QCommandLineParser commandLineParser; QCommandLineParser commandLineParser;
commandLineParser.addHelpOption(); commandLineParser.addHelpOption();
@ -86,27 +86,21 @@ int main(int argc, char *argv[])
checksum_map["MD4"] = new checksum_qt(QCryptographicHash::Md4, "MD4"); checksum_map["MD4"] = new checksum_qt(QCryptographicHash::Md4, "MD4");
checksum_map["MD5"] = new checksum_qt(QCryptographicHash::Md5, "MD5"); checksum_map["MD5"] = new checksum_qt(QCryptographicHash::Md5, "MD5");
checksum_map["SHA1"] = new checksum_qt(QCryptographicHash::Sha1, "SHA1"); checksum_map["SHA1"] = new checksum_qt(QCryptographicHash::Sha1, "SHA1");
checksum_map["SHA2-224"] = new checksum_qt(QCryptographicHash::Sha224, "SHA2-224");
checksum_map["SHA2-256"] = new checksum_qt(QCryptographicHash::Sha256, "SHA2-256"); checksum_map["SHA2-256"] = new checksum_qt(QCryptographicHash::Sha256, "SHA2-256");
checksum_map["SHA2-384"] = new checksum_qt(QCryptographicHash::Sha384, "SHA2-384");
checksum_map["SHA2-512"] = new checksum_qt(QCryptographicHash::Sha512, "SHA2-512"); checksum_map["SHA2-512"] = new checksum_qt(QCryptographicHash::Sha512, "SHA2-512");
checksum_map["SHA3-224"] = new checksum_qt(QCryptographicHash::Sha3_224, "SHA3-224");
checksum_map["SHA3-256"] = new checksum_qt(QCryptographicHash::Sha3_256, "SHA3-256"); checksum_map["SHA3-256"] = new checksum_qt(QCryptographicHash::Sha3_256, "SHA3-256");
checksum_map["SHA3-384"] = new checksum_qt(QCryptographicHash::Sha3_384, "SHA3-384");
checksum_map["SHA3-512"] = new checksum_qt(QCryptographicHash::Sha3_512, "SHA3-512"); checksum_map["SHA3-512"] = new checksum_qt(QCryptographicHash::Sha3_512, "SHA3-512");
#if QT_VERSION >= 0x050902 #if QT_VERSION >= 0x050902
checksum_map["Keccak-224"] = new checksum_qt(QCryptographicHash::Keccak_224, "Keccak-224");
checksum_map["Keccak-256"] = new checksum_qt(QCryptographicHash::Keccak_256, "Keccak-256"); checksum_map["Keccak-256"] = new checksum_qt(QCryptographicHash::Keccak_256, "Keccak-256");
checksum_map["Keccak-384"] = new checksum_qt(QCryptographicHash::Keccak_384, "Keccak-384");
checksum_map["Keccak-512"] = new checksum_qt(QCryptographicHash::Keccak_512, "Keccak-512"); checksum_map["Keccak-512"] = new checksum_qt(QCryptographicHash::Keccak_512, "Keccak-512");
#endif #endif
// 224 and 384 bit hashes are partial
if (strictHash) {
checksum_map["SHA2-224"] = new checksum_qt(QCryptographicHash::Sha224, "SHA2-224");
checksum_map["SHA2-384"] = new checksum_qt(QCryptographicHash::Sha384, "SHA2-384");
checksum_map["SHA3-224"] = new checksum_qt(QCryptographicHash::Sha3_224, "SHA3-224");
checksum_map["SHA3-384"] = new checksum_qt(QCryptographicHash::Sha3_384, "SHA3-384");
#if QT_VERSION >= 0x050902
checksum_map["Keccak-224"] = new checksum_qt(QCryptographicHash::Keccak_224, "Keccak-224");
checksum_map["Keccak-384"] = new checksum_qt(QCryptographicHash::Keccak_384, "Keccak-384");
#endif
}
mainthread instance(bruteforceFile, checksumsFile, algorithms, start, stop, lengths, threads, strictHash, checksum_map); mainthread instance(bruteforceFile, checksumsFile, algorithms, start, stop, lengths, threads, strictHash, checksum_map);
QObject::connect(&instance, &mainthread::finished, &a, &QCoreApplication::quit); QObject::connect(&instance, &mainthread::finished, &a, &QCoreApplication::quit);
if (!instance.isInitialised()) if (!instance.isInitialised())

View file

@ -26,29 +26,6 @@
mainthread::mainthread(const QString &bruteforceFile, const QString &checksumsFile, const QString &algorithms, const QString &start, const QString &stop, const QString &lenghts_str, const QString &threads_str, bool strictHash, const std::map<std::string,checksum*> &checksum_map) : checksum_map(checksum_map), strictHash(strictHash) mainthread::mainthread(const QString &bruteforceFile, const QString &checksumsFile, const QString &algorithms, const QString &start, const QString &stop, const QString &lenghts_str, const QString &threads_str, bool strictHash, const std::map<std::string,checksum*> &checksum_map) : checksum_map(checksum_map), strictHash(strictHash)
{ {
if (!algorithms.isEmpty()) {
for (const QString &algorithm : algorithms.split(',')) {
std::map<std::string,checksum*>::const_iterator it;
it = checksum_map.find(algorithm.toUpper().toStdString());
if (it != checksum_map.end()) {
QTextStream(stderr) << "INFO: Using " << QString::fromStdString(it->first) << " algorithm" << Qt::endl;
checksum_vector << it->second;
}
}
}
else {
std::map<std::string,checksum*>::const_iterator it;
for (it = checksum_map.begin(); it != checksum_map.end(); it++) {
QTextStream(stderr) << "INFO: Using " << QString::fromStdString(it->first) << " algorithm" << Qt::endl;
checksum_vector << it->second;
}
}
if (checksum_vector.isEmpty()) {
QTextStream(stderr) << "ERROR: No algorithms are used!" << Qt::endl;
initialised = false;
return;
}
QFile bruteforceFileHandler(bruteforceFile); QFile bruteforceFileHandler(bruteforceFile);
if (bruteforceFileHandler.open(QIODevice::ReadOnly)) { if (bruteforceFileHandler.open(QIODevice::ReadOnly)) {
QTextStream(stderr) << "INFO: Reading source file " << bruteforceFile << "..." << Qt::endl; QTextStream(stderr) << "INFO: Reading source file " << bruteforceFile << "..." << Qt::endl;
@ -92,6 +69,46 @@ mainthread::mainthread(const QString &bruteforceFile, const QString &checksumsFi
return; return;
} }
if (!algorithms.isEmpty()) {
for (const QString &algorithm : algorithms.split(',')) {
std::map<std::string,checksum*>::const_iterator it;
it = checksum_map.find(algorithm.toUpper().toStdString());
if (it != checksum_map.end()) {
QTextStream(stderr) << "INFO: Using " << QString::fromStdString(it->first) << " algorithm" << Qt::endl;
checksum_vector << it->second;
}
}
}
else {
std::map<std::string,checksum*>::const_iterator it;
for (it = checksum_map.begin(); it != checksum_map.end(); it++) {
const int checksumSize = it->second->checksumSize();
if (!strictHash) {
for (const QByteArray &hash : checksums) {
if (checksumSize >= hash.count() / 2) {
QTextStream(stderr) << "INFO: Using " << QString::fromStdString(it->first) << " algorithm" << Qt::endl;
checksum_vector << it->second;
break;
}
}
}
else {
for (const QByteArray &hash : checksums) {
if (checksumSize == hash.count() / 2) {
QTextStream(stderr) << "INFO: Using " << QString::fromStdString(it->first) << " algorithm" << Qt::endl;
checksum_vector << it->second;
break;
}
}
}
}
}
if (checksum_vector.isEmpty()) {
QTextStream(stderr) << "ERROR: No algorithms are used!" << Qt::endl;
initialised = false;
return;
}
if (!start.isEmpty()) { if (!start.isEmpty()) {
bool startOk; bool startOk;
begin = start.toULongLong(&startOk); begin = start.toULongLong(&startOk);
@ -250,7 +267,7 @@ void mainthread::run()
if (threads >= mthreads) if (threads >= mthreads)
eventLoop.exec(); eventLoop.exec();
QTextStream(stderr) << "[" << std::distance(lengths.constBegin(), it) + 1 << "/" << lengthsCount << "] [" << min << "/" << max << "] Bruteforcing...\r"; QTextStream(stderr) << "[" << std::distance(lengths.constBegin(), it) + 1 << "/" << lengthsCount << "] [" << min << "/" << max << "] Bruteforcing...\r";
brutethread *thread = new brutethread(fileContent, min, checksum_vector, checksums); brutethread *thread = new brutethread(fileContent, min, checksum_vector, checksums, strictHash);
QObject::connect(thread, &QThread::finished, this, &mainthread::threadFinished); QObject::connect(thread, &QThread::finished, this, &mainthread::threadFinished);
QObject::connect(thread, &brutethread::matched, this, &mainthread::matched); QObject::connect(thread, &brutethread::matched, this, &mainthread::matched);
thread->start(QThread::LowPriority); thread->start(QThread::LowPriority);
@ -261,7 +278,7 @@ void mainthread::run()
if (threads >= mthreads) if (threads >= mthreads)
eventLoop.exec(); eventLoop.exec();
QTextStream(stderr) << "[" << std::distance(lengths.constBegin(), it) + 1 << "/" << lengthsCount << "] [" << clength << "/" << max << "] Bruteforcing...\r"; QTextStream(stderr) << "[" << std::distance(lengths.constBegin(), it) + 1 << "/" << lengthsCount << "] [" << clength << "/" << max << "] Bruteforcing...\r";
brutethread *thread = new brutethread(fileContent, clength, checksum_vector, checksums); brutethread *thread = new brutethread(fileContent, clength, checksum_vector, checksums, strictHash);
QObject::connect(thread, &QThread::finished, this, &mainthread::threadFinished); QObject::connect(thread, &QThread::finished, this, &mainthread::threadFinished);
QObject::connect(thread, &brutethread::matched, this, &mainthread::matched); QObject::connect(thread, &brutethread::matched, this, &mainthread::matched);
thread->start(QThread::LowPriority); thread->start(QThread::LowPriority);
@ -273,7 +290,7 @@ void mainthread::run()
if (threads >= mthreads) if (threads >= mthreads)
eventLoop.exec(); eventLoop.exec();
QTextStream(stderr) << "[" << std::distance(lengths.constBegin(), it) + 1 << "/" << lengthsCount << "] [" << clength << "/" << max << "] Bruteforcing...\r"; QTextStream(stderr) << "[" << std::distance(lengths.constBegin(), it) + 1 << "/" << lengthsCount << "] [" << clength << "/" << max << "] Bruteforcing...\r";
brutethread *thread = new brutethread(fileContent, clength, checksum_vector, checksums); brutethread *thread = new brutethread(fileContent, clength, checksum_vector, checksums, strictHash);
QObject::connect(thread, &QThread::finished, this, &mainthread::threadFinished); QObject::connect(thread, &QThread::finished, this, &mainthread::threadFinished);
QObject::connect(thread, &brutethread::matched, this, &mainthread::matched); QObject::connect(thread, &brutethread::matched, this, &mainthread::matched);
thread->start(QThread::LowPriority); thread->start(QThread::LowPriority);