Advertisement
Shokedbrain

Bruteforcing MD5 hash

Mar 19th, 2022
1,792
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3. #include <Windows.h>
  4. #include <wincrypt.h>
  5. #include <chrono>
  6.  
  7. using namespace std;
  8.  
  9. // вывод ошибок
  10. void ErrorHandling(string message)
  11. {
  12.     DWORD dw = GetLastError();
  13.     cout << "Error with: " << message << "\n";
  14.     cout << "Error code: " << dw << "\n";
  15. }
  16.  
  17. string CreateMD5(string input)
  18. {
  19.     HCRYPTPROV  hCryptProv;
  20.     HCRYPTHASH  hHash;
  21.     HCRYPTHASH  hDuplicatedHash;
  22.     if (CryptAcquireContext(
  23.         &hCryptProv,
  24.         nullptr,
  25.         nullptr,
  26.         PROV_RSA_FULL,
  27.         0))
  28.     {
  29.         //cout << "CryptAcquireContext complete. \n";
  30.     }
  31.     else
  32.     {
  33.         ErrorHandling("Acquisition of context failed.");
  34.     }
  35.  
  36.     // получить дескриптор хэш объекта
  37.  
  38.     if (CryptCreateHash(
  39.         hCryptProv,
  40.         CALG_MD5,
  41.         0,
  42.         0,
  43.         &hHash))
  44.     {
  45.         //cout << "An empty hash object has been created. \n";
  46.     }
  47.     else
  48.     {
  49.         ErrorHandling("CryptBeginHash");
  50.     }
  51.  
  52.     if (CryptHashData(
  53.         hHash, reinterpret_cast<const BYTE*>(input.c_str()), input.size(),0
  54.     ))
  55.     {
  56.         //cout << "CryptHashData executed successfully\n";
  57.     }
  58.     else
  59.     {
  60.         ErrorHandling("CryptHashData");
  61.     }
  62.  
  63.    
  64.  
  65.     if (CryptDuplicateHash(
  66.         hHash,
  67.         nullptr,
  68.         0,
  69.         &hDuplicatedHash
  70.     ))
  71.     {
  72.         //cout << "The hash has been duplicated. \n";
  73.     }
  74.     else
  75.     {
  76.         ErrorHandling("CryptDuplicateHash");
  77.     }
  78.  
  79.     BYTE*        pbHash;
  80.     DWORD        dwHashLen;
  81.     DWORD        dwHashLenSize = sizeof(DWORD);
  82.  
  83.     if (CryptGetHashParam(
  84.         hDuplicatedHash,
  85.         HP_HASHSIZE,
  86.         reinterpret_cast<BYTE*>(&dwHashLen),
  87.         &dwHashLenSize,
  88.         0
  89.     ))
  90.     {
  91.         //cout << "CryptGetHashParam HP_HASHSIZE executed successfully\n";
  92.     }
  93.     else
  94.     {
  95.         ErrorHandling("CryptGetHashParam failed to get size.");
  96.     }
  97.  
  98.     if (!(pbHash = static_cast<BYTE*>(malloc(dwHashLen))))
  99.         cout << "Allocation failed\n";
  100.  
  101.  
  102.     string res{};
  103.     if (CryptGetHashParam(
  104.         hHash,
  105.         HP_HASHVAL,
  106.         pbHash,
  107.         &dwHashLen,
  108.         0))
  109.     {
  110.         if (pbHash == nullptr)
  111.         {
  112.             cout << "system error\n";
  113.             exit(1);
  114.         }
  115.         // первый вариант вывода
  116.         //cout << "hash is: ";
  117.         for (size_t i = 0; i!= 16; ++i)
  118.         {
  119.             res += "0123456789abcdef"[pbHash[i] / 16];
  120.             res += "0123456789abcdef"[pbHash[i] % 16];
  121.         }
  122.         //cout << res << "\n";
  123.  
  124.     }
  125.     else
  126.     {
  127.         ErrorHandling("CryptGetHashParam\n");
  128.     }
  129.  
  130.     if (hHash)
  131.         CryptDestroyHash(hHash);
  132.     if (hDuplicatedHash)
  133.         CryptDestroyHash(hDuplicatedHash);
  134.     if (hCryptProv)
  135.         CryptReleaseContext(hCryptProv, 0);
  136.     return res;
  137. }
  138.  
  139. int main()
  140. {
  141.     string input = CreateMD5("9560");
  142.     cout << "Bruteforcing " << input << " ...\n";
  143.     auto start = std::chrono::high_resolution_clock::now();
  144.     auto stop = start;
  145.     auto found = false;
  146.     for (int i = 0; i <= 9999; i++)
  147.     {
  148.         if (CreateMD5(to_string(i)) == input)
  149.         {
  150.             cout << "Found! " << i  << " " << CreateMD5(to_string(i)) << "\n";
  151.             found = true;
  152.             stop = std::chrono::high_resolution_clock::now();
  153.             std::cout << "Time: " << std::chrono::duration_cast<std::chrono::seconds>(stop - start).count()
  154.                 << " seconds\n";
  155.             break;
  156.         }
  157.     }
  158.     if (!found)
  159.         cout << "Hash MD5 doesn't found!\n";
  160.     return 0;
  161. }
  162.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement