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