2017-06-12 104 views
1

为了神圣的代码的爱,我想比较哈希找到正确的密码。我给出一个哈希作为命令行参数,我再从“一”到“ZZZZ”凑字,直到哈希对比赛之一。C:比较哈希值似乎消失

void decipher(string hash) 
{ 
    //Set the password, and the salt. 
    char pass[4] = "a"; 
    char salt[] ="50"; 

    //Compare the crypted pass againts the hash until found. 
    while (strcmp(hash,crypt(pass, salt)) != 0) 
    { 
     //Use int i to hold position, and return next char 
      int i = 0; 
      pass[i] = get_next(pass[i]); 
      tick_over (pass, i); 

      //Hardcode in a fail safe max length: exit. 
      if (strlen(pass) > 4) 
      { 
       break; 
      } 
    } 
    printf("%s\n", pass); 
} 

的问题是,它不会“抓”了正确的密码/比较,当密码为4个字母。它适用于1,2和3个字母的单词。

//Tick over casino style 
string tick_over (string pass, int i) 
{ 
    //Once a char reaches 'Z', move the next char in line up one value. 
    char a[] = "a"; 
    if (pass[i] == 'Z') 
    { 
     if (strlen(pass) < i+2) 
     { 
     strncat (pass, &a[0], 1); 
     return pass; 
     } 
     pass[i+1] = get_next(pass[i+1]); 
     //Recursively run again, moving along the string as necessary 
     tick_over (pass, i+1); 
    } 
    return pass; 
} 

//Give the next character in the sequence of available characters 
char get_next (char y) 
{ 
    if (y == 'z') 
    { 
     return 'A'; 
    } 
    else if (y == 'Z') 
    { 
     return 'a'; 
    } 
    else 
    { 
     return y + 1; 
    } 
} 

它遍历正确的单词,正如我在调试中发现的那样。我试图将

strcmp(hash, crypt(pass, salt)) == 0 

变成一个嵌套的if语句等,但它似乎不是问题。 c以某种方式'忘记'命令行值?在调试散列值时似乎已经消失:/请帮忙!

回答

2

char pass[4] = "a";你定义一个字符数组,其最多可包含3个字符+空终止符。

,这不是连贯与“安全”的测试:if (strlen(pass) > 4)

strlen 4的阵列已经被覆盖在内存中,因为null终止字符的东西:未定义行为

的QuickFix:char pass[5] ...

+0

谢谢这个工作。但是,较早创建tick_over方法和“安全性”测试前,通过[4]做的工作 - 在密码哈希将匹配,并从回路断线。我很好奇 - 你会知道为什么会发生这种情况,是否导致覆盖的安全测试? – sena

+1

但你确实有不确定的行为,因为你写出界。这就像俄罗斯轮盘赌一样。在某个时候你输了。安全测试只读取这样的数字。这是任何超出界限的写入操作。 RAM很便宜。放大缓冲区,你很好走。 –

0

下面是函数strncat的说明:

从字符串 追加字符追加源到目的地,加上终止空字符的第一NUM个字符。

大小为4,您不考虑四个字符数组的终止空字符。