Advertisement
Hesse99

SSS - LAB 1

Feb 28th, 2021 (edited)
744
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include <cstring>
  4. #include <crypt.h>
  5.  
  6. using namespace std;
  7.  
  8. //this is an example line from the shadow file:
  9. //$6$Iy/hHRfM$gC.Fw7CbqG.Qc9p9X59Tmo5uEHCf0ZAKCsPZuiYUKcejrsGuZtES1VQiusSTen0NRUPYN0v1z76PwX2G2.v1l1:15001:0:99999:7:::
  10. // the salt and password values are extracted as
  11. //$6$SvT3dVpN
  12. //$6$SvT3dVpN$lwb3GViLl0J0ntNk5BAWe2WtkbjSBMXtSkDCtZUkVhVPiz5X37WflWL4k3ZUusdoyh7IOUlSXE1jUHxIrg29p.
  13. string target_salt = "$6$SvT3dVpN";
  14. string target_pw_hash = "$6$SvT3dVpN$lwb3GViLl0J0ntNk5BAWe2WtkbjSBMXtSkDCtZUkVhVPiz5X37WflWL4k3ZUusdoyh7IOUlSXE1jUHxIrg29p.";
  15.  
  16. // define a null string which is returned in case of failure to find the password
  17. char null[] = {'\0'};
  18.  
  19. // define the maximum length for the password to be searched
  20. #define MAX_LEN 8
  21.  
  22. list<char*> pwlist;
  23.  
  24. // check if the pw and salt are matching the hash
  25. int check_password(char* pw, char* salt, char* hash)
  26. {
  27.     char* res = crypt(pw, salt);
  28.     cout << "password " << pw << "\n";
  29.     cout << "hashes to " << res << "\n";
  30.     for (int i = 0; i<strlen(hash); i++)
  31.         if (res[i]!=hash[i])
  32.             return 0;
  33.     cout << "match !!!" << "\n";
  34.     return 1;
  35. }
  36.  
  37. // builds passwords from the given character set
  38. // and verifies if they match the target
  39. char* exhaustive_search(char* charset, char* salt, char* target)
  40. {
  41.     char* current_password;
  42.     char* new_password;
  43.     int i, current_len;
  44.    
  45.     // begin by adding each character as a potential 1 character password
  46.     for (i = 0; i<strlen(charset); i++)
  47.     {
  48.         new_password = new char[2];
  49.         new_password[0] = charset[i];
  50.         new_password[1] = '\0';
  51.         pwlist.push_back(new_password);
  52.     }
  53.    
  54.     while(true){
  55.        
  56.         // test if queue is not empty and return null if so
  57.         if (pwlist.empty())
  58.             return null;
  59.        
  60.         // get the current current_password from queue
  61.         current_password = pwlist.front();
  62.         current_len = strlen(current_password);
  63.        
  64.         //  check  if  current  password  is  the  target  password,  if  yes  return  the current_password
  65.         if (check_password(current_password, salt, target))
  66.             return current_password;
  67.         //  else  generates  new  passwords  from  the  current  one  by  appending each character from the charlist
  68.         // only if the current length is less than the maxlength
  69.         if(current_len < MAX_LEN){
  70.             for (i = 0; i < strlen(charset); i++){
  71.                 new_password = new char[current_len + 2];
  72.                 memcpy(new_password, current_password, current_len);
  73.                 new_password[current_len] = charset[i];
  74.                 new_password[current_len+1] = '\0';
  75.                 pwlist.push_back(new_password);
  76.             }
  77.         }
  78.         // now remove the front element as it didn't match the password
  79.         pwlist.pop_front();
  80.     }
  81. }
  82.  
  83. main()
  84. {
  85.     char* salt;
  86.     char* target;
  87.     char* password;
  88.    
  89.     // define the character set from which the password will be built
  90.     char charset[] = {'a', 'b', 'c', '1', '2', '!', '@', '#', '\0'};
  91.     //convert the salt from string to char*
  92.     salt = new char[target_salt.length()+1];
  93.     copy(target_salt.begin(), target_salt.end(), salt);
  94.     //convert the hash from string to char*
  95.     target = new char[target_pw_hash.length()+1];
  96.     copy(target_pw_hash.begin(), target_pw_hash.end(), target);
  97.     //start the search
  98.     password = exhaustive_search(charset, salt, target);
  99.     if  (strlen(password)!= 0)
  100.         cout  <<  "Password  successfuly  recovered:  "  << password << "  \n";
  101.     else
  102.         cout << "Failure to find password, try distinct character set of size \n";
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement