-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreads.c
More file actions
139 lines (125 loc) · 3.47 KB
/
Copy paththreads.c
File metadata and controls
139 lines (125 loc) · 3.47 KB
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
#include <stdint.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/syscall.h>
#include "threads.h"
#include "utils.h"
static int onexit = 0;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static char password[256] = { 0 };
static char *error_output = NULL;
static char *stegfile = NULL;
static time_t t_start = -1;
static time_t t_end = -1;
static bool verbose = false;
/********** UTILS **********/
static void*
thread_func(void *args)
{
char **params = (char**)args;
char *begin = params[0];
char *end = params[1];
uintptr_t success = 0;
long id = syscall(__NR_gettid);
if (verbose)
{
pthread_mutex_lock(&mutex);
printf(CYAN("[THREAD %ld]")" working from %p to %p\n", id, begin, end);
pthread_mutex_unlock(&mutex);
}
// TODO
char *currentPtr = begin;
char current[256] = { 0 };
char buffer[256] = { 0 };
char *tmp;
int nb_tries = 0;
while (currentPtr < end && !onexit)
{
memset(current, 0, sizeof current);
tmp = currentPtr;
while (*tmp != '\n' && *(tmp++) != '\0');
assert((size_t) (tmp-currentPtr) < sizeof current);
strncpy(current, currentPtr, tmp-currentPtr);
current[tmp-currentPtr] = '\0';
currentPtr = tmp+1;
//if (verbose)
// printf(CYAN("[THREAD %ld] (%d)")" trying \"%s\"\n", id, nb_tries, current);
exec_steghide(stegfile, current, buffer, sizeof buffer);
trim(buffer);
if (strcmp(buffer, error_output) != 0)
{
t_end = time(NULL);
success = 1;
onexit = 1;
snprintf(password, sizeof password, "%s", current);
}
nb_tries++;
}
return (void*) success;
}
/********** API **********/
static char ***params;
pthread_t*
sb_run_threads(uint nb_threads, char *wl, char *error, char *filename, bool verbosity)
{
verbose = verbosity;
pthread_t *ths = mem_alloc(sizeof *ths * nb_threads);
error_output = strdup(error);
stegfile = strdup(filename);
char **pointers = segment_string(wl, nb_threads);
params = mem_alloc(sizeof *params * nb_threads);
for (uint i=0; i<nb_threads; i++)
{
params[i] = mem_alloc (sizeof *params[i] * 2);
params[i][0] = pointers[i];
if (i == nb_threads-1)
params[i][1] = wl+strlen(wl);
else
params[i][1] = pointers[i+1];
}
if (verbose)
{
printf(CYAN("Info: ")"Wordlist is from %p to %p (%lu bytes)\n", wl, wl+strlen(wl), strlen(wl));
printf(CYAN("Info: ")"Starting to bruteforce with %d threads !\n", nb_threads);
}
t_start = time(NULL);
for (uint i=0; i<nb_threads ; i++)
{
pthread_create(ths+i, NULL, thread_func, (void*)params[i]);
}
free(pointers);
return ths;
}
time_t
sb_get_exec_time()
{
if (t_start < 0 || t_end < 0)
return -1;
return t_end-t_start;
}
bool
sb_join_threads(pthread_t *threads, uint nb_threads, char *s, size_t sizeof_s)
{
bool ret = false;
uintptr_t success = 0;
for (uint i=0; i<nb_threads ; i++)
{
pthread_join(threads[i], (void*)&success);
if (success == 1)
{
ret = true;
assert(strlen(password) <= sizeof_s);
snprintf(s, sizeof_s, "%s", password);
}
}
for (uint i=0; i<nb_threads ; i++)
{
free(params[i]);
}
free(params);
free(threads);
return ret;
}