forked from wargio/vitatool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathungpkg.c
191 lines (147 loc) · 3.91 KB
/
ungpkg.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
// Copyright 2010-2011 Sven Peter <svenpeter@gmail.com>
// Licensed under the terms of the GNU GPL, version 2
// http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
// Thanks to Mathieulh for his C# retail unpacker
// (http://twitter.com/#!/Mathieulh/status/23070344881381376)
// Thanks to Matt_P for his python debug unpacker
// (https://github.com/HACKERCHANNEL/PS3Py/blob/master/pkg.py)
// Thanks to deroad for his modification to port this on the vita
#include "tools.h"
#include "types.h"
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#ifdef WIN32
#define MKDIR(x,y) mkdir(x)
#else
#define MKDIR(x,y) mkdir(x,y)
#endif
static u8 *pkg = NULL;
static u64 size;
static u64 offset;
static void decrypt_beta_retail_pkg(void)
{
u8 key[0x10];
u8 iv[0x10];
if (be16(pkg + 0x06) != 2)
fail("invalid pkg type: %x", be16(pkg + 0x06));
if (key_get_simple("gpkg-key-beta", key, 0x10) < 0)
fail("failed to load the beta package key.");
memcpy(iv, pkg + 0x70, 0x10);
aes128ctr(key, iv, pkg + offset, size, pkg + offset);
}
static void decrypt_retail_pkg(void)
{
u8 key[0x10];
u8 iv[0x10];
if (be16(pkg + 0x06) != 2)
fail("invalid pkg type: %x", be16(pkg + 0x06));
if (key_get_simple("gpkg-key", key, 0x10) < 0)
fail("failed to load the package key.");
memcpy(iv, pkg + 0x70, 0x10);
aes128ctr(key, iv, pkg + offset, size, pkg + offset);
}
static void decrypt_debug_pkg(void)
{
u8 key[0x40];
u8 bfr[0x1c];
u64 i;
memset(key, 0, sizeof key);
memcpy(key, pkg + 0x60, 8);
memcpy(key + 0x08, pkg + 0x60, 8);
memcpy(key + 0x10, pkg + 0x60 + 0x08, 8);
memcpy(key + 0x18, pkg + 0x60 + 0x08, 8);
sha1(key, sizeof key, bfr);
for (i = 0; i < size; i++) {
if (i != 0 && (i % 16) == 0) {
wbe64(key + 0x38, be64(key + 0x38) + 1);
sha1(key, sizeof key, bfr);
}
pkg[offset + i] ^= bfr[i & 0xf];
}
}
static void unpack_pkg(void)
{
char contentid[0x30];
u8 k_licensee[0x10];
u32 j;
for(j=0x0;j<0x30;j++)
contentid[j] = be8(pkg+0x30+j);
for(j=0x0;j<0x10;j++)
k_licensee[j] = be8(pkg+0x70+j);
printf("Content ID %s\n",contentid);
printf("K Licensee ");
for(j=0x0;j<0x10;j++)
printf("%x",k_licensee[j]);
printf("\n");
u64 i;
u64 n_files;
u32 fname_len;
u32 fname_off;
u64 file_offset;
u32 flags;
char fname[256];
u8 *tmp;
n_files = be32(pkg + 0x14);
for (i = 0; i < n_files; i++) {
tmp = pkg + offset + i*0x20;
fname_off = be32(tmp) + offset;
fname_len = be32(tmp + 0x04);
file_offset = be64(tmp + 0x08) + offset;
size = be64(tmp + 0x10);
flags = be32(tmp + 0x18);
if (fname_len >= sizeof fname)
fail("filename too long: %s", pkg + fname_off);
memset(fname, 0, sizeof fname);
strncpy(fname, (char *)(pkg + fname_off), fname_len);
flags &= 0xff;
if (flags == 4)
MKDIR(fname, 0777);
else if (flags == 0 || flags == 1 || flags == 3 || flags == 14)
memcpy_to_file(fname, pkg + file_offset, size);
else
fail("unknown flags: %08x", flags);
}
}
int main(int argc, char *argv[])
{
char *dir;
int beta=0;
if (argc != 2 && argc != 3)
fail("usage: ungpkg filename.pkg\n"
" This is just for PS Vita Game Packages. not PS Vita System Packages.\n"
" -b | use beta key instead of the retail one");
if (argc == 2) {
pkg = mmap_file(argv[1]);
dir = malloc(0x31);
memset(dir, 0, 0x31);
memset(dir, 0, 0x30);
memcpy(dir, pkg + 0x30, 0x30);
} else {
if (strcmp(argv[1], "-b") != 0)
fail("invalid option: %s", argv[1]);
else
beta=1;
pkg = mmap_file(argv[2]);
dir = malloc(0x31);
memset(dir, 0, 0x31);
memset(dir, 0, 0x30);
memcpy(dir, pkg + 0x30, 0x30);
}
MKDIR(dir, 0777);
if (chdir(dir) != 0)
fail("chdir(%s)", dir);
offset = be64(pkg + 0x20);
size = be64(pkg + 0x28);
if(be16(pkg + 0x04) & 0x8000 && beta)
decrypt_beta_retail_pkg();
else if (be16(pkg + 0x04) & 0x8000)
decrypt_retail_pkg();
else
decrypt_debug_pkg();
unpack_pkg();
return 0;
}