apprenticeharper / DeDRM_tools

DeDRM tools for ebooks
14.54k stars 1.52k forks source link

KFX DRM removal on Calibre Import fails on Linux #527

Closed DB99 closed 6 years ago

DB99 commented 6 years ago

I've attached the log below:

tmpq2Oz3s.txt

I've used an ebook known to be drm infested and downloaded the azw3 version from Amazon. The log shows me importing the kfx version directly from my Kindle Oasis, which fails to decrypt. I then import the azw3 version of the same book which decrypts and can be displayed in viewer. The same kfx book is decrypted successfully under Windows Calibre.

I'm wondering if kfx decryption is currently working under Linux at all, as the keys are entered and working correctly for azw3 but it seems to be the decryption step which is failing for kfx. I have a Calibre Development environment set up and running on Linux and am happy to assist with debugging if necessary.

DB99 commented 6 years ago

Have done a little work on this. I changed kfxdedrm.py to include a few relevant debug print statements:

for pid in [''] + totalpids: for dsn_len,secret_len in [(0,0), (16,0), (16,40), (32,40), (40,40)]: if len(pid) == dsn_len + secret_len: break # split pid into DSN and account secret else: continue

        try:
            print "Debug1: about to define voucher"
            voucher = ion.DrmIonVoucher(StringIO(data), pid[:dsn_len], pid[dsn_len:])
            print "Debug2: Voucher defined:" + voucher
            print "Debug3: About to parse"
            voucher.parse()
            print "Debug4: About to decrypt"
            voucher.decryptvoucher()
            break
        except:
            print "Debug5:  Looks like all keys failed."
            pass
    else:
        raise Exception(u'Failed to decrypt KFX DRM voucher with any key')

The relevant part of the debug log is as follows:

DeDRM v6.6.0: Trying to decrypt CTwxuo.kfx-zip Using Library AlfCrypto DLL/DYLIB/SO Decrypting KFX-ZIP ebook: CTwxuo Found 6 keys to try after 0.0 seconds Decrypting KFX DRM voucher: voucher Debug1: about to define voucher Debug5: Looks like all keys failed. Debug1: about to define voucher Debug5: Looks like all keys failed. Debug1: about to define voucher Debug5: Looks like all keys failed. Debug1: about to define voucher Debug5: Looks like all keys failed. Debug1: about to define voucher Debug5: Looks like all keys failed. Debug1: about to define voucher Debug5: Looks like all keys failed. Debug1: about to define voucher Debug5: Looks like all keys failed. DeDRM v6.6.0: Failed to decrypt with error: Failed to decrypt KFX DRM voucher with any key

Looks like this line is giving some sort of problems. Debug5 probably should read Exception rather than all keys failed, which of course does not happen until the else. It seems that the line after debug1 is throwing an exception in the line where voucher is defined and voucher is neither parsed or decrypted.

ElleKayEm commented 6 years ago

And the device selected for the AZW3 was the same Kindle Oasis?

DB99 commented 6 years ago

Yes.

On Sat, 2 Jun 2018 20:37 ElleKayEm notifications@github.com wrote:

And the device selected for the AZW3 was the same Kindle Oasis?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/apprenticeharper/DeDRM_tools/issues/527#issuecomment-394077276, or mute the thread https://github.com/notifications/unsubscribe-auth/AQ9KlOkFVjMfrR-oPexfy2rah6T17B4Oks5t4mr1gaJpZM4UXi2V .

j-howell commented 6 years ago

It appears that something is going wrong during the processing of the voucher. If you are willing to look into this some more I suggest replacing your:

       print "Debug5:  Looks like all keys failed."

with the following:

       import traceback
       traceback.print_exc()
DB99 commented 6 years ago

Thanks mate. I've done this.
The relevant error seems to be:

TypeError: cannot concatenate 'str' and 'DrmIonVoucher' objects

A bit bizarre as the same code seems to work on Windows. Does not appear to make a lot of sense.

Here is the relevant part of the new log.

Gather KFX-ZIP File Type (from KFX Input) 1.4.0: Importing /run/media/darryl/Kindle/documents/Coraline_B000FC1192.kfx Gather KFX-ZIP File Type (from KFX Input): Gathered 4 files as /tmp/calibre_3.25.0_tmp_CNGcJe/KTVVAM.kfx-zip DeDRM v6.6.0: Trying to decrypt KTVVAM.kfx-zip Using Library AlfCrypto DLL/DYLIB/SO Decrypting KFX-ZIP ebook: KTVVAM Found 6 keys to try after 0.0 seconds Decrypting KFX DRM voucher: voucher Debug1: about to define voucher Debug5: Traceback. Traceback (most recent call last): File "calibre_plugins.dedrm.kfxdedrm", line 73, in decrypt_voucher TypeError: cannot concatenate 'str' and 'DrmIonVoucher' objects Debug1: about to define voucher Debug5: Traceback. Traceback (most recent call last): File "calibre_plugins.dedrm.kfxdedrm", line 73, in decrypt_voucher TypeError: cannot concatenate 'str' and 'DrmIonVoucher' objects Debug1: about to define voucher Debug5: Traceback. Traceback (most recent call last): File "calibre_plugins.dedrm.kfxdedrm", line 73, in decrypt_voucher TypeError: cannot concatenate 'str' and 'DrmIonVoucher' objects Debug1: about to define voucher Debug5: Traceback. Traceback (most recent call last): File "calibre_plugins.dedrm.kfxdedrm", line 73, in decrypt_voucher TypeError: cannot concatenate 'str' and 'DrmIonVoucher' objects Debug1: about to define voucher Debug5: Traceback. Traceback (most recent call last): File "calibre_plugins.dedrm.kfxdedrm", line 73, in decrypt_voucher TypeError: cannot concatenate 'str' and 'DrmIonVoucher' objects Debug1: about to define voucher Debug5: Traceback. Traceback (most recent call last): File "calibre_plugins.dedrm.kfxdedrm", line 73, in decrypt_voucher TypeError: cannot concatenate 'str' and 'DrmIonVoucher' objects Debug1: about to define voucher Debug5: Traceback. Traceback (most recent call last): File "calibre_plugins.dedrm.kfxdedrm", line 73, in decrypt_voucher TypeError: cannot concatenate 'str' and 'DrmIonVoucher' objects DeDRM v6.6.0: Failed to decrypt with error: Failed to decrypt KFX DRM voucher with any key

j-howell commented 6 years ago

TypeError: cannot concatenate 'str' and 'DrmIonVoucher' objects

That is being caused by this line you added:

print "Debug2: Voucher defined:" + voucher

If you don't mind, try taking that out and see what error you get then.

DB99 commented 6 years ago

@j-howell. Thanks. It's now working after taking out that line. Which is a little bizarre because the line only went in because it wasn't working in the first place. I took the precaution of uninstalling the calibre plugin, re-downloading and installing again, and it worked immediately. The only mystery now is why it didn't work originally. Possibly some glitch or I've managed to stuff it up somehow. Anyway, thanks for your help. Sorry to have wasted your time. I will close the issue after you reply.

j-howell commented 6 years ago

Very strange. I'm glad that it is now working for you.

DB99 commented 6 years ago

Thanks for all your hard work.