-
Notifications
You must be signed in to change notification settings - Fork 47
/
rarcrack.c
424 lines (366 loc) · 13.2 KB
/
rarcrack.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
// Standard headers
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
// libxml2 headers
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/parserInternals.h>
#include <libxml/tree.h>
#include <libxml/threads.h>
// Default char list
char default_ABC[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
// Command checks the type of file
const char CMD_DETECT[] = "file -i -b %s";
// File extensions
// "" is the end of the list
const char *TYPE[] = { "rar", "7z", "zip", "" };
// File Types
const char *MIME[] = { "application/x-rar;", "application/x-7z-compressed;", "application/zip;", "" };
// Commnds for each file type
const char *CMD[] = { "unrar t -y -p%s %s 2>&1", "7z t -y -p%s %s 2>&1", "unzip -P%s -t %s 2>&1", "" };
// Max password length
#define PWD_LEN 100
char *getfirstpassword();
void crack_start(unsigned int threads);
char* ABC = (char*) &default_ABC;
int ABCLEN;
char password[PWD_LEN+1] = {'\0','\0'}; //this contains the actual password
char password_good[PWD_LEN+1] = {'\0', '\0'}; //this changed only once, when we found the good passord
unsigned int curr_len = 1; //current password length
long counter = 0; //this couning probed passwords
xmlMutexPtr pwdMutex; //mutex for password char array
char filename[255]; //the archive file name
char statname[259]; //status xml file name filename + ".xml"
xmlDocPtr status;
int finished = 0;
xmlMutexPtr finishedMutex;
char finalcmd[300] = {'\0', '\0'}; //this depending on arhive file type, it's a command to test file with password
char *getfirstpassword() {
static char ret[2];
ret[0] = ABC[0];
ret[1] = '\0';
return (char*) &ret;
}
void savestatus() {
xmlNodePtr root = NULL;
xmlNodePtr node = NULL;
xmlChar* tmp = NULL;
if ((strlen(statname) > 0) && status) {
root = xmlDocGetRootElement(status);
if (root) {
xmlMutexLock(finishedMutex);
for (node = root->children; node; node = node->next) {
if (xmlStrcmp(node->name, (const xmlChar*)"current") == 0) {
xmlMutexLock(pwdMutex);
tmp = xmlEncodeEntitiesReentrant(status, (const xmlChar*) &password);
xmlMutexUnlock(pwdMutex);
if (node->children) {
if (password[0] == '\0') {
xmlNodeSetContent(node->children, (const xmlChar*)getfirstpassword());
} else {
xmlNodeSetContent(node->children, tmp);
}
}
xmlFree(tmp);
} else if ((finished == 1) && (xmlStrcmp(node->name, (const xmlChar*)"good_password") == 0)) {
tmp = xmlEncodeEntitiesReentrant(status, (const xmlChar*) &password_good);
if (node->children) {
xmlNodeSetContent(node->children, tmp);
}
xmlFree(tmp);
}
}
xmlMutexUnlock(finishedMutex);
}
xmlSaveFormatFileEnc(statname, status, "UTF-8", 1);
}
}
int abcnumb(char a) {
int i = 0;
for (i = 0; i < ABCLEN; i++) {
if (ABC[i] == a) {
return i;
}
}
return 0;
}
int loadstatus() {
xmlNodePtr root = NULL;
xmlNodePtr node = NULL;
xmlParserCtxtPtr parserctxt;
int ret = 0;
char* tmp;
FILE* totest;
totest = fopen(statname, "r");
if (totest) {
fclose(totest);
status = xmlParseFile(statname);
}
if (status) {
root = xmlDocGetRootElement(status);
} else {
status = xmlNewDoc(NULL);
}
if (root) {
parserctxt = xmlNewParserCtxt();
for (node = root->children; node; node = node->next) {
if (xmlStrcmp(node->name, (const xmlChar*)"abc") == 0) {
if (node->children && (strlen((const char*)node->children->content) > 0)) {
ABC = (char *)xmlStringDecodeEntities(parserctxt, (const xmlChar*)node->children->content, XML_SUBSTITUTE_BOTH, 0, 0, 0);
} else {
ret = 1;
}
} else if (xmlStrcmp(node->name, (const xmlChar*)"current") == 0) {
if (node->children && (strlen((const char*)node->children->content) > 0)) {
tmp = (char *)xmlStringDecodeEntities(parserctxt, (const xmlChar*)node->children->content, XML_SUBSTITUTE_BOTH, 0, 0, 0);
strcpy(password,tmp);
curr_len = strlen(password);
printf("INFO: Resuming cracking from password: '%s'\n",password);
xmlFree(tmp);
} else {
ret = 1;
}
} else if (xmlStrcmp(node->name, (const xmlChar*)"good_password") == 0) {
if (node->children && (strlen((const char*)node->children->content) > 0)) {
tmp = (char *)xmlStringDecodeEntities(parserctxt, node->children->content, XML_SUBSTITUTE_BOTH,0,0,0);
strcpy(password,tmp);
curr_len = strlen(password);
xmlMutexLock(finishedMutex);
finished = 1;
xmlMutexUnlock(finishedMutex);
strcpy((char*) &password_good, (char*) &password);
printf("GOOD: This archive was succesfully cracked\n");
printf(" The good password is: '%s'\n", password);
xmlFree(tmp);
ret = 1;
}
}
}
xmlFreeParserCtxt(parserctxt);
} else {
root = xmlNewNode(NULL, (const xmlChar*)"rarcrack");
xmlDocSetRootElement(status, root);
node = xmlNewTextChild(root, NULL, (const xmlChar*)"abc", (const xmlChar*)ABC);
node = xmlNewTextChild(root, NULL, (const xmlChar*)"current", (const xmlChar*)getfirstpassword());
node = xmlNewTextChild(root, NULL, (const xmlChar*)"good_password", (const xmlChar*)"");
savestatus();
}
return ret;
}
void nextpass2(char *p, unsigned int n) {
int i;
if (p[n] == ABC[ABCLEN-1]) {
p[n] = ABC[0];
if (n > 0) {
nextpass2(p, n-1);
} else {
for (i=curr_len; i>=0; i--) {
p[i+1]=p[i];
}
p[0]=ABC[0];
p[++curr_len]='\0';
}
} else {
p[n] = ABC[abcnumb(p[n])+1];
}
}
char *nextpass() {
//IMPORTANT: the returned string must be freed
char *ok = malloc(sizeof(char)*(PWD_LEN+1));
xmlMutexLock(pwdMutex);
strcpy(ok, password);
nextpass2((char*) &password, curr_len - 1);
xmlMutexUnlock(pwdMutex);
return ok;
}
void *status_thread() {
int pwds;
const short status_sleep = 3;
while(1) {
sleep(status_sleep);
xmlMutexLock(finishedMutex);
pwds = counter / status_sleep;
counter = 0;
if (finished != 0) {
break;
}
xmlMutexUnlock(finishedMutex);
xmlMutexLock(pwdMutex);
printf("Probing: '%s' [%d pwds/sec]\n", password, pwds);
xmlMutexUnlock(pwdMutex);
savestatus(); //FIXME: this is wrong, when probing current password(s) is(are) not finished yet, and the program is exiting
}
}
void *crack_thread() {
char *current;
char ret[200];
char cmd[400];
FILE *Pipe;
while (1) {
current = nextpass();
sprintf((char*)&cmd, finalcmd, current, filename);
Pipe = popen(cmd, "r");
while (! feof(Pipe)) {
fgets((char*)&ret, 200, Pipe);
if (strcasestr(ret, "ok") != NULL) {
strcpy(password_good, current);
xmlMutexLock(finishedMutex);
finished = 1;
printf("GOOD: password cracked: '%s'\n", current);
xmlMutexUnlock(finishedMutex);
savestatus();
break;
}
}
pclose(Pipe);
xmlMutexLock(finishedMutex);
counter++;
if (finished != 0) {
xmlMutexUnlock(finishedMutex);
break;
}
xmlMutexUnlock(finishedMutex);
free(current);
}
}
void crack_start(unsigned int threads) {
pthread_t th[13];
unsigned int i;
for (i = 0; i < threads; i++) {
(void) pthread_create(&th[i], NULL, crack_thread, NULL);
}
(void) pthread_create(&th[12], NULL, status_thread, NULL);
for (i = 0; i < threads; i++) {
(void) pthread_join(th[i], NULL);
}
(void) pthread_join(th[12], NULL);
}
void init(int argc, char **argv) {
int i, j;
int help = 0;
int threads = 1;
int archive_type = -1;
FILE* totest;
char test[300];
xmlInitThreads();
pwdMutex = xmlNewMutex();
finishedMutex = xmlNewMutex();
if (argc == 1) {
printf("USAGE: rarcrack encrypted_archive.ext [--threads NUM] [--type rar|zip|7z]\n");
printf(" For more information please run \"rarcrack --help\"\n");
help = 1;
} else {
for (i = 1; i < argc; i++) {
if (strcmp(argv[i],"--help") == 0) {
printf("Usage: rarcrack encrypted_archive.ext [--threads NUM] [--type rar|zip|7z]\n\n");
printf("Options: --help: show this screen.\n");
printf(" --type: you can specify the archive program, this needed when\n");
printf(" the program couldn't detect the proper file type\n");
printf(" --threads: you can specify how many threads\n");
printf(" will be run, maximum 12 (default: 2)\n\n");
printf("Info: This program supports only RAR, ZIP and 7Z encrypted archives.\n");
printf(" RarCrack! usually detects the archive type.\n\n");
help = 1;
break;
} else if (strcmp(argv[i],"--threads") == 0) {
if ((i + 1) < argc) {
sscanf(argv[++i], "%d", &threads);
if (threads < 1) threads = 1;
if (threads > 12) {
printf("INFO: number of threads adjusted to 12\n");
threads = 12;
}
} else {
printf("ERROR: missing parameter for option: --threads!\n");
help = 1;
}
} else if (strcmp(argv[i],"--type") == 0) {
if ((i + 1) < argc) {
sscanf(argv[++i], "%s", test);
for (j = 0; strcmp(TYPE[j], "") != 0; j++) {
if (strcmp(TYPE[j], test) == 0) {
strcpy(finalcmd, CMD[j]);
archive_type = j;
break;
}
}
if (archive_type < 0) {
printf("WARNING: invalid parameter --type %s!\n", argv[i]);
finalcmd[0] = '\0';
}
} else {
printf("ERROR: missing parameter for option: --type!\n");
help = 1;
}
} else {
strcpy((char*)&filename, argv[i]);
}
}
}
if (help == 1) {
return;
}
sprintf((char*)&statname,"%s.xml",(char*)&filename);
totest = fopen(filename,"r");
if (totest == NULL) {
printf("ERROR: The specified file (%s) is not exists or \n", filename);
printf(" you don't have a right permissions!\n");
return;
} else {
fclose(totest);
}
if (finalcmd[0] == '\0') {
//when we specify the file type, the programm will skip the test
sprintf((char*)&test, CMD_DETECT, filename);
totest = popen(test,"r");
fscanf(totest,"%s",(char*)&test);
pclose(totest);
for (i = 0; strcmp(MIME[i],"") != 0; i++) {
if (strcmp(MIME[i],test) == 0) {
strcpy(finalcmd,CMD[i]);
archive_type = i;
break;
}
}
if (archive_type > -1 && archive_type < 3) {
printf("INFO: detected file type: %s\n", TYPE[archive_type]);
}
} else {
if (archive_type > -1 && archive_type < 3) {
printf("INFO: the specified archive type: %s\n", TYPE[archive_type]);
}
}
if (finalcmd[0] == '\0') {
printf("ERROR: Couldn't detect archive type\n");
return;
}
printf("INFO: cracking %s, status file: %s\n", filename, statname);
if (loadstatus() == 1) {
printf("ERROR: The status file (%s) is corrupted!\n", statname);
return;
}
ABCLEN = strlen(ABC);
if (password[0] == '\0') {
password[0] = ABC[0];
}
crack_start(threads);
}
int main(int argc, char **argv) {
// Print author
printf("RarCrack! 0.2 by David Zoltan Kedves ([email protected])\n\n");
init(argc,argv);
if (ABC != (char*) &default_ABC) {
xmlFree(ABC);
}
if (status) {
xmlFreeDoc(status);
}
// Free memory
xmlFreeMutex(pwdMutex);
xmlFreeMutex(finishedMutex);
// 0
return EXIT_SUCCESS;
}