I'm taking an edx course called CS50. Maybe some of you are familiar with it.
One of the problems set asks you to implement an algorithm to crack a password that was hashed using des-based encryption and is up to 4 characters.
So far, so good. I've done it.
But I decided to improve it a little bit and make it possible to crack passwords that are up to 8 characters, which is the maximum for des-based encryption.
And the problem is, when I add the possibility of the fifth character (or more), my code doesn't work anymore.
Here are my codes
This one is working:
#define _XOPEN_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <string.h>
/*
Use this to compile
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wshadow crack4.c -lcrypt -lm -o crack4
*/
int main(int argc, char *argv[])
{
if (argc != 2) //Checks if number of command-line arguments is valid
{
printf ("usage: ./crack + hash \n");
return 1; //Retuns 1 (error)
}
char *hash = argv[1]; //Gets hash passed as argument
char salt[3]; //Gets the salt
salt[0] = hash[0];
salt[1] = hash[1];
salt[2] = '\0';
//All possible characters used in a DES-based hashed password (taken from gnu library)
const char *const seedchars = " ./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
char text[9] = "\0"; //Text that is gonna be tried with crypt()
for (int d = 0; d <= 64 ; d++) //To check for passwords of up to 4 characters
{
printf("d %d \n", d);
if(d > 0)
{
text[4] = '\0'; //Defining null-terminator at index 4
text[3] = seedchars[d]; //Iterates through the seedchars list at index 3
}
for (int c = 0; c <= 64 ; c++) //To check for passwords of up to 3 characters
{
if(c > 0)
{
if (d == 0)
{
text[3] = '\0'; //Defining null-terminator at index 3
}
text[2] = seedchars[c]; //Iterates through the seedchars list at index 2
}
for (int b = 0; b <= 64 ; b++) //To check for passwords of up to 2 characters
{
if(b > 0)
{
if (c == 0 && d == 0)
{
text[2] = '\0'; //Defining null-terminator at index 2
}
text[1] = seedchars[b]; //Iterates through the seedchars list at index 1
}
for (int a = 0; a <= 64 ; a++) //To check for passwords of up to 1 character
{
if(b == 0 && c == 0 && d == 0)
{
text[1] = '\0'; //Defining null-terminator at index 1
}
text[0] = seedchars[a]; //Iterates through the seedchars list at index 0
char *password = crypt(text, salt); //Hash var text and save it to var password
if (strcmp(hash, password) == 0) //Compares the hash passed as argv with created above
{
printf("%s\n", text); //prints the text that led to said hash
return 0; //Returns 0 (okay)
}
}
}
}
}
return 1; //Retuns 1 (error)
}
This one isn't working:
#define _XOPEN_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <string.h>
/*
Use this to compile
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wshadow crack.c -lcrypt -lm -o crack
*/
int main(int argc, char *argv[])
{
if (argc != 2) //Checks if number of command-line arguments is valid
{
printf ("usage: ./crack + hash \n");
return 1; //Retuns 1 (error)
}
char *hash = argv[1]; //Gets hash passed as argument
char salt[3]; //Gets the salt
salt[0] = hash[0];
salt[1] = hash[1];
salt[2] = '\0';
//All possible characters used in a DES-based hashed password (taken from gnu library)
const char *const seedchars = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
char text[9] = "\0"; //Text that is gonna be tried with crypt()
for (int h = 0; h <= 64 ; h++) //To check for passwords of up to 8 characters
{
if(h > 0)
{
text[8] = '\0'; //Defining null-terminator at index 8
text[7] = seedchars[h]; //Iterates through the seedchars list at index 7
}
for (int g = 0; g <= 64 ; g++) //To check for passwords of up to 7 characters
{
if(g > 0)
{
if (h == 0)
{
text[7] = '\0'; //Defining null-terminator at index 7
}
text[6] = seedchars[g]; //Iterates through the seedchars list at index 6
}
for (int f = 0; f <= 64 ; f++) //To check for passwords of up to 6 characters
{
if(f > 0)
{
if (g == 0 && h == 0)
{
text[6] = '\0'; //Defining null-terminator at index 6
}
text[5] = seedchars[f]; //Iterates through the seedchars list at index 5
}
for (int e = 0; e <= 64 ; e++) //To check for passwords of up to 5 characters
{
if(e > 0)
{
if (f == 0 && g == 0 && h == 0)
{
text[5] = '\0'; //Defining null-terminator at index 5
}
text[4] = seedchars[e]; //Iterates through the seedchars list at index 4
for (int d = 0; d <= 64 ; d++) //To check for passwords of up to 4 characters
{
printf("d %d \n", d);
if(d > 0)
{
if (e == 0 && f == 0 && g == 0 && h == 0)
{
text[4] = '\0'; //Defining null-terminator at index 4
}
text[3] = seedchars[d]; //Iterates through the seedchars list at index 3
}
for (int c = 0; c <= 64 ; c++) //To check for passwords of up to 3 characters
{
if(c > 0)
{
if (d == 0 && e == 0 && f == 0 && g == 0 && h == 0)
{
text[3] = '\0'; //Defining null-terminator at index 3
}
text[2] = seedchars[c]; //Iterates through the seedchars list at index 2
}
for (int b = 0; b <= 64 ; b++) //To check for passwords of up to 2 characters
{
if(b > 0)
{
if (c == 0 && d == 0 && e == 0 && f == 0 && g == 0 && h == 0)
{
text[2] = '\0'; //Defining null-terminator at index 2
}
text[1] = seedchars[b]; //Iterates through the seedchars list at index 1
}
for (int a = 0; a <= 64 ; a++) //To check for passwords of up to 1 character
{
if(b == 0 && c == 0 && d == 0 && e == 0 && f == 0 && g == 0 && h == 0)
{
text[1] = '\0'; //Defining null-terminator at index 1
}
text[0] = seedchars[a]; //Iterates through the seedchars list at index 0
char *password = crypt(text, salt); //Hash var text and save it to var password
if (strcmp(hash, password) == 0) //Compares the hash passed as argv with created above
{
printf("%s\n", text); //prints the text that led to said hash
return 0; //Returns 0 (okay)
}
}
}
}
}
}
}
}
}
}
return 1; //Retuns 1 (error)
}
I'm using the hash below in both codes but it doesn't work on the second code.
hash - 50fkUxYHbnXGw
text - rofl
Can someone please help me understand why is it not working?
Thanks.
Edit:
The second code just finished running and it actually is working but it's taking way longer to crack the password. Here's a screenshot:
I'm not being able to post it as a image for some reason, so here's the link https://i.stack.imgur.com/lBarv.jpg
Edit 2: adding link to image and fixing title
Edit 3: re-fixing title