2018-03-13 00:35:52 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from __future__ import with_statement
|
2019-06-24 16:49:38 +00:00
|
|
|
from __future__ import print_function
|
2018-03-13 00:35:52 +00:00
|
|
|
|
|
|
|
# Engine to remove drm from Kindle KFX ebooks
|
|
|
|
|
2020-09-26 20:22:47 +00:00
|
|
|
# 2.0 - Added Python 3 compatibility for calibre 5.0
|
|
|
|
|
|
|
|
|
2018-03-13 00:35:52 +00:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import zipfile
|
|
|
|
|
|
|
|
try:
|
|
|
|
from cStringIO import StringIO
|
|
|
|
except ImportError:
|
2020-09-26 20:22:47 +00:00
|
|
|
try:
|
|
|
|
from StringIO import StringIO
|
|
|
|
except ImportError:
|
|
|
|
from io import StringIO
|
2018-03-13 00:35:52 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
from calibre_plugins.dedrm import ion
|
2018-04-18 08:54:53 +00:00
|
|
|
except ImportError:
|
|
|
|
import ion
|
2018-03-13 00:35:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
__license__ = 'GPL v3'
|
2020-09-26 20:22:47 +00:00
|
|
|
__version__ = '2.0'
|
2018-03-13 00:35:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class KFXZipBook:
|
|
|
|
def __init__(self, infile):
|
|
|
|
self.infile = infile
|
|
|
|
self.voucher = None
|
|
|
|
self.decrypted = {}
|
|
|
|
|
|
|
|
def getPIDMetaInfo(self):
|
|
|
|
return (None, None)
|
|
|
|
|
|
|
|
def processBook(self, totalpids):
|
|
|
|
with zipfile.ZipFile(self.infile, 'r') as zf:
|
|
|
|
for filename in zf.namelist():
|
2018-04-05 17:32:59 +00:00
|
|
|
with zf.open(filename) as fh:
|
|
|
|
data = fh.read(8)
|
|
|
|
if data != '\xeaDRMION\xee':
|
|
|
|
continue
|
|
|
|
data += fh.read()
|
2018-03-13 00:35:52 +00:00
|
|
|
if self.voucher is None:
|
|
|
|
self.decrypt_voucher(totalpids)
|
2019-06-24 16:49:38 +00:00
|
|
|
print(u'Decrypting KFX DRMION: {0}'.format(filename))
|
2018-03-13 00:35:52 +00:00
|
|
|
outfile = StringIO()
|
|
|
|
ion.DrmIon(StringIO(data[8:-8]), lambda name: self.voucher).parse(outfile)
|
|
|
|
self.decrypted[filename] = outfile.getvalue()
|
|
|
|
|
|
|
|
if not self.decrypted:
|
|
|
|
print(u'The .kfx-zip archive does not contain an encrypted DRMION file')
|
|
|
|
|
|
|
|
def decrypt_voucher(self, totalpids):
|
|
|
|
with zipfile.ZipFile(self.infile, 'r') as zf:
|
|
|
|
for info in zf.infolist():
|
2018-04-05 17:32:59 +00:00
|
|
|
with zf.open(info.filename) as fh:
|
|
|
|
data = fh.read(4)
|
|
|
|
if data != '\xe0\x01\x00\xea':
|
|
|
|
continue
|
|
|
|
|
|
|
|
data += fh.read()
|
|
|
|
if 'ProtectedData' in data:
|
2018-03-13 00:35:52 +00:00
|
|
|
break # found DRM voucher
|
|
|
|
else:
|
|
|
|
raise Exception(u'The .kfx-zip archive contains an encrypted DRMION file without a DRM voucher')
|
|
|
|
|
2019-06-24 16:49:38 +00:00
|
|
|
print(u'Decrypting KFX DRM voucher: {0}'.format(info.filename))
|
2018-03-13 00:35:52 +00:00
|
|
|
|
|
|
|
for pid in [''] + totalpids:
|
2019-06-14 19:20:56 +00:00
|
|
|
for dsn_len,secret_len in [(0,0), (16,0), (16,40), (32,40), (40,0), (40,40)]:
|
2018-03-13 00:35:52 +00:00
|
|
|
if len(pid) == dsn_len + secret_len:
|
|
|
|
break # split pid into DSN and account secret
|
|
|
|
else:
|
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
|
|
|
voucher = ion.DrmIonVoucher(StringIO(data), pid[:dsn_len], pid[dsn_len:])
|
|
|
|
voucher.parse()
|
|
|
|
voucher.decryptvoucher()
|
|
|
|
break
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise Exception(u'Failed to decrypt KFX DRM voucher with any key')
|
|
|
|
|
2019-06-24 16:49:38 +00:00
|
|
|
print(u'KFX DRM voucher successfully decrypted')
|
2018-03-13 00:35:52 +00:00
|
|
|
|
|
|
|
license_type = voucher.getlicensetype()
|
|
|
|
if license_type != "Purchase":
|
|
|
|
raise Exception((u'This book is licensed as {0}. '
|
|
|
|
'These tools are intended for use on purchased books.').format(license_type))
|
|
|
|
|
|
|
|
self.voucher = voucher
|
|
|
|
|
|
|
|
def getBookTitle(self):
|
|
|
|
return os.path.splitext(os.path.split(self.infile)[1])[0]
|
|
|
|
|
|
|
|
def getBookExtension(self):
|
|
|
|
return '.kfx-zip'
|
|
|
|
|
|
|
|
def getBookType(self):
|
|
|
|
return 'KFX-ZIP'
|
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def getFile(self, outpath):
|
|
|
|
if not self.decrypted:
|
|
|
|
shutil.copyfile(self.infile, outpath)
|
|
|
|
else:
|
|
|
|
with zipfile.ZipFile(self.infile, 'r') as zif:
|
|
|
|
with zipfile.ZipFile(outpath, 'w') as zof:
|
|
|
|
for info in zif.infolist():
|
|
|
|
zof.writestr(info, self.decrypted.get(info.filename, zif.read(info.filename)))
|