I'm trying to incrementally solve the crack problem by doing the following:
- Crack one character password "a" with salt of "50" (hash: 50OqznXGVcOJU)
- Crack two character password "aa" with salt of "50" (hash: 50/A0MHdXAgbA)
- and so on for a proof of concept that I am on the right track (only using a, b and c for letters allowed in password until I figure out the issue I am having to keep things simpler)
The single character password works as I would expect (ie: the hash of "50OqznXGVcOJU" is found to be the hash for the password "a" with the salt of "50").
Example output (prints the password being checked on first line and summary of what was matched on second):
My code:
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#define _XOPEN_SOURCE
#include <unistd.h>
#include <crypt.h>
string alpha_chars = "abc";
void check(string hash, string pword)
{
string encoded = crypt(pword, "50");
if (strcmp(encoded, hash) == 0)
{
printf("Salt: %s, Encoded: %s, Password: %s\n", "50", encoded, pword);
exit(1);
}
}
// pword "a", salt "50", hash: 50OqznXGVcOJU
// pword "aa", salt "50", hash: 50/A0MHdXAgbA
int main(int argc, string argv[])
{
// Accept one argument - a hashed password
if (argc != 2)
{
printf("Usage: ./crack hash\n");
return 1;
}
string hash = argv[1];
int alpha_chars_len = strlen(alpha_chars);
// Create pword w/ max of 5 chars
char pword[6];
pword[5] = '\0';
// Crack single character passwords
for (int k = 0; k < alpha_chars_len; k++)
{
pword[0] = alpha_chars[k];
printf("%s\n", pword);
check(hash, pword);
}
// Crack two character passwords
for (int j = 0; j < alpha_chars_len; j++)
{
pword[0] = alpha_chars[j];
for (int k = 0; k < alpha_chars_len; k++)
{
pword[1] = alpha_chars[k];
printf("%s\n", pword);
check(hash, pword);
}
}
}
However, when I am trying crack the hash of "50/A0MHdXAgbA" (password "aa" with salt of "50") it does not match. When I look at the output I am getting, it seems very strange. With the posted code above, all the first letters are "B" for the two letter passwords being checked:
But, when I try to add a line to check what the actual character is that I am grabbing it is indeed "a", "b" and "c" BUT now I get a "B" at the end of the password being printed to the console (screenshot below).
Example of line I added to try and debug:
// Crack two character passwords
for (int j = 0; j < alpha_chars_len; j++)
{
pword[0] = alpha_chars[j];
printf("Letter: %c\n", alpha_chars[j]); // Added this line
for (int k = 0; k < alpha_chars_len; k++)
{
pword[1] = alpha_chars[k];
printf("%s\n", pword);
check(hash, pword);
}
}
Output I get when I added that one line:
Anyone have any ideas why I am getting this type of output?