Ejemplo n.º 1
0
    def eReaderDecrypt(self,path_to_ebook):

        import calibre_plugins.dedrm.prefs as prefs
        import calibre_plugins.dedrm.erdr2pml

        dedrmprefs = prefs.DeDRM_Prefs()
        # Attempt to decrypt epub with each encryption key (generated or provided).
        for keyname, userkey in dedrmprefs['ereaderkeys'].items():
            keyname_masked = u"".join((u'X' if (x.isdigit()) else x) for x in keyname)
            print u"{0} v{1}: Trying Encryption key {2:s}".format(PLUGIN_NAME, PLUGIN_VERSION, keyname_masked)
            of = self.temporary_file(u".pmlz")

            # Give the userkey, ebook and TemporaryPersistent file to the decryption function.
            result = erdr2pml.decryptBook(path_to_ebook, of.name, True, userkey.decode('hex'))

            of.close()

            # Decryption was successful return the modified PersistentTemporary
            # file to Calibre's import process.
            if  result == 0:
                print u"{0} v{1}: Successfully decrypted with key {2:s} after {3:.1f} seconds".format(PLUGIN_NAME, PLUGIN_VERSION,keyname_masked,time.time()-self.starttime)
                return of.name

            print u"{0} v{1}: Failed to decrypt with key {2:s} after {3:.1f} seconds".format(PLUGIN_NAME, PLUGIN_VERSION,keyname_masked,time.time()-self.starttime)

        print u"{0} v{1}: Ultimately failed to decrypt after {2:.1f} seconds. Read the FAQs at Harper's repository: https://github.com/apprenticeharper/DeDRM_tools/blob/master/FAQs.md".format(PLUGIN_NAME, PLUGIN_VERSION,time.time()-self.starttime)
        raise DeDRMError(u"{0} v{1}: Ultimately failed to decrypt after {2:.1f} seconds. Read the FAQs at Harper's repository: https://github.com/apprenticeharper/DeDRM_tools/blob/master/FAQs.md".format(PLUGIN_NAME, PLUGIN_VERSION, time.time()-self.starttime))
Ejemplo n.º 2
0
    def KindleMobiDecrypt(self, path_to_ebook):

        # add the alfcrypto directory to sys.path so alfcrypto.py
        # will be able to locate the custom lib(s) for CDLL import.
        sys.path.insert(0, self.alfdir)
        # Had to move this import here so the custom libs can be
        # extracted to the appropriate places beforehand these routines
        # look for them.
        import calibre_plugins.dedrm.prefs as prefs
        import calibre_plugins.dedrm.k4mobidedrm

        dedrmprefs = prefs.DeDRM_Prefs()
        pids = dedrmprefs['pids']
        serials = dedrmprefs['serials']
        for android_serials_list in dedrmprefs['androidkeys'].values():
            #print android_serials_list
            serials.extend(android_serials_list)
        #print serials
        androidFiles = []
        kindleDatabases = list(dedrmprefs['kindlekeys'].items())

        try:
            book = k4mobidedrm.GetDecryptedBook(path_to_ebook, kindleDatabases,
                                                androidFiles, serials, pids,
                                                self.starttime)
        except Exception as e:
            decoded = False
            # perhaps we need to get a new default Kindle for Mac/PC key
            defaultkeys = []
            print("{0} v{1}: Failed to decrypt with error: {2}".format(
                PLUGIN_NAME, PLUGIN_VERSION, e.args[0]))
            print(
                "{0} v{1}: Looking for new default Kindle Key after {2:.1f} seconds"
                .format(PLUGIN_NAME, PLUGIN_VERSION,
                        time.time() - self.starttime))

            try:
                if iswindows or isosx:
                    from calibre_plugins.dedrm.kindlekey import kindlekeys

                    defaultkeys = kindlekeys()
                else:  # linux
                    from .wineutils import WineGetKeys

                    scriptpath = os.path.join(self.alfdir, "kindlekey.py")
                    defaultkeys = WineGetKeys(scriptpath, ".k4i",
                                              dedrmprefs['kindlewineprefix'])
            except:
                print(
                    "{0} v{1}: Exception when getting default Kindle Key after {2:.1f} seconds"
                    .format(PLUGIN_NAME, PLUGIN_VERSION,
                            time.time() - self.starttime))
                traceback.print_exc()
                pass

            newkeys = {}
            for i, keyvalue in enumerate(defaultkeys):
                keyname = "default_key_{0:d}".format(i + 1)
                if keyvalue not in dedrmprefs['kindlekeys'].values():
                    newkeys[keyname] = keyvalue
            if len(newkeys) > 0:
                print("{0} v{1}: Found {2} new {3}".format(
                    PLUGIN_NAME, PLUGIN_VERSION, len(newkeys),
                    "key" if len(newkeys) == 1 else "keys"))
                try:
                    book = k4mobidedrm.GetDecryptedBook(
                        path_to_ebook, list(newkeys.items()), [], [], [],
                        self.starttime)
                    decoded = True
                    # store the new successful keys in the defaults
                    print("{0} v{1}: Saving {2} new {3}".format(
                        PLUGIN_NAME, PLUGIN_VERSION, len(newkeys),
                        "key" if len(newkeys) == 1 else "keys"))
                    for keyvalue in newkeys.values():
                        dedrmprefs.addnamedvaluetoprefs(
                            'kindlekeys', 'default_key', keyvalue)
                    dedrmprefs.writeprefs()
                except Exception as e:
                    pass
            if not decoded:
                #if you reached here then no luck raise and exception
                print(
                    "{0} v{1}: Ultimately failed to decrypt after {2:.1f} seconds. Read the FAQs at Harper's repository: https://github.com/apprenticeharper/DeDRM_tools/blob/master/FAQs.md"
                    .format(PLUGIN_NAME, PLUGIN_VERSION,
                            time.time() - self.starttime))
                raise DeDRMError(
                    "{0} v{1}: Ultimately failed to decrypt after {2:.1f} seconds. Read the FAQs at Harper's repository: https://github.com/apprenticeharper/DeDRM_tools/blob/master/FAQs.md"
                    .format(PLUGIN_NAME, PLUGIN_VERSION,
                            time.time() - self.starttime))

        of = self.temporary_file(book.getBookExtension())
        book.getFile(of.name)
        of.close()
        book.cleanup()
        return of.name
Ejemplo n.º 3
0
    def PDFDecrypt(self, path_to_ebook):
        import calibre_plugins.dedrm.prefs as prefs
        import calibre_plugins.dedrm.ineptpdf

        dedrmprefs = prefs.DeDRM_Prefs()
        # Attempt to decrypt epub with each encryption key (generated or provided).
        print("{0} v{1}: {2} is a PDF ebook".format(
            PLUGIN_NAME, PLUGIN_VERSION, os.path.basename(path_to_ebook)))
        for keyname, userkeyhex in dedrmprefs['adeptkeys'].items():
            userkey = codecs.decode(userkeyhex, 'hex')
            print("{0} v{1}: Trying Encryption key {2:s}".format(
                PLUGIN_NAME, PLUGIN_VERSION, keyname))
            of = self.temporary_file(".pdf")

            # Give the user key, ebook and TemporaryPersistent file to the decryption function.
            try:
                result = ineptpdf.decryptBook(userkey, path_to_ebook, of.name)
            except:
                print(
                    "{0} v{1}: Exception when decrypting after {2:.1f} seconds"
                    .format(PLUGIN_NAME, PLUGIN_VERSION,
                            time.time() - self.starttime))
                traceback.print_exc()
                result = 1

            of.close()

            if result == 0:
                # Decryption was successful.
                # Return the modified PersistentTemporary file to calibre.
                return of.name

            print(
                "{0} v{1}: Failed to decrypt with key {2:s} after {3:.1f} seconds"
                .format(PLUGIN_NAME, PLUGIN_VERSION, keyname,
                        time.time() - self.starttime))

        # perhaps we need to get a new default ADE key
        print(
            "{0} v{1}: Looking for new default Adobe Digital Editions Keys after {2:.1f} seconds"
            .format(PLUGIN_NAME, PLUGIN_VERSION,
                    time.time() - self.starttime))

        # get the default Adobe keys
        defaultkeys = []

        try:
            if iswindows or isosx:
                from calibre_plugins.dedrm.adobekey import adeptkeys

                defaultkeys = adeptkeys()
            else:  # linux
                from .wineutils import WineGetKeys

                scriptpath = os.path.join(self.alfdir, "adobekey.py")
                defaultkeys = WineGetKeys(scriptpath, ".der",
                                          dedrmprefs['adobewineprefix'])

            self.default_key = defaultkeys[0]
        except:
            print(
                "{0} v{1}: Exception when getting default Adobe Key after {2:.1f} seconds"
                .format(PLUGIN_NAME, PLUGIN_VERSION,
                        time.time() - self.starttime))
            traceback.print_exc()
            self.default_key = ""

        newkeys = []
        for keyvalue in defaultkeys:
            if codecs.encode(keyvalue,
                             'hex') not in dedrmprefs['adeptkeys'].values():
                newkeys.append(keyvalue)

        if len(newkeys) > 0:
            try:
                for i, userkey in enumerate(newkeys):
                    print("{0} v{1}: Trying a new default key".format(
                        PLUGIN_NAME, PLUGIN_VERSION))
                    of = self.temporary_file(".pdf")

                    # Give the user key, ebook and TemporaryPersistent file to the decryption function.
                    try:
                        result = ineptpdf.decryptBook(userkey, path_to_ebook,
                                                      of.name)
                    except:
                        print(
                            "{0} v{1}: Exception when decrypting after {2:.1f} seconds"
                            .format(PLUGIN_NAME, PLUGIN_VERSION,
                                    time.time() - self.starttime))
                        traceback.print_exc()
                        result = 1

                    of.close()

                    if result == 0:
                        # Decryption was a success
                        # Store the new successful key in the defaults
                        print("{0} v{1}: Saving a new default key".format(
                            PLUGIN_NAME, PLUGIN_VERSION))
                        try:
                            dedrmprefs.addnamedvaluetoprefs(
                                'adeptkeys', 'default_key',
                                codecs.encode(keyvalue, 'hex'))
                            dedrmprefs.writeprefs()
                            print(
                                "{0} v{1}: Saved a new default key after {2:.1f} seconds"
                                .format(PLUGIN_NAME, PLUGIN_VERSION,
                                        time.time() - self.starttime))
                        except:
                            print(
                                "{0} v{1}: Exception when saving a new default key after {2:.1f} seconds"
                                .format(PLUGIN_NAME, PLUGIN_VERSION,
                                        time.time() - self.starttime))
                            traceback.print_exc()
                        # Return the modified PersistentTemporary file to calibre.
                        return of.name

                    print(
                        "{0} v{1}: Failed to decrypt with new default key after {2:.1f} seconds"
                        .format(PLUGIN_NAME, PLUGIN_VERSION,
                                time.time() - self.starttime))
            except Exception as e:
                pass

        # Something went wrong with decryption.
        print(
            "{0} v{1}: Ultimately failed to decrypt after {2:.1f} seconds. Read the FAQs at Harper's repository: https://github.com/apprenticeharper/DeDRM_tools/blob/master/FAQs.md"
            .format(PLUGIN_NAME, PLUGIN_VERSION,
                    time.time() - self.starttime))
        raise DeDRMError(
            "{0} v{1}: Ultimately failed to decrypt after {2:.1f} seconds. Read the FAQs at Harper's repository: https://github.com/apprenticeharper/DeDRM_tools/blob/master/FAQs.md"
            .format(PLUGIN_NAME, PLUGIN_VERSION,
                    time.time() - self.starttime))
Ejemplo n.º 4
0
    def ePubDecrypt(self, path_to_ebook):
        # Create a TemporaryPersistent file to work with.
        # Check original epub archive for zip errors.
        import calibre_plugins.dedrm.zipfix

        inf = self.temporary_file(".epub")
        try:
            print("{0} v{1}: Verifying zip archive integrity".format(
                PLUGIN_NAME, PLUGIN_VERSION))
            fr = zipfix.fixZip(path_to_ebook, inf.name)
            fr.fix()
        except Exception as e:
            print("{0} v{1}: Error \'{2}\' when checking zip archive".format(
                PLUGIN_NAME, PLUGIN_VERSION, e.args[0]))
            raise Exception(e)

        # import the decryption keys
        import calibre_plugins.dedrm.prefs as prefs
        dedrmprefs = prefs.DeDRM_Prefs()

        # import the Barnes & Noble ePub handler
        import calibre_plugins.dedrm.ignobleepub as ignobleepub

        #check the book
        if ignobleepub.ignobleBook(inf.name):
            print("{0} v{1}: “{2}” is a secure Barnes & Noble ePub".format(
                PLUGIN_NAME, PLUGIN_VERSION, os.path.basename(path_to_ebook)))

            # Attempt to decrypt epub with each encryption key (generated or provided).
            for keyname, userkey in dedrmprefs['bandnkeys'].items():
                keyname_masked = "".join(
                    ("X" if (x.isdigit()) else x) for x in keyname)
                print("{0} v{1}: Trying Encryption key {2:s}".format(
                    PLUGIN_NAME, PLUGIN_VERSION, keyname_masked))
                of = self.temporary_file(".epub")

                # Give the user key, ebook and TemporaryPersistent file to the decryption function.
                try:
                    result = ignobleepub.decryptBook(userkey, inf.name,
                                                     of.name)
                except:
                    print(
                        "{0} v{1}: Exception when trying to decrypt after {2:.1f} seconds"
                        .format(PLUGIN_NAME, PLUGIN_VERSION,
                                time.time() - self.starttime))
                    traceback.print_exc()
                    result = 1

                of.close()

                if result == 0:
                    # Decryption was successful.
                    # Return the modified PersistentTemporary file to calibre.
                    return of.name

                print(
                    "{0} v{1}: Failed to decrypt with key {2:s} after {3:.1f} seconds"
                    .format(PLUGIN_NAME, PLUGIN_VERSION, keyname_masked,
                            time.time() - self.starttime))

            # perhaps we should see if we can get a key from a log file
            print(
                "{0} v{1}: Looking for new NOOK Study Keys after {2:.1f} seconds"
                .format(PLUGIN_NAME, PLUGIN_VERSION,
                        time.time() - self.starttime))

            # get the default NOOK Study keys
            defaultkeys = []

            try:
                if iswindows or isosx:
                    from calibre_plugins.dedrm.ignoblekey import nookkeys

                    defaultkeys = nookkeys()
                else:  # linux
                    from .wineutils import WineGetKeys

                    scriptpath = os.path.join(self.alfdir, "ignoblekey.py")
                    defaultkeys = WineGetKeys(scriptpath, ".b64",
                                              dedrmprefs['adobewineprefix'])

            except:
                print(
                    "{0} v{1}: Exception when getting default NOOK Study Key after {2:.1f} seconds"
                    .format(PLUGIN_NAME, PLUGIN_VERSION,
                            time.time() - self.starttime))
                traceback.print_exc()

            newkeys = []
            for keyvalue in defaultkeys:
                if keyvalue not in dedrmprefs['bandnkeys'].values():
                    newkeys.append(keyvalue)

            if len(newkeys) > 0:
                try:
                    for i, userkey in enumerate(newkeys):
                        print("{0} v{1}: Trying a new default key".format(
                            PLUGIN_NAME, PLUGIN_VERSION))

                        of = self.temporary_file(".epub")

                        # Give the user key, ebook and TemporaryPersistent file to the decryption function.
                        try:
                            result = ignobleepub.decryptBook(
                                userkey, inf.name, of.name)
                        except:
                            print(
                                "{0} v{1}: Exception when trying to decrypt after {2:.1f} seconds"
                                .format(PLUGIN_NAME, PLUGIN_VERSION,
                                        time.time() - self.starttime))
                            traceback.print_exc()
                            result = 1

                        of.close()

                        if result == 0:
                            # Decryption was a success
                            # Store the new successful key in the defaults
                            print("{0} v{1}: Saving a new default key".format(
                                PLUGIN_NAME, PLUGIN_VERSION))
                            try:
                                dedrmprefs.addnamedvaluetoprefs(
                                    'bandnkeys', 'nook_Study_key', keyvalue)
                                dedrmprefs.writeprefs()
                                print(
                                    "{0} v{1}: Saved a new default key after {2:.1f} seconds"
                                    .format(PLUGIN_NAME, PLUGIN_VERSION,
                                            time.time() - self.starttime))
                            except:
                                print(
                                    "{0} v{1}: Exception saving a new default key after {2:.1f} seconds"
                                    .format(PLUGIN_NAME, PLUGIN_VERSION,
                                            time.time() - self.starttime))
                                traceback.print_exc()
                            # Return the modified PersistentTemporary file to calibre.
                            return of.name

                        print(
                            "{0} v{1}: Failed to decrypt with new default key after {2:.1f} seconds"
                            .format(PLUGIN_NAME, PLUGIN_VERSION,
                                    time.time() - self.starttime))
                except Exception as e:
                    pass

            print(
                "{0} v{1}: Ultimately failed to decrypt after {2:.1f} seconds. Read the FAQs at Harper's repository: https://github.com/apprenticeharper/DeDRM_tools/blob/master/FAQs.md"
                .format(PLUGIN_NAME, PLUGIN_VERSION,
                        time.time() - self.starttime))
            raise DeDRMError(
                "{0} v{1}: Ultimately failed to decrypt after {2:.1f} seconds. Read the FAQs at Harper's repository: https://github.com/apprenticeharper/DeDRM_tools/blob/master/FAQs.md"
                .format(PLUGIN_NAME, PLUGIN_VERSION,
                        time.time() - self.starttime))

        # import the Adobe Adept ePub handler
        import calibre_plugins.dedrm.ineptepub as ineptepub

        if ineptepub.adeptBook(inf.name):
            print("{0} v{1}: {2} is a secure Adobe Adept ePub".format(
                PLUGIN_NAME, PLUGIN_VERSION, os.path.basename(path_to_ebook)))

            # Attempt to decrypt epub with each encryption key (generated or provided).
            for keyname, userkeyhex in dedrmprefs['adeptkeys'].items():
                userkey = codecs.decode(userkeyhex, 'hex')
                print("{0} v{1}: Trying Encryption key {2:s}".format(
                    PLUGIN_NAME, PLUGIN_VERSION, keyname))
                of = self.temporary_file(".epub")

                # Give the user key, ebook and TemporaryPersistent file to the decryption function.
                try:
                    result = ineptepub.decryptBook(userkey, inf.name, of.name)
                except:
                    print(
                        "{0} v{1}: Exception when decrypting after {2:.1f} seconds"
                        .format(PLUGIN_NAME, PLUGIN_VERSION,
                                time.time() - self.starttime))
                    traceback.print_exc()
                    result = 1

                try:
                    of.close()
                except:
                    print(
                        "{0} v{1}: Exception closing temporary file after {2:.1f} seconds. Ignored."
                        .format(PLUGIN_NAME, PLUGIN_VERSION,
                                time.time() - self.starttime))

                if result == 0:
                    # Decryption was successful.
                    # Return the modified PersistentTemporary file to calibre.
                    print(
                        "{0} v{1}: Decrypted with key {2:s} after {3:.1f} seconds"
                        .format(PLUGIN_NAME, PLUGIN_VERSION, keyname,
                                time.time() - self.starttime))
                    return of.name

                print(
                    "{0} v{1}: Failed to decrypt with key {2:s} after {3:.1f} seconds"
                    .format(PLUGIN_NAME, PLUGIN_VERSION, keyname,
                            time.time() - self.starttime))

            # perhaps we need to get a new default ADE key
            print(
                "{0} v{1}: Looking for new default Adobe Digital Editions Keys after {2:.1f} seconds"
                .format(PLUGIN_NAME, PLUGIN_VERSION,
                        time.time() - self.starttime))

            # get the default Adobe keys
            defaultkeys = []

            try:
                if iswindows or isosx:
                    from calibre_plugins.dedrm.adobekey import adeptkeys

                    defaultkeys = adeptkeys()
                else:  # linux
                    from .wineutils import WineGetKeys

                    scriptpath = os.path.join(self.alfdir, "adobekey.py")
                    defaultkeys = WineGetKeys(scriptpath, ".der",
                                              dedrmprefs['adobewineprefix'])

                self.default_key = defaultkeys[0]
            except:
                print(
                    "{0} v{1}: Exception when getting default Adobe Key after {2:.1f} seconds"
                    .format(PLUGIN_NAME, PLUGIN_VERSION,
                            time.time() - self.starttime))
                traceback.print_exc()
                self.default_key = ""

            newkeys = []
            for keyvalue in defaultkeys:
                if codecs.encode(keyvalue, 'hex').decode(
                        'ascii') not in dedrmprefs['adeptkeys'].values():
                    newkeys.append(keyvalue)

            if len(newkeys) > 0:
                try:
                    for i, userkey in enumerate(newkeys):
                        print("{0} v{1}: Trying a new default key".format(
                            PLUGIN_NAME, PLUGIN_VERSION))
                        of = self.temporary_file(".epub")

                        # Give the user key, ebook and TemporaryPersistent file to the decryption function.
                        try:
                            result = ineptepub.decryptBook(
                                userkey, inf.name, of.name)
                        except:
                            print(
                                "{0} v{1}: Exception when decrypting after {2:.1f} seconds"
                                .format(PLUGIN_NAME, PLUGIN_VERSION,
                                        time.time() - self.starttime))
                            traceback.print_exc()
                            result = 1

                        of.close()

                        if result == 0:
                            # Decryption was a success
                            # Store the new successful key in the defaults
                            print("{0} v{1}: Saving a new default key".format(
                                PLUGIN_NAME, PLUGIN_VERSION))
                            try:
                                dedrmprefs.addnamedvaluetoprefs(
                                    'adeptkeys', 'default_key',
                                    codecs.encode(keyvalue,
                                                  'hex').decode('ascii'))
                                dedrmprefs.writeprefs()
                                print(
                                    "{0} v{1}: Saved a new default key after {2:.1f} seconds"
                                    .format(PLUGIN_NAME, PLUGIN_VERSION,
                                            time.time() - self.starttime))
                            except:
                                print(
                                    "{0} v{1}: Exception when saving a new default key after {2:.1f} seconds"
                                    .format(PLUGIN_NAME, PLUGIN_VERSION,
                                            time.time() - self.starttime))
                                traceback.print_exc()
                            print(
                                "{0} v{1}: Decrypted with new default key after {2:.1f} seconds"
                                .format(PLUGIN_NAME, PLUGIN_VERSION,
                                        time.time() - self.starttime))
                            # Return the modified PersistentTemporary file to calibre.
                            return of.name

                        print(
                            "{0} v{1}: Failed to decrypt with new default key after {2:.1f} seconds"
                            .format(PLUGIN_NAME, PLUGIN_VERSION,
                                    time.time() - self.starttime))
                except Exception as e:
                    print(
                        "{0} v{1}: Unexpected Exception trying a new default key after {2:.1f} seconds"
                        .format(PLUGIN_NAME, PLUGIN_VERSION,
                                time.time() - self.starttime))
                    traceback.print_exc()
                    pass

            # Something went wrong with decryption.
            print(
                "{0} v{1}: Ultimately failed to decrypt after {2:.1f} seconds. Read the FAQs at Harper's repository: https://github.com/apprenticeharper/DeDRM_tools/blob/master/FAQs.md"
                .format(PLUGIN_NAME, PLUGIN_VERSION,
                        time.time() - self.starttime))
            raise DeDRMError(
                "{0} v{1}: Ultimately failed to decrypt after {2:.1f} seconds. Read the FAQs at Harper's repository: https://github.com/apprenticeharper/DeDRM_tools/blob/master/FAQs.md"
                .format(PLUGIN_NAME, PLUGIN_VERSION,
                        time.time() - self.starttime))

        # Not a Barnes & Noble nor an Adobe Adept
        # Import the fixed epub.
        print(
            "{0} v{1}: “{2}” is neither an Adobe Adept nor a Barnes & Noble encrypted ePub"
            .format(PLUGIN_NAME, PLUGIN_VERSION,
                    os.path.basename(path_to_ebook)))
        raise DeDRMError(
            "{0} v{1}: Couldn't decrypt after {2:.1f} seconds. DRM free perhaps?"
            .format(PLUGIN_NAME, PLUGIN_VERSION,
                    time.time() - self.starttime))
Ejemplo n.º 5
0
    def KindleMobiDecrypt(self, path_to_ebook):

        # add the alfcrypto directory to sys.path so alfcrypto.py
        # will be able to locate the custom lib(s) for CDLL import.
        sys.path.insert(0, self.alfdir)
        # Had to move this import here so the custom libs can be
        # extracted to the appropriate places beforehand these routines
        # look for them.
        import calibre_plugins.dedrm.prefs as prefs
        import calibre_plugins.dedrm.k4mobidedrm

        dedrmprefs = prefs.DeDRM_Prefs()
        pids = dedrmprefs['pids']
        serials = dedrmprefs['serials']
        kindleDatabases = dedrmprefs['kindlekeys'].items()

        try:
            book = k4mobidedrm.GetDecryptedBook(path_to_ebook, kindleDatabases,
                                                serials, pids, self.starttime)
        except Exception, e:
            decoded = False
            # perhaps we need to get a new default Kindle for Mac/PC key
            defaultkeys = []
            print u"{0} v{1}: Failed to decrypt with error: {2}".format(
                PLUGIN_NAME, PLUGIN_VERSION, e.args[0])
            print u"{0} v{1}: Looking for new default Kindle Key after {2:.1f} seconds".format(
                PLUGIN_NAME, PLUGIN_VERSION,
                time.time() - self.starttime)

            try:
                if iswindows or isosx:
                    from calibre_plugins.dedrm.kindlekey import kindlekeys

                    defaultkeys = kindlekeys()
                else:  # linux
                    from wineutils import WineGetKeys

                    scriptpath = os.join(self.alfdir, u"kindlekey.py")
                    defaultkeys = self.WineGetKeys(
                        scriptpath, u".k4i", dedrmprefs['kindlewineprefix'])
            except:
                pass

            newkeys = {}
            for i, keyvalue in enumerate(defaultkeys):
                keyname = u"default_key_{0:d}".format(i + 1)
                if keyvalue not in dedrmprefs['kindlekeys'].values():
                    newkeys[keyname] = keyvalue
            if len(newkeys) > 0:
                print u"{0} v{1}: Found {2} new {3}".format(
                    PLUGIN_NAME, PLUGIN_VERSION, len(newkeys),
                    u"key" if len(newkeys) == 1 else u"keys")
                try:
                    book = k4mobidedrm.GetDecryptedBook(
                        path_to_ebook, newkeys.items(), [], [], self.starttime)
                    decoded = True
                    # store the new successful keys in the defaults
                    print u"{0} v{1}: Saving {2} new {3}".format(
                        PLUGIN_NAME, PLUGIN_VERSION, len(newkeys),
                        u"key" if len(newkeys) == 1 else u"keys")
                    for keyvalue in newkeys.values():
                        dedrmprefs.addnamedvaluetoprefs(
                            'kindlekeys', 'default_key', keyvalue)
                    dedrmprefs.writeprefs()
                except Exception, e:
                    pass
Ejemplo n.º 6
0
    def PDFDecrypt(self, path_to_ebook):
        import calibre_plugins.dedrm.prefs as prefs
        import calibre_plugins.dedrm.ineptpdf

        dedrmprefs = prefs.DeDRM_Prefs()
        # Attempt to decrypt epub with each encryption key (generated or provided).
        print u"{0} v{1}: {2} is a PDF ebook".format(
            PLUGIN_NAME, PLUGIN_VERSION, os.path.basename(path_to_ebook))
        for keyname, userkeyhex in dedrmprefs['adeptkeys'].items():
            userkey = userkeyhex.decode('hex')
            print u"{0} v{1}: Trying Encryption key {2:s}".format(
                PLUGIN_NAME, PLUGIN_VERSION, keyname)
            of = self.temporary_file(u".pdf")

            # Give the user key, ebook and TemporaryPersistent file to the decryption function.
            try:
                result = ineptpdf.decryptBook(userkey, path_to_ebook, of.name)
            except:
                result = 1

            of.close()

            if result == 0:
                # Decryption was successful.
                # Return the modified PersistentTemporary file to calibre.
                return of.name

            # perhaps we need to get a new default ADE key
            print u"{0} v{1}: Looking for new default Adobe Digital Editions Keys after {2:.1f} seconds".format(
                PLUGIN_NAME, PLUGIN_VERSION,
                time.time() - self.starttime)

            # get the default Adobe keys
            defaultkeys = []

            if iswindows or isosx:
                import calibre_plugins.dedrm.adobekey as adobe

                try:
                    defaultkeys = adobe.adeptkeys()
                except:
                    pass
            else:
                # linux
                try:
                    from wineutils import WineGetKeys

                    scriptpath = os.join(self.alfdir, u"adobekey.py")
                    defaultkeys = self.WineGetKeys(
                        scriptpath, u".der", dedrmprefs['adobewineprefix'])
                except:
                    pass

            newkeys = []
            for keyvalue in defaultkeys:
                if keyvalue.encode(
                        'hex') not in dedrmprefs['adeptkeys'].values():
                    newkeys.append(keyvalue)

            if len(newkeys) > 0:
                try:
                    for i, userkey in enumerate(newkeys):
                        print u"{0} v{1}: Trying a new default key".format(
                            PLUGIN_NAME, PLUGIN_VERSION)
                        of = self.temporary_file(u".pdf")

                        # Give the user key, ebook and TemporaryPersistent file to the decryption function.
                        try:
                            result = ineptepdf.decryptBook(
                                userkey, inf.name, of.name)
                        except:
                            result = 1

                        of.close()

                        if result == 0:
                            # Decryption was a success
                            # Store the new successful key in the defaults
                            print u"{0} v{1}: Saving a new default key".format(
                                PLUGIN_NAME, PLUGIN_VERSION)
                            try:
                                dedrmprefs.addnamedvaluetoprefs(
                                    'adeptkeys', 'default_key',
                                    keyvalue.encode('hex'))
                                dedrmprefs.writeprefs()
                            except:
                                traceback.print_exc()
                            # Return the modified PersistentTemporary file to calibre.
                            return of.name

                        print u"{0} v{1}: Failed to decrypt with new default key after {2:.1f} seconds".format(
                            PLUGIN_NAME, PLUGIN_VERSION,
                            time.time() - self.starttime)
                except Exception, e:
                    pass
Ejemplo n.º 7
0
        import calibre_plugins.dedrm.zipfix

        inf = self.temporary_file(u".epub")
        try:
            print u"{0} v{1}: Verifying zip archive integrity".format(
                PLUGIN_NAME, PLUGIN_VERSION)
            fr = zipfix.fixZip(path_to_ebook, inf.name)
            fr.fix()
        except Exception, e:
            print u"{0} v{1}: Error \'{2}\' when checking zip archive".format(
                PLUGIN_NAME, PLUGIN_VERSION, e.args[0])
            raise Exception(e)

        # import the decryption keys
        import calibre_plugins.dedrm.prefs as prefs
        dedrmprefs = prefs.DeDRM_Prefs()

        # import the Barnes & Noble ePub handler
        import calibre_plugins.dedrm.ignobleepub as ignobleepub

        #check the book
        if ignobleepub.ignobleBook(inf.name):
            print u"{0} v{1}: “{2}” is a secure Barnes & Noble ePub".format(
                PLUGIN_NAME, PLUGIN_VERSION, os.path.basename(path_to_ebook))

            # Attempt to decrypt epub with each encryption key (generated or provided).
            for keyname, userkey in dedrmprefs['bandnkeys'].items():
                keyname_masked = u"".join(
                    (u'X' if (x.isdigit()) else x) for x in keyname)
                print u"{0} v{1}: Trying Encryption key {2:s}".format(
                    PLUGIN_NAME, PLUGIN_VERSION, keyname_masked)
Ejemplo n.º 8
0
    def __init__(self, plugin_path, alfdir):
        QWidget.__init__(self)

        self.plugin_path = plugin_path
        self.alfdir = alfdir

        # get the prefs
        self.dedrmprefs = prefs.DeDRM_Prefs()

        # make a local copy
        self.tempdedrmprefs = {}
        self.tempdedrmprefs['bandnkeys'] = self.dedrmprefs['bandnkeys'].copy()
        self.tempdedrmprefs['adeptkeys'] = self.dedrmprefs['adeptkeys'].copy()
        self.tempdedrmprefs['ereaderkeys'] = self.dedrmprefs[
            'ereaderkeys'].copy()
        self.tempdedrmprefs['kindlekeys'] = self.dedrmprefs['kindlekeys'].copy(
        )
        self.tempdedrmprefs['pids'] = list(self.dedrmprefs['pids'])
        self.tempdedrmprefs['serials'] = list(self.dedrmprefs['serials'])
        self.tempdedrmprefs['adobewineprefix'] = self.dedrmprefs[
            'adobewineprefix']
        self.tempdedrmprefs['kindlewineprefix'] = self.dedrmprefs[
            'kindlewineprefix']

        # Start Qt Gui dialog layout
        layout = QVBoxLayout(self)
        self.setLayout(layout)

        help_layout = QHBoxLayout()
        layout.addLayout(help_layout)
        # Add hyperlink to a help file at the right. We will replace the correct name when it is clicked.
        help_label = QLabel('<a href="http://www.foo.com/">Plugin Help</a>',
                            self)
        help_label.setTextInteractionFlags(Qt.LinksAccessibleByMouse
                                           | Qt.LinksAccessibleByKeyboard)
        help_label.setAlignment(Qt.AlignRight)
        help_label.linkActivated.connect(self.help_link_activated)
        help_layout.addWidget(help_label)

        keys_group_box = QGroupBox(_('Configuration:'), self)
        layout.addWidget(keys_group_box)
        keys_group_box_layout = QHBoxLayout()
        keys_group_box.setLayout(keys_group_box_layout)

        button_layout = QVBoxLayout()
        keys_group_box_layout.addLayout(button_layout)
        self.bandn_button = QtGui.QPushButton(self)
        self.bandn_button.setToolTip(
            _(u"Click to manage keys for Barnes and Noble ebooks"))
        self.bandn_button.setText(u"Barnes and Noble ebooks")
        self.bandn_button.clicked.connect(self.bandn_keys)
        self.kindle_serial_button = QtGui.QPushButton(self)
        self.kindle_serial_button.setToolTip(
            _(u"Click to manage eInk Kindle serial numbers for Kindle ebooks"))
        self.kindle_serial_button.setText(u"eInk Kindle ebooks")
        self.kindle_serial_button.clicked.connect(self.kindle_serials)
        self.kindle_key_button = QtGui.QPushButton(self)
        self.kindle_key_button.setToolTip(
            _(u"Click to manage keys for Kindle for Mac/PC ebooks"))
        self.kindle_key_button.setText(u"Kindle for Mac/PC ebooks")
        self.kindle_key_button.clicked.connect(self.kindle_keys)
        self.adept_button = QtGui.QPushButton(self)
        self.adept_button.setToolTip(
            _(u"Click to manage keys for Adobe Digital Editions ebooks"))
        self.adept_button.setText(u"Adobe Digital Editions ebooks")
        self.adept_button.clicked.connect(self.adept_keys)
        self.mobi_button = QtGui.QPushButton(self)
        self.mobi_button.setToolTip(
            _(u"Click to manage PIDs for Mobipocket ebooks"))
        self.mobi_button.setText(u"Mobipocket ebooks")
        self.mobi_button.clicked.connect(self.mobi_keys)
        self.ereader_button = QtGui.QPushButton(self)
        self.ereader_button.setToolTip(
            _(u"Click to manage keys for eReader ebooks"))
        self.ereader_button.setText(u"eReader ebooks")
        self.ereader_button.clicked.connect(self.ereader_keys)
        button_layout.addWidget(self.kindle_serial_button)
        button_layout.addWidget(self.bandn_button)
        button_layout.addWidget(self.mobi_button)
        button_layout.addWidget(self.ereader_button)
        button_layout.addWidget(self.adept_button)
        button_layout.addWidget(self.kindle_key_button)

        self.resize(self.sizeHint())