-
Notifications
You must be signed in to change notification settings - Fork 4
/
passwords.c
329 lines (274 loc) · 7.33 KB
/
passwords.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
/**
* Copyright (C) 2006-2008 Henning Norén
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* Copyright (C) 2009 Andreas Meier, Michael Kuhn
* Multi-core (threads) and multi-computer (zones) extension
*
*/
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include "passwords.h"
#include "pdfcrack.h"
#define PASSLENGTH 33
static FILE *wordList = NULL;
static const char *wordListName;
static bool wlMore;
static bool (*npw)() = NULL;
static unsigned int (*spw)(uint8_t *outbuf) = NULL;
static passwordMethod pwMethod;
static int nrOfZones;
bool
nextPassword() { return npw(); }
unsigned int
setPassword(uint8_t *outbuf) { return spw(outbuf); }
static bool
wlNextPassword() { return wlMore; }
static unsigned int
wlSetPassword(uint8_t *outbuf) {
int ch;
unsigned int passlength;
ch = 0;
passlength = 0;
ch = getc(wordList);
while(ch != '\n' && ch != '\r' && ch != EOF && passlength < 32) {
outbuf[passlength++] = ch;
ch = getc(wordList);
}
/** clean up garbage of passwords longed than 32 chars */
if(unlikely(passlength == 32))
while(ch != '\n' && ch != '\r' && ch != EOF)
ch = getc(wordList);
if(ch == '\r') {
ch = getc(wordList);
if(ch != '\n')
ungetc(ch, wordList);
}
if(unlikely(ch == EOF))
wlMore = false;
return passlength;
}
static void
setWordList(FILE *file, const char *wl) {
wordList = file;
wordListName = wl;
npw = &wlNextPassword;
spw = &wlSetPassword;
wlMore = true;
}
static const uint8_t
stdchars[] = {"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789"};
static const uint8_t *charset;
static unsigned int charsetLen;
static unsigned int maxPasswordLen;
static int password[PASSLENGTH];
static unsigned int
genSetPassword(uint8_t *outbuf) {
unsigned int i;
for(i=0;password[i] != -1;i++)
outbuf[i] = charset[password[i]];
return i;
}
/*
* Init password offset for zones (0 for zone 1, 1 for zone 2, etc.)
*/
static bool
initPasswordOffsetZones(int off) {
unsigned int i = 0;
password[i] = off;
while (password[i] >= (int)charsetLen) {
if (password[i + 1] == -1) {
password[i + 1] = 0;
}
password[i + 1] += password[i] / (int)charsetLen;
password[i] = password[i] % (int)charsetLen;
i++;
}
// printPwd();
return (i < maxPasswordLen);
}
static bool
genNextPasswordZones() {
unsigned int i = 0;
// step size = nrOfZones
password[i] += nrOfZones;
while (password[i] >= (int)charsetLen) {
if (password[i + 1] == -1) {
password[i + 1] = 0;
}
password[i + 1] += password[i] / (int)charsetLen;
password[i] = password[i] % (int)charsetLen;
i++;
}
// printPwd();
return (i < maxPasswordLen);
}
static bool
genNextPassword() {
unsigned int i = 0;
/** this is pretty simple...
Change the current position (i) to the value in the next position of the
charset.
If we have reached the end of the charset, move the current position to
the next one and return true unless we have reached the last position we
want to try.
*/
while(++password[i] == (int)charsetLen)
password[i++] = 0;
return (i != maxPasswordLen);
}
static bool recovery = false;
static void
setCharset(const char *cs, const unsigned int minPw,
const unsigned int maxPw, const int zone, const int nrOfZones_local)
{
int i;
unsigned int min;
nrOfZones = nrOfZones_local;
if (nrOfZones > 1) {
npw = &genNextPasswordZones;
} else {
npw = &genNextPassword;
}
spw = &genSetPassword;
if(!recovery) {
/** This should already be set if we are loading from a saved state */
if(cs)
charset = (const uint8_t*)cs;
else
charset = stdchars;
charsetLen = strlen((const char*)charset);
/** Make sure that max- and min-password are smaller than 32 */
if(maxPw < PASSLENGTH)
maxPasswordLen = maxPw;
else
maxPasswordLen = PASSLENGTH-1;
if(minPw < PASSLENGTH)
min = minPw;
else
min = PASSLENGTH-1;
/** Initialize starting position */
for(i=0;i<(int)maxPasswordLen;i++) {
if(i<((int)min)-1)
password[i] = charsetLen-1;
else
password[i] = -1;
}
while(i < PASSLENGTH-1)
password[i++] = -1;
}
/** Put terminator (-1) at the last position */
password[PASSLENGTH-1] = -1;
if (nrOfZones > 1) {
// printf("before init offset:\n");
// printPwd();
initPasswordOffsetZones(zone - 1); // init to the offset (which is zone - 1)
// printf("after init offset:\n");
// printPwd();
}
}
void
initPasswords(const passwordMethod pm, FILE *file, const char *wl,
const char *cs, const unsigned int minPw,
const unsigned int maxPw, const int zone, const int nrOfZones_local) {
if(!recovery)
pwMethod = pm;
switch(pwMethod) {
case Generative:
setCharset(cs, minPw, maxPw, zone, nrOfZones_local);
break;
case Wordlist:
setWordList(file, wl);
break;
default:
/** The programmer is a twit! */
break;
}
}
/** Common patterns that is shared between pw_loadState and pw_saveState */
static const char string_PM[] = "\nPM: %d\n";
static const char string_MPCLC[] = "MaxPWL: %d\nCharset(%d): ";
bool
pw_loadState(FILE *file, char **wl) {
int pm;
unsigned int i, len;
char * __restrict string;
if(fscanf(file, string_PM, &pm) < 1)
return false;
if(pm == Generative) {
if(fscanf(file, string_MPCLC, &maxPasswordLen, &charsetLen) < 2)
return false;
/** check for extremely long charsets */
if(charsetLen > 256)
return false;
string = malloc(sizeof(uint8_t)*charsetLen+1);
for(i=0;i<charsetLen;i++)
string[i] = getc(file);
string[i] = '\0';
charset = (uint8_t*)string;
/** get the linebreak */
getc(file);
for(i=0;i<PASSLENGTH-1;i++)
if(fscanf(file, " %d", &password[i]) < 1) {
free(string);
return false;
}
}
else if(pm == Wordlist) {
if(fscanf(file, "Wordlist(%d): ", &len) < 1)
return false;
string = malloc(sizeof(char)*len+1);
if(fscanf(file, "%[^\n]\n", string) < 1) {
free(string);
return false;
}
string[len] = '\0';
*wl = string;
wordListName = string;
}
pwMethod = pm;
recovery = true;
return true;
}
void
pw_saveState(FILE *file) {
//setPwdMultiThread(&password[0], charset, charsetLen);
unsigned int i;
static int pwdMultiThread[32];
int * pwdArray = password;
fprintf(file, string_PM, pwMethod);
if(pwMethod == Generative) {
fprintf(file, string_MPCLC, maxPasswordLen, charsetLen);
fprintf(file, "%s\n", charset);
// adaption for multithreaded version
if (getNrOfThreads() > 1) {
// Find smallest password currently processed by the different threads
pwdArray = pwdMultiThread;
setPwdMultiThread(&pwdMultiThread[0], charset, charsetLen);
}
for(i=0;i<PASSLENGTH-1;i++)
fprintf(file," %d", pwdArray[i]);
}
else if(pwMethod == Wordlist) {
fprintf(file, "Wordlist(%zu): %s", strlen(wordListName), wordListName);
}
fprintf(file,"\n");
}