This repository has been archived by the owner on Oct 3, 2024. It is now read-only.
forked from vanhauser-thc/thc-hydra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfg.c
228 lines (198 loc) · 6.4 KB
/
bfg.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
/* code original by Jan Dlabal <[email protected]>, partially rewritten by vh */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#ifdef __sun
#include <sys/int_types.h>
#elif defined(__FreeBSD__) || defined(__IBMCPP__) || defined(_AIX)
#include <inttypes.h>
#else
#include <stdint.h>
#endif
#include "bfg.h"
bf_option bf_options;
#ifdef HAVE_MATH_H
extern int32_t debug;
static int32_t add_single_char(char ch, char flags, int32_t* crs_len) {
if ((ch >= '2' && ch <= '9') || ch == '0') {
if ((flags & BF_NUMS) > 0) {
printf("[ERROR] character %c defined in -x although the whole number range was already defined by '1', ignored\n", ch);
return 0;
}
//printf("[WARNING] adding character %c for -x, note that '1' will add all numbers from 0-9\n", ch);
}
if (tolower((int32_t) ch) >= 'b' && tolower((int32_t) ch) <= 'z') {
if ((ch <= 'Z' && (flags & BF_UPPER) > 0) || (ch > 'Z' && (flags & BF_UPPER) > 0)) {
printf("[ERROR] character %c defined in -x although the whole letter range was already defined by '%c', ignored\n", ch, ch <= 'Z' ? 'A' : 'a');
return 0;
}
//printf("[WARNING] adding character %c for -x, note that '%c' will add all %scase letters\n", ch, ch <= 'Z' ? 'A' : 'a', ch <= 'Z' ? "up" : "low");
}
(*crs_len)++;
if (BF_CHARSMAX - *crs_len < 1) {
free(bf_options.crs);
fprintf(stderr, "Error: charset specification exceeds %d characters.\n", BF_CHARSMAX);
return 1;
} else {
bf_options.crs[*crs_len - 1] = ch;
bf_options.crs[*crs_len] = '\0';
}
return 0;
}
// return values : 0 on success, 1 on error
//
// note that we check for -x .:.:ab but not for -x .:.:ba
//
int32_t bf_init(char *arg) {
int32_t i = 0;
int32_t crs_len = 0;
char flags = 0;
char *tmp = strchr(arg, ':');
if (!tmp) {
fprintf(stderr, "Error: Invalid option format for -x\n");
return 1;
} else {
tmp[0] = '\0';
}
bf_options.from = atoi(arg);
if (bf_options.from < 1 || bf_options.from > 127) {
fprintf(stderr, "Error: minimum length must be between 1 and 127, format: -x min:max:types\n");
return 1;
}
arg = tmp + 1;
tmp++;
if (!arg[0]) {
fprintf(stderr, "Error: no maximum length specified for -x min:max:types!\n");
return 1;
}
tmp = strchr(arg, ':');
if (!tmp) {
fprintf(stderr, "Error: Invalid option format for -x\n");
return 1;
} else {
tmp[0] = '\0';
}
bf_options.to = atoi(arg);
tmp++;
if (bf_options.from > bf_options.to) {
fprintf(stderr, "Error: you specified a minimum length higher than the maximum length!\n");
return 1;
}
if (tmp[0] == 0) {
fprintf(stderr, "Error: charset not specified!\n");
return 1;
}
bf_options.crs = malloc(sizeof(char) * BF_CHARSMAX);
if (bf_options.crs == NULL) {
fprintf(stderr, "Error: can't allocate enough memory!\n");
return 1;
}
bf_options.crs[0] = 0;
for (; tmp[i]; i++) {
if (bf_options.disable_symbols) {
if (add_single_char(tmp[i], flags, &crs_len) == -1)
return 1;
} else {
switch (tmp[i]) {
case 'a':
crs_len += 26;
if (BF_CHARSMAX - crs_len < 1) {
free(bf_options.crs);
fprintf(stderr, "Error: charset specification exceeds %d characters.\n", BF_CHARSMAX);
return 1;
} else if (flags & BF_LOWER) {
free(bf_options.crs);
fprintf(stderr, "Error: 'a' specified more than once in charset!\n");
return 1;
} else {
strcat(bf_options.crs, "abcdefghijklmnopqrstuvwxyz");
flags |= BF_LOWER;
}
break;
case 'A':
crs_len += 26;
if (BF_CHARSMAX - crs_len < 1) {
free(bf_options.crs);
fprintf(stderr, "Error: charset specification exceeds %d characters.\n", BF_CHARSMAX);
return 1;
} else if (flags & BF_UPPER) {
free(bf_options.crs);
fprintf(stderr, "Error: 'A' specified more than once in charset!\n");
return 1;
} else {
strcat(bf_options.crs, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
flags |= BF_UPPER;
}
break;
case '1':
crs_len += 10;
if (BF_CHARSMAX - crs_len < 1) {
free(bf_options.crs);
fprintf(stderr, "Error: charset specification exceeds %d characters.\n", BF_CHARSMAX);
return 1;
} else if (flags & BF_NUMS) {
free(bf_options.crs);
fprintf(stderr, "Error: '1' specified more than once in charset!\n");
return 1;
} else {
strcat(bf_options.crs, "0123456789");
flags |= BF_NUMS;
}
break;
default:
if (add_single_char(tmp[i], flags, &crs_len) == -1)
return 1;
break;
}
}
}
bf_options.crs_len = crs_len;
bf_options.current = bf_options.from;
memset((char *) bf_options.state, 0, sizeof(bf_options.state));
if (debug)
printf("[DEBUG] bfg INIT: from %u, to %u, len: %u, set: %s\n", bf_options.from, bf_options.to, bf_options.crs_len, bf_options.crs);
return 0;
}
uint64_t bf_get_pcount() {
int32_t i;
double count = 0;
uint64_t foo;
for (i = bf_options.from; i <= bf_options.to; i++)
count += (pow((double) bf_options.crs_len, (double) i));
if (count >= 0xffffffff) {
fprintf(stderr, "\n[ERROR] definition for password bruteforce (-x) generates more than 4 billion passwords\n");
exit(-1);
}
foo = count / 1;
return foo;
}
char *bf_next() {
int32_t i, pos = bf_options.current - 1;
if (bf_options.current > bf_options.to)
return NULL; // we are done
if ((bf_options.ptr = malloc(BF_CHARSMAX)) == NULL) {
fprintf(stderr, "Error: Can not allocate memory for -x data!\n");
return NULL;
}
for (i = 0; i < bf_options.current; i++)
bf_options.ptr[i] = bf_options.crs[bf_options.state[i]];
bf_options.ptr[bf_options.current] = 0;
if (debug) {
printf("[DEBUG] bfg IN: len %u, from %u, current %u, to %u, state:", bf_options.crs_len, bf_options.from, bf_options.current, bf_options.to);
for (i = 0; i < bf_options.current; i++)
printf(" %u", bf_options.state[i]);
printf(", x: %s\n", bf_options.ptr);
}
while (pos >= 0 && (++bf_options.state[pos]) >= bf_options.crs_len) {
bf_options.state[pos] = 0;
pos--;
}
if (pos < 0) {
bf_options.current++;
memset((char *) bf_options.state, 0, sizeof(bf_options.state));
}
return bf_options.ptr;
}
#endif