|
| 1 | +#include <math.h> |
| 2 | +#include <openssl/md5.h> |
| 3 | +#include <pthread.h> |
| 4 | +#include <stdbool.h> |
| 5 | +#include <stdint.h> |
| 6 | +#include <stdio.h> |
| 7 | +#include <stdlib.h> |
| 8 | +#include <string.h> |
| 9 | +#include <unistd.h> |
| 10 | + |
| 11 | +#include "libgthread.hh" |
| 12 | + |
| 13 | +#define NUM_THREAD 4 |
| 14 | +#define MAX_USERNAME_LENGTH 24 |
| 15 | +#define PASSWORD_LENGTH 6 |
| 16 | +#define TOTAL_PASSWORDS (pow(26.0, PASSWORD_LENGTH)) |
| 17 | + |
| 18 | +// Use this struct to pass arguments to our threads |
| 19 | +typedef struct thread_args { |
| 20 | + int arg; |
| 21 | +} thread_args_t; |
| 22 | + |
| 23 | +// use this struct to receive results from our threads |
| 24 | +typedef struct thread_result { |
| 25 | + int result; |
| 26 | +} thread_result_t; |
| 27 | + |
| 28 | +typedef struct password_entry { |
| 29 | + char username[MAX_USERNAME_LENGTH + 1]; |
| 30 | + uint8_t password_md5[MD5_DIGEST_LENGTH + 1]; |
| 31 | + bool cracked; |
| 32 | + struct password_entry *next; |
| 33 | +} password_entry_t; |
| 34 | + |
| 35 | +void crack_passwords(char *plaintext); |
| 36 | +void generate_all_possibilities(size_t number); |
| 37 | +int md5_string_to_bytes(const char *md5_string, uint8_t *bytes); |
| 38 | +password_entry_t *read_password_file(const char *filename); |
| 39 | +void print_md5_bytes(const uint8_t *bytes); |
| 40 | + |
| 41 | +password_entry_t *passwords; |
| 42 | + |
| 43 | +// pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER; |
| 44 | + |
| 45 | +/** |
| 46 | + * This is our thread function. When we call pthread_create with this as an |
| 47 | + * argument |
| 48 | + * a new thread is created to run this thread in parallel with the program's |
| 49 | + * main |
| 50 | + * thread. When passing parameters to thread functions or accepting return |
| 51 | + * values we |
| 52 | + * have to jump through a few hoops because POSIX threads can only take and |
| 53 | + * return |
| 54 | + * a void*. |
| 55 | + */ |
| 56 | +void *thread_fn(void *void_args) { |
| 57 | + // Case the args pointer to the appropriate type and print our argument |
| 58 | + thread_args_t *args = (thread_args_t *)void_args; |
| 59 | + generate_all_possibilities(args->arg); |
| 60 | + // Return the pointer to allocated memory to our parent thread. |
| 61 | + return NULL; |
| 62 | +} |
| 63 | + |
| 64 | +int main(int argc, char **argv) { |
| 65 | + if (argc != 2) { |
| 66 | + fprintf(stderr, "Usage: %s <path to password directory file>\n", argv[0]); |
| 67 | + exit(1); |
| 68 | + } |
| 69 | + |
| 70 | + // Read in the password file |
| 71 | + passwords = read_password_file(argv[1]); |
| 72 | + |
| 73 | + // Initilization |
| 74 | + password_entry_t *current = passwords; |
| 75 | + while (current != NULL) { |
| 76 | + current->cracked = false; |
| 77 | + current = current->next; |
| 78 | + } |
| 79 | + // You'll have to move over any code from partB that you would like to use. |
| 80 | + // Here's a quick little thread demo. |
| 81 | + pthread_t threads[NUM_THREAD]; |
| 82 | + |
| 83 | + // Make two structs so we can pass arguments to our threads |
| 84 | + thread_args_t thread_args[NUM_THREAD]; |
| 85 | + |
| 86 | + // Create threads |
| 87 | + for (size_t i = 0; i < NUM_THREAD; i++) { |
| 88 | + thread_args[i].arg = i * TOTAL_PASSWORDS / 4; |
| 89 | + if (pthread_create(&threads[i], NULL, thread_fn, &thread_args[i]) != 0) { |
| 90 | + perror("Error creating thread 1"); |
| 91 | + exit(2); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // Make pointers to the thread result structs that our threads will write into |
| 96 | + // thread_result_t *thread_result[NUM_THREAD]; |
| 97 | + |
| 98 | + // Wait threads to join |
| 99 | + for (size_t i = 0; i < NUM_THREAD; i++) { |
| 100 | + if (pthread_join(threads[i], NULL) != 0) { |
| 101 | + perror("Error joining with thread 1"); |
| 102 | + exit(2); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // // Show the results |
| 107 | + // printf("Thread 1 returned %d\n", thread1_result->result); |
| 108 | + // printf("Thread 2 retunred %d\n", thread2_result->result); |
| 109 | + // |
| 110 | + // // Free the result structs, which were originally allocated in our threads |
| 111 | + // free(thread1_result); |
| 112 | + // free(thread2_result); |
| 113 | +} |
| 114 | + |
| 115 | +void generate_all_possibilities(size_t number) { |
| 116 | + int cur_digit = PASSWORD_LENGTH - 1; |
| 117 | + size_t len = TOTAL_PASSWORDS / 4; |
| 118 | + char guess[7] = "aaaaaa"; |
| 119 | + size_t end = number + len; |
| 120 | + for (size_t i = number; i < end; i++) { |
| 121 | + size_t num = i; |
| 122 | + while (num > 0) { |
| 123 | + guess[cur_digit--] = num % 26 + 'a'; |
| 124 | + num /= 26; |
| 125 | + } |
| 126 | + cur_digit = PASSWORD_LENGTH - 1; |
| 127 | + |
| 128 | + crack_passwords(guess); |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +void crack_passwords(char *plaintext) { |
| 133 | + uint8_t password_hash[MD5_DIGEST_LENGTH]; |
| 134 | + MD5((unsigned char *)plaintext, strlen(plaintext), password_hash); |
| 135 | + |
| 136 | + // Check if the two hashes are equal |
| 137 | + password_entry_t *current = passwords; |
| 138 | + bool all_true = true; |
| 139 | + while (current != NULL) { |
| 140 | + if (!current->cracked) { // Has not been cracked yet |
| 141 | + if (memcmp(current->password_md5, password_hash, MD5_DIGEST_LENGTH) == |
| 142 | + 0) { |
| 143 | + printf("%s ", current->username); |
| 144 | + printf("%s\n", plaintext); |
| 145 | + // pthread_mutex_lock(&m); |
| 146 | + current->cracked = true; |
| 147 | + // pthread_mutex_unlock(&m); |
| 148 | + } else { |
| 149 | + all_true = false; |
| 150 | + } |
| 151 | + } |
| 152 | + current = current->next; |
| 153 | + } |
| 154 | + if (all_true) { |
| 155 | + printf("Rollback count= %zu\n", *Gstm::rollback_count_); |
| 156 | + _exit(0); |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +/** |
| 161 | + * Read a file of username and MD5 passwords. Return a linked list |
| 162 | + * of entries. |
| 163 | + * \param filename The path to the password file |
| 164 | + * \returns A pointer to the first node in the password list |
| 165 | + */ |
| 166 | +password_entry_t *read_password_file(const char *filename) { |
| 167 | + // Open the password file |
| 168 | + FILE *password_file = fopen(filename, "r"); |
| 169 | + if (password_file == NULL) { |
| 170 | + perror("opening password file"); |
| 171 | + exit(2); |
| 172 | + } |
| 173 | + |
| 174 | + // Keep track of the current list |
| 175 | + password_entry_t *list = NULL; |
| 176 | + |
| 177 | + // Read until we hit the end of the file |
| 178 | + while (!feof(password_file)) { |
| 179 | + // Make space for a new node |
| 180 | + password_entry_t *newnode = |
| 181 | + (password_entry_t *)malloc(sizeof(password_entry_t)); |
| 182 | + |
| 183 | + // Make space to hold the MD5 string |
| 184 | + char md5_string[MD5_DIGEST_LENGTH * 2 + 1]; |
| 185 | + |
| 186 | + // Try to read. The space in the format string is required to eat the |
| 187 | + // newline |
| 188 | + if (fscanf(password_file, "%s %s ", newnode->username, md5_string) != 2) { |
| 189 | + fprintf(stderr, "Error reading password file: malformed line\n"); |
| 190 | + exit(2); |
| 191 | + } |
| 192 | + |
| 193 | + // Convert the MD5 string to MD5 bytes in our new node |
| 194 | + if (md5_string_to_bytes(md5_string, newnode->password_md5) != 0) { |
| 195 | + fprintf(stderr, "Error reading MD5\n"); |
| 196 | + exit(2); |
| 197 | + } |
| 198 | + |
| 199 | + // Add the new node to the front of the list |
| 200 | + newnode->next = list; |
| 201 | + list = newnode; |
| 202 | + } |
| 203 | + |
| 204 | + return list; |
| 205 | +} |
| 206 | + |
| 207 | +/** |
| 208 | + * Convert a string representation of an MD5 hash to a sequence |
| 209 | + * of bytes. The input md5_string must be 32 characters long, and |
| 210 | + * the output buffer bytes must have room for MD5_DIGEST_LENGTH |
| 211 | + * bytes. |
| 212 | + * |
| 213 | + * \param md5_string The md5 string representation |
| 214 | + * \param bytes The destination buffer for the converted md5 hash |
| 215 | + * \returns 0 on success, -1 otherwise |
| 216 | + */ |
| 217 | +int md5_string_to_bytes(const char *md5_string, uint8_t *bytes) { |
| 218 | + // Check for a valid MD5 string |
| 219 | + if (strlen(md5_string) != 2 * MD5_DIGEST_LENGTH) return -1; |
| 220 | + |
| 221 | + // Start our "cursor" at the start of the string |
| 222 | + const char *pos = md5_string; |
| 223 | + |
| 224 | + // Loop until we've read enough bytes |
| 225 | + for (size_t i = 0; i < MD5_DIGEST_LENGTH; i++) { |
| 226 | + // Read one byte (two characters) |
| 227 | + int rc = sscanf(pos, "%2hhx", &bytes[i]); |
| 228 | + if (rc != 1) return -1; |
| 229 | + |
| 230 | + // Move the "cursor" to the next hexadecimal byte |
| 231 | + pos += 2; |
| 232 | + } |
| 233 | + |
| 234 | + return 0; |
| 235 | +} |
| 236 | + |
| 237 | +/** |
| 238 | + * Print a byte array that holds an MD5 hash to standard output. |
| 239 | + * |
| 240 | + * \param bytes An array of bytes from an MD5 hash function |
| 241 | + */ |
| 242 | +void print_md5_bytes(const uint8_t *bytes) { |
| 243 | + for (size_t i = 0; i < MD5_DIGEST_LENGTH; i++) { |
| 244 | + printf("%02hhx", bytes[i]); |
| 245 | + } |
| 246 | +} |
0 commit comments