forked from samyk/rarcrack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rarcrack.c
359 lines (341 loc) · 10.3 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
/*
* Copyright (C) 2007 by David Zoltan Kedves
* kedazo@gmail.com
*
* 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.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "rarcrack.h"
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;
}
static inline void savestatus() {
xmlNodePtr root = NULL;
xmlNodePtr node = NULL;
xmlChar* tmp = NULL;
if ((strlen(statname) > 0) && (status != NULL)) {
root = xmlDocGetRootElement(status);
if (root) {
xmlMutexLock(finishedMutex);
for (node = root->children; node; node = node->next) {
if (xmlStrcmp(node->name, "current") == 0) {
xmlMutexLock(pwdMutex);
tmp = xmlEncodeEntitiesReentrant(status, (const xmlChar*) &password);
xmlMutexUnlock(pwdMutex);
if (node->children) {
if (password[0] == '\0')
xmlNodeSetContent(node->children, getfirstpassword());
else
xmlNodeSetContent(node->children, tmp);
}
xmlFree(tmp);
} else if ((finished == 1) && (xmlStrcmp(node->name,"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);
}
return;
}
inline int abcnumb(char a) {
int i;
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 != NULL) {
fclose(totest);
status = xmlParseFile(statname);
}
if (status != NULL)
root = xmlDocGetRootElement(status);
else
status = xmlNewDoc(NULL);
if (root != NULL) {
parserctxt = xmlNewParserCtxt();
for (node = root->children; node; node = node->next) {
if (xmlStrcmp(node->name, "abc") == 0) {
if (node->children && (strlen(node->children->content) > 0)) {
ABC = xmlStringDecodeEntities(parserctxt,
node->children->content,XML_SUBSTITUTE_BOTH,0,0,0);
} else
ret = 1;
} else if (xmlStrcmp(node->name, "current") == 0) {
if (node->children && (strlen(node->children->content) > 0)) {
tmp = xmlStringDecodeEntities(parserctxt,
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, "good_password") == 0) {
if (node->children && (strlen(node->children->content) > 0)) {
tmp = 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, "rarcrack");
xmlDocSetRootElement(status, root);
node = xmlNewTextChild(root, NULL, "abc", ABC);
node = xmlNewTextChild(root, NULL, "current", getfirstpassword());
node = xmlNewTextChild(root, NULL, "good_password", "");
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];
return;
}
inline 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);
return;
}
void init(int argc, char **argv) {
unsigned int i, j;
int help = 0;
int threads = 2;
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 (strncmp(MIME[i], test, strlen(MIME[i])) == 0) {
strcpy(finalcmd,CMD[i]);
archive_type = i;
break;
}
}
if (archive_type < 0) {
printf("ERROR: Could not detect archive type (use --type?)\n");
return;
}
printf("INFO: detected file type: %s\n", TYPE[archive_type]);
} else {
printf("INFO: the specified archive type: %s\n", TYPE[archive_type]);
}
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);
return;
}
int main(int argc, char *argv[]) {
printf("RarCrack! 0.2 by David Zoltan Kedves (kedazo@gmail.com)\n\n");
init(argc,argv);
if (ABC != (char*) &default_ABC)
xmlFree(ABC);
if (status)
xmlFreeDoc(status);
xmlFreeMutex(pwdMutex);
xmlFreeMutex(finishedMutex);
return EXIT_SUCCESS;
}