def add(FW,filepath): import sq_tools dump={};newdict={};fwfiles=list() if os.path.exists(fwdb): with open(fwdb) as json_file: data = json.load(json_file) for i in data: dict=data[i] if len(dict.items())>0: dump[i]=dict else: return False if not FW in dump.items(): if filepath.endswith('nsp'): files_list=sq_tools.ret_nsp_offsets(filepath,32) # print(files_list) # print(len(files_list)) for i in range(len(files_list)): f=files_list[i] file=f[0] # print(file) fwfiles.append(file) newdict['files']=fwfiles dump[FW]=newdict app_json = json.dumps(dump, indent=4) with open(fwdb, 'w') as json_file: json_file.write(app_json) print('Added firmware {}'.format(FW)) else: print('Firmware {} is already in json'.format(FW))
def file_location(filepath,t='all',Printdata=False): filenames=retchunks(filepath) # print(filenames) locations=list() if filepath.endswith('.xc0'): files_list=sq_tools.ret_xci_offsets(filepath) elif filepath.endswith('.ns0'): files_list=sq_tools.ret_nsp_offsets(filepath) for file in files_list: if not t.lower()=='all': # print(file) if (str(file[0]).lower()).endswith(t.lower()): location1,tgoffset1=get_file_and_offset(filenames,file[1]) location2,tgoffset2=get_file_and_offset(filenames,file[2]) locations.append([file[0],file[3],location1,tgoffset1,location2,tgoffset2]) if Printdata==True: print('{}'.format(file[0])) print('- Starts in file {} at offset {}'.format(location1[-4:],tgoffset1)) print('- Ends in file {} at offset {}'.format(location2[-4:],tgoffset2)) print('') else:pass else: location1,tgoffset1=get_file_and_offset(filenames,file[1]) location2,tgoffset2=get_file_and_offset(filenames,file[2]) locations.append([file[0],file[3],location1,tgoffset1,location2,tgoffset2]) if Printdata==True: print('{}'.format(file[0])) print('- Starts in file {} at offset {}'.format(location1[-4:],tgoffset1)) print('- Ends in file {} at offset {}'.format(location2[-4:],tgoffset2)) print('') return locations
def getnpdmdata(filename,remotelocation=False): filename=html.unescape(filename) print('* Reading Data from Npdm') sys.stdout.flush() if remotelocation != False: global globalpath; global globalremote if globalpath!=filename: globalpath=filename lib,TD,libpath=get_library_from_path(remote_lib_file,filename) ID,name,type,size,md5,remote=DrivePrivate.get_Data(filename,TD=TD,Print=False) globalremote=remote feed=DriveHtmlInfo.read_npdm(file=globalremote) if feed=='': feed=html_feed(feed,2,message=str('No npdm in the file')) eel.set_npdm_data(feed) return else: if filename.endswith('.nsp')or filename.endswith('.nsx') or filename.endswith('.nsz'): f = Fs.ChromeNsp(filename, 'rb') files_list=sq_tools.ret_nsp_offsets(filename) elif filename.endswith('.xci') or filename.endswith('.xcz'): f = Fs.ChromeXci(filename) files_list=sq_tools.ret_xci_offsets(filename) else: eel.set_npdm_data("") return feed=f.read_npdm(files_list) f.flush() f.close() if feed=='': feed=html_feed(feed,2,message=str('No npdm in the file')) eel.set_npdm_data(feed) return
def read_start(filepath): filenames = retchunks(filepath) f = chain_streams(generate_open_file_streams(filenames)) if filepath.endswith('.xc0'): files_list = sq_tools.ret_xci_offsets(filepath) elif filepath.endswith('.ns0'): files_list = sq_tools.ret_nsp_offsets(filepath) print(files_list) # feed=f.read(0x500) # Hex.dump(feed) f.flush() f.close()
def getnpdmdata(filename): print('* Reading Data from Npdm') if filename.endswith('.nsp')or filename.endswith('.nsx'): f = Fs.ChromeNsp(filename, 'rb') files_list=sq_tools.ret_nsp_offsets(filename) elif filename.endswith('.xci'): f = Fs.ChromeXci(filename) files_list=sq_tools.ret_xci_offsets(filename) else: return "" feed=f.read_npdm(files_list) f.flush() f.close() if feed=='': feed=html_feed(feed,2,message=str('No npdm in the file')) return feed
def install_nsp_csv(filepath, destiny="SD", cachefolder=None, override=False, keypatch=False): check_connection() if cachefolder == None: cachefolder = os.path.join(ztools_dir, '_mtp_cache_') files_list = sq_tools.ret_nsp_offsets(filepath) print(f"Installing {filepath} by content") counter = 0 for i in range(len(files_list)): entry = files_list[i] cnmtfile = entry[0] if cnmtfile.endswith('.cnmt.nca'): counter += 1 print(f"- Detected {counter} content ids") for i in range(len(files_list)): entry = files_list[i] cnmtfile = entry[0] if cnmtfile.endswith('.cnmt.nca'): target_cnmt = cnmtfile nspname = gen_nsp_parts_spec1(filepath, target_cnmt=target_cnmt, cachefolder=cachefolder, keypatch=keypatch) if filepath.endswith('nsz'): nspname = nspname[:-1] + 'z' files_csv = os.path.join(cachefolder, 'files.csv') process = subprocess.Popen([ nscb_mtp, "InstallfromCSV", "-cs", files_csv, "-nm", nspname, "-dst", destiny ]) while process.poll() == None: if process.poll() != None: process.terminate() counter -= 1 print('\n- Still ' + str(counter) + ' subitems to process') if counter > 0: print("") if os.path.exists(cachefolder): for f in os.listdir(cachefolder): fp = os.path.join(cachefolder, f) try: shutil.rmtree(fp) except OSError: os.remove(fp)
def verify_ticket(fp): if fp.endswith('.nsp') or fp.endswith('.nsz'): files_list=sq_tools.ret_nsp_offsets(fp) for i in range(len(files_list)): entry=files_list[i] filepath=entry[0] if filepath.endswith('.tick'): pass # f=Nsp(fp,'rb') # f.flush() # f.close() elif fp.endswith('.xci') or fp.endswith('.xcz'): files_list=sq_tools.ret_xci_offsets(fp) pass # f=Xci(fp) # f.flush() # f.close()
def showicon(filename): filename=html.unescape(filename) # global globalocalpath; # if globalocalpath!=filename: # result=deepcheck_path(globalocalpath,filename) # if result==False: # globalocalpath=filename # else: # filename=globalocalpath print('* Seeking icon') sys.stdout.flush() # print(filename) try: if filename.endswith('.nsp')or filename.endswith('.nsx') or filename.endswith('.nsz'): files_list=sq_tools.ret_nsp_offsets(filename) f = Fs.Nsp(filename, 'rb') elif filename.endswith('.xci') or filename.endswith('.xcz'): files_list=sq_tools.ret_xci_offsets(filename) f = Fs.Xci(filename) elif filename.endswith('.xc0') or filename.endswith('.ns0') or filename.endswith('00'): a=file_chunk.icon_info(filename) encoded = b64encode(a).decode("ascii") data= "data:image/png;base64, " + encoded eel.setImage(data) return else: eel.setImage("") return a=f.icon_info(files_list) f.flush() f.close() encoded = b64encode(a).decode("ascii") data= "data:image/png;base64, " + encoded eel.setImage(data) return except BaseException as e: Print.error('Exception: ' + str(e)) sys.stdout.flush() iconurl=retrieve_icon_from_server(filename) if iconurl!=False: eel.setImage(iconurl) return else: eel.setImage("") return
def get_key_fromdict(fp): if fp.endswith('.nsp') or fp.endswith('.nsx'): files_list=sq_tools.ret_nsp_offsets(fp) files=list();filesizes=list() fplist=list() for k in range(len(files_list)): entry=files_list[k] fplist.append(entry[0]) for i in range(len(files_list)): entry=files_list[i] filepath=entry[0] if filepath.endswith('.cnmt.nca'): f=Nsp(fp,'rb') titleid,titleversion,base_ID,keygeneration,rightsId,RSV,RGV,ctype,metasdkversion,exesdkversion,hasHtmlManual,Installedsize,DeltaSize,ncadata=f.get_data_from_cnmt(filepath) titlekey,dectkey=f.db_get_titlekey(rightsId,keygeneration) f.flush() f.close() # print(titlekey);print(rightsId) return titlekey
def showicon(filename): print('* Seeking icon') try: if filename.endswith('.nsp')or filename.endswith('.nsx'): files_list=sq_tools.ret_nsp_offsets(filename) f = Fs.Nsp(filename, 'rb') elif filename.endswith('.xci'): files_list=sq_tools.ret_xci_offsets(filename) f = Fs.Xci(filename) else: return "" a=f.icon_info(files_list) f.flush() f.close() encoded = b64encode(a).decode("ascii") return "data:image/png;base64, " + encoded except BaseException as e: # Print.error('Exception: ' + str(e)) iconurl=retrieve_icon_from_server(filename) if iconurl!=False: return iconurl else:return ""
def compress(filePath, ofolder=None, level=17, threads=0, delta=False, ofile=None, buffer=65536, pos=False, nthreads=False): isthreaded = False if pos != False: isthreaded = True elif str(pos) == '0': isthreaded = True else: pos = 0 pos = int(pos) files_list = sq_tools.ret_nsp_offsets(filePath) files = list() filesizes = list() if isthreaded == True and nthreads != False: tqlist = list() for i in range(nthreads): tq = tqdm(total=0, unit='B', unit_scale=True, leave=False, position=i) tqlist.append(tq) fplist = list() for k in range(len(files_list)): entry = files_list[k] fplist.append(entry[0]) for i in range(len(files_list)): entry = files_list[i] cnmtfile = entry[0] if cnmtfile.endswith('.cnmt.nca'): f = squirrelNSP(filePath, 'rb') titleid, titleversion, base_ID, keygeneration, rightsId, RSV, RGV, ctype, metasdkversion, exesdkversion, hasHtmlManual, Installedsize, DeltaSize, ncadata = f.get_data_from_cnmt( cnmtfile) f.flush() f.close() for j in range(len(ncadata)): row = ncadata[j] # print(row) if row['NCAtype'] != 'Meta': test1 = str(row['NcaId']) + '.nca' test2 = str(row['NcaId']) + '.ncz' if test1 in fplist or test2 in fplist: # print(str(row['NcaId'])+'.nca') files.append(str(row['NcaId']) + '.nca') filesizes.append(int(row['Size'])) else: # print(str(row['NcaId'])+'.cnmt.nca') files.append(str(row['NcaId']) + '.cnmt.nca') filesizes.append(int(row['Size'])) for k in range(len(files_list)): entry = files_list[k] fp = entry[0] sz = int(entry[3]) if fp.endswith('xml'): files.append(fp) filesizes.append(sz) for k in range(len(files_list)): entry = files_list[k] fp = entry[0] sz = int(entry[3]) if fp.endswith('.tik'): files.append(fp) filesizes.append(sz) for k in range(len(files_list)): entry = files_list[k] fp = entry[0] sz = int(entry[3]) if fp.endswith('.cert'): files.append(fp) filesizes.append(sz) nspheader = sq_tools.gen_nsp_header(files, filesizes) properheadsize = len(nspheader) compressionLevel = int(level) container = nutFs.factory(filePath) container.open(filePath, 'rb') CHUNK_SZ = buffer if ofolder is None and ofile is None: nszPath = filePath[0:-1] + 'z' elif ofolder is not None: nszPath = os.path.join(ofolder, os.path.basename(filePath[0:-1] + 'z')) elif ofile is not None: nszPath = ofile tsize = properheadsize for sz in filesizes: tsize += sz if isthreaded == True: from colorama import Fore colors = Fore.__dict__ k = 0 l = pos for col in colors: if l > len(colors): l = l - len(colors) color = colors[col] if k == (l + 1): break else: k += 1 t = tqdm(total=tsize, unit='B', unit_scale=True, leave=False, position=pos, bar_format="{l_bar}%s{bar}%s{r_bar}" % (color, Fore.RESET)) else: t = tqdm(total=tsize, unit='B', unit_scale=True, leave=False, position=0) # nszPath = os.path.abspath(nszPath) nszPath = Path(filePath) nszPath = os.path.join(nszPath.parents[0], os.path.basename(filePath[0:-1] + 'z')) if isthreaded == False: t.write('\n Compressing with level %d and %d threads' % (compressionLevel, threads)) t.write('%s -> %s \n' % (filePath, nszPath)) newNsp = nutFs.Pfs0.Pfs0Stream(nszPath, headsize=properheadsize) for file in files: for nspf in container: if nspf._path == file: if isinstance( nspf, nutFs.Nca.Nca ) and nspf.header.contentType == nutFs.Type.Content.DATA and delta == False: if isthreaded == False: t.write('-> Skipping delta fragment') continue if isinstance(nspf, nutFs.Nca.Nca) and ( nspf.header.contentType == nutFs.Type.Content.PROGRAM or nspf.header.contentType == nutFs.Type.Content.PUBLIC_DATA): if isNcaPacked(nspf): cctx = zstandard.ZstdCompressor(level=compressionLevel, threads=threads) newFileName = nspf._path[0:-1] + 'z' f = newNsp.add(newFileName, nspf.size, t, isthreaded) start = f.tell() nspf.seek(0) f.write(nspf.read(ncaHeaderSize)) written = ncaHeaderSize compressor = cctx.stream_writer(f) sections = get_sections(nspf) header = b'NCZSECTN' header += len(sections).to_bytes(8, 'little') i = 0 for fs in sections: i += 1 header += fs.offset.to_bytes(8, 'little') header += fs.size.to_bytes(8, 'little') header += fs.cryptoType.to_bytes(8, 'little') header += b'\x00' * 8 header += fs.cryptoKey header += fs.cryptoCounter f.write(header) written += len(header) timestamp = time.time() decompressedBytes = ncaHeaderSize totsize = 0 for fs in sections: totsize += fs.size for section in sections: #print('offset: %x\t\tsize: %x\t\ttype: %d\t\tiv%s' % (section.offset, section.size, section.cryptoType, str(hx(section.cryptoCounter)))) o = nspf.partition(offset=section.offset, size=section.size, n=None, cryptoType=section.cryptoType, cryptoKey=section.cryptoKey, cryptoCounter=bytearray( section.cryptoCounter), autoOpen=True) while not o.eof(): buffer = o.read(CHUNK_SZ) t.update(len(buffer)) if len(buffer) == 0: raise IOError('read failed') written += compressor.write(buffer) decompressedBytes += len(buffer) compressor.flush(zstandard.FLUSH_FRAME) elapsed = time.time() - timestamp minutes = elapsed / 60 seconds = elapsed % 60 speed = 0 if elapsed == 0 else (nspf.size / elapsed) written = f.tell() - start if isthreaded == False: t.write( '\n * Compressed at %d%% from %s to %s - %s' % (int(written * 100 / nspf.size), str(sq_tools.getSize(decompressedBytes)), str(sq_tools.getSize(written)), nspf._path)) t.write( ' * Compressed in %02d:%02d at speed: %.1f MB/s\n' % (minutes, seconds, speed / 1000000.0)) newNsp.resize(newFileName, written) continue else: print('not packed!') f = newNsp.add(nspf._path, nspf.size, t, isthreaded) nspf.seek(0) while not nspf.eof(): buffer = nspf.read(CHUNK_SZ) t.update(len(buffer)) f.write(buffer) t.close() newNsp.close() if isthreaded == True and nthreads != False: for i in range(nthreads): tqlist[i].close()
def get_header_size(filepath): properheadsize=0;sz=0 if filepath.endswith('xci') or filepath.endswith('xcz'): files_list=sq_tools.ret_xci_offsets(filepath) files=list();filesizes=list() fplist=list() for k in range(len(files_list)): entry=files_list[k] fplist.append(entry[0]) for i in range(len(files_list)): entry=files_list[i] cnmtfile=entry[0] if cnmtfile.endswith('.cnmt.nca'): f=squirrelXCI(filepath) titleid,titleversion,base_ID,keygeneration,rightsId,RSV,RGV,ctype,metasdkversion,exesdkversion,hasHtmlManual,Installedsize,DeltaSize,ncadata=f.get_data_from_cnmt(cnmtfile) for j in range(len(ncadata)): row=ncadata[j] # print(row) if row['NCAtype']!='Meta': test1=str(row['NcaId'])+'.nca';test2=str(row['NcaId'])+'.ncz' if test1 in fplist or test2 in fplist: # print(str(row['NcaId'])+'.nca') files.append(str(row['NcaId'])+'.nca') filesizes.append(int(row['Size'])) sz+=int(row['Size']) elif row['NCAtype']=='Meta': # print(str(row['NcaId'])+'.cnmt.nca') files.append(str(row['NcaId'])+'.cnmt.nca') filesizes.append(int(row['Size'])) sz+=int(row['Size']) sec_hashlist=list() try: for file in files: sha,size,gamecard=f.file_hash(file) # print(sha) if sha != False: sec_hashlist.append(sha) except BaseException as e: Print.error('Exception: ' + str(e)) f.flush() f.close() xci_header,game_info,sig_padding,xci_certificate,root_header,upd_header,norm_header,sec_header,rootSize,upd_multiplier,norm_multiplier,sec_multiplier=sq_tools.get_xciheader(files,filesizes,sec_hashlist) outheader=xci_header outheader+=game_info outheader+=sig_padding outheader+=xci_certificate outheader+=root_header outheader+=upd_header outheader+=norm_header outheader+=sec_header elif filepath.endswith('nsp') or filepath.endswith('nsz'): files_list=sq_tools.ret_nsp_offsets(filepath) files=list();filesizes=list() fplist=list() for k in range(len(files_list)): entry=files_list[k] fplist.append(entry[0]) for i in range(len(files_list)): entry=files_list[i] cnmtfile=entry[0] if cnmtfile.endswith('.cnmt.nca'): f=squirrelNSP(filepath) titleid,titleversion,base_ID,keygeneration,rightsId,RSV,RGV,ctype,metasdkversion,exesdkversion,hasHtmlManual,Installedsize,DeltaSize,ncadata=f.get_data_from_cnmt(cnmtfile) f.flush() f.close() for j in range(len(ncadata)): row=ncadata[j] # print(row) if row['NCAtype']!='Meta': test1=str(row['NcaId'])+'.nca';test2=str(row['NcaId'])+'.ncz' if test1 in fplist or test2 in fplist: # print(str(row['NcaId'])+'.nca') files.append(str(row['NcaId'])+'.nca') filesizes.append(int(row['Size'])) sz+=int(row['Size']) elif row['NCAtype']=='Meta': # print(str(row['NcaId'])+'.cnmt.nca') files.append(str(row['NcaId'])+'.cnmt.nca') filesizes.append(int(row['Size'])) sz+=int(row['Size']) f.flush() f.close() outheader = sq_tools.gen_nsp_header(files,filesizes) properheadsize=len(outheader) return properheadsize,keygeneration,sz
def gen_nsp_parts_spec1(filepath,target_cnmt=None,cachefolder=None,keypatch=False): if keypatch!=False: try: keypatch=int(keypatch) except: keypatch=False if cachefolder==None: cachefolder=os.path.join(ztools_dir, '_mtp_cache_') if not os.path.exists(cachefolder): os.makedirs(cachefolder) else: for f in os.listdir(cachefolder): fp = os.path.join(cachefolder, f) try: shutil.rmtree(fp) except OSError: os.remove(fp) files_list=sq_tools.ret_nsp_offsets(filepath) files=list();filesizes=list() fplist=list() for k in range(len(files_list)): entry=files_list[k] fplist.append(entry[0]) if target_cnmt==None: for i in range(len(files_list)): entry=files_list[i] cnmtfile=entry[0] if cnmtfile.endswith('.cnmt.nca'): target_cnmt=cnmtfile break for i in range(len(files_list)): entry=files_list[i] cnmtfile=entry[0] if cnmtfile.endswith('.cnmt.nca') and target_cnmt==cnmtfile: f=squirrelNSP(filepath) titleid,titleversion,base_ID,keygeneration,rightsId,RSV,RGV,ctype,metasdkversion,exesdkversion,hasHtmlManual,Installedsize,DeltaSize,ncadata=f.get_data_from_cnmt(cnmtfile) f.flush() f.close() for j in range(len(ncadata)): row=ncadata[j] # print(row) if row['NCAtype']!='Meta' and row['NCAtype']!='Program': test1=str(row['NcaId'])+'.nca';test2=str(row['NcaId'])+'.ncz' if test1 in fplist: files.append(str(row['NcaId'])+'.nca') filesizes.append(int(row['Size'])) elif test2 in fplist: files.append(str(row['NcaId'])+'.ncz') for k in range(len(files_list)): entry=files_list[k] if entry[0]==test2: filesizes.append(int(entry[3])) break for j in range(len(ncadata)): row=ncadata[j] if row['NCAtype']=='Meta': # print(str(row['NcaId'])+'.cnmt.nca') files.append(str(row['NcaId'])+'.cnmt.nca') filesizes.append(int(row['Size'])) for j in range(len(ncadata)): row=ncadata[j] # print(row) if row['NCAtype']=='Program': test1=str(row['NcaId'])+'.nca';test2=str(row['NcaId'])+'.ncz' if test1 in fplist: files.append(str(row['NcaId'])+'.nca') filesizes.append(int(row['Size'])) elif test2 in fplist: files.append(str(row['NcaId'])+'.ncz') for k in range(len(files_list)): entry=files_list[k] if entry[0]==test2: filesizes.append(int(entry[3])) break break f.flush() f.close() outheader = sq_tools.gen_nsp_header(files,filesizes) properheadsize=len(outheader) # print(properheadsize) # print(bucketsize) i=0;sum=properheadsize; nsp=squirrelNSP(filepath) outfile=os.path.join(cachefolder, "0") outf = open(outfile, 'w+b') outf.write(outheader) written=0 for fi in files: for nca in nsp: if nca._path==fi: nca=Nca(nca) crypto1=nca.header.getCryptoType() crypto2=nca.header.getCryptoType2() if crypto2>crypto1: masterKeyRev=crypto2 if crypto2<=crypto1: masterKeyRev=crypto1 crypto = aes128.AESECB(Keys.keyAreaKey(Keys.getMasterKeyIndex(masterKeyRev), nca.header.keyIndex)) hcrypto = aes128.AESXTS(uhx(Keys.get('header_key'))) gc_flag='00'*0x01 crypto1=nca.header.getCryptoType() crypto2=nca.header.getCryptoType2() if nca.header.getRightsId() != 0: nca.rewind() if crypto2>crypto1: masterKeyRev=crypto2 if crypto2<=crypto1: masterKeyRev=crypto1 from mtp_tools import get_nca_ticket check,titleKey=get_nca_ticket(filepath,fi) if check==False: sys.exit("Can't verify titleckey") titleKeyDec = Keys.decryptTitleKey(titleKey, Keys.getMasterKeyIndex(int(masterKeyRev))) encKeyBlock = crypto.encrypt(titleKeyDec * 4) if str(keypatch) != "False": t = tqdm(total=False, unit='B', unit_scale=False, leave=False) if keypatch < nca.header.getCryptoType2(): encKeyBlock,crypto1,crypto2=squirrelNSP.get_new_cryptoblock(squirrelNSP,nca,keypatch,encKeyBlock,t) t.close() if nca.header.getRightsId() == 0: nca.rewind() encKeyBlock = nca.header.getKeyBlock() if str(keypatch) != "False": t = tqdm(total=False, unit='B', unit_scale=False, leave=False) if keypatch < nca.header.getCryptoType2(): encKeyBlock,crypto1,crypto2=squirrelNSP.get_new_cryptoblock(squirrelNSP,nca,keypatch,encKeyBlock,t) t.close() nca.rewind() i=0 newheader=nsp.get_newheader(nca,encKeyBlock,crypto1,crypto2,hcrypto,gc_flag) outf.write(newheader) written+=len(newheader) nca.seek(0xC00) break else:pass
def gen_mxci_parts(input_files, cachefolder=None, keypatch=False): from listmanager import calculate_name if keypatch != False: try: keypatch = int(keypatch) except: keypatch = False if cachefolder == None: cachefolder = os.path.join(ztools_dir, '_mtp_cache_') if not os.path.exists(cachefolder): os.makedirs(cachefolder) else: for f in os.listdir(cachefolder): fp = os.path.join(cachefolder, f) try: shutil.rmtree(fp) except OSError: os.remove(fp) end_name, prlist = calculate_name(input_files, romanize=True, ext='.xci') print(f"Calculated name {end_name}") outheader, properheadsize, sz, files = gen_multi_file_header( prlist, input_files) properheadsize = len(outheader) outfile = os.path.join(cachefolder, "0") outf = open(outfile, 'w+b') outf.write(outheader) # print(properheadsize) # print(bucketsize) i = 0 sum = properheadsize for fi in files: for filepath in input_files: if filepath.endswith('xci'): xci = squirrelXCI(filepath) written = 0 for nspF in xci.hfs0: if str(nspF._path) == "secure": for nca in nspF: if nca._path == fi: nca = Nca(nca) crypto1 = nca.header.getCryptoType() crypto2 = nca.header.getCryptoType2() if crypto2 > crypto1: masterKeyRev = crypto2 if crypto2 <= crypto1: masterKeyRev = crypto1 crypto = aes128.AESECB( Keys.keyAreaKey( Keys.getMasterKeyIndex(masterKeyRev), nca.header.keyIndex)) hcrypto = aes128.AESXTS( uhx(Keys.get('header_key'))) gc_flag = '00' * 0x01 crypto1 = nca.header.getCryptoType() crypto2 = nca.header.getCryptoType2() if nca.header.getRightsId() != 0: nca.rewind() if crypto2 > crypto1: masterKeyRev = crypto2 if crypto2 <= crypto1: masterKeyRev = crypto1 from mtp_tools import get_nca_ticket check, titleKey = get_nca_ticket( filepath, fi) if check == False: sys.exit("Can't verify titleckey") titleKeyDec = Keys.decryptTitleKey( titleKey, Keys.getMasterKeyIndex( int(masterKeyRev))) encKeyBlock = crypto.encrypt(titleKeyDec * 4) if str(keypatch) != "False": t = tqdm(total=False, unit='B', unit_scale=False, leave=False) if keypatch < nca.header.getCryptoType2( ): encKeyBlock, crypto1, crypto2 = squirrelXCI.get_new_cryptoblock( squirrelXCI, nca, keypatch, encKeyBlock, t) t.close() if nca.header.getRightsId() == 0: nca.rewind() encKeyBlock = nca.header.getKeyBlock() if str(keypatch) != "False": t = tqdm(total=False, unit='B', unit_scale=False, leave=False) if keypatch < nca.header.getCryptoType2( ): encKeyBlock, crypto1, crypto2 = squirrelXCI.get_new_cryptoblock( squirrelXCI, nca, keypatch, encKeyBlock, t) t.close() nca.rewind() i = 0 newheader = xci.get_newheader( nca, encKeyBlock, crypto1, crypto2, hcrypto, gc_flag) outf.write(newheader) written += len(newheader) nca.seek(0xC00) break else: pass xci.flush() xci.close() elif filepath.endswith('nsp'): nsp = squirrelNSP(filepath) written = 0 for nca in nsp: if nca._path == fi: nca = Nca(nca) crypto1 = nca.header.getCryptoType() crypto2 = nca.header.getCryptoType2() if crypto2 > crypto1: masterKeyRev = crypto2 if crypto2 <= crypto1: masterKeyRev = crypto1 crypto = aes128.AESECB( Keys.keyAreaKey( Keys.getMasterKeyIndex(masterKeyRev), nca.header.keyIndex)) hcrypto = aes128.AESXTS(uhx(Keys.get('header_key'))) gc_flag = '00' * 0x01 crypto1 = nca.header.getCryptoType() crypto2 = nca.header.getCryptoType2() if nca.header.getRightsId() != 0: nca.rewind() if crypto2 > crypto1: masterKeyRev = crypto2 if crypto2 <= crypto1: masterKeyRev = crypto1 from mtp_tools import get_nca_ticket check, titleKey = get_nca_ticket(filepath, fi) if check == False: sys.exit("Can't verify titleckey") titleKeyDec = Keys.decryptTitleKey( titleKey, Keys.getMasterKeyIndex(int(masterKeyRev))) encKeyBlock = crypto.encrypt(titleKeyDec * 4) if str(keypatch) != "False": t = tqdm(total=False, unit='B', unit_scale=False, leave=False) if keypatch < nca.header.getCryptoType2(): encKeyBlock, crypto1, crypto2 = squirrelNSP.get_new_cryptoblock( squirrelNSP, nca, keypatch, encKeyBlock, t) t.close() if nca.header.getRightsId() == 0: nca.rewind() encKeyBlock = nca.header.getKeyBlock() if str(keypatch) != "False": t = tqdm(total=False, unit='B', unit_scale=False, leave=False) if keypatch < nca.header.getCryptoType2(): encKeyBlock, crypto1, crypto2 = squirrelNSP.get_new_cryptoblock( squirrelNSP, nca, keypatch, encKeyBlock, t) t.close() nca.rewind() i = 0 newheader = nsp.get_newheader(nca, encKeyBlock, crypto1, crypto2, hcrypto, gc_flag) outf.write(newheader) written += len(newheader) nca.seek(0xC00) break else: pass nsp.flush() nsp.close() outf.flush() outf.close() tfile = os.path.join(cachefolder, "files.csv") with open(tfile, 'w') as csvfile: csvfile.write("{}|{}|{}|{}|{}|{}\n".format("step", "filepath", "size", "targetsize", "off1", "off2")) csvfile.write("{}|{}|{}|{}|{}|{}\n".format(0, outfile, properheadsize + written, properheadsize, 0, properheadsize)) k = 0 l = 0 for fi in files: for filepath in input_files: if filepath.endswith('xci'): files_list = sq_tools.ret_xci_offsets(filepath) elif filepath.endswith('nsp'): files_list = sq_tools.ret_nsp_offsets(filepath) for j in files_list: if j[0] == fi: csvfile.write("{}|{}|{}|{}|{}|{}\n".format( k + 1, outfile, properheadsize + written, 0xC00, (properheadsize + l * 0xC00), (properheadsize + (l * 0xC00) + 0xC00))) off1 = j[1] + 0xC00 off2 = j[2] targetsize = j[3] - 0xC00 csvfile.write("{}|{}|{}|{}|{}|{}\n".format( k + 2, filepath, (os.path.getsize(filepath)), targetsize, off1, off2)) break k += 2 l += 1 return end_name