daniel_lawson9999

Crack a password program (crypt())

Oct 11th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.83 KB | None | 0 0
  1. //Daniel Lawson
  2. #define _XOPEN_SOURCE
  3. #include <unistd.h>
  4. #include <stdio.h>
  5. #include <cs50.h>
  6. #include <string.h>
  7. string getPassword(string hash, string salt, char letters[], char output[]);
  8.  
  9. //void getPasswordRecursive(string hash, string salt, char letters[], int max, int startStrInt); //explanation for this further down...
  10. int main(int argc, string argv[])
  11. {
  12.     if (argc != 2)//make sure they entered two arguments
  13.     {
  14.         printf("enter two args (./crack 50jKHl3SFaNms)\n");
  15.         return 1;
  16.     }
  17.     char salt[3]; //create a string for the salt
  18.     salt[0] = argv[1][0]; //get the first character of the salt
  19.     salt[1] = argv[1][1]; //get the second character of the salt
  20.     salt[2] = '\0'; //add a null terminator
  21.     string hash = argv[1]; //get the hash
  22.     char letters[52]; //create an array for all possible letters
  23.     for (int i = 0; i < 26; i++) //fill the array with lowercase and capital letters
  24.     {
  25.         letters[i] = (char) 97 + i; //capital
  26.         letters[i + 26] = (char) 65 + i; //lowercase
  27.     }
  28.     char output[5]; //array to store output, must be created outside of the function,
  29.     //so the memory allocated to the array is not cleared
  30.     string password = getPassword(hash, salt, letters, output); //call  function getPassword
  31.     printf("%s\n", password); //print the password
  32.     //getPasswordRecursive(hash,salt,letters, 4, 0); //call the recursive version, get the same output (it doesn't work right now..)
  33.     return 0;
  34. }
  35.  
  36. /*hash stores the hash (including the salt), salt stores the seperated salt
  37. letters stores the possible characters, output stores possible passwords and returns the password
  38. why use a method with a return type? allows you break out of the function as soon as the answer is found
  39. */
  40.  
  41. string getPassword(string hash, string salt, char letters[], char output[])
  42. {
  43.  
  44.     for (int i = 0; i < 52; i++)//try a string starting with each character
  45.     {
  46.         output[0] = letters[i];//add a character to output
  47.         output[1] = '\0'; //add a null terminator
  48.         if (strcmp(crypt(output, salt), hash) == 0) //test the current state of output
  49.         {
  50.             return output;
  51.         }
  52.         for (int j = 0; j < 52; j++) //add another character to each previous character combination
  53.         {
  54.             output[1] = letters[j];//add a character to output, preserving previous characters from this loop
  55.             output[2] = '\0'; //add a null terminator
  56.             if (strcmp(crypt(output, salt), hash) == 0) //test the current state of output
  57.             {
  58.                 return output;
  59.             }
  60.             for (int k = 0; k < 52; k++)
  61.             {
  62.                 output[2] = letters[k];//add another letter, preserving the previous two
  63.                 output[3] = '\0'; //add a null terminator
  64.                 if (strcmp(crypt(output, salt), hash) == 0) //test the current state of output
  65.                 {
  66.                     return output;
  67.                 }
  68.                 for (int l = 0; l < 52; l++)
  69.                 {
  70.                     output[3] = letters[l];//add another letter, preserving the previous two
  71.                     output[4] = '\0'; //add a null terminator
  72.                     if (strcmp(crypt(output, salt), hash) == 0) //test the current state of output
  73.                     {
  74.                         return output;
  75.                     }
  76.                 }
  77.             }
  78.         }
  79.     }
  80.     return "answer not found"; //return answer not found if no combinatios up to 4 characters long were found
  81. }
  82. //                                      NORMAL PROGRAM ENDS HERE
  83. //                                   enter bellow at your own risk
  84.  
  85. /*recursive version that I didn't completely finish. The point of making it recursive
  86. is so that you can solve passwords greater than 4 characters (determined by what number you pass with max)
  87.  
  88. besides the logical errors that is causing the function not to work, I also need the function to check a global boolean
  89. it would be declared outside of the function, and in addition to end case (I think that's what it's called) which is max!=0 of the function
  90. , it would also check to see if the global boolean false, which is turned to false when an answer is found. This allows
  91. one unrelated branch to tell all the other branches to stop making more recurive calls.
  92.  
  93. for example if the password was just A, which is found pretty much immediately, if there was no boolean, and the function was set to look
  94. up to 4 characters, it would run 9834495 more loops than it needed to.
  95. */
  96. /*void getPasswordRecursive(string hash, string salt, char letters[], int max, int startStrInt){
  97.     if(max != 0){
  98.         max--;
  99.         int length = 0;
  100.         if(!startStrInt == 0){
  101.             length = snprintf(NULL, 0 , "%d" , startStrInt);
  102.         }
  103.         char startStr[length + 1];
  104.         snprintf(startStr, length +1, "%d", startStrInt);
  105.         startStr[length] = '\0';
  106.         int outputLength = length +2;
  107.         char output[outputLength];
  108.         strcpy(output,startStr);
  109.         for(int i = 0; i < 52; i++){
  110.             output[outputLength- 1] = '\0';
  111.             output[outputLength -2] = letters[i];
  112.             if(strcmp(crypt(output,salt),hash) == 0){
  113.                 printf("%s\n", output);
  114.             }
  115.             else{
  116.                 startStrInt = atoi(output);
  117.                 getPasswordRecursive(hash, salt, letters, max, startStrInt);
  118.             }
  119.         }
  120.     }
  121. }*/
  122.  
  123.  
  124. /*psuedocode
  125. make sure the user entered a hash in the command line
  126. assembly an array of possibly characters
  127. do the bruteforce:
  128.     for loop over each character - compare
  129.         for loop over each character - compare
  130.             for loop over each character - compare
  131.                 for loop over each character - compare
  132.  
  133. */
  134.  
  135. /*
  136.  
  137. desired output tests:
  138. $ ./crack 50fkUxYHbnXGw
  139. rofl
  140.  
  141. andi:50.jPgLzVirkc
  142.  
  143. */
Add Comment
Please, Sign In to add comment