home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / comm / tcp / Crak.lha / crak.c next >
Encoding:
C/C++ Source or Header  |  1997-02-27  |  8.1 KB  |  363 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <dos/dos.h>
  4. #include <exec/memory.h>
  5.  
  6. /* duh */
  7. #define TRUE        1  /* generic true/false because i don't want to waste time */
  8. #define FALSE       0  /* including exec/types.h so there. */
  9.  
  10. /* the following for passwd structure traversal */
  11. #define USER        1  /* at user field in line */
  12. #define PASSWD      2  /* at passwd field in passwd entry */
  13. #define DONE        3  /* at rest of crap and we can ignore this stuff */
  14.  
  15. /* maximum values for internal passwd structure */
  16. #define NAMESIZE   11  /* for struct passwd and for parsepass routine */
  17. #define SALTSIZE    3
  18. #define PASSWDSIZE 14
  19. #define WORDSIZE   20  /* maximum size of password in command line or password in dictfile */
  20.  
  21. /* values for return() from getnextdict */
  22. #define DICT_DONE  1  /* dictionary has been exhausted */
  23. #define DICT_ERROR 2  /* dictionary file is corrupt or something else just as fucked up */
  24.  
  25. int CXBRK(void) {return(0);}    /* sorry user we're gonna make SURE that you can't */
  26. int chkabort(void) {return(0);} /* f--k us up without giving us chance to clean up */
  27.  
  28. /*
  29. ** TODO: Must arrange the passwords according to salt values so that
  30. ** similar salts get called consecutively. This speeds crypt() up
  31. ** considerably! Ie: A LOT!!!
  32. */
  33. struct passwd
  34.   {
  35.   char name[NAMESIZE];
  36.   char salt[SALTSIZE];
  37.   char password[PASSWDSIZE];
  38.   struct passwd *nextitem;
  39.   };
  40.  
  41. FILE *dictionary;
  42.  
  43. char *crypt(char *, char *);
  44. struct passwd *parsepass(char *);
  45. void cleanup(struct passwd *);
  46. void log(char *, char *, char *);
  47. long getnextdict(char *, char *);
  48.  
  49. void main(int argc, char *argv[])
  50. {
  51. unsigned int counter;
  52. unsigned int c=FALSE;
  53. unsigned char commandword[WORDSIZE];
  54.  
  55. struct passwd *firstitem=NULL;
  56. struct passwd *currentitem=NULL;
  57.  
  58. long actualsigs;
  59. long waitmask = SIGBREAKF_CTRL_C;
  60. long status;
  61.  
  62. dictionary=NULL; /* i don't want to do this more than once */
  63.  
  64. for (counter = 0; counter < argc; counter++)
  65.   {
  66.   if (argv[counter][0] == '-')
  67.     {
  68.     c = TRUE;
  69.     for (counter=1; counter < WORDSIZE; counter++)
  70.       {
  71.       commandword[counter-1] = argv[2][counter];
  72.       if (commandword[counter-1] == 0) break;
  73.       }
  74.     break;
  75.     }
  76.   if (argv[counter][0] == '?')
  77.     {
  78. usage:
  79.     printf("Usage: %s <passwdfile> -<word> [outputfile]\n", argv[0]);
  80.     printf("       %s <passwdfile> <dictfile> [outputfile]\n", argv[0]);
  81.     printf("       %s ?\n", argv[0]);
  82.     printf("This program written in Feb 23, '97 by sudog.\n");
  83.     printf("So there.\n");
  84.     exit(0);
  85.     }
  86.   }
  87.  
  88. counter = 0;
  89.  
  90. if (argc > 2)
  91.   {
  92.   if (firstitem = parsepass(argv[1]))
  93.     {
  94.     currentitem = firstitem;
  95. /*
  96. ** This section deals with command-line supplied password.
  97. */
  98.     if (c)
  99.       {
  100.       while (currentitem != NULL)
  101.         {
  102.         if (!strcmp(currentitem->password, crypt(commandword, currentitem->salt)))
  103.           {
  104.           printf("%s %s\n", currentitem->name, commandword);
  105.           if (argc > 3)
  106.             {
  107.             log(argv[3], currentitem->name, commandword);
  108.             }
  109.           }
  110.         actualsigs = CheckSignal(waitmask);
  111.         if (actualsigs)
  112.           {
  113.           break;
  114.           }
  115.         currentitem = currentitem->nextitem;
  116.         }
  117.       }
  118. /*
  119. ** This section deals with a dictionary file!
  120. ** TODO:
  121. ** Add place holder so a system crash can be survived and iterations can
  122. ** continue from last point...  VERY cool. let's do som background cracking!
  123. */
  124.     else
  125.       {
  126.       while ( 1 )
  127.         {
  128.         if (!(status=getnextdict(commandword, argv[2])))
  129.           {
  130.           while (currentitem != NULL)
  131.             {
  132.             if (!strcmp(currentitem->password, crypt(commandword, currentitem->salt)))
  133.               {
  134.               printf("%s %s\n", currentitem->name, commandword);
  135.               if (argc > 3)
  136.                 {
  137.                 log(argv[3], currentitem->name, commandword);
  138.                 }
  139.               }
  140.             actualsigs = CheckSignal(waitmask);
  141.             if (actualsigs)
  142.               {
  143.               getnextdict(NULL, NULL);
  144.               cleanup(firstitem);
  145.               }
  146.             currentitem = currentitem->nextitem;
  147.             }
  148.           currentitem = firstitem;
  149.           }
  150.         else
  151.           {
  152.           if (status == DICT_DONE)
  153.             {
  154.             break;
  155.             }
  156.           if (status == DICT_ERROR)  /* getnextdict is smart enough to clean up as much as possible after error */
  157.             {
  158.             printf("Error: Dictionary file error.\n");
  159.             break;
  160.             }
  161.           }
  162.         }
  163.       }
  164.     }
  165.   else
  166.     {
  167.     printf("Error in parsing password file.\n");
  168.     }
  169.   }
  170. else
  171.   {
  172.   printf("Error. You don't have a clue. Here. Have one.\n");
  173.   goto usage;
  174.   }
  175.  
  176. cleanup(firstitem);
  177.  
  178. }
  179.  
  180. long getnextdict(char *commandword, char *filename)
  181. {
  182. long counter=0;
  183. int gotone=FALSE;
  184. char temp;
  185.  
  186. if (commandword == NULL && filename == NULL)
  187.   {
  188.   if (dictionary)
  189.     {
  190.     fclose(dictionary);
  191.     }
  192.   return(0);
  193.   }
  194. if (!dictionary)
  195.   {
  196.   if (!(dictionary = fopen(filename, "r")))
  197.     {
  198.     return(DICT_ERROR);
  199.     }
  200.   }
  201. if (dictionary)
  202.   {
  203.   while ( 1 )
  204.     {
  205.     temp = fgetc(dictionary);
  206.     if (feof(dictionary))
  207.       {
  208.       if (gotone)
  209.         {
  210.         commandword[counter] = 0;
  211.         return(0);
  212.         }
  213.       else
  214.         {
  215.         return(DICT_DONE);
  216.         }
  217.       }
  218.     if (temp > 31 && temp < 127)
  219.       {
  220.       if (gotone == FALSE)
  221.         {
  222.         gotone = TRUE;
  223.         }
  224.       if (counter < WORDSIZE)
  225.         {
  226.         commandword[counter++] = temp;
  227.         }
  228.       }
  229.     else
  230.       {
  231.       if (gotone == TRUE)
  232.         {
  233.         commandword[counter] = 0;
  234.         return(0);
  235.         }
  236.       }
  237.     }
  238.   }
  239. }
  240.     
  241. void cleanup(struct passwd *firstitem)
  242. {
  243. struct passwd *currentitem;
  244. while (firstitem != NULL)
  245.   {
  246.   currentitem = firstitem->nextitem;
  247.   FreeMem(firstitem, sizeof(struct passwd));
  248.   firstitem = currentitem;
  249.   }
  250. getnextdict(NULL,NULL);
  251. exit(0);
  252. }
  253.   
  254. void log (char *filename, char *username, char *password)
  255. {
  256. FILE *output;
  257.  
  258. if (output = fopen(filename, "a"))
  259.   {
  260.   fprintf(output, "%s %s\n", username, password);
  261.   fclose(output);
  262.   }
  263. else
  264.   {
  265.   printf("Error: Unable to open output file!\n");
  266.   }
  267. }
  268.  
  269. struct passwd *parsepass(char *filename)
  270. {
  271. struct passwd *currentitem=NULL;
  272. struct passwd *firstitem=NULL;
  273. unsigned int counter = 0;
  274. unsigned int state = USER;
  275. unsigned int ff = FALSE;
  276. FILE *input;
  277. char temp;
  278.  
  279.  
  280. if (input = fopen(filename, "r"))
  281.   {
  282.   while ( 1 )
  283.     {
  284.     temp = fgetc(input);
  285.     if (feof(input)) break;
  286.     if (temp > 31 && temp < 127)
  287.       {
  288.       if (ff == FALSE)
  289.         {
  290.         ff = TRUE;
  291.         if (!firstitem)
  292.           {
  293.           if (firstitem = (struct passwd *)AllocMem(sizeof(struct passwd), MEMF_CLEAR))
  294.             {
  295.             currentitem = firstitem;
  296.             state = USER;
  297.             counter = 0;
  298.             }
  299.           else
  300.             {
  301.             printf("Error: Can't allocate %d bytes..\n", sizeof(struct passwd));
  302.             break;
  303.             }
  304.           }
  305.         else
  306.           {
  307.           if (!(currentitem->nextitem = (struct passwd *)AllocMem(sizeof(struct passwd), MEMF_CLEAR)))
  308.             {
  309.             printf("Error: Can't allocate %d bytes.\n", sizeof(struct passwd));
  310.             break;
  311.             }
  312.           currentitem = currentitem->nextitem;
  313.           state = USER;
  314.           counter = 0;
  315.           }
  316.         }
  317.       if (state == USER)
  318.         {
  319.         if (temp != ':' && counter < NAMESIZE)
  320.           {
  321.           currentitem->name[counter++] = temp;
  322.           }
  323.         if (temp == ':')
  324.           {
  325.           state = PASSWD;
  326.           counter = 0;
  327.           }
  328.         }
  329.       else if (state == PASSWD)
  330.         {
  331.         if (temp != ':' && counter < 2)
  332.           {
  333.           currentitem->salt[counter] = temp;
  334.           }
  335.         if (counter == 2)
  336.           {
  337.           currentitem->salt[counter] = 0;
  338.           }
  339.         if (temp != ':' && counter < PASSWDSIZE)
  340.           {
  341.           currentitem->password[counter++] = temp;
  342.           }
  343.         if (temp == ':')
  344.           {
  345.           state = DONE;
  346.           counter = 0;
  347.           }
  348.         }
  349.       }
  350.     else
  351.       {
  352.       state = USER;
  353.       ff = FALSE;
  354.       }
  355.     }
  356.   }
  357. else
  358.   {
  359.   printf("Error: Unable to open specified passwd file.\n");
  360.   }
  361. return(firstitem);
  362. }
  363.