forked from robertdavidgraham/pemcrack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pemcrack.c
148 lines (123 loc) · 3.45 KB
/
pemcrack.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
/*
PEMCRACK
Cracks password encrypted private keys for SSL.
This is really slow, since it's using the high-level OpenSSL
API.
*/
#define _CRT_SECURE_NO_WARNINGS
#include <openssl/ssl.h>
#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/rsa.h>
/*
* Tell Microsoft's compilers to link to OpenSSL, so that the
* makefile doesn't need this explicitly.
*/
#if defined(_MSC_VER)
#pragma comment(lib, "libeay32.lib")
#pragma comment(lib, "ssleay32.lib")
#endif
/*
* Track progress
*/
unsigned progress_count = 0;
/*
* Tests a single password to see if it's the right one. If
* it is, then it prints it on the command line and exits the
* program.
*/
void crackpwd(FILE *fp, char *password)
{
EVP_PKEY *pkey = 0;
void *p;
/* Print regular progress. On a 3-GHz CPU, it's a few
* seconds between updates. Slower CPUs will be slower */
if ((progress_count++ & 0x7F) == 0)
fprintf(stderr, "-> %s\n", password);
/* Seek to the start of the file before trying a password,
* since we are using the high-level OpenSSL API */
fseek(fp, 0, SEEK_SET);
/* Attempt to read the file using the current password */
p = PEM_read_PrivateKey(fp,&pkey, NULL, password);
if (p) {
/* FOUND!!! print and exit the program */
printf("found: %s\n", password);
exit(0);
}
}
/*
* Attempt to brute-force the password trying all
* lower-case letters. Since password cracking is
* so slow, this is of little use
*/
void bruteforce(FILE *fp, char *password, unsigned depth)
{
unsigned i;
for (i=0; i<26; i++) {
password[depth] = 'a' + i;
if (depth)
bruteforce(fp, password, depth-1);
else {
crackpwd(fp, password);
}
}
}
/*
* pemcrack <pem> <dict>
*/
int main(int argc, char *argv[])
{
FILE *fp;
FILE *dict;
char password[64] = {0};
char line[2048];
fprintf(stderr, "--- pemcrack v1.0 - by Robert Graham ----\n");
if (argc <= 1 || 3 < argc) {
fprintf(stderr, "Usage:\n pemcrack <pem> <dict>\n");
exit(1);
}
/* have to add crypto algorithms, or the necessary AES
* code won't be there to decrypt */
OpenSSL_add_all_algorithms();
/* Open the file to read in the PEM file. We leave this file
* open while cracking, seeking back to the beginning each
* time. */
fp = fopen(argv[1], "rb");
if (fp == NULL) {
perror(argv[1]);
exit(1);
}
/*
* If no dictionary file given, then brute-force
*/
if (argc <= 2) {
fprintf(stderr, "no dictionary specified, so brute-forcing...\n");
bruteforce(fp, password, 0);
bruteforce(fp, password, 1);
bruteforce(fp, password, 2);
bruteforce(fp, password, 4);
bruteforce(fp, password, 5);
bruteforce(fp, password, 6);
return 1;
}
/*
* Open dictionary file of possible passwords
*/
dict = fopen(argv[2], "rt");
if (dict == NULL) {
perror(argv[2]);
exit(1);
}
/*
* For each password in the file, attempt to crack it
*/
while (fgets(line, sizeof(line), dict)) {
/* removing trailing whitespace */
while (strlen(line) && isspace(line[strlen(line)-1]))
line[strlen(line)-1] = '\0';
/* attempt to crack; if found, this will exit the program */
crackpwd(fp, line);
}
fprintf(stderr, "NOT FOUND\n");
return 1;
}