0

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):

Testing cracking single password works

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:

enter image description here

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:

enter image description here

Anyone have any ideas why I am getting this type of output?

1 Answer 1

1

The problem was hiding somewhere else:

// Create pword w/ max of 5 chars
char pword[6];
pword[5] = '\0';

That means that the first 4 chars are whatever random garbage is in them when the char array is created. So, what's in the first 4 chars as the code progresses? More importantly, where is the end of string marker when you're dealing with 1 and 2 char passwords? Note that only 0 and 1 are ever set, and elements 2,3,and 4 are never initialized or changed.

It also means that, when trying to process 2 char passwords, its actually passing 5 char passwords, so it fails every time.

Try initializing every element in the array to \0.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Wow, thank you for such a clear and concise answer. Super helpful. Did a little googling and seems like the appropriate way to set all values to be '\0' would be to do this: char pword[6] = { '\0' };, correct? Instead of doing a bunch of repetitive lines like: char pword[6]; pword[0] = '\0'; pword[1] = '\0'; pword[2] = '\0'.... Thank you again!
    – tec4
    Commented Mar 11, 2018 at 1:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .