-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexploit-omp.c
96 lines (77 loc) · 2.02 KB
/
exploit-omp.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <omp.h>
#include <math.h>
#define PASSWORD_LEN 8
int found = 0;
int num_threads = 1;
int total_tries = 0;
int main(int argc, char *argv[])
{
if (argc > 1)
{
num_threads = atoi(argv[1]);
}
struct timeval start_time, end_time;
gettimeofday(&start_time, NULL);
// Set the number of threads
omp_set_num_threads(num_threads);
// The total number of possible passwords
int total = pow(10, PASSWORD_LEN);
// Parallelize the password cracking with OpenMP
#pragma omp parallel for shared(found) reduction(+ : total_tries)
for (int i = 0; i < total; i++)
{
if (found)
{
continue;
}
char password[7];
sprintf(password, "%05d", i);
pid_t pid = fork();
if (pid == -1)
{
perror("fork");
continue;
}
else if (pid == 0)
{
char *args[] = {"./prog", password, NULL};
execv(args[0], args);
perror("execv");
exit(1);
}
else
{
int status;
waitpid(pid, &status, 0);
if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
{
#pragma omp critical
{
if (!found)
{
printf("Password found: %s\n", password);
found = 1;
}
}
}
}
total_tries++;
}
gettimeofday(&end_time, NULL);
if (!found)
{
printf("Password not found.\n");
}
double elapsed_time = (end_time.tv_sec - start_time.tv_sec) + (end_time.tv_usec - start_time.tv_usec) / 1e6;
double tries_per_second = total_tries / elapsed_time;
printf("Time taken: %.6f seconds\n", elapsed_time);
printf("Estimated tries per second: %.6f\n", tries_per_second);
return 0;
}