Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <sys/wait.h>
- #include <sys/types.h>
- #include <string.h>
- #include <crypt.h>
- #define max_pass_size 3
- #define students_pass_file "passwords.txt"
- #define max_forks 4
- typedef struct{
- char username[35];
- char password[15];
- } User;
- typedef struct{
- char username[35];
- char password[15];
- } New_User;
- static char *Alfabeth = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
- int nsucessos=0,ntentativas=0;
- void worker();
- void master();
- int n_pass();
- void carrega_tabela();
- int BruteForce();
- int CheckCrypt();
- // File descriptors for the pipe channel
- int channel[2];
- int main() {
- int size,i=0;
- New_User new_tabela;
- // Create a pipe
- pipe(channel);
- char word[max_pass_size+1] = "";
- size=n_pass();
- User tabela[size];
- carrega_tabela(tabela,size);
- //for (i=0;i<size;i++) printf("%s %s\n",tabela[i].username,tabela[i].password);
- // Create the processes
- pid_t id;
- while(i<max_forks){
- id = fork();
- if (id == 0) {
- close(channel[0]);
- //sleep(1);
- //worker(tabela, size, i);
- BruteForce(word, 0, size/max_forks, tabela, i);
- i=max_forks;
- //exit(0);
- }
- i++;
- }
- if (id > 0)
- {
- while(1){
- read(channel[0], &new_tabela, sizeof(New_User));
- printf("username:%s_password:%s\n", new_tabela.username, new_tabela.password);
- //master(tabela);
- }
- }
- return 0;
- }
- int n_pass(){
- int size=0;
- char temp[50];
- FILE *ficheiro=fopen(students_pass_file,"r");
- while ((fscanf(ficheiro,"%s",temp) != -1 )) size++;
- fclose(ficheiro);
- return size;
- }
- void carrega_tabela(User *tabela, int size){
- int i=0;
- char temp[50];
- char *result;
- FILE *ficheiro=fopen(students_pass_file,"r");
- while ((fscanf(ficheiro,"%s",temp) != -1 )){
- result=strtok(temp,":");
- strcpy(tabela[i].username,result);
- result=strtok(NULL,"\n");
- strcpy(tabela[i].password,result);
- i++;
- }
- fclose(ficheiro);
- }
- /*void worker(User *tabela, int size, int proc_n) {
- int i, test;
- test=size/max_forks;
- char word[10];
- close(channel[0]);
- while (1) {
- for (i = 1; i < max_pass_size+1; i++){
- BruteForce(word, 0, i, test, tabela, proc_n);
- }
- }
- return;
- }*/
- void master(User *tabela) {
- New_User new_tabela;
- close(channel[1]);
- fd_set read_set;
- FD_ZERO(&read_set);
- FD_SET(channel[0], &read_set);
- if ( select(channel[0]+1, &read_set, NULL, NULL, NULL) > 0 ) {
- if (FD_ISSET(channel[0], &read_set))
- read(channel[0], &new_tabela, sizeof(New_User));
- printf("username:%s_password:%s\n", new_tabela.username, new_tabela.password);
- return;
- }
- //sleep(1);
- }
- int BruteForce(char *word, int counter, int size, User *tabela, int check){
- New_User new_tabela;
- int i,j,result;
- char aux[15];
- if (counter >= max_pass_size) return 0;
- for (i = 0; i < strlen(Alfabeth); i++)
- {
- word[counter] = Alfabeth[i];
- word[counter+1] = '\0';
- strcpy(aux,crypt(word,"10"));
- for (j=0;j<size;j++){
- ntentativas++;
- result = strcmp(aux, tabela[j+(check*size)].password);
- if (result == 0)
- {
- strcpy(new_tabela.password,word);
- strcpy(new_tabela.username,tabela[i+(check*size)].username);
- nsucessos++;
- //printf("[%d][%d][%3d][%3d]username:%s_password:%s\n",check,getpid(),j+(check*size),nsucessos,tabela[j+(check*size)].username,word);
- write(channel[1], &new_tabela, sizeof(New_User));
- }
- }
- //CheckCrypt (word, size, tabela, check);
- //if (nsucessos == size) kill(getpid(), SIGKILL);
- BruteForce(word, counter+1, size, tabela, check);
- }
- return 0;
- }
- /*int CheckCrypt(char *word, int size, User *tabela, int check)
- {
- New_User new_tabela;
- int x=0;
- int result,i;
- char password[10];
- strcpy(password,word);
- word = crypt(word,"10");
- for (i=0;i<size;i++){
- ntentativas++;
- result = strcmp(word, tabela[i+(check*size)].password);
- if (result == 0){
- strcpy(new_tabela.password,password);
- strcpy(new_tabela.username,tabela[i+(check*size)].username);
- nsucessos++;
- printf("[%d][%d][%3d][%3d]username:%s_password:%s\n",check,getpid(),i+(check*size),nsucessos,tabela[i+(check*size)].username,password);
- write(channel[1], &new_tabela, sizeof(New_User));
- // check++;
- }
- }
- // check++;
- return 0;
- }*/
Add Comment
Please, Sign In to add comment