-
Notifications
You must be signed in to change notification settings - Fork 0
/
crack-passwd.c
155 lines (116 loc) · 3.53 KB
/
crack-passwd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#define _GNU_SOURCE
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <crypt.h>
#include <stdlib.h>
#include <semaphore.h>
#define TAM_HASH 256
#define TAM_SENHA 4
#define N_ITENS 20
/*buffer para armazenar as senhas candidatas*/
char buffer[N_ITENS][TAM_SENHA+1];
/*argumento do main*/
char *argumento;
/*-----------------variaveis semaphore-----------------*/
int inicio = 0, final = 0;
sem_t bloqueio, pos_vazia, pos_ocupada;
/* Obtém o hash a partir de uma senha e coloca o resultado em hash.
O vetor hash deve ter pelo menos 14 elementos. */
void calcula_hash_senha(const char *senha, char *hash);
void incrementa_senha(char *senha);
void testa_senha(const char *hash_alvo, const char *senha);
void* produtor(void *v);
void* consumidor(void *v);
/*--Main------------------------------------------------------------------------*/
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Uso: %s <hash>", argv[0]);
return 1;
}
argumento = argv[1];
// int i;
pthread_t thr_produtor, thr_consumidor;
sem_init(&bloqueio, 0, 1);
sem_init(&pos_vazia, 0, N_ITENS);
sem_init(&pos_ocupada, 0, 0);
/* Limpa o buffer */
/// for (i = 0; i < N_ITENS; i++)
//// buffer[i][TAM_SENHA+1] = '\0';
/*cria um produtor e dois consumidores...*/
pthread_create(&thr_produtor, NULL, produtor, NULL);
pthread_create(&thr_consumidor, NULL, consumidor, NULL);
pthread_create(&thr_consumidor, NULL, consumidor, NULL);
pthread_join(thr_produtor, NULL);
pthread_join(thr_consumidor, NULL);
return 0;
}
/*--Testa senha-----------------------------------------------------------------*/
void testa_senha(const char *hash_alvo, const char *senha) {
char hash_calculado[TAM_HASH + 1];
calcula_hash_senha(senha, hash_calculado);
if (!strcmp(hash_alvo, hash_calculado)) {
printf("Achou! %s\n", senha);
exit(0);
}
}
/*--Incrementa senha------------------------------------------------------------*/
void incrementa_senha(char *senha) {
int i;
i = TAM_SENHA - 1;
while (i >= 0) {
if (senha[i] != 'z') {
senha[i]++;
i = -2;
} else {
senha[i] = 'a';
i--;
}
}
if (i == -1) {
printf("Não achou!\n");
exit(1);
}
}
/*--Calcula hash senha----------------------------------------------------------*/
void calcula_hash_senha(const char *senha, char *hash) {
struct crypt_data data;
data.initialized = 0;
strcpy(hash, crypt_r(senha, "aa", &data));
}
/*--PRODUTOR--------------------------------------------------------------------*/
void* produtor(void *v) {
int i;
char senha[TAM_SENHA+1];
for (i = 0; i < TAM_SENHA; i++) {
senha[i] = 'a';
}
senha[TAM_SENHA] = '\0';
while(1){
final = (final + 1) % N_ITENS;
sem_wait(&pos_vazia);
sem_wait(&bloqueio);
strcpy(buffer[final],senha);
sem_post(&bloqueio);
sem_post(&pos_ocupada);
incrementa_senha(senha);
}
printf("Produção encerrada.\n");
return NULL;
}
/*--CONSUMIDOR------------------------------------------------------------------*/
void* consumidor(void *v) {
char hash[TAM_SENHA];
while(1) {
inicio = (inicio + 1) % N_ITENS;
sem_wait(&pos_ocupada);
sem_wait(&bloqueio);
strcpy(hash,buffer[inicio]);
sem_post(&bloqueio);
sem_post(&pos_vazia);
testa_senha(argumento,hash); /* Item é consumido */
}
printf("Consumo encerrado.\n");
return NULL;
}