1

I wrote a code for the crack problem. To test the code I set hpass (stands for hashed password which is the input the user is going to enter) to crypt("AB","50")(see line 15). The expected output of my code should be "AB", but my code outputs "A".

#define _XOPEN_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <cs50.h>
#include <string.h>
int main(int argc, string argv[])
{
    // To exit if less or more than 2 arguements
    if (!(argc == 2))
    {
         return 1;
    }
    char salt[2];
    string hpass = argv[1];
    hpass = crypt("AB","50");
    // To get salt
    for (int i = 0; i < 2; i++)
    {
        salt[i] = hpass[i];
    }
    // To iterate over all possible passwords
    for (int i = 0; i < 52; i++)
    {
        char gpass[6];
        printf("i: %i\n", i);
        // To iterate over lowercase letters
        if (i > 25)
        {
            gpass[0] = 71 + i;
        }
        else
        {
            gpass[0] = 65 + i;
        }
        eprintf("%s\n", gpass);
        string temp = crypt(gpass,salt);
        if (strcmp(crypt(gpass,salt),hpass) == 0)
        {
            eprintf("hpass: %s\n",hpass);
            eprintf("crypt: %s\n",crypt(gpass,salt));
            printf("%s\n", gpass);
            return 0;
        }
        for (int j = 0; j < 52; j++)
        {
            if (j > 25)
            {
                gpass[1] = 71 + j;
            }
            else
            {
                gpass[1] = 65 + j;
            }
            printf("%s\n", gpass);
            temp = crypt(gpass,salt);
            if (strcmp(temp,hpass) == 0)
            {
                printf("%s\n", gpass);
                return 0;
            }
            for (int k = 0; k < 52; k++)
            {
                if (k > 25)
                {
                    gpass[2] = 71 + k;
                }
                else
                {
                    gpass[2] = 65 + k;
                }
                eprintf("%s\n", gpass);
                if (strcmp(crypt(gpass,salt),hpass) == 0)
                {
                    printf("%s\n", gpass);
                    return 0;
                }
                for (int l = 0; l < 52; l++)
                {
                    if (l > 25)
                    {
                        gpass[3] = 71 + l;
                    }
                    else
                    {
                        gpass[3] = 26 + l;
                    }
                    eprintf("%s\n", gpass);
                    if (strcmp(crypt(gpass,salt),hpass))
                    {
                        printf("%s\n", gpass);
                        return 0;
                    }
                    for (int m = 0; m < 52; m++)
                    {
                        if (m > 25)
                        {
                            gpass[4] = 71 + m;
                        }
                        else
                        {
                            gpass[4] = 65 + m;
                        }
                        eprintf("%s\n", gpass);
                        if (strcmp(crypt(gpass,salt),hpass) == 0)
                        {
                            printf("%s\n", gpass);
                            return 0;
                        }
                    }
                }
            }
        }
    }
} 
1

crypt stores the string in its own memory, and re-uses this. So each time you call crypt, it returns the same pointer, but changes the characters stored there.

You would have to make a copy, for example using strcpy or an own loop, into some memory allocated on stack or heap.

Also, missing some == 0. And I think you forgot some null terminators '\0'.

2
  • Thank you for the answer, but I still didn't understand the problem with my code could you elaborate. May 8 '18 at 14:04
  • Every time you call crypt, it overwrites its previous output. As your code only stores a copy of the pointer (string is an alias for char*), you strcmp the same memory location, which obviously stores the same string, regardless of the hash you originally put in. You could for example char hpass[14] = {0}; strncpy(hpass, crypt("AB", "50"), 13);, or store on heap like string hpass = calloc(14, 1); strncpy(hpass, crypt("AB", "50"), 13); and later free(hpass);. Or hard-code the hash as a string constant.
    – Blauelf
    May 8 '18 at 14:46

You must log in to answer this question.

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