-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.c
454 lines (401 loc) · 11.2 KB
/
main.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/**
* Copyright (C) 2006 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 <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <signal.h>
#include <getopt.h>
#include <string.h>
#include <unistd.h>
#include "pdfparser.h"
#include "pdfcrack.h"
#include "benchmark.h"
#define PRINTERVAL 5 /** Print Progress Interval (seconds) */
#define SAVEINTERVAL 30 // in seconds (should be a multiple of PRINTINVERVAL)
#define CRASHFILE "savedstatecrash.sav"
#define SAVEFILE "savedstateperiodic.sav"
#define VERSION_MAJOR 0
#define VERSION_MINOR 11
char crashStateFileName[64];
char periodicStateFileName[64];
/** alarmInterrupt is used to print out the progress at specific intervals */
static void
alarmInterrupt() {
static int counter = 0;
if(!printProgress()){
alarm(PRINTERVAL);
}
if (++counter * PRINTERVAL >= SAVEINTERVAL){
counter = 0;
FILE *file;
if((file = fopen(periodicStateFileName, "w")) == 0) {
fprintf(stderr,"Could not save progress to file %s\n", periodicStateFileName);
printf("Could not save progress to file %s\n", periodicStateFileName);
} else {
saveState(file);
fclose(file);
printf("Successfully saved progress to %s\n",periodicStateFileName);
}
}
}
/** autoSave is used to save down the current state when interrupted */
__attribute__ ((noreturn)) static void
autoSave(int sig) {
FILE *file;
if(sig)
fprintf(stderr,"Caught signal %d!\nTrying to save state...\n",sig);
if((file = fopen(crashStateFileName, "w")) == 0) {
fprintf(stderr,"Could not open %s for writing\n", crashStateFileName);
}
else{
saveState(file);
fclose(file);
fprintf(stderr,"Successfully saved state to %s!\n", crashStateFileName);
}
exit(sig);
}
/** print some help for the user */
static void
printHelp(char *progname) {
printf("Usage: %s -f filename [OPTIONS]\n"
"OPTIONS:\n"
"-b, --bench\t\tperform benchmark and exit\n"
"-c, --charset=STRING\tUse the characters in STRING as charset\n"
"-w, --wordlist=FILE\tUse FILE as source of passwords to try\n"
"-n, --minpw=INTEGER\tSkip trying passwords shorter than this\n"
"-m, --maxpw=INTEGER\tStop when reaching this passwordlength\n"
"-t, --threads=INTEGER\tStop number of threads used (default 0)\n"
"\t\t\trecommendedfor multi-core systems\n"
"-l, --loadState=FILE\tContinue from the state saved in FILENAME\n"
"-o, --owner\t\tWork with the ownerpassword\n"
"-u, --user\t\tWork with the userpassword (default)\n"
"-p, --password=STRING\tGive userpassword to speed up breaking\n"
"\t\t\townerpassword (implies -o)\n"
"-q, --quiet\t\tRun quietly\n"
"-s, --permutate\t\tTry permutating the passwords (currently only\n"
"\t\t\tsupports switching first character to uppercase)\n"
"-v, --version\t\tPrint version and exit\n"
"-z, --zone=INT1/INT2\tRun program on multiple hosts (split search space)\n"
"\t\t\te.g. -z 3/8 (run on host 3 out of 8)\n",
progname);
}
static bool getZoneInfo(char* arg, int* zone, int* nrOfZones) {
//printf("zone parameter '%s'\n", arg);
char* str = &arg[0];
char ch = str[0];
char zoneStr[8];
char nrOfZonesStr[8];
int i = 0;
while (ch != '/' && ch != 0 && i < 8) {
zoneStr[i] = ch;
ch = str[++i];
}
if (i == 8 || ch == 0) {
return false; // malformed zone string
}
// else: everything ok
zoneStr[i] = 0;
str = &arg[i+1];
i = 0;
ch = str[0];
while (ch != 0 && i < 8) {
nrOfZonesStr[i] = ch;
ch = str[++i];
}
if (i == 8) {
return false;
}
nrOfZonesStr[i] = 0;
*zone = atoi(zoneStr);
*nrOfZones = atoi(nrOfZonesStr);
return true;
}
static void setZoneFileName(char* str, const char* prefix, int zone, int nrOfZones) {
sprintf(str, "%s.zone%iof%i", prefix, zone, nrOfZones);
}
int
main(int argc, char** argv) {
int ret = 0, minpw = 0, maxpw = 32, nrofthreads=0, nrOfZones = 1, zone = 1;;
struct sigaction act1, act2;
FILE *file = NULL, *wordlist = NULL;
bool recovery = false, quiet = false,
work_with_user = true, permutation = false, runBench = false;
uint8_t *userpassword = NULL;
char *charset = NULL, *inputfile = NULL, *wordlistfile = NULL;
EncData *e;
/** Parse arguments */
while(true) {
int c, option_index;
static struct option long_options[] = {
{"bench", no_argument , 0, 'b'},
{"charset", required_argument, 0, 'c'},
{"file", required_argument, 0, 'f'},
{"loadState",required_argument, 0, 'l'},
{"maxpw", required_argument, 0, 'm'},
{"minpw", required_argument, 0, 'n'},
{"threads", required_argument, 0, 't'},
{"owner", no_argument , 0, 'o'},
{"password", required_argument, 0, 'p'},
{"quiet", required_argument, 0, 'q'},
{"permutate",no_argument, 0, 's'},
{"user", no_argument , 0, 'u'},
{"wordlist", required_argument, 0, 'w'},
{"version", no_argument , 0, 'v'},
{"zones", required_argument, 0, 'z'},
{0, 0, 0, 0}};
/* getopt_long stores the option index here. */
option_index = 0;
c = getopt_long(argc, argv, "bc:f:l:m:n:t:op:qsuw:vz:",
long_options, &option_index);
/* Detect the end of the options. */
if(c == -1)
break;
switch(c) {
case 'b':
runBench = true;
break;
case 'c':
if(charset)
fprintf(stderr,"Charset already set\n");
else
charset = strdup(optarg);
break;
case 'f':
if(!recovery)
inputfile = strdup(optarg);
break;
case 'l':
if(inputfile) {
free(inputfile);
inputfile = NULL;
}
inputfile = strdup(optarg);
recovery = true;
break;
case 'm':
maxpw = atoi(optarg);
break;
case 'n':
minpw = atoi(optarg);
break;
case 't':
nrofthreads = atoi(optarg);
if (nrofthreads < 0){
fprintf(stderr, "Invalid --threads Parameter (must be set to a integer value >= 0)\n");
exit(1);
}
break;
case 'o':
work_with_user = false;
break;
case 'p':
userpassword = (uint8_t*)strdup(optarg);
work_with_user = false;
break;
case 'q':
quiet = true;
break;
case 'u':
if(!work_with_user) {
fprintf(stderr, "Cannot work with both user- and owner-password\n");
exit(1);
}
work_with_user = true;
break;
case 's':
permutation = true;
break;
case 'w':
if(wordlistfile)
fprintf(stderr, "Wordlist already set\n");
else
wordlistfile = strdup(optarg);
break;
case 'v':
printf("pdfcrack version %d.%d\n", VERSION_MAJOR, VERSION_MINOR);
return 0;
case 'z':
if (!getZoneInfo(optarg, &zone, &nrOfZones)) {
printf("Malformed zone info. Expected format 'zone/numZones'.\n");
}
if (nrOfZones < 1){
printf("Number of zones must be > 0.\n");
exit(1);
}
if (zone < 1){
printf("Zone must be > 0.\n");
exit(1);
}
if (zone > nrOfZones) {
printf("Zone must be smaller or equal to number of zones.\n");
exit(1);
}
break;
default:
printHelp(argv[0]);
ret = 1;
goto out3;
}
}
if (runBench) {
runBenchmark(nrofthreads);
return 0;
}
/** If there are any unhandled arguments and the filename and/or wordlist is
not set: try to match the first as the filename and the second as
wordlist.
*/
{
int i = optind;
if(i > 0) {
if(i < argc && !inputfile)
inputfile = strdup(argv[i++]);
if(i < argc && !wordlistfile)
wordlistfile = strdup(argv[i++]);
if(i < argc)
while(i<argc) {
fprintf(stderr,"Non-option argument %s\n", argv[i++]);
}
}
}
if(!inputfile || minpw < 0 || maxpw < 0) {
printHelp(argv[0]);
ret = 1;
goto out3;
}
if(nrOfZones > 1 && wordlistfile) {
printf("Zones cannot be used in combination with a wordlist. Split wordlist instead and run pdfcrack with different wordlists on the different hosts.\n");
ret = 1;
goto out3;
}
if((file = fopen(inputfile, "r")) == 0) {
fprintf(stderr,"Error: file %s not found\n", inputfile);
ret = 2;
goto out3;
}
e = calloc(1,sizeof(EncData));
if(recovery) {
if(wordlistfile) {
free(wordlistfile);
wordlistfile = NULL;
}
if(!loadState(file, e, &wordlistfile, &work_with_user)) {
fprintf(stderr, "Error: Not a savefile or savefile is damaged\n");
ret = 3;
goto out1;
}
if(!quiet)
printf("Loaded state for %s\n", inputfile);
}
else { /** !recovery */
if(!openPDF(file,e)) {
fprintf(stderr, "Error: Not a valid PDF\n");
ret = 3;
goto out1;
}
ret = getEncryptedInfo(file, e);
if(ret) {
if(ret == EENCNF)
fprintf(stderr, "Error: Could not extract encryption information\n");
else if(ret == ETRANF || ret == ETRENF || ret == ETRINF)
fprintf(stderr, "Error: Encryption not detected (is the document password protected?)\n");
ret = 4;
goto out1;
}
else if(e->revision < 2 || (strcmp(e->s_handler,"Standard") != 0)) {
fprintf(stderr, "The specific version is not supported (%s - %d)\n", e->s_handler, e->revision);
ret = 5;
goto out1;
}
}
if(fclose(file)) {
fprintf(stderr, "Error: closing file %s\n", inputfile);
}
if(minpw > maxpw) {
fprintf(stderr, "Warning: minimum pw-length bigger than max\n");
}
if(wordlistfile) {
if((wordlist = fopen(wordlistfile, "r")) == 0) {
fprintf(stderr,"Error: file %s not found\n", wordlistfile);
ret = 6;
goto out2;
}
}
if (nrOfZones > 1) {
setZoneFileName(periodicStateFileName, SAVEFILE, zone, nrOfZones);
setZoneFileName(crashStateFileName, CRASHFILE, zone, nrOfZones);
} else {
strcpy(periodicStateFileName, SAVEFILE);
strcpy(crashStateFileName, CRASHFILE);
}
act2.sa_handler = autoSave;
sigfillset(&act2.sa_mask);
act2.sa_flags = 0;
sigaction(SIGINT, &act2, 0);
if(!quiet) {
printEncData(e);
act1.sa_handler = alarmInterrupt;
sigemptyset(&act1.sa_mask);
act1.sa_flags = 0;
sigaction(SIGALRM, &act1, 0);
alarm(PRINTERVAL);
}
/** Try to initialize the cracking-engine */
if(!initPDFCrack(e, userpassword, work_with_user, wordlistfile,
wordlistfile?Wordlist:Generative, wordlist, charset,
(unsigned int)minpw, (unsigned int)maxpw, permutation, nrofthreads, zone, nrOfZones)) {
cleanPDFCrack();
fprintf(stderr, "Wrong userpassword, '%s'\n", userpassword);
ret = 7;
goto out2;
}
/** Do the actual crunching */
runCrack();
/** cleanup */
cleanPDFCrack();
freeEncData(e);
if(wordlistfile) {
fclose(wordlist);
free(wordlistfile);
}
if(inputfile)
free(inputfile);
if(charset)
free(charset);
if(userpassword)
free(userpassword);
return 0;
out1:
if(fclose(file))
fprintf(stderr, "Error: closing file %s\n", inputfile);
out2:
freeEncData(e);
out3:
if(inputfile)
free(inputfile);
if(charset)
free(charset);
if(userpassword)
free(userpassword);
exit(ret);
}