C++高效md5爆破脚本(根据需要更改)

发布于 2024-08-29  51 次阅读


C++md5爆破脚本(根据需要更改)

#include <iostream>
#include <string>
#include <openssl/md5.h>
#include <thread>
#include <vector>
#include <atomic>
#include <cstdlib>
#include <ctime>

const std::string CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

std::string generate_random_string(size_t length) {
    std::string str = "0e";  // 初始设置字符串以 "0e" 开头
    for (size_t i = 2; i < length; ++i) {  // 从索引2开始填充,因为0和1已经被 "0e" 占用
        str += CHARS[rand() % CHARS.size()];
    }
    return str;
}

std::string md5(const std::string& input) {
    unsigned char digest[MD5_DIGEST_LENGTH];
    MD5((unsigned char*)input.c_str(), input.size(), (unsigned char*)&digest);

    char md5_string[33];
    for (int i = 0; i < 16; ++i) {
        sprintf(&md5_string[i * 2], "%02x", (unsigned int)digest[i]);
    }
    return std::string(md5_string);
}

bool is_interesting(const std::string& hash) {
    return hash.substr(0, 2) == "0e" && hash.substr(2).find_first_not_of("0123456789") == std::string::npos;
}

void find_collision(std::atomic<bool>& found, size_t string_size) {
    while (!found.load()) {
        std::string random_string = generate_random_string(string_size);
        std::string hash = md5(random_string);

        if (is_interesting(hash)) {
            found.store(true);
            std::cout << "Found: " << random_string << " => " << hash << std::endl;
        }
    }
}

int main() {
    srand(time(0));
    size_t string_size = 20;  // 调整字符串长度以包括 "0e" 的两个字符
    std::atomic<bool> found(false);
    unsigned int num_threads = std::thread::hardware_concurrency();

    std::vector<std::thread> threads;
    for (unsigned int i = 0; i < num_threads; ++i) {
        threads.emplace_back(find_collision, std::ref(found), string_size);
    }

    for (auto& t : threads) {
        t.join();
    }

    return 0;
}

Found: 0eTkNsTreBs8G6VEXVB5 => 0e037642729645899252730799844376

使用例题,moectf2024-静态页面

image-20240821181739168

QQ:2219349024
最后更新于 2024-08-29