Debian Codegraph @

rsakeyfind.cpp

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 // RSAKeyFinder 1.0 (2008-07-18)
2 // By Nadia Heninger and J. Alex Halderman
3
4 #include <fcntl.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <fcntl.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <sys/mman.h>
•11 #include <string.h>
12 #include <iostream>
•13 #include <unistd.h>
14
15 #ifdef __FreeBSD__
16 #include <err.h>
17 #else
18 #define err(x,y) { perror(y); exit(x); }
19 #endif
20
21 using namespace std;
22
23 // Baby BER parser, just good enough for RSA keys.
24 //
25 // This is not robust to errors in the memory image, but if we added
26 // some entropy testing and intelligent guessing, it could be made to be.
27 //
28 // Parses a single field of the key, beginning at start. Each field
29 // consists of a type, a length, and a value. Puts the type of field
30 // into type, the number of bytes into len, and returns a pointer to
31 // the beginning of the value.
32 unsigned char* ParseNext(unsigned char* start, unsigned int &type,
33 unsigned int &len) {
34 unsigned char *val = NULL;
35 type = start[0];
36 len = 0;
37 if ((start[1] & 0x80) == 0) {
38 len = start[1];
39 val = &start[2];
40 } else {
41 int lensize = start[1] & 0x7F;
42 for (int i=0; i < lensize; i++)
43 len = (len << 8) | start[2+i];
44 val = &start[2+lensize];
45 }
46 return val;
47 }
48
49 // Sets output to a string displaying len bytes from buffer
50 void OutputBytes(unsigned char* buffer, int len, string& output) {
51 for (int i=0; i<len; i++) {
52 char tmp[4];
53 snprintf(tmp, sizeof(tmp), "%02x ",buffer[i]);
54 output += tmp;
55 if ((i < len-1) && i % 16 == 15) output += "\n";
56 }
57 output += "\n";
58 }
59
60 // Field names in private and public keys
61 string private_fields[10] = {"version", "modulus", "publicExponent",
62 "privateExponent","prime1","prime2",
63 "exponent1", "exponent2","coefficient", ""};
64 string public_fields[3] = {"modulus", "publicExponent", ""};
65
66 // Sets output to a string listing the fields from a BER-encoded
67 // record, beginning at start, with the fields named according to the
68 // array of strings (returns true iff a valid encoding was found)
69 bool PrintFields(unsigned char* start, string* fields, string &output) {
70 unsigned int len=0, type;
71 output = "";
72 start = ParseNext(start,type,len); // skip sequence field
73 for (; *fields != ""; fields++) {
74 start = ParseNext(start,type,len);
75 if (start == NULL || len == 0 || len > 1000)
76 return false;
77 output += *fields + " = \n";
78 OutputBytes(start,len,output);
79 start += len;
80 }
81
82 return true;
83 }
84
85 // Returns a pointer to the beginning of a BER-encoded key by working
86 // backwards from the given memory map offset, looking for the
87 // sequence identifier (this is not completely safe)
88 unsigned char *FindKeyStart(unsigned char *map, int offset) {
89 for (int k = offset; k >= 0 && k > offset-20; k--)
90 if (map[k] == 0x30)
91 return &map[k];
92 return NULL;
93 }
94
95 // Finds and prints private (or private and public) keys in the memory
96 // map by searching for given target pattern
97 void FindKeys(unsigned char *image, int isize, unsigned char *target, int target_size, bool find_public) {
98 for (int i = 0; i < isize - target_size; i++) {
99 if (memcmp(&image[i], target, target_size))
100 continue;
101
102 unsigned char *key = FindKeyStart(image, i);
103 if (!key)
104 continue;
105
106 string output;
107 if (PrintFields(key,private_fields,output)) {
108 printf("FOUND PRIVATE KEY AT %x\n", (unsigned int)(key-image));
109 cout << output << "\n";
110 } else if (find_public &&
111 PrintFields(key,public_fields,output)) {
112 printf("FOUND PUBLIC KEY AT %x\n", (unsigned int)(key-image));
113 cout << output << "\n";
114 }
115 }
116 }
117
118 // Memory maps filename and return a pointer on success, setting len
119 // to the length of the file (does not return on error)
120 unsigned char *MapFile(char *filename, unsigned int &len) {
121 int fd = open(filename, O_RDONLY);
122 if (fd < 0)
123 err(1, "image open failed");
124
125 struct stat st;
126 if (fstat(fd, &st) != 0)
127 err(1, "image fstat failed");
128
129 unsigned char *map;
130 map = (unsigned char*)mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
131 if (map == MAP_FAILED)
132 err(1, "image mmap failed");
133
134 len = st.st_size;
135 return map;
136 }
137
138 // Returns a decoded byte from a file of hex values, ignoring whitespace
139 int GetHexByte(int fd) {
140 for (;;) {
141 char a[3];
142 if (read(fd, &a[0], 1) < 1)
143 break;
144 if ((a[0] >= '0' && a[0] <= '9') || (a[0] >= 'a' && a[0] <= 'f')) {
145 if (read(fd, &a[1], 1) < 1)
146 break;
147 a[2] = '\0';
148 return strtol(a,NULL,16);
149 }
150 }
151 return -1;
152 }
153
154 // Reads hexadecimal bytes from filename and returns a byte array
155 // containing these values, setting len to the number of bytes (does not
156 // return on error)
157 unsigned char *ReadModulus(char *filename, unsigned int &len) {
158 int fd = open(filename, O_RDONLY);
159 if (fd < 0)
160 err(1, "modulus open failed");
161
162 struct stat st;
163 if (fstat(fd, &st) != 0)
164 err(1, "modulus fstat failed");
165
166 unsigned char *modulus = new unsigned char[st.st_size];
167 for (len=0; ;len++) {
168 int c = GetHexByte(fd);
169 if (c == -1)
170 break;
171 modulus[len] = c;
172 }
173
174 close(fd);
175 return modulus;
176 }
177
178 void Usage() {
179 fprintf(stderr, "USAGE: rsakeyfind MEMORY-IMAGE [MODULUS-FILE]\n"
180 "Locates BER-encoded RSA private keys in MEMORY-IMAGE.\n"
181 "If MODULUS-FILE is specified, it will locate private and public keys"
182 "matching the hex-encoded modulus read from this file.\n");
183 }
184
185 int main(int argc, char *argv[]) {
186 if (argc < 2 || argc > 3) {
187 Usage();
188 exit(1);
189 }
190
191 unsigned int ilen;
192 unsigned char *image = MapFile(argv[1], ilen);
193 if (argc == 3) {
194 // method 1: searching for modulus
195 unsigned int mlen;
196 unsigned char *modulus = ReadModulus(argv[2], mlen);
197 FindKeys(image, ilen, modulus, mlen, true);
198 } else {
199 // method 2: searching for versionmarker
200 unsigned char versionmarker[4] = {0x02, 0x01, 0x00, 0x02};
201 FindKeys(image, ilen, versionmarker, sizeof(versionmarker), false);
202 }
203
204 return 0;
205 }