Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <string.h>
- #include <unistd.h>
- #include <crypt.h>
- #include <time.h>
- #include <pthread.h>
- #include <semaphore.h>
- typedef enum
- {
- BM_ITER,
- BM_REC,
- } brute_mode_t;
- typedef struct config_t {
- brute_mode_t brute_mode;
- int length;
- char * alphabet;
- char * hash;
- } config_t;
- typedef struct task_t {
- char password[8];
- } task_t;
- typedef struct queue_t {
- task_t queue[8];
- int head, tail;
- pthread_t head_mutex, tail_mutex;
- sem_t empty, full;
- } queue_t;
- typedef bool (*password_handler_t) (config_t * config, char * password);
- void queue_init (queue_t * queue)
- {
- queue->head = 0;
- queue->tail = 0;
- pthread_mutex_init (queue->head_mutex, NULL);
- pthread_mutex_init (queue->tail_mutex, NULL);
- sem_init (queue->empty, 0, strlen (queue->queue));
- sem_init (queue->full, 0, 0);
- }
- void queue_push (queue_t * queue, task_t * task)
- {
- //7
- sem_wait (queue->empty);
- mutex_lock (queue->tail_mutex);
- queue->queue[queue->tail] = task;
- queue->tail++;
- mutex_unlock (queue->tail_mutex);
- sem_post (queue->full);
- }
- void queue_pop (queue_t * queue, task_t * task)
- {
- //7
- sem_wait(queue->full);
- mutex_lock (queue->head_mutex);
- int i;
- for (i = 0; i < queue->tail - 1; i++)
- queue->queue[i] = queue->queue[i - 1];
- queue->tail--;
- mutex_unlock (queue->head_mutex);
- sem_post (queue->empty);
- }
- bool rec(config_t* config, char* password, int pos, password_handler_t password_handler)
- {
- int i;
- if (pos >= config->length)
- {
- if (password_handler (config, password))
- {
- return (true);
- }
- }
- else
- {
- for (i = 0; config->alphabet[i]; ++i)
- {
- password[pos] = config->alphabet[i];
- if (rec (config, password, pos + 1, password_handler))
- return (true);
- }
- }
- return (false);
- }
- void rec_wrapper(config_t* config, char* password, password_handler_t password_handler)
- {
- rec (config, password, 0, password_handler);
- }
- void iter(config_t* config, char* password, password_handler_t password_handler)
- {
- int alph_length_1 = strlen(config->alphabet) - 1;
- int idx[config->length];
- int i;
- for (i = 0; i < config->length; i++)
- {
- idx[i] = 0;
- password[i] = config->alphabet[0];
- }
- for (;;)
- {
- if (password_handler (config, password))
- break;
- for (i = config->length - 1; (i >= 0) && (idx[i] == alph_length_1); i--)
- {
- idx[i] = 0;
- password[i] = config->alphabet[0];
- }
- if (i < 0)
- break;
- password[i] = config->alphabet[++idx[i]];
- }
- }
- void parse_params(config_t* config, int argc, char* argv[])
- {
- for (;;)
- {
- int opt = getopt(argc, argv, "a:l:h:ir");
- if (opt == -1)
- break;
- switch (opt)
- {
- case 'i':
- config->brute_mode = BM_ITER;
- break;
- case 'r':
- config->brute_mode = BM_REC;
- break;
- case 'a':
- config->alphabet = optarg;
- break;
- case 'h':
- config->hash = optarg;
- break;
- case 'l':
- config->length = atoi(optarg);
- break;
- default:
- break;
- }
- }
- }
- bool print_password (config_t * config, char * password)
- {
- printf ("%s\n", password);
- return (false);
- }
- bool check_password (config_t * config, char * password)
- {
- char * hash = crypt (password, config->hash);
- int cmp = strcmp (hash, config->hash);
- return (0 == cmp);
- }
- int main(int argc, char* argv[])
- {
- config_t config =
- {
- .brute_mode = BM_ITER,
- .alphabet = "01",
- .length = 8,
- //.hash = "abn1Mv9fopD9Y",
- .hash = strdup (crypt ("11110100", "11")),
- };
- parse_params(&config, argc, argv);
- char password[config.length + 1];
- password[config.length] = 0;
- switch (config.brute_mode)
- {
- case BM_ITER:
- iter (&config, password, check_password);
- break;
- case BM_REC:
- rec_wrapper(&config, password, check_password);
- break;
- }
- printf ("%s\n", password);
- return (EXIT_SUCCESS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement