-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnuspy.py
321 lines (256 loc) · 13.9 KB
/
nuspy.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
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
import os, struct, base64, \
binascii, sys, zlib, \
pathlib, requests, colorama
## Set up graceful script exiting ##
def exit_gracefully(signum=None, frame=None):
print("\nExited")
sys.exit(0)
try:
# Try to use signal.SIGINT on Unix-based systems
import signal
signal.signal(signal.SIGINT, exit_gracefully)
except (ImportError, AttributeError):
try:
# Try to use ctypes.windll.kernel32 on Windows
import ctypes
kernel32 = ctypes.windll.kernel32
# Enable Ctrl+C handling
kernel32.SetConsoleCtrlHandler(exit_gracefully, 1)
except:
print(
"Failed to set up signal handling. Graceful exit upon CTRL+C not supported"
)
## Set up graceful script exiting ##
## Define global vars ##
MA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TW6ggNESn74v2jMFN5TLi7i5d9ylskJjvtGuLSrmtQJD/kM5OeJZX73d/dmxAarGwVaqcHd4QLVTQLB78Fdho4PPseVwYVrSGbA7ECuy4jFpVKLw7cvWSNkUP5MuAMoSWLD32We76I3+5GxB/Oup/8P/x3sv83jj7chh/+Z1TboOpo0aqoSV+dZaMxwY4gVvdpcGkioR7ffRwDojILrCpfw1gPYNwkV4DkC6PwuftiEtVhvBiWUnFjnPfqBcH+oDds2WJ4ccUFyFcZsT/KlS/GsXEVGzMe2fHytJ3G5n7RuSpnQAartzwxd0lF2VLUa61NW6g9Ffr0yHRA90T3BGQvcj4qMnwsa66q7crVzwzW0s2Xuo822sHeFJ4pavpzrxs96gTQiJlQjVRTvYgykHPSk/F8eWZ3efJZkhli/OFczDlRkoe88DWIlL/+sUrxS63AKlznRWqAWZGYTk943czLKH/XKoEUj7+zaES9AbhSPR8Kv20bRyYhPGEnD+v/P4J+h1k="
)
)
TIK_DATA = zlib.decompress(
base64.b64decode(
"eJxjYGRguRi39K3o6odSI5VmoAAE5eeX6Do7GkCAsW5EMJSZTJz+f29/nSMXMzIwnEEDKGavggJK/MfASJFu6gBGBhEguQaIRaBsENBggLiuBYyZIYL/CQBaOxUAoIEBhQ=="
)
)
TIK_OFFSET = 0x140
## Define global vars ##
## Using slightly modified ticket patching from FunKiiU ##
def patch_ticket(ticket_data, offset, data):
ticket_offset_start = TIK_OFFSET + offset
ticket_offset_end = ticket_offset_start + len(data)
ticket_data[ticket_offset_start:ticket_offset_end] = data
def patch_ticket_demo(ticket_data):
# Offset to patch
offset = 0x124
# Demo data to be patched
demo_data = b"\x00" * 64
# Patch the ticket data with demo data
patch_ticket(ticket_data, offset, demo_data)
def patch_ticket_dlc(ticket_data):
# Offset to patch
offset = 0x164
# DLC data to be patched
dlc_data = base64.b64decode("eNpjYGQQYWBgWAPEIgwQNghoADEjELeAMTNE8D8BwEBjAABCdSH/")
dlc_data = zlib.decompress(dlc_data)
# Patch the ticket data with DLC data
patch_ticket(ticket_data, offset, dlc_data)
def generate_ticket(title_id, title_key, title_version, full_output_path, patch_demo=False, patch_dlc=False):
# Create an empty bytearray to store the ticket data
ticket_data = bytearray(TIK_DATA)
# Update the title version, title ID, and title key in the ticket data
ticket_data[TIK_OFFSET + 0xA6: TIK_OFFSET + 0xA8] = title_version
ticket_data[TIK_OFFSET + 0x9C: TIK_OFFSET + 0xA4] = binascii.a2b_hex(title_id)
ticket_data[TIK_OFFSET + 0x7F: TIK_OFFSET + 0x8F] = binascii.a2b_hex(title_key)
# Determine the type of title ID and apply the corresponding patch function if applicable
type_check = title_id[4:8]
patch_functions = {
"0002": patch_ticket_demo if patch_demo else None,
"000C": patch_ticket_dlc if patch_dlc else None,
}
patch_func = patch_functions.get(type_check)
if patch_func:
patch_func(ticket_data)
# Save the ticket data to the specified output path
with open(full_output_path, "wb") as f:
f.write(ticket_data)
## Using slightly modified ticket patching from FunKiiU ##
# function to look up values by key using lib
def get_value(key, key_value_pairs):
upper_key = key.upper() # convert to upper-case
return key_value_pairs.get(upper_key)
# Main function, most stuff branches off from here
def main(title_id: str, version: str = None):
# Install requirements if requirements.txt exists
requirements_file = pathlib.Path("requirements.txt")
if requirements_file.is_file():
os.system("pip install -r requirements.txt")
requirements_file.unlink()
# Check if title_id is valid
if len(title_id) not in (16, 32):
print(f"{colorama.Fore.RED}Title ID is invalid length. Expected length: 16 or 32.")
print(f"{colorama.Fore.GREEN}usage: {sys.argv[0]} <titleid>")
print("Latest version is downloaded, if no version is specified.")
print("This tool is for software archival purposes only. \n\
thegamershollow does not condone piracy of any kind as it is illegal.")
sys.exit(1)
# Get title key from NUSPY-Lib
url = "https://raw.githubusercontent.com/thegamershollow/NUSPY-Lib/main/lib" # Using Korozin lib for now
response = requests.get(url)
content = response.text
key_value_pairs = {}
for line in content.splitlines():
parts = line.split()
if len(parts) >= 2:
key = parts[0].upper()
value = parts[1]
key_value_pairs[key] = value
title_key = get_value(title_id, key_value_pairs).upper()
# Set up server URL(s)
app_categories = {"0000", "0002", "000C", "000E"}
base_url = f'http://{"ccs." if title_id[4:8] in app_categories else "nus."}cdn.c.shop.nintendowifi.net/ccs/download/{title_id}'
# Check title ID validity
if title_id[4:8] not in app_categories:
print("Invalid Title ID / Title Key")
sys.exit(1)
# Set path naming
suffix = {"0000": "_Game", "000E": "_Update", "000C": "_DLC", "0002": "_Demo"}.get(
title_id[4:8], ""
)
downloads_dir = pathlib.Path("Downloads")
downloads_dir.mkdir(exist_ok=True)
root_dir = pathlib.Path("Downloads/" + title_id + suffix)
root_dir.mkdir(exist_ok=True)
# Download title.tmd
tmd_url = f'{base_url}/tmd{"." + version if version else ""}'
tmd = download(tmd_url, colorama.Fore.BLUE + f"Downloading: title.tmd...")
contents = get_contents(tmd)
# Write title.tmd contents
with open(root_dir / "title.tmd", "wb") as f:
f.write(tmd)
# Get version from title.tmd
with open(root_dir / "title.tmd", "rb") as f:
tiver = f.read()[TIK_OFFSET + 0x9C : TIK_OFFSET + 0x9E]
# Download title.tik if title is an update, otherwise generate fake ticket
if "000E" in title_id[4:8]:
tik_url = f"{base_url}/cetk{('.' + version) if version else ''}"
tik = download(tik_url, colorama.Fore.BLUE + f"Downloading: title.tik...")
with open(root_dir / "title.tik", "wb") as f:
f.write(tik)
else:
# Generate fake ticket
tik_file = root_dir / "title.tik"
generate_ticket(title_id, title_key, tiver, tik_file)
# Write cert data
with open(root_dir / "title.cert", "wb") as f:
f.write(CERT_DATA)
# Download all contents
for index, content in enumerate(contents, start=1):
download_content(root_dir, base_url, contents, content, index, version)
print(colorama.Fore.GREEN + f"\nSuccessfully downloaded Title to: {root_dir}")
# Function that sets up files to be downloaded by download function
def download_content(root_dir, base_url, contents, content, index, version=None):
content_id, content_type, content_size, _ = content
app_path = root_dir / f"{content_id}.app"
if app_path.is_file() and app_path.stat().st_size == content_size:
skip_msg = (
colorama.Fore.YELLOW
+ "Skipping"
+ colorama.Fore.CYAN
+ f" [{index}/{len(contents)}] {colorama.Fore.RESET+content_id}.app due to existing file with proper size."
)
print(skip_msg)
return
app_msg = (
colorama.Fore.BLUE
+ "Downloading"
+ colorama.Fore.CYAN
+ f" [{index}/{len(contents)}] {colorama.Fore.RESET+content_id}.app..."
+ colorama.Fore.RESET
)
app_size = f"({content_size/(1024**2):.2f} MiB)"
app_content = download(f"{base_url}/{content_id}", app_msg, app_size)
with open(app_path, "wb") as f:
f.write(app_content)
if content_type & 0x2:
h3_path = root_dir / f"{content_id}.h3"
if h3_path.is_file() and h3_path.stat().st_size == content_size:
with open(h3_path, "rb") as f:
existing_h3_content = f.read()
if existing_h3_content == h3_content:
skip_msg = (
colorama.Fore.YELLOW
+ "Skipping"
+ colorama.Fore.RESET
+ f" {content_id}.h3 due to existing file with same content."
)
print(skip_msg)
return
h3_msg = (
colorama.Fore.BLUE
+ "Downloading"
+ colorama.Fore.CYAN
+ f" [{index}/{len(contents)}] {colorama.Fore.RESET+content_id}.h3..."
+ colorama.Fore.RESET
)
h3_size = f"({content_size/(1024**2):.2f} MiB)"
h3_content = download(f"{base_url}/{content_id}.h3", h3_msg, h3_size)
with open(h3_path, "wb") as f:
f.write(h3_content)
# Function that returns content info from tmd
def get_contents(tmd):
content_count = struct.unpack(">H", tmd[0x1DE:0x1E0])[0]
contents_list = []
for content_index in range(content_count):
content_id_bytes = tmd[0xB04 + (0x30 * content_index) : 0xB04 + (0x30 * content_index) + 0x4]
content_id = binascii.hexlify(content_id_bytes).decode("utf-8")
content_type_bytes = tmd[0xB0A + (0x30 * content_index) : 0xB0A + (0x30 * content_index) + 0x2]
content_type = struct.unpack(">H", content_type_bytes)[0]
content_size_bytes = tmd[0xB0C + (0x30 * content_index) : 0xB0C + (0x30 * content_index) + 0x8]
content_size = struct.unpack(">Q", content_size_bytes)[0]
contents_list.append((content_id, content_type, content_size, content_index + 1))
print(colorama.Fore.RESET + "Contents:" + colorama.Fore.CYAN + f" {content_count}")
total_size = sum(content[2] for content in contents_list)
print(colorama.Fore.RESET + "Total size:" + f" ({total_size / (1024 ** 2)} MiB)")
return contents_list
# Function that downloads the files
def download(url: str, message: str, message_suffix: str = "") -> bytes:
response = requests.get(url, stream=True)
response.raise_for_status()
downloaded_content = b""
total_size = int(response.headers.get("content-length", 0))
# Download content in chunks
if not total_size:
downloaded_content = response.content
else:
print(message, end="", flush=True)
downloaded_size = 0
for chunk in response.iter_content(chunk_size=MAX_BLOCK_SIZE):
downloaded_content += chunk
downloaded_size += len(chunk)
# Show download progress
percent = round(downloaded_size * 100.0 / total_size, 1)
downloaded_size_mb = downloaded_size / (1024 ** 2)
total_size_mb = total_size / (1024 ** 2)
progress_text = f"\r{message} ({downloaded_size_mb:.2f} MiB / {total_size_mb:.2f} MiB) ({percent}%) {message_suffix}"
print(progress_text, end="", flush=True)
print("")
return downloaded_content
if __name__ == "__main__":
# Check for the correct number of command line arguments
if len(sys.argv) == 1:
print(f"Usage: {sys.argv[0]} <titleid>")
print("Latest version is downloaded if no version is specified.")
sys.exit(1)
# Call the main function with the specified arguments
try:
main(title_id=sys.argv[1].upper(),
version=sys.argv[2] if len(sys.argv) > 2 else None)
except IndexError:
# Handle invalid number of arguments
print(colorama.Fore.RED + "Invalid number of arguments")
print(f"Usage: {sys.argv[0]} <titleid>")
print("Latest version of the title is downloaded if no version is specified.")
print("For help go to https://github.com/thegamershollow/NUSPY/wiki")
sys.exit(1)