From aa3efb449f4979398c60e64ed072c7a5b8feb90d Mon Sep 17 00:00:00 2001 From: Syping Date: Sun, 25 Oct 2020 00:24:45 +0200 Subject: [PATCH] strict mode bruteforcing --- src/brutethread.cpp | 8 ++++-- src/brutethread.h | 3 +- src/main.cpp | 20 +++++-------- src/mainthread.cpp | 69 ++++++++++++++++++++++++++++----------------- 4 files changed, 58 insertions(+), 42 deletions(-) diff --git a/src/brutethread.cpp b/src/brutethread.cpp index 56b5fb0..a821d2e 100644 --- a/src/brutethread.cpp +++ b/src/brutethread.cpp @@ -19,7 +19,7 @@ #include #include "brutethread.h" -brutethread::brutethread(const QByteArray &fileContent, quint64 length, QVector checksum_vector, QVector checksums) : checksum_vector(checksum_vector), checksums(checksums), fileContent(fileContent), length(length) {} +brutethread::brutethread(const QByteArray &fileContent, quint64 length, QVector checksum_vector, QVector checksums, bool strictMatch) : checksum_vector(checksum_vector), checksums(checksums), fileContent(fileContent), strictMatch(strictMatch), length(length) {} void brutethread::run() { @@ -29,7 +29,11 @@ void brutethread::run() const QByteArray content = fileContent.mid(seek, length); const QByteArray generatedHash = generator->generateChecksum(content).toHex(); 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; emit matched(); } diff --git a/src/brutethread.h b/src/brutethread.h index e6d3574..d011af9 100644 --- a/src/brutethread.h +++ b/src/brutethread.h @@ -33,13 +33,14 @@ class brutethread : public QThread { Q_OBJECT public: - explicit brutethread(const QByteArray &fileContent, quint64 length, QVector checksum_vector, QVector checksums); + explicit brutethread(const QByteArray &fileContent, quint64 length, QVector checksum_vector, QVector checksums, bool strictMatch); void run(); private: QVector checksum_vector; QVector checksums; QByteArray fileContent; + bool strictMatch; quint64 length; signals: diff --git a/src/main.cpp b/src/main.cpp index c633619..a85a050 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -32,7 +32,7 @@ int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); a.setApplicationName("checkbrute"); - a.setApplicationVersion("0.1"); + a.setApplicationVersion("0.1.1"); QCommandLineParser commandLineParser; commandLineParser.addHelpOption(); @@ -86,27 +86,21 @@ int main(int argc, char *argv[]) checksum_map["MD4"] = new checksum_qt(QCryptographicHash::Md4, "MD4"); checksum_map["MD5"] = new checksum_qt(QCryptographicHash::Md5, "MD5"); 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-384"] = new checksum_qt(QCryptographicHash::Sha384, "SHA2-384"); 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-384"] = new checksum_qt(QCryptographicHash::Sha3_384, "SHA3-384"); checksum_map["SHA3-512"] = new checksum_qt(QCryptographicHash::Sha3_512, "SHA3-512"); #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-384"] = new checksum_qt(QCryptographicHash::Keccak_384, "Keccak-384"); checksum_map["Keccak-512"] = new checksum_qt(QCryptographicHash::Keccak_512, "Keccak-512"); #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); QObject::connect(&instance, &mainthread::finished, &a, &QCoreApplication::quit); if (!instance.isInitialised()) diff --git a/src/mainthread.cpp b/src/mainthread.cpp index eb51326..5209d38 100644 --- a/src/mainthread.cpp +++ b/src/mainthread.cpp @@ -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 &checksum_map) : checksum_map(checksum_map), strictHash(strictHash) { - if (!algorithms.isEmpty()) { - for (const QString &algorithm : algorithms.split(',')) { - std::map::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::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); if (bruteforceFileHandler.open(QIODevice::ReadOnly)) { QTextStream(stderr) << "INFO: Reading source file " << bruteforceFile << "..." << Qt::endl; @@ -92,6 +69,46 @@ mainthread::mainthread(const QString &bruteforceFile, const QString &checksumsFi return; } + if (!algorithms.isEmpty()) { + for (const QString &algorithm : algorithms.split(',')) { + std::map::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::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()) { bool startOk; begin = start.toULongLong(&startOk); @@ -250,7 +267,7 @@ void mainthread::run() if (threads >= mthreads) eventLoop.exec(); 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, &brutethread::matched, this, &mainthread::matched); thread->start(QThread::LowPriority); @@ -261,7 +278,7 @@ void mainthread::run() if (threads >= mthreads) eventLoop.exec(); 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, &brutethread::matched, this, &mainthread::matched); thread->start(QThread::LowPriority); @@ -273,7 +290,7 @@ void mainthread::run() if (threads >= mthreads) eventLoop.exec(); 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, &brutethread::matched, this, &mainthread::matched); thread->start(QThread::LowPriority);