-
Notifications
You must be signed in to change notification settings - Fork 7
/
PfsPageMapper.cpp
438 lines (356 loc) · 14.9 KB
/
PfsPageMapper.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#include "PfsPageMapper.h"
#include "SecretGenerator.h"
#include "UnicvDbParser.h"
#include "FilesDbParser.h"
PfsPageMapper::PfsPageMapper(std::shared_ptr<ICryptoOperations> cryptops, std::shared_ptr<IF00DKeyEncryptor> iF00D, std::ostream& output, const unsigned char* klicensee, const psvpfs::path& titleIdPath)
: m_cryptops(cryptops), m_iF00D(iF00D), m_output(output), m_titleIdPath(titleIdPath)
{
memcpy(m_klicensee, klicensee, 0x10);
}
std::shared_ptr<sce_junction> PfsPageMapper::brutforce_hashes(const std::unique_ptr<FilesDbParser>& filesDbParser, std::map<sce_junction, std::vector<std::uint8_t>>& fileDatas, const unsigned char* secret, const unsigned char* signature) const
{
const sce_ng_pfs_header_t& ngpfs = filesDbParser->get_header();
unsigned char signature_key[0x14] = {0};
if(img_spec_to_is_unicv(ngpfs.image_spec))
{
//we will be checking only first sector of each file hence we can precalculate a signature_key
//because both secret and sector_salt will not vary
int sector_salt = 0; //sector number is most likely a salt which is logically correct in terms of xts-aes
m_cryptops->hmac_sha1((unsigned char*)§or_salt, signature_key, 4, secret, 0x14);
}
else
{
//for icv files sector salt is not used. salt is global and is specified in the name of the file
//this means that we can just use secret as is
memcpy(signature_key, secret, 0x14);
}
//go through each first sector of the file
for(auto& f : fileDatas)
{
//calculate sector signature
unsigned char realSignature[0x14] = {0};
m_cryptops->hmac_sha1(f.second.data(), realSignature, f.second.size(), signature_key, 0x14);
//try to match the signatures
if(memcmp(signature, realSignature, 0x14) == 0)
{
std::shared_ptr<sce_junction> found_path(new sce_junction(f.first));
//remove newly found path from the search list to reduce time with each next iteration
fileDatas.erase(f.first);
return found_path;
}
}
return std::shared_ptr<sce_junction>();
}
//this is a tree walker function and it should not be a part of the class
int find_zero_sector_index(std::shared_ptr<merkle_tree_node<icv> > node, void* ctx)
{
std::pair<std::uint32_t, std::uint32_t>* ctx_pair = (std::pair<std::uint32_t, std::uint32_t>*)ctx;
if(node->isLeaf())
{
if(node->m_index == 0)
{
ctx_pair->second = ctx_pair->first; //save global counter to result
return -1;
}
else
{
ctx_pair->first++; //increase global counter
return 0;
}
}
else
{
ctx_pair->first++; //increase global counter
return 0;
}
}
//this is a tree walker function and it should not be a part of the class
int assign_hash(std::shared_ptr<merkle_tree_node<icv> > node, void* ctx)
{
if(!node->isLeaf())
return 0;
std::map<std::uint32_t, icv>* sectorHashMap = (std::map<std::uint32_t, icv>*)ctx;
auto item = sectorHashMap->find(node->m_index);
if(item == sectorHashMap->end())
throw std::runtime_error("Missing sector hash");
node->m_context.m_data.assign(item->second.m_data.begin(), item->second.m_data.end());
return 0;
}
//this is a tree walker function and it should not be a part of the class
int combine_hash(std::shared_ptr<merkle_tree_node<icv> > result, std::shared_ptr<merkle_tree_node<icv> > left, std::shared_ptr<merkle_tree_node<icv> > right, void* ctx)
{
unsigned char bytes28[0x28] = {0};
memcpy(bytes28, left->m_context.m_data.data(), 0x14);
memcpy(bytes28 + 0x14, right->m_context.m_data.data(), 0x14);
std::pair<std::shared_ptr<ICryptoOperations>, unsigned char*>* ctx_cast = (std::pair<std::shared_ptr<ICryptoOperations>, unsigned char*>*)ctx;
std::shared_ptr<ICryptoOperations> cryptops = ctx_cast->first;
unsigned char* secret = ctx_cast->second;
result->m_context.m_data.resize(0x14);
cryptops->hmac_sha1(bytes28, result->m_context.m_data.data(), 0x28, secret, 0x14);
return 0;
}
//this is a tree walker function and it should not be a part of the class
int collect_hash(std::shared_ptr<merkle_tree_node<icv> > node, void* ctx)
{
std::vector<icv>* hashTable = (std::vector<icv>*)ctx;
hashTable->push_back(node->m_context);
return 0;
}
int PfsPageMapper::compare_hash_tables(const std::vector<icv>& left, const std::vector<icv>& right)
{
if(left.size() != right.size())
return -1;
for(std::size_t i = 0; i < left.size(); i++)
{
if(memcmp(left[i].m_data.data(), right[i].m_data.data(), 0x14) != 0)
return -1;
}
return 0;
}
//pageMap - relates icv salt (icv filename) to junction (real file in filesystem)
//merkleTrees - relates icv table entry (icv file) to merkle tree of real file
//idea is to find icv table entry by icv filename - this way we can relate junction to merkle tree
//then we can read the file and hash it into merkle tree
//then merkle tree is collected into hash table
//then hash table is compared to the hash table from icv table entry
int PfsPageMapper::validate_merkle_trees(const std::unique_ptr<FilesDbParser>& filesDbParser, std::vector<std::pair<std::shared_ptr<sce_iftbl_base_t>, std::shared_ptr<merkle_tree<icv> > > >& merkleTrees)
{
const sce_ng_pfs_header_t& ngpfs = filesDbParser->get_header();
m_output << "Validating merkle trees..." << std::endl;
for(auto entry : merkleTrees)
{
//get table
std::shared_ptr<sce_iftbl_base_t> table = entry.first;
//calculate secret
unsigned char secret[0x14];
scePfsUtilGetSecret(m_cryptops, m_iF00D, secret, m_klicensee, ngpfs.files_salt, img_spec_to_crypto_engine_flag(ngpfs.image_spec), table->get_icv_salt(), 0);
//find junction
auto junctionIt = m_pageMap.find(table->get_icv_salt());
if(junctionIt == m_pageMap.end())
{
m_output << "Table item not found in page map" << std::endl;
return -1;
}
const sce_junction& junction = junctionIt->second;
//read junction into sector map
std::ifstream inputStream;
junction.open(inputStream);
std::uint32_t sectorSize = table->get_header()->get_fileSectorSize();
std::uintmax_t fileSize = junction.file_size();
std::uint32_t nSectors = static_cast<std::uint32_t>(fileSize / sectorSize);
std::uint32_t tailSize = fileSize % sectorSize;
std::map<std::uint32_t, icv> sectorHashMap;
std::vector<std::uint8_t> raw_data(sectorSize);
for(std::uint32_t i = 0; i < nSectors; i++)
{
auto currentItem = sectorHashMap.insert(std::make_pair(i, icv()));
icv& currentIcv = currentItem.first->second;
inputStream.read((char*)raw_data.data(), sectorSize);
currentIcv.m_data.resize(0x14);
m_cryptops->hmac_sha1(raw_data.data(), currentIcv.m_data.data(), sectorSize, secret, 0x14);
}
if(tailSize > 0)
{
auto currentItem = sectorHashMap.insert(std::make_pair(nSectors, icv()));
icv& currentIcv = currentItem.first->second;
inputStream.read((char*)raw_data.data(), tailSize);
currentIcv.m_data.resize(0x14);
m_cryptops->hmac_sha1(raw_data.data(), currentIcv.m_data.data(), tailSize, secret, 0x14);
}
try
{
//get merkle tree (it should already be indexed)
std::shared_ptr<merkle_tree<icv> > mkt = entry.second;
//assign hashes to leaves
walk_tree(mkt, assign_hash, §orHashMap);
//calculate node hashes
auto combine_ctx = std::make_pair(m_cryptops, secret);
bottom_top_walk_combine(mkt, combine_hash, &combine_ctx);
//collect hashes into table
std::vector<icv> hashTable;
walk_tree(mkt, collect_hash, &hashTable);
//compare tables
if(compare_hash_tables(hashTable, table->m_blocks.front().m_signatures) < 0)
{
m_output << "Merkle tree is invalid in file " << junction << std::endl;
return -1;
}
m_output << "File: " << std::hex << table->get_icv_salt() << " [OK]" << std::endl;
}
catch(std::runtime_error& e)
{
m_output << e.what() << std::endl;
return -1;
}
}
return 0;
}
//filesDbParser and unicvDbParser are not made part of the context of PfsPageMapper
//the reason is because both filesDbParser and unicvDbParser have to be
//initialized with parse method externally prior to calling bruteforce_map
//having filesDbParser and unicvDbParser as constructor arguments will
//introduce ambiguity in usage of PfsPageMapper
int PfsPageMapper::bruteforce_map(const std::unique_ptr<FilesDbParser>& filesDbParser, const std::unique_ptr<UnicvDbParser>& unicvDbParser)
{
const sce_ng_pfs_header_t& ngpfs = filesDbParser->get_header();
const std::unique_ptr<sce_idb_base_t>& unicv = unicvDbParser->get_idatabase();
if(img_spec_to_is_unicv(ngpfs.image_spec))
m_output << "Building unicv.db -> files.db relation..." << std::endl;
else
m_output << "Building icv.db -> files.db relation..." << std::endl;
psvpfs::path root(m_titleIdPath);
//check file fileSectorSize
std::set<std::uint32_t> fileSectorSizes;
for(auto& t : unicv->m_tables)
{
//skip SCEINULL blocks
if(t->m_blocks.size() > 0)
fileSectorSizes.insert(t->get_header()->get_fileSectorSize());
}
if(fileSectorSizes.size() > 1)
{
m_output << "File sector size is not unique. This bruteforce mode is not supported now" << std::endl;
return -1;
}
std::uint32_t uniqueSectorSize = *fileSectorSizes.begin();
//get all files and directories
std::set<psvpfs::path> files;
std::set<psvpfs::path> directories;
getFileListNoPfs(root, files, directories);
//pre read all the files once
std::map<sce_junction, std::vector<std::uint8_t>> fileDatas;
for(auto& real_file : files)
{
sce_junction sp(real_file);
sp.link_to_real(real_file);
std::uintmax_t fsz = sp.file_size();
// using uniqueSectorSize here.
// in theory this size may vary per SCEIFTBL - this will make bruteforcing a bit harder.
// files can not be pre read in this case
// in practice though it does not change.
std::uintmax_t fsz_limited = (fsz < uniqueSectorSize) ? fsz : uniqueSectorSize;
//empty files should be allowed!
if(fsz_limited == 0)
{
m_output << "File " << sp << " is empty" << std::endl;
m_emptyFiles.insert(sp);
}
else
{
const auto& fdt = fileDatas.insert(std::make_pair(sp, std::vector<std::uint8_t>(static_cast<std::vector<std::uint8_t>::size_type>(fsz_limited))));
std::ifstream in;
if(!sp.open(in))
{
m_output << "Failed to open " << sp << std::endl;
return -1;
}
in.read((char*)fdt.first->second.data(), fsz_limited);
in.close();
}
}
std::vector<std::pair<std::shared_ptr<sce_iftbl_base_t>, std::shared_ptr<merkle_tree<icv> > > > merkleTrees;
//brutforce each sce_iftbl_t record
for(auto& t : unicv->m_tables)
{
//process only files that are not empty
if(t->get_header()->get_numSectors() > 0)
{
//generate secret - one secret per unicv.db page is required
unsigned char secret[0x14];
scePfsUtilGetSecret(m_cryptops, m_iF00D, secret, m_klicensee, ngpfs.files_salt, img_spec_to_crypto_engine_flag(ngpfs.image_spec), t->get_icv_salt(), 0);
std::shared_ptr<sce_junction> found_path;
if(img_spec_to_is_unicv(ngpfs.image_spec))
{
//in unicv - hash table has same order as sectors in a file
const unsigned char* zeroSectorIcv = t->m_blocks.front().m_signatures.front().m_data.data();
//try to find match by hash of zero sector
found_path = brutforce_hashes(filesDbParser, fileDatas, secret, zeroSectorIcv);
}
else
{
try
{
//create merkle tree for corresponding table
std::shared_ptr<merkle_tree<icv> > mkt = generate_merkle_tree<icv>(t->get_header()->get_numSectors());
index_merkle_tree(mkt);
//save merkle tree
merkleTrees.push_back(std::make_pair(t, mkt));
//use merkle tree to find index of zero sector in hash table
std::pair<std::uint32_t, std::uint32_t> ctx;
walk_tree(mkt, find_zero_sector_index, &ctx);
//in icv - hash table is ordered according to merkle tree structure
//that is why it is required to walk through the tree to find zero sector hash in hash table
const unsigned char* zeroSectorIcv = t->m_blocks.front().m_signatures.at(ctx.second).m_data.data();
//try to find match by hash of zero sector
found_path = brutforce_hashes(filesDbParser, fileDatas, secret, zeroSectorIcv);
}
catch(std::runtime_error& e)
{
m_output << e.what() << std::endl;
return -1;
}
}
if(found_path)
{
m_output << "Match found: " << std::hex << t->get_icv_salt() << " " << *found_path << std::endl;
m_pageMap.insert(std::make_pair(t->get_icv_salt(), *found_path));
}
else
{
m_output << "Match not found: " << std::hex << t->get_icv_salt() << std::endl;
return -1;
}
}
}
//in icv - additional step checks that hash table corresponds to merkle tree
if(!img_spec_to_is_unicv(ngpfs.image_spec))
{
if(validate_merkle_trees(filesDbParser, merkleTrees) < 0)
return -1;
}
if(files.size() != (m_pageMap.size() + m_emptyFiles.size()))
{
m_output << "Extra files are left after mapping (warning): " << (files.size() - (m_pageMap.size() + m_emptyFiles.size())) << std::endl;
}
if(fileDatas.size() != 0)
{
for(auto& f : fileDatas)
m_output << f.first << std::endl;
}
return 0;
}
//this is a test method that was supposed to be used for caching
int PfsPageMapper::load_page_map(const psvpfs::path& filepath, std::map<std::uint32_t, std::string>& pageMap) const
{
const auto& fp = filepath;
if(!psvpfs::exists(fp))
{
m_output << "File " << fp.generic_string() << " does not exist" << std::endl;
return -1;
}
std::ifstream in(fp.generic_string().c_str());
if(!in.is_open())
{
m_output << "Failed to open " << fp.generic_string() << std::endl;
return -1;
}
std::string line;
while(std::getline(in, line))
{
int index = line.find(' ');
std::string pageStr = line.substr(0, index);
std::string path = line.substr(index + 1);
std::uint32_t page = std::atoi(pageStr.c_str());
pageMap.insert(std::make_pair(page, path));
}
in.close();
return 0;
}
const std::map<std::uint32_t, sce_junction>& PfsPageMapper::get_pageMap() const
{
return m_pageMap;
}
const std::set<sce_junction>& PfsPageMapper::get_emptyFiles() const
{
return m_emptyFiles;
}