Advertisement
Kirokyri

C6

Nov 13th, 2020
599
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.45 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include <string.h>
  5.  
  6. #include <unistd.h>
  7. #include <crypt.h>
  8. #include <time.h>
  9.  
  10. #include <pthread.h>
  11. #include <semaphore.h>
  12.  
  13. typedef enum
  14. {
  15.     BM_ITER,
  16.     BM_REC,
  17. } brute_mode_t;
  18.  
  19. typedef struct config_t {
  20.     brute_mode_t brute_mode;
  21.     int length;
  22.     char * alphabet;
  23.     char * hash;
  24. } config_t;
  25.  
  26. typedef struct task_t {
  27.     char password[8];
  28. } task_t;
  29.  
  30. typedef struct queue_t {
  31.     task_t queue[8];
  32.     int head, tail;
  33.     pthread_t head_mutex, tail_mutex;
  34.     sem_t empty, full;
  35. } queue_t;
  36.  
  37. typedef bool (*password_handler_t) (config_t * config, char * password);
  38.  
  39. void queue_init (queue_t * queue)
  40. {
  41.     queue->head = 0;
  42.     queue->tail = 0;
  43.     pthread_mutex_init (queue->head_mutex, NULL);
  44.     pthread_mutex_init (queue->tail_mutex, NULL);
  45.     sem_init (queue->empty, 0, strlen (queue->queue));
  46.     sem_init (queue->full, 0, 0);
  47. }
  48.  
  49. void queue_push (queue_t * queue, task_t * task)
  50. {
  51.     //7
  52.     sem_wait (queue->empty);
  53.     mutex_lock (queue->tail_mutex);
  54.     queue->queue[queue->tail] = task;
  55.     queue->tail++;
  56.     mutex_unlock (queue->tail_mutex);
  57.     sem_post (queue->full);
  58.    
  59. }
  60.  
  61. void queue_pop (queue_t * queue, task_t * task)
  62. {
  63.     //7
  64.     sem_wait(queue->full);
  65.     mutex_lock (queue->head_mutex);
  66.     int i;
  67.     for (i = 0; i < queue->tail - 1; i++)
  68.         queue->queue[i] = queue->queue[i - 1];
  69.     queue->tail--;
  70.     mutex_unlock (queue->head_mutex);
  71.     sem_post (queue->empty);
  72. }
  73.  
  74. bool rec(config_t* config, char* password, int pos, password_handler_t password_handler)
  75. {
  76.     int i;
  77.     if (pos >= config->length)
  78.     {
  79.         if (password_handler (config, password))
  80.         {
  81.             return (true);
  82.         }
  83.     }
  84.     else
  85.     {
  86.         for (i = 0; config->alphabet[i]; ++i)
  87.         {
  88.             password[pos] = config->alphabet[i];
  89.             if (rec (config, password, pos + 1, password_handler))
  90.                 return (true);
  91.         }
  92.     }
  93.     return (false);
  94. }
  95.  
  96. void rec_wrapper(config_t* config, char* password, password_handler_t password_handler)
  97. {
  98.     rec (config, password, 0, password_handler);
  99. }
  100.  
  101. void iter(config_t* config, char* password, password_handler_t password_handler)
  102. {
  103.     int alph_length_1 = strlen(config->alphabet) - 1;
  104.     int idx[config->length];
  105.     int i;
  106.  
  107.     for (i = 0; i < config->length; i++)
  108.     {
  109.         idx[i] = 0;
  110.         password[i] = config->alphabet[0];
  111.     }
  112.  
  113.     for (;;)
  114.     {
  115.          if (password_handler (config, password))
  116.             break;
  117.         for (i = config->length - 1; (i >= 0) && (idx[i] == alph_length_1); i--)
  118.         {
  119.             idx[i] = 0;
  120.             password[i] = config->alphabet[0];
  121.         }
  122.  
  123.         if (i < 0)
  124.             break;
  125.  
  126.         password[i] = config->alphabet[++idx[i]];
  127.     }
  128. }
  129.  
  130. void parse_params(config_t* config, int argc, char* argv[])
  131. {
  132.     for (;;)
  133.     {
  134.         int opt = getopt(argc, argv, "a:l:h:ir");
  135.         if (opt == -1)
  136.             break;
  137.         switch (opt)
  138.         {
  139.         case 'i':
  140.             config->brute_mode = BM_ITER;
  141.             break;
  142.  
  143.         case 'r':
  144.             config->brute_mode = BM_REC;
  145.             break;
  146.  
  147.         case 'a':
  148.             config->alphabet = optarg;
  149.             break;
  150.  
  151.         case 'h':
  152.             config->hash = optarg;
  153.             break;
  154.  
  155.         case 'l':
  156.             config->length = atoi(optarg);
  157.             break;
  158.  
  159.         default:
  160.             break;
  161.         }
  162.     }
  163. }
  164.  
  165. bool print_password (config_t * config, char * password)
  166. {
  167.     printf ("%s\n", password);
  168.     return (false);
  169. }
  170.  
  171. bool check_password (config_t * config, char * password)
  172. {
  173.     char * hash = crypt (password, config->hash);
  174.     int cmp = strcmp (hash, config->hash);
  175.     return (0 == cmp);
  176. }
  177.  
  178. int main(int argc, char* argv[])
  179. {
  180.     config_t config =
  181.     {
  182.         .brute_mode = BM_ITER,
  183.         .alphabet = "01",
  184.         .length = 8,
  185.         //.hash = "abn1Mv9fopD9Y",
  186.         .hash = strdup (crypt ("11110100", "11")),
  187.     };
  188.  
  189.     parse_params(&config, argc, argv);
  190.  
  191.     char password[config.length + 1];
  192.     password[config.length] = 0;
  193.  
  194.  
  195.     switch (config.brute_mode)
  196.     {
  197.         case BM_ITER:
  198.             iter (&config, password, check_password);
  199.             break;
  200.         case BM_REC:
  201.             rec_wrapper(&config, password, check_password);
  202.             break;
  203.     }
  204.     printf ("%s\n", password);
  205.     return (EXIT_SUCCESS);
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement