Debian Codegraph @

rsakeyfind.cpp

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 // RSAKeyFinder 1.0 (2008-07-18)cxx . . $ `<file>/rsakeyfind.cpp`/
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 errcxx . . $ `rsakeyfind.cpp:18:9`!(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* ParseNextcxx . . $ ParseNext(931b4ec0a3c350fd).(unsigned char* start, unsigned int &type,
33 unsigned int &len) {
34 unsigned char *val = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
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 OutputBytescxx . . $ OutputBytes(498857f3395ec710).(unsigned char* buffer, int len, stringcxx . . $ std/string#& output) {
51 for (int i=0; i<len; i++) {
52 char tmp[4];
53 snprintf(tmp, sizeof(tmp), "%02x ",buffer[i]);
54 output +=cxx . . $ std/__cxx11/basic_string#`operator+=`(978ca1133b5f6e74). tmp;
55 if ((i < len-1) && i % 16 == 15) output +=cxx . . $ std/__cxx11/basic_string#`operator+=`(978ca1133b5f6e74). "\n";
56 }
57 output +=cxx . . $ std/__cxx11/basic_string#`operator+=`(978ca1133b5f6e74). "\n";
58 }
59
60 // Field names in private and public keys
61 stringcxx . . $ std/string# private_fieldscxx . . $ private_fields.[10] = {"version", "modulus", "publicExponent",
62 "privateExponent","prime1","prime2",
63 "exponent1", "exponent2","coefficient", ""};
64 stringcxx . . $ std/string# public_fieldscxx . . $ 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 PrintFieldscxx . . $ PrintFields(446e5313bced872c).(unsigned char* start, stringcxx . . $ std/string#* fields, stringcxx . . $ std/string# &output) {
70 unsigned int len=0, type;
71 output =cxx . . $ std/__cxx11/basic_string#`operator=`(978ca1133b5f6e74). "";
72 start = ParseNextcxx . . $ ParseNext(931b4ec0a3c350fd).(start,type,len); // skip sequence field
73 for (; *fields !=cxx . . $ std/`operator!=`(4b17aa87c9f300c6). ""; fields++) {
74 start = ParseNextcxx . . $ ParseNext(931b4ec0a3c350fd).(start,type,len);
75 if (start == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`! || len == 0 || len > 1000)
76 return false;
77 output +=cxx . . $ std/__cxx11/basic_string#`operator+=`(c0b47be1fddc0e75). *fields +cxx . . $ std/operator+(40fc1db9226233c6). " = \n";
78 OutputBytescxx . . $ OutputBytes(498857f3395ec710).(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 *FindKeyStartcxx . . $ FindKeyStart(9d0786fb5198b4b4).(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 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
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 FindKeyscxx . . $ FindKeys(c3162e65e7b49979).(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 = FindKeyStartcxx . . $ FindKeyStart(9d0786fb5198b4b4).(image, i);
103 if (!key)
104 continue;
105
106 stringcxx . . $ std/string# outputcxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391).;
107 if (PrintFieldscxx . . $ PrintFields(446e5313bced872c).(key,private_fieldscxx . . $ private_fields.,output)) {
108 printf("FOUND PRIVATE KEY AT %x\n", (unsigned int)(key-image));
109 coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(88df5bef833f7f83). output <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\n";
110 } else if (find_public &&
111 PrintFieldscxx . . $ PrintFields(446e5313bced872c).(key,public_fieldscxx . . $ public_fields.,output)) {
112 printf("FOUND PUBLIC KEY AT %x\n", (unsigned int)(key-image));
113 coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(88df5bef833f7f83). output <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\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 *MapFilecxx . . $ MapFile(b324cf788d7d7d6e).(char *filename, unsigned int &len) {
121 int fd = open(filename, O_RDONLYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:43:9`!);
122 if (fd < 0)
123 errcxx . . $ `rsakeyfind.cpp:18:9`!(1, "image open failed");
124
125 struct stat st;
126 if (fstat(fd, &st) != 0)
127 errcxx . . $ `rsakeyfind.cpp:18:9`!(1, "image fstat failed");
128
129 unsigned char *map;
130 map = (unsigned char*)mmap(0, st.st_size, PROT_READcxx . . $ `<external>/17226767460805440860/mman-linux.h:32:9`!, MAP_SHAREDcxx . . $ `<external>/17226767460805440860/mman-linux.h:42:9`!, fd, 0);
131 if (map == MAP_FAILEDcxx . . $ `<external>/6724834311684550606/mman.h:44:9`!)
132 errcxx . . $ `rsakeyfind.cpp:18:9`!(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 GetHexBytecxx . . $ GetHexByte(7864480464b09eea).(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 strtolcxx . . $ strtol(8cc4c7acb13e570c).(a,NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!,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 *ReadModuluscxx . . $ ReadModulus(b324cf788d7d7d6e).(char *filename, unsigned int &len) {
158 int fd = open(filename, O_RDONLYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:43:9`!);
159 if (fd < 0)
160 errcxx . . $ `rsakeyfind.cpp:18:9`!(1, "modulus open failed");
161
162 struct stat st;
163 if (fstat(fd, &st) != 0)
164 errcxx . . $ `rsakeyfind.cpp:18:9`!(1, "modulus fstat failed");
165
166 unsigned char *modulus = new unsigned char[st.st_size];
167 for (len=0; ;len++) {
168 int c = GetHexBytecxx . . $ GetHexByte(7864480464b09eea).(fd);
169 if (c == -1)
170 break;
171 modulus[len] = c;
172 }
173
174 close(fd);
175 return modulus;
176 }
177
178 void Usagecxx . . $ Usage(49f6e7a06ebc5aa8).() {
179 fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "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 maincxx . . $ main(afcd3168fc1188a4).(int argc, char *argv[]) {
186 if (argc < 2 || argc > 3) {
187 Usagecxx . . $ Usage(49f6e7a06ebc5aa8).();
188 exitcxx . . $ exit(44ed87f96534fad7).(1);
189 }
190
191 unsigned int ilen;
192 unsigned char *image = MapFilecxx . . $ MapFile(b324cf788d7d7d6e).(argv[1], ilen);
193 if (argc == 3) {
194 // method 1: searching for modulus
195 unsigned int mlen;
196 unsigned char *modulus = ReadModuluscxx . . $ ReadModulus(b324cf788d7d7d6e).(argv[2], mlen);
197 FindKeyscxx . . $ FindKeys(c3162e65e7b49979).(image, ilen, modulus, mlen, true);
198 } else {
199 // method 2: searching for versionmarker
200 unsigned char versionmarker[4] = {0x02, 0x01, 0x00, 0x02};
201 FindKeyscxx . . $ FindKeys(c3162e65e7b49979).(image, ilen, versionmarker, sizeof(versionmarker), false);
202 }
203
204 return 0;
205 }