示例#1
0
    def open(self,
             file=None,
             mode='rb',
             cryptoType=-1,
             cryptoKey=-1,
             cryptoCounter=-1):
        super(Nca, self).open(file, mode, cryptoType, cryptoKey, cryptoCounter)

        self.header = NcaHeader()
        self.partition(0x0, 0xC00, self.header, Fs.Type.Crypto.XTS,
                       uhx(Keys.get('header_key')))
        #Print.info('partition complete, seeking')
        self.header.seek(0x400)
        # Print.info('reading')
        # Hex.dump(self.header.read(0x200))
        # exit()

        if self._path is not None and self._path.endswith('.ncz'):
            return

        for i in range(4):
            hdr = self.header.read(0x200)
            section = BaseFs(hdr, cryptoKey=self.header.titleKeyDec)
            fs = GetSectionFilesystem(hdr, cryptoKey=-1)
            #Print.info('fs type = ' + hex(fs.fsType))
            #Print.info('fs crypto = ' + hex(fs.cryptoType))
            #Print.info('st end offset = ' + str(self.header.sectionTables[i].endOffset - self.header.sectionTables[i].offset))
            #Print.info('fs offset = ' + hex(self.header.sectionTables[i].offset))
            #Print.info('fs section start = ' + hex(fs.sectionStart))
            #Print.info('titleKey = ' + hex(self.header.titleKeyDec))

            self.partition(self.header.sectionTables[i].offset,
                           self.header.sectionTables[i].endOffset -
                           self.header.sectionTables[i].offset,
                           section,
                           cryptoKey=self.header.titleKeyDec)

            try:
                section.partition(fs.sectionStart,
                                  section.size - fs.sectionStart, fs)
            except BaseException as e:
                pass
                # Print.info(e)
                # raise

            if fs.fsType:
                self.sectionFilesystems.append(fs)
                self.sections.append(section)

            try:
                fs.open(None, 'rb')
            except BaseException as e:
                Print.error(str(e))
                traceback.print_exc(file=sys.stdout)

        self.titleKeyDec = None
示例#2
0
    def verify(self):
        ncaHeader = NcaHeader()
        ncaHeader.open(
            MemoryFile(self.ncaHeader, Type.Crypto.XTS,
                       uhx(Keys.get('header_key'))))

        id = ncaHeader.rightsId[0:16].decode().upper()
        if str(self.titleId) != id:
            raise IndexError('Title IDs do not match!  ' + str(self.titleId) +
                             ' != ' + id)

        decKey = Keys.decryptTitleKey(uhx(self.titleKey), ncaHeader.masterKey)

        pfs0 = Fs.Pfs0(self.sectionHeaderBlock)
        '''
		print('encKey = ' + str(self.titleKey))
		print('decKey = ' + str(hx(decKey)))
		print('master key = ' + str(ncaHeader.masterKey))
		print('ctr = ' + str(hx(pfs0.cryptoCounter)))
		print('offset = ' + str(self.pfs0Offset))
		'''

        if self.sectionHeaderBlock[8:12] == b'IVFC':
            #Hex.dump(self.sectionHeaderBlock)
            #Print.info(hx(self.sectionHeaderBlock[0xc8:0xc8+0x20]).decode('utf-8'))
            mem = MemoryFile(self.pfs0Header,
                             Type.Crypto.CTR,
                             decKey,
                             pfs0.cryptoCounter,
                             offset=self.pfs0Offset)
            data = mem.read()
            #Hex.dump(data)
            #print('hash = %s' % str(_sha256(data)))
            if hx(self.sectionHeaderBlock[0xc8:0xc8 +
                                          0x20]).decode('utf-8') == str(
                                              _sha256(data)):
                return True
            else:
                return False
        else:
            mem = MemoryFile(self.pfs0Header,
                             Type.Crypto.CTR,
                             decKey,
                             pfs0.cryptoCounter,
                             offset=self.pfs0Offset)
            magic = mem.read()[0:4]
            if magic != b'PFS0':
                raise LookupError('Title Key is incorrect!')

        return True
示例#3
0
文件: Nca.py 项目: zhentan/nut
    def open(self,
             file=None,
             mode='rb',
             cryptoType=-1,
             cryptoKey=-1,
             cryptoCounter=-1):

        super(Nca, self).open(file, mode, cryptoType, cryptoKey, cryptoCounter)

        self.header = NcaHeader()
        self.partition(0x0, 0xC00, self.header, Fs.Type.Crypto.XTS,
                       uhx(Keys.get('header_key')))
        #Print.info('partition complete, seeking')
        self.header.seek(0x400)
        #Print.info('reading')
        #Hex.dump(self.header.read(0x200))
        #exit()

        for i in range(4):
            fs = GetSectionFilesystem(self.header.read(0x200),
                                      cryptoKey=self.header.titleKeyDec)
            #Print.info('fs type = ' + hex(fs.fsType))
            #Print.info('fs crypto = ' + hex(fs.cryptoType))
            #Print.info('st end offset = ' + str(self.header.sectionTables[i].endOffset - self.header.sectionTables[i].offset))
            #Print.info('fs offset = ' + hex(self.header.sectionTables[i].offset))
            #Print.info('fs section start = ' + hex(fs.sectionStart))
            #Print.info('titleKey = ' + hex(self.header.titleKeyDec))
            try:
                self.partition(self.header.sectionTables[i].offset +
                               fs.sectionStart,
                               self.header.sectionTables[i].endOffset -
                               self.header.sectionTables[i].offset,
                               fs,
                               cryptoKey=self.header.titleKeyDec)
            except BaseException as e:
                pass
                #Print.info(e)
                #raise

            if fs.fsType:
                self.sectionFilesystems.append(fs)

        self.titleKeyDec = None
        self.masterKey = None
示例#4
0
文件: Nca.py 项目: yukun451/nut
    def verifyKey(self, userkey):
        if not self.header.hasTitleRights():
            titleKeyDec = Keys.decryptTitleKey(
                file.getTitleKeyBlock().to_bytes(16, byteorder='big'),
                Keys.getMasterKeyIndex(self.masterKey()))
        else:
            encKey = userkey

            titleKeyDec = Keys.decryptTitleKey(
                encKey, Keys.getMasterKeyIndex(self.masterKey()))
            '''
			print('\nTesting {} with:'.format(self))
			print('- Keygeneration {}'.format(self.masterKey()))
			print('- Encrypted key {}'.format(str(hx(encKey))[2:-1]))
			print('- Decrypted key {}'.format(str(hx(titleKeyDec))[2:-1]))
			'''

        decKey = titleKeyDec
        f = self

        if self.header.getRightsId() != 0:
            for fs in self:
                # print(fs.fsType)
                # print(fs.cryptoType)
                if fs.fsType == Type.Fs.PFS0 and fs.cryptoType == Type.Crypto.CTR:
                    f.seek(0)
                    ncaHeader = NcaHeader()
                    ncaHeader.open(
                        MemoryFile(f.read(0x400), Type.Crypto.XTS,
                                   uhx(Keys.get('header_key'))))
                    pfs0 = fs
                    sectionHeaderBlock = fs.buffer

                    #fs.f.setKey(b'\x00' * 16)
                    #print('- Current key {}'.format(str(hx(fs.f.cryptoKey))[2:-1]))

                    fs.seek(0)
                    pfs0Offset = 0  # int.from_bytes(sectionHeaderBlock[0x38:0x40], byteorder='little', signed=False)
                    pfs0Header = fs.read(0x10)

                    # Hex.dump(sectionHeaderBlock)
                    #mem = MemoryFile(pfs0Header, Type.Crypto.CTR, decKey, pfs0.cryptoCounter, offset = pfs0Offset)
                    data = pfs0Header  # mem.read();
                    # Hex.dump(pfs0Header)
                    magic = data[0:4]
                    # print(magic)
                    if magic != b'PFS0':
                        return False
                    else:
                        return True

                if fs.fsType == Type.Fs.ROMFS and fs.cryptoType == Type.Crypto.CTR:
                    f.seek(0)
                    ncaHeader = NcaHeader()
                    ncaHeader.open(
                        MemoryFile(f.read(0x400), Type.Crypto.XTS,
                                   uhx(Keys.get('header_key'))))
                    ncaHeader = f.read(0x400)
                    pfs0 = fs
                    sectionHeaderBlock = fs.buffer

                    levelOffset = int.from_bytes(sectionHeaderBlock[0x18:0x20],
                                                 byteorder='little',
                                                 signed=False)
                    levelSize = int.from_bytes(sectionHeaderBlock[0x20:0x28],
                                               byteorder='little',
                                               signed=False)

                    pfs0Offset = levelOffset
                    f.seek(pfs0Offset + fs.f.offset)
                    pfs0Header = f.read(levelSize)

                    # fs.seek(pfs0Offset)
                    #pfs0Header = fs.read(levelSize)

                    #print(sectionHeaderBlock[8:12] == b'IVFC')
                    if sectionHeaderBlock[8:12] == b'IVFC':
                        # Hex.dump(sectionHeaderBlock)
                        # Print.info(hx(sectionHeaderBlock[0xc8:0xc8+0x20]).decode('utf-8'))
                        mem = MemoryFile(pfs0Header,
                                         Type.Crypto.CTR,
                                         decKey,
                                         pfs0.cryptoCounter,
                                         offset=fs.f.offset)

                        data = mem.read()

                        #Hex.dump(data, 48)
                        #print('hash = %s' % str(sha256(data).hexdigest()))
                        if hx(sectionHeaderBlock[0xc8:0xc8 +
                                                 0x20]).decode('utf-8') == str(
                                                     sha256(data).hexdigest()):
                            return True
                        else:
                            return False
                    else:
                        mem = MemoryFile(pfs0Header,
                                         Type.Crypto.CTR,
                                         decKey,
                                         pfs0.cryptoCounter,
                                         offset=pfs0Offset)
                        data = mem.read()
                        # Hex.dump(data)
                        magic = mem.read()[0:4]
                        # print(magic)
                        if magic != b'PFS0':
                            pass
                        else:
                            return True

                if fs.fsType == Type.Fs.ROMFS and fs.cryptoType == Type.Crypto.BKTR and str(
                        f.header.contentType) == 'Content.PROGRAM':
                    f.seek(0)
                    ncaHeader = NcaHeader()
                    ncaHeader.open(
                        MemoryFile(f.read(0x400), Type.Crypto.XTS,
                                   uhx(Keys.get('header_key'))))
                    ncaHeader = f.read(0x400)
                    pfs0 = fs
                    sectionHeaderBlock = fs.buffer

                    levelOffset = int.from_bytes(sectionHeaderBlock[0x18:0x20],
                                                 byteorder='little',
                                                 signed=False)
                    levelSize = int.from_bytes(sectionHeaderBlock[0x20:0x28],
                                               byteorder='little',
                                               signed=False)

                    pfs0Offset = fs.offset + levelOffset
                    f.seek(pfs0Offset)
                    pfs0Header = f.read(levelSize)

                    if sectionHeaderBlock[8:12] == b'IVFC':
                        for i in range(10):
                            ini = 0x100 + (i * 0x10)
                            fin = 0x110 + (i * 4)
                            test = sectionHeaderBlock[ini:fin]
                            if test == b'BKTR':
                                return True
        return False