forked from ihaveamac/wiiu-things
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wiiu_cdndownload.py
executable file
·166 lines (136 loc) · 9.58 KB
/
wiiu_cdndownload.py
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
#!/usr/bin/env python3
# usage: wiiu_cdndownload.py <titleid>
import base64
import binascii
import json
import os
import struct
import sys
import zlib
from urllib.request import Request, urlopen, HTTPError
def usage():
print('Usage: wiiu_cdndownload.py <titleid>')
print('\nFor the list of Title IDs: https://wiiubrew.org/wiki/Title_database')
sys.exit(1)
if len(sys.argv) == 1:
usage()
title_id = sys.argv[1].upper()
if len(title_id) != 16:
usage()
keyfile = 'titlekeys.json'
if not os.path.isfile(keyfile):
print("ERROR: titlekeys.json not found. Please run wiiu_titlekeys.py.")
sys.exit(1)
base = 'http://ccs.cdn.c.shop.nintendowifi.net/ccs/download/' + title_id
keysite = 'vault.titlekeys.ovh'
with open(keyfile) as data_file:
titlekeys_data = json.load(data_file)
title_data = next((t for t in titlekeys_data if t['titleID'] == title_id), None)
if not title_data:
print("ERROR: No title data for {} on {}".format(title_id, keysite))
sys.exit(1)
title_name = title_data['name'].replace('\n',' ')
typecheck = title_id[4:8]
if typecheck == '0002':
title_name = title_name + ' [Demo]'
elif typecheck == '000C':
title_name = title_name + ' [DLC]'
elif typecheck == '000E':
title_name = title_name + ' [Update]'
# compressed with zlibm encoded with base64
titlecert = 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GwVaqcHd4QLVTQLB78Fdho4PPseVwYVrSGbA7ECuy4jFpVKLw7cvWSNkUP5MuAMoSWLD32We76I3+5GxB/Oup/8P/x3sv83jj7chh/+Z1TboOpo0aqoSV+dZaMxwY4gVvdpcGkioR7ffRwDojILrCpfw1gPYNwkV4DkC6PwuftiEtVhvBiWUnFjnPfqBcH+oDds2WJ4ccUFyFcZsT/KlS/GsXEVGzMe2fHytJ3G5n7RuSpnQAartzwxd0lF2VLUa61NW6g9Ffr0yHRA90T3BGQvcj4qMnwsa66q7crVzwzW0s2Xuo822sHeFJ4pavpzrxs96gTQiJlQjVRTvYgykHPSk/F8eWZ3efJZkhli/OFczDlRkoe88DWIlL/+sUrxS63AKlznRWqAWZGYTk943czLKH/XKoEUj7+zaES9AbhSPR8Kv20bRyYhPGEnD+v/P4J+h1k='
TIKTEM = binascii.a2b_hex('00010004d15ea5ed15abe11ad15ea5ed15abe11ad15ea5ed15abe11ad15ea5ed15abe11ad15ea5ed15abe11ad15ea5ed15abe11ad15ea5ed15abe11ad15ea5ed15abe11ad15ea5ed15abe11ad15ea5ed15abe11ad15ea5ed15abe11ad15ea5ed15abe11ad15ea5ed15abe11ad15ea5ed15abe11ad15ea5ed15abe11ad15ea5ed15abe11ad15ea5ed15abe11ad15ea5ed15abe11ad15ea5ed15abe11ad15ea5ed15abe11ad15ea5ed15abe11ad15ea5ed15abe11ad15ea5ed15abe11ad15ea5ed15abe11ad15ea5ed15abe11ad15ea5ed15abe11ad15ea5ed15abe11ad15ea5ed15abe11ad15ea5ed15abe11ad15ea5ed15abe11ad15ea5ed15abe11ad15ea5ed15abe11a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000526f6f742d434130303030303030332d585330303030303030630000000000000000000000000000000000000000000000000000000000000000000000000000feedfacefeedfacefeedfacefeedfacefeedfacefeedfacefeedfacefeedfacefeedfacefeedfacefeedfacefeedfacefeedfacefeedfacefeedface010000cccccccccccccccccccccccccccccccc00000000000000000000000000aaaaaaaaaaaaaaaa00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010014000000ac000000140001001400000000000000280000000100000084000000840003000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000')
TK = 0x140
# http://stackoverflow.com/questions/8866046/python-round-up-integer-to-next-hundred
def roundup(x, base=64):
return x if x % base == 0 else x + base - x % base
def download(url, printprogress=False, outfile=None, message_prefix='', message_suffix=''):
cn = urlopen(url)
totalsize = int(cn.headers['content-length'])
totalread = 0
# some things used from
# http://stackoverflow.com/questions/13881092/download-progressbar-for-python-3
blocksize = 10 * 1024
if not outfile:
ct = b''
while totalsize > totalread:
toread = min(totalsize - totalread, blocksize)
co = cn.read(toread)
totalread += toread
if printprogress:
percent = min(totalread * 1e2 / totalsize, 1e2)
print('\r{:29} {:>5.1f}% {:>10} / {:>10} {}'.format(message_prefix, percent, totalread, totalsize, message_suffix), end='')
if outfile:
outfile.write(co)
else:
ct += co
if printprogress:
print('')
if not outfile:
return ct
os.makedirs(title_name, exist_ok=True)
print('Processing title ' + title_id + ': ' + title_name)
print('Downloading TMD (title.tmd)...')
# this is a mess how can i make it better
tmd = download(base + '/tmd{}'.format(('.' + sys.argv[2]) if len(sys.argv) > 2 else ''))
contents = []
count = struct.unpack('>H', tmd[0x1DE:0x1E0])[0]
print(' Contents: {}'.format(count))
contentsize = 0
for c in range(count):
contents.append([
# content ID
binascii.hexlify(tmd[0xB04 + (0x30 * c):0xB04 + (0x30 * c) + 0x4]).decode('utf-8'),
# content type
struct.unpack('>H', tmd[0xB0A + (0x30 * c):0xB0A + (0x30 * c) + 0x2])[0],
# content size
struct.unpack('>Q', tmd[0xB0C + (0x30 * c):0xB0C + (0x30 * c) + 0x8])[0],
])
with open(title_name + '/title.tmd', 'wb') as f:
f.write(tmd)
total_size = sum(c[2] for c in contents)
print(" Total size: 0x{:X} ({} MiB)".format(total_size, total_size / (1024 ** 2)))
print('Getting CETK (title.tik)...')
cetk_file = title_name + '/title.tik'
# things to not try and download cetk for
app_categories = {
'0000', # application
'0002', # demo
'000C', # DLC
}
if title_id[4:8] not in app_categories:
print(' Downloading CDN version from: ' + base)
with open(cetk_file, 'wb') as f:
download(base + '/cetk', False, f)
else:
if title_data['ticket'] == 1:
print(' Downloading Disc version from: ' + keysite)
request = Request('https://{}/ticket/{}.tik'.format(keysite, title_id))
request.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)')
response = urlopen(request).read()
with open(cetk_file, 'wb') as f:
f.write(response)
else:
title_key = title_data['titleKey']
print(' Generating a fake one from encrypted key: ' + title_key)
print(' Fake CETK requires custom firmware.')
title_version = tmd[TK + 0x9C:TK + 0x9E]
tikdata = bytearray(TIKTEM)
tikdata[TK + 0xA6:TK + 0xA8] = title_version
tikdata[TK + 0x9C:TK + 0xA4] = binascii.a2b_hex(title_id)
tikdata[TK + 0x7F:TK + 0x8F] = binascii.a2b_hex(title_key)
# not sure what the value at 0xB3 is... mine is 0 but some i see 5.
# or 0xE0, the reserved data is...?
open(cetk_file, 'wb').write(tikdata)
print('Writing cert (title.cert)...')
with open(title_name + '/title.cert', 'wb') as f:
f.write(zlib.decompress(base64.b64decode(titlecert)))
for c in contents:
if os.path.isfile(title_name + '/' + c[0] + '.app') and os.path.getsize(title_name + '/' + c[0] + '.app') == c[2]:
print('Skipping {}.app due to existing file with proper size'.format(c[0]))
else:
with open(title_name + '/' + c[0] + '.app', 'wb') as f:
download(base + '/' + c[0], True, f, 'Downloading: {}.app...'.format(c[0]), '({}) MiB)'.format(c[2] / (1024 ** 2)))
if c[1] & 0x2:
with open(title_name + '/' + c[0] + '.h3', 'wb') as f:
download(base + '/' + c[0] + '.h3', True, f, 'Downloading: {}.h3...'.format(c[0]))