当前位置:   article > 正文

Linux环境rarcrack源码原理(重在学习)_error: the specified file (10) is not exists or yo

error: the specified file (10) is not exists or you don't have a right permi
  1. // Standard headers
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <pthread.h>
  6. #include <unistd.h>
  7. // libxml2 headers
  8. #include <libxml/xmlmemory.h>
  9. #include <libxml/parser.h>
  10. #include <libxml/parserInternals.h>
  11. #include <libxml/tree.h>
  12. #include <libxml/threads.h>
  13. // Default char list
  14. char default_ABC[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  15. // Command checks the type of file
  16. const char CMD_DETECT[] = "file -i -b %s";
  17. // File extensions
  18. // "" is the end of the list
  19. const char *TYPE[] = { "rar", "7z", "zip", "" };
  20. // File Types
  21. const char *MIME[] = { "application/x-rar;", "application/x-7z-compressed;", "application/zip;", "" };
  22. // Commnds for each file type
  23. 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", "" };
  24. // Max password length
  25. #define PWD_LEN 100
  26. char *getfirstpassword();
  27. void crack_start(unsigned int threads);
  28. char* ABC = (char*) &default_ABC;
  29. int ABCLEN;
  30. char password[PWD_LEN+1] = {'\0','\0'}; //this contains the actual password
  31. char password_good[PWD_LEN+1] = {'\0', '\0'}; //this changed only once, when we found the good passord
  32. unsigned int curr_len = 1; //current password length
  33. long counter = 0; //this couning probed passwords
  34. xmlMutexPtr pwdMutex; //mutex for password char array
  35. char filename[255]; //the archive file name
  36. char statname[259]; //status xml file name filename + ".xml"
  37. xmlDocPtr status;
  38. int finished = 0;
  39. xmlMutexPtr finishedMutex;
  40. char finalcmd[300] = {'\0', '\0'}; //this depending on arhive file type, it's a command to test file with password
  41. char *getfirstpassword() {
  42. static char ret[2];
  43. ret[0] = ABC[0];
  44. ret[1] = '\0';
  45. return (char*) &ret;
  46. }
  47. void savestatus() {
  48. xmlNodePtr root = NULL;
  49. xmlNodePtr node = NULL;
  50. xmlChar* tmp = NULL;
  51. if ((strlen(statname) > 0) && status) {
  52. root = xmlDocGetRootElement(status);
  53. if (root) {
  54. xmlMutexLock(finishedMutex);
  55. for (node = root->children; node; node = node->next) {
  56. if (xmlStrcmp(node->name, (const xmlChar*)"current") == 0) {
  57. xmlMutexLock(pwdMutex);
  58. tmp = xmlEncodeEntitiesReentrant(status, (const xmlChar*) &password);
  59. xmlMutexUnlock(pwdMutex);
  60. if (node->children) {
  61. if (password[0] == '\0') {
  62. xmlNodeSetContent(node->children, (const xmlChar*)getfirstpassword());
  63. } else {
  64. xmlNodeSetContent(node->children, tmp);
  65. }
  66. }
  67. xmlFree(tmp);
  68. } else if ((finished == 1) && (xmlStrcmp(node->name, (const xmlChar*)"good_password") == 0)) {
  69. tmp = xmlEncodeEntitiesReentrant(status, (const xmlChar*) &password_good);
  70. if (node->children) {
  71. xmlNodeSetContent(node->children, tmp);
  72. }
  73. xmlFree(tmp);
  74. }
  75. }
  76. xmlMutexUnlock(finishedMutex);
  77. }
  78. xmlSaveFormatFileEnc(statname, status, "UTF-8", 1);
  79. }
  80. }
  81. int abcnumb(char a) {
  82. int i = 0;
  83. for (i = 0; i < ABCLEN; i++) {
  84. if (ABC[i] == a) {
  85. return i;
  86. }
  87. }
  88. return 0;
  89. }
  90. int loadstatus() {
  91. xmlNodePtr root = NULL;
  92. xmlNodePtr node = NULL;
  93. xmlParserCtxtPtr parserctxt;
  94. int ret = 0;
  95. char* tmp;
  96. FILE* totest;
  97. totest = fopen(statname, "r");
  98. if (totest) {
  99. fclose(totest);
  100. status = xmlParseFile(statname);
  101. }
  102. if (status) {
  103. root = xmlDocGetRootElement(status);
  104. } else {
  105. status = xmlNewDoc(NULL);
  106. }
  107. if (root) {
  108. parserctxt = xmlNewParserCtxt();
  109. for (node = root->children; node; node = node->next) {
  110. if (xmlStrcmp(node->name, (const xmlChar*)"abc") == 0) {
  111. if (node->children && (strlen((const char*)node->children->content) > 0)) {
  112. ABC = (char *)xmlStringDecodeEntities(parserctxt, (const xmlChar*)node->children->content, XML_SUBSTITUTE_BOTH, 0, 0, 0);
  113. } else {
  114. ret = 1;
  115. }
  116. } else if (xmlStrcmp(node->name, (const xmlChar*)"current") == 0) {
  117. if (node->children && (strlen((const char*)node->children->content) > 0)) {
  118. tmp = (char *)xmlStringDecodeEntities(parserctxt, (const xmlChar*)node->children->content, XML_SUBSTITUTE_BOTH, 0, 0, 0);
  119. strcpy(password,tmp);
  120. curr_len = strlen(password);
  121. printf("INFO: Resuming cracking from password: '%s'\n",password);
  122. xmlFree(tmp);
  123. } else {
  124. ret = 1;
  125. }
  126. } else if (xmlStrcmp(node->name, (const xmlChar*)"good_password") == 0) {
  127. if (node->children && (strlen((const char*)node->children->content) > 0)) {
  128. tmp = (char *)xmlStringDecodeEntities(parserctxt, node->children->content, XML_SUBSTITUTE_BOTH,0,0,0);
  129. strcpy(password,tmp);
  130. curr_len = strlen(password);
  131. xmlMutexLock(finishedMutex);
  132. finished = 1;
  133. xmlMutexUnlock(finishedMutex);
  134. strcpy((char*) &password_good, (char*) &password);
  135. printf("GOOD: This archive was succesfully cracked\n");
  136. printf(" The good password is: '%s'\n", password);
  137. xmlFree(tmp);
  138. ret = 1;
  139. }
  140. }
  141. }
  142. xmlFreeParserCtxt(parserctxt);
  143. } else {
  144. root = xmlNewNode(NULL, (const xmlChar*)"rarcrack");
  145. xmlDocSetRootElement(status, root);
  146. node = xmlNewTextChild(root, NULL, (const xmlChar*)"abc", (const xmlChar*)ABC);
  147. node = xmlNewTextChild(root, NULL, (const xmlChar*)"current", (const xmlChar*)getfirstpassword());
  148. node = xmlNewTextChild(root, NULL, (const xmlChar*)"good_password", (const xmlChar*)"");
  149. savestatus();
  150. }
  151. return ret;
  152. }
  153. void nextpass2(char *p, unsigned int n) {
  154. int i;
  155. if (p[n] == ABC[ABCLEN-1]) {
  156. p[n] = ABC[0];
  157. if (n > 0) {
  158. nextpass2(p, n-1);
  159. } else {
  160. for (i=curr_len; i>=0; i--) {
  161. p[i+1]=p[i];
  162. }
  163. p[0]=ABC[0];
  164. p[++curr_len]='\0';
  165. }
  166. } else {
  167. p[n] = ABC[abcnumb(p[n])+1];
  168. }
  169. }
  170. char *nextpass() {
  171. //IMPORTANT: the returned string must be freed
  172. char *ok = malloc(sizeof(char)*(PWD_LEN+1));
  173. xmlMutexLock(pwdMutex);
  174. strcpy(ok, password);
  175. nextpass2((char*) &password, curr_len - 1);
  176. xmlMutexUnlock(pwdMutex);
  177. return ok;
  178. }
  179. void *status_thread() {
  180. int pwds;
  181. const short status_sleep = 3;
  182. while(1) {
  183. sleep(status_sleep);
  184. xmlMutexLock(finishedMutex);
  185. pwds = counter / status_sleep;
  186. counter = 0;
  187. if (finished != 0) {
  188. break;
  189. }
  190. xmlMutexUnlock(finishedMutex);
  191. xmlMutexLock(pwdMutex);
  192. printf("Probing: '%s' [%d pwds/sec]\n", password, pwds);
  193. xmlMutexUnlock(pwdMutex);
  194. savestatus(); //FIXME: this is wrong, when probing current password(s) is(are) not finished yet, and the program is exiting
  195. }
  196. }
  197. void *crack_thread() {
  198. char *current;
  199. char ret[200];
  200. char cmd[400];
  201. FILE *Pipe;
  202. while (1) {
  203. current = nextpass();
  204. sprintf((char*)&cmd, finalcmd, current, filename);
  205. Pipe = popen(cmd, "r");
  206. while (! feof(Pipe)) {
  207. fgets((char*)&ret, 200, Pipe);
  208. if (strcasestr(ret, "ok") != NULL) {
  209. strcpy(password_good, current);
  210. xmlMutexLock(finishedMutex);
  211. finished = 1;
  212. printf("GOOD: password cracked: '%s'\n", current);
  213. xmlMutexUnlock(finishedMutex);
  214. savestatus();
  215. break;
  216. }
  217. }
  218. pclose(Pipe);
  219. xmlMutexLock(finishedMutex);
  220. counter++;
  221. if (finished != 0) {
  222. xmlMutexUnlock(finishedMutex);
  223. break;
  224. }
  225. xmlMutexUnlock(finishedMutex);
  226. free(current);
  227. }
  228. }
  229. void crack_start(unsigned int threads) {
  230. pthread_t th[13];
  231. unsigned int i;
  232. for (i = 0; i < threads; i++) {
  233. (void) pthread_create(&th[i], NULL, crack_thread, NULL);
  234. }
  235. (void) pthread_create(&th[12], NULL, status_thread, NULL);
  236. for (i = 0; i < threads; i++) {
  237. (void) pthread_join(th[i], NULL);
  238. }
  239. (void) pthread_join(th[12], NULL);
  240. }
  241. void init(int argc, char **argv) {
  242. int i, j;
  243. int help = 0;
  244. int threads = 1;
  245. int archive_type = -1;
  246. FILE* totest;
  247. char test[300];
  248. xmlInitThreads();
  249. pwdMutex = xmlNewMutex();
  250. finishedMutex = xmlNewMutex();
  251. if (argc == 1) {
  252. printf("USAGE: rarcrack encrypted_archive.ext [--threads NUM] [--type rar|zip|7z]\n");
  253. printf(" For more information please run \"rarcrack --help\"\n");
  254. help = 1;
  255. } else {
  256. for (i = 1; i < argc; i++) {
  257. if (strcmp(argv[i],"--help") == 0) {
  258. printf("Usage: rarcrack encrypted_archive.ext [--threads NUM] [--type rar|zip|7z]\n\n");
  259. printf("Options: --help: show this screen.\n");
  260. printf(" --type: you can specify the archive program, this needed when\n");
  261. printf(" the program couldn't detect the proper file type\n");
  262. printf(" --threads: you can specify how many threads\n");
  263. printf(" will be run, maximum 12 (default: 2)\n\n");
  264. printf("Info: This program supports only RAR, ZIP and 7Z encrypted archives.\n");
  265. printf(" RarCrack! usually detects the archive type.\n\n");
  266. help = 1;
  267. break;
  268. } else if (strcmp(argv[i],"--threads") == 0) {
  269. if ((i + 1) < argc) {
  270. sscanf(argv[++i], "%d", &threads);
  271. if (threads < 1) threads = 1;
  272. if (threads > 12) {
  273. printf("INFO: number of threads adjusted to 12\n");
  274. threads = 12;
  275. }
  276. } else {
  277. printf("ERROR: missing parameter for option: --threads!\n");
  278. help = 1;
  279. }
  280. } else if (strcmp(argv[i],"--type") == 0) {
  281. if ((i + 1) < argc) {
  282. sscanf(argv[++i], "%s", test);
  283. for (j = 0; strcmp(TYPE[j], "") != 0; j++) {
  284. if (strcmp(TYPE[j], test) == 0) {
  285. strcpy(finalcmd, CMD[j]);
  286. archive_type = j;
  287. break;
  288. }
  289. }
  290. if (archive_type < 0) {
  291. printf("WARNING: invalid parameter --type %s!\n", argv[i]);
  292. finalcmd[0] = '\0';
  293. }
  294. } else {
  295. printf("ERROR: missing parameter for option: --type!\n");
  296. help = 1;
  297. }
  298. } else {
  299. strcpy((char*)&filename, argv[i]);
  300. }
  301. }
  302. }
  303. if (help == 1) {
  304. return;
  305. }
  306. sprintf((char*)&statname,"%s.xml",(char*)&filename);
  307. totest = fopen(filename,"r");
  308. if (totest == NULL) {
  309. printf("ERROR: The specified file (%s) is not exists or \n", filename);
  310. printf(" you don't have a right permissions!\n");
  311. return;
  312. } else {
  313. fclose(totest);
  314. }
  315. if (finalcmd[0] == '\0') {
  316. //when we specify the file type, the programm will skip the test
  317. sprintf((char*)&test, CMD_DETECT, filename);
  318. totest = popen(test,"r");
  319. fscanf(totest,"%s",(char*)&test);
  320. pclose(totest);
  321. for (i = 0; strcmp(MIME[i],"") != 0; i++) {
  322. if (strcmp(MIME[i],test) == 0) {
  323. strcpy(finalcmd,CMD[i]);
  324. archive_type = i;
  325. break;
  326. }
  327. }
  328. if (archive_type > -1 && archive_type < 3) {
  329. printf("INFO: detected file type: %s\n", TYPE[archive_type]);
  330. }
  331. } else {
  332. if (archive_type > -1 && archive_type < 3) {
  333. printf("INFO: the specified archive type: %s\n", TYPE[archive_type]);
  334. }
  335. }
  336. if (finalcmd[0] == '\0') {
  337. printf("ERROR: Couldn't detect archive type\n");
  338. return;
  339. }
  340. printf("INFO: cracking %s, status file: %s\n", filename, statname);
  341. if (loadstatus() == 1) {
  342. printf("ERROR: The status file (%s) is corrupted!\n", statname);
  343. return;
  344. }
  345. ABCLEN = strlen(ABC);
  346. if (password[0] == '\0') {
  347. password[0] = ABC[0];
  348. }
  349. crack_start(threads);
  350. }
  351. int main(int argc, char **argv) {
  352. // Print author
  353. printf("RarCrack! 0.2 by David Zoltan Kedves (kedazo@gmail.com)\n\n");
  354. init(argc,argv);
  355. if (ABC != (char*) &default_ABC) {
  356. xmlFree(ABC);
  357. }
  358. if (status) {
  359. xmlFreeDoc(status);
  360. }
  361. // Free memory
  362. xmlFreeMutex(pwdMutex);
  363. xmlFreeMutex(finishedMutex);
  364. // 0
  365. return EXIT_SUCCESS;
  366. }

附使用方法:编译安装或执行apt-get install rarcrack下载安装后,执行如下命令

rarcrack 文件名 --threads 线程数 --type rar|zip|7z

记住,参数单词不要写错,比如threads不能写成thread,“--”不能写成‘-’,不然会出现类似如下错误

  1. ERROR: The specified file (rar) is not exists or
  2. you don't have a right permissions!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/664972
推荐阅读
相关标签
  

闽ICP备14008679号