diff --git a/decrypt.c b/decrypt.c index f48850088307ced9f80905bc7bd5e6836bbc58dd..48dd94e26a7368e81b51a8bdf91c5621425d3fa2 100644 --- a/decrypt.c +++ b/decrypt.c @@ -39,14 +39,9 @@ int read_hash_file_four(char *file_name, int n_guesses){ while((c = getc(file)) != EOF){ n +=sprintf(hashes+n,"%02x", c); } - // printf("%s",hashes); char **words = store_password_hashes(hashes); HashTable *password_hashes = new_hash_table(strlen(hashes)/64); - for(int i=0; i < strlen(hashes)/64; i++){ - printf("%d: %s\n", i, words[i]); - hash_table_put(password_hashes, words[i], (i+1)); - }; Passwords *cracked_passwords = create_passwords(strlen(hashes)/64, n_guesses); // Tries cracking passwords based on common passwords @@ -56,9 +51,7 @@ int read_hash_file_four(char *file_name, int n_guesses){ // Checks all combinations of four passwords brute_force_four(password_hashes, cracked_passwords); n_guesses = get_remaining_guesses(cracked_passwords); - print_passwords(cracked_passwords); for(int i=0; i < strlen(hashes)/64; i++){ - printf("%d: %s\n", i, words[i]); free(words[i]); }; free(words); @@ -90,7 +83,6 @@ void read_hash_file_six(char *file_name, int n_guesses){ //creates hashtable of # of passwords HashTable *password_hashes = new_hash_table(strlen(hashes)/64); for(int i=0; i < strlen(hashes)/64; i++){ - printf("%d: %s\n", i, words[i]); hash_table_put(password_hashes, words[i], (i+1)); }; Passwords *cracked_passwords = create_passwords(strlen(hashes)/64, n_guesses); @@ -128,13 +120,10 @@ void check_hashed_passwords(char *password_list, int n_guesses,char *file_name){ //creates hashtable of # of passwords HashTable *password_hashes = new_hash_table(strlen(hashes)/64); - for(int i=0; i < strlen(hashes)/64; i++){ - printf("%d: %s\n", i, words[i]); - hash_table_put(password_hashes, words[i], (i+1)); - }; // Passwords to check against hashes FILE* pwrd_file = fopen(password_list, "r"); - char line[20]; + char line[100000]; + memset(line, 0, 100000); SHA256_CTX ctx; int hash; while (fgets(line, sizeof(line), pwrd_file) && remaining_hashes(password_hashes) > 0){ @@ -151,7 +140,6 @@ void check_hashed_passwords(char *password_list, int n_guesses,char *file_name){ } free(hex_guess); } - printf("finished file"); fclose(pwrd_file); free(words); free_hash_table(password_hashes); @@ -167,7 +155,6 @@ int main(int argc, char *argv[]){ } else if(argc == 2){ int n_guesses = atoi(argv[argc-1]); - printf("remaining passwords are : %d\n", n_guesses); // have n amount of guesses n_guesses = read_hash_file_four("pwd4sha256", n_guesses); if (n_guesses>0){ diff --git a/dh.c b/dh.c index fbfbf05ab8a4c00c55fd8b9cef1b79a3a0ff11c1..2f8e70126d788a8f5194c8396f092d22d9ae0421 100644 --- a/dh.c +++ b/dh.c @@ -40,10 +40,7 @@ int main(int argc, char *argv[]){ fprintf(stderr, "usage %s hostname port\n", argv[0]); exit(EXIT_FAILURE); } - char privKey[5]; - memcpy(privKey, argv[2], 2); - privKey[2] = '\0'; - int b = (int)strtol(privKey, NULL, 16); + int b = atoi(argv[2]); portno = 7800; /* Translate host name into peer's IP address ; diff --git a/four_password_strategy.c b/four_password_strategy.c index 1fb6f9d4f4ebef3f4c37566cf0209f9ec58e0dc3..bb0bad32551698c4e1a7d7c96d140a88ac470e74 100644 --- a/four_password_strategy.c +++ b/four_password_strategy.c @@ -9,13 +9,15 @@ #include "sha256-helper.h" #include "passwords.h" void brute_force_four(HashTable *ht, Passwords* solved){ - printf("brute force\n"); + // Checks every password combination and guesses char brute_guess[4]; SHA256_CTX ctx; int hash; + // Ensures all passwords not already guessed or haven't guessed all guesses if (remaining_hashes(ht) == 0 || get_remaining_guesses(solved) == 0){ return; } + // Tries every combination of characters, starting from A for(int i=65;i < 127; i++){ brute_guess[0] = (char)i; for(int j=33;j < 127; j++){ @@ -30,11 +32,10 @@ void brute_force_four(HashTable *ht, Passwords* solved){ BYTE guess[32]; sha256_final(&ctx, guess); char* hex_guess = sha256_byteToHexString(guess); - // printf("%s\n", brute_guess); if ((hash = hash_table_get(ht, hex_guess))>0){ printf("%s %d\n", brute_guess, hash); add_new_cracked(solved, brute_guess); - printf("remaining passwords are : %d\n", remaining_hashes(ht)); + // Tries generating guesses with common substituions for guessed password generate_common_subs_four(solved,ht); if (get_remaining_guesses(solved) == 0 || remaining_hashes(ht) == 0){ return; @@ -58,7 +59,7 @@ void brute_force_four(HashTable *ht, Passwords* solved){ } int popular_character_guess_four(HashTable *ht, Passwords* solved){ - printf("popular character guess strategy\n"); + // Gusses passwords using common password file frequency char brute_guess[4]; SHA256_CTX ctx; int hash; @@ -66,6 +67,7 @@ int popular_character_guess_four(HashTable *ht, Passwords* solved){ FILE *file = fopen("common_password_frequency.txt", "r"); char frequent_characters[60]; int index = 0; + // Ensures all passwords havent been guessed if (remaining_hashes(ht) == 0 || get_remaining_guesses(solved) == 0){ fclose(file); return 0; @@ -89,11 +91,11 @@ int popular_character_guess_four(HashTable *ht, Passwords* solved){ BYTE guess[32]; sha256_final(&ctx, guess); char* hex_guess = sha256_byteToHexString(guess); - // printf("%s\n", brute_guess); + // Checks if guess matched password hash if ((hash = hash_table_get(ht, hex_guess))>0){ printf("%s %d\n", brute_guess, hash); add_new_cracked(solved, brute_guess); - printf("remaining passwords are : %d\n", remaining_hashes(ht)); + //Finds common substituion guesses generate_common_subs_four(solved,ht); if (get_remaining_guesses(solved) == 0 || remaining_hashes(ht) == 0){ return 0; @@ -112,22 +114,23 @@ int popular_character_guess_four(HashTable *ht, Passwords* solved){ } int generate_guesses_four(HashTable *ht, Passwords* solved){ + // Guesses common passwords from common password file FILE* file = fopen("proj-2_common_passwords.txt", "r"); char line[20]; SHA256_CTX ctx; int hash; while (fgets(line, sizeof(line), file) && remaining_hashes(ht) > 0 && get_remaining_guesses(solved) != 0){ line[4] = '\0'; - // printf("%s\n", line); sha256_init(&ctx); sha256_update(&ctx, (BYTE*)line, strlen(line)); BYTE guess[32]; sha256_final(&ctx, guess); char* hex_guess = sha256_byteToHexString(guess); + // Checks for matching password if ((hash = hash_table_get(ht, hex_guess))>0){ printf("%s %d\n", line, hash); add_new_cracked(solved, line); - printf("remaining passwords are : %d\n", remaining_hashes(ht)); + // Checks for common substituion with guessed password generate_common_subs_four(solved,ht); if (get_remaining_guesses(solved) == 0 || remaining_hashes(ht) == 0){ return 0; @@ -136,7 +139,6 @@ int generate_guesses_four(HashTable *ht, Passwords* solved){ made_guess(solved); free(hex_guess); } - printf("finished file\n"); fclose(file); return get_remaining_guesses(solved); } \ No newline at end of file diff --git a/four_password_strategy.h b/four_password_strategy.h index 9f21d820b2e06db5ea3b14101f1050da235d2916..f672a4bb98abcec7c03d1e3a8a4c8dd73f4670bc 100644 --- a/four_password_strategy.h +++ b/four_password_strategy.h @@ -3,8 +3,11 @@ #include "hashtable.h" #include "proj-2_sha256.h" #include "passwords.h" +// Tries all character combinations and tries to guess void brute_force_four(HashTable *ht, Passwords* solved); +// Checks a file in order of popular characters from the distribution of common passwords int popular_character_guess_four(HashTable *ht, Passwords* solved); +// Genereates guesses using common passwords file int generate_guesses_four(HashTable *ht, Passwords* solved); diff --git a/hashtable.c b/hashtable.c index 47200e40cca0721d5de06676a89d77bb48ba11cc..b583dc92920da29b69b9cf7c5b823cbb222e7ab2 100644 --- a/hashtable.c +++ b/hashtable.c @@ -22,7 +22,7 @@ struct bucket { struct table { int size; //number of buckets Bucket *buckets; //list of buckets - int remaining; + int remaining; // checks number of remaining elements }; /************************************************************************/ @@ -183,5 +183,6 @@ bool hash_table_has(HashTable *table, char *key) { } /************************************************************************/ int remaining_hashes(HashTable *table){ + /* Returns remaining elements in hash table*/ return table->remaining; } \ No newline at end of file diff --git a/passwords.c b/passwords.c index 95b729e4db3f7afc0acffa50bec589ad3fe691bb..144b3e6c89c21e7a7ea6d2eb50b1e8d766204beb 100644 --- a/passwords.c +++ b/passwords.c @@ -12,12 +12,15 @@ struct passwords { char **cracked; //array of words - int passwords_left; - int current_size; - int n_guesses; + int passwords_left; // passwords left to crack + int current_size; //current number of passwords solved + int n_guesses; // number of permitted guesses }; Passwords* create_passwords(int passwords_to_guess, int n_guesses){ + /*** + * Creates a password structure to hold solved passwords + * */ Passwords* passwords = malloc(sizeof *passwords); assert(passwords); passwords->cracked = malloc(sizeof(char*)*passwords_to_guess); @@ -29,6 +32,9 @@ Passwords* create_passwords(int passwords_to_guess, int n_guesses){ } void add_new_cracked(Passwords * pwrds, char* pword){ + /*** + * Adds a cracked password + * */ pwrds->cracked[pwrds->current_size] = calloc(6, sizeof(char*)); assert(pwrds->cracked[pwrds->current_size]); memcpy(pwrds->cracked[pwrds->current_size], pword, strlen(pword)); @@ -37,10 +43,12 @@ void add_new_cracked(Passwords * pwrds, char* pword){ } int remaining_passwords(Passwords *pwrds){ + /* Returns the number of passwords left to crack*/ return pwrds->passwords_left; } void free_passwords(Passwords* pwrds){ + // Deallocates memory for(int i=0; i < pwrds->current_size; i++){ free(pwrds->cracked[i]); } @@ -49,20 +57,24 @@ void free_passwords(Passwords* pwrds){ } void print_passwords(Passwords* pwrds){ + // Prints passwords for(int i=0; i< pwrds->current_size; i++){ printf("%s\n", pwrds->cracked[i]); } } void made_guess(Passwords* pwrds){ + // Decreases the number of guesses pwrds->n_guesses--; } int get_remaining_guesses(Passwords* pwrds){ + // Returns the number of guesses that can be made return pwrds->n_guesses; } int generate_common_subs_four(Passwords* solved, HashTable *ht){ + // Generates passwords using common substituions if (remaining_hashes(ht) == 0 || get_remaining_guesses(solved) == 0){ return 0; } @@ -82,12 +94,14 @@ int generate_common_subs_four(Passwords* solved, HashTable *ht){ assert(subs[i]); subs_size[i] = 0; FILE *file = fopen("common_subs.txt", "r"); + // Reads file of common subtitutions while (fgets(line, sizeof(line), file)){ if(line[0] == word[i]){ characters[i] = line[0]; subs[i][subs_size[i]] = line[0]; subs_size[i]++; token = strtok(strstr(line, s), s); + // Builds an array for common letter substitutions for each passwor character while(token != NULL && strlen(token) == 1){ subs[i][subs_size[i]] = (char)*token; subs_size[i]++; @@ -99,6 +113,7 @@ int generate_common_subs_four(Passwords* solved, HashTable *ht){ char sub_guess[4]; int hash; SHA256_CTX ctx; + // Iterates and creates combinations of common substituions for(int i=0; i<subs_size[0]; i++){ sub_guess[0] = subs[0][i]; for(int j=0; j<subs_size[1]; j++){ @@ -113,11 +128,10 @@ int generate_common_subs_four(Passwords* solved, HashTable *ht){ BYTE guess[32]; sha256_final(&ctx, guess); char* hex_guess = sha256_byteToHexString(guess); - // printf("%s\n", sub_guess); + // Checks if password cracked if ((hash = hash_table_get(ht, hex_guess))>0){ printf("%s %d\n", sub_guess, hash); add_new_cracked(solved, sub_guess); - printf("remaining passwords are : %d\n", remaining_hashes(ht)); } free(hex_guess); made_guess(solved); @@ -130,7 +144,7 @@ int generate_common_subs_four(Passwords* solved, HashTable *ht){ } - + // Deallocate memory for(int i=0; i < 4; i++){ free(subs[i]); } @@ -142,6 +156,7 @@ int generate_common_subs_four(Passwords* solved, HashTable *ht){ int generate_common_subs_six(Passwords* solved, HashTable *ht){ + // Generates passwords using common substituions if (remaining_hashes(ht) == 0 || get_remaining_guesses(solved) == 0){ return 0; } @@ -161,12 +176,14 @@ int generate_common_subs_six(Passwords* solved, HashTable *ht){ assert(subs[i]); subs_size[i] = 0; FILE *file = fopen("common_subs.txt", "r"); + // Reads file of common subtitutions while (fgets(line, sizeof(line), file)){ if(line[0] == word[i]){ characters[i] = line[0]; subs[i][subs_size[i]] = line[0]; subs_size[i]++; token = strtok(strstr(line, s), s); + // Stores letter substituions in an array while(token != NULL && strlen(token) == 1){ subs[i][subs_size[i]] = (char)*token; subs_size[i]++; @@ -178,6 +195,7 @@ int generate_common_subs_six(Passwords* solved, HashTable *ht){ char sub_guess[6]; int hash; SHA256_CTX ctx; + // Iterates and creates combinations of common substituions for(int i=0; i<subs_size[0]; i++){ sub_guess[0] = subs[0][i]; for(int j=0; j<subs_size[1]; j++){ @@ -196,11 +214,10 @@ int generate_common_subs_six(Passwords* solved, HashTable *ht){ BYTE guess[32]; sha256_final(&ctx, guess); char* hex_guess = sha256_byteToHexString(guess); - // printf("%s\n", sub_guess); + // Checks if found a password if ((hash = hash_table_get(ht, hex_guess))>0){ printf("%s %d\n", sub_guess, hash); add_new_cracked(solved, sub_guess); - printf("remaining passwords are : %d\n", remaining_hashes(ht)); } free(hex_guess); made_guess(solved); @@ -213,6 +230,7 @@ int generate_common_subs_six(Passwords* solved, HashTable *ht){ } } } + //Deallocate memory for(int i=0; i < 4; i++){ free(subs[i]); } diff --git a/passwords.h b/passwords.h index e143441b0a18b73c701da8d0942d6832166f0958..c97825671319bc3463033cb96dbe629cdae92fb2 100644 --- a/passwords.h +++ b/passwords.h @@ -3,21 +3,30 @@ typedef struct passwords Passwords; - +//Creates a password structure to hold solved passwords Passwords* create_passwords(int passwords_to_guess, int n_guesses); +// Adds a cracked password void add_new_cracked(Passwords * pwrds, char* pword); +// Returns the number of passwords left to crack int remaining_passwords(Passwords *pwrds); +// Deallocates memory void free_passwords(Passwords* pwrds); +// Prints passwords void print_passwords(Passwords* pwrds); + +// Generates passwords using common substituions for 4 letter password int generate_common_subs_four(Passwords* solved, HashTable *ht); +// Generates passwords using common substituions for 6 letter password int generate_common_subs_six(Passwords* solved, HashTable *ht); +// Returns the number of guesses that can be made int get_remaining_guesses(Passwords *pwrds); +// Decreases the number of guesses void made_guess(Passwords *pwrds); \ No newline at end of file diff --git a/sha256-helper.c b/sha256-helper.c index 1e56c168d2bc758817592a7d102d00141859026c..cd70177e217db181ee5769f18d7e8fe7b5e7dd84 100644 --- a/sha256-helper.c +++ b/sha256-helper.c @@ -5,6 +5,10 @@ // adapted from https://github.com/RemyNoulin/sha256 char *sha256_byteToHexString(BYTE data[]) { + /** + * Converts the byte to a hexadecimal string + */ + char *hexC = "0123456789abcdef"; char *hexS = malloc(65); assert(hexS); diff --git a/sha256-helper.h b/sha256-helper.h index 1177d563b8c845f41b6f3fae6c4592ccadb7abae..575f7b3ff9078ce194f54bc4ea22920b67f0a158 100644 --- a/sha256-helper.h +++ b/sha256-helper.h @@ -1,4 +1,6 @@ #include "proj-2_sha256.h" + +// Converts the byte to a hexadecimal string char *sha256_byteToHexString(BYTE data[]); \ No newline at end of file diff --git a/six_password_strategy.c b/six_password_strategy.c index 09e4fa4b4b4bf56e0977ac8a8ca70f3ce81fc1c0..2d593e5921175fbbae8abce44cf610ef93b2390e 100644 --- a/six_password_strategy.c +++ b/six_password_strategy.c @@ -10,12 +10,15 @@ #include "passwords.h" void brute_force_six(HashTable *ht, Passwords *solved){ + // Checks every password combination and guesses char brute_guess[6]; SHA256_CTX ctx; int hash; + // Ensures all passwords not already guessed or haven't guessed all guesses if (remaining_hashes(ht) == 0 || get_remaining_guesses(solved) == 0){ return; } + // Tries every combination of characters, starting from A for(int i=65;i < 127; i++){ brute_guess[0] = (char)i; for(int j=65;j < 127; j++){ @@ -37,7 +40,7 @@ void brute_force_six(HashTable *ht, Passwords *solved){ if ((hash = hash_table_get(ht, hex_guess))>0){ printf("%s %d\n", brute_guess, hash); add_new_cracked(solved, brute_guess); - printf("remaining passwords are : %d\n", remaining_hashes(ht)); + // Tries generating guesses with common substituions for guessed password generate_common_subs_six(solved, ht); if (get_remaining_guesses(solved) == 0 || remaining_hashes(ht) == 0){ return; @@ -62,7 +65,7 @@ void brute_force_six(HashTable *ht, Passwords *solved){ } } int popular_character_guess_six(HashTable *ht, Passwords *solved){ - printf("popular character guess strategy\n"); + // Gusses passwords using common password file frequency char brute_guess[4]; SHA256_CTX ctx; int hash; @@ -70,6 +73,7 @@ int popular_character_guess_six(HashTable *ht, Passwords *solved){ FILE *file = fopen("common_password_frequency.txt", "r"); char frequent_characters[60]; int index = 0; + // Ensures all passwords havent been guessed if (remaining_hashes(ht) == 0 || get_remaining_guesses(solved) == 0){ fclose(file); return 0; @@ -97,11 +101,11 @@ int popular_character_guess_six(HashTable *ht, Passwords *solved){ BYTE guess[32]; sha256_final(&ctx, guess); char* hex_guess = sha256_byteToHexString(guess); - // printf("%s\n", brute_guess); + // Checks if guess matched password hash if ((hash = hash_table_get(ht, hex_guess))>0){ printf("%s %d\n", brute_guess, hash); add_new_cracked(solved, brute_guess); - printf("remaining passwords are : %d\n", remaining_hashes(ht)); + //Finds common substituion guesses generate_common_subs_six(solved,ht); if (get_remaining_guesses(solved) == 0 || remaining_hashes(ht) == 0){ return 0; @@ -125,24 +129,24 @@ int popular_character_guess_six(HashTable *ht, Passwords *solved){ int generate_guesses_six(char* file_name, HashTable *ht, Passwords *solved){ + // Guesses common passwords from common password file FILE* file = fopen(file_name, "r"); char line[20]; SHA256_CTX ctx; int hash; - printf("start generate"); while (fgets(line, sizeof(line), file) && remaining_hashes(ht) > 0 && get_remaining_guesses(solved) != 0){ line[6] = '\0'; - // printf("%s\n", line); sha256_init(&ctx); sha256_update(&ctx, (BYTE*)line, strlen(line)); BYTE guess[32]; sha256_final(&ctx, guess); char* hex_guess = sha256_byteToHexString(guess); + // Checks for matching password if ((hash = hash_table_get(ht, hex_guess))>0){ printf("%s %d\n", line, hash); add_new_cracked(solved, line); - printf("remaining passwords are : %d\n", remaining_hashes(ht)); + // Checks for common substituion with guessed password generate_common_subs_six(solved,ht); if (get_remaining_guesses(solved) == 0 || remaining_hashes(ht) == 0){ return 0; @@ -151,7 +155,6 @@ int generate_guesses_six(char* file_name, HashTable *ht, Passwords *solved){ made_guess(solved); free(hex_guess); } - printf("finished file"); fclose(file); return get_remaining_guesses(solved); } \ No newline at end of file diff --git a/six_password_strategy.h b/six_password_strategy.h index 324b639fa93eb83214ea4254b841aaeb57a5942e..0872b1e3d13a91b46a0a805cd03fcfbf1cea1bb6 100644 --- a/six_password_strategy.h +++ b/six_password_strategy.h @@ -2,6 +2,12 @@ #include "hashtable.h" #include "passwords.h" + +// Tries all character combinations and tries to guess void brute_force_six(HashTable *ht, Passwords *solved); + +// Checks a file in order of popular characters from the distribution of common passwords int popular_character_guess_six(HashTable *ht, Passwords *solved); + +// Genereates guesses using common passwords file int generate_guesses_six(char* file_name, HashTable *ht, Passwords *solved); \ No newline at end of file