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 enum //
- {
- RM_SINGLE,
- RM_MULTI,
- } run_mode_t;
- typedef struct config_t {
- brute_mode_t brute_mode;
- run_mode_t run_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_mutex_t head_mutex, tail_mutex;
- sem_t empty, full;
- } queue_t;
- typedef struct {
- config_t * config;
- task_t found;
- queue_t queue;
- } pc_context_t;
- typedef bool (*password_handler_t) (void * context, 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, sizeof (queue->queue) / sizeof (queue->queue[0]));
- sem_init (&queue->full, 0, 0);
- }
- void queue_push (queue_t * queue, task_t * task)
- {
- //7
- sem_wait (&queue->empty);
- pthread_mutex_lock (&queue->tail_mutex);
- queue->queue[queue->tail] = *task;
- if (++queue->tail == sizeof (queue->queue) / sizeof (queue->queue[0]))
- queue->tail = 0;
- pthread_mutex_unlock (&queue->tail_mutex);
- sem_post (&queue->full);
- }
- void queue_pop (queue_t * queue, task_t * task)
- {
- //7
- sem_wait(&queue->full);
- pthread_mutex_lock (&queue->head_mutex);
- *task = queue->queue[queue->head];
- if (++queue->head == sizeof (queue->queue) / sizeof (queue->queue[0]))
- queue->head = 0;
- pthread_mutex_unlock (&queue->head_mutex);
- sem_post (&queue->empty);
- }
- bool rec(config_t* config, char* password, int pos, password_handler_t password_handler, void * context)
- {
- int i;
- if (pos >= config->length)
- {
- if (password_handler (context, password))
- {
- return (true);
- }
- }
- else
- {
- for (i = 0; config->alphabet[i]; ++i)
- {
- password[pos] = config->alphabet[i];
- if (rec (config, password, pos + 1, password_handler, context))
- return (true);
- }
- }
- return (false);
- }
- void rec_wrapper(config_t* config, char* password, password_handler_t password_handler, void * context)
- {
- rec (config, password, 0, password_handler, context);
- }
- void iter(config_t* config, char* password, password_handler_t password_handler, void * context)
- {
- 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 (context, 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:irsm"); //
- if (opt == -1)
- break;
- switch (opt)
- {
- case 's':
- config->run_mode = RM_SINGLE; //
- break;
- case 'm':
- config->run_mode = RM_MULTI; //
- break;
- 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 (void * context, char * password)
- {
- config_t * config = context;
- char * hash = crypt (password, config->hash);
- printf ("%s %s %s\n","hash in check", hash, password);
- //printf ("%s %s\n", "config_hash in check", config->hash);
- int cmp = strcmp (hash, config->hash);
- return (0 == cmp);
- }
- void run_single (config_t * config, char * password)
- {
- switch (config->brute_mode)
- {
- case BM_ITER:
- iter (config, password, check_password, config);
- break;
- case BM_REC:
- rec_wrapper(config, password, check_password, config);
- break;
- }
- }
- void * consumer (void * arg)
- {
- pc_context_t * pc_context = arg;
- for (;;)
- {
- task_t task;
- queue_pop (&pc_context->queue, &task);
- printf ("consumer task password %s\n", task.password);
- if (check_password (pc_context->config, task.password))
- {
- pc_context->found = task;
- }
- }
- }
- bool push_to_queue (void * context, char * password)
- {
- pc_context_t * pc_context = context;
- task_t task;
- strcpy (task.password, password);
- queue_push (&pc_context->queue, &task);
- return (pc_context->found.password[0]);
- }
- void run_multi (config_t * config, char * password)
- {
- pc_context_t pc_context;
- pc_context.config = config;
- printf ("%s %s\n", "pc_context_config_in_multi", pc_context.config->hash);
- //int num_cpu = sysconf (_SC_NPROCESSORS_ONLN);
- int num_cpu = 1;
- int i;
- pc_context.found.password[0] = 0;
- queue_init (&pc_context.queue);
- for (i = 0; i < num_cpu; ++i)
- {
- pthread_t id;
- pthread_create (&id, NULL, consumer, &pc_context);
- }
- switch (config->brute_mode)
- {
- case BM_ITER:
- iter (config, password, push_to_queue, &pc_context);
- break;
- case BM_REC:
- rec_wrapper(config, password, push_to_queue, &pc_context);
- break;
- }
- //password = pc_context.found.password;
- password = strdup (pc_context.found.password);
- }
- int main(int argc, char* argv[])
- {
- config_t config =
- {
- .run_mode = RM_SINGLE, //
- .brute_mode = BM_ITER,
- .alphabet = "01",
- .length = 6,
- .hash = strdup (crypt ("110110", "11")),
- //.hash = "11G3BJNEUV/Pk",
- };
- parse_params(&config, argc, argv);
- char password[config.length + 1];
- password[config.length] = 0;
- switch (config.run_mode)
- {
- case RM_SINGLE:
- run_single (&config, password);
- break;
- case RM_MULTI:
- run_multi (&config, password);
- }
- printf ("%s\n", password);
- return (EXIT_SUCCESS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement