9 Commits

7 changed files with 195 additions and 149 deletions

View File

@@ -118,5 +118,10 @@ jobs:
- name: Upload Application
uses: actions/upload-artifact@v4
with:
path: D:\a\WiiPy\WiiPy\wiipy.exe
path: D:\a\WiiPy\WiiPy\WiiPy.dist
name: WiiPy-Windows-bin
- name: Upload Onefile Application
uses: actions/upload-artifact@v4
with:
path: D:\a\WiiPy\WiiPy\wiipy.exe
name: WiiPy-Windows-onefile-bin

View File

@@ -7,7 +7,20 @@ from modules.core import fatal_error
def handle_lz77_compress(args):
print("Compression is not implemented yet.")
input_path = pathlib.Path(args.input)
if args.output is not None:
output_path = pathlib.Path(args.output)
else:
output_path = pathlib.Path(input_path.name + ".lz77")
if not input_path.exists():
fatal_error(f"The specified file \"{input_path}\" does not exist!")
lz77_data = input_path.read_bytes()
data = libWiiPy.archive.compress_lz77(lz77_data)
output_path.write_bytes(data)
print("LZ77 file compressed!")
def handle_lz77_decompress(args):

View File

@@ -78,7 +78,8 @@ def handle_emunand_info(args):
else:
print(f" IOS{int(ios[-2:], 16)} ({ios.upper()})")
tmd = emunand.get_title_tmd(ios)
print(f" Version: {tmd.title_version} ({tmd.title_version_converted})")
print(f" Version: {tmd.title_version} "
f"({libWiiPy.title.title_ver_dec_to_standard(tmd.title_version, tmd.title_id, bool(tmd.vwii))})")
print("")
print(f"Installed Titles:")
@@ -180,8 +181,13 @@ def handle_emunand_install_missing(args):
print(f"\nAll missing IOSes have been installed!")
def _emunand_logger(log):
print(log)
def handle_emunand_title(args):
emunand = libWiiPy.nand.EmuNAND(args.emunand)
logger = _emunand_logger if args.verbose else lambda _: None
emunand = libWiiPy.nand.EmuNAND(args.emunand, logger)
if args.skip_hash:
skip_hash = True
else:

View File

@@ -4,7 +4,9 @@
import binascii
import pathlib
import re
import libWiiPy
from modules.core import fatal_error
@@ -22,11 +24,12 @@ def _print_tmd_info(tmd: libWiiPy.title.TMD, signing_cert=None):
else:
print(f" Title ID: {tmd.title_id.upper()}")
# This type of version number really only applies to the System Menu and IOS.
title_version_converted = libWiiPy.title.title_ver_dec_to_standard(tmd.title_version, tmd.title_id, bool(tmd.vwii))
if tmd.title_id.startswith("00000001"):
if tmd.title_id == "0000000100000001":
print(f" Title Version: {tmd.title_version} (boot2v{tmd.title_version})")
else:
print(f" Title Version: {tmd.title_version} ({tmd.title_version_converted})")
print(f" Title Version: {tmd.title_version} ({title_version_converted})")
else:
print(f" Title Version: {tmd.title_version}")
print(f" TMD Version: {tmd.tmd_version}")
@@ -43,13 +46,16 @@ def _print_tmd_info(tmd: libWiiPy.title.TMD, signing_cert=None):
elif tmd.signature_issuer.find("CP00000007") != -1:
print(f" Certificate: CP00000007 (Development)")
print(f" Certificate Issuer: Root-CA00000002 (Development)")
elif tmd.signature_issuer.find("CP00000005") != -1:
print(f" Certificate: CP00000005 (Development/Unknown)")
print(f" Certificate Issuer: Root-CA00000002 (Development)")
elif tmd.signature_issuer.find("CP10000000") != -1:
print(f" Certificate: CP10000000 (Arcade)")
print(f" Certificate Issuer: Root-CA10000000 (Arcade)")
else:
print(f" Certificate Info: {tmd.signature_issuer} (Unknown)")
if tmd.title_id == "0000000100000002":
match tmd.title_version_converted[-1:]:
match title_version_converted[-1:]:
case "U":
region = "USA"
case "E":
@@ -67,23 +73,22 @@ def _print_tmd_info(tmd: libWiiPy.title.TMD, signing_cert=None):
print(f" Region: {region}")
print(f" Title Type: {tmd.get_title_type()}")
print(f" vWii Title: {bool(tmd.vwii)}")
print(f" DVD Video Access: {tmd.get_access_right(tmd.AccessFlags.DVD_VIDEO)}")
print(f" AHB Access: {tmd.get_access_right(tmd.AccessFlags.AHB)}")
print(f" DVD Video Access: {tmd.get_access_right(libWiiPy.title.AccessFlags.DVD_VIDEO)}")
print(f" AHB Access: {tmd.get_access_right(libWiiPy.title.AccessFlags.AHB)}")
if signing_cert is not None:
try:
signed = libWiiPy.title.verify_tmd_sig(signing_cert, tmd)
if signed:
signing_str = "Valid (Unmodified)"
if libWiiPy.title.verify_tmd_sig(signing_cert, tmd):
signing_str = "Valid (Unmodified TMD)"
elif tmd.get_is_fakesigned():
signing_str = "Fakesigned"
else:
signing_str = "Invalid (Modified)"
signing_str = "Invalid (Modified TMD)"
except ValueError:
if tmd.get_is_fakesigned():
signing_str = "Fakesigned"
else:
signing_str = "Invalid (Modified)"
print(f" Signing Status: {signing_str}")
signing_str = "Invalid (Modified TMD)"
print(f" Signature: {signing_str}")
else:
print(f" Fakesigned: {tmd.get_is_fakesigned()}")
# Iterate over the content and print their details.
@@ -103,22 +108,23 @@ def _print_ticket_info(ticket: libWiiPy.title.Ticket, signing_cert=None):
# Get all important keys from the TMD and print them out nicely.
print(f"Ticket Info")
ascii_tid = ""
decoded_tid = binascii.hexlify(ticket.title_id).decode()
try:
ascii_tid = str(bytes.fromhex(ticket.title_id.decode()[8:].replace("00", "30")).decode("ascii"))
except UnicodeDecodeError or binascii.Error:
ascii_tid = str(bytes.fromhex(decoded_tid[8:].replace("00", "30")).decode("ascii"))
except UnicodeDecodeError:
pass
pattern = r"^[a-z0-9!@#$%^&*]{4}$"
if re.fullmatch(pattern, ascii_tid, re.IGNORECASE):
print(f" Title ID: {ticket.title_id.decode().upper()} ({ascii_tid})")
print(f" Title ID: {decoded_tid.upper()} ({ascii_tid})")
else:
print(f" Title ID: {ticket.title_id.decode().upper()}")
print(f" Title ID: {decoded_tid.upper()}")
# This type of version number really only applies to the System Menu and IOS.
if ticket.title_id.decode().startswith("00000001"):
if ticket.title_id.decode() == "0000000100000001":
if decoded_tid.startswith("00000001"):
if decoded_tid == "0000000100000001":
print(f" Title Version: {ticket.title_version} (boot2v{ticket.title_version})")
else:
print(f" Title Version: {ticket.title_version} "
f"({libWiiPy.title.title_ver_dec_to_standard(ticket.title_version, ticket.title_id.decode())})")
f"({libWiiPy.title.title_ver_dec_to_standard(ticket.title_version, decoded_tid)})")
else:
print(f" Title Version: {ticket.title_version}")
print(f" Ticket Version: {ticket.ticket_version}")
@@ -128,8 +134,12 @@ def _print_ticket_info(ticket: libWiiPy.title.Ticket, signing_cert=None):
elif ticket.signature_issuer.find("XS00000006") != -1:
print(f" Certificate: XS00000006 (Development)")
print(f" Certificate Issuer: Root-CA00000002 (Development)")
elif ticket.signature_issuer.find("XS00000004") != -1:
print(f" Certificate: XS00000004 (Development/Unknown)")
print(f" Certificate Issuer: Root-CA00000002 (Development)")
else:
print(f" Certificate Info: {ticket.signature_issuer} (Unknown)")
key = ""
match ticket.common_key_index:
case 0:
if ticket.is_dev:
@@ -147,19 +157,18 @@ def _print_ticket_info(ticket: libWiiPy.title.Ticket, signing_cert=None):
print(f" Title Key (Decrypted): {binascii.hexlify(ticket.get_title_key()).decode()}")
if signing_cert is not None:
try:
signed = libWiiPy.title.verify_ticket_sig(signing_cert, ticket)
if signed:
signing_str = "Valid (Unmodified)"
if libWiiPy.title.verify_ticket_sig(signing_cert, ticket):
signing_str = "Valid (Unmodified Ticket)"
elif ticket.get_is_fakesigned():
signing_str = "Fakesigned"
else:
signing_str = "Invalid (Modified)"
signing_str = "Invalid (Modified Ticket)"
except ValueError:
if ticket.get_is_fakesigned():
signing_str = "Fakesigned"
else:
signing_str = "Invalid (Modified)"
print(f" Signing Status: {signing_str}")
signing_str = "Invalid (Modified Ticket)"
print(f" Signature: {signing_str}")
else:
print(f" Fakesigned: {ticket.get_is_fakesigned()}")
@@ -199,17 +208,22 @@ def _print_wad_info(title: libWiiPy.title.Title):
tmd_cert = None
ticket_cert = None
try:
signed = title.get_is_signed()
tmd_cert = title.cert_chain.tmd_cert
ticket_cert = title.cert_chain.ticket_cert
if signed:
signing_str = "Valid (Unmodified)"
if title.get_is_signed():
signing_str = "Legitimate (Unmodified TMD + Ticket)"
elif title.get_is_fakesigned():
signing_str = "Fakesigned"
elif (libWiiPy.title.verify_tmd_sig(tmd_cert, title.tmd)
and not libWiiPy.title.verify_ticket_sig(ticket_cert, title.ticket)):
signing_str = "Piratelegit (Unmodified TMD, Modified Ticket)"
elif (not libWiiPy.title.verify_tmd_sig(tmd_cert, title.tmd)
and libWiiPy.title.verify_ticket_sig(ticket_cert, title.ticket)):
signing_str = "Edited (Modified TMD, Unmodified Ticket)"
else:
signing_str = "Invalid (Modified)"
signing_str = "Illegitimate (Modified TMD + Ticket)"
except ValueError:
signing_str = "Invalid (Modified)"
signing_str = "Illegitimate (Modified TMD + Ticket)"
print(f" Signing Status: {signing_str}")
print("")
_print_ticket_info(title.ticket, ticket_cert)

View File

@@ -8,119 +8,6 @@ import libWiiPy
from modules.core import fatal_error
def handle_nus_title(args):
title_version = None
wad_file = None
output_dir = None
can_decrypt = False
tid = args.tid
if args.wii:
wiiu_nus_enabled = False
else:
wiiu_nus_enabled = True
# Check if --version was passed, because it'll be None if it wasn't.
if args.version is not None:
try:
title_version = int(args.version)
except ValueError:
fatal_error("The specified Title Version must be a valid integer!")
# If --wad was passed, check to make sure the path is okay.
if args.wad is not None:
wad_file = pathlib.Path(args.wad)
if wad_file.suffix != ".wad":
wad_file = wad_file.with_suffix(".wad")
# If --output was passed, make sure the directory either doesn't exist or is empty.
if args.output is not None:
output_dir = pathlib.Path(args.output)
if output_dir.exists():
if output_dir.is_file():
fatal_error("A file already exists with the provided directory name!")
else:
output_dir.mkdir()
# Download the title from the NUS. This is done "manually" (as opposed to using download_title()) so that we can
# provide verbose output.
title = libWiiPy.title.Title()
# Announce the title being downloaded, and the version if applicable.
if title_version is not None:
print(f"Downloading title {tid} v{title_version}, please wait...")
else:
print(f"Downloading title {tid} vLatest, please wait...")
print(" - Downloading and parsing TMD...")
# Download a specific TMD version if a version was specified, otherwise just download the latest TMD.
if title_version is not None:
title.load_tmd(libWiiPy.title.download_tmd(tid, title_version, wiiu_endpoint=wiiu_nus_enabled))
else:
title.load_tmd(libWiiPy.title.download_tmd(tid, wiiu_endpoint=wiiu_nus_enabled))
title_version = title.tmd.title_version
# Write out the TMD to a file.
if output_dir is not None:
output_dir.joinpath(f"tmd.{title_version}").write_bytes(title.tmd.dump())
# Download the ticket, if we can.
print(" - Downloading and parsing Ticket...")
try:
title.load_ticket(libWiiPy.title.download_ticket(tid, wiiu_endpoint=wiiu_nus_enabled))
can_decrypt = True
if output_dir is not None:
output_dir.joinpath("tik").write_bytes(title.ticket.dump())
except ValueError:
# If libWiiPy returns an error, then no ticket is available. Log this, and disable options requiring a
# ticket so that they aren't attempted later.
print(" - No Ticket is available!")
if wad_file is not None and output_dir is None:
fatal_error("--wad was passed, but this title has no common ticket and cannot be packed into a WAD!")
# Load the content records from the TMD, and begin iterating over the records.
title.load_content_records()
content_list = []
for content in range(len(title.tmd.content_records)):
# Generate the content file name by converting the Content ID to hex and then removing the 0x.
content_file_name = hex(title.tmd.content_records[content].content_id)[2:]
while len(content_file_name) < 8:
content_file_name = "0" + content_file_name
print(f" - Downloading content {content + 1} of {len(title.tmd.content_records)} "
f"(Content ID: {title.tmd.content_records[content].content_id}, "
f"Size: {title.tmd.content_records[content].content_size} bytes)...")
content_list.append(libWiiPy.title.download_content(tid, title.tmd.content_records[content].content_id,
wiiu_endpoint=wiiu_nus_enabled))
print(" - Done!")
# If we're supposed to be outputting to a folder, then write these files out.
if output_dir is not None:
output_dir.joinpath(content_file_name).write_bytes(content_list[content])
title.content.content_list = content_list
# Try to decrypt the contents for this title if a ticket was available.
if output_dir is not None:
if can_decrypt is True:
for content in range(len(title.tmd.content_records)):
print(f" - Decrypting content {content + 1} of {len(title.tmd.content_records)} "
f"(Content ID: {title.tmd.content_records[content].content_id})...")
dec_content = title.get_content_by_index(content)
content_file_name = f"{title.tmd.content_records[content].content_id:08X}".lower() + ".app"
output_dir.joinpath(content_file_name).write_bytes(dec_content)
else:
print("Title has no Ticket, so content will not be decrypted!")
# If --wad was passed, pack a WAD and output that.
if wad_file is not None:
# Get the WAD certificate chain.
print(" - Building certificate...")
title.load_cert_chain(libWiiPy.title.download_cert_chain(wiiu_endpoint=wiiu_nus_enabled))
# Ensure that the path ends in .wad, and add that if it doesn't.
print("Packing WAD...")
if wad_file.suffix != ".wad":
wad_file = wad_file.with_suffix(".wad")
# Have libWiiPy dump the WAD, and write that data out.
pathlib.Path(wad_file).write_bytes(title.dump_wad())
print(f"Downloaded title with Title ID \"{args.tid}\"!")
def handle_nus_content(args):
tid = args.tid
cid = args.cid
@@ -157,7 +44,7 @@ def handle_nus_content(args):
except ValueError:
fatal_error("The specified Title ID or Content ID could not be found!")
if decrypt_content is True:
if decrypt_content:
output_path = output_path.with_suffix(".app")
tmd = libWiiPy.title.TMD()
tmd.load(libWiiPy.title.download_tmd(tid, version))
@@ -198,6 +85,122 @@ def handle_nus_content(args):
print(f"Downloaded content with Content ID \"{cid}\"!")
def handle_nus_title(args):
title_version = None
wad_file = None
output_dir = None
can_decrypt = False
tid = args.tid
wiiu_nus_enabled = False if args.wii else True
endpoint_override = args.endpoint if args.endpoint else None
# Check if --version was passed, because it'll be None if it wasn't.
if args.version is not None:
try:
title_version = int(args.version)
except ValueError:
fatal_error("The specified Title Version must be a valid integer!")
# If --wad was passed, check to make sure the path is okay.
if args.wad is not None:
wad_file = pathlib.Path(args.wad)
if wad_file.suffix != ".wad":
wad_file = wad_file.with_suffix(".wad")
# If --output was passed, make sure the directory either doesn't exist or is empty.
if args.output is not None:
output_dir = pathlib.Path(args.output)
if output_dir.exists():
if output_dir.is_file():
fatal_error("A file already exists with the provided directory name!")
else:
output_dir.mkdir()
# Download the title from the NUS. This is done "manually" (as opposed to using download_title()) so that we can
# provide verbose output.
title = libWiiPy.title.Title()
# Announce the title being downloaded, and the version if applicable.
if title_version is not None:
print(f"Downloading title {tid} v{title_version}, please wait...")
else:
print(f"Downloading title {tid} vLatest, please wait...")
print(" - Downloading and parsing TMD...")
# Download a specific TMD version if a version was specified, otherwise just download the latest TMD.
if title_version is not None:
title.load_tmd(libWiiPy.title.download_tmd(tid, title_version, wiiu_endpoint=wiiu_nus_enabled,
endpoint_override=endpoint_override))
else:
title.load_tmd(libWiiPy.title.download_tmd(tid, wiiu_endpoint=wiiu_nus_enabled,
endpoint_override=endpoint_override))
title_version = title.tmd.title_version
# Write out the TMD to a file.
if output_dir is not None:
output_dir.joinpath(f"tmd.{title_version}").write_bytes(title.tmd.dump())
# Download the ticket, if we can.
print(" - Downloading and parsing Ticket...")
try:
title.load_ticket(libWiiPy.title.download_ticket(tid, wiiu_endpoint=wiiu_nus_enabled,
endpoint_override=endpoint_override))
can_decrypt = True
if output_dir is not None:
output_dir.joinpath("tik").write_bytes(title.ticket.dump())
except ValueError:
# If libWiiPy returns an error, then no ticket is available. Log this, and disable options requiring a
# ticket so that they aren't attempted later.
print(" - No Ticket is available!")
if wad_file is not None and output_dir is None:
fatal_error("--wad was passed, but this title has no common ticket and cannot be packed into a WAD!")
# Load the content records from the TMD, and begin iterating over the records.
title.load_content_records()
content_list = []
for content in range(len(title.tmd.content_records)):
# Generate the content file name by converting the Content ID to hex and then removing the 0x.
content_file_name = hex(title.tmd.content_records[content].content_id)[2:]
while len(content_file_name) < 8:
content_file_name = "0" + content_file_name
print(f" - Downloading content {content + 1} of {len(title.tmd.content_records)} "
f"(Content ID: {title.tmd.content_records[content].content_id}, "
f"Size: {title.tmd.content_records[content].content_size} bytes)...")
content_list.append(libWiiPy.title.download_content(tid, title.tmd.content_records[content].content_id,
wiiu_endpoint=wiiu_nus_enabled,
endpoint_override=endpoint_override))
print(" - Done!")
# If we're supposed to be outputting to a folder, then write these files out.
if output_dir is not None:
output_dir.joinpath(content_file_name).write_bytes(content_list[content])
title.content.content_list = content_list
# Try to decrypt the contents for this title if a ticket was available.
if output_dir is not None:
if can_decrypt:
for content in range(len(title.tmd.content_records)):
print(f" - Decrypting content {content + 1} of {len(title.tmd.content_records)} "
f"(Content ID: {title.tmd.content_records[content].content_id})...")
dec_content = title.get_content_by_index(content)
content_file_name = f"{title.tmd.content_records[content].content_id:08X}".lower() + ".app"
output_dir.joinpath(content_file_name).write_bytes(dec_content)
else:
print("Title has no Ticket, so content will not be decrypted!")
# If --wad was passed, pack a WAD and output that.
if wad_file is not None:
# Get the WAD certificate chain.
print(" - Building certificate...")
title.load_cert_chain(libWiiPy.title.download_cert_chain(wiiu_endpoint=wiiu_nus_enabled,
endpoint_override=endpoint_override))
# Ensure that the path ends in .wad, and add that if it doesn't.
print("Packing WAD...")
if wad_file.suffix != ".wad":
wad_file = wad_file.with_suffix(".wad")
# Have libWiiPy dump the WAD, and write that data out.
pathlib.Path(wad_file).write_bytes(title.dump_wad())
print(f"Downloaded title with Title ID \"{args.tid}\"!")
def handle_nus_tmd(args):
tid = args.tid

View File

@@ -81,7 +81,6 @@ def handle_wad_convert(args):
else:
fatal_error("No valid encryption target was specified!")
output_path = pathlib.Path(args.output)
if args.output is None:
match target:
case "development":
@@ -92,6 +91,8 @@ def handle_wad_convert(args):
output_path = pathlib.Path(input_path.stem + "_vWii" + input_path.suffix)
case _:
fatal_error("No valid encryption target was specified!")
else:
output_path = pathlib.Path(args.output)
if not input_path.exists():
fatal_error(f"The specified WAD file \"{input_path}\" does not exist!")

View File

@@ -18,7 +18,7 @@ from commands.title.nus import *
from commands.title.tmd import *
from commands.title.wad import *
wiipy_ver = "1.5.0"
wiipy_ver = "1.6.0"
if __name__ == "__main__":
# Main argument parser.
@@ -111,6 +111,8 @@ if __name__ == "__main__":
"accepts a WAD file to read the TID from)")
emunand_title_parser.add_argument("-s", "--skip-hash", help="skips validating the hashes of decrypted "
"content (install only)", action="store_true")
emunand_title_parser.add_argument("-v", "--verbose", action="store_true",
help="show verbose installation/uninstallation details")
# Argument parser for the fakesign subcommand.
fakesign_parser = subparsers.add_parser("fakesign", help="fakesign a TMD, Ticket, or WAD (trucha bug)",
@@ -186,8 +188,10 @@ if __name__ == "__main__":
help="download the title to a folder")
nus_title_out_group.add_argument("-w", "--wad", metavar="WAD", type=str,
help="pack a wad with the provided name")
nus_title_parser.add_argument("--wii", help="use original Wii NUS instead of the Wii U servers",
nus_title_parser.add_argument("--wii", help="use the original Wii NUS endpoint instead of the Wii U endpoint",
action="store_true")
nus_title_parser.add_argument("-e", "--endpoint", metavar="ENDPOINT", type=str,
help="use the specified NUS endpoint instead of the official one")
# Content NUS subcommand.
nus_content_parser = nus_subparsers.add_parser("content", help="download a specific content from the NUS",
description="download a specific content from the NUS")