Advertisement
aestu

crackpkcs12.c

May 8th, 2011
1,142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.13 KB | None | 0 0
  1. /* This program is free software: you can redistribute it and/or modify
  2. *  it under the terms of the GNU General Public License as published by
  3. *  the Free Software Foundation, either version 3 of the License, or
  4. *  (at your option) any later version.
  5. *
  6. *  This program is distributed in the hope that it will be useful,
  7. *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  9. *  GNU General Public License for more details.
  10. *
  11. *  You should have received a copy of the GNU General Public License
  12. *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  13. */
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <unistd.h>
  19. #include <errno.h>
  20. #include <openssl/crypto.h>
  21. #include <openssl/bio.h>
  22. #include <openssl/pkcs12.h>
  23.  
  24. #define DEFAULTMSGINTERVAL 10000;
  25. #define MINARGNUMBER 4
  26.  
  27. void usage() {
  28.     printf(
  29. "\nUsage: crackpkcs12 -d <dictionary_file> [ -v ] [ -s <message_interval> ] <file_to_crack>\n"
  30. "\n"
  31. "  -d <dictionary_file>     Specify dictionary file path\n"
  32. "  -v                       Verbose mode\n"
  33. "  -s <message_inteval>     Number of attemps between messages (implied -v)\n\n"
  34.     );
  35.     exit(100);
  36. }
  37.  
  38. int main(int argc, char** argv) {
  39.  
  40.     if (argc < MINARGNUMBER) usage();
  41.  
  42.     char *psw, *infile, *dict, *nt, *msgintstring, c, verbose;
  43.     int msginterval = DEFAULTMSGINTERVAL;    
  44.     verbose = 0;
  45.     msgintstring = NULL;
  46.     dict = NULL;
  47.     infile = NULL;
  48.  
  49.     while ((c = getopt (argc, argv, "d:vs:")) != -1)
  50.         switch (c) {
  51.             case 'd':
  52.                 dict = optarg;
  53.                 break;
  54.             case 'v':
  55.                 verbose = 1;
  56.                 break;
  57.             case 's':
  58.                 verbose = 1;
  59.                 msgintstring = optarg;
  60.                 break;
  61.             case '?':
  62.                 if (optopt == 'd' || optopt == 's') {
  63.                     fprintf (stderr, "Option -%c requires an argument.\n", optopt);
  64.                 }
  65.             default:
  66.                 usage();
  67.         }
  68.  
  69.     if (optind != argc-1)
  70.         usage();
  71.     else
  72.         infile = argv[optind];
  73.  
  74.     if (dict == NULL) {
  75.         fprintf(stderr,"Error: No dictionary file specified\n\n");
  76.         usage();
  77.     }
  78.  
  79.     if (msgintstring != NULL) {
  80.         msginterval = strtol(msgintstring, NULL, 10);
  81.         if (errno == EINVAL)
  82.             usage();
  83.     }
  84.     else if (verbose == 1)
  85.         msginterval = DEFAULTMSGINTERVAL;
  86.  
  87.     // Opening p12 file  
  88.     BIO* in = NULL;
  89.     in = BIO_new_file(infile, "rb");
  90.     if (!in) {
  91.         perror("P12 file not found\n");
  92.         exit(10);
  93.     }
  94.  
  95.     // Creating PKCS12 object
  96.     PKCS12 *p12 = NULL;
  97.     if (!(p12 = d2i_PKCS12_bio (in, NULL))) {
  98.         perror("p12 not created\n");
  99.         exit(30);      
  100.     }
  101.  
  102.     // Opening dictionary file
  103.     FILE *file = fopen(dict,"r");
  104.     if (!file) {
  105.         perror("Dictionary file not found\n");
  106.         exit(20);
  107.     }
  108.  
  109.     OpenSSL_add_all_algorithms();
  110.  
  111.     char line[256];
  112.     char found = 0;
  113.     int count = 0;
  114.     while (fgets ( line, sizeof line, file ) != NULL) {
  115.         if (line[strlen(line) - 1] == '\n')
  116.             line[strlen(line) - 1] = '\0';      
  117.         if (strlen(line) > 0 && line[strlen(line) - 1] == '\r')
  118.             line[strlen(line) - 1] = '\0';
  119.         if (verbose == 1) {        
  120.             count++;
  121.             if (count % msginterval==0)
  122.                 printf("Attemp %d (%s)\n",count,line);
  123.         }
  124.         if (PKCS12_verify_mac(p12, line, -1)) {
  125.             found = 1;          
  126.             break;
  127.         }
  128.     }
  129.  
  130.  
  131.     if (found) {
  132.         if (verbose == 1) printf("\n********************************************\n");        
  133.         printf("Password found: %s\n",line);
  134.         if (verbose == 1) printf("********************************************\n\n");      
  135.     }
  136.     else {
  137.         if (verbose == 1) printf("\n********************************************\n");        
  138.         printf("No password found\n");
  139.         if (verbose == 1) printf("********************************************\n\n");      
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement