Guest User

Untitled

a guest
Jan 18th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <sys/wait.h>
  5. #include <sys/types.h>
  6. #include <string.h>
  7. #include <crypt.h>
  8.  
  9. #define max_pass_size 3
  10. #define students_pass_file "passwords.txt"
  11. #define max_forks 4
  12.  
  13. typedef struct{
  14.         char username[35];
  15.         char password[15];
  16. }       User;
  17.  
  18. typedef struct{
  19.         char username[35];
  20.         char password[15];
  21. }       New_User;
  22.  
  23. static char *Alfabeth = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  24. int nsucessos=0,ntentativas=0;
  25.  
  26. void worker();
  27. void master();
  28. int n_pass();
  29. void carrega_tabela();
  30. int BruteForce();
  31. int CheckCrypt();
  32.  
  33. // File descriptors for the pipe channel
  34. int channel[2];
  35.  
  36. int main() {  
  37.         int size,i=0;
  38.         New_User new_tabela;
  39.     // Create a pipe
  40.     pipe(channel);
  41.     char word[max_pass_size+1] = "";
  42.         size=n_pass();
  43.         User tabela[size];
  44.         carrega_tabela(tabela,size);
  45.         //for (i=0;i<size;i++) printf("%s %s\n",tabela[i].username,tabela[i].password);
  46.     // Create the processes
  47.     pid_t id;
  48.     while(i<max_forks){
  49.                 id = fork();
  50.         if (id == 0) {
  51.             close(channel[0]);
  52.                         //sleep(1);
  53.                         //worker(tabela, size, i);
  54.                         BruteForce(word, 0, size/max_forks, tabela, i);
  55.             i=max_forks;
  56.             //exit(0);
  57.         }
  58.         i++;
  59.         }
  60.         if (id > 0)
  61.         {
  62.                         while(1){
  63.                 read(channel[0], &new_tabela, sizeof(New_User));
  64.                 printf("username:%s_password:%s\n", new_tabela.username, new_tabela.password);
  65.                         //master(tabela);
  66.                     }
  67.                 }
  68.     return 0;
  69. }
  70.  
  71. int n_pass(){
  72.         int size=0;
  73.         char temp[50];
  74.         FILE *ficheiro=fopen(students_pass_file,"r");
  75.         while ((fscanf(ficheiro,"%s",temp) != -1 ))             size++;
  76.         fclose(ficheiro);
  77.         return size;
  78. }
  79.  
  80. void carrega_tabela(User *tabela, int size){
  81.  
  82.     int i=0;
  83.     char temp[50];
  84.         char *result;
  85.  
  86.         FILE *ficheiro=fopen(students_pass_file,"r");
  87.  
  88.         while ((fscanf(ficheiro,"%s",temp) != -1 )){
  89.                 result=strtok(temp,":");
  90.                 strcpy(tabela[i].username,result);
  91.                 result=strtok(NULL,"\n");
  92.                 strcpy(tabela[i].password,result);
  93.                 i++;
  94.         }
  95.         fclose(ficheiro);
  96. }
  97.  
  98. /*void worker(User *tabela, int size, int proc_n) {
  99.  
  100.     int i, test;
  101.     test=size/max_forks;
  102.     char word[10];
  103.  
  104.     close(channel[0]);
  105.  
  106.     while (1) {
  107.         for (i = 1; i < max_pass_size+1; i++){
  108.             BruteForce(word, 0, i, test, tabela, proc_n);
  109.         }
  110.     }
  111.     return;
  112. }*/
  113.  
  114. void master(User *tabela) {
  115.         New_User new_tabela;
  116.  
  117.     close(channel[1]);
  118.     fd_set read_set;
  119.  
  120.         FD_ZERO(&read_set);
  121.         FD_SET(channel[0], &read_set);
  122.         if ( select(channel[0]+1, &read_set, NULL, NULL, NULL) > 0 ) {
  123.             if (FD_ISSET(channel[0], &read_set))
  124.                 read(channel[0], &new_tabela, sizeof(New_User));
  125.                 printf("username:%s_password:%s\n", new_tabela.username, new_tabela.password);
  126.                 return;
  127.         }
  128.         //sleep(1);
  129. }
  130.  
  131. int BruteForce(char *word, int counter, int size, User *tabela, int check){
  132.         New_User new_tabela;
  133.     int i,j,result;
  134.     char aux[15];
  135.     if (counter >= max_pass_size) return 0;
  136.     for (i = 0; i < strlen(Alfabeth); i++)
  137.     {
  138.         word[counter] = Alfabeth[i];
  139.         word[counter+1] = '\0';
  140.         strcpy(aux,crypt(word,"10"));
  141.         for (j=0;j<size;j++){
  142.                         ntentativas++;
  143.                         result = strcmp(aux, tabela[j+(check*size)].password);
  144.                                 if (result == 0)
  145.                                 {
  146.                                         strcpy(new_tabela.password,word);
  147.                                         strcpy(new_tabela.username,tabela[i+(check*size)].username);
  148.                                         nsucessos++;
  149.                                         //printf("[%d][%d][%3d][%3d]username:%s_password:%s\n",check,getpid(),j+(check*size),nsucessos,tabela[j+(check*size)].username,word);
  150.                                         write(channel[1], &new_tabela, sizeof(New_User));
  151.                                 }
  152.                 }
  153.                 //CheckCrypt (word, size, tabela, check);
  154.                 //if (nsucessos == size)                kill(getpid(), SIGKILL);
  155.                 BruteForce(word, counter+1, size, tabela, check);
  156.     }
  157.     return 0;
  158. }
  159.  
  160. /*int CheckCrypt(char *word, int size, User *tabela, int check)
  161. {
  162.         New_User new_tabela;
  163.         int x=0;
  164.         int result,i;
  165.         char password[10];
  166.         strcpy(password,word);
  167.         word = crypt(word,"10");
  168.  
  169.         for (i=0;i<size;i++){
  170.                 ntentativas++;
  171.                 result = strcmp(word, tabela[i+(check*size)].password);
  172.  
  173.                 if (result == 0){
  174.                     strcpy(new_tabela.password,password);
  175.                     strcpy(new_tabela.username,tabela[i+(check*size)].username);
  176.                         nsucessos++;
  177.  
  178.                         printf("[%d][%d][%3d][%3d]username:%s_password:%s\n",check,getpid(),i+(check*size),nsucessos,tabela[i+(check*size)].username,password);
  179.  
  180.                         write(channel[1], &new_tabela, sizeof(New_User));
  181. //                      check++;
  182.                 }
  183.         }
  184. //      check++;
  185.         return 0;
  186. }*/
Add Comment
Please, Sign In to add comment