/
brute_force.c
352 lines (311 loc) · 10.3 KB
/
brute_force.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
* libattkthread - A threaded attack library template.
*
* Copyright (c) 2008-2013, Adam Bregenzer <adam@bregenzer.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation. See COPYING for more
* details.
*
* libattkthread is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#define _GNU_SOURCE
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "brute_force.h"
/** @defgroup brute_force brute_force
*
* Generate a series of records based on the supplied alphabet.
*
* Brute force implements the file interface for libattkthread.
* It generates a series of records starting with the start
* buffer, stopping when it reaches the end buffer, and using
* the alphabet string as the sequence of characters to
* process.
*/
inline char *char_index(char *haystack, size_t hay_size, char *needle) {
return (char *)memchr(haystack, (int)*needle, hay_size);
}
inline char *alp_index(file_st *file, char *needle) {
brute_force_data_st *bf_st = file->file_data;
return (char *)memchr(bf_st->alphabet, (int)*needle,
strlen(bf_st->alphabet));
}
/** Initializes a read file structure.
* Clears a new read file structure, copies the file path, sets the record
* size, and sets up its thread mutex.
*
* @param[in] file The file structure.
* @param[in] records_per_block The number of records per block.
* @param[in] start Start string.
* @param[in] end End string.
* @param[in] alphabet Alphabet string to use.
*/
int brute_force_init(file_st *file, int records_per_block, char *start,
char *end, char *alphabet) {
brute_force_data_st *bf_st;
size_t start_len;
size_t end_len;
size_t alp_len;
int i;
start_len = strlen(start);
end_len = strlen(end);
alp_len = strlen(alphabet);
/* Start can not be longer than end */
if (start_len > end_len) {
errno = EINVAL;
return E_ATTK_SYSTEM;
}
/* Every character in start must be in the alphabet */
for (i = 0; i < start_len; i++) {
if (char_index(alphabet, alp_len, start + i) == NULL) {
errno = EINVAL;
return E_ATTK_SYSTEM;
}
}
/* Every character in end must be in the alphabet */
for (i = 0; i < end_len; i++) {
if (char_index(alphabet, alp_len, end + i) == NULL) {
errno = EINVAL;
return E_ATTK_SYSTEM;
}
}
/* If they are the same size, start can not be greater than end */
if (start_len == end_len) {
for (i = 0; i < start_len; i++) {
if (char_index(alphabet, alp_len, start + i)
> char_index(alphabet, alp_len, end + i)) {
errno = EINVAL;
return E_ATTK_SYSTEM;
}
if (char_index(alphabet, alp_len, start + i)
< char_index(alphabet, alp_len, end + i)) {
break;
}
}
}
/* Clear the structure */
memset(file, 0, sizeof(file_st));
/* Set defaults */
file->record_size = strlen(end) + 1;
file->records_per_block = records_per_block;
/* Initialize pthread objects */
pthread_mutex_init(&(file->mut), NULL);
/* Create brute_force_data_st */
bf_st = malloc(sizeof(brute_force_data_st));
file->file_data = bf_st;
/* Create buffers */
bf_st->start = malloc(strlen(start) + 1);
bf_st->end = malloc(strlen(end) + 1);
bf_st->alphabet = malloc(strlen(alphabet) + 1);
bf_st->last = malloc(strlen(end) + 1);
memset(bf_st->start, 0, strlen(start) + 1);
memset(bf_st->end, 0, strlen(end) + 1);
memset(bf_st->alphabet, 0, strlen(alphabet) + 1);
memset(bf_st->last, 0, strlen(end) + 1);
/* Fill buffers */
memcpy(bf_st->start, start, strlen(start));
memcpy(bf_st->end, end, strlen(end));
memcpy(bf_st->alphabet, alphabet, strlen(alphabet));
/* Setup class methods */
file->open_file = bf_open_file;
file->next_block = bf_next_block;
file->free_block = bf_free_block;
file->close_file = bf_close_file;
return 0;
}
/** Destroys a brute force structure.
* Clears a brute force structure and destroys its thread mutex. Also clears
* all private data.
*
* @param[in] file The file to destroy.
*/
void brute_force_destroy(file_st *file) {
brute_force_data_st *bf_st = file->file_data;
/* Destroy pthread objects */
pthread_mutex_destroy(&(file->mut));
/* Destroy brute_force_data_st */
free(bf_st->start);
free(bf_st->end);
free(bf_st->alphabet);
free(bf_st->last);
free(bf_st);
}
/** Start the generator.
* Calculates the number of words that will be generated.
*
* @param[in] file The file structure.
*
* @return Returns 0 on success, otherwise an error code.
*/
int bf_open_file(file_st *file) {
brute_force_data_st *bf_st = file->file_data;
char *curr_p;
char *start_end_p;
char *end_end_p;
char *alp_end_p;
size_t start_len;
size_t end_len;
size_t alp_len;
unsigned long temp;
int i, j;
/* Calculate string lengths */
start_len = strlen(bf_st->start);
end_len = strlen(bf_st->end);
alp_len = strlen(bf_st->alphabet);
/* Calculate pointers to the ends of the strings */
start_end_p = bf_st->start + start_len - 1;
end_end_p = bf_st->end + end_len - 1;
alp_end_p = bf_st->alphabet + alp_len - 1;
/* Calculate the total number of records */
file->total_records = 1;
/* Calculate number of records to finish start string */
curr_p = start_end_p;
while (curr_p >= bf_st->start) {
temp = alp_end_p - alp_index(file, curr_p);
for (i = 0; i < start_end_p - curr_p; i++) {
temp *= alp_len;
}
file->total_records += temp;
curr_p--;
}
/* Calculate number of records to reach end's length */
for (i = 0; i < (end_len - start_len); i++) {
temp = alp_len;
for (j = 0; j < (start_len + i); j++) {
temp *= alp_len;
}
file->total_records += temp;
}
/* Remove possibilities that will not be generated */
curr_p = end_end_p;
while (curr_p >= bf_st->end) {
temp = alp_end_p - alp_index(file, curr_p);
for (i = 0; i < (end_end_p - curr_p); i++) {
temp *= alp_len;
}
file->total_records -= temp;
curr_p--;
}
return 0;
}
/** Read in a block and return it.
* Generates the next block of records and allocates it into a buffer.
*
* @param[in] file The file structure.
* @param[out] buf The block to return.
* @param[in] buf_size The size of the block, 0 if block is not allocated.
*
* @return Returns the number of bytes, otherwise an error code.
*/
ssize_t bf_next_block(file_st *file, char **buf, size_t buf_size) {
brute_force_data_st *bf_st = file->file_data;
char *buffer;
char *buf_p;
char *start_end_p;
char *end_end_p;
char *alp_end_p;
char *last_end_p;
char *curr_p;
size_t start_len;
size_t end_len;
size_t alp_len;
/* Calculate string lengths */
start_len = strlen(bf_st->start);
end_len = strlen(bf_st->end);
alp_len = strlen(bf_st->alphabet);
/* Calculate pointers to the ends of the strings */
start_end_p = bf_st->start + start_len - 1;
end_end_p = bf_st->end + end_len - 1;
alp_end_p = bf_st->alphabet + alp_len - 1;
/* Calculate buffer size */
if (buf_size == 0) {
/* Calculate buffer size */
buf_size = file->record_size * file->records_per_block;
/* Allocate buffer */
buffer = malloc(buf_size);
memset(buffer, 0, buf_size);
*buf = buffer;
} else {
buf_size = (buf_size / file->record_size) * file->record_size;
buffer = *buf;
}
buf_p = buffer;
/* First run? */
if (strlen(bf_st->last) == 0) {
memcpy(bf_st->last, bf_st->start, strlen(bf_st->start));
memcpy(buffer, bf_st->last, strlen(bf_st->last));
buf_p += file->record_size;
}
/* Generate more records */
last_end_p = bf_st->last + strlen(bf_st->last) - 1;
while (buf_p < (buffer + buf_size)) {
if (memcmp(bf_st->last, bf_st->end, end_len) == 0) {
/* All records generated */
break;
}
/* Find the first char to increase */
curr_p = last_end_p;
while (memcmp(curr_p, alp_end_p, 1) == 0) {
if (curr_p < bf_st->last) {
break;
}
curr_p--;
}
if (curr_p < bf_st->last) {
/* We need to make the string longer */
if (strlen(bf_st->last) < end_len) {
memset(bf_st->last, (int)*bf_st->alphabet,
strlen(bf_st->last) + 1);
last_end_p++;
} else {
/* All records generated */
break;
}
} else {
/* Increase char to next in alphabet */
memcpy(curr_p, alp_index(file, curr_p) + 1, 1);
curr_p++;
/* Reset any following characters */
while (curr_p <= last_end_p) {
memcpy(curr_p, bf_st->alphabet, 1);
curr_p++;
}
}
/* Add this word to the buffer */
memcpy(buf_p, bf_st->last, strlen(bf_st->last));
buf_p += file->record_size;
}
return buf_p - buffer;
}
/** Free a block.
* Frees a block that was previously read.
*
* @param[in] file The file structure.
* @param[in] buf The block to free.
* @param[in] buf_len The size of the block.
*
* @return Returns 0 on success, otherwise an error code.
*/
int bf_free_block(file_st *file, char *buf, size_t buf_len) {
/* Free the buffer */
free(buf);
return 0;
}
/** Close the file.
* Does nothing for brute force.
*
* @param[in] file The file structure.
*
* @return Returns 0 on success, otherwise an error code.
*/
int bf_close_file(file_st *file) {
/* Nothing to do */
return 0;
}