user4073801
user4073801

Reputation:

I have no idea why I'm getting the errors I'm getting

Here is my code:

#define _XOPEN_SOURCE
#include <unistd.h>
#include <stdio.h>
#include <math.h> // this means I also need -lm at the execution of my code

int main(int argc, char *argv[])
{
    if(argc != 2)
        return 1;

    const char salt[] = {argv[1][13], argv[1][12]}; // the value of salt is the first two chars in string argv[1], and the length of the hash is always 13

   // This was const char key[9] and accounted for 8 of the errors
   char key[9]; // create array of size 9 since one of the values is the null char

    long long int i = 32; // initialize the incrementing variable at the lowest value printable ASCII character; long long because it's going to reach very high numbers
    int j = (((i - 32))/95); // digit for key 1
    int k = ((i - 32)/pow(95,2)); // digit for key 2
    int l = ((i - 32)/pow(95,3)); // digit for key 3
    int m = ((i - 32)/pow(95,4)); // digit for key 4
    int n = ((i - 32)/pow(95,5)); // digit for key 5
    int o = ((i - 32)/pow(95,6)); // digit for key 6
    int p = ((i - 32)/pow(95,7)); // digit for key 7

    while(i < pow(95,8) + pow(95,7) + pow(95,6) + pow(95,5) + pow(95,4) + pow(95,3) + pow(95,2) + 95) // this is inefficient but goes through every combination & string length
    {
        key[0] = ((i - 32) % 95) + 32;
        key[1] = (j % 95) + 32;
        key[3] = (k % 95) + 32;
        key[4] = (l % 95) + 32;        
        key[5] = (m % 95) + 32;
        key[6] = (n % 95) + 32;
        key[7] = (o % 95) + 32;
        key[8] = (p % 95) + 32;

        if(char *crypt_r(const char *key, const char *salt, struct crypt_data *data) == argv[1]) // compare the hash of the current key string to the inputted hash
        {
            printf("%s\n", key);
            return 0; //print password and exit
        }

        else // if the hashes don't match, then increment and try again
        {
            i++;
        }
    }
}

The point of the problem is to receive any hashed password that's been hashed using C's DES-based crypt function and use brute force to figure it out.

The problem is that when I try to compile, I get 9 errors. I'm using

 clang -o crack crack.c -lcrypt -lm

Eight of them are from key[] =something, and they say that the "read only variable is not assignable".

The last one has an issue with the "if" statement at the end with the char in it, and it puts an arrow below char and says "expected expression". I've spent hours on this code, and I'd really appreciate some help with it.

Note: I am a student, and so it's much more helpful for explanations instead of "here you go I fixed your code". It's also against the rules for this assignment, and I will be linking the post to my assignment since the course mandates I recognize any additional help I received outside of the course's teaching fellows and instructive materials.

EDIT: I changed the key[] and made it not constant, which fixed 8 of the errors. The last one with "expected expression" remains.

Upvotes: 0

Views: 183

Answers (1)

Brendan Long
Brendan Long

Reputation: 54232

Regarding this line:

if(char *crypt_r(const char *key, const char *salt, struct crypt_data *data) == argv[1]) // compare the hash of the current key string to the inputted hash

It looks like you copied the declaration for crypt_r instead of calling the function. The difference is that the declaration tells you what the function is, and when you call it, you need to fill everything in.

For example, if you have:

char* key;
char* salt;
struct crypt_data* data;
// initialize all of those

Then you would call it like:

char* result = crypt_r(key, salt, data);

In general, if you have a function in the form:

some_type function_name(another_type parameter_name);

Then the function returns a some_type and expects an another_type as the first parameter. You don't need to redeclare the whole thing, you just need to give it the two things it wants:

another_type x = whatever;
some_type result = function_name(x);

Upvotes: 1

Related Questions