jako93

Parallel SHA-512 Cracker

Nov 20th, 2015
584
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. *   Parallel SHA-512 Password Cracker with OpenMP
  3. *   Compile:    gcc -fopenmp -lcrypt -std=c99 -o parshacrk parshacrk.c
  4. *   Written by: JaKo
  5. *   Date: 16/11/2015
  6. */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <crypt.h>
  10. #include <stdlib.h>
  11. #include <omp.h>
  12.  
  13. void chop(char *word){
  14.   int lenword=strlen(word);
  15.   if(word[lenword-1] == '\n')   word[lenword-1] = '\0';
  16. }
  17.  
  18. int numlines(FILE *file){
  19.     if(file==NULL)  return -1;
  20.     char ch;
  21.     int lines = 0;
  22.     while (ch != EOF){
  23.         ch = fgetc(file);
  24.             if(ch == '\n')  lines++;   
  25.     }
  26.     return lines;
  27. }
  28.  
  29. void main(int argc, char *argv[]){
  30.   //Parameters error handling
  31.   if(argc!=4){
  32.     printf("Parallel SHA-512 Password Cracker\nUSAGE: ./parshacrk <PATH TO DICTIONARY> '$6$<SALT>$' '$6$<SALT>$<SHA-512 HASH>'\n");
  33.     exit(-1);
  34.   }
  35.   int found=0;  //0 = password not found ; 1 = password found
  36.   char word[BUFSIZ],salt[BUFSIZ], pwhash[BUFSIZ];
  37.   FILE *words = fopen(argv[1],"r"); //Open dictionary file
  38.   //File error handling
  39.   if(words==NULL){
  40.     printf("Cannot open dictionary file.\n");  
  41.     exit(-1);  
  42.   }
  43.   strcpy(salt,argv[2]);
  44.   strcpy(pwhash,argv[3]);
  45.   int size = numlines(words); // Number of words in dictionary
  46.   if(fseek(words,0,SEEK_SET)==-1)   exit(-1);   //seek error handling
  47.   //Parallel Region
  48.   #pragma omp parallel for private(word) shared(found) schedule(dynamic)
  49.   for(int i=0;i<size;i++){
  50.     if(fgets(word,BUFSIZ,words) != NULL){
  51.         chop(word);
  52.         if(found==1)    exit(1);
  53.         printf("[*] THREAD %i TRYING: %s\n",omp_get_thread_num(),word);
  54.         char *hash = (char*)crypt(word,salt);
  55.         if(strcmp(hash,pwhash) == 0){
  56.             printf("[+] PASSWORD FOUND: %s\n",word);
  57.             found=1;
  58.         }
  59.     }
  60.   }
  61.   fclose(words);
  62.   if(found==0)  printf("[-] PASSWORD NOT FOUND, EXITING...\n");
  63.   exit(0);
  64. }
RAW Paste Data