2017-09-14 6 views
1

J'essaie de casser les mots de passe de maximum 4 lettres, à partir d'un hachage. Idéalement, je peux écrire ./crack 50fkUxYHbnXGw et il renvoie rofl.crack mot de passe court avec la force brute: imbriqué pour la boucle

Mon approche est avec une boucle for imbriquée. Pour l'appliquer à des chaînes de longueurs variables, il m'a été recommandé d'utiliser un terminateur null en C, mais pour l'instant j'ai essayé une instruction if pour chaque longueur possible.

Connaissez-vous une façon plus élégante de faire cela?

Enfin, j'ai un bug qui ne me donne aucun résultat.

#define _XOPEN_SOURCE // for crypt 
#include <cs50.h> // used for get_string 
#include <stdio.h> // used for printf 
#include <string.h> // used for strlen 
#include <ctype.h> // used for isalpha 
#include <stdlib.h> // for atoi 
#include <crypt.h> // for crypt 

int main(int argc, char* argv[]) 
{ 
    // accept only two arguments from command line 
    // hash is max 13 characters. First two are the salt. 
    // next 11 are the key 
    if (argc != 2) 
    { 
     printf("Usage: ./crack hash \n"); 
     return 1; 
    } 

    // make second command line argument into string 
    char* hash = argv[1]; 
    char* salt = argv[1]; 

    strncpy(salt, hash, 2); 
    salt[2] = 0; // null terminates destination 

    char* key = hash + 2; // just key without salt 

    char* abAB = "abcdefghijklmnopqrstuvwxyzQWERTYUIOPASDFGHJKLZXCVBNM"; 

    for (int i = 0, n = strlen(abAB); i < n; i++) 
    { //check for every length 
     char tri1[1] = {abAB[i]}; 
     // produce my own encrpyted key with salt 
     char* cr1 = crypt(tri1, salt); 
     // if own key is equal to key provided, print out the string tried. 
     int comp1 = strcmp(cr1, key); 

     if (comp1 == 0) 
     { 
      printf("%s", tri1); 
     } 

     for (int j = 0, m = strlen(abAB); j < m; j++) 
     { 
       char tri2[2] = {abAB[i],abAB[j]}; 
       char* cr2 = crypt(tri2, salt); 
       int comp2 = strcmp(cr2, key); 
       if (comp2 == 0) 
       { 
        printf("%s", tri2); 
       } 

      for (int k = 0, p = strlen(abAB); k < p; k++) 
      { 
       char tri3[3] = {abAB[i],abAB[j],abAB[k]}; 
       char* cr3 = crypt(tri3, salt); 
       int comp3 = strcmp(cr3, key); 
       if (comp3 == 0) 
       { 
        printf("%s", tri3); 
       } 

       for (int l = 0, q = strlen(abAB); l < q; l++) 
       { 
        char tri4[4] = {abAB[i],abAB[j],abAB[k],abAB[l]}; 
        // produce my own encrpyted key with salt 
        char* cr4 = crypt(tri4, salt); 
        // if own key is equal to key provided, print out the string tried. 
        int comp4 = strcmp(cr4, key); 
        if (comp4 == 0) 
        { 
         printf("%s", tri4); 
        } 
       } 
      } 
     } 
    } 
    printf("\n"); 
    return 0; 
} 
+0

Vous êtes à la recherche d'un DFS. – SLaks

+0

'char * hash = argv [1]; char * salt = argv [1]; strncpy (sel, hash, 2); 'ressemble à des ennuis. – yano

+0

Oui c'est vrai, un sel de deux caractères de l'alphabet ./0-9A-Za-z, et le résultat de la crypte sera ces deux caractères suivis de 11 autres de la même alphabet, donc 13 au total. – Tarae

Répondre

0

C'est ce que j'ai fait à la fin pour les personnes intéressées. Je ne sais toujours pas comment j'aurais pu changer la longueur des cordes avec moins de répétitions.

#define _XOPEN_SOURCE // for crypt 
#include <cs50.h> // used for get_string 
#include <stdio.h> // used for printf 
#include <string.h> // used for strlen 
#include <ctype.h> // used for isalpha 
#include <stdlib.h> // for atoi 
#include <crypt.h> // for crypt 

int main(int argc, char* argv[]) 
{ 

// accept only two arguments from command line 
// hash is max 13 characters. First two elements are the salt, 
// the other 11 are the key 

if (argc != 2) 
{ 
    printf("Usage: ./crack hash \n"); 
    return 1; 
} 

// make second command line argument into string 
char* hash = argv[1]; 
char salt[3]; 
strncpy(salt, hash, 2); 
salt[2] = '\0'; // null terminates destination 

char* key = hash + 2; // just key without salt 

printf("key: %s\n", key); 
printf("salt: %s\n", salt); 

char* abAB = "abcdefghijklmnopqrstuvwxyzQWERTYUIOPASDFGHJKLZXCVBNM"; 

for (int i = 0, n = strlen(abAB); i < n; i++) 
{ 
    //check for every length 
    char tri1[2] = {abAB[i], 0}; 

    // produce my own encrpyted key with salt 
    char* cr1 = crypt(tri1, salt); 

    // if own key is equal to key provided, print out the string tried. 
    if (strcmp(cr1, hash) == 0) 
    { 
     printf("%s\n", tri1); 
     return 0; 
    } 

    for (int j = 0, m = strlen(abAB); j < m; j++) 
    { 
     char tri2[3] = {abAB[i], abAB[j], 0}; 
     char* cr2 = crypt(tri2, salt); 

     if (strcmp(cr2, hash) == 0) 
     { 
      printf("%s\n", tri2); 
      return 0; 
     } 

     // this didn't work - do you know why??? 
     // int comp2 = strcmp(cr2, hash); 
     // if (comp2 == 0) 
     // { 
     //  printf("%s", tri2); 
     //  printf("test"); 
     //  return 0; 
     // } 

     for (int k = 0, p = strlen(abAB); k < p; k++) 
     { 
      char tri3[4] = {abAB[i],abAB[j],abAB[k], 0}; 
      char* cr3 = crypt(tri3, salt); 

      if (strcmp(cr3, hash) == 0) 
      { 
       printf("%s\n", tri3); 
       return 0; 
      } 

      for (int l = 0, q = strlen(abAB); l < q; l++) 
      { 
       char tri4[5] = {abAB[i],abAB[j],abAB[k],abAB[l], 0}; 
       // produce my own encrpyted key with salt 
       char* cr4 = crypt(tri4, salt); 

       // if own key is equal to key provided, print out the string tried. 
       if (strcmp(cr4, hash) == 0) 
       { 
        printf("%s\n", tri4); 
        return 0; 
       } 
      } 
     } 
    } 
} 

printf("\n"); 
return 1; 
}