Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _GNU_SOURCE
- #include <crypt.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <sys/mman.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <string.h>
- size_t getFileSize(const char* filename)
- {
- struct stat st;
- stat(filename, &st);
- return st.st_size;
- }
- typedef struct args
- {
- int start;
- int partSize;
- };
- char *map;
- int isSolved = 0;
- char *password;
- char *salt;
- struct crypt_data data[1] = {0};
- size_t size;
- pthread_mutex_t smutex = PTHREAD_MUTEX_INITIALIZER;
- int pos = 0;
- void *findPassword(void *arguments)
- {
- // odczytujemy parametry startowe wątku
- int start = ((struct args*)arguments)->start;
- int partSize=((struct args*)arguments)->partSize;
- int i=start;
- while(i<start+partSize)
- {
- // sprawdzamy czy jakiś inny wątek nie znalazł rozwiązania
- pthread_mutex_lock(&smutex);
- if(isSolved == 1)
- {
- pthread_mutex_unlock(&smutex);
- break;
- }
- pthread_mutex_unlock(&smutex);
- // weryfikujemy kolejne rozwiązanie w naszym obszarze pamięci
- int j = 0;
- char *tempPassword = (char*)malloc(100);
- while(map[i]!=10)
- {
- tempPassword[j++] = map[i++];
- }
- i++;
- tempPassword[j] = '\0';
- pthread_mutex_lock(&smutex);
- char *test = crypt_r(tempPassword, salt, data);
- if(strcmp(test,password)==0)
- {
- printf("\nHaslo to %s\n",tempPassword);
- isSolved = 1;
- free(tempPassword);
- pthread_mutex_unlock(&smutex);
- break;
- }
- pthread_mutex_unlock(&smutex);
- free(tempPassword);
- //aktualizujemy progress
- pthread_mutex_lock(&smutex);
- pos+=j+1;
- printf("%.2f%\r",((float)pos/size*100));
- pthread_mutex_unlock(&smutex);
- }
- return NULL;
- }
- int main(int argc, char **argv)
- {
- // liczba wątków jest odczytywana z parametru i porównywana
- // z liczbą procesorów w systemie, wybierana mniejsza wartość
- int numberOfThreads;
- int processors = sysconf(_SC_NPROCESSORS_ONLN);
- if(atoi(argv[3]) < processors)
- {
- numberOfThreads = atoi(argv[3]);
- }
- else
- {
- numberOfThreads = processors;
- }
- //tworzymy obiekty wątków w liczbie ustalonej powyżej
- pthread_t *th = (pthread_t*)malloc(numberOfThreads*sizeof(pthread_t));
- struct args* partArg = (struct args*)malloc(numberOfThreads*sizeof(struct args));
- // TODO
- int used = 0;
- password = argv[1];
- salt = (char*)malloc(50);
- salt[0] = '$';
- salt[1] = argv[1][1];
- salt[2] = '$';
- int i = 3;
- while(argv[1][i]!='$')
- {
- salt[i]=argv[1][i++];
- }
- salt[i]='\0';
- //otwieramy plik z hasłami
- size = getFileSize(argv[2]);
- int fd = open(argv[2], O_RDONLY);
- if(fd<0)
- {
- printf("Blad\n");
- return -1;
- }
- map = mmap(0, size, PROT_READ, MAP_PRIVATE, fd, 0);
- // dzielimy plik na części dla poszczególnych wątków
- for(i=0; i<numberOfThreads; i++)
- {
- partArg[i].start=used;
- if(i==numberOfThreads-1)
- {
- partArg[i].partSize = size-used;
- break;
- }
- partArg[i].partSize = size/numberOfThreads;
- while(map[used+partArg[i].partSize]!=10 && map[used+partArg[i].partSize]!='\0')
- {
- partArg[i].partSize++;
- }
- used += partArg[i].partSize;
- }
- // odpalamy wątki, każdy wykonuje funkcję findPassword
- for(i=0; i<numberOfThreads; i++)
- {
- pthread_create(&th[i], NULL, findPassword, (void*)&partArg[i]);
- }
- // czekamy aż wątki skończą pracę
- for(i=0; i<numberOfThreads; i++) pthread_join(th[i], NULL);
- // zwalniamy plik
- munmap(map, size);
- if(!isSolved) printf("Nie udalo sie odnalezc hasla\n");
- free(salt);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement