forked from idkwim/dma_unlocker
/
dma_crack.cpp
212 lines (192 loc) · 6.34 KB
/
dma_crack.cpp
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "dma_crack.h"
#include "keygen/keygen.h"
#include "decryptor/aes256.h"
#include "decryptor/decryptor.h"
#include "util.h"
#include "filetypes/TypeValidator.h"
//#define DEBUG 0
void log_key(uint32_t seed, char *key, int key_num, std::string filename)
{
FILE* f_out = fopen("key_out.txt", "a");
if (!f_out) return;
fprintf (f_out, "File: %s\n", filename.c_str());
fprintf (f_out, "KEY: %s\n", key);
fprintf (f_out, "INIT %x = %d (num: %d)\n", seed, seed, key_num);
fprintf (f_out, "---\n");
fflush(f_out);
fclose(f_out);
}
std::string search_key(Params ¶ms,
TypeValidator *validator,
size_t series_min,
size_t series_max
)
{
if (validator == NULL) {
printf("Validator not found!\n");
return "";
}
size_t days = 0;
int day_start = params.seed;
int deadline = 2; //2 days max
//---
int seed = params.seed;
char key[0x100];
printf("Searching key started...\n");
#ifdef DEBUG
printf("Start seed: %d = %#x\n----------\n", seed, seed);
print_time_str(seed);
#endif
if (series_min != series_max) {
#ifdef DEBUG
printf("Smart search mode: ON!\nWarning: it works only if the file have a valid modification timestamp!\n");
printf("Series min = %d , max = %d\n----------\n", series_min, series_max);
#endif
} else {
#ifdef DEBUG
printf("Smart search mode: OFF!\n");
printf("Series min = %d , max = %d\n----------\n", series_min, series_max);
#endif
}
size_t series = series_min;
while (deadline > 0) {
srand (seed);
for (size_t key_num = 0; key_num < series; key_num++) {
make_random_key(key, sizeof(key));
if (validator->testKey(key)) {
if (validator->getAccuracy() >= PIVOT_MIN) {
#ifdef DEBUG
printf("Adjusting seed to to found one!\n");
#endif
params.seed = seed;
params.key_num = key_num;
}
printf(">> KEY FOUND: %s\n", key);
printf("[SUCCESS]\n");
#ifdef DEBUG
printf ("KEY: %s\nSEED %x = %d\nkey number in series: %d\n", key, seed, seed, key_num);
#endif
log_key(seed, key, key_num, params.filename);
return key;
}
}
if (params.incrementalMode) {
seed++;
} else {
seed--;
if (series < series_max) {
//max number of encrypted files per milisecons
series += series_min;
}
}
if (abs(day_start - seed) > DAY_LEN) {
day_start = seed;
days++;
deadline--;
printf("%d day passed!\n", days);
}
}
return "";
}
std::string search_key_in_series(uint32_t seed, TypeValidator *validator, unsigned char *enc_buf)
{
if (validator == NULL) {
printf("Validator not found!\n");
return "";
}
//single series without incrementing seed
char key[0x100];
printf("Searching key started...\n");
#ifdef DEBUG
printf("Start seed: %d = %#x\n----------\n", seed, seed);
print_time_str(seed);
#endif
srand (seed);
for (size_t key_num = 0; key_num < 10000000; key_num++) {
make_random_key(key, sizeof(key));
if (validator->testKey(key)) {
printf("> KEY FOUND: %s\n", key);
printf("[SUCCESS]\n");
#ifdef DEBUG
printf ("KEY: %s\nSEED %x = %d\nkey number in series: %d\n", key, seed, seed, key_num);
#endif
//log_key(seed, key, key_num, params.filename);
return key;
}
}
return "";
}
TypeValidator* makeValidator(FileType *my_type)
{
if (my_type == NULL) return NULL;
if (my_type->group == T_GROUP::HEADER) {
#ifdef DEBUG
printf("Making Header validator\n");
#endif
return new HeaderValidator();
}
if (my_type->group == T_GROUP::MULTI) {
#ifdef DEBUG
printf("Making MULTI validator\n");
#endif
return new MultiValidator();
}
if (my_type->extension == ".bmp") {
#ifdef DEBUG
printf ("BMP: Special validator required!\n");
#endif
return new BMPValidator();
}
return NULL;
}
bool decrypt_file(FileTypesSet &fileTypes, Params ¶ms, std::string enc_filename)
{
//get extension:
std::string extension = get_extension(enc_filename);
FileType *my_type = fileTypes.getType(extension);
if (my_type == NULL) {
printf("[ERROR] Not supported file type! Add a header for %s into '%s' folder!\n", extension.c_str(), HDRS_DIR);
return false;
}
TypeValidator *validator = makeValidator(my_type);
if (validator == NULL) {
printf("[ERROR] Not suppeoted File Type: %s\n", extension.c_str());
return false;
}
validator->init(enc_filename, my_type);
printf("Extension: %s\n", extension.c_str());
//crack it!
bool isDecrypted = false;
std::string key = search_key(params, validator, params.series_min, params.series_max);
if (key.length() == DmaDecryptor::KEY_LEN ) {
DmaDecryptor decr(enc_filename, key);
decr.init();
isDecrypted = decr.decrypt(DmaDecryptor::makeOutName(enc_filename.c_str()));
}
delete validator;
return isDecrypted;
}
bool decrypt_file(FileTypesSet &fileTypes, Params ¶ms, std::string enc_filename, std::string &key)
{
//get extension:
std::string extension = get_extension(enc_filename);
FileType *my_type = fileTypes.getType(extension);
if (my_type == NULL) {
printf("[ERROR] Not supported file type! Add a header for %s into '%s' folder!\n", extension.c_str(), HDRS_DIR);
return false;
}
TypeValidator *validator = makeValidator(my_type);
if (validator == NULL) {
printf("[ERROR] Not suppeoted File Type\n");
return false;
}
validator->init(enc_filename, my_type);
//crack it!
bool isDecrypted = false;
if (validator->testKey(key)) {
DmaDecryptor decr(enc_filename, key);
decr.init();
isDecrypted = decr.decrypt(DmaDecryptor::makeOutName(enc_filename.c_str()));
}
return isDecrypted;
}