2019.01
[oweals/thc-archive.git] / Tools / thc_ssh_crack.c
1 /*
2  * THC/2003
3  *
4  * Simple ssh-private key cracker. Tries to brute force (dictionary
5  * attack) almost any ssh private key file format.
6  *
7  * This is just a quick tool from THC. Using OpenSSL is not really
8  * fast...
9  *
10  * COMPILE:
11  *     gcc -Wall -O2 -o thc-ssh-crack thc-ssh-crack.c -lssl
12  *
13  * RUN:
14  * John is a good password generator. We use it for thc-ssh-crack:
15  * 
16  * $ john -stdout -incremental | nice -19 thc-ssh-crack id_dsa
17  *
18  * Normal dictionary (without john's permutation engine):
19  *
20  * $ nice -19 thc-ssh-crack id_dsa <dictionary.txt
21  *
22  * Enjoy,
23  *
24  * http://www.thc.org
25  */
26 #include <stdio.h>
27 #include <openssl/ssl.h>
28 #include <openssl/err.h>
29 #include <openssl/evp.h>
30 #include <openssl/pem.h>
31 #include <string.h>
32
33 int
34 main(int argc, char *argv[])
35 {
36         FILE *fp = fopen(argv[1], "r");
37         EVP_PKEY *pk;
38         char *ptr;
39         char pwd[1024];
40
41         SSL_library_init();
42         pwd[0] = '\0';
43         while (1)
44         {
45                 if (!fgets(pwd, sizeof pwd, stdin))
46                 {
47                         printf("Password not found.\n");
48                         exit(0);
49                 }
50                 ptr = strchr(pwd, '\n');
51                 if (ptr)
52                         *ptr = '\0';
53                 pk = PEM_read_PrivateKey(fp, NULL, NULL, (char *)pwd);
54                 if (pk)
55                 {
56                         printf("THC THC THC THC THC THC THC THC THC\n");
57                         printf("----> pwd is '%s' <-----\n", pwd);
58                         printf("THC THC THC THC THC THC THC THC THC\n");
59                         exit(0);
60                 }
61         }
62
63         return 0;
64 }
65
66