From a4e07b796ac2d06b4c5cefcb0b2de0b41afd3dd1 Mon Sep 17 00:00:00 2001 From: xprism1 Date: Thu, 29 Jun 2023 11:57:54 +0800 Subject: [PATCH] ctr_cdn: add other titlekey passwords --- lib/ctr_cdn.py | 42 ++++++++++++++++++++++++++++++++++++++++-- utils.py | 14 +++++++++----- 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/lib/ctr_cdn.py b/lib/ctr_cdn.py index c5eb39e..21e8302 100644 --- a/lib/ctr_cdn.py +++ b/lib/ctr_cdn.py @@ -16,8 +16,46 @@ class CDNReader: self.tik_read = tikReader(tik, dev) self.titlekey = self.tik_read.titlekey else: # Use titlekey generation algorithm - self.titlekey = hextobytes(CTR.titlekey_gen(self.tmd_read.titleID, 'mypass')) - + if self.tmd_read.titleID[3:5] == '48': + if self.tmd_read.titleID in [ + '000480044b424145', + '000480044b474e4a', + '000480044b4f514a', + '000480044b524e45', + '000480044b54394a', + '000480044b594945', + ]: + pw = '5037' + elif self.tmd_read.titleID in [ + '00048005484e4443', + '00048005484e444b', + ]: + pw = 'redsst' + else: + pw = 'mypass' + self.titlekey = hextobytes(CTR.titlekey_gen(self.tmd_read.titleID, pw)) + else: + for i in self.content_files: + for name, info in self.tmd_read.files.items(): + if name.split('.')[1] == i: + file = (i, info['iv']) + break + + self.titlekey = b'' + for i in ['mypass', 'password', 'nintendo', 'redsst']: + titlekey = hextobytes(CTR.titlekey_gen(self.tmd_read.titleID, i)) + cipher = AES.new(titlekey, AES.MODE_CBC, iv=file[1]) + with open(file[0], 'rb') as f: + magic = cipher.decrypt(f.read(0x110))[0x100:0x104] + try: + if magic.decode('utf-8') == 'NCCH': + self.titlekey = titlekey + break + except UnicodeDecodeError: + continue + if self.titlekey == b'': + raise Exception('Could not generate valid titlekey') + def decrypt(self): for i in self.content_files: for name, info in self.tmd_read.files.items(): diff --git a/utils.py b/utils.py index f82b7d2..58e26f7 100644 --- a/utils.py +++ b/utils.py @@ -547,15 +547,19 @@ def cdn2cia(path, out='', title_ver='', cdn_dev=0, cia_dev=0): t = TMDReader(tmd) if out == '': out = f'{name}.{t.hdr.title_ver}.cia' - if tik == '': - tikBuilder(titleID=t.titleID, title_ver=t.hdr.title_ver, regen_sig=regen_sig, out='tik') - tik = 'tik' - cdn = CDNReader(content_files=content_files, tmd=tmd, tik=tik, dev=cdn_dev) + cdn = CDNReader(content_files=content_files, tmd=tmd, dev=cdn_dev) cdn.decrypt() cf = [i for i in os.listdir('.') if i.endswith('.ncch') or i.endswith('.nds')] - CIABuilder(content_files=cf, tik=tik, tmd=tmd, meta=1, dev=cia_dev, out='tmp.cia') + if tik == '': + tikBuilder(titleID=t.titleID, title_ver=t.hdr.title_ver, titlekey=hex(readbe(cdn.titlekey))[2:].zfill(32), regen_sig=regen_sig, out='tik') + tik = 'tik' + + meta = 1 + if t.titleID[3:5] == '48': + meta = 0 + CIABuilder(content_files=cf, tik=tik, tmd=tmd, meta=meta, dev=cia_dev, out='tmp.cia') for i in cf: os.remove(i) if os.path.isfile('tik'):