Exemplo n.º 1
0
Arquivo: Nca.py Projeto: yukun451/nut
    def setStandardCryptoBuffer(self, buffer, keyGen):
        buffer[0x30:0x40] = b'\x00' * 0x10

        emptyKey = b'\x00' * 0x10

        kek = Keys.keyAreaKey(Keys.getMasterKeyIndex(keyGen), self.keyIndex)
        crypto = aes128.AESECB(kek)

        encKeyBlock = crypto.encrypt(emptyKey + emptyKey + self.titleKeyDec +
                                     emptyKey)

        buffer[0x100:0x140] = encKeyBlock

        if keyGen <= 2:
            buffer[0x6] = keyGen
            buffer[0x20] = 0
        else:
            buffer[0x6] = 2
            buffer[0x20] = keyGen

        return buffer
Exemplo n.º 2
0
    def removeTitleRights(self):
        if not Titles.contains(self.titleId):
            raise IOError('No title key found in database! ' + self.titleId)

        ticket = self.ticket()
        masterKeyRev = ticket.getMasterKeyRevision()
        titleKeyDec = Keys.decryptTitleKey(
            ticket.getTitleKeyBlock().to_bytes(16, byteorder='big'),
            Keys.getMasterKeyIndex(masterKeyRev))
        rightsId = ticket.getRightsId()

        Print.info('rightsId =\t' + hex(rightsId))
        Print.info('titleKeyDec =\t' + str(hx(titleKeyDec)))
        Print.info('masterKeyRev =\t' + hex(masterKeyRev))

        for nca in self:
            if type(nca) == Nca:
                if nca.header.getCryptoType2() != masterKeyRev:
                    pass
                    raise IOError('Mismatched masterKeyRevs!')

        ticket.setRightsId(0)

        for nca in self:
            if type(nca) == Nca:
                if nca.header.getRightsId() == 0:
                    continue

                kek = Keys.keyAreaKey(Keys.getMasterKeyIndex(masterKeyRev),
                                      nca.header.keyIndex)
                Print.info('writing masterKeyRev for %s, %d' %
                           (str(nca._path), masterKeyRev))
                Print.info('kek =\t' + hx(kek).decode())
                crypto = aes128.AESECB(kek)

                encKeyBlock = crypto.encrypt(titleKeyDec * 4)
                nca.header.setRightsId(0)
                nca.header.setKeyBlock(encKeyBlock)
                Hex.dump(encKeyBlock)
Exemplo n.º 3
0
    def open(self,
             file=None,
             mode='rb',
             cryptoType=-1,
             cryptoKey=-1,
             cryptoCounter=-1):
        super(NcaHeader, self).open(file, mode, cryptoType, cryptoKey,
                                    cryptoCounter)
        self.rewind()
        self.signature1 = self.read(0x100)
        self.signature2 = self.read(0x100)
        self.magic = self.read(0x4)
        self.isGameCard = self.readInt8()
        self.contentType = self.readInt8()

        try:
            self.contentType = Fs.Type.Content(self.contentType)
        except:
            pass

        self.cryptoType = self.readInt8()
        self.keyIndex = self.readInt8()
        self.size = self.readInt64()
        self.titleId = hx(self.read(8)[::-1]).decode('utf-8').upper()
        self.contentIndex = self.readInt32()
        self.sdkVersion = self.readInt32()
        self.cryptoType2 = self.readInt8()

        self.read(0xF)  # padding

        self.rightsId = hx(self.read(0x10))

        if self.magic not in [b'NCA3', b'NCA2']:
            raise Exception('Failed to decrypt NCA header: ' + str(self.magic))

        self.sectionHashes = []

        for i in range(4):
            self.sectionTables.append(SectionTableEntry(self.read(0x10)))

        for i in range(4):
            self.sectionHashes.append(self.sectionTables[i])

        self.masterKey = (self.cryptoType if self.cryptoType > self.cryptoType2
                          else self.cryptoType2) - 1

        if self.masterKey < 0:
            self.masterKey = 0

        self.encKeyBlock = self.getKeyBlock()
        #for i in range(4):
        #	offset = i * 0x10
        #	key = encKeyBlock[offset:offset+0x10]
        #	Print.info('enc %d: %s' % (i, hx(key)))

        if Keys.keyAreaKey(self.masterKey, self.keyIndex):
            crypto = aes128.AESECB(
                Keys.keyAreaKey(self.masterKey, self.keyIndex))
            self.keyBlock = crypto.decrypt(self.encKeyBlock)
            self.keys = []
            for i in range(4):
                offset = i * 0x10
                key = self.keyBlock[offset:offset + 0x10]
                #Print.info('dec %d: %s' % (i, hx(key)))
                self.keys.append(key)
        else:
            self.keys = [None, None, None, None, None, None, None]

        if self.hasTitleRights():
            if self.titleId.upper() in Titles.keys() and Titles.get(
                    self.titleId.upper()).key:
                self.titleKeyDec = Keys.decryptTitleKey(
                    uhx(Titles.get(self.titleId.upper()).key), self.masterKey)
            else:
                pass
                #Print.info('could not find title key!')
        else:
            self.titleKeyDec = self.key()

        return True
Exemplo n.º 4
0
    def setMasterKeyRev(self, newMasterKeyRev):
        if not Titles.contains(self.titleId):
            raise IOError('No title key found in database! ' + self.titleId)

        ticket = self.ticket()
        masterKeyRev = ticket.getMasterKeyRevision()
        titleKey = ticket.getTitleKeyBlock()
        newTitleKey = Keys.changeTitleKeyMasterKey(
            titleKey.to_bytes(16, byteorder='big'),
            Keys.getMasterKeyIndex(masterKeyRev),
            Keys.getMasterKeyIndex(newMasterKeyRev))
        rightsId = ticket.getRightsId()

        if rightsId != 0:
            raise IOError('please remove titlerights first')

        if (newMasterKeyRev == None
                and rightsId == 0) or masterKeyRev == newMasterKeyRev:
            Print.info('Nothing to do')
            return

        Print.info('rightsId =\t' + hex(rightsId))
        Print.info('titleKey =\t' +
                   str(hx(titleKey.to_bytes(16, byteorder='big'))))
        Print.info('newTitleKey =\t' + str(hx(newTitleKey)))
        Print.info('masterKeyRev =\t' + hex(masterKeyRev))

        for nca in self:
            if type(nca) == Nca:
                if nca.header.getCryptoType2() != masterKeyRev:
                    pass
                    raise IOError('Mismatched masterKeyRevs!')

        ticket.setMasterKeyRevision(newMasterKeyRev)
        ticket.setRightsId((ticket.getRightsId()
                            & 0xFFFFFFFFFFFFFFFF0000000000000000) +
                           newMasterKeyRev)
        ticket.setTitleKeyBlock(int.from_bytes(newTitleKey, 'big'))

        for nca in self:
            if type(nca) == Nca:
                if nca.header.getCryptoType2() != newMasterKeyRev:
                    Print.info('writing masterKeyRev for %s, %d -> %s' %
                               (str(nca._path), nca.header.getCryptoType2(),
                                str(newMasterKeyRev)))

                    encKeyBlock = nca.header.getKeyBlock()

                    if sum(encKeyBlock) != 0:
                        key = Keys.keyAreaKey(
                            Keys.getMasterKeyIndex(masterKeyRev),
                            nca.header.keyIndex)
                        Print.info('decrypting with %s (%d, %d)' %
                                   (str(hx(key)),
                                    Keys.getMasterKeyIndex(masterKeyRev),
                                    nca.header.keyIndex))
                        crypto = aes128.AESECB(key)
                        decKeyBlock = crypto.decrypt(encKeyBlock)

                        key = Keys.keyAreaKey(
                            Keys.getMasterKeyIndex(newMasterKeyRev),
                            nca.header.keyIndex)
                        Print.info('encrypting with %s (%d, %d)' %
                                   (str(hx(key)),
                                    Keys.getMasterKeyIndex(newMasterKeyRev),
                                    nca.header.keyIndex))
                        crypto = aes128.AESECB(key)

                        reEncKeyBlock = crypto.encrypt(decKeyBlock)
                        nca.header.setKeyBlock(reEncKeyBlock)

                    if newMasterKeyRev >= 3:
                        nca.header.setCryptoType(2)
                        nca.header.setCryptoType2(newMasterKeyRev)
                    else:
                        nca.header.setCryptoType(newMasterKeyRev)
                        nca.header.setCryptoType2(0)