diff --git a/decrypt.c b/decrypt.c index 35a51cbd0983752b4320f8eb3335f692e3b9de57..f48850088307ced9f80905bc7bd5e6836bbc58dd 100644 --- a/decrypt.c +++ b/decrypt.c @@ -8,8 +8,11 @@ #include "four_password_strategy.h" #include "six_password_strategy.h" #include "passwords.h" - +#include "sha256-helper.h" char** store_password_hashes(char* hashes){ + /** + * Stores the string of password hashes into an array + * */ char **password_hashes = (char**)malloc((strlen(hashes)/64)*sizeof(char*)); assert(password_hashes); memset(password_hashes,0,(strlen(hashes)/64)); @@ -21,11 +24,16 @@ char** store_password_hashes(char* hashes){ return password_hashes; } -void read_hash_file_four(char *file_name, int n_guesses){ +int read_hash_file_four(char *file_name, int n_guesses){ + /** + * Reads a file_name containing passwords of length 4 and makes n_guesses + * Run with -1 when there is no specified number of guesses + * */ FILE *file; file = fopen(file_name, "r"); if (file){ - char hashes[650]; + char hashes[4000]; + memset(hashes, 0, 4000); int n=0; int c; while((c = getc(file)) != EOF){ @@ -34,15 +42,20 @@ void read_hash_file_four(char *file_name, int n_guesses){ // printf("%s",hashes); char **words = store_password_hashes(hashes); - HashTable *password_hashes = new_hash_table(10); + 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(10); - n_guesses = generate_guesses_four(n_guesses, password_hashes, cracked_passwords); - n_guesses = popular_character_guess_four(password_hashes, cracked_passwords, n_guesses); - brute_force_four(password_hashes, cracked_passwords, n_guesses); + + Passwords *cracked_passwords = create_passwords(strlen(hashes)/64, n_guesses); + // Tries cracking passwords based on common passwords + generate_guesses_four(password_hashes, cracked_passwords); + // Tries cracking passwords based on highly frequent character passwords + popular_character_guess_four(password_hashes, cracked_passwords); + // 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]); @@ -53,33 +66,41 @@ void read_hash_file_four(char *file_name, int n_guesses){ free_passwords(cracked_passwords); fclose(file); } + return n_guesses; } void read_hash_file_six(char *file_name, int n_guesses){ + /** + * Reads a file_name containing passwords of length 6 and makes n_guesses + * Run with -1 when there is no specified number of guesses + * */ FILE *file; file = fopen(file_name, "r"); if (file){ - char hashes[650]; + char hashes[4000]; + memset(hashes, 0, 4000); int n=0; int c; 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(20); + //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(20); - n_guesses = generate_guesses_six(n_guesses, password_hashes, cracked_passwords); - n_guesses = popular_character_guess_six(password_hashes, cracked_passwords, n_guesses); - brute_force_six(password_hashes, cracked_passwords,n_guesses); + Passwords *cracked_passwords = create_passwords(strlen(hashes)/64, n_guesses); + // Tries cracking passwords based on common passwords + generate_guesses_six("proj-2_common_passwords.txt", password_hashes, cracked_passwords); + // Tries cracking passwords based on highly frequent character passwords + popular_character_guess_six(password_hashes, cracked_passwords); + // Checks all combinations of four passwords + brute_force_six(password_hashes, cracked_passwords); for(int i=0; i < strlen(hashes)/64; i++){ - printf("%d: %s\n", i, words[i]); free(words[i]); }; free(words); @@ -88,6 +109,56 @@ void read_hash_file_six(char *file_name, int n_guesses){ } } +void check_hashed_passwords(char *password_list, int n_guesses,char *file_name){ + /** + * Reads a file_name containing hashed passwords of assumed length 6 and a list + * of plaint text passwords which it is checked against + * */ + FILE *file; + file = fopen(file_name, "r"); + if (file){ + char hashes[4000]; + memset(hashes, 0, 4000); + int n=0; + int c; + while((c = getc(file)) != EOF){ + n +=sprintf(hashes+n,"%02x", c); + } + char **words = store_password_hashes(hashes); + + //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]; + SHA256_CTX ctx; + int hash; + while (fgets(line, sizeof(line), pwrd_file) && remaining_hashes(password_hashes) > 0){ + line[6] = '\0'; + + sha256_init(&ctx); + sha256_update(&ctx, (BYTE*)line, strlen(line)); + BYTE guess[32]; + sha256_final(&ctx, guess); + char* hex_guess = sha256_byteToHexString(guess); + // Print matching passwords + if ((hash = hash_table_get(password_hashes, hex_guess))>0){ + printf("%s %d\n", line, hash); + } + free(hex_guess); + } + printf("finished file"); + fclose(pwrd_file); + free(words); + free_hash_table(password_hashes); + } + fclose(file); +} + int main(int argc, char *argv[]){ if (argc == 1){ //generate guesses and test against SHA256 hashes @@ -98,10 +169,13 @@ int main(int argc, char *argv[]){ int n_guesses = atoi(argv[argc-1]); printf("remaining passwords are : %d\n", n_guesses); // have n amount of guesses - read_hash_file_four("pwd4sha256", n_guesses); - read_hash_file_six("pwd6sha256", n_guesses); + n_guesses = read_hash_file_four("pwd4sha256", n_guesses); + if (n_guesses>0){ + read_hash_file_six("pwd6sha256", n_guesses); + } } else if(argc == 3){ - // read file and do stuff + // Checks the hash of passwords with provided password file + check_hashed_passwords(argv[1], -1,argv[2]); } } \ No newline at end of file diff --git a/DiffieHellman.c b/dh.c similarity index 95% rename from DiffieHellman.c rename to dh.c index bf63fd9aa8c30a7fc28fa2eace5246b7cce186b7..fbfbf05ab8a4c00c55fd8b9cef1b79a3a0ff11c1 100644 --- a/DiffieHellman.c +++ b/dh.c @@ -10,6 +10,7 @@ #include <math.h> #include <unistd.h> int modulo(int a, int b, int n){ + // adapted from Wikipedia pow explanation long long x=1, y=a; while (b > 0) { if (b%2 == 1) { @@ -81,8 +82,7 @@ int main(int argc, char *argv[]){ exit(EXIT_FAILURE); } - /* Do processing */ - printf("send username\n"); + /* Sends username*/ char *username = "nirmalathasa"; n = write(sockfd, username, strlen(username)); n = write(sockfd, "\n", 1); @@ -91,8 +91,8 @@ int main(int argc, char *argv[]){ perror("ERROR writing to socket"); exit(EXIT_FAILURE); } + /* Sends secret*/ int client_secret = modulo(g,b,p); - printf("my key is is %d\n", client_secret); char secret[100]; sprintf(secret,"%d", client_secret); for(int i=0; i < strlen(secret); i++){ @@ -104,11 +104,13 @@ int main(int argc, char *argv[]){ } } n = write(sockfd, "\n", 1); + /* Recieves secret*/ n = read(sockfd, buffer, 255); if(!strncmp(buffer,"\n",1)){ buffer[n] = 0; } buffer[n] = 0; + /* Applies b to sent message*/ int responseDiffie = atoi(buffer); char shared_value[100]; sprintf(shared_value,"%d",modulo(responseDiffie, b, p)); diff --git a/four_password_strategy.c b/four_password_strategy.c index 1a95405a61e20d3210b61e61782c9f4feb467416..1fb6f9d4f4ebef3f4c37566cf0209f9ec58e0dc3 100644 --- a/four_password_strategy.c +++ b/four_password_strategy.c @@ -8,12 +8,12 @@ #include "four_password_strategy.h" #include "sha256-helper.h" #include "passwords.h" -void brute_force_four(HashTable *ht, Passwords* solved, int n_guesses){ +void brute_force_four(HashTable *ht, Passwords* solved){ printf("brute force\n"); char brute_guess[4]; SHA256_CTX ctx; int hash; - if (remaining_hashes(ht) == 0 || n_guesses == 0){ + if (remaining_hashes(ht) == 0 || get_remaining_guesses(solved) == 0){ return; } for(int i=65;i < 127; i++){ @@ -35,11 +35,14 @@ void brute_force_four(HashTable *ht, Passwords* solved, int n_guesses){ printf("%s %d\n", brute_guess, hash); add_new_cracked(solved, brute_guess); printf("remaining passwords are : %d\n", remaining_hashes(ht)); - n_guesses = generate_common_subs_four(solved,n_guesses,ht); + generate_common_subs_four(solved,ht); + if (get_remaining_guesses(solved) == 0 || remaining_hashes(ht) == 0){ + return; + } } free(hex_guess); - n_guesses--; - if (remaining_hashes(ht) == 0 || n_guesses == 0){ + made_guess(solved); + if (remaining_hashes(ht) == 0 || get_remaining_guesses(solved) == 0){ return; } } @@ -54,7 +57,7 @@ void brute_force_four(HashTable *ht, Passwords* solved, int n_guesses){ } } -int popular_character_guess_four(HashTable *ht, Passwords* solved, int n_guesses){ +int popular_character_guess_four(HashTable *ht, Passwords* solved){ printf("popular character guess strategy\n"); char brute_guess[4]; SHA256_CTX ctx; @@ -63,7 +66,7 @@ int popular_character_guess_four(HashTable *ht, Passwords* solved, int n_guesses FILE *file = fopen("common_password_frequency.txt", "r"); char frequent_characters[60]; int index = 0; - if (remaining_hashes(ht) == 0 || n_guesses == 0){ + if (remaining_hashes(ht) == 0 || get_remaining_guesses(solved) == 0){ fclose(file); return 0; } @@ -91,26 +94,29 @@ int popular_character_guess_four(HashTable *ht, Passwords* solved, int n_guesses printf("%s %d\n", brute_guess, hash); add_new_cracked(solved, brute_guess); printf("remaining passwords are : %d\n", remaining_hashes(ht)); - n_guesses = generate_common_subs_four(solved,n_guesses,ht); + generate_common_subs_four(solved,ht); + if (get_remaining_guesses(solved) == 0 || remaining_hashes(ht) == 0){ + return 0; + } } free(hex_guess); - n_guesses--; - if (remaining_hashes(ht) == 0 || n_guesses == 0){ + made_guess(solved); + if (remaining_hashes(ht) == 0 || get_remaining_guesses(solved) == 0){ return 0; } } } } } - return n_guesses; + return get_remaining_guesses(solved); } -int generate_guesses_four(int n_guesses, HashTable *ht, Passwords* solved){ +int generate_guesses_four(HashTable *ht, Passwords* solved){ 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 && n_guesses != 0){ + 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); @@ -122,12 +128,15 @@ int generate_guesses_four(int n_guesses, HashTable *ht, Passwords* solved){ printf("%s %d\n", line, hash); add_new_cracked(solved, line); printf("remaining passwords are : %d\n", remaining_hashes(ht)); - n_guesses = generate_common_subs_four(solved,n_guesses,ht); + generate_common_subs_four(solved,ht); + if (get_remaining_guesses(solved) == 0 || remaining_hashes(ht) == 0){ + return 0; + } } - n_guesses--; + made_guess(solved); free(hex_guess); } printf("finished file\n"); fclose(file); - return n_guesses; + return get_remaining_guesses(solved); } \ No newline at end of file diff --git a/four_password_strategy.h b/four_password_strategy.h index b9492cc288ac5e2c6e62d732bcb8fad3f517085e..9f21d820b2e06db5ea3b14101f1050da235d2916 100644 --- a/four_password_strategy.h +++ b/four_password_strategy.h @@ -3,8 +3,8 @@ #include "hashtable.h" #include "proj-2_sha256.h" #include "passwords.h" -void brute_force_four(HashTable *ht, Passwords* solved, int n_guesses); +void brute_force_four(HashTable *ht, Passwords* solved); -int popular_character_guess_four(HashTable *ht, Passwords* solved, int n_guesses); +int popular_character_guess_four(HashTable *ht, Passwords* solved); -int generate_guesses_four(int n_guesses, HashTable *ht, Passwords* solved); +int generate_guesses_four(HashTable *ht, Passwords* solved); diff --git a/passwords.c b/passwords.c index fa7ae11bcb9cae7440fc8837904e8d55f8f5cd06..95b729e4db3f7afc0acffa50bec589ad3fe691bb 100644 --- a/passwords.c +++ b/passwords.c @@ -14,15 +14,17 @@ struct passwords { char **cracked; //array of words int passwords_left; int current_size; + int n_guesses; }; -Passwords* create_passwords(int passwords_to_guess){ +Passwords* create_passwords(int passwords_to_guess, int n_guesses){ Passwords* passwords = malloc(sizeof *passwords); assert(passwords); passwords->cracked = malloc(sizeof(char*)*passwords_to_guess); assert(passwords->cracked); passwords->passwords_left = passwords_to_guess; passwords->current_size = 0; + passwords->n_guesses = n_guesses; return passwords; } @@ -52,9 +54,16 @@ void print_passwords(Passwords* pwrds){ } } +void made_guess(Passwords* pwrds){ + pwrds->n_guesses--; +} + +int get_remaining_guesses(Passwords* pwrds){ + return pwrds->n_guesses; +} -int generate_common_subs_four(Passwords* solved, int n_guesses, HashTable *ht){ - if (remaining_hashes(ht) == 0 || n_guesses == 0){ +int generate_common_subs_four(Passwords* solved, HashTable *ht){ + if (remaining_hashes(ht) == 0 || get_remaining_guesses(solved) == 0){ return 0; } char **subs; @@ -111,8 +120,8 @@ int generate_common_subs_four(Passwords* solved, int n_guesses, HashTable *ht){ printf("remaining passwords are : %d\n", remaining_hashes(ht)); } free(hex_guess); - n_guesses--; - if (remaining_hashes(ht) == 0 || n_guesses == 0){ + made_guess(solved); + if (remaining_hashes(ht) == 0 || get_remaining_guesses(solved) == 0){ return 0; } } @@ -128,12 +137,12 @@ int generate_common_subs_four(Passwords* solved, int n_guesses, HashTable *ht){ free(characters); free(subs); free(subs_size); - return n_guesses; + return get_remaining_guesses(solved); } -int generate_common_subs_six(Passwords* solved, int n_guesses, HashTable *ht){ - if (remaining_hashes(ht) == 0 || n_guesses == 0){ +int generate_common_subs_six(Passwords* solved, HashTable *ht){ + if (remaining_hashes(ht) == 0 || get_remaining_guesses(solved) == 0){ return 0; } char **subs; @@ -194,8 +203,8 @@ int generate_common_subs_six(Passwords* solved, int n_guesses, HashTable *ht){ printf("remaining passwords are : %d\n", remaining_hashes(ht)); } free(hex_guess); - n_guesses--; - if (remaining_hashes(ht) == 0 || n_guesses == 0){ + made_guess(solved); + if (remaining_hashes(ht) == 0 || get_remaining_guesses(solved) == 0){ return 0; } } @@ -210,5 +219,5 @@ int generate_common_subs_six(Passwords* solved, int n_guesses, HashTable *ht){ free(characters); free(subs); free(subs_size); - return n_guesses; + return get_remaining_guesses(solved); } \ No newline at end of file diff --git a/passwords.h b/passwords.h index 888a700da7bb83ab8589bae105a40ee7e46bcf8b..e143441b0a18b73c701da8d0942d6832166f0958 100644 --- a/passwords.h +++ b/passwords.h @@ -4,7 +4,7 @@ typedef struct passwords Passwords; -Passwords* create_passwords(int passwords_to_guess); +Passwords* create_passwords(int passwords_to_guess, int n_guesses); void add_new_cracked(Passwords * pwrds, char* pword); @@ -14,6 +14,10 @@ void free_passwords(Passwords* pwrds); void print_passwords(Passwords* pwrds); -int generate_common_subs_four(Passwords* solved, int n_guesses, HashTable *ht); +int generate_common_subs_four(Passwords* solved, HashTable *ht); -int generate_common_subs_six(Passwords* solved, int n_guesses, HashTable *ht); \ No newline at end of file +int generate_common_subs_six(Passwords* solved, HashTable *ht); + +int get_remaining_guesses(Passwords *pwrds); + +void made_guess(Passwords *pwrds); \ No newline at end of file diff --git a/six_password_strategy.c b/six_password_strategy.c index 8b5ce8e061b297363bc833ee32a21d93960c340f..09e4fa4b4b4bf56e0977ac8a8ca70f3ce81fc1c0 100644 --- a/six_password_strategy.c +++ b/six_password_strategy.c @@ -9,11 +9,11 @@ #include "sha256-helper.h" #include "passwords.h" -void brute_force_six(HashTable *ht, Passwords *solved, int n_guesses){ +void brute_force_six(HashTable *ht, Passwords *solved){ char brute_guess[6]; SHA256_CTX ctx; int hash; - if (remaining_hashes(ht) == 0 || n_guesses == 0){ + if (remaining_hashes(ht) == 0 || get_remaining_guesses(solved) == 0){ return; } for(int i=65;i < 127; i++){ @@ -38,11 +38,14 @@ void brute_force_six(HashTable *ht, Passwords *solved, int n_guesses){ printf("%s %d\n", brute_guess, hash); add_new_cracked(solved, brute_guess); printf("remaining passwords are : %d\n", remaining_hashes(ht)); - n_guesses = generate_common_subs_six(solved, n_guesses, ht); + generate_common_subs_six(solved, ht); + if (get_remaining_guesses(solved) == 0 || remaining_hashes(ht) == 0){ + return; + } } free(hex_guess); - n_guesses--; - if (remaining_hashes(ht) == 0 || n_guesses == 0){ + made_guess(solved); + if (remaining_hashes(ht) == 0 || get_remaining_guesses(solved) == 0){ return; } } @@ -58,7 +61,7 @@ void brute_force_six(HashTable *ht, Passwords *solved, int n_guesses){ } } } -int popular_character_guess_six(HashTable *ht, Passwords *solved,int n_guesses){ +int popular_character_guess_six(HashTable *ht, Passwords *solved){ printf("popular character guess strategy\n"); char brute_guess[4]; SHA256_CTX ctx; @@ -67,7 +70,7 @@ int popular_character_guess_six(HashTable *ht, Passwords *solved,int n_guesses){ FILE *file = fopen("common_password_frequency.txt", "r"); char frequent_characters[60]; int index = 0; - if (remaining_hashes(ht) == 0 || n_guesses == 0){ + if (remaining_hashes(ht) == 0 || get_remaining_guesses(solved) == 0){ fclose(file); return 0; } @@ -99,34 +102,38 @@ int popular_character_guess_six(HashTable *ht, Passwords *solved,int n_guesses){ printf("%s %d\n", brute_guess, hash); add_new_cracked(solved, brute_guess); printf("remaining passwords are : %d\n", remaining_hashes(ht)); - n_guesses = generate_common_subs_six(solved,n_guesses,ht); + generate_common_subs_six(solved,ht); + if (get_remaining_guesses(solved) == 0 || remaining_hashes(ht) == 0){ + return 0; + } } free(hex_guess); - n_guesses--; - if (remaining_hashes(ht) == 0 || n_guesses == 0){ - return n_guesses; + made_guess(solved); + if (remaining_hashes(ht) == 0 || get_remaining_guesses(solved) == 0){ + return get_remaining_guesses(solved); } } } } } } - }return n_guesses; + }return get_remaining_guesses(solved); } -int generate_guesses_six(int n_guesses, HashTable *ht, Passwords *solved){ - FILE* file = fopen("proj-2_common_passwords.txt", "r"); +int generate_guesses_six(char* file_name, HashTable *ht, Passwords *solved){ + 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 && n_guesses != 0){ + 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]; @@ -136,12 +143,15 @@ int generate_guesses_six(int n_guesses, HashTable *ht, Passwords *solved){ printf("%s %d\n", line, hash); add_new_cracked(solved, line); printf("remaining passwords are : %d\n", remaining_hashes(ht)); - n_guesses = generate_common_subs_six(solved,n_guesses,ht); + generate_common_subs_six(solved,ht); + if (get_remaining_guesses(solved) == 0 || remaining_hashes(ht) == 0){ + return 0; + } } - n_guesses--; + made_guess(solved); free(hex_guess); } printf("finished file"); fclose(file); - return n_guesses; + return get_remaining_guesses(solved); } \ No newline at end of file diff --git a/six_password_strategy.h b/six_password_strategy.h index 3809a9791ef80742f77d2e1c1e9a6f7216c867f0..324b639fa93eb83214ea4254b841aaeb57a5942e 100644 --- a/six_password_strategy.h +++ b/six_password_strategy.h @@ -2,6 +2,6 @@ #include "hashtable.h" #include "passwords.h" -void brute_force_six(HashTable *ht, Passwords *solved,int n_guesses); -int popular_character_guess_six(HashTable *ht, Passwords *solved,int n_guesses); -int generate_guesses_six(int n_guesses, HashTable *ht, Passwords *solved); \ No newline at end of file +void brute_force_six(HashTable *ht, Passwords *solved); +int popular_character_guess_six(HashTable *ht, Passwords *solved); +int generate_guesses_six(char* file_name, HashTable *ht, Passwords *solved); \ No newline at end of file