forked from st4rk/PkgDecrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pkg_dec.c
812 lines (723 loc) · 32.4 KB
/
pkg_dec.c
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
/**
* Decrypts PS Vita PKG files
* The code is a total mess, use at your own risk.
* Written by St4rk
* Special thanks to Proxima <3
*/
#include "keyflate.h"
#include "libb64/b64/cdecode.h"
#include "pkg.h"
#include "pkgdb.h"
#include "platform.h"
#include "rif.h"
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define VERSION_MAJOR 1
#define VERSION_MINOR 3
#define VERSION_PATCH 2
#define MIN_KEY_SIZE 512
#define MAX_KEY_SIZE 2048
int decode_license( char *encoded, uint8_t *target ) {
//First check encoded buffer
int deflated = 0;
for ( char *ptr = encoded; *ptr != 0; ptr++ ) {
if ( !( ( *ptr >= '0' && *ptr <= '9' ) || ( *ptr >= 'a' && *ptr <= 'f' ) || ( *ptr >= 'A' && *ptr <= 'F' ) ) ) {
deflated = 1;
break;
}
}
if ( deflated ) {
char buf[MAX_KEY_SIZE];
base64_decodestate state;
base64_init_decodestate( &state );
size_t len = base64_decode_block( encoded, strlen( encoded ), buf, &state );
len = inflateKey( (unsigned char *) buf, len, target, MAX_KEY_SIZE );
if ( len < MIN_KEY_SIZE ) {
return -1;
}
return 0;
} else {
SceNpDrmLicense *lic = (SceNpDrmLicense *) target;
lic->aid = FAKE_AID;
lic->version = __builtin_bswap16( 1 );
lic->version_flag = __builtin_bswap16( 1 );
lic->flags = __builtin_bswap16( 2 );
lic->type = __builtin_bswap16( 1 );
for ( int i = 0; i < 32; i += 2 ) {
char b = encoded[i + 2];
encoded[i + 2] = 0;
lic->key[i >> 1] = strtol( encoded + i, NULL, 16 );
encoded[i + 2] = b;
}
return 1;
}
}
int get_license_type( uint8_t *lic ) {
if ( *( (uint16_t *) ( lic + 4 ) ) == 0 ) {
return 1;
}
return 0;
}
char *strrep( char *text, char *substr, char *subst ) {
char *loc = strstr( text, substr );
if ( loc >= 0 ) {
memmove( loc + strlen( subst ), loc + strlen( substr ), strlen( loc ) - strlen( substr ) + 1 );
memcpy( loc, subst, strlen( subst ) );
}
return loc;
}
int mkdirs( char *path ) {
struct stat st = {0};
if ( stat( path, &st ) == -1 ) {
if ( mkdir( path, 0777 ) < 0 ) {
switch ( errno ) {
case EEXIST:
return 0;
case ENOENT: {
//Create missing parent directories
int p = strlen( path );
while ( p > 0 && path[--p] != PATH_SEPARATOR )
;
if ( p > 0 ) {
char c = path[p];
path[p] = '\0';
if ( mkdirs( path ) == 0 ) {
path[p] = c;
return mkdir( path, 0777 );
} else {
path[p] = c;
return -1;
}
}
break;
}
default:
return -1;
}
}
}
return 0;
}
size_t writeFile( const char *path, const uint8_t *buf, const uint32_t length ) {
FILE *out = fopen( path, "wb" );
if ( out ) {
if ( length > 0 ) {
size_t written = fwrite( buf, sizeof( uint8_t ), length, out );
fclose( out );
return written;
} else {
fclose( out );
return 1;
}
}
return 0;
}
/**
Usage:
pkg_dec [--make-dirs=id|ux] [--license=<encoded_key_or_license>] [--raw] input.pkg [output_directory]
*/
int main( int argc, char **argv ) {
fprintf( stderr, "pkg_dec - PS Vita PKG decryptor/unpacker, version %d.%d.%d.\n", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH );
//Parse arguments
char *input_file = NULL;
char *output_dir = NULL;
char *encoded_license = NULL;
int md_mode = 0;
int raw_mode = 0;
int split_dirs = 0;
int queue_length = 32;
int position = 0;
for ( int i = 1; i < argc; i++ ) {
char *splitp = strchr( argv[i], '=' );
if ( splitp != NULL ) {
if ( strncmp( argv[i], "--make-dirs", splitp - argv[i] ) == 0 ) {
if ( strcmp( splitp, "=id" ) == 0 ) {
md_mode = 1;
} else if ( strcmp( splitp, "=ux" ) == 0 ) {
md_mode = 2;
} else {
printf( "Error: invalid directory creation mode, must be \"ux\" or \"id\"." );
return 1;
}
} else if ( strncmp( argv[i], "--license", splitp - argv[i] ) == 0 ) {
encoded_license = splitp + 1;
} else if (strncmp( argv[i], "--queue-length", splitp - argv[i]) == 0){
long int value = strtol( splitp + 1, NULL, 10 );
if (value <= 0){
fprintf( stderr, "Install queue length must be greater than 0.\n" );
exit( 1 );
}
queue_length = value;
} else
goto positional_arg;
} else {
if ( strcmp( argv[i], "--raw" ) == 0 ) {
raw_mode = 1;
continue;
} else if ( strcmp( argv[i], "--split" ) == 0 ) {
split_dirs = 1;
continue;
}
positional_arg:
switch ( position++ ) {
case 0:
input_file = argv[i];
break;
case 1:
output_dir = argv[i];
break;
default:
printf( "Error: too many arguments." );
return 1;
}
}
}
if ( input_file ) {
PKG_FILE_STREAM *pkg = pkg_open( input_file );
if ( pkg == NULL ) {
if ( errno != 0 ) {
fprintf( stderr, "PKG %s is not a valid Vita PKG file!\n", input_file );
return -1;
} else {
char error[1024];
memset( error, 0, 1024 );
snprintf( error, 1023, "Error unpacking %s", input_file );
perror( error );
return 1;
}
}
if ( output_dir == NULL || strlen( output_dir ) == 0 ) {
output_dir = ".";
}
printf( "Successfully opened %s as PKG file...\n", input_file );
if ( raw_mode ) {
//Just decrypt PKG, don't attempt to parse structures
printf( "Decrypting PKG...\n" );
uint8_t *buf = malloc( 0x10000 );
char *outfile = malloc( 1024 );
strncpy( outfile, output_dir, 1024 );
//First check if output points to an existing directory, use path literally as file output if it could be used this way
struct stat st = {0};
if ( stat( outfile, &st ) == 0 && S_ISDIR( st.st_mode ) ) {
strncat( outfile, PATH_SEPARATOR_STR, 1024 );
strncat( outfile, "plaintext.pkg", 1024 );
}
pkg_seek( pkg, 0 );
FILE *out = fopen( outfile, "wb" );
if ( out ) {
while ( 1 ) {
int read = pkg_read( pkg, buf, 0x10000 );
if ( read > 0 ) {
int written = 0;
while ( written < read )
written += fwrite( buf + written, sizeof( unsigned char ), read - written, out );
} else
break;
}
fclose( out );
} else {
fprintf( stderr, "Can't open output file." );
if ( errno != 0 )
perror( "Error" );
}
free( outfile );
free( buf );
exit( 0 );
}
if ( pkg->sfo_file )
printf( "Package content title: %s\n", psfGetString( pkg->sfo_file, "TITLE" ) );
printf( "Package metatdata:\n\tDRM type: 0x%X\n\tContent type: 0x%X\n\tPackage flags: 0x%X\n",
pkg->metadata.drm_type,
pkg->metadata.content_type,
pkg->metadata.package_flags );
//Determine PKG content type
int is_dlc = 0;
int is_patch = 0;
int is_psm = 0;
switch ( pkg->metadata.content_type ) {
case 0x16:
//DLC content for Vita
is_dlc = 1;
printf( "Package contains PS Vita DLC content, content id %s\n", pkg->header.content_id );
break;
case 0x15:
//Game content
//Check sfo to distinguish between game data and game patch
if ( strcmp( psfGetString( pkg->sfo_file, "CATEGORY" ), "gp" ) == 0 ) {
is_patch = 1;
printf( "Package contains Patch for PS Vita Game, content id %s\n", pkg->header.content_id );
} else {
printf( "Package contains PS Vita Game, content id %s\n", pkg->header.content_id );
}
break;
case 0x18:
//PSM package
is_psm = 1;
printf( "Package contains PSM application, content id %s\n", pkg->header.content_id );
break;
case 0x1f:
printf( "Package contains PS Vita Theme, content id %s\n", pkg->header.content_id );
break;
default:
//Unknown content
printf( "Unknown type of content 0x%x, content id %s\n", pkg->metadata.content_type, pkg->header.content_id );
break;
}
//Decode provided license (if any provided)
if ( encoded_license ) {
uint8_t *ltext = malloc( MAX_KEY_SIZE );
memset( ltext, 0, MAX_KEY_SIZE );
int result = decode_license( encoded_license, ltext );
if ( result < 0 ) {
free( ltext );
encoded_license = NULL;
fprintf( stderr, "Provided license string doesn't encode valid key or zRIF.\n" );
} else if ( result == 0 ) {
//zRIF
encoded_license = (char *) ltext;
//Check content id
if ( get_license_type( encoded_license ) != is_psm ) {
fprintf( stderr, "Incorrect license type provided, %s expected, but got %s.\n", ( is_psm ? "PsmDrm" : "NpDrm" ), ( is_psm ? "NpDrm" : "PsmDrm" ) );
free( ltext );
encoded_license = NULL;
} else if ( is_psm ) {
ScePsmDrmLicense *lic = (ScePsmDrmLicense *) ltext;
if ( strcmp( lic->content_id, pkg->header.content_id ) != 0 ) {
fprintf( stderr, "Provided zRIF is not applicable to specified package.\nPackage content id: %s\nLicense content id: %s\n", pkg->header.content_id, lic->content_id );
fprintf( stderr, "RIF file will not be written.\n" );
free( ltext );
encoded_license = NULL;
} else {
printf( "Successfully decompressed zRIF from provided license string.\n" );
}
} else {
SceNpDrmLicense *lic = (SceNpDrmLicense *) ltext;
if ( strcmp( lic->content_id, pkg->header.content_id ) != 0 ) {
fprintf( stderr, "Provided zRIF is not applicable to specified package.\nPackage content id: %s\nLicense content id: %s\n", pkg->header.content_id, lic->content_id );
fprintf( stderr, "RIF file will not be written.\n" );
free( ltext );
encoded_license = NULL;
} else {
printf( "Successfully decompressed zRIF from provided license string.\n" );
}
}
} else if ( result == 1 ) {
//extracted klicensee - regenerate some flags
if ( is_psm ) {
fprintf( stderr, "Creating PsmDrm license from klicensee is not possible.\n" );
free( ltext );
encoded_license = NULL;
} else {
SceNpDrmLicense *lic = (SceNpDrmLicense *) ltext;
printf( "Regenerating RIF from license string.\n" );
memcpy( lic->content_id, pkg->header.content_id, 0x30 );
if ( pkg->metadata.drm_type == 0x3 || pkg->metadata.drm_type == 0xD ) {
lic->sku_flag = __builtin_bswap32( 0x3 );
printf( "Sku_flag (trial version promote) set.\n" );
}
encoded_license = (char *) ltext;
}
}
}
char *temp = malloc( 1024 );
switch ( md_mode ) {
case 0:
//Direct output to specified folder
break;
case 1:
//Output to the "AAAA00000_00-CONTENTID"
snprintf( temp, 1024, "%s%s%s", output_dir, PATH_SEPARATOR_STR, pkg->header.content_id + 7 );
output_dir = temp;
break;
case 2:
//Make directory hierarchy as found in ux0 on Vita (app/GAME00000, addcont/GAME00000/CONTENTID, etc.)
strncpy( temp, output_dir, 1024 );
strncat( temp, PATH_SEPARATOR_STR, 1024 );
if ( is_dlc ) {
//Placing dlcs in ux0:bgdl/t/########/<GAMEID>/
//Creating d0.pdb, d1.pdb and f0.pdb inside ux0:bgdl/t/########
snprintf( temp, 1024, "%s%sbgdl%st%s", output_dir, PATH_SEPARATOR_STR, PATH_SEPARATOR_STR, PATH_SEPARATOR_STR );
char *sub = strlen( temp ) + temp;
//Check first usable folder in sequence 00000000->99999999
struct stat st = {0};
int next_dir = 1;
int next_slot = 1;
do {
if ( next_slot >= queue_length ) {
if ( split_dirs ) {
snprintf( temp, 1024, "%s%sbgdl_%d%st%s", output_dir, PATH_SEPARATOR_STR, next_dir++, PATH_SEPARATOR_STR, PATH_SEPARATOR_STR );
sub = strlen( temp ) + temp;
next_slot = 1;
} else {
fprintf( stderr, "Error: Too many DLCs in the output directory already!\n" );
exit( 1 );
}
}
snprintf( sub, 600, "%08x", next_slot++ );
} while ( stat( temp, &st ) != -1 );
//Warn user if we were forced to create another output directory and redirect content
if ( next_dir > 1 )
fprintf( stderr, "Too many DLCs in the original output directory, placing new in the %s\n", temp );
//Put DLC data in the game id folder
sub = strlen( temp ) + temp;
snprintf( sub, 600, "%s%s", PATH_SEPARATOR_STR, psfGetString( pkg->sfo_file, "TITLE_ID" ) );
} else {
pkg->header.content_id[16] = '\0';
snprintf( temp, 1024, "%s%s%s%s%s",
output_dir, PATH_SEPARATOR_STR,
( is_patch ? "patch" : ( is_psm ? "psm" : "app" ) ), PATH_SEPARATOR_STR,
pkg->header.content_id + 7 );
pkg->header.content_id[16] = '_';
}
output_dir = temp;
break;
}
//Make directories according to PKG content type
if ( strcmp( output_dir, "." ) != 0 ) {
if ( mkdirs( output_dir ) < 0 ) {
fprintf( stderr, "Can't create output directory %s.\n", output_dir );
if ( errno != 0 )
perror( "Error" );
exit( 1 );
}
}
//Create sce_sys/package directory in the output, so our spoils aren't lost in space-time
char *tpath = malloc( 1024 );
snprintf( tpath, 1024, "%s%s%s%s%s", output_dir, PATH_SEPARATOR_STR, "sce_sys", PATH_SEPARATOR_STR, "package" );
if ( mkdirs( tpath ) < 0 ) {
fprintf( stderr, "Can't create directory %s.\n", tpath );
if ( errno != 0 )
perror( "Error" );
exit( 1 );
}
//Read index table
uint8_t *index_table = malloc( pkg->metadata.index_table_size );
pkg_seek( pkg, pkg->header.data_offset + pkg->metadata.index_table_offset );
int read = pkg_read( pkg, index_table, pkg->metadata.index_table_size );
//Decrypt and unpack all the files
PKG_ITEM_RECORD *filerec = (PKG_ITEM_RECORD *) index_table;
uint32_t record_count = pkg->header.item_count;
printf( "Extracting %u records to %s...\n", record_count, output_dir );
char *t2 = malloc( 1024 );
while ( record_count > 0 ) {
#if ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ )
filerec->filename_offset = __builtin_bswap32( filerec->filename_offset );
filerec->filename_size = __builtin_bswap32( filerec->filename_size );
filerec->data_offset = __builtin_bswap64( filerec->data_offset );
filerec->data_size = __builtin_bswap64( filerec->data_size );
filerec->flags = __builtin_bswap32( filerec->flags );
#endif
switch ( filerec->flags & 0xff ) {
case 4:
case 18: {
//Construct output path
memcpy( t2, index_table + filerec->filename_offset - pkg->metadata.index_table_offset, filerec->filename_size );
t2[filerec->filename_size] = '\0';
if ( is_psm && md_mode == 2 ) strrep( t2, "contents", "RO" );
snprintf( tpath, 1024, "%s%s%s", output_dir, PATH_SEPARATOR_STR, t2 );
convertPath( tpath );
if ( mkdirs( tpath ) < 0 ) {
fprintf( stderr, "Can't create directory %s.\n", tpath );
if ( errno != 0 )
perror( "Error" );
exit( 1 );
} else {
printf( "[%02X] Directory %s\n", filerec->flags & 0xff, tpath );
}
break;
}
case 0:
case 1:
// all regular data files have this type
case 3:
// user-mode executables have this type (eboot.bin, sce_modules contents)
case 14:
case 15:
// keystone have this type
case 16:
// PFS files have this type (files.db, unicv.db, pflist)
case 17:
// temp.bin have this type
case 19:
case 20:
// clearsign have this type
case 21:
// right.suprx have this type
case 22: {
//Construct output path
decrypt_regular_file:
memcpy( t2, index_table + filerec->filename_offset - pkg->metadata.index_table_offset, filerec->filename_size );
t2[filerec->filename_size] = '\0';
if ( is_psm && md_mode == 2 ) strrep( t2, "contents", "RO" );
snprintf( tpath, 1024, "%s%s%s", output_dir, PATH_SEPARATOR_STR, t2 );
convertPath( tpath );
//Mark as requiring decryption
filerec->reserved = 0;
continue_decrypt:
//Unpack output file
pkg_seek( pkg, filerec->data_offset + pkg->header.data_offset );
printf( "[%02X] File %s, size %llu\n", filerec->flags & 0xff, tpath, filerec->data_size );
FILE *temp = fopen( tpath, "wb" );
/** Read data in 64kb chunks */
uint8_t *data = (unsigned char *) malloc( sizeof( unsigned char ) * 0x10000 );
if ( data ) {
uint64_t left = filerec->data_size;
while ( left > 0 ) {
/** read file data */
size_t required = ulmin( left, 0x10000 );
int read = 0;
if ( filerec->reserved )
read = fread( data, sizeof( unsigned char ), required, pkg->stream );
else
read = pkg_read( pkg, data, required );
if ( read > 0 ) {
/** write file data */
int written = 0;
while ( written < read )
written += fwrite( data + written, sizeof( unsigned char ), read - written, temp );
left -= read;
} else {
fprintf( stderr, "Out of info to read!! Left %llu\n", left );
break;
}
}
free( data );
} else {
fprintf( stderr, "Failed to allocate output buffer for file unpacking." );
exit( 2 );
}
fclose( temp );
break;
}
// digs.bin have this type, unpack encrypted
case 24: {
//Construct output path
memset( tpath, 0, 1024 );
memcpy( tpath, index_table + filerec->filename_offset - pkg->metadata.index_table_offset, filerec->filename_size );
if ( strstr( tpath, "digs.bin" ) ) {
snprintf( tpath, 1024, "%s%ssce_sys/package/body.bin", output_dir, PATH_SEPARATOR_STR );
convertPath( tpath );
//Using reserved space to mark as encrypted extraction
filerec->reserved = 1;
goto continue_decrypt;
} else {
fprintf( stderr, "Filetype is 0x18, but file is not a digs.bin, decrypting in default mode." );
goto decrypt_regular_file;
}
break;
}
default:
printf( "Unknown record type %d.\n", filerec->flags & 0xff );
break;
}
filerec++;
record_count--;
}
//Output sce_sys/package directory (dump directly, bypassing decryption)
// head.bin (from 0 to pkg->header.data_offset + metadata.index_table_size)
// tail.bin (from pkg->header.data_offset + pkg->header.data_size to EOF)
// body.bin (encrypted digs.bin file data) { for completeness purpose only, file data is probably not the same }
// work.bin (reconstruction from key or or decompressed zRIF)
// temp.bin (available inside the package)
// stat.bin (unknown contents, seems to be not checked)
{
//head.bin
size_t length = pkg->header.data_offset + pkg->metadata.index_table_size;
uint8_t *data = malloc( length );
if ( data ) {
pkg_seek( pkg, 0 );
//Read pkg bypassing automatic decryption
fread( data, 1, length, pkg->stream );
snprintf( tpath, 1024, "%s%s%s%s%s%s%s", output_dir, PATH_SEPARATOR_STR, "sce_sys", PATH_SEPARATOR_STR, "package", PATH_SEPARATOR_STR, "head.bin" );
FILE *headbin = fopen( tpath, "wb" );
if ( headbin ) {
fwrite( data, 1, length, headbin );
fclose( headbin );
printf( "File %s, size %zu\n", tpath, length );
} else {
fprintf( stderr, "Can't write head.bin.\n" );
}
free( data );
} else {
fprintf( stderr, "Can't allocate buffer to write output.\n" );
exit( 2 );
}
//tail.bin
off64_t offset = pkg->header.data_offset + pkg->header.data_size;
length = pkg->header.total_size - offset;
data = malloc( length );
if ( data ) {
pkg_seek( pkg, offset );
//Read pkg bypassing automatic decryption
fread( data, 1, length, pkg->stream );
snprintf( tpath, 1024, "%s%s%s%s%s%s%s", output_dir, PATH_SEPARATOR_STR, "sce_sys", PATH_SEPARATOR_STR, "package", PATH_SEPARATOR_STR, "tail.bin" );
FILE *headbin = fopen( tpath, "wb" );
if ( headbin ) {
fwrite( data, 1, length, headbin );
fclose( headbin );
printf( "File %s, size %zu\n", tpath, length );
} else {
fprintf( stderr, "Can't write tail.bin.\n" );
}
free( data );
} else {
fprintf( stderr, "Can't allocate buffer to write output.\n" );
exit( 2 );
}
//stat.bin
length = 0x300;
data = malloc( length );
memset( data, 0, length );
if ( data ) {
snprintf( tpath, 1024, "%s%s%s%s%s%s%s", output_dir, PATH_SEPARATOR_STR, "sce_sys", PATH_SEPARATOR_STR, "package", PATH_SEPARATOR_STR, "stat.bin" );
FILE *headbin = fopen( tpath, "wb" );
if ( headbin ) {
fwrite( data, 1, length, headbin );
fclose( headbin );
printf( "File %s, size %zu\n", tpath, length );
} else {
fprintf( stderr, "Can't write stat.bin.\n" );
}
free( data );
} else {
fprintf( stderr, "Can't allocate buffer to write output.\n" );
exit( 2 );
}
//work.bin
if ( encoded_license ) {
if ( is_psm ) {
snprintf( tpath, 1024, "%s%s%s%s%s%s%s", output_dir, PATH_SEPARATOR_STR, "RO", PATH_SEPARATOR_STR, "License", PATH_SEPARATOR_STR, "FAKE.rif" );
} else {
//Now DLCs also use standard location in the package folder
snprintf( tpath, 1024, "%s%s%s%s%s%s%s", output_dir, PATH_SEPARATOR_STR, "sce_sys", PATH_SEPARATOR_STR, "package", PATH_SEPARATOR_STR, "work.bin" );
}
char *last = strrchr( tpath, PATH_SEPARATOR );
*last = '\0';
if ( mkdirs( tpath ) < 0 ) {
fprintf( stderr, "Can't create directory %s.\n", tpath );
if ( errno != 0 )
perror( "Error" );
exit( 1 );
}
*last = PATH_SEPARATOR;
length = is_psm ? ScePsmDrmLicenseSize : SceNpDrmLicenseSize;
FILE *workbin = fopen( tpath, "wb" );
if ( workbin ) {
fwrite( encoded_license, 1, length, workbin );
fclose( workbin );
printf( "File %s, size %zu\n", tpath, length );
} else {
fprintf( stderr, "Can't write work.bin.\n" );
}
free( encoded_license );
}
//PDB files for dlcs
if ( is_dlc ) {
uint8_t *pkgdb = malloc( 0x2000 );
if ( pkgdb ) {
uint32_t dblen = pkgdbGenerate( pkgdb, 0x2000,
psfGetString( pkg->sfo_file, "TITLE" ),
psfGetString( pkg->sfo_file, "TITLE_ID" ),
/* TODO basename of pkg */ NULL,
/* TODO pkg url from args */ NULL,
pkg->header.total_size,
is_dlc - 1 );
char *sub;
strcpy( tpath, output_dir );
if ( md_mode == 2 )
sub = strrchr( tpath, PATH_SEPARATOR );
else
sub = tpath + strlen( tpath );
snprintf( sub, 600, "%s%s", PATH_SEPARATOR_STR, "d0.pdb" );
if ( !writeFile( tpath, pkgdb, dblen ) ) {
fprintf( stderr, "Can't write out %s!\n", tpath );
} else
printf( "File %s\n", tpath );
pkgdb[0x20] = 0;
snprintf( sub, 600, "%s%s", PATH_SEPARATOR_STR, "d1.pdb" );
if ( !writeFile( tpath, pkgdb, dblen ) ) {
fprintf( stderr, "Can't write out %s!\n", tpath );
} else
printf( "File %s\n", tpath );
snprintf( sub, 600, "%s%s", PATH_SEPARATOR_STR, "f0.pdb" );
if ( !writeFile( tpath, NULL, 0 ) ) {
fprintf( stderr, "Can't write out %s!\n", tpath );
} else
printf( "File %s\n", tpath );
} else {
fprintf( stderr, "Error: Can't allocate memory to create PDB files.\n" );
}
}
//Additional directories for PSM
if ( is_psm && md_mode == 2 ) {
snprintf( tpath, 1024, "%s%s%s%s%s", output_dir, PATH_SEPARATOR_STR, "RW", PATH_SEPARATOR_STR, "Documents" );
if ( mkdirs( tpath ) < 0 ) {
fprintf( stderr, "Can't create directory %s.\n", tpath );
if ( errno != 0 )
perror( "Error" );
exit( 1 );
}
char *last = strrchr( tpath, PATH_SEPARATOR );
*( last + 1 ) = '\0';
strcat( tpath, "Temp" );
if ( mkdirs( tpath ) < 0 ) {
fprintf( stderr, "Can't create directory %s.\n", tpath );
if ( errno != 0 )
perror( "Error" );
exit( 1 );
}
last = strrchr( tpath, PATH_SEPARATOR );
*( last + 1 ) = '\0';
strcat( tpath, "System" );
if ( mkdirs( tpath ) < 0 ) {
fprintf( stderr, "Can't create directory %s.\n", tpath );
if ( errno != 0 )
perror( "Error" );
exit( 1 );
}
strcat( tpath, PATH_SEPARATOR_STR );
strcat( tpath, "content_id" );
if ( !writeFile( tpath, pkg->header.content_id, 0x30 ) ) {
fprintf( stderr, "Can't write out %s!\n", tpath );
} else {
printf( "File %s\n", tpath );
}
last = strrchr( tpath, PATH_SEPARATOR );
*( last + 1 ) = '\0';
strcat( tpath, "pm.dat" );
data = malloc( 0x10000 );
memset(data, 0, 0x10000);
if ( data ) {
if ( !writeFile( tpath, data, 0x10000 ) ) {
fprintf( stderr, "Can't write out %s!\n", tpath );
} else
printf( "File %s\n", tpath );
free( data );
} else {
fprintf( stderr, "Error: Can't allocate memory to create pm.dat file.\n" );
}
}
}
//Close package read stream
pkg_close( pkg );
//Free allocated resources
free( tpath );
free( temp );
/*
*/
} else {
printf( "PkgDecrypt - tool to decrypt and extract PSVita PKG files.\n" );
printf( "Usage:\n\tpkg_dec [--make-dirs=id|ux] [--license=<key>] [--split] [--queue-length=<int>] [--raw] filename.pkg [output_directory] \nParameters:\n" );
printf( "\t--make-dirs=id|ux\tUse output directory to create special hierarchy,\n\t\t\t\tid\tplaces all output in the <CONTENTID> folder\n\t\t\t\tux\tplaces all output in ux0-style hierarchy\n" );
printf( "\t--license=<key>\t\tProvide key to use as base for work.bin (*.rif) file creation.\n\t\t\t\tTwo formats accepted - klicensee key (deprecated) and zRIF (recommended)\n\t\t\t\tzRIF could be made by NoNpDrm fake RIFs using make_key\n" );
printf( "\t--split\t\t\tCreates new directory to continue installation queue if limit reached\n" );
printf( "\t--queue-length=<int>\tMaximum length of install queue for DLCs to generate, default is 32\n" );
printf( "\t--raw\t\t\tOutput fully decrypted PKG instead of unpacking it, exclusive\n" );
printf( "\t<filename.pkg>\t\tInput PKG file\n" );
printf( "\t<output_directory>\tDirectory where all files will be places. Current directory by default.\n" );
}
return 0;
}