Olá, estou fazendo um trabalho para implementar um código de força bruta utilizando pthereads no code blocks no Linux, só que na hora de compilar está dando erro:
"||error: ld returned 1 exit status|"
Alguém pode me dar uma luz do que fazer ? Será que errei algo na implementação ou nas bibliotecas carregadas do open ssl ? Encontrei muito pouca referência para apoio sobre o assunto na internet.
Objetivo dado: implementar, usando threads (por exemplo, a biblioteca pthreads), um programa paralelo para tentar quebrar senhas usando a técnica simples de força-bruta (que testar “todas” as possibilidades). Além disso, os resultados de tempo devem ser comparados usando alguma função de captura do tempo de execução, executada possivelmente em uma máquina multiprocessada (2, 4 núcleos).
Arquivo original dado:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/md5.h>
#include "timer.h"
#define MAX 100
#define MIN_ASCII_PRINTBLE 33
#define MAX_ASCII_PRINTBLE 127
// Função que gera strings aleatórias com caracteres printáveis.
int gen_random_string(char string[MAX], int s){
int i, num_char;
char c;
for(i=0; i<s; i++){
// Calcula a quantidade de caracteres
num_char = MAX_ASCII_PRINTBLE-MIN_ASCII_PRINTBLE;
// Limita o número aleatorio gerado para que fique
// entre MIN_ASCII_PRINTBLE e MAX_ASCII_PRINTBLE
c = (char)((rand() % num_char) + MIN_ASCII_PRINTBLE);
// Monta a string aleatória
string[i] = c;
}
// Colocar o terminador
string[s] = 0;
}
int main(int argc, char* argv[])
{
const char password[] = "123"; // Apenas para testes
char string[MAX]; // String gerada
unsigned char hash1[MD5_DIGEST_LENGTH]; // hash de password
unsigned char hash2[MD5_DIGEST_LENGTH]; // hash de string
int i, j;
int cont = 0; // contador do numero de colisões
unsigned long int N; // Número de repetições
int tamanhoMinPalavra; // Tamanho mínimo de string a ser testada
int tamanhoMaxPalavra; // Tamanho máximo de string a ser testada
double tempo_ini, tempo_fim;
printf("Entre com o número de repetições: ");
scanf("%lu", &N);
printf("Entre com o tamanho mínimo da string: ");
scanf("%d", &tamanhoMinPalavra);
printf("Entre com o tamanho máximo da string: ");
scanf("%d", &tamanhoMaxPalavra);
// Tempo inicial
tempo_ini = GET_TIME();
// Usa o tempo como semente dos números aleatórios
srand(time(NULL));
// Gera o MD5 da senha de teste.
// O ideal é passar como entrada o hash já gerado.
MD5(password, sizeof(password), hash1);
// Controle dos tamanhos das strings
for (j=tamanhoMinPalavra; j<=tamanhoMaxPalavra; j++){
// Controle das repetições
for (i=0; i<N; i++){
// Gera string aleatória
gen_random_string(string, j);
// Gera o MD5 da string para comparar com o hash da
// senha alvo.
MD5(string, j+1, hash2);
// Compara os hashes
if(strcmp(hash1, hash2) == 0){
printf("Achou: %s\n", string);
cont++;
}
}
}
tempo_fim = GET_TIME();
// Colisões
printf("Colisoes: %d (tempo: %f)\n", cont, tempo_fim - tempo_ini);
return 0;
}
Implementação com pthreads que fiz até agora:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/md5.h>
#include "timer.h"
#include <pthread.h>
#define MAX 100
#define MIN_ASCII_PRINTBLE 33
#define MAX_ASCII_PRINTBLE 127
#define NTHREADS 10
int cont = 0; // contador do numero de colisões
unsigned long int N; // Número de repetições
int tamanhoMinPalavra; // Tamanho mínimo de string a ser testada
int tamanhoMaxPalavra; // Tamanho máximo de string a ser testada
const char password[] = "123"; // Apenas para testes
char string[MAX]; // String gerada
unsigned char hash1[MD5_DIGEST_LENGTH]; // hash de password
unsigned char hash2[MD5_DIGEST_LENGTH]; // hash de string
// Função que gera strings aleatórias com caracteres printáveis.
int gen_random_string(char string[MAX], int s){
int i, num_char;
char c;
for(i=0; i<s; i++){
// Calcula a quantidade de caracteres
num_char = MAX_ASCII_PRINTBLE-MIN_ASCII_PRINTBLE;
// Limita o número aleatorio gerado para que fique
// entre MIN_ASCII_PRINTBLE e MAX_ASCII_PRINTBLE
c = (char)((rand() % num_char) + MIN_ASCII_PRINTBLE);
// Monta a string aleatória
string[i] = c;
}
// Colocar o terminador
string[s] = 0;
}
void *PrintHello (void *arg)
{ int i,j;
// Gera o MD5 da senha de teste.
// O ideal é passar como entrada o hash já gerado.
MD5(password, sizeof(password), hash1);
// Controle dos tamanhos das strings
for (j=tamanhoMinPalavra; j<=tamanhoMaxPalavra; j++){
// Controle das repetições
for (i=0; i<N; i++){
// Gera string aleatória
gen_random_string(string, j);
// Gera o MD5 da string para comparar com o hash da
// senha alvo.
MD5(string, j+1, hash2);
// Compara os hashes
if(strcmp(hash1, hash2) == 0){
printf("Achou: %s\n", string);
cont++;
}
}
}
pthread_exit(NULL);
}
int main(int argc, char* argv[])
{
pthread_t tid_sistema[NTHREADS];
int i;
int arg;
double tempo_ini, tempo_fim;
printf("Entre com o número de repetições: ");
scanf("%lu", &N);
printf("Entre com o tamanho mínimo da string: ");
scanf("%d", &tamanhoMinPalavra);
printf("Entre com o tamanho máximo da string: ");
scanf("%d", &tamanhoMaxPalavra);
// Tempo inicial
tempo_ini = GET_TIME();
// Usa o tempo como semente dos números aleatórios
srand(time(NULL));
for(i=0; i<NTHREADS; i++){
printf("Cria a thread %d\n", i);
arg = i;
if(pthread_create(&tid_sistema[i], NULL, PrintHello, (void*) &arg)) {
printf("Erro: pthread_create()");
exit(-1);
}
}
for(i=0; i<NTHREADS; i++){
printf("teste %d\n", i);
if(pthread_join(tid_sistema[i], NULL)) {
printf("Erro: pthread_join()");
exit(-1);
}
}
tempo_fim = GET_TIME();
// Colisões
printf("Colisoes: %d (tempo: %f)\n", cont, tempo_fim - tempo_ini);
return 0;
}