tools v4.0
New calibre plugin interface (0.7.55) Dropped unswindle.pyw Added Android patch
This commit is contained in:
parent
529dd3f160
commit
4f34a9a196
|
@ -1,5 +1,5 @@
|
||||||
#! /usr/bin/env python
|
#! /usr/bin/env python
|
||||||
# ineptpdf.pyw, version 7.7
|
# ineptpdf.pyw, version 7.9
|
||||||
|
|
||||||
from __future__ import with_statement
|
from __future__ import with_statement
|
||||||
|
|
||||||
|
@ -33,6 +33,7 @@ from __future__ import with_statement
|
||||||
# 7.6 - backported AES and other fixes from version 8.4.48
|
# 7.6 - backported AES and other fixes from version 8.4.48
|
||||||
# 7.7 - On Windows try PyCrypto first and OpenSSL next
|
# 7.7 - On Windows try PyCrypto first and OpenSSL next
|
||||||
# 7.8 - Modify interface to allow use of import
|
# 7.8 - Modify interface to allow use of import
|
||||||
|
# 7.9 - Bug fix for some session key errors when len(bookkey) > length required
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Decrypts Adobe ADEPT-encrypted PDF files.
|
Decrypts Adobe ADEPT-encrypted PDF files.
|
||||||
|
@ -156,6 +157,7 @@ def _load_crypto_libcrypto():
|
||||||
return out.raw
|
return out.raw
|
||||||
|
|
||||||
class AES(object):
|
class AES(object):
|
||||||
|
MODE_CBC = 0
|
||||||
@classmethod
|
@classmethod
|
||||||
def new(cls, userkey, mode, iv):
|
def new(cls, userkey, mode, iv):
|
||||||
self = AES()
|
self = AES()
|
||||||
|
@ -1531,16 +1533,30 @@ class PDFDocument(object):
|
||||||
bookkey = bookkey[index:]
|
bookkey = bookkey[index:]
|
||||||
ebx_V = int_value(param.get('V', 4))
|
ebx_V = int_value(param.get('V', 4))
|
||||||
ebx_type = int_value(param.get('EBX_ENCRYPTIONTYPE', 6))
|
ebx_type = int_value(param.get('EBX_ENCRYPTIONTYPE', 6))
|
||||||
# added because of the booktype / decryption book session key error
|
# added because of improper booktype / decryption book session key errors
|
||||||
if ebx_V == 3:
|
if length > 0:
|
||||||
V = 3
|
if len(bookkey) == length:
|
||||||
elif ebx_V < 4 or ebx_type < 6:
|
if ebx_V == 3:
|
||||||
V = ord(bookkey[0])
|
V = 3
|
||||||
bookkey = bookkey[1:]
|
else:
|
||||||
|
V = 2
|
||||||
|
elif len(bookkey) == length + 1:
|
||||||
|
V = ord(bookkey[0])
|
||||||
|
bookkey = bookkey[1:]
|
||||||
|
else:
|
||||||
|
print "ebx_V is %d and ebx_type is %d" % (ebx_V, ebx_type)
|
||||||
|
print "length is %d and len(bookkey) is %d" % (length, len(bookkey))
|
||||||
|
print "bookkey[0] is %d" % ord(bookkey[0])
|
||||||
|
raise ADEPTError('error decrypting book session key - mismatched length')
|
||||||
else:
|
else:
|
||||||
V = 2
|
# proper length unknown try with whatever you have
|
||||||
if length and len(bookkey) != length:
|
print "ebx_V is %d and ebx_type is %d" % (ebx_V, ebx_type)
|
||||||
raise ADEPTError('error decrypting book session key')
|
print "length is %d and len(bookkey) is %d" % (length, len(bookkey))
|
||||||
|
print "bookkey[0] is %d" % ord(bookkey[0])
|
||||||
|
if ebx_V == 3:
|
||||||
|
V = 3
|
||||||
|
else:
|
||||||
|
V = 2
|
||||||
self.decrypt_key = bookkey
|
self.decrypt_key = bookkey
|
||||||
self.genkey = self.genkey_v3 if V == 3 else self.genkey_v2
|
self.genkey = self.genkey_v3 if V == 3 else self.genkey_v2
|
||||||
self.decipher = self.decrypt_rc4
|
self.decipher = self.decrypt_rc4
|
||||||
|
|
|
@ -0,0 +1,116 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
from __future__ import with_statement
|
||||||
|
|
||||||
|
from calibre.customize import FileTypePlugin
|
||||||
|
from calibre.gui2 import is_ok_to_use_qt
|
||||||
|
# from calibre.ptempfile import PersistentTemporaryDirectory
|
||||||
|
|
||||||
|
from calibre_plugins.k4mobidedrm import kgenpids
|
||||||
|
from calibre_plugins.k4mobidedrm import topazextract
|
||||||
|
from calibre_plugins.k4mobidedrm import mobidedrm
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
|
||||||
|
class K4DeDRM(FileTypePlugin):
|
||||||
|
name = 'K4PC, K4Mac, Kindle Mobi and Topaz DeDRM' # Name of the plugin
|
||||||
|
description = 'Removes DRM from K4PC and Mac, Kindle Mobi and Topaz files. Provided by the work of many including DiapDealer, SomeUpdates, IHeartCabbages, CMBDTC, Skindle, DarkReverser, ApprenticeAlf, etc.'
|
||||||
|
supported_platforms = ['osx', 'windows', 'linux'] # Platforms this plugin will run on
|
||||||
|
author = 'DiapDealer, SomeUpdates' # The author of this plugin
|
||||||
|
version = (0, 3, 1) # The version number of this plugin
|
||||||
|
file_types = set(['prc','mobi','azw','azw1','tpz']) # The file types that this plugin will be applied to
|
||||||
|
on_import = True # Run this plugin during the import
|
||||||
|
priority = 210 # run this plugin before mobidedrm, k4pcdedrm, k4dedrm
|
||||||
|
minimum_calibre_version = (0, 7, 55)
|
||||||
|
|
||||||
|
def run(self, path_to_ebook):
|
||||||
|
|
||||||
|
k4 = True
|
||||||
|
if sys.platform.startswith('linux'):
|
||||||
|
k4 = False
|
||||||
|
pids = []
|
||||||
|
serials = []
|
||||||
|
kInfoFiles = []
|
||||||
|
|
||||||
|
# Get supplied list of PIDs to try from plugin customization.
|
||||||
|
customvalues = self.site_customization.split(',')
|
||||||
|
for customvalue in customvalues:
|
||||||
|
customvalue = str(customvalue)
|
||||||
|
customvalue = customvalue.strip()
|
||||||
|
if len(customvalue) == 10 or len(customvalue) == 8:
|
||||||
|
pids.append(customvalue)
|
||||||
|
else :
|
||||||
|
if len(customvalue) == 16 and customvalue[0] == 'B':
|
||||||
|
serials.append(customvalue)
|
||||||
|
else:
|
||||||
|
print "%s is not a valid Kindle serial number or PID." % str(customvalue)
|
||||||
|
|
||||||
|
# Load any kindle info files (*.info) included Calibre's config directory.
|
||||||
|
try:
|
||||||
|
# Find Calibre's configuration directory.
|
||||||
|
confpath = os.path.split(os.path.split(self.plugin_path)[0])[0]
|
||||||
|
print 'K4MobiDeDRM: Calibre configuration directory = %s' % confpath
|
||||||
|
files = os.listdir(confpath)
|
||||||
|
filefilter = re.compile("\.info$|\.kinf$", re.IGNORECASE)
|
||||||
|
files = filter(filefilter.search, files)
|
||||||
|
if files:
|
||||||
|
for filename in files:
|
||||||
|
fpath = os.path.join(confpath, filename)
|
||||||
|
kInfoFiles.append(fpath)
|
||||||
|
print 'K4MobiDeDRM: Kindle info/kinf file %s found in config folder.' % filename
|
||||||
|
except IOError:
|
||||||
|
print 'K4MobiDeDRM: Error reading kindle info/kinf files from config directory.'
|
||||||
|
pass
|
||||||
|
|
||||||
|
mobi = True
|
||||||
|
magic3 = file(path_to_ebook,'rb').read(3)
|
||||||
|
if magic3 == 'TPZ':
|
||||||
|
mobi = False
|
||||||
|
|
||||||
|
bookname = os.path.splitext(os.path.basename(path_to_ebook))[0]
|
||||||
|
|
||||||
|
if mobi:
|
||||||
|
mb = mobidedrm.MobiBook(path_to_ebook)
|
||||||
|
else:
|
||||||
|
mb = topazextract.TopazBook(path_to_ebook)
|
||||||
|
|
||||||
|
title = mb.getBookTitle()
|
||||||
|
md1, md2 = mb.getPIDMetaInfo()
|
||||||
|
pidlst = kgenpids.getPidList(md1, md2, k4, pids, serials, kInfoFiles)
|
||||||
|
|
||||||
|
try:
|
||||||
|
mb.processBook(pidlst)
|
||||||
|
|
||||||
|
except mobidedrm.DrmException:
|
||||||
|
#if you reached here then no luck raise and exception
|
||||||
|
if is_ok_to_use_qt():
|
||||||
|
from PyQt4.Qt import QMessageBox
|
||||||
|
d = QMessageBox(QMessageBox.Warning, "K4MobiDeDRM Plugin", "Error decoding: %s\n" % path_to_ebook)
|
||||||
|
d.show()
|
||||||
|
d.raise_()
|
||||||
|
d.exec_()
|
||||||
|
raise Exception("K4MobiDeDRM plugin could not decode the file")
|
||||||
|
except topazextract.TpzDRMError:
|
||||||
|
#if you reached here then no luck raise and exception
|
||||||
|
if is_ok_to_use_qt():
|
||||||
|
from PyQt4.Qt import QMessageBox
|
||||||
|
d = QMessageBox(QMessageBox.Warning, "K4MobiDeDRM Plugin", "Error decoding: %s\n" % path_to_ebook)
|
||||||
|
d.show()
|
||||||
|
d.raise_()
|
||||||
|
d.exec_()
|
||||||
|
raise Exception("K4MobiDeDRM plugin could not decode the file")
|
||||||
|
|
||||||
|
print "Success!"
|
||||||
|
if mobi:
|
||||||
|
of = self.temporary_file(bookname+'.mobi')
|
||||||
|
mb.getMobiFile(of.name)
|
||||||
|
else :
|
||||||
|
of = self.temporary_file(bookname+'.htmlz')
|
||||||
|
mb.getHTMLZip(of.name)
|
||||||
|
mb.cleanup()
|
||||||
|
return of.name
|
||||||
|
|
||||||
|
def customization_help(self, gui=False):
|
||||||
|
return 'Enter 10 character PIDs and/or Kindle serial numbers, use a comma (no spaces) to separate each PID or SerialNumber from the next.'
|
|
@ -21,10 +21,21 @@ from struct import unpack
|
||||||
|
|
||||||
|
|
||||||
# local support routines
|
# local support routines
|
||||||
import convert2xml
|
if 'calibre' in sys.modules:
|
||||||
import flatxml2html
|
inCalibre = True
|
||||||
import flatxml2svg
|
else:
|
||||||
import stylexml2css
|
inCalibre = False
|
||||||
|
|
||||||
|
if inCalibre :
|
||||||
|
from calibre_plugins.k4mobidedrm import convert2xml
|
||||||
|
from calibre_plugins.k4mobidedrm import flatxml2html
|
||||||
|
from calibre_plugins.k4mobidedrm import flatxml2svg
|
||||||
|
from calibre_plugins.k4mobidedrm import stylexml2css
|
||||||
|
else :
|
||||||
|
import convert2xml
|
||||||
|
import flatxml2html
|
||||||
|
import flatxml2svg
|
||||||
|
import stylexml2css
|
||||||
|
|
||||||
|
|
||||||
# Get a 7 bit encoded number from a file
|
# Get a 7 bit encoded number from a file
|
||||||
|
@ -504,7 +515,7 @@ def generateBook(bookDir, raw, fixedimage):
|
||||||
opfstr += ' </metadata>\n'
|
opfstr += ' </metadata>\n'
|
||||||
opfstr += '<manifest>\n'
|
opfstr += '<manifest>\n'
|
||||||
opfstr += ' <item id="book" href="book.html" media-type="application/xhtml+xml"/>\n'
|
opfstr += ' <item id="book" href="book.html" media-type="application/xhtml+xml"/>\n'
|
||||||
opfstr += ' <item id="stylesheet" href="style.css" media-type="text.css"/>\n'
|
opfstr += ' <item id="stylesheet" href="style.css" media-type="text/css"/>\n'
|
||||||
# adding image files to manifest
|
# adding image files to manifest
|
||||||
filenames = os.listdir(imgDir)
|
filenames = os.listdir(imgDir)
|
||||||
filenames = sorted(filenames)
|
filenames = sorted(filenames)
|
||||||
|
|
|
@ -0,0 +1,199 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
from __future__ import with_statement
|
||||||
|
|
||||||
|
# engine to remove drm from Kindle for Mac and Kindle for PC books
|
||||||
|
# for personal use for archiving and converting your ebooks
|
||||||
|
|
||||||
|
# PLEASE DO NOT PIRATE EBOOKS!
|
||||||
|
|
||||||
|
# We want all authors and publishers, and eBook stores to live
|
||||||
|
# long and prosperous lives but at the same time we just want to
|
||||||
|
# be able to read OUR books on whatever device we want and to keep
|
||||||
|
# readable for a long, long time
|
||||||
|
|
||||||
|
# This borrows very heavily from works by CMBDTC, IHeartCabbages, skindle,
|
||||||
|
# unswindle, DarkReverser, ApprenticeAlf, DiapDealer, some_updates
|
||||||
|
# and many many others
|
||||||
|
|
||||||
|
|
||||||
|
__version__ = '3.1'
|
||||||
|
|
||||||
|
class Unbuffered:
|
||||||
|
def __init__(self, stream):
|
||||||
|
self.stream = stream
|
||||||
|
def write(self, data):
|
||||||
|
self.stream.write(data)
|
||||||
|
self.stream.flush()
|
||||||
|
def __getattr__(self, attr):
|
||||||
|
return getattr(self.stream, attr)
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import os, csv, getopt
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
|
||||||
|
class DrmException(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
if 'calibre' in sys.modules:
|
||||||
|
inCalibre = True
|
||||||
|
else:
|
||||||
|
inCalibre = False
|
||||||
|
|
||||||
|
if inCalibre:
|
||||||
|
from calibre_plugins.k4mobidedrm import mobidedrm
|
||||||
|
from calibre_plugins.k4mobidedrm import topazextract
|
||||||
|
from calibre_plugins.k4mobidedrm import kgenpids
|
||||||
|
else:
|
||||||
|
import mobidedrm
|
||||||
|
import topazextract
|
||||||
|
import kgenpids
|
||||||
|
|
||||||
|
|
||||||
|
# cleanup bytestring filenames
|
||||||
|
# borrowed from calibre from calibre/src/calibre/__init__.py
|
||||||
|
# added in removal of non-printing chars
|
||||||
|
# and removal of . at start
|
||||||
|
# convert spaces to underscores
|
||||||
|
def cleanup_name(name):
|
||||||
|
_filename_sanitize = re.compile(r'[\xae\0\\|\?\*<":>\+/]')
|
||||||
|
substitute='_'
|
||||||
|
one = ''.join(char for char in name if char in string.printable)
|
||||||
|
one = _filename_sanitize.sub(substitute, one)
|
||||||
|
one = re.sub(r'\s', ' ', one).strip()
|
||||||
|
one = re.sub(r'^\.+$', '_', one)
|
||||||
|
one = one.replace('..', substitute)
|
||||||
|
# Windows doesn't like path components that end with a period
|
||||||
|
if one.endswith('.'):
|
||||||
|
one = one[:-1]+substitute
|
||||||
|
# Mac and Unix don't like file names that begin with a full stop
|
||||||
|
if len(one) > 0 and one[0] == '.':
|
||||||
|
one = substitute+one[1:]
|
||||||
|
one = one.replace(' ','_')
|
||||||
|
return one
|
||||||
|
|
||||||
|
def decryptBook(infile, outdir, k4, kInfoFiles, serials, pids):
|
||||||
|
# handle the obvious cases at the beginning
|
||||||
|
if not os.path.isfile(infile):
|
||||||
|
print "Error: Input file does not exist"
|
||||||
|
return 1
|
||||||
|
|
||||||
|
mobi = True
|
||||||
|
magic3 = file(infile,'rb').read(3)
|
||||||
|
if magic3 == 'TPZ':
|
||||||
|
mobi = False
|
||||||
|
|
||||||
|
bookname = os.path.splitext(os.path.basename(infile))[0]
|
||||||
|
|
||||||
|
if mobi:
|
||||||
|
mb = mobidedrm.MobiBook(infile)
|
||||||
|
else:
|
||||||
|
mb = topazextract.TopazBook(infile)
|
||||||
|
|
||||||
|
title = mb.getBookTitle()
|
||||||
|
print "Processing Book: ", title
|
||||||
|
filenametitle = cleanup_name(title)
|
||||||
|
outfilename = bookname
|
||||||
|
if len(bookname)>4 and len(filenametitle)>4 and bookname[:4] != filenametitle[:4]:
|
||||||
|
outfilename = outfilename + "_" + filenametitle
|
||||||
|
|
||||||
|
# build pid list
|
||||||
|
md1, md2 = mb.getPIDMetaInfo()
|
||||||
|
pidlst = kgenpids.getPidList(md1, md2, k4, pids, serials, kInfoFiles)
|
||||||
|
|
||||||
|
try:
|
||||||
|
mb.processBook(pidlst)
|
||||||
|
|
||||||
|
except mobidedrm.DrmException, e:
|
||||||
|
print "Error: " + str(e) + "\nDRM Removal Failed.\n"
|
||||||
|
return 1
|
||||||
|
except topazextract.TpzDRMError, e:
|
||||||
|
print "Error: " + str(e) + "\nDRM Removal Failed.\n"
|
||||||
|
return 1
|
||||||
|
except Exception, e:
|
||||||
|
print "Error: " + str(e) + "\nDRM Removal Failed.\n"
|
||||||
|
return 1
|
||||||
|
|
||||||
|
if mobi:
|
||||||
|
outfile = os.path.join(outdir, outfilename + '_nodrm' + '.mobi')
|
||||||
|
mb.getMobiFile(outfile)
|
||||||
|
return 0
|
||||||
|
|
||||||
|
# topaz:
|
||||||
|
print " Creating NoDRM HTMLZ Archive"
|
||||||
|
zipname = os.path.join(outdir, outfilename + '_nodrm' + '.htmlz')
|
||||||
|
mb.getHTMLZip(zipname)
|
||||||
|
|
||||||
|
print " Creating SVG HTMLZ Archive"
|
||||||
|
zipname = os.path.join(outdir, outfilename + '_SVG' + '.htmlz')
|
||||||
|
mb.getSVGZip(zipname)
|
||||||
|
|
||||||
|
print " Creating XML ZIP Archive"
|
||||||
|
zipname = os.path.join(outdir, outfilename + '_XML' + '.zip')
|
||||||
|
mb.getXMLZip(zipname)
|
||||||
|
|
||||||
|
# remove internal temporary directory of Topaz pieces
|
||||||
|
mb.cleanup()
|
||||||
|
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
def usage(progname):
|
||||||
|
print "Removes DRM protection from K4PC/M, Kindle, Mobi and Topaz ebooks"
|
||||||
|
print "Usage:"
|
||||||
|
print " %s [-k <kindle.info>] [-p <pidnums>] [-s <kindleSerialNumbers>] <infile> <outdir> " % progname
|
||||||
|
|
||||||
|
#
|
||||||
|
# Main
|
||||||
|
#
|
||||||
|
def main(argv=sys.argv):
|
||||||
|
progname = os.path.basename(argv[0])
|
||||||
|
|
||||||
|
k4 = False
|
||||||
|
kInfoFiles = []
|
||||||
|
serials = []
|
||||||
|
pids = []
|
||||||
|
|
||||||
|
print ('K4MobiDeDrm v%(__version__)s '
|
||||||
|
'provided by the work of many including DiapDealer, SomeUpdates, IHeartCabbages, CMBDTC, Skindle, DarkReverser, ApprenticeAlf, etc .' % globals())
|
||||||
|
|
||||||
|
print ' '
|
||||||
|
try:
|
||||||
|
opts, args = getopt.getopt(sys.argv[1:], "k:p:s:")
|
||||||
|
except getopt.GetoptError, err:
|
||||||
|
print str(err)
|
||||||
|
usage(progname)
|
||||||
|
sys.exit(2)
|
||||||
|
if len(args)<2:
|
||||||
|
usage(progname)
|
||||||
|
sys.exit(2)
|
||||||
|
|
||||||
|
for o, a in opts:
|
||||||
|
if o == "-k":
|
||||||
|
if a == None :
|
||||||
|
raise DrmException("Invalid parameter for -k")
|
||||||
|
kInfoFiles.append(a)
|
||||||
|
if o == "-p":
|
||||||
|
if a == None :
|
||||||
|
raise DrmException("Invalid parameter for -p")
|
||||||
|
pids = a.split(',')
|
||||||
|
if o == "-s":
|
||||||
|
if a == None :
|
||||||
|
raise DrmException("Invalid parameter for -s")
|
||||||
|
serials = a.split(',')
|
||||||
|
|
||||||
|
# try with built in Kindle Info files
|
||||||
|
k4 = True
|
||||||
|
if sys.platform.startswith('linux'):
|
||||||
|
k4 = False
|
||||||
|
kInfoFiles = None
|
||||||
|
infile = args[0]
|
||||||
|
outdir = args[1]
|
||||||
|
return decryptBook(infile, outdir, k4, kInfoFiles, serials, pids)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
sys.stdout=Unbuffered(sys.stdout)
|
||||||
|
sys.exit(main())
|
||||||
|
|
|
@ -11,16 +11,28 @@ from struct import pack, unpack, unpack_from
|
||||||
class DrmException(Exception):
|
class DrmException(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
global kindleDatabase
|
|
||||||
global charMap1
|
global charMap1
|
||||||
global charMap2
|
|
||||||
global charMap3
|
global charMap3
|
||||||
global charMap4
|
global charMap4
|
||||||
|
|
||||||
if sys.platform.startswith('win'):
|
if 'calibre' in sys.modules:
|
||||||
from k4pcutils import getKindleInfoFiles, parseKindleInfo, CryptUnprotectData, GetUserName, GetVolumeSerialNumber, charMap2
|
inCalibre = True
|
||||||
if sys.platform.startswith('darwin'):
|
else:
|
||||||
from k4mutils import getKindleInfoFiles, parseKindleInfo, CryptUnprotectData, GetUserName, GetVolumeSerialNumber, charMap2
|
inCalibre = False
|
||||||
|
|
||||||
|
if inCalibre:
|
||||||
|
if sys.platform.startswith('win'):
|
||||||
|
from calibre_plugins.k4mobidedrm.k4pcutils import getKindleInfoFiles, getDBfromFile, GetUserName, GetVolumeSerialNumber
|
||||||
|
|
||||||
|
if sys.platform.startswith('darwin'):
|
||||||
|
from calibre_plugins.k4mobidedrm.k4mutils import getKindleInfoFiles, getDBfromFile, GetUserName, GetVolumeSerialNumber
|
||||||
|
else:
|
||||||
|
if sys.platform.startswith('win'):
|
||||||
|
from k4pcutils import getKindleInfoFiles, getDBfromFile, GetUserName, GetVolumeSerialNumber
|
||||||
|
|
||||||
|
if sys.platform.startswith('darwin'):
|
||||||
|
from k4mutils import getKindleInfoFiles, getDBfromFile, GetUserName, GetVolumeSerialNumber
|
||||||
|
|
||||||
|
|
||||||
charMap1 = "n5Pr6St7Uv8Wx9YzAb0Cd1Ef2Gh3Jk4M"
|
charMap1 = "n5Pr6St7Uv8Wx9YzAb0Cd1Ef2Gh3Jk4M"
|
||||||
charMap3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
charMap3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
||||||
|
@ -66,50 +78,7 @@ def decode(data,map):
|
||||||
value = (((high * len(map)) ^ 0x80) & 0xFF) + low
|
value = (((high * len(map)) ^ 0x80) & 0xFF) + low
|
||||||
result += pack("B",value)
|
result += pack("B",value)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
# Get a record from the Kindle.info file for the key "hashedKey" (already hashed and encoded).
|
|
||||||
# Return the decoded and decrypted record
|
|
||||||
def getKindleInfoValueForHash(hashedKey):
|
|
||||||
global kindleDatabase
|
|
||||||
global charMap1
|
|
||||||
global charMap2
|
|
||||||
encryptedValue = decode(kindleDatabase[hashedKey],charMap2)
|
|
||||||
if sys.platform.startswith('win'):
|
|
||||||
return CryptUnprotectData(encryptedValue,"")
|
|
||||||
else:
|
|
||||||
cleartext = CryptUnprotectData(encryptedValue)
|
|
||||||
return decode(cleartext, charMap1)
|
|
||||||
|
|
||||||
# Get a record from the Kindle.info file for the string in "key" (plaintext).
|
|
||||||
# Return the decoded and decrypted record
|
|
||||||
def getKindleInfoValueForKey(key):
|
|
||||||
global charMap2
|
|
||||||
return getKindleInfoValueForHash(encodeHash(key,charMap2))
|
|
||||||
|
|
||||||
# Find if the original string for a hashed/encoded string is known.
|
|
||||||
# If so return the original string othwise return an empty string.
|
|
||||||
def findNameForHash(hash):
|
|
||||||
global charMap2
|
|
||||||
names = ["kindle.account.tokens","kindle.cookie.item","eulaVersionAccepted","login_date","kindle.token.item","login","kindle.key.item","kindle.name.info","kindle.device.info", "MazamaRandomNumber"]
|
|
||||||
result = ""
|
|
||||||
for name in names:
|
|
||||||
if hash == encodeHash(name, charMap2):
|
|
||||||
result = name
|
|
||||||
break
|
|
||||||
return result
|
|
||||||
|
|
||||||
# Print all the records from the kindle.info file (option -i)
|
|
||||||
def printKindleInfo():
|
|
||||||
for record in kindleDatabase:
|
|
||||||
name = findNameForHash(record)
|
|
||||||
if name != "" :
|
|
||||||
print (name)
|
|
||||||
print ("--------------------------")
|
|
||||||
else :
|
|
||||||
print ("Unknown Record")
|
|
||||||
print getKindleInfoValueForHash(record)
|
|
||||||
print "\n"
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# PID generation routines
|
# PID generation routines
|
||||||
#
|
#
|
||||||
|
@ -222,15 +191,15 @@ def getKindlePid(pidlst, rec209, token, serialnum):
|
||||||
return pidlst
|
return pidlst
|
||||||
|
|
||||||
|
|
||||||
# Parse the EXTH header records and parse the Kindleinfo
|
# parse the Kindleinfo file to calculate the book pid.
|
||||||
# file to calculate the book pid.
|
|
||||||
|
keynames = ["kindle.account.tokens","kindle.cookie.item","eulaVersionAccepted","login_date","kindle.token.item","login","kindle.key.item","kindle.name.info","kindle.device.info", "MazamaRandomNumber"]
|
||||||
|
|
||||||
def getK4Pids(pidlst, rec209, token, kInfoFile):
|
def getK4Pids(pidlst, rec209, token, kInfoFile):
|
||||||
global kindleDatabase
|
|
||||||
global charMap1
|
global charMap1
|
||||||
kindleDatabase = None
|
kindleDatabase = None
|
||||||
try:
|
try:
|
||||||
kindleDatabase = parseKindleInfo(kInfoFile)
|
kindleDatabase = getDBfromFile(kInfoFile)
|
||||||
except Exception, message:
|
except Exception, message:
|
||||||
print(message)
|
print(message)
|
||||||
kindleDatabase = None
|
kindleDatabase = None
|
||||||
|
@ -241,10 +210,10 @@ def getK4Pids(pidlst, rec209, token, kInfoFile):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Get the Mazama Random number
|
# Get the Mazama Random number
|
||||||
MazamaRandomNumber = getKindleInfoValueForKey("MazamaRandomNumber")
|
MazamaRandomNumber = kindleDatabase["MazamaRandomNumber"]
|
||||||
|
|
||||||
# Get the kindle account token
|
# Get the kindle account token
|
||||||
kindleAccountToken = getKindleInfoValueForKey("kindle.account.tokens")
|
kindleAccountToken = kindleDatabase["kindle.account.tokens"]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
print "Keys not found in " + kInfoFile
|
print "Keys not found in " + kInfoFile
|
||||||
return pidlst
|
return pidlst
|
||||||
|
|
|
@ -10,7 +10,12 @@ class Unbuffered:
|
||||||
return getattr(self.stream, attr)
|
return getattr(self.stream, attr)
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
sys.stdout=Unbuffered(sys.stdout)
|
|
||||||
|
if 'calibre' in sys.modules:
|
||||||
|
inCalibre = True
|
||||||
|
else:
|
||||||
|
inCalibre = False
|
||||||
|
|
||||||
import os, csv, getopt
|
import os, csv, getopt
|
||||||
import zlib, zipfile, tempfile, shutil
|
import zlib, zipfile, tempfile, shutil
|
||||||
from struct import pack
|
from struct import pack
|
||||||
|
@ -18,10 +23,32 @@ from struct import unpack
|
||||||
|
|
||||||
class TpzDRMError(Exception):
|
class TpzDRMError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
# local support routines
|
# local support routines
|
||||||
import kgenpids
|
if inCalibre:
|
||||||
import genbook
|
from calibre_plugins.k4mobidedrm import kgenpids
|
||||||
|
from calibre_plugins.k4mobidedrm import genbook
|
||||||
|
else:
|
||||||
|
import kgenpids
|
||||||
|
import genbook
|
||||||
|
|
||||||
|
|
||||||
|
# recursive zip creation support routine
|
||||||
|
def zipUpDir(myzip, tdir, localname):
|
||||||
|
currentdir = tdir
|
||||||
|
if localname != "":
|
||||||
|
currentdir = os.path.join(currentdir,localname)
|
||||||
|
list = os.listdir(currentdir)
|
||||||
|
for file in list:
|
||||||
|
afilename = file
|
||||||
|
localfilePath = os.path.join(localname, afilename)
|
||||||
|
realfilePath = os.path.join(currentdir,file)
|
||||||
|
if os.path.isfile(realfilePath):
|
||||||
|
myzip.write(realfilePath, localfilePath)
|
||||||
|
elif os.path.isdir(realfilePath):
|
||||||
|
zipUpDir(myzip, tdir, localfilePath)
|
||||||
|
|
||||||
#
|
#
|
||||||
# Utility routines
|
# Utility routines
|
||||||
#
|
#
|
||||||
|
@ -110,9 +137,9 @@ def decryptDkeyRecords(data,PID):
|
||||||
|
|
||||||
|
|
||||||
class TopazBook:
|
class TopazBook:
|
||||||
def __init__(self, filename, outdir):
|
def __init__(self, filename):
|
||||||
self.fo = file(filename, 'rb')
|
self.fo = file(filename, 'rb')
|
||||||
self.outdir = outdir
|
self.outdir = tempfile.mkdtemp()
|
||||||
self.bookPayloadOffset = 0
|
self.bookPayloadOffset = 0
|
||||||
self.bookHeaderRecords = {}
|
self.bookHeaderRecords = {}
|
||||||
self.bookMetadata = {}
|
self.bookMetadata = {}
|
||||||
|
@ -317,21 +344,33 @@ class TopazBook:
|
||||||
file(outputFile, 'wb').write(record)
|
file(outputFile, 'wb').write(record)
|
||||||
print " "
|
print " "
|
||||||
|
|
||||||
|
def getHTMLZip(self, zipname):
|
||||||
|
htmlzip = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
||||||
|
htmlzip.write(os.path.join(self.outdir,'book.html'),'book.html')
|
||||||
|
htmlzip.write(os.path.join(self.outdir,'book.opf'),'book.opf')
|
||||||
|
if os.path.isfile(os.path.join(self.outdir,'cover.jpg')):
|
||||||
|
htmlzip.write(os.path.join(self.outdir,'cover.jpg'),'cover.jpg')
|
||||||
|
htmlzip.write(os.path.join(self.outdir,'style.css'),'style.css')
|
||||||
|
zipUpDir(htmlzip, self.outdir, 'img')
|
||||||
|
htmlzip.close()
|
||||||
|
|
||||||
def zipUpDir(myzip, tempdir,localname):
|
def getSVGZip(self, zipname):
|
||||||
currentdir = tempdir
|
svgzip = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
||||||
if localname != "":
|
svgzip.write(os.path.join(self.outdir,'index_svg.xhtml'),'index_svg.xhtml')
|
||||||
currentdir = os.path.join(currentdir,localname)
|
zipUpDir(svgzip, self.outdir, 'svg')
|
||||||
list = os.listdir(currentdir)
|
zipUpDir(svgzip, self.outdir, 'img')
|
||||||
for file in list:
|
svgzip.close()
|
||||||
afilename = file
|
|
||||||
localfilePath = os.path.join(localname, afilename)
|
|
||||||
realfilePath = os.path.join(currentdir,file)
|
|
||||||
if os.path.isfile(realfilePath):
|
|
||||||
myzip.write(realfilePath, localfilePath)
|
|
||||||
elif os.path.isdir(realfilePath):
|
|
||||||
zipUpDir(myzip, tempdir, localfilePath)
|
|
||||||
|
|
||||||
|
def getXMLZip(self, zipname):
|
||||||
|
xmlzip = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
||||||
|
targetdir = os.path.join(self.outdir,'xml')
|
||||||
|
zipUpDir(xmlzip, targetdir, '')
|
||||||
|
zipUpDir(xmlzip, self.outdir, 'img')
|
||||||
|
xmlzip.close()
|
||||||
|
|
||||||
|
def cleanup(self):
|
||||||
|
if os.path.isdir(self.outdir):
|
||||||
|
shutil.rmtree(self.outdir, True)
|
||||||
|
|
||||||
def usage(progname):
|
def usage(progname):
|
||||||
print "Removes DRM protection from Topaz ebooks and extract the contents"
|
print "Removes DRM protection from Topaz ebooks and extract the contents"
|
||||||
|
@ -383,58 +422,46 @@ def main(argv=sys.argv):
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
bookname = os.path.splitext(os.path.basename(infile))[0]
|
bookname = os.path.splitext(os.path.basename(infile))[0]
|
||||||
tempdir = tempfile.mkdtemp()
|
|
||||||
|
|
||||||
tb = TopazBook(infile, tempdir)
|
tb = TopazBook(infile)
|
||||||
title = tb.getBookTitle()
|
title = tb.getBookTitle()
|
||||||
print "Processing Book: ", title
|
print "Processing Book: ", title
|
||||||
keysRecord, keysRecordRecord = tb.getPIDMetaInfo()
|
keysRecord, keysRecordRecord = tb.getPIDMetaInfo()
|
||||||
pidlst = kgenpids.getPidList(keysRecord, keysRecordRecord, k4, pids, serials, kInfoFiles)
|
pidlst = kgenpids.getPidList(keysRecord, keysRecordRecord, k4, pids, serials, kInfoFiles)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
print "Decrypting Book"
|
||||||
tb.processBook(pidlst)
|
tb.processBook(pidlst)
|
||||||
|
|
||||||
|
print " Creating HTML ZIP Archive"
|
||||||
|
zipname = os.path.join(outdir, bookname + '_nodrm' + '.htmlz')
|
||||||
|
tb.getHTMLZip(zipname)
|
||||||
|
|
||||||
|
print " Creating SVG ZIP Archive"
|
||||||
|
zipname = os.path.join(outdir, bookname + '_SVG' + '.htmlz')
|
||||||
|
tb.getSVGZip(zipname)
|
||||||
|
|
||||||
|
print " Creating XML ZIP Archive"
|
||||||
|
zipname = os.path.join(outdir, bookname + '_XML' + '.zip')
|
||||||
|
tb.getXMLZip(zipname)
|
||||||
|
|
||||||
|
# removing internal temporary directory of pieces
|
||||||
|
tb.cleanup()
|
||||||
|
|
||||||
except TpzDRMError, e:
|
except TpzDRMError, e:
|
||||||
print str(e)
|
print str(e)
|
||||||
print " Creating DeBug Full Zip Archive of Book"
|
tb.cleanup()
|
||||||
zipname = os.path.join(outdir, bookname + '_debug' + '.zip')
|
|
||||||
myzip = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
|
||||||
zipUpDir(myzip, tempdir, '')
|
|
||||||
myzip.close()
|
|
||||||
shutil.rmtree(tempdir, True)
|
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
print " Creating HTML ZIP Archive"
|
except Exception, e:
|
||||||
zipname = os.path.join(outdir, bookname + '_nodrm' + '.zip')
|
print str(e)
|
||||||
myzip1 = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
tb.cleanup
|
||||||
myzip1.write(os.path.join(tempdir,'book.html'),'book.html')
|
return 1
|
||||||
myzip1.write(os.path.join(tempdir,'book.opf'),'book.opf')
|
|
||||||
if os.path.isfile(os.path.join(tempdir,'cover.jpg')):
|
|
||||||
myzip1.write(os.path.join(tempdir,'cover.jpg'),'cover.jpg')
|
|
||||||
myzip1.write(os.path.join(tempdir,'style.css'),'style.css')
|
|
||||||
zipUpDir(myzip1, tempdir, 'img')
|
|
||||||
myzip1.close()
|
|
||||||
|
|
||||||
print " Creating SVG ZIP Archive"
|
|
||||||
zipname = os.path.join(outdir, bookname + '_SVG' + '.zip')
|
|
||||||
myzip2 = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
|
||||||
myzip2.write(os.path.join(tempdir,'index_svg.xhtml'),'index_svg.xhtml')
|
|
||||||
zipUpDir(myzip2, tempdir, 'svg')
|
|
||||||
zipUpDir(myzip2, tempdir, 'img')
|
|
||||||
myzip2.close()
|
|
||||||
|
|
||||||
print " Creating XML ZIP Archive"
|
|
||||||
zipname = os.path.join(outdir, bookname + '_XML' + '.zip')
|
|
||||||
myzip3 = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
|
||||||
targetdir = os.path.join(tempdir,'xml')
|
|
||||||
zipUpDir(myzip3, targetdir, '')
|
|
||||||
zipUpDir(myzip3, tempdir, 'img')
|
|
||||||
myzip3.close()
|
|
||||||
|
|
||||||
shutil.rmtree(tempdir, True)
|
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
sys.stdout=Unbuffered(sys.stdout)
|
||||||
sys.exit(main())
|
sys.exit(main())
|
||||||
|
|
||||||
|
|
|
@ -5,12 +5,11 @@ This plugin supersedes MobiDeDRM, K4DeDRM, and K4PCDeDRM and K4X plugins. If yo
|
||||||
This plugin is meant to remove the DRM from .prc, .azw, .azw1, and .tpz ebooks. Calibre can then convert them to whatever format you desire. It is meant to function without having to install any dependencies except for Calibre being on your same machine and in the same account as your "Kindle for PC" or "Kindle for Mac" application if you are going to remove the DRM from those types of books.
|
This plugin is meant to remove the DRM from .prc, .azw, .azw1, and .tpz ebooks. Calibre can then convert them to whatever format you desire. It is meant to function without having to install any dependencies except for Calibre being on your same machine and in the same account as your "Kindle for PC" or "Kindle for Mac" application if you are going to remove the DRM from those types of books.
|
||||||
|
|
||||||
Installation:
|
Installation:
|
||||||
Go to Calibre's Preferences page... click on the Plugins button. Use the file dialog button to select the plugin's zip file (K4MobiDeDRM_vXX_plugin.zip) and click the 'Add' button. You're done.
|
Go to Calibre's Preferences page... click on the Plugins button. Click on the "Add a new plugin" button at the bottom of the screen. Use the file dialog button to select the plugin's zip file (K4MobiDeDRM_vXX_plugin.zip) and click the "Add" (or it may say "Open" button. Then click on the "Yes" button in the warning dialog that appears. A Confirmation dialog appears that says the plugin has been installed.
|
||||||
|
|
||||||
|
|
||||||
Please note: Calibre does not provide any immediate feedback to indicate that adding the plugin was a success. You can always click on the File-Type plugins to see if the plugin was added.
|
|
||||||
|
|
||||||
Configuration:
|
Configuration:
|
||||||
Highlight the plugin (K4MobiDeDRM under the "File type plugins" category) and click the "Customize Plugin" button on Calibre's Preferences->Plugins page. Enter a comma separated list of your 10 digit PIDs. Include in this list (again separated by commas) any 16 digit serial numbers the standalone Kindles you may have (these typically begin "B0...") This is not needed if you only want to decode "Kindle for PC" or "Kindle for Mac" books.
|
Highlight the plugin (K4MobiDeDRM under the "File type plugins" category) and click the "Customize Plugin" button on Calibre's Preferences->Plugins page. Enter your 10 digit PID. If you have more than one PID separate them with a comma (no spaces). If you have a standalone Kindle include the 16 digit serial number (these typically begin "B0...") in this list (again separated from the PIDs or other serial numbers with a comma (no spaces). This configuration is not needed if you only want to decode "Kindle for PC" or "Kindle for Mac" books.
|
||||||
|
|
||||||
|
|
||||||
Troubleshooting:
|
Troubleshooting:
|
||||||
|
|
Binary file not shown.
|
@ -31,10 +31,15 @@
|
||||||
# 0.0.1 - Initial release
|
# 0.0.1 - Initial release
|
||||||
# 0.0.2 - updated to distinguish it from earlier non-openssl version
|
# 0.0.2 - updated to distinguish it from earlier non-openssl version
|
||||||
# 0.0.3 - removed added psyco code as it is not supported under Calibre's Python 2.7
|
# 0.0.3 - removed added psyco code as it is not supported under Calibre's Python 2.7
|
||||||
|
# 0.0.4 - minor typos fixed
|
||||||
|
# 0.0.5 - updated to the new calibre plugin interface
|
||||||
|
|
||||||
import sys, os
|
import sys, os
|
||||||
|
|
||||||
from calibre.customize import FileTypePlugin
|
from calibre.customize import FileTypePlugin
|
||||||
|
from calibre.ptempfile import PersistentTemporaryDirectory
|
||||||
|
from calibre.constants import iswindows, isosx
|
||||||
|
from calibre_plugins.erdrpdb2pml import erdr2pml
|
||||||
|
|
||||||
class eRdrDeDRM(FileTypePlugin):
|
class eRdrDeDRM(FileTypePlugin):
|
||||||
name = 'eReader PDB 2 PML' # Name of the plugin
|
name = 'eReader PDB 2 PML' # Name of the plugin
|
||||||
|
@ -42,16 +47,14 @@ class eRdrDeDRM(FileTypePlugin):
|
||||||
Credit given to The Dark Reverser for the original standalone script.'
|
Credit given to The Dark Reverser for the original standalone script.'
|
||||||
supported_platforms = ['linux', 'osx', 'windows'] # Platforms this plugin will run on
|
supported_platforms = ['linux', 'osx', 'windows'] # Platforms this plugin will run on
|
||||||
author = 'DiapDealer' # The author of this plugin
|
author = 'DiapDealer' # The author of this plugin
|
||||||
version = (0, 0, 4) # The version number of this plugin
|
version = (0, 0, 5) # The version number of this plugin
|
||||||
file_types = set(['pdb']) # The file types that this plugin will be applied to
|
file_types = set(['pdb']) # The file types that this plugin will be applied to
|
||||||
on_import = True # Run this plugin during the import
|
on_import = True # Run this plugin during the import
|
||||||
|
minimum_calibre_version = (0, 7, 55)
|
||||||
|
|
||||||
def run(self, path_to_ebook):
|
def run(self, path_to_ebook):
|
||||||
from calibre.ptempfile import PersistentTemporaryDirectory
|
|
||||||
from calibre.constants import iswindows, isosx
|
|
||||||
|
|
||||||
global bookname, erdr2pml
|
global bookname, erdr2pml
|
||||||
import erdr2pml
|
|
||||||
|
|
||||||
infile = path_to_ebook
|
infile = path_to_ebook
|
||||||
bookname = os.path.splitext(os.path.basename(infile))[0]
|
bookname = os.path.splitext(os.path.basename(infile))[0]
|
|
@ -58,8 +58,9 @@
|
||||||
# 0.17 - added support for pycrypto's DES as well
|
# 0.17 - added support for pycrypto's DES as well
|
||||||
# 0.18 - on Windows try PyCrypto first and OpenSSL next
|
# 0.18 - on Windows try PyCrypto first and OpenSSL next
|
||||||
# 0.19 - Modify the interface to allow use of import
|
# 0.19 - Modify the interface to allow use of import
|
||||||
|
# 0.20 - modify to allow use inside new interface for calibre plugins
|
||||||
|
|
||||||
__version__='0.19'
|
__version__='0.20'
|
||||||
|
|
||||||
class Unbuffered:
|
class Unbuffered:
|
||||||
def __init__(self, stream):
|
def __init__(self, stream):
|
||||||
|
@ -71,32 +72,50 @@ class Unbuffered:
|
||||||
return getattr(self.stream, attr)
|
return getattr(self.stream, attr)
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
sys.stdout=Unbuffered(sys.stdout)
|
|
||||||
|
|
||||||
import struct, binascii, getopt, zlib, os, os.path, urllib, tempfile
|
import struct, binascii, getopt, zlib, os, os.path, urllib, tempfile
|
||||||
|
|
||||||
|
if 'calibre' in sys.modules:
|
||||||
|
inCalibre = True
|
||||||
|
else:
|
||||||
|
inCalibre = False
|
||||||
|
|
||||||
Des = None
|
Des = None
|
||||||
if sys.platform.startswith('win'):
|
if sys.platform.startswith('win'):
|
||||||
# first try with pycrypto
|
# first try with pycrypto
|
||||||
import pycrypto_des
|
if inCalibre:
|
||||||
|
from calibre_plugins.erdrpdb2pml import pycrypto_des
|
||||||
|
else:
|
||||||
|
import pycrypto_des
|
||||||
Des = pycrypto_des.load_pycrypto()
|
Des = pycrypto_des.load_pycrypto()
|
||||||
if Des == None:
|
if Des == None:
|
||||||
# they try with openssl
|
# they try with openssl
|
||||||
import openssl_des
|
if inCalibre:
|
||||||
|
from calibre_plugins.erdrpdb2pml import openssl_des
|
||||||
|
else:
|
||||||
|
import openssl_des
|
||||||
Des = openssl_des.load_libcrypto()
|
Des = openssl_des.load_libcrypto()
|
||||||
else:
|
else:
|
||||||
# first try with openssl
|
# first try with openssl
|
||||||
import openssl_des
|
if inCalibre:
|
||||||
|
from calibre_plugins.erdrpdb2pml import openssl_des
|
||||||
|
else:
|
||||||
|
import openssl_des
|
||||||
Des = openssl_des.load_libcrypto()
|
Des = openssl_des.load_libcrypto()
|
||||||
if Des == None:
|
if Des == None:
|
||||||
# then try with pycrypto
|
# then try with pycrypto
|
||||||
import pycrypto_des
|
if inCalibre:
|
||||||
|
from calibre_plugins.erdrpdb2pml import pycrypto_des
|
||||||
|
else:
|
||||||
|
import pycrypto_des
|
||||||
Des = pycrypto_des.load_pycrypto()
|
Des = pycrypto_des.load_pycrypto()
|
||||||
|
|
||||||
# if that did not work then use pure python implementation
|
# if that did not work then use pure python implementation
|
||||||
# of DES and try to speed it up with Psycho
|
# of DES and try to speed it up with Psycho
|
||||||
if Des == None:
|
if Des == None:
|
||||||
import python_des
|
if inCalibre:
|
||||||
|
from calibre_plugins.erdrpdb2pml import python_des
|
||||||
|
else:
|
||||||
|
import python_des
|
||||||
Des = python_des.Des
|
Des = python_des.Des
|
||||||
# Import Psyco if available
|
# Import Psyco if available
|
||||||
try:
|
try:
|
||||||
|
@ -480,5 +499,6 @@ def main(argv=None):
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
sys.stdout=Unbuffered(sys.stdout)
|
||||||
sys.exit(main())
|
sys.exit(main())
|
||||||
|
|
||||||
|
|
Binary file not shown.
|
@ -4,7 +4,7 @@
|
||||||
# Released under the terms of the GNU General Public Licence, version 3 or
|
# Released under the terms of the GNU General Public Licence, version 3 or
|
||||||
# later. <http://www.gnu.org/licenses/>
|
# later. <http://www.gnu.org/licenses/>
|
||||||
#
|
#
|
||||||
# Requires Calibre version 0.6.44 or higher.
|
# Requires Calibre version 0.7.55 or higher.
|
||||||
#
|
#
|
||||||
# All credit given to I <3 Cabbages for the original standalone scripts.
|
# All credit given to I <3 Cabbages for the original standalone scripts.
|
||||||
# I had the much easier job of converting them to Calibre a plugin.
|
# I had the much easier job of converting them to Calibre a plugin.
|
||||||
|
@ -48,6 +48,7 @@
|
||||||
# 0.1.3 - Try PyCrypto on Windows first
|
# 0.1.3 - Try PyCrypto on Windows first
|
||||||
# 0.1.4 - update zipfix to deal with mimetype not in correct place
|
# 0.1.4 - update zipfix to deal with mimetype not in correct place
|
||||||
# 0.1.5 - update zipfix to deal with completely missing mimetype files
|
# 0.1.5 - update zipfix to deal with completely missing mimetype files
|
||||||
|
# 0.1.6 - update ot the new calibre plugin interface
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Decrypt Barnes & Noble ADEPT encrypted EPUB books.
|
Decrypt Barnes & Noble ADEPT encrypted EPUB books.
|
||||||
|
@ -266,6 +267,7 @@ def plugin_main(userkey, inpath, outpath):
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
from calibre.customize import FileTypePlugin
|
from calibre.customize import FileTypePlugin
|
||||||
|
from calibre.constants import iswindows, isosx
|
||||||
|
|
||||||
class IgnobleDeDRM(FileTypePlugin):
|
class IgnobleDeDRM(FileTypePlugin):
|
||||||
name = 'Ignoble Epub DeDRM'
|
name = 'Ignoble Epub DeDRM'
|
||||||
|
@ -273,8 +275,8 @@ class IgnobleDeDRM(FileTypePlugin):
|
||||||
Credit given to I <3 Cabbages for the original stand-alone scripts.'
|
Credit given to I <3 Cabbages for the original stand-alone scripts.'
|
||||||
supported_platforms = ['linux', 'osx', 'windows']
|
supported_platforms = ['linux', 'osx', 'windows']
|
||||||
author = 'DiapDealer'
|
author = 'DiapDealer'
|
||||||
version = (0, 1, 5)
|
version = (0, 1, 6)
|
||||||
minimum_calibre_version = (0, 6, 44) # Compiled python libraries cannot be imported in earlier versions.
|
minimum_calibre_version = (0, 7, 55) # Compiled python libraries cannot be imported in earlier versions.
|
||||||
file_types = set(['epub'])
|
file_types = set(['epub'])
|
||||||
on_import = True
|
on_import = True
|
||||||
|
|
||||||
|
@ -282,10 +284,6 @@ class IgnobleDeDRM(FileTypePlugin):
|
||||||
global AES
|
global AES
|
||||||
global AES2
|
global AES2
|
||||||
|
|
||||||
from calibre.gui2 import is_ok_to_use_qt
|
|
||||||
from PyQt4.Qt import QMessageBox
|
|
||||||
from calibre.constants import iswindows, isosx
|
|
||||||
|
|
||||||
AES, AES2 = _load_crypto()
|
AES, AES2 = _load_crypto()
|
||||||
|
|
||||||
if AES == None or AES2 == None:
|
if AES == None or AES2 == None:
|
||||||
|
@ -341,7 +339,7 @@ class IgnobleDeDRM(FileTypePlugin):
|
||||||
for userkey in userkeys:
|
for userkey in userkeys:
|
||||||
# Create a TemporaryPersistent file to work with.
|
# Create a TemporaryPersistent file to work with.
|
||||||
# Check original epub archive for zip errors.
|
# Check original epub archive for zip errors.
|
||||||
import zipfix
|
from calibre_plugins.ignobleepub import zipfix
|
||||||
inf = self.temporary_file('.epub')
|
inf = self.temporary_file('.epub')
|
||||||
try:
|
try:
|
||||||
fr = zipfix.fixZip(path_to_ebook, inf.name)
|
fr = zipfix.fixZip(path_to_ebook, inf.name)
|
||||||
|
|
Binary file not shown.
|
@ -4,7 +4,7 @@
|
||||||
# Released under the terms of the GNU General Public Licence, version 3 or
|
# Released under the terms of the GNU General Public Licence, version 3 or
|
||||||
# later. <http://www.gnu.org/licenses/>
|
# later. <http://www.gnu.org/licenses/>
|
||||||
#
|
#
|
||||||
# Requires Calibre version 0.6.44 or higher.
|
# Requires Calibre version 0.7.55 or higher.
|
||||||
#
|
#
|
||||||
# All credit given to I <3 Cabbages for the original standalone scripts.
|
# All credit given to I <3 Cabbages for the original standalone scripts.
|
||||||
# I had the much easier job of converting them to a Calibre plugin.
|
# I had the much easier job of converting them to a Calibre plugin.
|
||||||
|
@ -49,6 +49,7 @@
|
||||||
# 0.1.4 - default to try PyCrypto first on Windows
|
# 0.1.4 - default to try PyCrypto first on Windows
|
||||||
# 0.1.5 - update zipfix to handle out of position mimetypes
|
# 0.1.5 - update zipfix to handle out of position mimetypes
|
||||||
# 0.1.6 - update zipfix to handle completely missing mimetype files
|
# 0.1.6 - update zipfix to handle completely missing mimetype files
|
||||||
|
# 0.1.7 - update to new calibre plugin interface
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Decrypt Adobe ADEPT-encrypted EPUB books.
|
Decrypt Adobe ADEPT-encrypted EPUB books.
|
||||||
|
@ -365,6 +366,7 @@ def plugin_main(userkey, inpath, outpath):
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
from calibre.customize import FileTypePlugin
|
from calibre.customize import FileTypePlugin
|
||||||
|
from calibre.constants import iswindows, isosx
|
||||||
|
|
||||||
class IneptDeDRM(FileTypePlugin):
|
class IneptDeDRM(FileTypePlugin):
|
||||||
name = 'Inept Epub DeDRM'
|
name = 'Inept Epub DeDRM'
|
||||||
|
@ -372,8 +374,8 @@ class IneptDeDRM(FileTypePlugin):
|
||||||
Credit given to I <3 Cabbages for the original stand-alone scripts.'
|
Credit given to I <3 Cabbages for the original stand-alone scripts.'
|
||||||
supported_platforms = ['linux', 'osx', 'windows']
|
supported_platforms = ['linux', 'osx', 'windows']
|
||||||
author = 'DiapDealer'
|
author = 'DiapDealer'
|
||||||
version = (0, 1, 6)
|
version = (0, 1, 7)
|
||||||
minimum_calibre_version = (0, 6, 44) # Compiled python libraries cannot be imported in earlier versions.
|
minimum_calibre_version = (0, 7, 55) # Compiled python libraries cannot be imported in earlier versions.
|
||||||
file_types = set(['epub'])
|
file_types = set(['epub'])
|
||||||
on_import = True
|
on_import = True
|
||||||
priority = 100
|
priority = 100
|
||||||
|
@ -382,10 +384,6 @@ class IneptDeDRM(FileTypePlugin):
|
||||||
global AES
|
global AES
|
||||||
global RSA
|
global RSA
|
||||||
|
|
||||||
from calibre.gui2 import is_ok_to_use_qt
|
|
||||||
from PyQt4.Qt import QMessageBox
|
|
||||||
from calibre.constants import iswindows, isosx
|
|
||||||
|
|
||||||
AES, RSA = _load_crypto()
|
AES, RSA = _load_crypto()
|
||||||
|
|
||||||
if AES == None or RSA == None:
|
if AES == None or RSA == None:
|
||||||
|
@ -418,7 +416,7 @@ class IneptDeDRM(FileTypePlugin):
|
||||||
# Calibre's configuration directory for future use.
|
# Calibre's configuration directory for future use.
|
||||||
if iswindows or isosx:
|
if iswindows or isosx:
|
||||||
# ADE key retrieval script included in respective OS folder.
|
# ADE key retrieval script included in respective OS folder.
|
||||||
from ade_key import retrieve_key
|
from calibre_plugins.ineptepub.ade_key import retrieve_key
|
||||||
try:
|
try:
|
||||||
keydata = retrieve_key()
|
keydata = retrieve_key()
|
||||||
userkeys.append(keydata)
|
userkeys.append(keydata)
|
||||||
|
@ -439,7 +437,7 @@ class IneptDeDRM(FileTypePlugin):
|
||||||
for userkey in userkeys:
|
for userkey in userkeys:
|
||||||
# Create a TemporaryPersistent file to work with.
|
# Create a TemporaryPersistent file to work with.
|
||||||
# Check original epub archive for zip errors.
|
# Check original epub archive for zip errors.
|
||||||
import zipfix
|
from calibre_plugins.ineptepub import zipfix
|
||||||
inf = self.temporary_file('.epub')
|
inf = self.temporary_file('.epub')
|
||||||
try:
|
try:
|
||||||
fr = zipfix.fixZip(path_to_ebook, inf.name)
|
fr = zipfix.fixZip(path_to_ebook, inf.name)
|
Binary file not shown.
|
@ -1,6 +1,6 @@
|
||||||
#! /usr/bin/env python
|
#! /usr/bin/env python
|
||||||
|
|
||||||
# ineptpdf_plugin.py
|
# ineptpdf plugin __init__.py
|
||||||
# Released under the terms of the GNU General Public Licence, version 3 or
|
# Released under the terms of the GNU General Public Licence, version 3 or
|
||||||
# later. <http://www.gnu.org/licenses/>
|
# later. <http://www.gnu.org/licenses/>
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@
|
||||||
# be able to read OUR books on whatever device we want and to keep
|
# be able to read OUR books on whatever device we want and to keep
|
||||||
# readable for a long, long time
|
# readable for a long, long time
|
||||||
|
|
||||||
# Requires Calibre version 0.6.44 or higher.
|
# Requires Calibre version 0.7.55 or higher.
|
||||||
#
|
#
|
||||||
# All credit given to I <3 Cabbages for the original standalone scripts.
|
# All credit given to I <3 Cabbages for the original standalone scripts.
|
||||||
# I had the much easier job of converting them to a Calibre plugin.
|
# I had the much easier job of converting them to a Calibre plugin.
|
||||||
|
@ -51,6 +51,7 @@
|
||||||
# 0.1.1 - back port ineptpdf 8.4.X support for increased number of encryption methods
|
# 0.1.1 - back port ineptpdf 8.4.X support for increased number of encryption methods
|
||||||
# 0.1.2 - back port ineptpdf 8.4.X bug fixes
|
# 0.1.2 - back port ineptpdf 8.4.X bug fixes
|
||||||
# 0.1.3 - add in fix for improper rejection of session bookkeys with len(bookkey) = length + 1
|
# 0.1.3 - add in fix for improper rejection of session bookkeys with len(bookkey) = length + 1
|
||||||
|
# 0.1.4 - update to the new calibre plugin interface
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Decrypts Adobe ADEPT-encrypted PDF files.
|
Decrypts Adobe ADEPT-encrypted PDF files.
|
||||||
|
@ -174,6 +175,7 @@ def _load_crypto_libcrypto():
|
||||||
return out.raw
|
return out.raw
|
||||||
|
|
||||||
class AES(object):
|
class AES(object):
|
||||||
|
MODE_CBC = 0
|
||||||
@classmethod
|
@classmethod
|
||||||
def new(cls, userkey, mode, iv):
|
def new(cls, userkey, mode, iv):
|
||||||
self = AES()
|
self = AES()
|
||||||
|
@ -2126,6 +2128,7 @@ def plugin_main(keypath, inpath, outpath):
|
||||||
|
|
||||||
|
|
||||||
from calibre.customize import FileTypePlugin
|
from calibre.customize import FileTypePlugin
|
||||||
|
from calibre.constants import iswindows, isosx
|
||||||
|
|
||||||
class IneptPDFDeDRM(FileTypePlugin):
|
class IneptPDFDeDRM(FileTypePlugin):
|
||||||
name = 'Inept PDF DeDRM'
|
name = 'Inept PDF DeDRM'
|
||||||
|
@ -2133,17 +2136,14 @@ class IneptPDFDeDRM(FileTypePlugin):
|
||||||
Credit given to I <3 Cabbages for the original stand-alone scripts.'
|
Credit given to I <3 Cabbages for the original stand-alone scripts.'
|
||||||
supported_platforms = ['linux', 'osx', 'windows']
|
supported_platforms = ['linux', 'osx', 'windows']
|
||||||
author = 'DiapDealer'
|
author = 'DiapDealer'
|
||||||
version = (0, 1, 3)
|
version = (0, 1, 4)
|
||||||
minimum_calibre_version = (0, 6, 44) # Compiled python libraries cannot be imported in earlier versions.
|
minimum_calibre_version = (0, 7, 55) # for the new plugin interface
|
||||||
file_types = set(['pdf'])
|
file_types = set(['pdf'])
|
||||||
on_import = True
|
on_import = True
|
||||||
|
|
||||||
def run(self, path_to_ebook):
|
def run(self, path_to_ebook):
|
||||||
global ARC4, RSA, AES
|
global ARC4, RSA, AES
|
||||||
|
|
||||||
from calibre.gui2 import is_ok_to_use_qt
|
|
||||||
from PyQt4.Qt import QMessageBox
|
|
||||||
from calibre.constants import iswindows, isosx
|
|
||||||
|
|
||||||
ARC4, RSA, AES = _load_crypto()
|
ARC4, RSA, AES = _load_crypto()
|
||||||
|
|
||||||
|
@ -2177,7 +2177,7 @@ class IneptPDFDeDRM(FileTypePlugin):
|
||||||
# Calibre's configuration directory for future use.
|
# Calibre's configuration directory for future use.
|
||||||
if iswindows or isosx:
|
if iswindows or isosx:
|
||||||
# ADE key retrieval script.
|
# ADE key retrieval script.
|
||||||
from ade_key import retrieve_key
|
from calibre_plugins.ineptpdf.ade_key import retrieve_key
|
||||||
try:
|
try:
|
||||||
keydata = retrieve_key()
|
keydata = retrieve_key()
|
||||||
userkeys.append(keydata)
|
userkeys.append(keydata)
|
Binary file not shown.
|
@ -1,374 +0,0 @@
|
||||||
#!/usr/bin/env python
|
|
||||||
|
|
||||||
from __future__ import with_statement
|
|
||||||
|
|
||||||
# engine to remove drm from Kindle for Mac and Kindle for PC books
|
|
||||||
# for personal use for archiving and converting your ebooks
|
|
||||||
|
|
||||||
# PLEASE DO NOT PIRATE EBOOKS!
|
|
||||||
|
|
||||||
# We want all authors and publishers, and eBook stores to live
|
|
||||||
# long and prosperous lives but at the same time we just want to
|
|
||||||
# be able to read OUR books on whatever device we want and to keep
|
|
||||||
# readable for a long, long time
|
|
||||||
|
|
||||||
# This borrows very heavily from works by CMBDTC, IHeartCabbages, skindle,
|
|
||||||
# unswindle, DarkReverser, ApprenticeAlf, DiapDealer, some_updates
|
|
||||||
# and many many others
|
|
||||||
|
|
||||||
# It can run standalone to convert K4M/K4PC/Mobi files, or it can be installed as a
|
|
||||||
# plugin for Calibre (http://calibre-ebook.com/about) so that importing
|
|
||||||
# K4 or Mobi with DRM is no londer a multi-step process.
|
|
||||||
#
|
|
||||||
# ***NOTE*** If you are using this script as a calibre plugin for a K4M or K4PC ebook
|
|
||||||
# then calibre must be installed on the same machine and in the same account as K4PC or K4M
|
|
||||||
# for the plugin version to function properly.
|
|
||||||
#
|
|
||||||
# To create a Calibre plugin, rename this file so that the filename
|
|
||||||
# ends in '_plugin.py', put it into a ZIP file with all its supporting python routines
|
|
||||||
# and import that ZIP into Calibre using its plugin configuration GUI.
|
|
||||||
|
|
||||||
|
|
||||||
__version__ = '2.8'
|
|
||||||
|
|
||||||
class Unbuffered:
|
|
||||||
def __init__(self, stream):
|
|
||||||
self.stream = stream
|
|
||||||
def write(self, data):
|
|
||||||
self.stream.write(data)
|
|
||||||
self.stream.flush()
|
|
||||||
def __getattr__(self, attr):
|
|
||||||
return getattr(self.stream, attr)
|
|
||||||
|
|
||||||
import sys
|
|
||||||
import os, csv, getopt
|
|
||||||
import string
|
|
||||||
import binascii
|
|
||||||
import zlib
|
|
||||||
import re
|
|
||||||
import zlib, zipfile, tempfile, shutil
|
|
||||||
from struct import pack, unpack, unpack_from
|
|
||||||
|
|
||||||
class DrmException(Exception):
|
|
||||||
pass
|
|
||||||
|
|
||||||
if 'calibre' in sys.modules:
|
|
||||||
inCalibre = True
|
|
||||||
else:
|
|
||||||
inCalibre = False
|
|
||||||
|
|
||||||
def zipUpDir(myzip, tempdir,localname):
|
|
||||||
currentdir = tempdir
|
|
||||||
if localname != "":
|
|
||||||
currentdir = os.path.join(currentdir,localname)
|
|
||||||
list = os.listdir(currentdir)
|
|
||||||
for file in list:
|
|
||||||
afilename = file
|
|
||||||
localfilePath = os.path.join(localname, afilename)
|
|
||||||
realfilePath = os.path.join(currentdir,file)
|
|
||||||
if os.path.isfile(realfilePath):
|
|
||||||
myzip.write(realfilePath, localfilePath)
|
|
||||||
elif os.path.isdir(realfilePath):
|
|
||||||
zipUpDir(myzip, tempdir, localfilePath)
|
|
||||||
|
|
||||||
# cleanup bytestring filenames
|
|
||||||
# borrowed from calibre from calibre/src/calibre/__init__.py
|
|
||||||
# added in removal of non-printing chars
|
|
||||||
# and removal of . at start
|
|
||||||
# convert spaces to underscores
|
|
||||||
def cleanup_name(name):
|
|
||||||
_filename_sanitize = re.compile(r'[\xae\0\\|\?\*<":>\+/]')
|
|
||||||
substitute='_'
|
|
||||||
one = ''.join(char for char in name if char in string.printable)
|
|
||||||
one = _filename_sanitize.sub(substitute, one)
|
|
||||||
one = re.sub(r'\s', ' ', one).strip()
|
|
||||||
one = re.sub(r'^\.+$', '_', one)
|
|
||||||
one = one.replace('..', substitute)
|
|
||||||
# Windows doesn't like path components that end with a period
|
|
||||||
if one.endswith('.'):
|
|
||||||
one = one[:-1]+substitute
|
|
||||||
# Mac and Unix don't like file names that begin with a full stop
|
|
||||||
if len(one) > 0 and one[0] == '.':
|
|
||||||
one = substitute+one[1:]
|
|
||||||
one = one.replace(' ','_')
|
|
||||||
return one
|
|
||||||
|
|
||||||
def decryptBook(infile, outdir, k4, kInfoFiles, serials, pids):
|
|
||||||
import mobidedrm
|
|
||||||
import topazextract
|
|
||||||
import kgenpids
|
|
||||||
|
|
||||||
# handle the obvious cases at the beginning
|
|
||||||
if not os.path.isfile(infile):
|
|
||||||
print "Error: Input file does not exist"
|
|
||||||
return 1
|
|
||||||
|
|
||||||
mobi = True
|
|
||||||
magic3 = file(infile,'rb').read(3)
|
|
||||||
if magic3 == 'TPZ':
|
|
||||||
mobi = False
|
|
||||||
|
|
||||||
bookname = os.path.splitext(os.path.basename(infile))[0]
|
|
||||||
|
|
||||||
if mobi:
|
|
||||||
mb = mobidedrm.MobiBook(infile)
|
|
||||||
else:
|
|
||||||
tempdir = tempfile.mkdtemp()
|
|
||||||
mb = topazextract.TopazBook(infile, tempdir)
|
|
||||||
|
|
||||||
title = mb.getBookTitle()
|
|
||||||
print "Processing Book: ", title
|
|
||||||
filenametitle = cleanup_name(title)
|
|
||||||
outfilename = bookname
|
|
||||||
if len(bookname)>4 and len(filenametitle)>4 and bookname[:4] != filenametitle[:4]:
|
|
||||||
outfilename = outfilename + "_" + filenametitle
|
|
||||||
|
|
||||||
# build pid list
|
|
||||||
md1, md2 = mb.getPIDMetaInfo()
|
|
||||||
pidlst = kgenpids.getPidList(md1, md2, k4, pids, serials, kInfoFiles)
|
|
||||||
|
|
||||||
try:
|
|
||||||
if mobi:
|
|
||||||
unlocked_file = mb.processBook(pidlst)
|
|
||||||
else:
|
|
||||||
mb.processBook(pidlst)
|
|
||||||
|
|
||||||
except mobidedrm.DrmException, e:
|
|
||||||
print "Error: " + str(e) + "\nDRM Removal Failed.\n"
|
|
||||||
return 1
|
|
||||||
except Exception, e:
|
|
||||||
if not mobi:
|
|
||||||
print "Error: " + str(e) + "\nDRM Removal Failed.\n"
|
|
||||||
print " Creating DeBug Full Zip Archive of Book"
|
|
||||||
zipname = os.path.join(outdir, bookname + '_debug' + '.zip')
|
|
||||||
myzip = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
|
||||||
zipUpDir(myzip, tempdir, '')
|
|
||||||
myzip.close()
|
|
||||||
shutil.rmtree(tempdir, True)
|
|
||||||
return 1
|
|
||||||
pass
|
|
||||||
|
|
||||||
if mobi:
|
|
||||||
outfile = os.path.join(outdir,outfilename + '_nodrm' + '.mobi')
|
|
||||||
file(outfile, 'wb').write(unlocked_file)
|
|
||||||
return 0
|
|
||||||
|
|
||||||
# topaz: build up zip archives of results
|
|
||||||
print " Creating HTML ZIP Archive"
|
|
||||||
zipname = os.path.join(outdir, outfilename + '_nodrm' + '.zip')
|
|
||||||
myzip1 = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
|
||||||
myzip1.write(os.path.join(tempdir,'book.html'),'book.html')
|
|
||||||
myzip1.write(os.path.join(tempdir,'book.opf'),'book.opf')
|
|
||||||
if os.path.isfile(os.path.join(tempdir,'cover.jpg')):
|
|
||||||
myzip1.write(os.path.join(tempdir,'cover.jpg'),'cover.jpg')
|
|
||||||
myzip1.write(os.path.join(tempdir,'style.css'),'style.css')
|
|
||||||
zipUpDir(myzip1, tempdir, 'img')
|
|
||||||
myzip1.close()
|
|
||||||
|
|
||||||
print " Creating SVG ZIP Archive"
|
|
||||||
zipname = os.path.join(outdir, outfilename + '_SVG' + '.zip')
|
|
||||||
myzip2 = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
|
||||||
myzip2.write(os.path.join(tempdir,'index_svg.xhtml'),'index_svg.xhtml')
|
|
||||||
zipUpDir(myzip2, tempdir, 'svg')
|
|
||||||
zipUpDir(myzip2, tempdir, 'img')
|
|
||||||
myzip2.close()
|
|
||||||
|
|
||||||
print " Creating XML ZIP Archive"
|
|
||||||
zipname = os.path.join(outdir, outfilename + '_XML' + '.zip')
|
|
||||||
myzip3 = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
|
||||||
targetdir = os.path.join(tempdir,'xml')
|
|
||||||
zipUpDir(myzip3, targetdir, '')
|
|
||||||
zipUpDir(myzip3, tempdir, 'img')
|
|
||||||
myzip3.close()
|
|
||||||
|
|
||||||
shutil.rmtree(tempdir, True)
|
|
||||||
return 0
|
|
||||||
|
|
||||||
|
|
||||||
def usage(progname):
|
|
||||||
print "Removes DRM protection from K4PC/M, Kindle, Mobi and Topaz ebooks"
|
|
||||||
print "Usage:"
|
|
||||||
print " %s [-k <kindle.info>] [-p <pidnums>] [-s <kindleSerialNumbers>] <infile> <outdir> " % progname
|
|
||||||
|
|
||||||
#
|
|
||||||
# Main
|
|
||||||
#
|
|
||||||
def main(argv=sys.argv):
|
|
||||||
progname = os.path.basename(argv[0])
|
|
||||||
|
|
||||||
k4 = False
|
|
||||||
kInfoFiles = []
|
|
||||||
serials = []
|
|
||||||
pids = []
|
|
||||||
|
|
||||||
print ('K4MobiDeDrm v%(__version__)s '
|
|
||||||
'provided by the work of many including DiapDealer, SomeUpdates, IHeartCabbages, CMBDTC, Skindle, DarkReverser, ApprenticeAlf, etc .' % globals())
|
|
||||||
|
|
||||||
print ' '
|
|
||||||
try:
|
|
||||||
opts, args = getopt.getopt(sys.argv[1:], "k:p:s:")
|
|
||||||
except getopt.GetoptError, err:
|
|
||||||
print str(err)
|
|
||||||
usage(progname)
|
|
||||||
sys.exit(2)
|
|
||||||
if len(args)<2:
|
|
||||||
usage(progname)
|
|
||||||
sys.exit(2)
|
|
||||||
|
|
||||||
for o, a in opts:
|
|
||||||
if o == "-k":
|
|
||||||
if a == None :
|
|
||||||
raise DrmException("Invalid parameter for -k")
|
|
||||||
kInfoFiles.append(a)
|
|
||||||
if o == "-p":
|
|
||||||
if a == None :
|
|
||||||
raise DrmException("Invalid parameter for -p")
|
|
||||||
pids = a.split(',')
|
|
||||||
if o == "-s":
|
|
||||||
if a == None :
|
|
||||||
raise DrmException("Invalid parameter for -s")
|
|
||||||
serials = a.split(',')
|
|
||||||
|
|
||||||
# try with built in Kindle Info files
|
|
||||||
k4 = True
|
|
||||||
if sys.platform.startswith('linux'):
|
|
||||||
k4 = False
|
|
||||||
kInfoFiles = None
|
|
||||||
infile = args[0]
|
|
||||||
outdir = args[1]
|
|
||||||
|
|
||||||
return decryptBook(infile, outdir, k4, kInfoFiles, serials, pids)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
sys.stdout=Unbuffered(sys.stdout)
|
|
||||||
sys.exit(main())
|
|
||||||
|
|
||||||
if not __name__ == "__main__" and inCalibre:
|
|
||||||
from calibre.customize import FileTypePlugin
|
|
||||||
|
|
||||||
class K4DeDRM(FileTypePlugin):
|
|
||||||
name = 'K4PC, K4Mac, Kindle Mobi and Topaz DeDRM' # Name of the plugin
|
|
||||||
description = 'Removes DRM from K4PC and Mac, Kindle Mobi and Topaz files. \
|
|
||||||
Provided by the work of many including DiapDealer, SomeUpdates, IHeartCabbages, CMBDTC, Skindle, DarkReverser, ApprenticeAlf, etc.'
|
|
||||||
supported_platforms = ['osx', 'windows', 'linux'] # Platforms this plugin will run on
|
|
||||||
author = 'DiapDealer, SomeUpdates' # The author of this plugin
|
|
||||||
version = (0, 2, 8) # The version number of this plugin
|
|
||||||
file_types = set(['prc','mobi','azw','azw1','tpz']) # The file types that this plugin will be applied to
|
|
||||||
on_import = True # Run this plugin during the import
|
|
||||||
priority = 210 # run this plugin before mobidedrm, k4pcdedrm, k4dedrm
|
|
||||||
|
|
||||||
def run(self, path_to_ebook):
|
|
||||||
from calibre.gui2 import is_ok_to_use_qt
|
|
||||||
from PyQt4.Qt import QMessageBox
|
|
||||||
from calibre.ptempfile import PersistentTemporaryDirectory
|
|
||||||
|
|
||||||
import kgenpids
|
|
||||||
import zlib
|
|
||||||
import zipfile
|
|
||||||
import topazextract
|
|
||||||
import mobidedrm
|
|
||||||
|
|
||||||
k4 = True
|
|
||||||
if sys.platform.startswith('linux'):
|
|
||||||
k4 = False
|
|
||||||
pids = []
|
|
||||||
serials = []
|
|
||||||
kInfoFiles = []
|
|
||||||
|
|
||||||
# Get supplied list of PIDs to try from plugin customization.
|
|
||||||
customvalues = self.site_customization.split(',')
|
|
||||||
for customvalue in customvalues:
|
|
||||||
customvalue = str(customvalue)
|
|
||||||
customvalue = customvalue.strip()
|
|
||||||
if len(customvalue) == 10 or len(customvalue) == 8:
|
|
||||||
pids.append(customvalue)
|
|
||||||
else :
|
|
||||||
if len(customvalue) == 16 and customvalue[0] == 'B':
|
|
||||||
serials.append(customvalue)
|
|
||||||
else:
|
|
||||||
print "%s is not a valid Kindle serial number or PID." % str(customvalue)
|
|
||||||
|
|
||||||
# Load any kindle info files (*.info) included Calibre's config directory.
|
|
||||||
try:
|
|
||||||
# Find Calibre's configuration directory.
|
|
||||||
confpath = os.path.split(os.path.split(self.plugin_path)[0])[0]
|
|
||||||
print 'K4MobiDeDRM: Calibre configuration directory = %s' % confpath
|
|
||||||
files = os.listdir(confpath)
|
|
||||||
filefilter = re.compile("\.info$", re.IGNORECASE)
|
|
||||||
files = filter(filefilter.search, files)
|
|
||||||
|
|
||||||
if files:
|
|
||||||
for filename in files:
|
|
||||||
fpath = os.path.join(confpath, filename)
|
|
||||||
kInfoFiles.append(fpath)
|
|
||||||
print 'K4MobiDeDRM: Kindle info file %s found in config folder.' % filename
|
|
||||||
except IOError:
|
|
||||||
print 'K4MobiDeDRM: Error reading kindle info files from config directory.'
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
mobi = True
|
|
||||||
magic3 = file(path_to_ebook,'rb').read(3)
|
|
||||||
if magic3 == 'TPZ':
|
|
||||||
mobi = False
|
|
||||||
|
|
||||||
bookname = os.path.splitext(os.path.basename(path_to_ebook))[0]
|
|
||||||
|
|
||||||
if mobi:
|
|
||||||
mb = mobidedrm.MobiBook(path_to_ebook)
|
|
||||||
else:
|
|
||||||
tempdir = PersistentTemporaryDirectory()
|
|
||||||
mb = topazextract.TopazBook(path_to_ebook, tempdir)
|
|
||||||
|
|
||||||
title = mb.getBookTitle()
|
|
||||||
md1, md2 = mb.getPIDMetaInfo()
|
|
||||||
pidlst = kgenpids.getPidList(md1, md2, k4, pids, serials, kInfoFiles)
|
|
||||||
|
|
||||||
try:
|
|
||||||
if mobi:
|
|
||||||
unlocked_file = mb.processBook(pidlst)
|
|
||||||
else:
|
|
||||||
mb.processBook(pidlst)
|
|
||||||
|
|
||||||
except mobidedrm.DrmException:
|
|
||||||
#if you reached here then no luck raise and exception
|
|
||||||
if is_ok_to_use_qt():
|
|
||||||
d = QMessageBox(QMessageBox.Warning, "K4MobiDeDRM Plugin", "Error decoding: %s\n" % path_to_ebook)
|
|
||||||
d.show()
|
|
||||||
d.raise_()
|
|
||||||
d.exec_()
|
|
||||||
raise Exception("K4MobiDeDRM plugin could not decode the file")
|
|
||||||
return ""
|
|
||||||
except topazextract.TpzDRMError:
|
|
||||||
#if you reached here then no luck raise and exception
|
|
||||||
if is_ok_to_use_qt():
|
|
||||||
d = QMessageBox(QMessageBox.Warning, "K4MobiDeDRM Plugin", "Error decoding: %s\n" % path_to_ebook)
|
|
||||||
d.show()
|
|
||||||
d.raise_()
|
|
||||||
d.exec_()
|
|
||||||
raise Exception("K4MobiDeDRM plugin could not decode the file")
|
|
||||||
return ""
|
|
||||||
|
|
||||||
print "Success!"
|
|
||||||
if mobi:
|
|
||||||
of = self.temporary_file(bookname+'.mobi')
|
|
||||||
of.write(unlocked_file)
|
|
||||||
of.close()
|
|
||||||
return of.name
|
|
||||||
|
|
||||||
# topaz: build up zip archives of results
|
|
||||||
print " Creating HTML ZIP Archive"
|
|
||||||
of = self.temporary_file(bookname + '.zip')
|
|
||||||
myzip = zipfile.ZipFile(of.name,'w',zipfile.ZIP_DEFLATED, False)
|
|
||||||
myzip.write(os.path.join(tempdir,'book.html'),'book.html')
|
|
||||||
myzip.write(os.path.join(tempdir,'book.opf'),'book.opf')
|
|
||||||
if os.path.isfile(os.path.join(tempdir,'cover.jpg')):
|
|
||||||
myzip.write(os.path.join(tempdir,'cover.jpg'),'cover.jpg')
|
|
||||||
myzip.write(os.path.join(tempdir,'style.css'),'style.css')
|
|
||||||
zipUpDir(myzip, tempdir, 'img')
|
|
||||||
myzip.close()
|
|
||||||
return of.name
|
|
||||||
|
|
||||||
def customization_help(self, gui=False):
|
|
||||||
return 'Enter 10 character PIDs and/or Kindle serial numbers, separated by commas.'
|
|
|
@ -1,10 +1,12 @@
|
||||||
# standlone set of Mac OSX specific routines needed for K4DeDRM
|
# standlone set of Mac OSX specific routines needed for K4DeDRM
|
||||||
|
|
||||||
from __future__ import with_statement
|
from __future__ import with_statement
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
|
from struct import pack, unpack, unpack_from
|
||||||
|
|
||||||
class DrmException(Exception):
|
class DrmException(Exception):
|
||||||
pass
|
pass
|
||||||
|
@ -66,9 +68,8 @@ def _load_crypto_libcrypto():
|
||||||
raise DrmException('AES decryption failed')
|
raise DrmException('AES decryption failed')
|
||||||
return out.raw
|
return out.raw
|
||||||
|
|
||||||
def keyivgen(self, passwd):
|
def keyivgen(self, passwd, salt):
|
||||||
salt = '16743'
|
saltlen = len(salt)
|
||||||
saltlen = 5
|
|
||||||
passlen = len(passwd)
|
passlen = len(passwd)
|
||||||
iter = 0x3e8
|
iter = 0x3e8
|
||||||
keylen = 80
|
keylen = 80
|
||||||
|
@ -91,12 +92,78 @@ LibCrypto = _load_crypto()
|
||||||
# Utility Routines
|
# Utility Routines
|
||||||
#
|
#
|
||||||
|
|
||||||
|
# crypto digestroutines
|
||||||
|
import hashlib
|
||||||
|
|
||||||
|
def MD5(message):
|
||||||
|
ctx = hashlib.md5()
|
||||||
|
ctx.update(message)
|
||||||
|
return ctx.digest()
|
||||||
|
|
||||||
|
def SHA1(message):
|
||||||
|
ctx = hashlib.sha1()
|
||||||
|
ctx.update(message)
|
||||||
|
return ctx.digest()
|
||||||
|
|
||||||
|
def SHA256(message):
|
||||||
|
ctx = hashlib.sha256()
|
||||||
|
ctx.update(message)
|
||||||
|
return ctx.digest()
|
||||||
|
|
||||||
# Various character maps used to decrypt books. Probably supposed to act as obfuscation
|
# Various character maps used to decrypt books. Probably supposed to act as obfuscation
|
||||||
charMap1 = "n5Pr6St7Uv8Wx9YzAb0Cd1Ef2Gh3Jk4M"
|
charMap1 = "n5Pr6St7Uv8Wx9YzAb0Cd1Ef2Gh3Jk4M"
|
||||||
charMap2 = "ZB0bYyc1xDdW2wEV3Ff7KkPpL8UuGA4gz-Tme9Nn_tHh5SvXCsIiR6rJjQaqlOoM"
|
charMap2 = "ZB0bYyc1xDdW2wEV3Ff7KkPpL8UuGA4gz-Tme9Nn_tHh5SvXCsIiR6rJjQaqlOoM"
|
||||||
charMap3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
|
||||||
charMap4 = "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789"
|
# For Future Reference from .kinf approach of K4PC
|
||||||
|
charMap5 = "AzB0bYyCeVvaZ3FfUuG4g-TtHh5SsIiR6rJjQq7KkPpL8lOoMm9Nn_c1XxDdW2wE"
|
||||||
|
|
||||||
|
|
||||||
|
def encode(data, map):
|
||||||
|
result = ""
|
||||||
|
for char in data:
|
||||||
|
value = ord(char)
|
||||||
|
Q = (value ^ 0x80) // len(map)
|
||||||
|
R = value % len(map)
|
||||||
|
result += map[Q]
|
||||||
|
result += map[R]
|
||||||
|
return result
|
||||||
|
|
||||||
|
# Hash the bytes in data and then encode the digest with the characters in map
|
||||||
|
def encodeHash(data,map):
|
||||||
|
return encode(MD5(data),map)
|
||||||
|
|
||||||
|
# Decode the string in data with the characters in map. Returns the decoded bytes
|
||||||
|
def decode(data,map):
|
||||||
|
result = ""
|
||||||
|
for i in range (0,len(data)-1,2):
|
||||||
|
high = map.find(data[i])
|
||||||
|
low = map.find(data[i+1])
|
||||||
|
if (high == -1) or (low == -1) :
|
||||||
|
break
|
||||||
|
value = (((high * len(map)) ^ 0x80) & 0xFF) + low
|
||||||
|
result += pack("B",value)
|
||||||
|
return result
|
||||||
|
|
||||||
|
# For Future Reference from .kinf approach of K4PC
|
||||||
|
# generate table of prime number less than or equal to int n
|
||||||
|
def primes(n):
|
||||||
|
if n==2: return [2]
|
||||||
|
elif n<2: return []
|
||||||
|
s=range(3,n+1,2)
|
||||||
|
mroot = n ** 0.5
|
||||||
|
half=(n+1)/2-1
|
||||||
|
i=0
|
||||||
|
m=3
|
||||||
|
while m <= mroot:
|
||||||
|
if s[i]:
|
||||||
|
j=(m*m-3)/2
|
||||||
|
s[j]=0
|
||||||
|
while j<half:
|
||||||
|
s[j]=0
|
||||||
|
j+=m
|
||||||
|
i=i+1
|
||||||
|
m=2*i+3
|
||||||
|
return [2]+[x for x in s if x]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -137,30 +204,12 @@ def GetUserName():
|
||||||
username = os.getenv('USER')
|
username = os.getenv('USER')
|
||||||
return username
|
return username
|
||||||
|
|
||||||
|
|
||||||
def encode(data, map):
|
|
||||||
result = ""
|
|
||||||
for char in data:
|
|
||||||
value = ord(char)
|
|
||||||
Q = (value ^ 0x80) // len(map)
|
|
||||||
R = value % len(map)
|
|
||||||
result += map[Q]
|
|
||||||
result += map[R]
|
|
||||||
return result
|
|
||||||
|
|
||||||
import hashlib
|
|
||||||
|
|
||||||
def SHA256(message):
|
|
||||||
ctx = hashlib.sha256()
|
|
||||||
ctx.update(message)
|
|
||||||
return ctx.digest()
|
|
||||||
|
|
||||||
# implements an Pseudo Mac Version of Windows built-in Crypto routine
|
# implements an Pseudo Mac Version of Windows built-in Crypto routine
|
||||||
def CryptUnprotectData(encryptedData):
|
def CryptUnprotectData(encryptedData, salt):
|
||||||
sp = GetVolumeSerialNumber() + '!@#' + GetUserName()
|
sp = GetVolumeSerialNumber() + '!@#' + GetUserName()
|
||||||
passwdData = encode(SHA256(sp),charMap1)
|
passwdData = encode(SHA256(sp),charMap1)
|
||||||
crp = LibCrypto()
|
crp = LibCrypto()
|
||||||
key_iv = crp.keyivgen(passwdData)
|
key_iv = crp.keyivgen(passwdData, salt)
|
||||||
key = key_iv[0:32]
|
key = key_iv[0:32]
|
||||||
iv = key_iv[32:48]
|
iv = key_iv[32:48]
|
||||||
crp.set_decrypt_key(key,iv)
|
crp.set_decrypt_key(key,iv)
|
||||||
|
@ -170,6 +219,7 @@ def CryptUnprotectData(encryptedData):
|
||||||
|
|
||||||
# Locate the .kindle-info files
|
# Locate the .kindle-info files
|
||||||
def getKindleInfoFiles(kInfoFiles):
|
def getKindleInfoFiles(kInfoFiles):
|
||||||
|
# first search for current .kindle-info files
|
||||||
home = os.getenv('HOME')
|
home = os.getenv('HOME')
|
||||||
cmdline = 'find "' + home + '/Library/Application Support" -name ".kindle-info"'
|
cmdline = 'find "' + home + '/Library/Application Support" -name ".kindle-info"'
|
||||||
cmdline = cmdline.encode(sys.getfilesystemencoding())
|
cmdline = cmdline.encode(sys.getfilesystemencoding())
|
||||||
|
@ -178,23 +228,130 @@ def getKindleInfoFiles(kInfoFiles):
|
||||||
reslst = out1.split('\n')
|
reslst = out1.split('\n')
|
||||||
kinfopath = 'NONE'
|
kinfopath = 'NONE'
|
||||||
found = False
|
found = False
|
||||||
cnt = len(reslst)
|
|
||||||
for resline in reslst:
|
for resline in reslst:
|
||||||
if os.path.isfile(resline):
|
if os.path.isfile(resline):
|
||||||
kInfoFiles.append(resline)
|
kInfoFiles.append(resline)
|
||||||
found = True
|
found = True
|
||||||
|
# For Future Reference
|
||||||
|
#
|
||||||
|
# # add any .kinf files
|
||||||
|
# cmdline = 'find "' + home + '/Library/Application Support" -name "rainier*.kinf"'
|
||||||
|
# cmdline = cmdline.encode(sys.getfilesystemencoding())
|
||||||
|
# p1 = subprocess.Popen(cmdline, shell=True, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=False)
|
||||||
|
# out1, out2 = p1.communicate()
|
||||||
|
# reslst = out1.split('\n')
|
||||||
|
# for resline in reslst:
|
||||||
|
# if os.path.isfile(resline):
|
||||||
|
# kInfoFiles.append(resline)
|
||||||
|
# found = True
|
||||||
if not found:
|
if not found:
|
||||||
print('No .kindle-info files have been found.')
|
print('No kindle-info files have been found.')
|
||||||
return kInfoFiles
|
return kInfoFiles
|
||||||
|
|
||||||
# Parse the Kindle.info file and return the records as a list of key-values
|
# determine type of kindle info provided and return a
|
||||||
def parseKindleInfo(kInfoFile):
|
# database of keynames and values
|
||||||
|
def getDBfromFile(kInfoFile):
|
||||||
|
names = ["kindle.account.tokens","kindle.cookie.item","eulaVersionAccepted","login_date","kindle.token.item","login","kindle.key.item","kindle.name.info","kindle.device.info", "MazamaRandomNumber"]
|
||||||
DB = {}
|
DB = {}
|
||||||
|
cnt = 0
|
||||||
infoReader = open(kInfoFile, 'r')
|
infoReader = open(kInfoFile, 'r')
|
||||||
infoReader.read(1)
|
hdr = infoReader.read(1)
|
||||||
data = infoReader.read()
|
data = infoReader.read()
|
||||||
items = data.split('[')
|
|
||||||
for item in items:
|
if data.find('[') != -1 :
|
||||||
splito = item.split(':')
|
|
||||||
DB[splito[0]] =splito[1]
|
# older style kindle-info file
|
||||||
|
items = data.split('[')
|
||||||
|
for item in items:
|
||||||
|
if item != '':
|
||||||
|
keyhash, rawdata = item.split(':')
|
||||||
|
keyname = "unknown"
|
||||||
|
for name in names:
|
||||||
|
if encodeHash(name,charMap2) == keyhash:
|
||||||
|
keyname = name
|
||||||
|
break
|
||||||
|
if keyname == "unknown":
|
||||||
|
keyname = keyhash
|
||||||
|
encryptedValue = decode(rawdata,charMap2)
|
||||||
|
salt = '16743'
|
||||||
|
cleartext = CryptUnprotectData(encryptedValue, salt)
|
||||||
|
DB[keyname] = decode(cleartext,charMap1)
|
||||||
|
cnt = cnt + 1
|
||||||
|
if cnt == 0:
|
||||||
|
DB = None
|
||||||
|
return DB
|
||||||
|
|
||||||
|
# For Future Reference taken from K4PC 1.5.0 .kinf
|
||||||
|
#
|
||||||
|
# # else newer style .kinf file
|
||||||
|
# # the .kinf file uses "/" to separate it into records
|
||||||
|
# # so remove the trailing "/" to make it easy to use split
|
||||||
|
# data = data[:-1]
|
||||||
|
# items = data.split('/')
|
||||||
|
#
|
||||||
|
# # loop through the item records until all are processed
|
||||||
|
# while len(items) > 0:
|
||||||
|
#
|
||||||
|
# # get the first item record
|
||||||
|
# item = items.pop(0)
|
||||||
|
#
|
||||||
|
# # the first 32 chars of the first record of a group
|
||||||
|
# # is the MD5 hash of the key name encoded by charMap5
|
||||||
|
# keyhash = item[0:32]
|
||||||
|
#
|
||||||
|
# # the raw keyhash string is also used to create entropy for the actual
|
||||||
|
# # CryptProtectData Blob that represents that keys contents
|
||||||
|
# entropy = SHA1(keyhash)
|
||||||
|
#
|
||||||
|
# # the remainder of the first record when decoded with charMap5
|
||||||
|
# # has the ':' split char followed by the string representation
|
||||||
|
# # of the number of records that follow
|
||||||
|
# # and make up the contents
|
||||||
|
# srcnt = decode(item[34:],charMap5)
|
||||||
|
# rcnt = int(srcnt)
|
||||||
|
#
|
||||||
|
# # read and store in rcnt records of data
|
||||||
|
# # that make up the contents value
|
||||||
|
# edlst = []
|
||||||
|
# for i in xrange(rcnt):
|
||||||
|
# item = items.pop(0)
|
||||||
|
# edlst.append(item)
|
||||||
|
#
|
||||||
|
# keyname = "unknown"
|
||||||
|
# for name in names:
|
||||||
|
# if encodeHash(name,charMap5) == keyhash:
|
||||||
|
# keyname = name
|
||||||
|
# break
|
||||||
|
# if keyname == "unknown":
|
||||||
|
# keyname = keyhash
|
||||||
|
#
|
||||||
|
# # the charMap5 encoded contents data has had a length
|
||||||
|
# # of chars (always odd) cut off of the front and moved
|
||||||
|
# # to the end to prevent decoding using charMap5 from
|
||||||
|
# # working properly, and thereby preventing the ensuing
|
||||||
|
# # CryptUnprotectData call from succeeding.
|
||||||
|
#
|
||||||
|
# # The offset into the charMap5 encoded contents seems to be:
|
||||||
|
# # len(contents) - largest prime number less than or equal to int(len(content)/3)
|
||||||
|
# # (in other words split "about" 2/3rds of the way through)
|
||||||
|
#
|
||||||
|
# # move first offsets chars to end to align for decode by charMap5
|
||||||
|
# encdata = "".join(edlst)
|
||||||
|
# contlen = len(encdata)
|
||||||
|
# noffset = contlen - primes(int(contlen/3))[-1]
|
||||||
|
#
|
||||||
|
# # now properly split and recombine
|
||||||
|
# # by moving noffset chars from the start of the
|
||||||
|
# # string to the end of the string
|
||||||
|
# pfx = encdata[0:noffset]
|
||||||
|
# encdata = encdata[noffset:]
|
||||||
|
# encdata = encdata + pfx
|
||||||
|
#
|
||||||
|
# # decode using Map5 to get the CryptProtect Data
|
||||||
|
# encryptedValue = decode(encdata,charMap5)
|
||||||
|
# DB[keyname] = CryptUnprotectData(encryptedValue, entropy, 1)
|
||||||
|
# cnt = cnt + 1
|
||||||
|
|
||||||
|
if cnt == 0:
|
||||||
|
DB = None
|
||||||
return DB
|
return DB
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
# K4PC Windows specific routines
|
# K4PC Windows specific routines
|
||||||
|
|
||||||
from __future__ import with_statement
|
from __future__ import with_statement
|
||||||
|
|
||||||
import sys, os
|
import sys, os
|
||||||
|
from struct import pack, unpack, unpack_from
|
||||||
|
|
||||||
from ctypes import windll, c_char_p, c_wchar_p, c_uint, POINTER, byref, \
|
from ctypes import windll, c_char_p, c_wchar_p, c_uint, POINTER, byref, \
|
||||||
create_unicode_buffer, create_string_buffer, CFUNCTYPE, addressof, \
|
create_unicode_buffer, create_string_buffer, CFUNCTYPE, addressof, \
|
||||||
|
@ -10,25 +12,86 @@ from ctypes import windll, c_char_p, c_wchar_p, c_uint, POINTER, byref, \
|
||||||
|
|
||||||
import _winreg as winreg
|
import _winreg as winreg
|
||||||
|
|
||||||
import traceback
|
|
||||||
|
|
||||||
MAX_PATH = 255
|
MAX_PATH = 255
|
||||||
|
|
||||||
kernel32 = windll.kernel32
|
kernel32 = windll.kernel32
|
||||||
advapi32 = windll.advapi32
|
advapi32 = windll.advapi32
|
||||||
crypt32 = windll.crypt32
|
crypt32 = windll.crypt32
|
||||||
|
|
||||||
|
import traceback
|
||||||
|
|
||||||
# Various character maps used to decrypt books. Probably supposed to act as obfuscation
|
# crypto digestroutines
|
||||||
charMap1 = "n5Pr6St7Uv8Wx9YzAb0Cd1Ef2Gh3Jk4M"
|
import hashlib
|
||||||
|
|
||||||
|
def MD5(message):
|
||||||
|
ctx = hashlib.md5()
|
||||||
|
ctx.update(message)
|
||||||
|
return ctx.digest()
|
||||||
|
|
||||||
|
def SHA1(message):
|
||||||
|
ctx = hashlib.sha1()
|
||||||
|
ctx.update(message)
|
||||||
|
return ctx.digest()
|
||||||
|
|
||||||
|
|
||||||
|
# simple primes table (<= n) calculator
|
||||||
|
def primes(n):
|
||||||
|
if n==2: return [2]
|
||||||
|
elif n<2: return []
|
||||||
|
s=range(3,n+1,2)
|
||||||
|
mroot = n ** 0.5
|
||||||
|
half=(n+1)/2-1
|
||||||
|
i=0
|
||||||
|
m=3
|
||||||
|
while m <= mroot:
|
||||||
|
if s[i]:
|
||||||
|
j=(m*m-3)/2
|
||||||
|
s[j]=0
|
||||||
|
while j<half:
|
||||||
|
s[j]=0
|
||||||
|
j+=m
|
||||||
|
i=i+1
|
||||||
|
m=2*i+3
|
||||||
|
return [2]+[x for x in s if x]
|
||||||
|
|
||||||
|
|
||||||
|
# Various character maps used to decrypt kindle info values.
|
||||||
|
# Probably supposed to act as obfuscation
|
||||||
charMap2 = "AaZzB0bYyCc1XxDdW2wEeVv3FfUuG4g-TtHh5SsIiR6rJjQq7KkPpL8lOoMm9Nn_"
|
charMap2 = "AaZzB0bYyCc1XxDdW2wEeVv3FfUuG4g-TtHh5SsIiR6rJjQq7KkPpL8lOoMm9Nn_"
|
||||||
charMap3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
charMap5 = "AzB0bYyCeVvaZ3FfUuG4g-TtHh5SsIiR6rJjQq7KkPpL8lOoMm9Nn_c1XxDdW2wE"
|
||||||
charMap4 = "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789"
|
|
||||||
|
|
||||||
class DrmException(Exception):
|
class DrmException(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
# Encode the bytes in data with the characters in map
|
||||||
|
def encode(data, map):
|
||||||
|
result = ""
|
||||||
|
for char in data:
|
||||||
|
value = ord(char)
|
||||||
|
Q = (value ^ 0x80) // len(map)
|
||||||
|
R = value % len(map)
|
||||||
|
result += map[Q]
|
||||||
|
result += map[R]
|
||||||
|
return result
|
||||||
|
|
||||||
|
# Hash the bytes in data and then encode the digest with the characters in map
|
||||||
|
def encodeHash(data,map):
|
||||||
|
return encode(MD5(data),map)
|
||||||
|
|
||||||
|
# Decode the string in data with the characters in map. Returns the decoded bytes
|
||||||
|
def decode(data,map):
|
||||||
|
result = ""
|
||||||
|
for i in range (0,len(data)-1,2):
|
||||||
|
high = map.find(data[i])
|
||||||
|
low = map.find(data[i+1])
|
||||||
|
if (high == -1) or (low == -1) :
|
||||||
|
break
|
||||||
|
value = (((high * len(map)) ^ 0x80) & 0xFF) + low
|
||||||
|
result += pack("B",value)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
# interface with Windows OS Routines
|
||||||
class DataBlob(Structure):
|
class DataBlob(Structure):
|
||||||
_fields_ = [('cbData', c_uint),
|
_fields_ = [('cbData', c_uint),
|
||||||
('pbData', c_void_p)]
|
('pbData', c_void_p)]
|
||||||
|
@ -59,59 +122,175 @@ def GetVolumeSerialNumber():
|
||||||
return GetVolumeSerialNumber
|
return GetVolumeSerialNumber
|
||||||
GetVolumeSerialNumber = GetVolumeSerialNumber()
|
GetVolumeSerialNumber = GetVolumeSerialNumber()
|
||||||
|
|
||||||
|
def getLastError():
|
||||||
|
GetLastError = kernel32.GetLastError
|
||||||
|
GetLastError.argtypes = None
|
||||||
|
GetLastError.restype = c_uint
|
||||||
|
def getLastError():
|
||||||
|
return GetLastError()
|
||||||
|
return getLastError
|
||||||
|
getLastError = getLastError()
|
||||||
|
|
||||||
def GetUserName():
|
def GetUserName():
|
||||||
GetUserNameW = advapi32.GetUserNameW
|
GetUserNameW = advapi32.GetUserNameW
|
||||||
GetUserNameW.argtypes = [c_wchar_p, POINTER(c_uint)]
|
GetUserNameW.argtypes = [c_wchar_p, POINTER(c_uint)]
|
||||||
GetUserNameW.restype = c_uint
|
GetUserNameW.restype = c_uint
|
||||||
def GetUserName():
|
def GetUserName():
|
||||||
buffer = create_unicode_buffer(32)
|
buffer = create_unicode_buffer(2)
|
||||||
size = c_uint(len(buffer))
|
size = c_uint(len(buffer))
|
||||||
while not GetUserNameW(buffer, byref(size)):
|
while not GetUserNameW(buffer, byref(size)):
|
||||||
|
errcd = getLastError()
|
||||||
|
if errcd == 234:
|
||||||
|
# bad wine implementation up through wine 1.3.21
|
||||||
|
return "AlternateUserName"
|
||||||
buffer = create_unicode_buffer(len(buffer) * 2)
|
buffer = create_unicode_buffer(len(buffer) * 2)
|
||||||
size.value = len(buffer)
|
size.value = len(buffer)
|
||||||
return buffer.value.encode('utf-16-le')[::2]
|
return buffer.value.encode('utf-16-le')[::2]
|
||||||
return GetUserName
|
return GetUserName
|
||||||
GetUserName = GetUserName()
|
GetUserName = GetUserName()
|
||||||
|
|
||||||
|
|
||||||
def CryptUnprotectData():
|
def CryptUnprotectData():
|
||||||
_CryptUnprotectData = crypt32.CryptUnprotectData
|
_CryptUnprotectData = crypt32.CryptUnprotectData
|
||||||
_CryptUnprotectData.argtypes = [DataBlob_p, c_wchar_p, DataBlob_p,
|
_CryptUnprotectData.argtypes = [DataBlob_p, c_wchar_p, DataBlob_p,
|
||||||
c_void_p, c_void_p, c_uint, DataBlob_p]
|
c_void_p, c_void_p, c_uint, DataBlob_p]
|
||||||
_CryptUnprotectData.restype = c_uint
|
_CryptUnprotectData.restype = c_uint
|
||||||
def CryptUnprotectData(indata, entropy):
|
def CryptUnprotectData(indata, entropy, flags):
|
||||||
indatab = create_string_buffer(indata)
|
indatab = create_string_buffer(indata)
|
||||||
indata = DataBlob(len(indata), cast(indatab, c_void_p))
|
indata = DataBlob(len(indata), cast(indatab, c_void_p))
|
||||||
entropyb = create_string_buffer(entropy)
|
entropyb = create_string_buffer(entropy)
|
||||||
entropy = DataBlob(len(entropy), cast(entropyb, c_void_p))
|
entropy = DataBlob(len(entropy), cast(entropyb, c_void_p))
|
||||||
outdata = DataBlob()
|
outdata = DataBlob()
|
||||||
if not _CryptUnprotectData(byref(indata), None, byref(entropy),
|
if not _CryptUnprotectData(byref(indata), None, byref(entropy),
|
||||||
None, None, 0, byref(outdata)):
|
None, None, flags, byref(outdata)):
|
||||||
raise DrmException("Failed to Unprotect Data")
|
raise DrmException("Failed to Unprotect Data")
|
||||||
return string_at(outdata.pbData, outdata.cbData)
|
return string_at(outdata.pbData, outdata.cbData)
|
||||||
return CryptUnprotectData
|
return CryptUnprotectData
|
||||||
CryptUnprotectData = CryptUnprotectData()
|
CryptUnprotectData = CryptUnprotectData()
|
||||||
|
|
||||||
# Locate the .kindle-info files
|
|
||||||
|
# Locate all of the kindle-info style files and return as list
|
||||||
def getKindleInfoFiles(kInfoFiles):
|
def getKindleInfoFiles(kInfoFiles):
|
||||||
regkey = winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\\")
|
regkey = winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\\")
|
||||||
path = winreg.QueryValueEx(regkey, 'Local AppData')[0]
|
path = winreg.QueryValueEx(regkey, 'Local AppData')[0]
|
||||||
|
|
||||||
|
# first look for older kindle-info files
|
||||||
kinfopath = path +'\\Amazon\\Kindle For PC\\{AMAwzsaPaaZAzmZzZQzgZCAkZ3AjA_AY}\\kindle.info'
|
kinfopath = path +'\\Amazon\\Kindle For PC\\{AMAwzsaPaaZAzmZzZQzgZCAkZ3AjA_AY}\\kindle.info'
|
||||||
if not os.path.isfile(kinfopath):
|
if not os.path.isfile(kinfopath):
|
||||||
print('The kindle.info files has not been found.')
|
print('No kindle.info files have not been found.')
|
||||||
|
else:
|
||||||
|
kInfoFiles.append(kinfopath)
|
||||||
|
|
||||||
|
# now look for newer (K4PC 1.5.0 and later rainier.2.1.1.kinf file
|
||||||
|
kinfopath = path +'\\Amazon\\Kindle For PC\\storage\\rainier.2.1.1.kinf'
|
||||||
|
if not os.path.isfile(kinfopath):
|
||||||
|
print('No .kinf files have not been found.')
|
||||||
else:
|
else:
|
||||||
kInfoFiles.append(kinfopath)
|
kInfoFiles.append(kinfopath)
|
||||||
return kInfoFiles
|
return kInfoFiles
|
||||||
|
|
||||||
# Parse the Kindle.info file and return the records as a list of key-values
|
|
||||||
def parseKindleInfo(kInfoFile):
|
# determine type of kindle info provided and return a
|
||||||
|
# database of keynames and values
|
||||||
|
def getDBfromFile(kInfoFile):
|
||||||
|
names = ["kindle.account.tokens","kindle.cookie.item","eulaVersionAccepted","login_date","kindle.token.item","login","kindle.key.item","kindle.name.info","kindle.device.info", "MazamaRandomNumber"]
|
||||||
DB = {}
|
DB = {}
|
||||||
|
cnt = 0
|
||||||
infoReader = open(kInfoFile, 'r')
|
infoReader = open(kInfoFile, 'r')
|
||||||
infoReader.read(1)
|
hdr = infoReader.read(1)
|
||||||
data = infoReader.read()
|
data = infoReader.read()
|
||||||
items = data.split('{')
|
|
||||||
for item in items:
|
if data.find('{') != -1 :
|
||||||
splito = item.split(':')
|
|
||||||
DB[splito[0]] =splito[1]
|
# older style kindle-info file
|
||||||
|
items = data.split('{')
|
||||||
|
for item in items:
|
||||||
|
if item != '':
|
||||||
|
keyhash, rawdata = item.split(':')
|
||||||
|
keyname = "unknown"
|
||||||
|
for name in names:
|
||||||
|
if encodeHash(name,charMap2) == keyhash:
|
||||||
|
keyname = name
|
||||||
|
break
|
||||||
|
if keyname == "unknown":
|
||||||
|
keyname = keyhash
|
||||||
|
encryptedValue = decode(rawdata,charMap2)
|
||||||
|
DB[keyname] = CryptUnprotectData(encryptedValue, "", 0)
|
||||||
|
cnt = cnt + 1
|
||||||
|
if cnt == 0:
|
||||||
|
DB = None
|
||||||
|
return DB
|
||||||
|
|
||||||
|
# else newer style .kinf file
|
||||||
|
# the .kinf file uses "/" to separate it into records
|
||||||
|
# so remove the trailing "/" to make it easy to use split
|
||||||
|
data = data[:-1]
|
||||||
|
items = data.split('/')
|
||||||
|
|
||||||
|
# loop through the item records until all are processed
|
||||||
|
while len(items) > 0:
|
||||||
|
|
||||||
|
# get the first item record
|
||||||
|
item = items.pop(0)
|
||||||
|
|
||||||
|
# the first 32 chars of the first record of a group
|
||||||
|
# is the MD5 hash of the key name encoded by charMap5
|
||||||
|
keyhash = item[0:32]
|
||||||
|
|
||||||
|
# the raw keyhash string is also used to create entropy for the actual
|
||||||
|
# CryptProtectData Blob that represents that keys contents
|
||||||
|
entropy = SHA1(keyhash)
|
||||||
|
|
||||||
|
# the remainder of the first record when decoded with charMap5
|
||||||
|
# has the ':' split char followed by the string representation
|
||||||
|
# of the number of records that follow
|
||||||
|
# and make up the contents
|
||||||
|
srcnt = decode(item[34:],charMap5)
|
||||||
|
rcnt = int(srcnt)
|
||||||
|
|
||||||
|
# read and store in rcnt records of data
|
||||||
|
# that make up the contents value
|
||||||
|
edlst = []
|
||||||
|
for i in xrange(rcnt):
|
||||||
|
item = items.pop(0)
|
||||||
|
edlst.append(item)
|
||||||
|
|
||||||
|
keyname = "unknown"
|
||||||
|
for name in names:
|
||||||
|
if encodeHash(name,charMap5) == keyhash:
|
||||||
|
keyname = name
|
||||||
|
break
|
||||||
|
if keyname == "unknown":
|
||||||
|
keyname = keyhash
|
||||||
|
|
||||||
|
# the charMap5 encoded contents data has had a length
|
||||||
|
# of chars (always odd) cut off of the front and moved
|
||||||
|
# to the end to prevent decoding using charMap5 from
|
||||||
|
# working properly, and thereby preventing the ensuing
|
||||||
|
# CryptUnprotectData call from succeeding.
|
||||||
|
|
||||||
|
# The offset into the charMap5 encoded contents seems to be:
|
||||||
|
# len(contents) - largest prime number less than or equal to int(len(content)/3)
|
||||||
|
# (in other words split "about" 2/3rds of the way through)
|
||||||
|
|
||||||
|
# move first offsets chars to end to align for decode by charMap5
|
||||||
|
encdata = "".join(edlst)
|
||||||
|
contlen = len(encdata)
|
||||||
|
noffset = contlen - primes(int(contlen/3))[-1]
|
||||||
|
|
||||||
|
# now properly split and recombine
|
||||||
|
# by moving noffset chars from the start of the
|
||||||
|
# string to the end of the string
|
||||||
|
pfx = encdata[0:noffset]
|
||||||
|
encdata = encdata[noffset:]
|
||||||
|
encdata = encdata + pfx
|
||||||
|
|
||||||
|
# decode using Map5 to get the CryptProtect Data
|
||||||
|
encryptedValue = decode(encdata,charMap5)
|
||||||
|
DB[keyname] = CryptUnprotectData(encryptedValue, entropy, 1)
|
||||||
|
cnt = cnt + 1
|
||||||
|
|
||||||
|
if cnt == 0:
|
||||||
|
DB = None
|
||||||
return DB
|
return DB
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -51,8 +51,9 @@
|
||||||
# 0.29 - It seems that the ideas about when multibyte trailing characters were
|
# 0.29 - It seems that the ideas about when multibyte trailing characters were
|
||||||
# included in the encryption were wrong. They aren't for DOC compressed
|
# included in the encryption were wrong. They aren't for DOC compressed
|
||||||
# files, but they are for HUFF/CDIC compress files!
|
# files, but they are for HUFF/CDIC compress files!
|
||||||
|
# 0.30 - Modified interface slightly to work better with new calibre plugin style
|
||||||
|
|
||||||
__version__ = '0.29'
|
__version__ = '0.30'
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
@ -163,6 +164,7 @@ class MobiBook:
|
||||||
def __init__(self, infile):
|
def __init__(self, infile):
|
||||||
# initial sanity check on file
|
# initial sanity check on file
|
||||||
self.data_file = file(infile, 'rb').read()
|
self.data_file = file(infile, 'rb').read()
|
||||||
|
self.mobi_data = ''
|
||||||
self.header = self.data_file[0:78]
|
self.header = self.data_file[0:78]
|
||||||
if self.header[0x3C:0x3C+8] != 'BOOKMOBI' and self.header[0x3C:0x3C+8] != 'TEXtREAd':
|
if self.header[0x3C:0x3C+8] != 'BOOKMOBI' and self.header[0x3C:0x3C+8] != 'TEXtREAd':
|
||||||
raise DrmException("invalid file format")
|
raise DrmException("invalid file format")
|
||||||
|
@ -301,13 +303,17 @@ class MobiBook:
|
||||||
break
|
break
|
||||||
return [found_key,pid]
|
return [found_key,pid]
|
||||||
|
|
||||||
|
def getMobiFile(self, outpath):
|
||||||
|
file(outpath,'wb').write(self.mobi_data)
|
||||||
|
|
||||||
def processBook(self, pidlist):
|
def processBook(self, pidlist):
|
||||||
crypto_type, = struct.unpack('>H', self.sect[0xC:0xC+2])
|
crypto_type, = struct.unpack('>H', self.sect[0xC:0xC+2])
|
||||||
print 'Crypto Type is: ', crypto_type
|
print 'Crypto Type is: ', crypto_type
|
||||||
self.crypto_type = crypto_type
|
self.crypto_type = crypto_type
|
||||||
if crypto_type == 0:
|
if crypto_type == 0:
|
||||||
print "This book is not encrypted."
|
print "This book is not encrypted."
|
||||||
return self.data_file
|
self.mobi_data = self.data_file
|
||||||
|
return
|
||||||
if crypto_type != 2 and crypto_type != 1:
|
if crypto_type != 2 and crypto_type != 1:
|
||||||
raise DrmException("Cannot decode unknown Mobipocket encryption type %d" % crypto_type)
|
raise DrmException("Cannot decode unknown Mobipocket encryption type %d" % crypto_type)
|
||||||
|
|
||||||
|
@ -353,33 +359,35 @@ class MobiBook:
|
||||||
|
|
||||||
# decrypt sections
|
# decrypt sections
|
||||||
print "Decrypting. Please wait . . .",
|
print "Decrypting. Please wait . . .",
|
||||||
new_data = self.data_file[:self.sections[1][0]]
|
self.mobi_data = self.data_file[:self.sections[1][0]]
|
||||||
for i in xrange(1, self.records+1):
|
for i in xrange(1, self.records+1):
|
||||||
data = self.loadSection(i)
|
data = self.loadSection(i)
|
||||||
extra_size = getSizeOfTrailingDataEntries(data, len(data), self.extra_data_flags)
|
extra_size = getSizeOfTrailingDataEntries(data, len(data), self.extra_data_flags)
|
||||||
if i%100 == 0:
|
if i%100 == 0:
|
||||||
print ".",
|
print ".",
|
||||||
# print "record %d, extra_size %d" %(i,extra_size)
|
# print "record %d, extra_size %d" %(i,extra_size)
|
||||||
new_data += PC1(found_key, data[0:len(data) - extra_size])
|
self.mobi_data += PC1(found_key, data[0:len(data) - extra_size])
|
||||||
if extra_size > 0:
|
if extra_size > 0:
|
||||||
new_data += data[-extra_size:]
|
self.mobi_data += data[-extra_size:]
|
||||||
if self.num_sections > self.records+1:
|
if self.num_sections > self.records+1:
|
||||||
new_data += self.data_file[self.sections[self.records+1][0]:]
|
self.mobi_data += self.data_file[self.sections[self.records+1][0]:]
|
||||||
self.data_file = new_data
|
|
||||||
print "done"
|
print "done"
|
||||||
return self.data_file
|
return
|
||||||
|
|
||||||
def getUnencryptedBook(infile,pid):
|
def getUnencryptedBook(infile,pid):
|
||||||
if not os.path.isfile(infile):
|
if not os.path.isfile(infile):
|
||||||
raise DrmException('Input File Not Found')
|
raise DrmException('Input File Not Found')
|
||||||
book = MobiBook(infile)
|
book = MobiBook(infile)
|
||||||
return book.processBook([pid])
|
book.processBook([pid])
|
||||||
|
return book.mobi_data
|
||||||
|
|
||||||
def getUnencryptedBookWithList(infile,pidlist):
|
def getUnencryptedBookWithList(infile,pidlist):
|
||||||
if not os.path.isfile(infile):
|
if not os.path.isfile(infile):
|
||||||
raise DrmException('Input File Not Found')
|
raise DrmException('Input File Not Found')
|
||||||
book = MobiBook(infile)
|
book = MobiBook(infile)
|
||||||
return book.processBook(pidlist)
|
book.processBook(pidlist)
|
||||||
|
return book.mobi_data
|
||||||
|
|
||||||
|
|
||||||
def main(argv=sys.argv):
|
def main(argv=sys.argv):
|
||||||
print ('MobiDeDrm v%(__version__)s. '
|
print ('MobiDeDrm v%(__version__)s. '
|
||||||
|
|
Binary file not shown.
|
@ -24,7 +24,7 @@
|
||||||
<key>CFBundleExecutable</key>
|
<key>CFBundleExecutable</key>
|
||||||
<string>droplet</string>
|
<string>droplet</string>
|
||||||
<key>CFBundleGetInfoString</key>
|
<key>CFBundleGetInfoString</key>
|
||||||
<string>DeDRM 2.6, Written 2010–2011 by Apprentice Alf and others.</string>
|
<string>DeDRM 2.7, Written 2010–2011 by Apprentice Alf and others.</string>
|
||||||
<key>CFBundleIconFile</key>
|
<key>CFBundleIconFile</key>
|
||||||
<string>droplet</string>
|
<string>droplet</string>
|
||||||
<key>CFBundleInfoDictionaryVersion</key>
|
<key>CFBundleInfoDictionaryVersion</key>
|
||||||
|
@ -34,7 +34,7 @@
|
||||||
<key>CFBundlePackageType</key>
|
<key>CFBundlePackageType</key>
|
||||||
<string>APPL</string>
|
<string>APPL</string>
|
||||||
<key>CFBundleShortVersionString</key>
|
<key>CFBundleShortVersionString</key>
|
||||||
<string>2.6</string>
|
<string>2.7</string>
|
||||||
<key>CFBundleSignature</key>
|
<key>CFBundleSignature</key>
|
||||||
<string>dplt</string>
|
<string>dplt</string>
|
||||||
<key>LSMinimumSystemVersion</key>
|
<key>LSMinimumSystemVersion</key>
|
||||||
|
|
Binary file not shown.
|
@ -58,8 +58,9 @@
|
||||||
# 0.17 - added support for pycrypto's DES as well
|
# 0.17 - added support for pycrypto's DES as well
|
||||||
# 0.18 - on Windows try PyCrypto first and OpenSSL next
|
# 0.18 - on Windows try PyCrypto first and OpenSSL next
|
||||||
# 0.19 - Modify the interface to allow use of import
|
# 0.19 - Modify the interface to allow use of import
|
||||||
|
# 0.20 - modify to allow use inside new interface for calibre plugins
|
||||||
|
|
||||||
__version__='0.19'
|
__version__='0.20'
|
||||||
|
|
||||||
class Unbuffered:
|
class Unbuffered:
|
||||||
def __init__(self, stream):
|
def __init__(self, stream):
|
||||||
|
@ -71,32 +72,50 @@ class Unbuffered:
|
||||||
return getattr(self.stream, attr)
|
return getattr(self.stream, attr)
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
sys.stdout=Unbuffered(sys.stdout)
|
|
||||||
|
|
||||||
import struct, binascii, getopt, zlib, os, os.path, urllib, tempfile
|
import struct, binascii, getopt, zlib, os, os.path, urllib, tempfile
|
||||||
|
|
||||||
|
if 'calibre' in sys.modules:
|
||||||
|
inCalibre = True
|
||||||
|
else:
|
||||||
|
inCalibre = False
|
||||||
|
|
||||||
Des = None
|
Des = None
|
||||||
if sys.platform.startswith('win'):
|
if sys.platform.startswith('win'):
|
||||||
# first try with pycrypto
|
# first try with pycrypto
|
||||||
import pycrypto_des
|
if inCalibre:
|
||||||
|
from calibre_plugins.erdrpdb2pml import pycrypto_des
|
||||||
|
else:
|
||||||
|
import pycrypto_des
|
||||||
Des = pycrypto_des.load_pycrypto()
|
Des = pycrypto_des.load_pycrypto()
|
||||||
if Des == None:
|
if Des == None:
|
||||||
# they try with openssl
|
# they try with openssl
|
||||||
import openssl_des
|
if inCalibre:
|
||||||
|
from calibre_plugins.erdrpdb2pml import openssl_des
|
||||||
|
else:
|
||||||
|
import openssl_des
|
||||||
Des = openssl_des.load_libcrypto()
|
Des = openssl_des.load_libcrypto()
|
||||||
else:
|
else:
|
||||||
# first try with openssl
|
# first try with openssl
|
||||||
import openssl_des
|
if inCalibre:
|
||||||
|
from calibre_plugins.erdrpdb2pml import openssl_des
|
||||||
|
else:
|
||||||
|
import openssl_des
|
||||||
Des = openssl_des.load_libcrypto()
|
Des = openssl_des.load_libcrypto()
|
||||||
if Des == None:
|
if Des == None:
|
||||||
# then try with pycrypto
|
# then try with pycrypto
|
||||||
import pycrypto_des
|
if inCalibre:
|
||||||
|
from calibre_plugins.erdrpdb2pml import pycrypto_des
|
||||||
|
else:
|
||||||
|
import pycrypto_des
|
||||||
Des = pycrypto_des.load_pycrypto()
|
Des = pycrypto_des.load_pycrypto()
|
||||||
|
|
||||||
# if that did not work then use pure python implementation
|
# if that did not work then use pure python implementation
|
||||||
# of DES and try to speed it up with Psycho
|
# of DES and try to speed it up with Psycho
|
||||||
if Des == None:
|
if Des == None:
|
||||||
import python_des
|
if inCalibre:
|
||||||
|
from calibre_plugins.erdrpdb2pml import python_des
|
||||||
|
else:
|
||||||
|
import python_des
|
||||||
Des = python_des.Des
|
Des = python_des.Des
|
||||||
# Import Psyco if available
|
# Import Psyco if available
|
||||||
try:
|
try:
|
||||||
|
@ -480,5 +499,6 @@ def main(argv=None):
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
sys.stdout=Unbuffered(sys.stdout)
|
||||||
sys.exit(main())
|
sys.exit(main())
|
||||||
|
|
||||||
|
|
|
@ -21,10 +21,21 @@ from struct import unpack
|
||||||
|
|
||||||
|
|
||||||
# local support routines
|
# local support routines
|
||||||
import convert2xml
|
if 'calibre' in sys.modules:
|
||||||
import flatxml2html
|
inCalibre = True
|
||||||
import flatxml2svg
|
else:
|
||||||
import stylexml2css
|
inCalibre = False
|
||||||
|
|
||||||
|
if inCalibre :
|
||||||
|
from calibre_plugins.k4mobidedrm import convert2xml
|
||||||
|
from calibre_plugins.k4mobidedrm import flatxml2html
|
||||||
|
from calibre_plugins.k4mobidedrm import flatxml2svg
|
||||||
|
from calibre_plugins.k4mobidedrm import stylexml2css
|
||||||
|
else :
|
||||||
|
import convert2xml
|
||||||
|
import flatxml2html
|
||||||
|
import flatxml2svg
|
||||||
|
import stylexml2css
|
||||||
|
|
||||||
|
|
||||||
# Get a 7 bit encoded number from a file
|
# Get a 7 bit encoded number from a file
|
||||||
|
@ -504,7 +515,7 @@ def generateBook(bookDir, raw, fixedimage):
|
||||||
opfstr += ' </metadata>\n'
|
opfstr += ' </metadata>\n'
|
||||||
opfstr += '<manifest>\n'
|
opfstr += '<manifest>\n'
|
||||||
opfstr += ' <item id="book" href="book.html" media-type="application/xhtml+xml"/>\n'
|
opfstr += ' <item id="book" href="book.html" media-type="application/xhtml+xml"/>\n'
|
||||||
opfstr += ' <item id="stylesheet" href="style.css" media-type="text.css"/>\n'
|
opfstr += ' <item id="stylesheet" href="style.css" media-type="text/css"/>\n'
|
||||||
# adding image files to manifest
|
# adding image files to manifest
|
||||||
filenames = os.listdir(imgDir)
|
filenames = os.listdir(imgDir)
|
||||||
filenames = sorted(filenames)
|
filenames = sorted(filenames)
|
||||||
|
|
|
@ -157,6 +157,7 @@ def _load_crypto_libcrypto():
|
||||||
return out.raw
|
return out.raw
|
||||||
|
|
||||||
class AES(object):
|
class AES(object):
|
||||||
|
MODE_CBC = 0
|
||||||
@classmethod
|
@classmethod
|
||||||
def new(cls, userkey, mode, iv):
|
def new(cls, userkey, mode, iv):
|
||||||
self = AES()
|
self = AES()
|
||||||
|
|
|
@ -16,20 +16,8 @@ from __future__ import with_statement
|
||||||
# unswindle, DarkReverser, ApprenticeAlf, DiapDealer, some_updates
|
# unswindle, DarkReverser, ApprenticeAlf, DiapDealer, some_updates
|
||||||
# and many many others
|
# and many many others
|
||||||
|
|
||||||
# It can run standalone to convert K4M/K4PC/Mobi files, or it can be installed as a
|
|
||||||
# plugin for Calibre (http://calibre-ebook.com/about) so that importing
|
|
||||||
# K4 or Mobi with DRM is no londer a multi-step process.
|
|
||||||
#
|
|
||||||
# ***NOTE*** If you are using this script as a calibre plugin for a K4M or K4PC ebook
|
|
||||||
# then calibre must be installed on the same machine and in the same account as K4PC or K4M
|
|
||||||
# for the plugin version to function properly.
|
|
||||||
#
|
|
||||||
# To create a Calibre plugin, rename this file so that the filename
|
|
||||||
# ends in '_plugin.py', put it into a ZIP file with all its supporting python routines
|
|
||||||
# and import that ZIP into Calibre using its plugin configuration GUI.
|
|
||||||
|
|
||||||
|
__version__ = '3.1'
|
||||||
__version__ = '2.8'
|
|
||||||
|
|
||||||
class Unbuffered:
|
class Unbuffered:
|
||||||
def __init__(self, stream):
|
def __init__(self, stream):
|
||||||
|
@ -43,11 +31,7 @@ class Unbuffered:
|
||||||
import sys
|
import sys
|
||||||
import os, csv, getopt
|
import os, csv, getopt
|
||||||
import string
|
import string
|
||||||
import binascii
|
|
||||||
import zlib
|
|
||||||
import re
|
import re
|
||||||
import zlib, zipfile, tempfile, shutil
|
|
||||||
from struct import pack, unpack, unpack_from
|
|
||||||
|
|
||||||
class DrmException(Exception):
|
class DrmException(Exception):
|
||||||
pass
|
pass
|
||||||
|
@ -57,19 +41,15 @@ if 'calibre' in sys.modules:
|
||||||
else:
|
else:
|
||||||
inCalibre = False
|
inCalibre = False
|
||||||
|
|
||||||
def zipUpDir(myzip, tempdir,localname):
|
if inCalibre:
|
||||||
currentdir = tempdir
|
from calibre_plugins.k4mobidedrm import mobidedrm
|
||||||
if localname != "":
|
from calibre_plugins.k4mobidedrm import topazextract
|
||||||
currentdir = os.path.join(currentdir,localname)
|
from calibre_plugins.k4mobidedrm import kgenpids
|
||||||
list = os.listdir(currentdir)
|
else:
|
||||||
for file in list:
|
import mobidedrm
|
||||||
afilename = file
|
import topazextract
|
||||||
localfilePath = os.path.join(localname, afilename)
|
import kgenpids
|
||||||
realfilePath = os.path.join(currentdir,file)
|
|
||||||
if os.path.isfile(realfilePath):
|
|
||||||
myzip.write(realfilePath, localfilePath)
|
|
||||||
elif os.path.isdir(realfilePath):
|
|
||||||
zipUpDir(myzip, tempdir, localfilePath)
|
|
||||||
|
|
||||||
# cleanup bytestring filenames
|
# cleanup bytestring filenames
|
||||||
# borrowed from calibre from calibre/src/calibre/__init__.py
|
# borrowed from calibre from calibre/src/calibre/__init__.py
|
||||||
|
@ -94,10 +74,6 @@ def cleanup_name(name):
|
||||||
return one
|
return one
|
||||||
|
|
||||||
def decryptBook(infile, outdir, k4, kInfoFiles, serials, pids):
|
def decryptBook(infile, outdir, k4, kInfoFiles, serials, pids):
|
||||||
import mobidedrm
|
|
||||||
import topazextract
|
|
||||||
import kgenpids
|
|
||||||
|
|
||||||
# handle the obvious cases at the beginning
|
# handle the obvious cases at the beginning
|
||||||
if not os.path.isfile(infile):
|
if not os.path.isfile(infile):
|
||||||
print "Error: Input file does not exist"
|
print "Error: Input file does not exist"
|
||||||
|
@ -113,8 +89,7 @@ def decryptBook(infile, outdir, k4, kInfoFiles, serials, pids):
|
||||||
if mobi:
|
if mobi:
|
||||||
mb = mobidedrm.MobiBook(infile)
|
mb = mobidedrm.MobiBook(infile)
|
||||||
else:
|
else:
|
||||||
tempdir = tempfile.mkdtemp()
|
mb = topazextract.TopazBook(infile)
|
||||||
mb = topazextract.TopazBook(infile, tempdir)
|
|
||||||
|
|
||||||
title = mb.getBookTitle()
|
title = mb.getBookTitle()
|
||||||
print "Processing Book: ", title
|
print "Processing Book: ", title
|
||||||
|
@ -128,60 +103,39 @@ def decryptBook(infile, outdir, k4, kInfoFiles, serials, pids):
|
||||||
pidlst = kgenpids.getPidList(md1, md2, k4, pids, serials, kInfoFiles)
|
pidlst = kgenpids.getPidList(md1, md2, k4, pids, serials, kInfoFiles)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if mobi:
|
mb.processBook(pidlst)
|
||||||
unlocked_file = mb.processBook(pidlst)
|
|
||||||
else:
|
|
||||||
mb.processBook(pidlst)
|
|
||||||
|
|
||||||
except mobidedrm.DrmException, e:
|
except mobidedrm.DrmException, e:
|
||||||
print "Error: " + str(e) + "\nDRM Removal Failed.\n"
|
print "Error: " + str(e) + "\nDRM Removal Failed.\n"
|
||||||
return 1
|
return 1
|
||||||
|
except topazextract.TpzDRMError, e:
|
||||||
|
print "Error: " + str(e) + "\nDRM Removal Failed.\n"
|
||||||
|
return 1
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
if not mobi:
|
print "Error: " + str(e) + "\nDRM Removal Failed.\n"
|
||||||
print "Error: " + str(e) + "\nDRM Removal Failed.\n"
|
return 1
|
||||||
print " Creating DeBug Full Zip Archive of Book"
|
|
||||||
zipname = os.path.join(outdir, bookname + '_debug' + '.zip')
|
|
||||||
myzip = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
|
||||||
zipUpDir(myzip, tempdir, '')
|
|
||||||
myzip.close()
|
|
||||||
shutil.rmtree(tempdir, True)
|
|
||||||
return 1
|
|
||||||
pass
|
|
||||||
|
|
||||||
if mobi:
|
if mobi:
|
||||||
outfile = os.path.join(outdir,outfilename + '_nodrm' + '.mobi')
|
outfile = os.path.join(outdir, outfilename + '_nodrm' + '.mobi')
|
||||||
file(outfile, 'wb').write(unlocked_file)
|
mb.getMobiFile(outfile)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
# topaz: build up zip archives of results
|
# topaz:
|
||||||
print " Creating HTML ZIP Archive"
|
print " Creating NoDRM HTMLZ Archive"
|
||||||
zipname = os.path.join(outdir, outfilename + '_nodrm' + '.zip')
|
zipname = os.path.join(outdir, outfilename + '_nodrm' + '.htmlz')
|
||||||
myzip1 = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
mb.getHTMLZip(zipname)
|
||||||
myzip1.write(os.path.join(tempdir,'book.html'),'book.html')
|
|
||||||
myzip1.write(os.path.join(tempdir,'book.opf'),'book.opf')
|
|
||||||
if os.path.isfile(os.path.join(tempdir,'cover.jpg')):
|
|
||||||
myzip1.write(os.path.join(tempdir,'cover.jpg'),'cover.jpg')
|
|
||||||
myzip1.write(os.path.join(tempdir,'style.css'),'style.css')
|
|
||||||
zipUpDir(myzip1, tempdir, 'img')
|
|
||||||
myzip1.close()
|
|
||||||
|
|
||||||
print " Creating SVG ZIP Archive"
|
print " Creating SVG HTMLZ Archive"
|
||||||
zipname = os.path.join(outdir, outfilename + '_SVG' + '.zip')
|
zipname = os.path.join(outdir, outfilename + '_SVG' + '.htmlz')
|
||||||
myzip2 = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
mb.getSVGZip(zipname)
|
||||||
myzip2.write(os.path.join(tempdir,'index_svg.xhtml'),'index_svg.xhtml')
|
|
||||||
zipUpDir(myzip2, tempdir, 'svg')
|
|
||||||
zipUpDir(myzip2, tempdir, 'img')
|
|
||||||
myzip2.close()
|
|
||||||
|
|
||||||
print " Creating XML ZIP Archive"
|
print " Creating XML ZIP Archive"
|
||||||
zipname = os.path.join(outdir, outfilename + '_XML' + '.zip')
|
zipname = os.path.join(outdir, outfilename + '_XML' + '.zip')
|
||||||
myzip3 = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
mb.getXMLZip(zipname)
|
||||||
targetdir = os.path.join(tempdir,'xml')
|
|
||||||
zipUpDir(myzip3, targetdir, '')
|
# remove internal temporary directory of Topaz pieces
|
||||||
zipUpDir(myzip3, tempdir, 'img')
|
mb.cleanup()
|
||||||
myzip3.close()
|
|
||||||
|
|
||||||
shutil.rmtree(tempdir, True)
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
@ -236,7 +190,6 @@ def main(argv=sys.argv):
|
||||||
kInfoFiles = None
|
kInfoFiles = None
|
||||||
infile = args[0]
|
infile = args[0]
|
||||||
outdir = args[1]
|
outdir = args[1]
|
||||||
|
|
||||||
return decryptBook(infile, outdir, k4, kInfoFiles, serials, pids)
|
return decryptBook(infile, outdir, k4, kInfoFiles, serials, pids)
|
||||||
|
|
||||||
|
|
||||||
|
@ -244,131 +197,3 @@ if __name__ == '__main__':
|
||||||
sys.stdout=Unbuffered(sys.stdout)
|
sys.stdout=Unbuffered(sys.stdout)
|
||||||
sys.exit(main())
|
sys.exit(main())
|
||||||
|
|
||||||
if not __name__ == "__main__" and inCalibre:
|
|
||||||
from calibre.customize import FileTypePlugin
|
|
||||||
|
|
||||||
class K4DeDRM(FileTypePlugin):
|
|
||||||
name = 'K4PC, K4Mac, Kindle Mobi and Topaz DeDRM' # Name of the plugin
|
|
||||||
description = 'Removes DRM from K4PC and Mac, Kindle Mobi and Topaz files. \
|
|
||||||
Provided by the work of many including DiapDealer, SomeUpdates, IHeartCabbages, CMBDTC, Skindle, DarkReverser, ApprenticeAlf, etc.'
|
|
||||||
supported_platforms = ['osx', 'windows', 'linux'] # Platforms this plugin will run on
|
|
||||||
author = 'DiapDealer, SomeUpdates' # The author of this plugin
|
|
||||||
version = (0, 2, 8) # The version number of this plugin
|
|
||||||
file_types = set(['prc','mobi','azw','azw1','tpz']) # The file types that this plugin will be applied to
|
|
||||||
on_import = True # Run this plugin during the import
|
|
||||||
priority = 210 # run this plugin before mobidedrm, k4pcdedrm, k4dedrm
|
|
||||||
|
|
||||||
def run(self, path_to_ebook):
|
|
||||||
from calibre.gui2 import is_ok_to_use_qt
|
|
||||||
from PyQt4.Qt import QMessageBox
|
|
||||||
from calibre.ptempfile import PersistentTemporaryDirectory
|
|
||||||
|
|
||||||
import kgenpids
|
|
||||||
import zlib
|
|
||||||
import zipfile
|
|
||||||
import topazextract
|
|
||||||
import mobidedrm
|
|
||||||
|
|
||||||
k4 = True
|
|
||||||
if sys.platform.startswith('linux'):
|
|
||||||
k4 = False
|
|
||||||
pids = []
|
|
||||||
serials = []
|
|
||||||
kInfoFiles = []
|
|
||||||
|
|
||||||
# Get supplied list of PIDs to try from plugin customization.
|
|
||||||
customvalues = self.site_customization.split(',')
|
|
||||||
for customvalue in customvalues:
|
|
||||||
customvalue = str(customvalue)
|
|
||||||
customvalue = customvalue.strip()
|
|
||||||
if len(customvalue) == 10 or len(customvalue) == 8:
|
|
||||||
pids.append(customvalue)
|
|
||||||
else :
|
|
||||||
if len(customvalue) == 16 and customvalue[0] == 'B':
|
|
||||||
serials.append(customvalue)
|
|
||||||
else:
|
|
||||||
print "%s is not a valid Kindle serial number or PID." % str(customvalue)
|
|
||||||
|
|
||||||
# Load any kindle info files (*.info) included Calibre's config directory.
|
|
||||||
try:
|
|
||||||
# Find Calibre's configuration directory.
|
|
||||||
confpath = os.path.split(os.path.split(self.plugin_path)[0])[0]
|
|
||||||
print 'K4MobiDeDRM: Calibre configuration directory = %s' % confpath
|
|
||||||
files = os.listdir(confpath)
|
|
||||||
filefilter = re.compile("\.info$", re.IGNORECASE)
|
|
||||||
files = filter(filefilter.search, files)
|
|
||||||
|
|
||||||
if files:
|
|
||||||
for filename in files:
|
|
||||||
fpath = os.path.join(confpath, filename)
|
|
||||||
kInfoFiles.append(fpath)
|
|
||||||
print 'K4MobiDeDRM: Kindle info file %s found in config folder.' % filename
|
|
||||||
except IOError:
|
|
||||||
print 'K4MobiDeDRM: Error reading kindle info files from config directory.'
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
mobi = True
|
|
||||||
magic3 = file(path_to_ebook,'rb').read(3)
|
|
||||||
if magic3 == 'TPZ':
|
|
||||||
mobi = False
|
|
||||||
|
|
||||||
bookname = os.path.splitext(os.path.basename(path_to_ebook))[0]
|
|
||||||
|
|
||||||
if mobi:
|
|
||||||
mb = mobidedrm.MobiBook(path_to_ebook)
|
|
||||||
else:
|
|
||||||
tempdir = PersistentTemporaryDirectory()
|
|
||||||
mb = topazextract.TopazBook(path_to_ebook, tempdir)
|
|
||||||
|
|
||||||
title = mb.getBookTitle()
|
|
||||||
md1, md2 = mb.getPIDMetaInfo()
|
|
||||||
pidlst = kgenpids.getPidList(md1, md2, k4, pids, serials, kInfoFiles)
|
|
||||||
|
|
||||||
try:
|
|
||||||
if mobi:
|
|
||||||
unlocked_file = mb.processBook(pidlst)
|
|
||||||
else:
|
|
||||||
mb.processBook(pidlst)
|
|
||||||
|
|
||||||
except mobidedrm.DrmException:
|
|
||||||
#if you reached here then no luck raise and exception
|
|
||||||
if is_ok_to_use_qt():
|
|
||||||
d = QMessageBox(QMessageBox.Warning, "K4MobiDeDRM Plugin", "Error decoding: %s\n" % path_to_ebook)
|
|
||||||
d.show()
|
|
||||||
d.raise_()
|
|
||||||
d.exec_()
|
|
||||||
raise Exception("K4MobiDeDRM plugin could not decode the file")
|
|
||||||
return ""
|
|
||||||
except topazextract.TpzDRMError:
|
|
||||||
#if you reached here then no luck raise and exception
|
|
||||||
if is_ok_to_use_qt():
|
|
||||||
d = QMessageBox(QMessageBox.Warning, "K4MobiDeDRM Plugin", "Error decoding: %s\n" % path_to_ebook)
|
|
||||||
d.show()
|
|
||||||
d.raise_()
|
|
||||||
d.exec_()
|
|
||||||
raise Exception("K4MobiDeDRM plugin could not decode the file")
|
|
||||||
return ""
|
|
||||||
|
|
||||||
print "Success!"
|
|
||||||
if mobi:
|
|
||||||
of = self.temporary_file(bookname+'.mobi')
|
|
||||||
of.write(unlocked_file)
|
|
||||||
of.close()
|
|
||||||
return of.name
|
|
||||||
|
|
||||||
# topaz: build up zip archives of results
|
|
||||||
print " Creating HTML ZIP Archive"
|
|
||||||
of = self.temporary_file(bookname + '.zip')
|
|
||||||
myzip = zipfile.ZipFile(of.name,'w',zipfile.ZIP_DEFLATED, False)
|
|
||||||
myzip.write(os.path.join(tempdir,'book.html'),'book.html')
|
|
||||||
myzip.write(os.path.join(tempdir,'book.opf'),'book.opf')
|
|
||||||
if os.path.isfile(os.path.join(tempdir,'cover.jpg')):
|
|
||||||
myzip.write(os.path.join(tempdir,'cover.jpg'),'cover.jpg')
|
|
||||||
myzip.write(os.path.join(tempdir,'style.css'),'style.css')
|
|
||||||
zipUpDir(myzip, tempdir, 'img')
|
|
||||||
myzip.close()
|
|
||||||
return of.name
|
|
||||||
|
|
||||||
def customization_help(self, gui=False):
|
|
||||||
return 'Enter 10 character PIDs and/or Kindle serial numbers, separated by commas.'
|
|
|
@ -1,10 +1,12 @@
|
||||||
# standlone set of Mac OSX specific routines needed for K4DeDRM
|
# standlone set of Mac OSX specific routines needed for K4DeDRM
|
||||||
|
|
||||||
from __future__ import with_statement
|
from __future__ import with_statement
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
|
from struct import pack, unpack, unpack_from
|
||||||
|
|
||||||
class DrmException(Exception):
|
class DrmException(Exception):
|
||||||
pass
|
pass
|
||||||
|
@ -66,9 +68,8 @@ def _load_crypto_libcrypto():
|
||||||
raise DrmException('AES decryption failed')
|
raise DrmException('AES decryption failed')
|
||||||
return out.raw
|
return out.raw
|
||||||
|
|
||||||
def keyivgen(self, passwd):
|
def keyivgen(self, passwd, salt):
|
||||||
salt = '16743'
|
saltlen = len(salt)
|
||||||
saltlen = 5
|
|
||||||
passlen = len(passwd)
|
passlen = len(passwd)
|
||||||
iter = 0x3e8
|
iter = 0x3e8
|
||||||
keylen = 80
|
keylen = 80
|
||||||
|
@ -91,12 +92,78 @@ LibCrypto = _load_crypto()
|
||||||
# Utility Routines
|
# Utility Routines
|
||||||
#
|
#
|
||||||
|
|
||||||
|
# crypto digestroutines
|
||||||
|
import hashlib
|
||||||
|
|
||||||
|
def MD5(message):
|
||||||
|
ctx = hashlib.md5()
|
||||||
|
ctx.update(message)
|
||||||
|
return ctx.digest()
|
||||||
|
|
||||||
|
def SHA1(message):
|
||||||
|
ctx = hashlib.sha1()
|
||||||
|
ctx.update(message)
|
||||||
|
return ctx.digest()
|
||||||
|
|
||||||
|
def SHA256(message):
|
||||||
|
ctx = hashlib.sha256()
|
||||||
|
ctx.update(message)
|
||||||
|
return ctx.digest()
|
||||||
|
|
||||||
# Various character maps used to decrypt books. Probably supposed to act as obfuscation
|
# Various character maps used to decrypt books. Probably supposed to act as obfuscation
|
||||||
charMap1 = "n5Pr6St7Uv8Wx9YzAb0Cd1Ef2Gh3Jk4M"
|
charMap1 = "n5Pr6St7Uv8Wx9YzAb0Cd1Ef2Gh3Jk4M"
|
||||||
charMap2 = "ZB0bYyc1xDdW2wEV3Ff7KkPpL8UuGA4gz-Tme9Nn_tHh5SvXCsIiR6rJjQaqlOoM"
|
charMap2 = "ZB0bYyc1xDdW2wEV3Ff7KkPpL8UuGA4gz-Tme9Nn_tHh5SvXCsIiR6rJjQaqlOoM"
|
||||||
charMap3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
|
||||||
charMap4 = "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789"
|
# For Future Reference from .kinf approach of K4PC
|
||||||
|
charMap5 = "AzB0bYyCeVvaZ3FfUuG4g-TtHh5SsIiR6rJjQq7KkPpL8lOoMm9Nn_c1XxDdW2wE"
|
||||||
|
|
||||||
|
|
||||||
|
def encode(data, map):
|
||||||
|
result = ""
|
||||||
|
for char in data:
|
||||||
|
value = ord(char)
|
||||||
|
Q = (value ^ 0x80) // len(map)
|
||||||
|
R = value % len(map)
|
||||||
|
result += map[Q]
|
||||||
|
result += map[R]
|
||||||
|
return result
|
||||||
|
|
||||||
|
# Hash the bytes in data and then encode the digest with the characters in map
|
||||||
|
def encodeHash(data,map):
|
||||||
|
return encode(MD5(data),map)
|
||||||
|
|
||||||
|
# Decode the string in data with the characters in map. Returns the decoded bytes
|
||||||
|
def decode(data,map):
|
||||||
|
result = ""
|
||||||
|
for i in range (0,len(data)-1,2):
|
||||||
|
high = map.find(data[i])
|
||||||
|
low = map.find(data[i+1])
|
||||||
|
if (high == -1) or (low == -1) :
|
||||||
|
break
|
||||||
|
value = (((high * len(map)) ^ 0x80) & 0xFF) + low
|
||||||
|
result += pack("B",value)
|
||||||
|
return result
|
||||||
|
|
||||||
|
# For Future Reference from .kinf approach of K4PC
|
||||||
|
# generate table of prime number less than or equal to int n
|
||||||
|
def primes(n):
|
||||||
|
if n==2: return [2]
|
||||||
|
elif n<2: return []
|
||||||
|
s=range(3,n+1,2)
|
||||||
|
mroot = n ** 0.5
|
||||||
|
half=(n+1)/2-1
|
||||||
|
i=0
|
||||||
|
m=3
|
||||||
|
while m <= mroot:
|
||||||
|
if s[i]:
|
||||||
|
j=(m*m-3)/2
|
||||||
|
s[j]=0
|
||||||
|
while j<half:
|
||||||
|
s[j]=0
|
||||||
|
j+=m
|
||||||
|
i=i+1
|
||||||
|
m=2*i+3
|
||||||
|
return [2]+[x for x in s if x]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -137,30 +204,12 @@ def GetUserName():
|
||||||
username = os.getenv('USER')
|
username = os.getenv('USER')
|
||||||
return username
|
return username
|
||||||
|
|
||||||
|
|
||||||
def encode(data, map):
|
|
||||||
result = ""
|
|
||||||
for char in data:
|
|
||||||
value = ord(char)
|
|
||||||
Q = (value ^ 0x80) // len(map)
|
|
||||||
R = value % len(map)
|
|
||||||
result += map[Q]
|
|
||||||
result += map[R]
|
|
||||||
return result
|
|
||||||
|
|
||||||
import hashlib
|
|
||||||
|
|
||||||
def SHA256(message):
|
|
||||||
ctx = hashlib.sha256()
|
|
||||||
ctx.update(message)
|
|
||||||
return ctx.digest()
|
|
||||||
|
|
||||||
# implements an Pseudo Mac Version of Windows built-in Crypto routine
|
# implements an Pseudo Mac Version of Windows built-in Crypto routine
|
||||||
def CryptUnprotectData(encryptedData):
|
def CryptUnprotectData(encryptedData, salt):
|
||||||
sp = GetVolumeSerialNumber() + '!@#' + GetUserName()
|
sp = GetVolumeSerialNumber() + '!@#' + GetUserName()
|
||||||
passwdData = encode(SHA256(sp),charMap1)
|
passwdData = encode(SHA256(sp),charMap1)
|
||||||
crp = LibCrypto()
|
crp = LibCrypto()
|
||||||
key_iv = crp.keyivgen(passwdData)
|
key_iv = crp.keyivgen(passwdData, salt)
|
||||||
key = key_iv[0:32]
|
key = key_iv[0:32]
|
||||||
iv = key_iv[32:48]
|
iv = key_iv[32:48]
|
||||||
crp.set_decrypt_key(key,iv)
|
crp.set_decrypt_key(key,iv)
|
||||||
|
@ -170,6 +219,7 @@ def CryptUnprotectData(encryptedData):
|
||||||
|
|
||||||
# Locate the .kindle-info files
|
# Locate the .kindle-info files
|
||||||
def getKindleInfoFiles(kInfoFiles):
|
def getKindleInfoFiles(kInfoFiles):
|
||||||
|
# first search for current .kindle-info files
|
||||||
home = os.getenv('HOME')
|
home = os.getenv('HOME')
|
||||||
cmdline = 'find "' + home + '/Library/Application Support" -name ".kindle-info"'
|
cmdline = 'find "' + home + '/Library/Application Support" -name ".kindle-info"'
|
||||||
cmdline = cmdline.encode(sys.getfilesystemencoding())
|
cmdline = cmdline.encode(sys.getfilesystemencoding())
|
||||||
|
@ -178,23 +228,130 @@ def getKindleInfoFiles(kInfoFiles):
|
||||||
reslst = out1.split('\n')
|
reslst = out1.split('\n')
|
||||||
kinfopath = 'NONE'
|
kinfopath = 'NONE'
|
||||||
found = False
|
found = False
|
||||||
cnt = len(reslst)
|
|
||||||
for resline in reslst:
|
for resline in reslst:
|
||||||
if os.path.isfile(resline):
|
if os.path.isfile(resline):
|
||||||
kInfoFiles.append(resline)
|
kInfoFiles.append(resline)
|
||||||
found = True
|
found = True
|
||||||
|
# For Future Reference
|
||||||
|
#
|
||||||
|
# # add any .kinf files
|
||||||
|
# cmdline = 'find "' + home + '/Library/Application Support" -name "rainier*.kinf"'
|
||||||
|
# cmdline = cmdline.encode(sys.getfilesystemencoding())
|
||||||
|
# p1 = subprocess.Popen(cmdline, shell=True, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=False)
|
||||||
|
# out1, out2 = p1.communicate()
|
||||||
|
# reslst = out1.split('\n')
|
||||||
|
# for resline in reslst:
|
||||||
|
# if os.path.isfile(resline):
|
||||||
|
# kInfoFiles.append(resline)
|
||||||
|
# found = True
|
||||||
if not found:
|
if not found:
|
||||||
print('No .kindle-info files have been found.')
|
print('No kindle-info files have been found.')
|
||||||
return kInfoFiles
|
return kInfoFiles
|
||||||
|
|
||||||
# Parse the Kindle.info file and return the records as a list of key-values
|
# determine type of kindle info provided and return a
|
||||||
def parseKindleInfo(kInfoFile):
|
# database of keynames and values
|
||||||
|
def getDBfromFile(kInfoFile):
|
||||||
|
names = ["kindle.account.tokens","kindle.cookie.item","eulaVersionAccepted","login_date","kindle.token.item","login","kindle.key.item","kindle.name.info","kindle.device.info", "MazamaRandomNumber"]
|
||||||
DB = {}
|
DB = {}
|
||||||
|
cnt = 0
|
||||||
infoReader = open(kInfoFile, 'r')
|
infoReader = open(kInfoFile, 'r')
|
||||||
infoReader.read(1)
|
hdr = infoReader.read(1)
|
||||||
data = infoReader.read()
|
data = infoReader.read()
|
||||||
items = data.split('[')
|
|
||||||
for item in items:
|
if data.find('[') != -1 :
|
||||||
splito = item.split(':')
|
|
||||||
DB[splito[0]] =splito[1]
|
# older style kindle-info file
|
||||||
|
items = data.split('[')
|
||||||
|
for item in items:
|
||||||
|
if item != '':
|
||||||
|
keyhash, rawdata = item.split(':')
|
||||||
|
keyname = "unknown"
|
||||||
|
for name in names:
|
||||||
|
if encodeHash(name,charMap2) == keyhash:
|
||||||
|
keyname = name
|
||||||
|
break
|
||||||
|
if keyname == "unknown":
|
||||||
|
keyname = keyhash
|
||||||
|
encryptedValue = decode(rawdata,charMap2)
|
||||||
|
salt = '16743'
|
||||||
|
cleartext = CryptUnprotectData(encryptedValue, salt)
|
||||||
|
DB[keyname] = decode(cleartext,charMap1)
|
||||||
|
cnt = cnt + 1
|
||||||
|
if cnt == 0:
|
||||||
|
DB = None
|
||||||
|
return DB
|
||||||
|
|
||||||
|
# For Future Reference taken from K4PC 1.5.0 .kinf
|
||||||
|
#
|
||||||
|
# # else newer style .kinf file
|
||||||
|
# # the .kinf file uses "/" to separate it into records
|
||||||
|
# # so remove the trailing "/" to make it easy to use split
|
||||||
|
# data = data[:-1]
|
||||||
|
# items = data.split('/')
|
||||||
|
#
|
||||||
|
# # loop through the item records until all are processed
|
||||||
|
# while len(items) > 0:
|
||||||
|
#
|
||||||
|
# # get the first item record
|
||||||
|
# item = items.pop(0)
|
||||||
|
#
|
||||||
|
# # the first 32 chars of the first record of a group
|
||||||
|
# # is the MD5 hash of the key name encoded by charMap5
|
||||||
|
# keyhash = item[0:32]
|
||||||
|
#
|
||||||
|
# # the raw keyhash string is also used to create entropy for the actual
|
||||||
|
# # CryptProtectData Blob that represents that keys contents
|
||||||
|
# entropy = SHA1(keyhash)
|
||||||
|
#
|
||||||
|
# # the remainder of the first record when decoded with charMap5
|
||||||
|
# # has the ':' split char followed by the string representation
|
||||||
|
# # of the number of records that follow
|
||||||
|
# # and make up the contents
|
||||||
|
# srcnt = decode(item[34:],charMap5)
|
||||||
|
# rcnt = int(srcnt)
|
||||||
|
#
|
||||||
|
# # read and store in rcnt records of data
|
||||||
|
# # that make up the contents value
|
||||||
|
# edlst = []
|
||||||
|
# for i in xrange(rcnt):
|
||||||
|
# item = items.pop(0)
|
||||||
|
# edlst.append(item)
|
||||||
|
#
|
||||||
|
# keyname = "unknown"
|
||||||
|
# for name in names:
|
||||||
|
# if encodeHash(name,charMap5) == keyhash:
|
||||||
|
# keyname = name
|
||||||
|
# break
|
||||||
|
# if keyname == "unknown":
|
||||||
|
# keyname = keyhash
|
||||||
|
#
|
||||||
|
# # the charMap5 encoded contents data has had a length
|
||||||
|
# # of chars (always odd) cut off of the front and moved
|
||||||
|
# # to the end to prevent decoding using charMap5 from
|
||||||
|
# # working properly, and thereby preventing the ensuing
|
||||||
|
# # CryptUnprotectData call from succeeding.
|
||||||
|
#
|
||||||
|
# # The offset into the charMap5 encoded contents seems to be:
|
||||||
|
# # len(contents) - largest prime number less than or equal to int(len(content)/3)
|
||||||
|
# # (in other words split "about" 2/3rds of the way through)
|
||||||
|
#
|
||||||
|
# # move first offsets chars to end to align for decode by charMap5
|
||||||
|
# encdata = "".join(edlst)
|
||||||
|
# contlen = len(encdata)
|
||||||
|
# noffset = contlen - primes(int(contlen/3))[-1]
|
||||||
|
#
|
||||||
|
# # now properly split and recombine
|
||||||
|
# # by moving noffset chars from the start of the
|
||||||
|
# # string to the end of the string
|
||||||
|
# pfx = encdata[0:noffset]
|
||||||
|
# encdata = encdata[noffset:]
|
||||||
|
# encdata = encdata + pfx
|
||||||
|
#
|
||||||
|
# # decode using Map5 to get the CryptProtect Data
|
||||||
|
# encryptedValue = decode(encdata,charMap5)
|
||||||
|
# DB[keyname] = CryptUnprotectData(encryptedValue, entropy, 1)
|
||||||
|
# cnt = cnt + 1
|
||||||
|
|
||||||
|
if cnt == 0:
|
||||||
|
DB = None
|
||||||
return DB
|
return DB
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
# K4PC Windows specific routines
|
# K4PC Windows specific routines
|
||||||
|
|
||||||
from __future__ import with_statement
|
from __future__ import with_statement
|
||||||
|
|
||||||
import sys, os
|
import sys, os
|
||||||
|
from struct import pack, unpack, unpack_from
|
||||||
|
|
||||||
from ctypes import windll, c_char_p, c_wchar_p, c_uint, POINTER, byref, \
|
from ctypes import windll, c_char_p, c_wchar_p, c_uint, POINTER, byref, \
|
||||||
create_unicode_buffer, create_string_buffer, CFUNCTYPE, addressof, \
|
create_unicode_buffer, create_string_buffer, CFUNCTYPE, addressof, \
|
||||||
|
@ -10,25 +12,86 @@ from ctypes import windll, c_char_p, c_wchar_p, c_uint, POINTER, byref, \
|
||||||
|
|
||||||
import _winreg as winreg
|
import _winreg as winreg
|
||||||
|
|
||||||
import traceback
|
|
||||||
|
|
||||||
MAX_PATH = 255
|
MAX_PATH = 255
|
||||||
|
|
||||||
kernel32 = windll.kernel32
|
kernel32 = windll.kernel32
|
||||||
advapi32 = windll.advapi32
|
advapi32 = windll.advapi32
|
||||||
crypt32 = windll.crypt32
|
crypt32 = windll.crypt32
|
||||||
|
|
||||||
|
import traceback
|
||||||
|
|
||||||
# Various character maps used to decrypt books. Probably supposed to act as obfuscation
|
# crypto digestroutines
|
||||||
charMap1 = "n5Pr6St7Uv8Wx9YzAb0Cd1Ef2Gh3Jk4M"
|
import hashlib
|
||||||
|
|
||||||
|
def MD5(message):
|
||||||
|
ctx = hashlib.md5()
|
||||||
|
ctx.update(message)
|
||||||
|
return ctx.digest()
|
||||||
|
|
||||||
|
def SHA1(message):
|
||||||
|
ctx = hashlib.sha1()
|
||||||
|
ctx.update(message)
|
||||||
|
return ctx.digest()
|
||||||
|
|
||||||
|
|
||||||
|
# simple primes table (<= n) calculator
|
||||||
|
def primes(n):
|
||||||
|
if n==2: return [2]
|
||||||
|
elif n<2: return []
|
||||||
|
s=range(3,n+1,2)
|
||||||
|
mroot = n ** 0.5
|
||||||
|
half=(n+1)/2-1
|
||||||
|
i=0
|
||||||
|
m=3
|
||||||
|
while m <= mroot:
|
||||||
|
if s[i]:
|
||||||
|
j=(m*m-3)/2
|
||||||
|
s[j]=0
|
||||||
|
while j<half:
|
||||||
|
s[j]=0
|
||||||
|
j+=m
|
||||||
|
i=i+1
|
||||||
|
m=2*i+3
|
||||||
|
return [2]+[x for x in s if x]
|
||||||
|
|
||||||
|
|
||||||
|
# Various character maps used to decrypt kindle info values.
|
||||||
|
# Probably supposed to act as obfuscation
|
||||||
charMap2 = "AaZzB0bYyCc1XxDdW2wEeVv3FfUuG4g-TtHh5SsIiR6rJjQq7KkPpL8lOoMm9Nn_"
|
charMap2 = "AaZzB0bYyCc1XxDdW2wEeVv3FfUuG4g-TtHh5SsIiR6rJjQq7KkPpL8lOoMm9Nn_"
|
||||||
charMap3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
charMap5 = "AzB0bYyCeVvaZ3FfUuG4g-TtHh5SsIiR6rJjQq7KkPpL8lOoMm9Nn_c1XxDdW2wE"
|
||||||
charMap4 = "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789"
|
|
||||||
|
|
||||||
class DrmException(Exception):
|
class DrmException(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
# Encode the bytes in data with the characters in map
|
||||||
|
def encode(data, map):
|
||||||
|
result = ""
|
||||||
|
for char in data:
|
||||||
|
value = ord(char)
|
||||||
|
Q = (value ^ 0x80) // len(map)
|
||||||
|
R = value % len(map)
|
||||||
|
result += map[Q]
|
||||||
|
result += map[R]
|
||||||
|
return result
|
||||||
|
|
||||||
|
# Hash the bytes in data and then encode the digest with the characters in map
|
||||||
|
def encodeHash(data,map):
|
||||||
|
return encode(MD5(data),map)
|
||||||
|
|
||||||
|
# Decode the string in data with the characters in map. Returns the decoded bytes
|
||||||
|
def decode(data,map):
|
||||||
|
result = ""
|
||||||
|
for i in range (0,len(data)-1,2):
|
||||||
|
high = map.find(data[i])
|
||||||
|
low = map.find(data[i+1])
|
||||||
|
if (high == -1) or (low == -1) :
|
||||||
|
break
|
||||||
|
value = (((high * len(map)) ^ 0x80) & 0xFF) + low
|
||||||
|
result += pack("B",value)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
# interface with Windows OS Routines
|
||||||
class DataBlob(Structure):
|
class DataBlob(Structure):
|
||||||
_fields_ = [('cbData', c_uint),
|
_fields_ = [('cbData', c_uint),
|
||||||
('pbData', c_void_p)]
|
('pbData', c_void_p)]
|
||||||
|
@ -59,59 +122,175 @@ def GetVolumeSerialNumber():
|
||||||
return GetVolumeSerialNumber
|
return GetVolumeSerialNumber
|
||||||
GetVolumeSerialNumber = GetVolumeSerialNumber()
|
GetVolumeSerialNumber = GetVolumeSerialNumber()
|
||||||
|
|
||||||
|
def getLastError():
|
||||||
|
GetLastError = kernel32.GetLastError
|
||||||
|
GetLastError.argtypes = None
|
||||||
|
GetLastError.restype = c_uint
|
||||||
|
def getLastError():
|
||||||
|
return GetLastError()
|
||||||
|
return getLastError
|
||||||
|
getLastError = getLastError()
|
||||||
|
|
||||||
def GetUserName():
|
def GetUserName():
|
||||||
GetUserNameW = advapi32.GetUserNameW
|
GetUserNameW = advapi32.GetUserNameW
|
||||||
GetUserNameW.argtypes = [c_wchar_p, POINTER(c_uint)]
|
GetUserNameW.argtypes = [c_wchar_p, POINTER(c_uint)]
|
||||||
GetUserNameW.restype = c_uint
|
GetUserNameW.restype = c_uint
|
||||||
def GetUserName():
|
def GetUserName():
|
||||||
buffer = create_unicode_buffer(32)
|
buffer = create_unicode_buffer(2)
|
||||||
size = c_uint(len(buffer))
|
size = c_uint(len(buffer))
|
||||||
while not GetUserNameW(buffer, byref(size)):
|
while not GetUserNameW(buffer, byref(size)):
|
||||||
|
errcd = getLastError()
|
||||||
|
if errcd == 234:
|
||||||
|
# bad wine implementation up through wine 1.3.21
|
||||||
|
return "AlternateUserName"
|
||||||
buffer = create_unicode_buffer(len(buffer) * 2)
|
buffer = create_unicode_buffer(len(buffer) * 2)
|
||||||
size.value = len(buffer)
|
size.value = len(buffer)
|
||||||
return buffer.value.encode('utf-16-le')[::2]
|
return buffer.value.encode('utf-16-le')[::2]
|
||||||
return GetUserName
|
return GetUserName
|
||||||
GetUserName = GetUserName()
|
GetUserName = GetUserName()
|
||||||
|
|
||||||
|
|
||||||
def CryptUnprotectData():
|
def CryptUnprotectData():
|
||||||
_CryptUnprotectData = crypt32.CryptUnprotectData
|
_CryptUnprotectData = crypt32.CryptUnprotectData
|
||||||
_CryptUnprotectData.argtypes = [DataBlob_p, c_wchar_p, DataBlob_p,
|
_CryptUnprotectData.argtypes = [DataBlob_p, c_wchar_p, DataBlob_p,
|
||||||
c_void_p, c_void_p, c_uint, DataBlob_p]
|
c_void_p, c_void_p, c_uint, DataBlob_p]
|
||||||
_CryptUnprotectData.restype = c_uint
|
_CryptUnprotectData.restype = c_uint
|
||||||
def CryptUnprotectData(indata, entropy):
|
def CryptUnprotectData(indata, entropy, flags):
|
||||||
indatab = create_string_buffer(indata)
|
indatab = create_string_buffer(indata)
|
||||||
indata = DataBlob(len(indata), cast(indatab, c_void_p))
|
indata = DataBlob(len(indata), cast(indatab, c_void_p))
|
||||||
entropyb = create_string_buffer(entropy)
|
entropyb = create_string_buffer(entropy)
|
||||||
entropy = DataBlob(len(entropy), cast(entropyb, c_void_p))
|
entropy = DataBlob(len(entropy), cast(entropyb, c_void_p))
|
||||||
outdata = DataBlob()
|
outdata = DataBlob()
|
||||||
if not _CryptUnprotectData(byref(indata), None, byref(entropy),
|
if not _CryptUnprotectData(byref(indata), None, byref(entropy),
|
||||||
None, None, 0, byref(outdata)):
|
None, None, flags, byref(outdata)):
|
||||||
raise DrmException("Failed to Unprotect Data")
|
raise DrmException("Failed to Unprotect Data")
|
||||||
return string_at(outdata.pbData, outdata.cbData)
|
return string_at(outdata.pbData, outdata.cbData)
|
||||||
return CryptUnprotectData
|
return CryptUnprotectData
|
||||||
CryptUnprotectData = CryptUnprotectData()
|
CryptUnprotectData = CryptUnprotectData()
|
||||||
|
|
||||||
# Locate the .kindle-info files
|
|
||||||
|
# Locate all of the kindle-info style files and return as list
|
||||||
def getKindleInfoFiles(kInfoFiles):
|
def getKindleInfoFiles(kInfoFiles):
|
||||||
regkey = winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\\")
|
regkey = winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\\")
|
||||||
path = winreg.QueryValueEx(regkey, 'Local AppData')[0]
|
path = winreg.QueryValueEx(regkey, 'Local AppData')[0]
|
||||||
|
|
||||||
|
# first look for older kindle-info files
|
||||||
kinfopath = path +'\\Amazon\\Kindle For PC\\{AMAwzsaPaaZAzmZzZQzgZCAkZ3AjA_AY}\\kindle.info'
|
kinfopath = path +'\\Amazon\\Kindle For PC\\{AMAwzsaPaaZAzmZzZQzgZCAkZ3AjA_AY}\\kindle.info'
|
||||||
if not os.path.isfile(kinfopath):
|
if not os.path.isfile(kinfopath):
|
||||||
print('The kindle.info files has not been found.')
|
print('No kindle.info files have not been found.')
|
||||||
|
else:
|
||||||
|
kInfoFiles.append(kinfopath)
|
||||||
|
|
||||||
|
# now look for newer (K4PC 1.5.0 and later rainier.2.1.1.kinf file
|
||||||
|
kinfopath = path +'\\Amazon\\Kindle For PC\\storage\\rainier.2.1.1.kinf'
|
||||||
|
if not os.path.isfile(kinfopath):
|
||||||
|
print('No .kinf files have not been found.')
|
||||||
else:
|
else:
|
||||||
kInfoFiles.append(kinfopath)
|
kInfoFiles.append(kinfopath)
|
||||||
return kInfoFiles
|
return kInfoFiles
|
||||||
|
|
||||||
# Parse the Kindle.info file and return the records as a list of key-values
|
|
||||||
def parseKindleInfo(kInfoFile):
|
# determine type of kindle info provided and return a
|
||||||
|
# database of keynames and values
|
||||||
|
def getDBfromFile(kInfoFile):
|
||||||
|
names = ["kindle.account.tokens","kindle.cookie.item","eulaVersionAccepted","login_date","kindle.token.item","login","kindle.key.item","kindle.name.info","kindle.device.info", "MazamaRandomNumber"]
|
||||||
DB = {}
|
DB = {}
|
||||||
|
cnt = 0
|
||||||
infoReader = open(kInfoFile, 'r')
|
infoReader = open(kInfoFile, 'r')
|
||||||
infoReader.read(1)
|
hdr = infoReader.read(1)
|
||||||
data = infoReader.read()
|
data = infoReader.read()
|
||||||
items = data.split('{')
|
|
||||||
for item in items:
|
if data.find('{') != -1 :
|
||||||
splito = item.split(':')
|
|
||||||
DB[splito[0]] =splito[1]
|
# older style kindle-info file
|
||||||
|
items = data.split('{')
|
||||||
|
for item in items:
|
||||||
|
if item != '':
|
||||||
|
keyhash, rawdata = item.split(':')
|
||||||
|
keyname = "unknown"
|
||||||
|
for name in names:
|
||||||
|
if encodeHash(name,charMap2) == keyhash:
|
||||||
|
keyname = name
|
||||||
|
break
|
||||||
|
if keyname == "unknown":
|
||||||
|
keyname = keyhash
|
||||||
|
encryptedValue = decode(rawdata,charMap2)
|
||||||
|
DB[keyname] = CryptUnprotectData(encryptedValue, "", 0)
|
||||||
|
cnt = cnt + 1
|
||||||
|
if cnt == 0:
|
||||||
|
DB = None
|
||||||
|
return DB
|
||||||
|
|
||||||
|
# else newer style .kinf file
|
||||||
|
# the .kinf file uses "/" to separate it into records
|
||||||
|
# so remove the trailing "/" to make it easy to use split
|
||||||
|
data = data[:-1]
|
||||||
|
items = data.split('/')
|
||||||
|
|
||||||
|
# loop through the item records until all are processed
|
||||||
|
while len(items) > 0:
|
||||||
|
|
||||||
|
# get the first item record
|
||||||
|
item = items.pop(0)
|
||||||
|
|
||||||
|
# the first 32 chars of the first record of a group
|
||||||
|
# is the MD5 hash of the key name encoded by charMap5
|
||||||
|
keyhash = item[0:32]
|
||||||
|
|
||||||
|
# the raw keyhash string is also used to create entropy for the actual
|
||||||
|
# CryptProtectData Blob that represents that keys contents
|
||||||
|
entropy = SHA1(keyhash)
|
||||||
|
|
||||||
|
# the remainder of the first record when decoded with charMap5
|
||||||
|
# has the ':' split char followed by the string representation
|
||||||
|
# of the number of records that follow
|
||||||
|
# and make up the contents
|
||||||
|
srcnt = decode(item[34:],charMap5)
|
||||||
|
rcnt = int(srcnt)
|
||||||
|
|
||||||
|
# read and store in rcnt records of data
|
||||||
|
# that make up the contents value
|
||||||
|
edlst = []
|
||||||
|
for i in xrange(rcnt):
|
||||||
|
item = items.pop(0)
|
||||||
|
edlst.append(item)
|
||||||
|
|
||||||
|
keyname = "unknown"
|
||||||
|
for name in names:
|
||||||
|
if encodeHash(name,charMap5) == keyhash:
|
||||||
|
keyname = name
|
||||||
|
break
|
||||||
|
if keyname == "unknown":
|
||||||
|
keyname = keyhash
|
||||||
|
|
||||||
|
# the charMap5 encoded contents data has had a length
|
||||||
|
# of chars (always odd) cut off of the front and moved
|
||||||
|
# to the end to prevent decoding using charMap5 from
|
||||||
|
# working properly, and thereby preventing the ensuing
|
||||||
|
# CryptUnprotectData call from succeeding.
|
||||||
|
|
||||||
|
# The offset into the charMap5 encoded contents seems to be:
|
||||||
|
# len(contents) - largest prime number less than or equal to int(len(content)/3)
|
||||||
|
# (in other words split "about" 2/3rds of the way through)
|
||||||
|
|
||||||
|
# move first offsets chars to end to align for decode by charMap5
|
||||||
|
encdata = "".join(edlst)
|
||||||
|
contlen = len(encdata)
|
||||||
|
noffset = contlen - primes(int(contlen/3))[-1]
|
||||||
|
|
||||||
|
# now properly split and recombine
|
||||||
|
# by moving noffset chars from the start of the
|
||||||
|
# string to the end of the string
|
||||||
|
pfx = encdata[0:noffset]
|
||||||
|
encdata = encdata[noffset:]
|
||||||
|
encdata = encdata + pfx
|
||||||
|
|
||||||
|
# decode using Map5 to get the CryptProtect Data
|
||||||
|
encryptedValue = decode(encdata,charMap5)
|
||||||
|
DB[keyname] = CryptUnprotectData(encryptedValue, entropy, 1)
|
||||||
|
cnt = cnt + 1
|
||||||
|
|
||||||
|
if cnt == 0:
|
||||||
|
DB = None
|
||||||
return DB
|
return DB
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -11,16 +11,28 @@ from struct import pack, unpack, unpack_from
|
||||||
class DrmException(Exception):
|
class DrmException(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
global kindleDatabase
|
|
||||||
global charMap1
|
global charMap1
|
||||||
global charMap2
|
|
||||||
global charMap3
|
global charMap3
|
||||||
global charMap4
|
global charMap4
|
||||||
|
|
||||||
if sys.platform.startswith('win'):
|
if 'calibre' in sys.modules:
|
||||||
from k4pcutils import getKindleInfoFiles, parseKindleInfo, CryptUnprotectData, GetUserName, GetVolumeSerialNumber, charMap2
|
inCalibre = True
|
||||||
if sys.platform.startswith('darwin'):
|
else:
|
||||||
from k4mutils import getKindleInfoFiles, parseKindleInfo, CryptUnprotectData, GetUserName, GetVolumeSerialNumber, charMap2
|
inCalibre = False
|
||||||
|
|
||||||
|
if inCalibre:
|
||||||
|
if sys.platform.startswith('win'):
|
||||||
|
from calibre_plugins.k4mobidedrm.k4pcutils import getKindleInfoFiles, getDBfromFile, GetUserName, GetVolumeSerialNumber
|
||||||
|
|
||||||
|
if sys.platform.startswith('darwin'):
|
||||||
|
from calibre_plugins.k4mobidedrm.k4mutils import getKindleInfoFiles, getDBfromFile, GetUserName, GetVolumeSerialNumber
|
||||||
|
else:
|
||||||
|
if sys.platform.startswith('win'):
|
||||||
|
from k4pcutils import getKindleInfoFiles, getDBfromFile, GetUserName, GetVolumeSerialNumber
|
||||||
|
|
||||||
|
if sys.platform.startswith('darwin'):
|
||||||
|
from k4mutils import getKindleInfoFiles, getDBfromFile, GetUserName, GetVolumeSerialNumber
|
||||||
|
|
||||||
|
|
||||||
charMap1 = "n5Pr6St7Uv8Wx9YzAb0Cd1Ef2Gh3Jk4M"
|
charMap1 = "n5Pr6St7Uv8Wx9YzAb0Cd1Ef2Gh3Jk4M"
|
||||||
charMap3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
charMap3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
||||||
|
@ -66,50 +78,7 @@ def decode(data,map):
|
||||||
value = (((high * len(map)) ^ 0x80) & 0xFF) + low
|
value = (((high * len(map)) ^ 0x80) & 0xFF) + low
|
||||||
result += pack("B",value)
|
result += pack("B",value)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
# Get a record from the Kindle.info file for the key "hashedKey" (already hashed and encoded).
|
|
||||||
# Return the decoded and decrypted record
|
|
||||||
def getKindleInfoValueForHash(hashedKey):
|
|
||||||
global kindleDatabase
|
|
||||||
global charMap1
|
|
||||||
global charMap2
|
|
||||||
encryptedValue = decode(kindleDatabase[hashedKey],charMap2)
|
|
||||||
if sys.platform.startswith('win'):
|
|
||||||
return CryptUnprotectData(encryptedValue,"")
|
|
||||||
else:
|
|
||||||
cleartext = CryptUnprotectData(encryptedValue)
|
|
||||||
return decode(cleartext, charMap1)
|
|
||||||
|
|
||||||
# Get a record from the Kindle.info file for the string in "key" (plaintext).
|
|
||||||
# Return the decoded and decrypted record
|
|
||||||
def getKindleInfoValueForKey(key):
|
|
||||||
global charMap2
|
|
||||||
return getKindleInfoValueForHash(encodeHash(key,charMap2))
|
|
||||||
|
|
||||||
# Find if the original string for a hashed/encoded string is known.
|
|
||||||
# If so return the original string othwise return an empty string.
|
|
||||||
def findNameForHash(hash):
|
|
||||||
global charMap2
|
|
||||||
names = ["kindle.account.tokens","kindle.cookie.item","eulaVersionAccepted","login_date","kindle.token.item","login","kindle.key.item","kindle.name.info","kindle.device.info", "MazamaRandomNumber"]
|
|
||||||
result = ""
|
|
||||||
for name in names:
|
|
||||||
if hash == encodeHash(name, charMap2):
|
|
||||||
result = name
|
|
||||||
break
|
|
||||||
return result
|
|
||||||
|
|
||||||
# Print all the records from the kindle.info file (option -i)
|
|
||||||
def printKindleInfo():
|
|
||||||
for record in kindleDatabase:
|
|
||||||
name = findNameForHash(record)
|
|
||||||
if name != "" :
|
|
||||||
print (name)
|
|
||||||
print ("--------------------------")
|
|
||||||
else :
|
|
||||||
print ("Unknown Record")
|
|
||||||
print getKindleInfoValueForHash(record)
|
|
||||||
print "\n"
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# PID generation routines
|
# PID generation routines
|
||||||
#
|
#
|
||||||
|
@ -222,15 +191,15 @@ def getKindlePid(pidlst, rec209, token, serialnum):
|
||||||
return pidlst
|
return pidlst
|
||||||
|
|
||||||
|
|
||||||
# Parse the EXTH header records and parse the Kindleinfo
|
# parse the Kindleinfo file to calculate the book pid.
|
||||||
# file to calculate the book pid.
|
|
||||||
|
keynames = ["kindle.account.tokens","kindle.cookie.item","eulaVersionAccepted","login_date","kindle.token.item","login","kindle.key.item","kindle.name.info","kindle.device.info", "MazamaRandomNumber"]
|
||||||
|
|
||||||
def getK4Pids(pidlst, rec209, token, kInfoFile):
|
def getK4Pids(pidlst, rec209, token, kInfoFile):
|
||||||
global kindleDatabase
|
|
||||||
global charMap1
|
global charMap1
|
||||||
kindleDatabase = None
|
kindleDatabase = None
|
||||||
try:
|
try:
|
||||||
kindleDatabase = parseKindleInfo(kInfoFile)
|
kindleDatabase = getDBfromFile(kInfoFile)
|
||||||
except Exception, message:
|
except Exception, message:
|
||||||
print(message)
|
print(message)
|
||||||
kindleDatabase = None
|
kindleDatabase = None
|
||||||
|
@ -241,10 +210,10 @@ def getK4Pids(pidlst, rec209, token, kInfoFile):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Get the Mazama Random number
|
# Get the Mazama Random number
|
||||||
MazamaRandomNumber = getKindleInfoValueForKey("MazamaRandomNumber")
|
MazamaRandomNumber = kindleDatabase["MazamaRandomNumber"]
|
||||||
|
|
||||||
# Get the kindle account token
|
# Get the kindle account token
|
||||||
kindleAccountToken = getKindleInfoValueForKey("kindle.account.tokens")
|
kindleAccountToken = kindleDatabase["kindle.account.tokens"]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
print "Keys not found in " + kInfoFile
|
print "Keys not found in " + kInfoFile
|
||||||
return pidlst
|
return pidlst
|
||||||
|
|
|
@ -51,8 +51,9 @@
|
||||||
# 0.29 - It seems that the ideas about when multibyte trailing characters were
|
# 0.29 - It seems that the ideas about when multibyte trailing characters were
|
||||||
# included in the encryption were wrong. They aren't for DOC compressed
|
# included in the encryption were wrong. They aren't for DOC compressed
|
||||||
# files, but they are for HUFF/CDIC compress files!
|
# files, but they are for HUFF/CDIC compress files!
|
||||||
|
# 0.30 - Modified interface slightly to work better with new calibre plugin style
|
||||||
|
|
||||||
__version__ = '0.29'
|
__version__ = '0.30'
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
@ -163,6 +164,7 @@ class MobiBook:
|
||||||
def __init__(self, infile):
|
def __init__(self, infile):
|
||||||
# initial sanity check on file
|
# initial sanity check on file
|
||||||
self.data_file = file(infile, 'rb').read()
|
self.data_file = file(infile, 'rb').read()
|
||||||
|
self.mobi_data = ''
|
||||||
self.header = self.data_file[0:78]
|
self.header = self.data_file[0:78]
|
||||||
if self.header[0x3C:0x3C+8] != 'BOOKMOBI' and self.header[0x3C:0x3C+8] != 'TEXtREAd':
|
if self.header[0x3C:0x3C+8] != 'BOOKMOBI' and self.header[0x3C:0x3C+8] != 'TEXtREAd':
|
||||||
raise DrmException("invalid file format")
|
raise DrmException("invalid file format")
|
||||||
|
@ -301,13 +303,17 @@ class MobiBook:
|
||||||
break
|
break
|
||||||
return [found_key,pid]
|
return [found_key,pid]
|
||||||
|
|
||||||
|
def getMobiFile(self, outpath):
|
||||||
|
file(outpath,'wb').write(self.mobi_data)
|
||||||
|
|
||||||
def processBook(self, pidlist):
|
def processBook(self, pidlist):
|
||||||
crypto_type, = struct.unpack('>H', self.sect[0xC:0xC+2])
|
crypto_type, = struct.unpack('>H', self.sect[0xC:0xC+2])
|
||||||
print 'Crypto Type is: ', crypto_type
|
print 'Crypto Type is: ', crypto_type
|
||||||
self.crypto_type = crypto_type
|
self.crypto_type = crypto_type
|
||||||
if crypto_type == 0:
|
if crypto_type == 0:
|
||||||
print "This book is not encrypted."
|
print "This book is not encrypted."
|
||||||
return self.data_file
|
self.mobi_data = self.data_file
|
||||||
|
return
|
||||||
if crypto_type != 2 and crypto_type != 1:
|
if crypto_type != 2 and crypto_type != 1:
|
||||||
raise DrmException("Cannot decode unknown Mobipocket encryption type %d" % crypto_type)
|
raise DrmException("Cannot decode unknown Mobipocket encryption type %d" % crypto_type)
|
||||||
|
|
||||||
|
@ -353,33 +359,35 @@ class MobiBook:
|
||||||
|
|
||||||
# decrypt sections
|
# decrypt sections
|
||||||
print "Decrypting. Please wait . . .",
|
print "Decrypting. Please wait . . .",
|
||||||
new_data = self.data_file[:self.sections[1][0]]
|
self.mobi_data = self.data_file[:self.sections[1][0]]
|
||||||
for i in xrange(1, self.records+1):
|
for i in xrange(1, self.records+1):
|
||||||
data = self.loadSection(i)
|
data = self.loadSection(i)
|
||||||
extra_size = getSizeOfTrailingDataEntries(data, len(data), self.extra_data_flags)
|
extra_size = getSizeOfTrailingDataEntries(data, len(data), self.extra_data_flags)
|
||||||
if i%100 == 0:
|
if i%100 == 0:
|
||||||
print ".",
|
print ".",
|
||||||
# print "record %d, extra_size %d" %(i,extra_size)
|
# print "record %d, extra_size %d" %(i,extra_size)
|
||||||
new_data += PC1(found_key, data[0:len(data) - extra_size])
|
self.mobi_data += PC1(found_key, data[0:len(data) - extra_size])
|
||||||
if extra_size > 0:
|
if extra_size > 0:
|
||||||
new_data += data[-extra_size:]
|
self.mobi_data += data[-extra_size:]
|
||||||
if self.num_sections > self.records+1:
|
if self.num_sections > self.records+1:
|
||||||
new_data += self.data_file[self.sections[self.records+1][0]:]
|
self.mobi_data += self.data_file[self.sections[self.records+1][0]:]
|
||||||
self.data_file = new_data
|
|
||||||
print "done"
|
print "done"
|
||||||
return self.data_file
|
return
|
||||||
|
|
||||||
def getUnencryptedBook(infile,pid):
|
def getUnencryptedBook(infile,pid):
|
||||||
if not os.path.isfile(infile):
|
if not os.path.isfile(infile):
|
||||||
raise DrmException('Input File Not Found')
|
raise DrmException('Input File Not Found')
|
||||||
book = MobiBook(infile)
|
book = MobiBook(infile)
|
||||||
return book.processBook([pid])
|
book.processBook([pid])
|
||||||
|
return book.mobi_data
|
||||||
|
|
||||||
def getUnencryptedBookWithList(infile,pidlist):
|
def getUnencryptedBookWithList(infile,pidlist):
|
||||||
if not os.path.isfile(infile):
|
if not os.path.isfile(infile):
|
||||||
raise DrmException('Input File Not Found')
|
raise DrmException('Input File Not Found')
|
||||||
book = MobiBook(infile)
|
book = MobiBook(infile)
|
||||||
return book.processBook(pidlist)
|
book.processBook(pidlist)
|
||||||
|
return book.mobi_data
|
||||||
|
|
||||||
|
|
||||||
def main(argv=sys.argv):
|
def main(argv=sys.argv):
|
||||||
print ('MobiDeDrm v%(__version__)s. '
|
print ('MobiDeDrm v%(__version__)s. '
|
||||||
|
|
|
@ -10,7 +10,12 @@ class Unbuffered:
|
||||||
return getattr(self.stream, attr)
|
return getattr(self.stream, attr)
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
sys.stdout=Unbuffered(sys.stdout)
|
|
||||||
|
if 'calibre' in sys.modules:
|
||||||
|
inCalibre = True
|
||||||
|
else:
|
||||||
|
inCalibre = False
|
||||||
|
|
||||||
import os, csv, getopt
|
import os, csv, getopt
|
||||||
import zlib, zipfile, tempfile, shutil
|
import zlib, zipfile, tempfile, shutil
|
||||||
from struct import pack
|
from struct import pack
|
||||||
|
@ -18,10 +23,32 @@ from struct import unpack
|
||||||
|
|
||||||
class TpzDRMError(Exception):
|
class TpzDRMError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
# local support routines
|
# local support routines
|
||||||
import kgenpids
|
if inCalibre:
|
||||||
import genbook
|
from calibre_plugins.k4mobidedrm import kgenpids
|
||||||
|
from calibre_plugins.k4mobidedrm import genbook
|
||||||
|
else:
|
||||||
|
import kgenpids
|
||||||
|
import genbook
|
||||||
|
|
||||||
|
|
||||||
|
# recursive zip creation support routine
|
||||||
|
def zipUpDir(myzip, tdir, localname):
|
||||||
|
currentdir = tdir
|
||||||
|
if localname != "":
|
||||||
|
currentdir = os.path.join(currentdir,localname)
|
||||||
|
list = os.listdir(currentdir)
|
||||||
|
for file in list:
|
||||||
|
afilename = file
|
||||||
|
localfilePath = os.path.join(localname, afilename)
|
||||||
|
realfilePath = os.path.join(currentdir,file)
|
||||||
|
if os.path.isfile(realfilePath):
|
||||||
|
myzip.write(realfilePath, localfilePath)
|
||||||
|
elif os.path.isdir(realfilePath):
|
||||||
|
zipUpDir(myzip, tdir, localfilePath)
|
||||||
|
|
||||||
#
|
#
|
||||||
# Utility routines
|
# Utility routines
|
||||||
#
|
#
|
||||||
|
@ -110,9 +137,9 @@ def decryptDkeyRecords(data,PID):
|
||||||
|
|
||||||
|
|
||||||
class TopazBook:
|
class TopazBook:
|
||||||
def __init__(self, filename, outdir):
|
def __init__(self, filename):
|
||||||
self.fo = file(filename, 'rb')
|
self.fo = file(filename, 'rb')
|
||||||
self.outdir = outdir
|
self.outdir = tempfile.mkdtemp()
|
||||||
self.bookPayloadOffset = 0
|
self.bookPayloadOffset = 0
|
||||||
self.bookHeaderRecords = {}
|
self.bookHeaderRecords = {}
|
||||||
self.bookMetadata = {}
|
self.bookMetadata = {}
|
||||||
|
@ -317,21 +344,33 @@ class TopazBook:
|
||||||
file(outputFile, 'wb').write(record)
|
file(outputFile, 'wb').write(record)
|
||||||
print " "
|
print " "
|
||||||
|
|
||||||
|
def getHTMLZip(self, zipname):
|
||||||
|
htmlzip = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
||||||
|
htmlzip.write(os.path.join(self.outdir,'book.html'),'book.html')
|
||||||
|
htmlzip.write(os.path.join(self.outdir,'book.opf'),'book.opf')
|
||||||
|
if os.path.isfile(os.path.join(self.outdir,'cover.jpg')):
|
||||||
|
htmlzip.write(os.path.join(self.outdir,'cover.jpg'),'cover.jpg')
|
||||||
|
htmlzip.write(os.path.join(self.outdir,'style.css'),'style.css')
|
||||||
|
zipUpDir(htmlzip, self.outdir, 'img')
|
||||||
|
htmlzip.close()
|
||||||
|
|
||||||
def zipUpDir(myzip, tempdir,localname):
|
def getSVGZip(self, zipname):
|
||||||
currentdir = tempdir
|
svgzip = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
||||||
if localname != "":
|
svgzip.write(os.path.join(self.outdir,'index_svg.xhtml'),'index_svg.xhtml')
|
||||||
currentdir = os.path.join(currentdir,localname)
|
zipUpDir(svgzip, self.outdir, 'svg')
|
||||||
list = os.listdir(currentdir)
|
zipUpDir(svgzip, self.outdir, 'img')
|
||||||
for file in list:
|
svgzip.close()
|
||||||
afilename = file
|
|
||||||
localfilePath = os.path.join(localname, afilename)
|
|
||||||
realfilePath = os.path.join(currentdir,file)
|
|
||||||
if os.path.isfile(realfilePath):
|
|
||||||
myzip.write(realfilePath, localfilePath)
|
|
||||||
elif os.path.isdir(realfilePath):
|
|
||||||
zipUpDir(myzip, tempdir, localfilePath)
|
|
||||||
|
|
||||||
|
def getXMLZip(self, zipname):
|
||||||
|
xmlzip = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
||||||
|
targetdir = os.path.join(self.outdir,'xml')
|
||||||
|
zipUpDir(xmlzip, targetdir, '')
|
||||||
|
zipUpDir(xmlzip, self.outdir, 'img')
|
||||||
|
xmlzip.close()
|
||||||
|
|
||||||
|
def cleanup(self):
|
||||||
|
if os.path.isdir(self.outdir):
|
||||||
|
shutil.rmtree(self.outdir, True)
|
||||||
|
|
||||||
def usage(progname):
|
def usage(progname):
|
||||||
print "Removes DRM protection from Topaz ebooks and extract the contents"
|
print "Removes DRM protection from Topaz ebooks and extract the contents"
|
||||||
|
@ -383,58 +422,46 @@ def main(argv=sys.argv):
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
bookname = os.path.splitext(os.path.basename(infile))[0]
|
bookname = os.path.splitext(os.path.basename(infile))[0]
|
||||||
tempdir = tempfile.mkdtemp()
|
|
||||||
|
|
||||||
tb = TopazBook(infile, tempdir)
|
tb = TopazBook(infile)
|
||||||
title = tb.getBookTitle()
|
title = tb.getBookTitle()
|
||||||
print "Processing Book: ", title
|
print "Processing Book: ", title
|
||||||
keysRecord, keysRecordRecord = tb.getPIDMetaInfo()
|
keysRecord, keysRecordRecord = tb.getPIDMetaInfo()
|
||||||
pidlst = kgenpids.getPidList(keysRecord, keysRecordRecord, k4, pids, serials, kInfoFiles)
|
pidlst = kgenpids.getPidList(keysRecord, keysRecordRecord, k4, pids, serials, kInfoFiles)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
print "Decrypting Book"
|
||||||
tb.processBook(pidlst)
|
tb.processBook(pidlst)
|
||||||
|
|
||||||
|
print " Creating HTML ZIP Archive"
|
||||||
|
zipname = os.path.join(outdir, bookname + '_nodrm' + '.htmlz')
|
||||||
|
tb.getHTMLZip(zipname)
|
||||||
|
|
||||||
|
print " Creating SVG ZIP Archive"
|
||||||
|
zipname = os.path.join(outdir, bookname + '_SVG' + '.htmlz')
|
||||||
|
tb.getSVGZip(zipname)
|
||||||
|
|
||||||
|
print " Creating XML ZIP Archive"
|
||||||
|
zipname = os.path.join(outdir, bookname + '_XML' + '.zip')
|
||||||
|
tb.getXMLZip(zipname)
|
||||||
|
|
||||||
|
# removing internal temporary directory of pieces
|
||||||
|
tb.cleanup()
|
||||||
|
|
||||||
except TpzDRMError, e:
|
except TpzDRMError, e:
|
||||||
print str(e)
|
print str(e)
|
||||||
print " Creating DeBug Full Zip Archive of Book"
|
tb.cleanup()
|
||||||
zipname = os.path.join(outdir, bookname + '_debug' + '.zip')
|
|
||||||
myzip = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
|
||||||
zipUpDir(myzip, tempdir, '')
|
|
||||||
myzip.close()
|
|
||||||
shutil.rmtree(tempdir, True)
|
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
print " Creating HTML ZIP Archive"
|
except Exception, e:
|
||||||
zipname = os.path.join(outdir, bookname + '_nodrm' + '.zip')
|
print str(e)
|
||||||
myzip1 = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
tb.cleanup
|
||||||
myzip1.write(os.path.join(tempdir,'book.html'),'book.html')
|
return 1
|
||||||
myzip1.write(os.path.join(tempdir,'book.opf'),'book.opf')
|
|
||||||
if os.path.isfile(os.path.join(tempdir,'cover.jpg')):
|
|
||||||
myzip1.write(os.path.join(tempdir,'cover.jpg'),'cover.jpg')
|
|
||||||
myzip1.write(os.path.join(tempdir,'style.css'),'style.css')
|
|
||||||
zipUpDir(myzip1, tempdir, 'img')
|
|
||||||
myzip1.close()
|
|
||||||
|
|
||||||
print " Creating SVG ZIP Archive"
|
|
||||||
zipname = os.path.join(outdir, bookname + '_SVG' + '.zip')
|
|
||||||
myzip2 = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
|
||||||
myzip2.write(os.path.join(tempdir,'index_svg.xhtml'),'index_svg.xhtml')
|
|
||||||
zipUpDir(myzip2, tempdir, 'svg')
|
|
||||||
zipUpDir(myzip2, tempdir, 'img')
|
|
||||||
myzip2.close()
|
|
||||||
|
|
||||||
print " Creating XML ZIP Archive"
|
|
||||||
zipname = os.path.join(outdir, bookname + '_XML' + '.zip')
|
|
||||||
myzip3 = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
|
||||||
targetdir = os.path.join(tempdir,'xml')
|
|
||||||
zipUpDir(myzip3, targetdir, '')
|
|
||||||
zipUpDir(myzip3, tempdir, 'img')
|
|
||||||
myzip3.close()
|
|
||||||
|
|
||||||
shutil.rmtree(tempdir, True)
|
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
sys.stdout=Unbuffered(sys.stdout)
|
||||||
sys.exit(main())
|
sys.exit(main())
|
||||||
|
|
||||||
|
|
|
@ -124,14 +124,17 @@ class PrefsDialog(Toplevel):
|
||||||
button = Tkinter.Button(body, text="...", command=self.get_bnkpath)
|
button = Tkinter.Button(body, text="...", command=self.get_bnkpath)
|
||||||
button.grid(row=1, column=2)
|
button.grid(row=1, column=2)
|
||||||
|
|
||||||
Tkinter.Label(body, text='Additional kindle.info file').grid(row=2, sticky=Tkconstants.E)
|
Tkinter.Label(body, text='Additional kindle.info or .kinf file').grid(row=2, sticky=Tkconstants.E)
|
||||||
self.altinfopath = Tkinter.Entry(body, width=50)
|
self.altinfopath = Tkinter.Entry(body, width=50)
|
||||||
self.altinfopath.grid(row=2, column=1, sticky=sticky)
|
self.altinfopath.grid(row=2, column=1, sticky=sticky)
|
||||||
prefdir = self.prefs_array['dir']
|
prefdir = self.prefs_array['dir']
|
||||||
infofile = os.path.join(prefdir,'kindle.info')
|
|
||||||
path = ''
|
path = ''
|
||||||
|
infofile = os.path.join(prefdir,'kindle.info')
|
||||||
|
ainfofile = os.path.join(prefdir,'.kinf')
|
||||||
if os.path.isfile(infofile):
|
if os.path.isfile(infofile):
|
||||||
path = infofile
|
path = infofile
|
||||||
|
elif os.path.isfile(ainfofile):
|
||||||
|
path = ainfofile
|
||||||
path = path.encode('utf-8')
|
path = path.encode('utf-8')
|
||||||
self.altinfopath.insert(0, path)
|
self.altinfopath.insert(0, path)
|
||||||
button = Tkinter.Button(body, text="...", command=self.get_altinfopath)
|
button = Tkinter.Button(body, text="...", command=self.get_altinfopath)
|
||||||
|
@ -245,8 +248,8 @@ class PrefsDialog(Toplevel):
|
||||||
|
|
||||||
def get_altinfopath(self):
|
def get_altinfopath(self):
|
||||||
cpath = self.altinfopath.get()
|
cpath = self.altinfopath.get()
|
||||||
altinfopath = tkFileDialog.askopenfilename(parent=None, title='Select Alternative kindle.info File',
|
altinfopath = tkFileDialog.askopenfilename(parent=None, title='Select Alternative kindle.info or .kinf File',
|
||||||
defaultextension='.info', filetypes=[('Kindle Info', '.info'),('All Files', '.*')],
|
defaultextension='.info', filetypes=[('Kindle Info', '.info'),('Kindle KInf','.kinf')('All Files', '.*')],
|
||||||
initialdir=cpath)
|
initialdir=cpath)
|
||||||
if altinfopath:
|
if altinfopath:
|
||||||
altinfopath = os.path.normpath(altinfopath)
|
altinfopath = os.path.normpath(altinfopath)
|
||||||
|
@ -457,8 +460,7 @@ class ConvDialog(Toplevel):
|
||||||
name, ext = os.path.splitext(os.path.basename(infile))
|
name, ext = os.path.splitext(os.path.basename(infile))
|
||||||
ext = ext.lower()
|
ext = ext.lower()
|
||||||
if ext == '.epub':
|
if ext == '.epub':
|
||||||
outfile = os.path.join(outdir, name + '_nodrm.epub')
|
self.p2 = processEPUB(apphome, infile, outdir, rscpath)
|
||||||
self.p2 = processEPUB(apphome, infile, outfile, rscpath)
|
|
||||||
return 0
|
return 0
|
||||||
if ext == '.pdb':
|
if ext == '.pdb':
|
||||||
self.p2 = processPDB(apphome, infile, outdir, rscpath)
|
self.p2 = processPDB(apphome, infile, outdir, rscpath)
|
||||||
|
@ -467,8 +469,7 @@ class ConvDialog(Toplevel):
|
||||||
self.p2 = processK4MOBI(apphome, infile, outdir, rscpath)
|
self.p2 = processK4MOBI(apphome, infile, outdir, rscpath)
|
||||||
return 0
|
return 0
|
||||||
if ext == '.pdf':
|
if ext == '.pdf':
|
||||||
outfile = os.path.join(outdir, name + '_nodrm.pdf')
|
self.p2 = processPDF(apphome, infile, outdir, rscpath)
|
||||||
self.p2 = processPDF(apphome, infile, outfile, rscpath)
|
|
||||||
return 0
|
return 0
|
||||||
return rv
|
return rv
|
||||||
|
|
||||||
|
@ -506,7 +507,7 @@ def processK4MOBI(apphome, infile, outdir, rscpath):
|
||||||
parms += '-s "' + serialnums + '" '
|
parms += '-s "' + serialnums + '" '
|
||||||
|
|
||||||
files = os.listdir(rscpath)
|
files = os.listdir(rscpath)
|
||||||
filefilter = re.compile("\.info$", re.IGNORECASE)
|
filefilter = re.compile("\.info$|\.kinf$", re.IGNORECASE)
|
||||||
files = filter(filefilter.search, files)
|
files = filter(filefilter.search, files)
|
||||||
if files:
|
if files:
|
||||||
for filename in files:
|
for filename in files:
|
||||||
|
@ -516,16 +517,16 @@ def processK4MOBI(apphome, infile, outdir, rscpath):
|
||||||
p2 = runit(apphome, cmd, parms)
|
p2 = runit(apphome, cmd, parms)
|
||||||
return p2
|
return p2
|
||||||
|
|
||||||
def processPDF(apphome, infile, outfile, rscpath):
|
def processPDF(apphome, infile, outdir, rscpath):
|
||||||
cmd = os.path.join('lib','decryptpdf.py')
|
cmd = os.path.join('lib','decryptpdf.py')
|
||||||
parms = '"' + infile + '" "' + outfile + '" "' + rscpath + '"'
|
parms = '"' + infile + '" "' + outdir + '" "' + rscpath + '"'
|
||||||
p2 = runit(apphome, cmd, parms)
|
p2 = runit(apphome, cmd, parms)
|
||||||
return p2
|
return p2
|
||||||
|
|
||||||
def processEPUB(apphome, infile, outfile, rscpath):
|
def processEPUB(apphome, infile, outdir, rscpath):
|
||||||
# invoke routine to check both Adept and Barnes and Noble
|
# invoke routine to check both Adept and Barnes and Noble
|
||||||
cmd = os.path.join('lib','decryptepub.py')
|
cmd = os.path.join('lib','decryptepub.py')
|
||||||
parms = '"' + infile + '" "' + outfile + '" "' + rscpath + '"'
|
parms = '"' + infile + '" "' + outdir + '" "' + rscpath + '"'
|
||||||
p2 = runit(apphome, cmd, parms)
|
p2 = runit(apphome, cmd, parms)
|
||||||
return p2
|
return p2
|
||||||
|
|
||||||
|
|
|
@ -24,7 +24,7 @@ def main(argv=sys.argv):
|
||||||
if len(args) != 3:
|
if len(args) != 3:
|
||||||
return -1
|
return -1
|
||||||
infile = args[0]
|
infile = args[0]
|
||||||
outfile = args[1]
|
outdir = args[1]
|
||||||
rscpath = args[2]
|
rscpath = args[2]
|
||||||
errlog = ''
|
errlog = ''
|
||||||
|
|
||||||
|
@ -37,6 +37,9 @@ def main(argv=sys.argv):
|
||||||
print "Error while trying to fix epub"
|
print "Error while trying to fix epub"
|
||||||
return rv
|
return rv
|
||||||
|
|
||||||
|
# determine a good name for the output file
|
||||||
|
outfile = os.path.join(outdir, name + '_nodrm.epub')
|
||||||
|
|
||||||
rv = 1
|
rv = 1
|
||||||
# first try with the Adobe adept epub
|
# first try with the Adobe adept epub
|
||||||
# try with any keyfiles (*.der) in the rscpath
|
# try with any keyfiles (*.der) in the rscpath
|
||||||
|
|
|
@ -21,10 +21,15 @@ def main(argv=sys.argv):
|
||||||
if len(args) != 3:
|
if len(args) != 3:
|
||||||
return -1
|
return -1
|
||||||
infile = args[0]
|
infile = args[0]
|
||||||
outfile = args[1]
|
outdir = args[1]
|
||||||
rscpath = args[2]
|
rscpath = args[2]
|
||||||
errlog = ''
|
errlog = ''
|
||||||
rv = 1
|
rv = 1
|
||||||
|
|
||||||
|
# determine a good name for the output file
|
||||||
|
name, ext = os.path.splitext(os.path.basename(infile))
|
||||||
|
outfile = os.path.join(outdir, name + '_nodrm.pdf')
|
||||||
|
|
||||||
# try with any keyfiles (*.der) in the rscpath
|
# try with any keyfiles (*.der) in the rscpath
|
||||||
files = os.listdir(rscpath)
|
files = os.listdir(rscpath)
|
||||||
filefilter = re.compile("\.der$", re.IGNORECASE)
|
filefilter = re.compile("\.der$", re.IGNORECASE)
|
||||||
|
|
|
@ -58,8 +58,9 @@
|
||||||
# 0.17 - added support for pycrypto's DES as well
|
# 0.17 - added support for pycrypto's DES as well
|
||||||
# 0.18 - on Windows try PyCrypto first and OpenSSL next
|
# 0.18 - on Windows try PyCrypto first and OpenSSL next
|
||||||
# 0.19 - Modify the interface to allow use of import
|
# 0.19 - Modify the interface to allow use of import
|
||||||
|
# 0.20 - modify to allow use inside new interface for calibre plugins
|
||||||
|
|
||||||
__version__='0.19'
|
__version__='0.20'
|
||||||
|
|
||||||
class Unbuffered:
|
class Unbuffered:
|
||||||
def __init__(self, stream):
|
def __init__(self, stream):
|
||||||
|
@ -71,32 +72,50 @@ class Unbuffered:
|
||||||
return getattr(self.stream, attr)
|
return getattr(self.stream, attr)
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
sys.stdout=Unbuffered(sys.stdout)
|
|
||||||
|
|
||||||
import struct, binascii, getopt, zlib, os, os.path, urllib, tempfile
|
import struct, binascii, getopt, zlib, os, os.path, urllib, tempfile
|
||||||
|
|
||||||
|
if 'calibre' in sys.modules:
|
||||||
|
inCalibre = True
|
||||||
|
else:
|
||||||
|
inCalibre = False
|
||||||
|
|
||||||
Des = None
|
Des = None
|
||||||
if sys.platform.startswith('win'):
|
if sys.platform.startswith('win'):
|
||||||
# first try with pycrypto
|
# first try with pycrypto
|
||||||
import pycrypto_des
|
if inCalibre:
|
||||||
|
from calibre_plugins.erdrpdb2pml import pycrypto_des
|
||||||
|
else:
|
||||||
|
import pycrypto_des
|
||||||
Des = pycrypto_des.load_pycrypto()
|
Des = pycrypto_des.load_pycrypto()
|
||||||
if Des == None:
|
if Des == None:
|
||||||
# they try with openssl
|
# they try with openssl
|
||||||
import openssl_des
|
if inCalibre:
|
||||||
|
from calibre_plugins.erdrpdb2pml import openssl_des
|
||||||
|
else:
|
||||||
|
import openssl_des
|
||||||
Des = openssl_des.load_libcrypto()
|
Des = openssl_des.load_libcrypto()
|
||||||
else:
|
else:
|
||||||
# first try with openssl
|
# first try with openssl
|
||||||
import openssl_des
|
if inCalibre:
|
||||||
|
from calibre_plugins.erdrpdb2pml import openssl_des
|
||||||
|
else:
|
||||||
|
import openssl_des
|
||||||
Des = openssl_des.load_libcrypto()
|
Des = openssl_des.load_libcrypto()
|
||||||
if Des == None:
|
if Des == None:
|
||||||
# then try with pycrypto
|
# then try with pycrypto
|
||||||
import pycrypto_des
|
if inCalibre:
|
||||||
|
from calibre_plugins.erdrpdb2pml import pycrypto_des
|
||||||
|
else:
|
||||||
|
import pycrypto_des
|
||||||
Des = pycrypto_des.load_pycrypto()
|
Des = pycrypto_des.load_pycrypto()
|
||||||
|
|
||||||
# if that did not work then use pure python implementation
|
# if that did not work then use pure python implementation
|
||||||
# of DES and try to speed it up with Psycho
|
# of DES and try to speed it up with Psycho
|
||||||
if Des == None:
|
if Des == None:
|
||||||
import python_des
|
if inCalibre:
|
||||||
|
from calibre_plugins.erdrpdb2pml import python_des
|
||||||
|
else:
|
||||||
|
import python_des
|
||||||
Des = python_des.Des
|
Des = python_des.Des
|
||||||
# Import Psyco if available
|
# Import Psyco if available
|
||||||
try:
|
try:
|
||||||
|
@ -480,5 +499,6 @@ def main(argv=None):
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
sys.stdout=Unbuffered(sys.stdout)
|
||||||
sys.exit(main())
|
sys.exit(main())
|
||||||
|
|
||||||
|
|
|
@ -21,10 +21,21 @@ from struct import unpack
|
||||||
|
|
||||||
|
|
||||||
# local support routines
|
# local support routines
|
||||||
import convert2xml
|
if 'calibre' in sys.modules:
|
||||||
import flatxml2html
|
inCalibre = True
|
||||||
import flatxml2svg
|
else:
|
||||||
import stylexml2css
|
inCalibre = False
|
||||||
|
|
||||||
|
if inCalibre :
|
||||||
|
from calibre_plugins.k4mobidedrm import convert2xml
|
||||||
|
from calibre_plugins.k4mobidedrm import flatxml2html
|
||||||
|
from calibre_plugins.k4mobidedrm import flatxml2svg
|
||||||
|
from calibre_plugins.k4mobidedrm import stylexml2css
|
||||||
|
else :
|
||||||
|
import convert2xml
|
||||||
|
import flatxml2html
|
||||||
|
import flatxml2svg
|
||||||
|
import stylexml2css
|
||||||
|
|
||||||
|
|
||||||
# Get a 7 bit encoded number from a file
|
# Get a 7 bit encoded number from a file
|
||||||
|
@ -504,7 +515,7 @@ def generateBook(bookDir, raw, fixedimage):
|
||||||
opfstr += ' </metadata>\n'
|
opfstr += ' </metadata>\n'
|
||||||
opfstr += '<manifest>\n'
|
opfstr += '<manifest>\n'
|
||||||
opfstr += ' <item id="book" href="book.html" media-type="application/xhtml+xml"/>\n'
|
opfstr += ' <item id="book" href="book.html" media-type="application/xhtml+xml"/>\n'
|
||||||
opfstr += ' <item id="stylesheet" href="style.css" media-type="text.css"/>\n'
|
opfstr += ' <item id="stylesheet" href="style.css" media-type="text/css"/>\n'
|
||||||
# adding image files to manifest
|
# adding image files to manifest
|
||||||
filenames = os.listdir(imgDir)
|
filenames = os.listdir(imgDir)
|
||||||
filenames = sorted(filenames)
|
filenames = sorted(filenames)
|
||||||
|
|
|
@ -157,6 +157,7 @@ def _load_crypto_libcrypto():
|
||||||
return out.raw
|
return out.raw
|
||||||
|
|
||||||
class AES(object):
|
class AES(object):
|
||||||
|
MODE_CBC = 0
|
||||||
@classmethod
|
@classmethod
|
||||||
def new(cls, userkey, mode, iv):
|
def new(cls, userkey, mode, iv):
|
||||||
self = AES()
|
self = AES()
|
||||||
|
|
|
@ -16,20 +16,8 @@ from __future__ import with_statement
|
||||||
# unswindle, DarkReverser, ApprenticeAlf, DiapDealer, some_updates
|
# unswindle, DarkReverser, ApprenticeAlf, DiapDealer, some_updates
|
||||||
# and many many others
|
# and many many others
|
||||||
|
|
||||||
# It can run standalone to convert K4M/K4PC/Mobi files, or it can be installed as a
|
|
||||||
# plugin for Calibre (http://calibre-ebook.com/about) so that importing
|
|
||||||
# K4 or Mobi with DRM is no londer a multi-step process.
|
|
||||||
#
|
|
||||||
# ***NOTE*** If you are using this script as a calibre plugin for a K4M or K4PC ebook
|
|
||||||
# then calibre must be installed on the same machine and in the same account as K4PC or K4M
|
|
||||||
# for the plugin version to function properly.
|
|
||||||
#
|
|
||||||
# To create a Calibre plugin, rename this file so that the filename
|
|
||||||
# ends in '_plugin.py', put it into a ZIP file with all its supporting python routines
|
|
||||||
# and import that ZIP into Calibre using its plugin configuration GUI.
|
|
||||||
|
|
||||||
|
__version__ = '3.1'
|
||||||
__version__ = '2.8'
|
|
||||||
|
|
||||||
class Unbuffered:
|
class Unbuffered:
|
||||||
def __init__(self, stream):
|
def __init__(self, stream):
|
||||||
|
@ -43,11 +31,7 @@ class Unbuffered:
|
||||||
import sys
|
import sys
|
||||||
import os, csv, getopt
|
import os, csv, getopt
|
||||||
import string
|
import string
|
||||||
import binascii
|
|
||||||
import zlib
|
|
||||||
import re
|
import re
|
||||||
import zlib, zipfile, tempfile, shutil
|
|
||||||
from struct import pack, unpack, unpack_from
|
|
||||||
|
|
||||||
class DrmException(Exception):
|
class DrmException(Exception):
|
||||||
pass
|
pass
|
||||||
|
@ -57,19 +41,15 @@ if 'calibre' in sys.modules:
|
||||||
else:
|
else:
|
||||||
inCalibre = False
|
inCalibre = False
|
||||||
|
|
||||||
def zipUpDir(myzip, tempdir,localname):
|
if inCalibre:
|
||||||
currentdir = tempdir
|
from calibre_plugins.k4mobidedrm import mobidedrm
|
||||||
if localname != "":
|
from calibre_plugins.k4mobidedrm import topazextract
|
||||||
currentdir = os.path.join(currentdir,localname)
|
from calibre_plugins.k4mobidedrm import kgenpids
|
||||||
list = os.listdir(currentdir)
|
else:
|
||||||
for file in list:
|
import mobidedrm
|
||||||
afilename = file
|
import topazextract
|
||||||
localfilePath = os.path.join(localname, afilename)
|
import kgenpids
|
||||||
realfilePath = os.path.join(currentdir,file)
|
|
||||||
if os.path.isfile(realfilePath):
|
|
||||||
myzip.write(realfilePath, localfilePath)
|
|
||||||
elif os.path.isdir(realfilePath):
|
|
||||||
zipUpDir(myzip, tempdir, localfilePath)
|
|
||||||
|
|
||||||
# cleanup bytestring filenames
|
# cleanup bytestring filenames
|
||||||
# borrowed from calibre from calibre/src/calibre/__init__.py
|
# borrowed from calibre from calibre/src/calibre/__init__.py
|
||||||
|
@ -94,10 +74,6 @@ def cleanup_name(name):
|
||||||
return one
|
return one
|
||||||
|
|
||||||
def decryptBook(infile, outdir, k4, kInfoFiles, serials, pids):
|
def decryptBook(infile, outdir, k4, kInfoFiles, serials, pids):
|
||||||
import mobidedrm
|
|
||||||
import topazextract
|
|
||||||
import kgenpids
|
|
||||||
|
|
||||||
# handle the obvious cases at the beginning
|
# handle the obvious cases at the beginning
|
||||||
if not os.path.isfile(infile):
|
if not os.path.isfile(infile):
|
||||||
print "Error: Input file does not exist"
|
print "Error: Input file does not exist"
|
||||||
|
@ -113,8 +89,7 @@ def decryptBook(infile, outdir, k4, kInfoFiles, serials, pids):
|
||||||
if mobi:
|
if mobi:
|
||||||
mb = mobidedrm.MobiBook(infile)
|
mb = mobidedrm.MobiBook(infile)
|
||||||
else:
|
else:
|
||||||
tempdir = tempfile.mkdtemp()
|
mb = topazextract.TopazBook(infile)
|
||||||
mb = topazextract.TopazBook(infile, tempdir)
|
|
||||||
|
|
||||||
title = mb.getBookTitle()
|
title = mb.getBookTitle()
|
||||||
print "Processing Book: ", title
|
print "Processing Book: ", title
|
||||||
|
@ -128,60 +103,39 @@ def decryptBook(infile, outdir, k4, kInfoFiles, serials, pids):
|
||||||
pidlst = kgenpids.getPidList(md1, md2, k4, pids, serials, kInfoFiles)
|
pidlst = kgenpids.getPidList(md1, md2, k4, pids, serials, kInfoFiles)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if mobi:
|
mb.processBook(pidlst)
|
||||||
unlocked_file = mb.processBook(pidlst)
|
|
||||||
else:
|
|
||||||
mb.processBook(pidlst)
|
|
||||||
|
|
||||||
except mobidedrm.DrmException, e:
|
except mobidedrm.DrmException, e:
|
||||||
print "Error: " + str(e) + "\nDRM Removal Failed.\n"
|
print "Error: " + str(e) + "\nDRM Removal Failed.\n"
|
||||||
return 1
|
return 1
|
||||||
|
except topazextract.TpzDRMError, e:
|
||||||
|
print "Error: " + str(e) + "\nDRM Removal Failed.\n"
|
||||||
|
return 1
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
if not mobi:
|
print "Error: " + str(e) + "\nDRM Removal Failed.\n"
|
||||||
print "Error: " + str(e) + "\nDRM Removal Failed.\n"
|
return 1
|
||||||
print " Creating DeBug Full Zip Archive of Book"
|
|
||||||
zipname = os.path.join(outdir, bookname + '_debug' + '.zip')
|
|
||||||
myzip = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
|
||||||
zipUpDir(myzip, tempdir, '')
|
|
||||||
myzip.close()
|
|
||||||
shutil.rmtree(tempdir, True)
|
|
||||||
return 1
|
|
||||||
pass
|
|
||||||
|
|
||||||
if mobi:
|
if mobi:
|
||||||
outfile = os.path.join(outdir,outfilename + '_nodrm' + '.mobi')
|
outfile = os.path.join(outdir, outfilename + '_nodrm' + '.mobi')
|
||||||
file(outfile, 'wb').write(unlocked_file)
|
mb.getMobiFile(outfile)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
# topaz: build up zip archives of results
|
# topaz:
|
||||||
print " Creating HTML ZIP Archive"
|
print " Creating NoDRM HTMLZ Archive"
|
||||||
zipname = os.path.join(outdir, outfilename + '_nodrm' + '.zip')
|
zipname = os.path.join(outdir, outfilename + '_nodrm' + '.htmlz')
|
||||||
myzip1 = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
mb.getHTMLZip(zipname)
|
||||||
myzip1.write(os.path.join(tempdir,'book.html'),'book.html')
|
|
||||||
myzip1.write(os.path.join(tempdir,'book.opf'),'book.opf')
|
|
||||||
if os.path.isfile(os.path.join(tempdir,'cover.jpg')):
|
|
||||||
myzip1.write(os.path.join(tempdir,'cover.jpg'),'cover.jpg')
|
|
||||||
myzip1.write(os.path.join(tempdir,'style.css'),'style.css')
|
|
||||||
zipUpDir(myzip1, tempdir, 'img')
|
|
||||||
myzip1.close()
|
|
||||||
|
|
||||||
print " Creating SVG ZIP Archive"
|
print " Creating SVG HTMLZ Archive"
|
||||||
zipname = os.path.join(outdir, outfilename + '_SVG' + '.zip')
|
zipname = os.path.join(outdir, outfilename + '_SVG' + '.htmlz')
|
||||||
myzip2 = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
mb.getSVGZip(zipname)
|
||||||
myzip2.write(os.path.join(tempdir,'index_svg.xhtml'),'index_svg.xhtml')
|
|
||||||
zipUpDir(myzip2, tempdir, 'svg')
|
|
||||||
zipUpDir(myzip2, tempdir, 'img')
|
|
||||||
myzip2.close()
|
|
||||||
|
|
||||||
print " Creating XML ZIP Archive"
|
print " Creating XML ZIP Archive"
|
||||||
zipname = os.path.join(outdir, outfilename + '_XML' + '.zip')
|
zipname = os.path.join(outdir, outfilename + '_XML' + '.zip')
|
||||||
myzip3 = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
mb.getXMLZip(zipname)
|
||||||
targetdir = os.path.join(tempdir,'xml')
|
|
||||||
zipUpDir(myzip3, targetdir, '')
|
# remove internal temporary directory of Topaz pieces
|
||||||
zipUpDir(myzip3, tempdir, 'img')
|
mb.cleanup()
|
||||||
myzip3.close()
|
|
||||||
|
|
||||||
shutil.rmtree(tempdir, True)
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
@ -236,7 +190,6 @@ def main(argv=sys.argv):
|
||||||
kInfoFiles = None
|
kInfoFiles = None
|
||||||
infile = args[0]
|
infile = args[0]
|
||||||
outdir = args[1]
|
outdir = args[1]
|
||||||
|
|
||||||
return decryptBook(infile, outdir, k4, kInfoFiles, serials, pids)
|
return decryptBook(infile, outdir, k4, kInfoFiles, serials, pids)
|
||||||
|
|
||||||
|
|
||||||
|
@ -244,131 +197,3 @@ if __name__ == '__main__':
|
||||||
sys.stdout=Unbuffered(sys.stdout)
|
sys.stdout=Unbuffered(sys.stdout)
|
||||||
sys.exit(main())
|
sys.exit(main())
|
||||||
|
|
||||||
if not __name__ == "__main__" and inCalibre:
|
|
||||||
from calibre.customize import FileTypePlugin
|
|
||||||
|
|
||||||
class K4DeDRM(FileTypePlugin):
|
|
||||||
name = 'K4PC, K4Mac, Kindle Mobi and Topaz DeDRM' # Name of the plugin
|
|
||||||
description = 'Removes DRM from K4PC and Mac, Kindle Mobi and Topaz files. \
|
|
||||||
Provided by the work of many including DiapDealer, SomeUpdates, IHeartCabbages, CMBDTC, Skindle, DarkReverser, ApprenticeAlf, etc.'
|
|
||||||
supported_platforms = ['osx', 'windows', 'linux'] # Platforms this plugin will run on
|
|
||||||
author = 'DiapDealer, SomeUpdates' # The author of this plugin
|
|
||||||
version = (0, 2, 8) # The version number of this plugin
|
|
||||||
file_types = set(['prc','mobi','azw','azw1','tpz']) # The file types that this plugin will be applied to
|
|
||||||
on_import = True # Run this plugin during the import
|
|
||||||
priority = 210 # run this plugin before mobidedrm, k4pcdedrm, k4dedrm
|
|
||||||
|
|
||||||
def run(self, path_to_ebook):
|
|
||||||
from calibre.gui2 import is_ok_to_use_qt
|
|
||||||
from PyQt4.Qt import QMessageBox
|
|
||||||
from calibre.ptempfile import PersistentTemporaryDirectory
|
|
||||||
|
|
||||||
import kgenpids
|
|
||||||
import zlib
|
|
||||||
import zipfile
|
|
||||||
import topazextract
|
|
||||||
import mobidedrm
|
|
||||||
|
|
||||||
k4 = True
|
|
||||||
if sys.platform.startswith('linux'):
|
|
||||||
k4 = False
|
|
||||||
pids = []
|
|
||||||
serials = []
|
|
||||||
kInfoFiles = []
|
|
||||||
|
|
||||||
# Get supplied list of PIDs to try from plugin customization.
|
|
||||||
customvalues = self.site_customization.split(',')
|
|
||||||
for customvalue in customvalues:
|
|
||||||
customvalue = str(customvalue)
|
|
||||||
customvalue = customvalue.strip()
|
|
||||||
if len(customvalue) == 10 or len(customvalue) == 8:
|
|
||||||
pids.append(customvalue)
|
|
||||||
else :
|
|
||||||
if len(customvalue) == 16 and customvalue[0] == 'B':
|
|
||||||
serials.append(customvalue)
|
|
||||||
else:
|
|
||||||
print "%s is not a valid Kindle serial number or PID." % str(customvalue)
|
|
||||||
|
|
||||||
# Load any kindle info files (*.info) included Calibre's config directory.
|
|
||||||
try:
|
|
||||||
# Find Calibre's configuration directory.
|
|
||||||
confpath = os.path.split(os.path.split(self.plugin_path)[0])[0]
|
|
||||||
print 'K4MobiDeDRM: Calibre configuration directory = %s' % confpath
|
|
||||||
files = os.listdir(confpath)
|
|
||||||
filefilter = re.compile("\.info$", re.IGNORECASE)
|
|
||||||
files = filter(filefilter.search, files)
|
|
||||||
|
|
||||||
if files:
|
|
||||||
for filename in files:
|
|
||||||
fpath = os.path.join(confpath, filename)
|
|
||||||
kInfoFiles.append(fpath)
|
|
||||||
print 'K4MobiDeDRM: Kindle info file %s found in config folder.' % filename
|
|
||||||
except IOError:
|
|
||||||
print 'K4MobiDeDRM: Error reading kindle info files from config directory.'
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
mobi = True
|
|
||||||
magic3 = file(path_to_ebook,'rb').read(3)
|
|
||||||
if magic3 == 'TPZ':
|
|
||||||
mobi = False
|
|
||||||
|
|
||||||
bookname = os.path.splitext(os.path.basename(path_to_ebook))[0]
|
|
||||||
|
|
||||||
if mobi:
|
|
||||||
mb = mobidedrm.MobiBook(path_to_ebook)
|
|
||||||
else:
|
|
||||||
tempdir = PersistentTemporaryDirectory()
|
|
||||||
mb = topazextract.TopazBook(path_to_ebook, tempdir)
|
|
||||||
|
|
||||||
title = mb.getBookTitle()
|
|
||||||
md1, md2 = mb.getPIDMetaInfo()
|
|
||||||
pidlst = kgenpids.getPidList(md1, md2, k4, pids, serials, kInfoFiles)
|
|
||||||
|
|
||||||
try:
|
|
||||||
if mobi:
|
|
||||||
unlocked_file = mb.processBook(pidlst)
|
|
||||||
else:
|
|
||||||
mb.processBook(pidlst)
|
|
||||||
|
|
||||||
except mobidedrm.DrmException:
|
|
||||||
#if you reached here then no luck raise and exception
|
|
||||||
if is_ok_to_use_qt():
|
|
||||||
d = QMessageBox(QMessageBox.Warning, "K4MobiDeDRM Plugin", "Error decoding: %s\n" % path_to_ebook)
|
|
||||||
d.show()
|
|
||||||
d.raise_()
|
|
||||||
d.exec_()
|
|
||||||
raise Exception("K4MobiDeDRM plugin could not decode the file")
|
|
||||||
return ""
|
|
||||||
except topazextract.TpzDRMError:
|
|
||||||
#if you reached here then no luck raise and exception
|
|
||||||
if is_ok_to_use_qt():
|
|
||||||
d = QMessageBox(QMessageBox.Warning, "K4MobiDeDRM Plugin", "Error decoding: %s\n" % path_to_ebook)
|
|
||||||
d.show()
|
|
||||||
d.raise_()
|
|
||||||
d.exec_()
|
|
||||||
raise Exception("K4MobiDeDRM plugin could not decode the file")
|
|
||||||
return ""
|
|
||||||
|
|
||||||
print "Success!"
|
|
||||||
if mobi:
|
|
||||||
of = self.temporary_file(bookname+'.mobi')
|
|
||||||
of.write(unlocked_file)
|
|
||||||
of.close()
|
|
||||||
return of.name
|
|
||||||
|
|
||||||
# topaz: build up zip archives of results
|
|
||||||
print " Creating HTML ZIP Archive"
|
|
||||||
of = self.temporary_file(bookname + '.zip')
|
|
||||||
myzip = zipfile.ZipFile(of.name,'w',zipfile.ZIP_DEFLATED, False)
|
|
||||||
myzip.write(os.path.join(tempdir,'book.html'),'book.html')
|
|
||||||
myzip.write(os.path.join(tempdir,'book.opf'),'book.opf')
|
|
||||||
if os.path.isfile(os.path.join(tempdir,'cover.jpg')):
|
|
||||||
myzip.write(os.path.join(tempdir,'cover.jpg'),'cover.jpg')
|
|
||||||
myzip.write(os.path.join(tempdir,'style.css'),'style.css')
|
|
||||||
zipUpDir(myzip, tempdir, 'img')
|
|
||||||
myzip.close()
|
|
||||||
return of.name
|
|
||||||
|
|
||||||
def customization_help(self, gui=False):
|
|
||||||
return 'Enter 10 character PIDs and/or Kindle serial numbers, separated by commas.'
|
|
|
@ -1,10 +1,12 @@
|
||||||
# standlone set of Mac OSX specific routines needed for K4DeDRM
|
# standlone set of Mac OSX specific routines needed for K4DeDRM
|
||||||
|
|
||||||
from __future__ import with_statement
|
from __future__ import with_statement
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
|
from struct import pack, unpack, unpack_from
|
||||||
|
|
||||||
class DrmException(Exception):
|
class DrmException(Exception):
|
||||||
pass
|
pass
|
||||||
|
@ -66,9 +68,8 @@ def _load_crypto_libcrypto():
|
||||||
raise DrmException('AES decryption failed')
|
raise DrmException('AES decryption failed')
|
||||||
return out.raw
|
return out.raw
|
||||||
|
|
||||||
def keyivgen(self, passwd):
|
def keyivgen(self, passwd, salt):
|
||||||
salt = '16743'
|
saltlen = len(salt)
|
||||||
saltlen = 5
|
|
||||||
passlen = len(passwd)
|
passlen = len(passwd)
|
||||||
iter = 0x3e8
|
iter = 0x3e8
|
||||||
keylen = 80
|
keylen = 80
|
||||||
|
@ -91,12 +92,78 @@ LibCrypto = _load_crypto()
|
||||||
# Utility Routines
|
# Utility Routines
|
||||||
#
|
#
|
||||||
|
|
||||||
|
# crypto digestroutines
|
||||||
|
import hashlib
|
||||||
|
|
||||||
|
def MD5(message):
|
||||||
|
ctx = hashlib.md5()
|
||||||
|
ctx.update(message)
|
||||||
|
return ctx.digest()
|
||||||
|
|
||||||
|
def SHA1(message):
|
||||||
|
ctx = hashlib.sha1()
|
||||||
|
ctx.update(message)
|
||||||
|
return ctx.digest()
|
||||||
|
|
||||||
|
def SHA256(message):
|
||||||
|
ctx = hashlib.sha256()
|
||||||
|
ctx.update(message)
|
||||||
|
return ctx.digest()
|
||||||
|
|
||||||
# Various character maps used to decrypt books. Probably supposed to act as obfuscation
|
# Various character maps used to decrypt books. Probably supposed to act as obfuscation
|
||||||
charMap1 = "n5Pr6St7Uv8Wx9YzAb0Cd1Ef2Gh3Jk4M"
|
charMap1 = "n5Pr6St7Uv8Wx9YzAb0Cd1Ef2Gh3Jk4M"
|
||||||
charMap2 = "ZB0bYyc1xDdW2wEV3Ff7KkPpL8UuGA4gz-Tme9Nn_tHh5SvXCsIiR6rJjQaqlOoM"
|
charMap2 = "ZB0bYyc1xDdW2wEV3Ff7KkPpL8UuGA4gz-Tme9Nn_tHh5SvXCsIiR6rJjQaqlOoM"
|
||||||
charMap3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
|
||||||
charMap4 = "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789"
|
# For Future Reference from .kinf approach of K4PC
|
||||||
|
charMap5 = "AzB0bYyCeVvaZ3FfUuG4g-TtHh5SsIiR6rJjQq7KkPpL8lOoMm9Nn_c1XxDdW2wE"
|
||||||
|
|
||||||
|
|
||||||
|
def encode(data, map):
|
||||||
|
result = ""
|
||||||
|
for char in data:
|
||||||
|
value = ord(char)
|
||||||
|
Q = (value ^ 0x80) // len(map)
|
||||||
|
R = value % len(map)
|
||||||
|
result += map[Q]
|
||||||
|
result += map[R]
|
||||||
|
return result
|
||||||
|
|
||||||
|
# Hash the bytes in data and then encode the digest with the characters in map
|
||||||
|
def encodeHash(data,map):
|
||||||
|
return encode(MD5(data),map)
|
||||||
|
|
||||||
|
# Decode the string in data with the characters in map. Returns the decoded bytes
|
||||||
|
def decode(data,map):
|
||||||
|
result = ""
|
||||||
|
for i in range (0,len(data)-1,2):
|
||||||
|
high = map.find(data[i])
|
||||||
|
low = map.find(data[i+1])
|
||||||
|
if (high == -1) or (low == -1) :
|
||||||
|
break
|
||||||
|
value = (((high * len(map)) ^ 0x80) & 0xFF) + low
|
||||||
|
result += pack("B",value)
|
||||||
|
return result
|
||||||
|
|
||||||
|
# For Future Reference from .kinf approach of K4PC
|
||||||
|
# generate table of prime number less than or equal to int n
|
||||||
|
def primes(n):
|
||||||
|
if n==2: return [2]
|
||||||
|
elif n<2: return []
|
||||||
|
s=range(3,n+1,2)
|
||||||
|
mroot = n ** 0.5
|
||||||
|
half=(n+1)/2-1
|
||||||
|
i=0
|
||||||
|
m=3
|
||||||
|
while m <= mroot:
|
||||||
|
if s[i]:
|
||||||
|
j=(m*m-3)/2
|
||||||
|
s[j]=0
|
||||||
|
while j<half:
|
||||||
|
s[j]=0
|
||||||
|
j+=m
|
||||||
|
i=i+1
|
||||||
|
m=2*i+3
|
||||||
|
return [2]+[x for x in s if x]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -137,30 +204,12 @@ def GetUserName():
|
||||||
username = os.getenv('USER')
|
username = os.getenv('USER')
|
||||||
return username
|
return username
|
||||||
|
|
||||||
|
|
||||||
def encode(data, map):
|
|
||||||
result = ""
|
|
||||||
for char in data:
|
|
||||||
value = ord(char)
|
|
||||||
Q = (value ^ 0x80) // len(map)
|
|
||||||
R = value % len(map)
|
|
||||||
result += map[Q]
|
|
||||||
result += map[R]
|
|
||||||
return result
|
|
||||||
|
|
||||||
import hashlib
|
|
||||||
|
|
||||||
def SHA256(message):
|
|
||||||
ctx = hashlib.sha256()
|
|
||||||
ctx.update(message)
|
|
||||||
return ctx.digest()
|
|
||||||
|
|
||||||
# implements an Pseudo Mac Version of Windows built-in Crypto routine
|
# implements an Pseudo Mac Version of Windows built-in Crypto routine
|
||||||
def CryptUnprotectData(encryptedData):
|
def CryptUnprotectData(encryptedData, salt):
|
||||||
sp = GetVolumeSerialNumber() + '!@#' + GetUserName()
|
sp = GetVolumeSerialNumber() + '!@#' + GetUserName()
|
||||||
passwdData = encode(SHA256(sp),charMap1)
|
passwdData = encode(SHA256(sp),charMap1)
|
||||||
crp = LibCrypto()
|
crp = LibCrypto()
|
||||||
key_iv = crp.keyivgen(passwdData)
|
key_iv = crp.keyivgen(passwdData, salt)
|
||||||
key = key_iv[0:32]
|
key = key_iv[0:32]
|
||||||
iv = key_iv[32:48]
|
iv = key_iv[32:48]
|
||||||
crp.set_decrypt_key(key,iv)
|
crp.set_decrypt_key(key,iv)
|
||||||
|
@ -170,6 +219,7 @@ def CryptUnprotectData(encryptedData):
|
||||||
|
|
||||||
# Locate the .kindle-info files
|
# Locate the .kindle-info files
|
||||||
def getKindleInfoFiles(kInfoFiles):
|
def getKindleInfoFiles(kInfoFiles):
|
||||||
|
# first search for current .kindle-info files
|
||||||
home = os.getenv('HOME')
|
home = os.getenv('HOME')
|
||||||
cmdline = 'find "' + home + '/Library/Application Support" -name ".kindle-info"'
|
cmdline = 'find "' + home + '/Library/Application Support" -name ".kindle-info"'
|
||||||
cmdline = cmdline.encode(sys.getfilesystemencoding())
|
cmdline = cmdline.encode(sys.getfilesystemencoding())
|
||||||
|
@ -178,23 +228,130 @@ def getKindleInfoFiles(kInfoFiles):
|
||||||
reslst = out1.split('\n')
|
reslst = out1.split('\n')
|
||||||
kinfopath = 'NONE'
|
kinfopath = 'NONE'
|
||||||
found = False
|
found = False
|
||||||
cnt = len(reslst)
|
|
||||||
for resline in reslst:
|
for resline in reslst:
|
||||||
if os.path.isfile(resline):
|
if os.path.isfile(resline):
|
||||||
kInfoFiles.append(resline)
|
kInfoFiles.append(resline)
|
||||||
found = True
|
found = True
|
||||||
|
# For Future Reference
|
||||||
|
#
|
||||||
|
# # add any .kinf files
|
||||||
|
# cmdline = 'find "' + home + '/Library/Application Support" -name "rainier*.kinf"'
|
||||||
|
# cmdline = cmdline.encode(sys.getfilesystemencoding())
|
||||||
|
# p1 = subprocess.Popen(cmdline, shell=True, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=False)
|
||||||
|
# out1, out2 = p1.communicate()
|
||||||
|
# reslst = out1.split('\n')
|
||||||
|
# for resline in reslst:
|
||||||
|
# if os.path.isfile(resline):
|
||||||
|
# kInfoFiles.append(resline)
|
||||||
|
# found = True
|
||||||
if not found:
|
if not found:
|
||||||
print('No .kindle-info files have been found.')
|
print('No kindle-info files have been found.')
|
||||||
return kInfoFiles
|
return kInfoFiles
|
||||||
|
|
||||||
# Parse the Kindle.info file and return the records as a list of key-values
|
# determine type of kindle info provided and return a
|
||||||
def parseKindleInfo(kInfoFile):
|
# database of keynames and values
|
||||||
|
def getDBfromFile(kInfoFile):
|
||||||
|
names = ["kindle.account.tokens","kindle.cookie.item","eulaVersionAccepted","login_date","kindle.token.item","login","kindle.key.item","kindle.name.info","kindle.device.info", "MazamaRandomNumber"]
|
||||||
DB = {}
|
DB = {}
|
||||||
|
cnt = 0
|
||||||
infoReader = open(kInfoFile, 'r')
|
infoReader = open(kInfoFile, 'r')
|
||||||
infoReader.read(1)
|
hdr = infoReader.read(1)
|
||||||
data = infoReader.read()
|
data = infoReader.read()
|
||||||
items = data.split('[')
|
|
||||||
for item in items:
|
if data.find('[') != -1 :
|
||||||
splito = item.split(':')
|
|
||||||
DB[splito[0]] =splito[1]
|
# older style kindle-info file
|
||||||
|
items = data.split('[')
|
||||||
|
for item in items:
|
||||||
|
if item != '':
|
||||||
|
keyhash, rawdata = item.split(':')
|
||||||
|
keyname = "unknown"
|
||||||
|
for name in names:
|
||||||
|
if encodeHash(name,charMap2) == keyhash:
|
||||||
|
keyname = name
|
||||||
|
break
|
||||||
|
if keyname == "unknown":
|
||||||
|
keyname = keyhash
|
||||||
|
encryptedValue = decode(rawdata,charMap2)
|
||||||
|
salt = '16743'
|
||||||
|
cleartext = CryptUnprotectData(encryptedValue, salt)
|
||||||
|
DB[keyname] = decode(cleartext,charMap1)
|
||||||
|
cnt = cnt + 1
|
||||||
|
if cnt == 0:
|
||||||
|
DB = None
|
||||||
|
return DB
|
||||||
|
|
||||||
|
# For Future Reference taken from K4PC 1.5.0 .kinf
|
||||||
|
#
|
||||||
|
# # else newer style .kinf file
|
||||||
|
# # the .kinf file uses "/" to separate it into records
|
||||||
|
# # so remove the trailing "/" to make it easy to use split
|
||||||
|
# data = data[:-1]
|
||||||
|
# items = data.split('/')
|
||||||
|
#
|
||||||
|
# # loop through the item records until all are processed
|
||||||
|
# while len(items) > 0:
|
||||||
|
#
|
||||||
|
# # get the first item record
|
||||||
|
# item = items.pop(0)
|
||||||
|
#
|
||||||
|
# # the first 32 chars of the first record of a group
|
||||||
|
# # is the MD5 hash of the key name encoded by charMap5
|
||||||
|
# keyhash = item[0:32]
|
||||||
|
#
|
||||||
|
# # the raw keyhash string is also used to create entropy for the actual
|
||||||
|
# # CryptProtectData Blob that represents that keys contents
|
||||||
|
# entropy = SHA1(keyhash)
|
||||||
|
#
|
||||||
|
# # the remainder of the first record when decoded with charMap5
|
||||||
|
# # has the ':' split char followed by the string representation
|
||||||
|
# # of the number of records that follow
|
||||||
|
# # and make up the contents
|
||||||
|
# srcnt = decode(item[34:],charMap5)
|
||||||
|
# rcnt = int(srcnt)
|
||||||
|
#
|
||||||
|
# # read and store in rcnt records of data
|
||||||
|
# # that make up the contents value
|
||||||
|
# edlst = []
|
||||||
|
# for i in xrange(rcnt):
|
||||||
|
# item = items.pop(0)
|
||||||
|
# edlst.append(item)
|
||||||
|
#
|
||||||
|
# keyname = "unknown"
|
||||||
|
# for name in names:
|
||||||
|
# if encodeHash(name,charMap5) == keyhash:
|
||||||
|
# keyname = name
|
||||||
|
# break
|
||||||
|
# if keyname == "unknown":
|
||||||
|
# keyname = keyhash
|
||||||
|
#
|
||||||
|
# # the charMap5 encoded contents data has had a length
|
||||||
|
# # of chars (always odd) cut off of the front and moved
|
||||||
|
# # to the end to prevent decoding using charMap5 from
|
||||||
|
# # working properly, and thereby preventing the ensuing
|
||||||
|
# # CryptUnprotectData call from succeeding.
|
||||||
|
#
|
||||||
|
# # The offset into the charMap5 encoded contents seems to be:
|
||||||
|
# # len(contents) - largest prime number less than or equal to int(len(content)/3)
|
||||||
|
# # (in other words split "about" 2/3rds of the way through)
|
||||||
|
#
|
||||||
|
# # move first offsets chars to end to align for decode by charMap5
|
||||||
|
# encdata = "".join(edlst)
|
||||||
|
# contlen = len(encdata)
|
||||||
|
# noffset = contlen - primes(int(contlen/3))[-1]
|
||||||
|
#
|
||||||
|
# # now properly split and recombine
|
||||||
|
# # by moving noffset chars from the start of the
|
||||||
|
# # string to the end of the string
|
||||||
|
# pfx = encdata[0:noffset]
|
||||||
|
# encdata = encdata[noffset:]
|
||||||
|
# encdata = encdata + pfx
|
||||||
|
#
|
||||||
|
# # decode using Map5 to get the CryptProtect Data
|
||||||
|
# encryptedValue = decode(encdata,charMap5)
|
||||||
|
# DB[keyname] = CryptUnprotectData(encryptedValue, entropy, 1)
|
||||||
|
# cnt = cnt + 1
|
||||||
|
|
||||||
|
if cnt == 0:
|
||||||
|
DB = None
|
||||||
return DB
|
return DB
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
# K4PC Windows specific routines
|
# K4PC Windows specific routines
|
||||||
|
|
||||||
from __future__ import with_statement
|
from __future__ import with_statement
|
||||||
|
|
||||||
import sys, os
|
import sys, os
|
||||||
|
from struct import pack, unpack, unpack_from
|
||||||
|
|
||||||
from ctypes import windll, c_char_p, c_wchar_p, c_uint, POINTER, byref, \
|
from ctypes import windll, c_char_p, c_wchar_p, c_uint, POINTER, byref, \
|
||||||
create_unicode_buffer, create_string_buffer, CFUNCTYPE, addressof, \
|
create_unicode_buffer, create_string_buffer, CFUNCTYPE, addressof, \
|
||||||
|
@ -10,25 +12,86 @@ from ctypes import windll, c_char_p, c_wchar_p, c_uint, POINTER, byref, \
|
||||||
|
|
||||||
import _winreg as winreg
|
import _winreg as winreg
|
||||||
|
|
||||||
import traceback
|
|
||||||
|
|
||||||
MAX_PATH = 255
|
MAX_PATH = 255
|
||||||
|
|
||||||
kernel32 = windll.kernel32
|
kernel32 = windll.kernel32
|
||||||
advapi32 = windll.advapi32
|
advapi32 = windll.advapi32
|
||||||
crypt32 = windll.crypt32
|
crypt32 = windll.crypt32
|
||||||
|
|
||||||
|
import traceback
|
||||||
|
|
||||||
# Various character maps used to decrypt books. Probably supposed to act as obfuscation
|
# crypto digestroutines
|
||||||
charMap1 = "n5Pr6St7Uv8Wx9YzAb0Cd1Ef2Gh3Jk4M"
|
import hashlib
|
||||||
|
|
||||||
|
def MD5(message):
|
||||||
|
ctx = hashlib.md5()
|
||||||
|
ctx.update(message)
|
||||||
|
return ctx.digest()
|
||||||
|
|
||||||
|
def SHA1(message):
|
||||||
|
ctx = hashlib.sha1()
|
||||||
|
ctx.update(message)
|
||||||
|
return ctx.digest()
|
||||||
|
|
||||||
|
|
||||||
|
# simple primes table (<= n) calculator
|
||||||
|
def primes(n):
|
||||||
|
if n==2: return [2]
|
||||||
|
elif n<2: return []
|
||||||
|
s=range(3,n+1,2)
|
||||||
|
mroot = n ** 0.5
|
||||||
|
half=(n+1)/2-1
|
||||||
|
i=0
|
||||||
|
m=3
|
||||||
|
while m <= mroot:
|
||||||
|
if s[i]:
|
||||||
|
j=(m*m-3)/2
|
||||||
|
s[j]=0
|
||||||
|
while j<half:
|
||||||
|
s[j]=0
|
||||||
|
j+=m
|
||||||
|
i=i+1
|
||||||
|
m=2*i+3
|
||||||
|
return [2]+[x for x in s if x]
|
||||||
|
|
||||||
|
|
||||||
|
# Various character maps used to decrypt kindle info values.
|
||||||
|
# Probably supposed to act as obfuscation
|
||||||
charMap2 = "AaZzB0bYyCc1XxDdW2wEeVv3FfUuG4g-TtHh5SsIiR6rJjQq7KkPpL8lOoMm9Nn_"
|
charMap2 = "AaZzB0bYyCc1XxDdW2wEeVv3FfUuG4g-TtHh5SsIiR6rJjQq7KkPpL8lOoMm9Nn_"
|
||||||
charMap3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
charMap5 = "AzB0bYyCeVvaZ3FfUuG4g-TtHh5SsIiR6rJjQq7KkPpL8lOoMm9Nn_c1XxDdW2wE"
|
||||||
charMap4 = "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789"
|
|
||||||
|
|
||||||
class DrmException(Exception):
|
class DrmException(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
# Encode the bytes in data with the characters in map
|
||||||
|
def encode(data, map):
|
||||||
|
result = ""
|
||||||
|
for char in data:
|
||||||
|
value = ord(char)
|
||||||
|
Q = (value ^ 0x80) // len(map)
|
||||||
|
R = value % len(map)
|
||||||
|
result += map[Q]
|
||||||
|
result += map[R]
|
||||||
|
return result
|
||||||
|
|
||||||
|
# Hash the bytes in data and then encode the digest with the characters in map
|
||||||
|
def encodeHash(data,map):
|
||||||
|
return encode(MD5(data),map)
|
||||||
|
|
||||||
|
# Decode the string in data with the characters in map. Returns the decoded bytes
|
||||||
|
def decode(data,map):
|
||||||
|
result = ""
|
||||||
|
for i in range (0,len(data)-1,2):
|
||||||
|
high = map.find(data[i])
|
||||||
|
low = map.find(data[i+1])
|
||||||
|
if (high == -1) or (low == -1) :
|
||||||
|
break
|
||||||
|
value = (((high * len(map)) ^ 0x80) & 0xFF) + low
|
||||||
|
result += pack("B",value)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
# interface with Windows OS Routines
|
||||||
class DataBlob(Structure):
|
class DataBlob(Structure):
|
||||||
_fields_ = [('cbData', c_uint),
|
_fields_ = [('cbData', c_uint),
|
||||||
('pbData', c_void_p)]
|
('pbData', c_void_p)]
|
||||||
|
@ -59,59 +122,175 @@ def GetVolumeSerialNumber():
|
||||||
return GetVolumeSerialNumber
|
return GetVolumeSerialNumber
|
||||||
GetVolumeSerialNumber = GetVolumeSerialNumber()
|
GetVolumeSerialNumber = GetVolumeSerialNumber()
|
||||||
|
|
||||||
|
def getLastError():
|
||||||
|
GetLastError = kernel32.GetLastError
|
||||||
|
GetLastError.argtypes = None
|
||||||
|
GetLastError.restype = c_uint
|
||||||
|
def getLastError():
|
||||||
|
return GetLastError()
|
||||||
|
return getLastError
|
||||||
|
getLastError = getLastError()
|
||||||
|
|
||||||
def GetUserName():
|
def GetUserName():
|
||||||
GetUserNameW = advapi32.GetUserNameW
|
GetUserNameW = advapi32.GetUserNameW
|
||||||
GetUserNameW.argtypes = [c_wchar_p, POINTER(c_uint)]
|
GetUserNameW.argtypes = [c_wchar_p, POINTER(c_uint)]
|
||||||
GetUserNameW.restype = c_uint
|
GetUserNameW.restype = c_uint
|
||||||
def GetUserName():
|
def GetUserName():
|
||||||
buffer = create_unicode_buffer(32)
|
buffer = create_unicode_buffer(2)
|
||||||
size = c_uint(len(buffer))
|
size = c_uint(len(buffer))
|
||||||
while not GetUserNameW(buffer, byref(size)):
|
while not GetUserNameW(buffer, byref(size)):
|
||||||
|
errcd = getLastError()
|
||||||
|
if errcd == 234:
|
||||||
|
# bad wine implementation up through wine 1.3.21
|
||||||
|
return "AlternateUserName"
|
||||||
buffer = create_unicode_buffer(len(buffer) * 2)
|
buffer = create_unicode_buffer(len(buffer) * 2)
|
||||||
size.value = len(buffer)
|
size.value = len(buffer)
|
||||||
return buffer.value.encode('utf-16-le')[::2]
|
return buffer.value.encode('utf-16-le')[::2]
|
||||||
return GetUserName
|
return GetUserName
|
||||||
GetUserName = GetUserName()
|
GetUserName = GetUserName()
|
||||||
|
|
||||||
|
|
||||||
def CryptUnprotectData():
|
def CryptUnprotectData():
|
||||||
_CryptUnprotectData = crypt32.CryptUnprotectData
|
_CryptUnprotectData = crypt32.CryptUnprotectData
|
||||||
_CryptUnprotectData.argtypes = [DataBlob_p, c_wchar_p, DataBlob_p,
|
_CryptUnprotectData.argtypes = [DataBlob_p, c_wchar_p, DataBlob_p,
|
||||||
c_void_p, c_void_p, c_uint, DataBlob_p]
|
c_void_p, c_void_p, c_uint, DataBlob_p]
|
||||||
_CryptUnprotectData.restype = c_uint
|
_CryptUnprotectData.restype = c_uint
|
||||||
def CryptUnprotectData(indata, entropy):
|
def CryptUnprotectData(indata, entropy, flags):
|
||||||
indatab = create_string_buffer(indata)
|
indatab = create_string_buffer(indata)
|
||||||
indata = DataBlob(len(indata), cast(indatab, c_void_p))
|
indata = DataBlob(len(indata), cast(indatab, c_void_p))
|
||||||
entropyb = create_string_buffer(entropy)
|
entropyb = create_string_buffer(entropy)
|
||||||
entropy = DataBlob(len(entropy), cast(entropyb, c_void_p))
|
entropy = DataBlob(len(entropy), cast(entropyb, c_void_p))
|
||||||
outdata = DataBlob()
|
outdata = DataBlob()
|
||||||
if not _CryptUnprotectData(byref(indata), None, byref(entropy),
|
if not _CryptUnprotectData(byref(indata), None, byref(entropy),
|
||||||
None, None, 0, byref(outdata)):
|
None, None, flags, byref(outdata)):
|
||||||
raise DrmException("Failed to Unprotect Data")
|
raise DrmException("Failed to Unprotect Data")
|
||||||
return string_at(outdata.pbData, outdata.cbData)
|
return string_at(outdata.pbData, outdata.cbData)
|
||||||
return CryptUnprotectData
|
return CryptUnprotectData
|
||||||
CryptUnprotectData = CryptUnprotectData()
|
CryptUnprotectData = CryptUnprotectData()
|
||||||
|
|
||||||
# Locate the .kindle-info files
|
|
||||||
|
# Locate all of the kindle-info style files and return as list
|
||||||
def getKindleInfoFiles(kInfoFiles):
|
def getKindleInfoFiles(kInfoFiles):
|
||||||
regkey = winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\\")
|
regkey = winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\\")
|
||||||
path = winreg.QueryValueEx(regkey, 'Local AppData')[0]
|
path = winreg.QueryValueEx(regkey, 'Local AppData')[0]
|
||||||
|
|
||||||
|
# first look for older kindle-info files
|
||||||
kinfopath = path +'\\Amazon\\Kindle For PC\\{AMAwzsaPaaZAzmZzZQzgZCAkZ3AjA_AY}\\kindle.info'
|
kinfopath = path +'\\Amazon\\Kindle For PC\\{AMAwzsaPaaZAzmZzZQzgZCAkZ3AjA_AY}\\kindle.info'
|
||||||
if not os.path.isfile(kinfopath):
|
if not os.path.isfile(kinfopath):
|
||||||
print('The kindle.info files has not been found.')
|
print('No kindle.info files have not been found.')
|
||||||
|
else:
|
||||||
|
kInfoFiles.append(kinfopath)
|
||||||
|
|
||||||
|
# now look for newer (K4PC 1.5.0 and later rainier.2.1.1.kinf file
|
||||||
|
kinfopath = path +'\\Amazon\\Kindle For PC\\storage\\rainier.2.1.1.kinf'
|
||||||
|
if not os.path.isfile(kinfopath):
|
||||||
|
print('No .kinf files have not been found.')
|
||||||
else:
|
else:
|
||||||
kInfoFiles.append(kinfopath)
|
kInfoFiles.append(kinfopath)
|
||||||
return kInfoFiles
|
return kInfoFiles
|
||||||
|
|
||||||
# Parse the Kindle.info file and return the records as a list of key-values
|
|
||||||
def parseKindleInfo(kInfoFile):
|
# determine type of kindle info provided and return a
|
||||||
|
# database of keynames and values
|
||||||
|
def getDBfromFile(kInfoFile):
|
||||||
|
names = ["kindle.account.tokens","kindle.cookie.item","eulaVersionAccepted","login_date","kindle.token.item","login","kindle.key.item","kindle.name.info","kindle.device.info", "MazamaRandomNumber"]
|
||||||
DB = {}
|
DB = {}
|
||||||
|
cnt = 0
|
||||||
infoReader = open(kInfoFile, 'r')
|
infoReader = open(kInfoFile, 'r')
|
||||||
infoReader.read(1)
|
hdr = infoReader.read(1)
|
||||||
data = infoReader.read()
|
data = infoReader.read()
|
||||||
items = data.split('{')
|
|
||||||
for item in items:
|
if data.find('{') != -1 :
|
||||||
splito = item.split(':')
|
|
||||||
DB[splito[0]] =splito[1]
|
# older style kindle-info file
|
||||||
|
items = data.split('{')
|
||||||
|
for item in items:
|
||||||
|
if item != '':
|
||||||
|
keyhash, rawdata = item.split(':')
|
||||||
|
keyname = "unknown"
|
||||||
|
for name in names:
|
||||||
|
if encodeHash(name,charMap2) == keyhash:
|
||||||
|
keyname = name
|
||||||
|
break
|
||||||
|
if keyname == "unknown":
|
||||||
|
keyname = keyhash
|
||||||
|
encryptedValue = decode(rawdata,charMap2)
|
||||||
|
DB[keyname] = CryptUnprotectData(encryptedValue, "", 0)
|
||||||
|
cnt = cnt + 1
|
||||||
|
if cnt == 0:
|
||||||
|
DB = None
|
||||||
|
return DB
|
||||||
|
|
||||||
|
# else newer style .kinf file
|
||||||
|
# the .kinf file uses "/" to separate it into records
|
||||||
|
# so remove the trailing "/" to make it easy to use split
|
||||||
|
data = data[:-1]
|
||||||
|
items = data.split('/')
|
||||||
|
|
||||||
|
# loop through the item records until all are processed
|
||||||
|
while len(items) > 0:
|
||||||
|
|
||||||
|
# get the first item record
|
||||||
|
item = items.pop(0)
|
||||||
|
|
||||||
|
# the first 32 chars of the first record of a group
|
||||||
|
# is the MD5 hash of the key name encoded by charMap5
|
||||||
|
keyhash = item[0:32]
|
||||||
|
|
||||||
|
# the raw keyhash string is also used to create entropy for the actual
|
||||||
|
# CryptProtectData Blob that represents that keys contents
|
||||||
|
entropy = SHA1(keyhash)
|
||||||
|
|
||||||
|
# the remainder of the first record when decoded with charMap5
|
||||||
|
# has the ':' split char followed by the string representation
|
||||||
|
# of the number of records that follow
|
||||||
|
# and make up the contents
|
||||||
|
srcnt = decode(item[34:],charMap5)
|
||||||
|
rcnt = int(srcnt)
|
||||||
|
|
||||||
|
# read and store in rcnt records of data
|
||||||
|
# that make up the contents value
|
||||||
|
edlst = []
|
||||||
|
for i in xrange(rcnt):
|
||||||
|
item = items.pop(0)
|
||||||
|
edlst.append(item)
|
||||||
|
|
||||||
|
keyname = "unknown"
|
||||||
|
for name in names:
|
||||||
|
if encodeHash(name,charMap5) == keyhash:
|
||||||
|
keyname = name
|
||||||
|
break
|
||||||
|
if keyname == "unknown":
|
||||||
|
keyname = keyhash
|
||||||
|
|
||||||
|
# the charMap5 encoded contents data has had a length
|
||||||
|
# of chars (always odd) cut off of the front and moved
|
||||||
|
# to the end to prevent decoding using charMap5 from
|
||||||
|
# working properly, and thereby preventing the ensuing
|
||||||
|
# CryptUnprotectData call from succeeding.
|
||||||
|
|
||||||
|
# The offset into the charMap5 encoded contents seems to be:
|
||||||
|
# len(contents) - largest prime number less than or equal to int(len(content)/3)
|
||||||
|
# (in other words split "about" 2/3rds of the way through)
|
||||||
|
|
||||||
|
# move first offsets chars to end to align for decode by charMap5
|
||||||
|
encdata = "".join(edlst)
|
||||||
|
contlen = len(encdata)
|
||||||
|
noffset = contlen - primes(int(contlen/3))[-1]
|
||||||
|
|
||||||
|
# now properly split and recombine
|
||||||
|
# by moving noffset chars from the start of the
|
||||||
|
# string to the end of the string
|
||||||
|
pfx = encdata[0:noffset]
|
||||||
|
encdata = encdata[noffset:]
|
||||||
|
encdata = encdata + pfx
|
||||||
|
|
||||||
|
# decode using Map5 to get the CryptProtect Data
|
||||||
|
encryptedValue = decode(encdata,charMap5)
|
||||||
|
DB[keyname] = CryptUnprotectData(encryptedValue, entropy, 1)
|
||||||
|
cnt = cnt + 1
|
||||||
|
|
||||||
|
if cnt == 0:
|
||||||
|
DB = None
|
||||||
return DB
|
return DB
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -11,16 +11,28 @@ from struct import pack, unpack, unpack_from
|
||||||
class DrmException(Exception):
|
class DrmException(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
global kindleDatabase
|
|
||||||
global charMap1
|
global charMap1
|
||||||
global charMap2
|
|
||||||
global charMap3
|
global charMap3
|
||||||
global charMap4
|
global charMap4
|
||||||
|
|
||||||
if sys.platform.startswith('win'):
|
if 'calibre' in sys.modules:
|
||||||
from k4pcutils import getKindleInfoFiles, parseKindleInfo, CryptUnprotectData, GetUserName, GetVolumeSerialNumber, charMap2
|
inCalibre = True
|
||||||
if sys.platform.startswith('darwin'):
|
else:
|
||||||
from k4mutils import getKindleInfoFiles, parseKindleInfo, CryptUnprotectData, GetUserName, GetVolumeSerialNumber, charMap2
|
inCalibre = False
|
||||||
|
|
||||||
|
if inCalibre:
|
||||||
|
if sys.platform.startswith('win'):
|
||||||
|
from calibre_plugins.k4mobidedrm.k4pcutils import getKindleInfoFiles, getDBfromFile, GetUserName, GetVolumeSerialNumber
|
||||||
|
|
||||||
|
if sys.platform.startswith('darwin'):
|
||||||
|
from calibre_plugins.k4mobidedrm.k4mutils import getKindleInfoFiles, getDBfromFile, GetUserName, GetVolumeSerialNumber
|
||||||
|
else:
|
||||||
|
if sys.platform.startswith('win'):
|
||||||
|
from k4pcutils import getKindleInfoFiles, getDBfromFile, GetUserName, GetVolumeSerialNumber
|
||||||
|
|
||||||
|
if sys.platform.startswith('darwin'):
|
||||||
|
from k4mutils import getKindleInfoFiles, getDBfromFile, GetUserName, GetVolumeSerialNumber
|
||||||
|
|
||||||
|
|
||||||
charMap1 = "n5Pr6St7Uv8Wx9YzAb0Cd1Ef2Gh3Jk4M"
|
charMap1 = "n5Pr6St7Uv8Wx9YzAb0Cd1Ef2Gh3Jk4M"
|
||||||
charMap3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
charMap3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
||||||
|
@ -66,50 +78,7 @@ def decode(data,map):
|
||||||
value = (((high * len(map)) ^ 0x80) & 0xFF) + low
|
value = (((high * len(map)) ^ 0x80) & 0xFF) + low
|
||||||
result += pack("B",value)
|
result += pack("B",value)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
# Get a record from the Kindle.info file for the key "hashedKey" (already hashed and encoded).
|
|
||||||
# Return the decoded and decrypted record
|
|
||||||
def getKindleInfoValueForHash(hashedKey):
|
|
||||||
global kindleDatabase
|
|
||||||
global charMap1
|
|
||||||
global charMap2
|
|
||||||
encryptedValue = decode(kindleDatabase[hashedKey],charMap2)
|
|
||||||
if sys.platform.startswith('win'):
|
|
||||||
return CryptUnprotectData(encryptedValue,"")
|
|
||||||
else:
|
|
||||||
cleartext = CryptUnprotectData(encryptedValue)
|
|
||||||
return decode(cleartext, charMap1)
|
|
||||||
|
|
||||||
# Get a record from the Kindle.info file for the string in "key" (plaintext).
|
|
||||||
# Return the decoded and decrypted record
|
|
||||||
def getKindleInfoValueForKey(key):
|
|
||||||
global charMap2
|
|
||||||
return getKindleInfoValueForHash(encodeHash(key,charMap2))
|
|
||||||
|
|
||||||
# Find if the original string for a hashed/encoded string is known.
|
|
||||||
# If so return the original string othwise return an empty string.
|
|
||||||
def findNameForHash(hash):
|
|
||||||
global charMap2
|
|
||||||
names = ["kindle.account.tokens","kindle.cookie.item","eulaVersionAccepted","login_date","kindle.token.item","login","kindle.key.item","kindle.name.info","kindle.device.info", "MazamaRandomNumber"]
|
|
||||||
result = ""
|
|
||||||
for name in names:
|
|
||||||
if hash == encodeHash(name, charMap2):
|
|
||||||
result = name
|
|
||||||
break
|
|
||||||
return result
|
|
||||||
|
|
||||||
# Print all the records from the kindle.info file (option -i)
|
|
||||||
def printKindleInfo():
|
|
||||||
for record in kindleDatabase:
|
|
||||||
name = findNameForHash(record)
|
|
||||||
if name != "" :
|
|
||||||
print (name)
|
|
||||||
print ("--------------------------")
|
|
||||||
else :
|
|
||||||
print ("Unknown Record")
|
|
||||||
print getKindleInfoValueForHash(record)
|
|
||||||
print "\n"
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# PID generation routines
|
# PID generation routines
|
||||||
#
|
#
|
||||||
|
@ -222,15 +191,15 @@ def getKindlePid(pidlst, rec209, token, serialnum):
|
||||||
return pidlst
|
return pidlst
|
||||||
|
|
||||||
|
|
||||||
# Parse the EXTH header records and parse the Kindleinfo
|
# parse the Kindleinfo file to calculate the book pid.
|
||||||
# file to calculate the book pid.
|
|
||||||
|
keynames = ["kindle.account.tokens","kindle.cookie.item","eulaVersionAccepted","login_date","kindle.token.item","login","kindle.key.item","kindle.name.info","kindle.device.info", "MazamaRandomNumber"]
|
||||||
|
|
||||||
def getK4Pids(pidlst, rec209, token, kInfoFile):
|
def getK4Pids(pidlst, rec209, token, kInfoFile):
|
||||||
global kindleDatabase
|
|
||||||
global charMap1
|
global charMap1
|
||||||
kindleDatabase = None
|
kindleDatabase = None
|
||||||
try:
|
try:
|
||||||
kindleDatabase = parseKindleInfo(kInfoFile)
|
kindleDatabase = getDBfromFile(kInfoFile)
|
||||||
except Exception, message:
|
except Exception, message:
|
||||||
print(message)
|
print(message)
|
||||||
kindleDatabase = None
|
kindleDatabase = None
|
||||||
|
@ -241,10 +210,10 @@ def getK4Pids(pidlst, rec209, token, kInfoFile):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Get the Mazama Random number
|
# Get the Mazama Random number
|
||||||
MazamaRandomNumber = getKindleInfoValueForKey("MazamaRandomNumber")
|
MazamaRandomNumber = kindleDatabase["MazamaRandomNumber"]
|
||||||
|
|
||||||
# Get the kindle account token
|
# Get the kindle account token
|
||||||
kindleAccountToken = getKindleInfoValueForKey("kindle.account.tokens")
|
kindleAccountToken = kindleDatabase["kindle.account.tokens"]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
print "Keys not found in " + kInfoFile
|
print "Keys not found in " + kInfoFile
|
||||||
return pidlst
|
return pidlst
|
||||||
|
|
|
@ -51,8 +51,9 @@
|
||||||
# 0.29 - It seems that the ideas about when multibyte trailing characters were
|
# 0.29 - It seems that the ideas about when multibyte trailing characters were
|
||||||
# included in the encryption were wrong. They aren't for DOC compressed
|
# included in the encryption were wrong. They aren't for DOC compressed
|
||||||
# files, but they are for HUFF/CDIC compress files!
|
# files, but they are for HUFF/CDIC compress files!
|
||||||
|
# 0.30 - Modified interface slightly to work better with new calibre plugin style
|
||||||
|
|
||||||
__version__ = '0.29'
|
__version__ = '0.30'
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
@ -163,6 +164,7 @@ class MobiBook:
|
||||||
def __init__(self, infile):
|
def __init__(self, infile):
|
||||||
# initial sanity check on file
|
# initial sanity check on file
|
||||||
self.data_file = file(infile, 'rb').read()
|
self.data_file = file(infile, 'rb').read()
|
||||||
|
self.mobi_data = ''
|
||||||
self.header = self.data_file[0:78]
|
self.header = self.data_file[0:78]
|
||||||
if self.header[0x3C:0x3C+8] != 'BOOKMOBI' and self.header[0x3C:0x3C+8] != 'TEXtREAd':
|
if self.header[0x3C:0x3C+8] != 'BOOKMOBI' and self.header[0x3C:0x3C+8] != 'TEXtREAd':
|
||||||
raise DrmException("invalid file format")
|
raise DrmException("invalid file format")
|
||||||
|
@ -301,13 +303,17 @@ class MobiBook:
|
||||||
break
|
break
|
||||||
return [found_key,pid]
|
return [found_key,pid]
|
||||||
|
|
||||||
|
def getMobiFile(self, outpath):
|
||||||
|
file(outpath,'wb').write(self.mobi_data)
|
||||||
|
|
||||||
def processBook(self, pidlist):
|
def processBook(self, pidlist):
|
||||||
crypto_type, = struct.unpack('>H', self.sect[0xC:0xC+2])
|
crypto_type, = struct.unpack('>H', self.sect[0xC:0xC+2])
|
||||||
print 'Crypto Type is: ', crypto_type
|
print 'Crypto Type is: ', crypto_type
|
||||||
self.crypto_type = crypto_type
|
self.crypto_type = crypto_type
|
||||||
if crypto_type == 0:
|
if crypto_type == 0:
|
||||||
print "This book is not encrypted."
|
print "This book is not encrypted."
|
||||||
return self.data_file
|
self.mobi_data = self.data_file
|
||||||
|
return
|
||||||
if crypto_type != 2 and crypto_type != 1:
|
if crypto_type != 2 and crypto_type != 1:
|
||||||
raise DrmException("Cannot decode unknown Mobipocket encryption type %d" % crypto_type)
|
raise DrmException("Cannot decode unknown Mobipocket encryption type %d" % crypto_type)
|
||||||
|
|
||||||
|
@ -353,33 +359,35 @@ class MobiBook:
|
||||||
|
|
||||||
# decrypt sections
|
# decrypt sections
|
||||||
print "Decrypting. Please wait . . .",
|
print "Decrypting. Please wait . . .",
|
||||||
new_data = self.data_file[:self.sections[1][0]]
|
self.mobi_data = self.data_file[:self.sections[1][0]]
|
||||||
for i in xrange(1, self.records+1):
|
for i in xrange(1, self.records+1):
|
||||||
data = self.loadSection(i)
|
data = self.loadSection(i)
|
||||||
extra_size = getSizeOfTrailingDataEntries(data, len(data), self.extra_data_flags)
|
extra_size = getSizeOfTrailingDataEntries(data, len(data), self.extra_data_flags)
|
||||||
if i%100 == 0:
|
if i%100 == 0:
|
||||||
print ".",
|
print ".",
|
||||||
# print "record %d, extra_size %d" %(i,extra_size)
|
# print "record %d, extra_size %d" %(i,extra_size)
|
||||||
new_data += PC1(found_key, data[0:len(data) - extra_size])
|
self.mobi_data += PC1(found_key, data[0:len(data) - extra_size])
|
||||||
if extra_size > 0:
|
if extra_size > 0:
|
||||||
new_data += data[-extra_size:]
|
self.mobi_data += data[-extra_size:]
|
||||||
if self.num_sections > self.records+1:
|
if self.num_sections > self.records+1:
|
||||||
new_data += self.data_file[self.sections[self.records+1][0]:]
|
self.mobi_data += self.data_file[self.sections[self.records+1][0]:]
|
||||||
self.data_file = new_data
|
|
||||||
print "done"
|
print "done"
|
||||||
return self.data_file
|
return
|
||||||
|
|
||||||
def getUnencryptedBook(infile,pid):
|
def getUnencryptedBook(infile,pid):
|
||||||
if not os.path.isfile(infile):
|
if not os.path.isfile(infile):
|
||||||
raise DrmException('Input File Not Found')
|
raise DrmException('Input File Not Found')
|
||||||
book = MobiBook(infile)
|
book = MobiBook(infile)
|
||||||
return book.processBook([pid])
|
book.processBook([pid])
|
||||||
|
return book.mobi_data
|
||||||
|
|
||||||
def getUnencryptedBookWithList(infile,pidlist):
|
def getUnencryptedBookWithList(infile,pidlist):
|
||||||
if not os.path.isfile(infile):
|
if not os.path.isfile(infile):
|
||||||
raise DrmException('Input File Not Found')
|
raise DrmException('Input File Not Found')
|
||||||
book = MobiBook(infile)
|
book = MobiBook(infile)
|
||||||
return book.processBook(pidlist)
|
book.processBook(pidlist)
|
||||||
|
return book.mobi_data
|
||||||
|
|
||||||
|
|
||||||
def main(argv=sys.argv):
|
def main(argv=sys.argv):
|
||||||
print ('MobiDeDrm v%(__version__)s. '
|
print ('MobiDeDrm v%(__version__)s. '
|
||||||
|
|
|
@ -10,7 +10,12 @@ class Unbuffered:
|
||||||
return getattr(self.stream, attr)
|
return getattr(self.stream, attr)
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
sys.stdout=Unbuffered(sys.stdout)
|
|
||||||
|
if 'calibre' in sys.modules:
|
||||||
|
inCalibre = True
|
||||||
|
else:
|
||||||
|
inCalibre = False
|
||||||
|
|
||||||
import os, csv, getopt
|
import os, csv, getopt
|
||||||
import zlib, zipfile, tempfile, shutil
|
import zlib, zipfile, tempfile, shutil
|
||||||
from struct import pack
|
from struct import pack
|
||||||
|
@ -18,10 +23,32 @@ from struct import unpack
|
||||||
|
|
||||||
class TpzDRMError(Exception):
|
class TpzDRMError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
# local support routines
|
# local support routines
|
||||||
import kgenpids
|
if inCalibre:
|
||||||
import genbook
|
from calibre_plugins.k4mobidedrm import kgenpids
|
||||||
|
from calibre_plugins.k4mobidedrm import genbook
|
||||||
|
else:
|
||||||
|
import kgenpids
|
||||||
|
import genbook
|
||||||
|
|
||||||
|
|
||||||
|
# recursive zip creation support routine
|
||||||
|
def zipUpDir(myzip, tdir, localname):
|
||||||
|
currentdir = tdir
|
||||||
|
if localname != "":
|
||||||
|
currentdir = os.path.join(currentdir,localname)
|
||||||
|
list = os.listdir(currentdir)
|
||||||
|
for file in list:
|
||||||
|
afilename = file
|
||||||
|
localfilePath = os.path.join(localname, afilename)
|
||||||
|
realfilePath = os.path.join(currentdir,file)
|
||||||
|
if os.path.isfile(realfilePath):
|
||||||
|
myzip.write(realfilePath, localfilePath)
|
||||||
|
elif os.path.isdir(realfilePath):
|
||||||
|
zipUpDir(myzip, tdir, localfilePath)
|
||||||
|
|
||||||
#
|
#
|
||||||
# Utility routines
|
# Utility routines
|
||||||
#
|
#
|
||||||
|
@ -110,9 +137,9 @@ def decryptDkeyRecords(data,PID):
|
||||||
|
|
||||||
|
|
||||||
class TopazBook:
|
class TopazBook:
|
||||||
def __init__(self, filename, outdir):
|
def __init__(self, filename):
|
||||||
self.fo = file(filename, 'rb')
|
self.fo = file(filename, 'rb')
|
||||||
self.outdir = outdir
|
self.outdir = tempfile.mkdtemp()
|
||||||
self.bookPayloadOffset = 0
|
self.bookPayloadOffset = 0
|
||||||
self.bookHeaderRecords = {}
|
self.bookHeaderRecords = {}
|
||||||
self.bookMetadata = {}
|
self.bookMetadata = {}
|
||||||
|
@ -317,21 +344,33 @@ class TopazBook:
|
||||||
file(outputFile, 'wb').write(record)
|
file(outputFile, 'wb').write(record)
|
||||||
print " "
|
print " "
|
||||||
|
|
||||||
|
def getHTMLZip(self, zipname):
|
||||||
|
htmlzip = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
||||||
|
htmlzip.write(os.path.join(self.outdir,'book.html'),'book.html')
|
||||||
|
htmlzip.write(os.path.join(self.outdir,'book.opf'),'book.opf')
|
||||||
|
if os.path.isfile(os.path.join(self.outdir,'cover.jpg')):
|
||||||
|
htmlzip.write(os.path.join(self.outdir,'cover.jpg'),'cover.jpg')
|
||||||
|
htmlzip.write(os.path.join(self.outdir,'style.css'),'style.css')
|
||||||
|
zipUpDir(htmlzip, self.outdir, 'img')
|
||||||
|
htmlzip.close()
|
||||||
|
|
||||||
def zipUpDir(myzip, tempdir,localname):
|
def getSVGZip(self, zipname):
|
||||||
currentdir = tempdir
|
svgzip = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
||||||
if localname != "":
|
svgzip.write(os.path.join(self.outdir,'index_svg.xhtml'),'index_svg.xhtml')
|
||||||
currentdir = os.path.join(currentdir,localname)
|
zipUpDir(svgzip, self.outdir, 'svg')
|
||||||
list = os.listdir(currentdir)
|
zipUpDir(svgzip, self.outdir, 'img')
|
||||||
for file in list:
|
svgzip.close()
|
||||||
afilename = file
|
|
||||||
localfilePath = os.path.join(localname, afilename)
|
|
||||||
realfilePath = os.path.join(currentdir,file)
|
|
||||||
if os.path.isfile(realfilePath):
|
|
||||||
myzip.write(realfilePath, localfilePath)
|
|
||||||
elif os.path.isdir(realfilePath):
|
|
||||||
zipUpDir(myzip, tempdir, localfilePath)
|
|
||||||
|
|
||||||
|
def getXMLZip(self, zipname):
|
||||||
|
xmlzip = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
||||||
|
targetdir = os.path.join(self.outdir,'xml')
|
||||||
|
zipUpDir(xmlzip, targetdir, '')
|
||||||
|
zipUpDir(xmlzip, self.outdir, 'img')
|
||||||
|
xmlzip.close()
|
||||||
|
|
||||||
|
def cleanup(self):
|
||||||
|
if os.path.isdir(self.outdir):
|
||||||
|
shutil.rmtree(self.outdir, True)
|
||||||
|
|
||||||
def usage(progname):
|
def usage(progname):
|
||||||
print "Removes DRM protection from Topaz ebooks and extract the contents"
|
print "Removes DRM protection from Topaz ebooks and extract the contents"
|
||||||
|
@ -383,58 +422,46 @@ def main(argv=sys.argv):
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
bookname = os.path.splitext(os.path.basename(infile))[0]
|
bookname = os.path.splitext(os.path.basename(infile))[0]
|
||||||
tempdir = tempfile.mkdtemp()
|
|
||||||
|
|
||||||
tb = TopazBook(infile, tempdir)
|
tb = TopazBook(infile)
|
||||||
title = tb.getBookTitle()
|
title = tb.getBookTitle()
|
||||||
print "Processing Book: ", title
|
print "Processing Book: ", title
|
||||||
keysRecord, keysRecordRecord = tb.getPIDMetaInfo()
|
keysRecord, keysRecordRecord = tb.getPIDMetaInfo()
|
||||||
pidlst = kgenpids.getPidList(keysRecord, keysRecordRecord, k4, pids, serials, kInfoFiles)
|
pidlst = kgenpids.getPidList(keysRecord, keysRecordRecord, k4, pids, serials, kInfoFiles)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
print "Decrypting Book"
|
||||||
tb.processBook(pidlst)
|
tb.processBook(pidlst)
|
||||||
|
|
||||||
|
print " Creating HTML ZIP Archive"
|
||||||
|
zipname = os.path.join(outdir, bookname + '_nodrm' + '.htmlz')
|
||||||
|
tb.getHTMLZip(zipname)
|
||||||
|
|
||||||
|
print " Creating SVG ZIP Archive"
|
||||||
|
zipname = os.path.join(outdir, bookname + '_SVG' + '.htmlz')
|
||||||
|
tb.getSVGZip(zipname)
|
||||||
|
|
||||||
|
print " Creating XML ZIP Archive"
|
||||||
|
zipname = os.path.join(outdir, bookname + '_XML' + '.zip')
|
||||||
|
tb.getXMLZip(zipname)
|
||||||
|
|
||||||
|
# removing internal temporary directory of pieces
|
||||||
|
tb.cleanup()
|
||||||
|
|
||||||
except TpzDRMError, e:
|
except TpzDRMError, e:
|
||||||
print str(e)
|
print str(e)
|
||||||
print " Creating DeBug Full Zip Archive of Book"
|
tb.cleanup()
|
||||||
zipname = os.path.join(outdir, bookname + '_debug' + '.zip')
|
|
||||||
myzip = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
|
||||||
zipUpDir(myzip, tempdir, '')
|
|
||||||
myzip.close()
|
|
||||||
shutil.rmtree(tempdir, True)
|
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
print " Creating HTML ZIP Archive"
|
except Exception, e:
|
||||||
zipname = os.path.join(outdir, bookname + '_nodrm' + '.zip')
|
print str(e)
|
||||||
myzip1 = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
tb.cleanup
|
||||||
myzip1.write(os.path.join(tempdir,'book.html'),'book.html')
|
return 1
|
||||||
myzip1.write(os.path.join(tempdir,'book.opf'),'book.opf')
|
|
||||||
if os.path.isfile(os.path.join(tempdir,'cover.jpg')):
|
|
||||||
myzip1.write(os.path.join(tempdir,'cover.jpg'),'cover.jpg')
|
|
||||||
myzip1.write(os.path.join(tempdir,'style.css'),'style.css')
|
|
||||||
zipUpDir(myzip1, tempdir, 'img')
|
|
||||||
myzip1.close()
|
|
||||||
|
|
||||||
print " Creating SVG ZIP Archive"
|
|
||||||
zipname = os.path.join(outdir, bookname + '_SVG' + '.zip')
|
|
||||||
myzip2 = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
|
||||||
myzip2.write(os.path.join(tempdir,'index_svg.xhtml'),'index_svg.xhtml')
|
|
||||||
zipUpDir(myzip2, tempdir, 'svg')
|
|
||||||
zipUpDir(myzip2, tempdir, 'img')
|
|
||||||
myzip2.close()
|
|
||||||
|
|
||||||
print " Creating XML ZIP Archive"
|
|
||||||
zipname = os.path.join(outdir, bookname + '_XML' + '.zip')
|
|
||||||
myzip3 = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
|
||||||
targetdir = os.path.join(tempdir,'xml')
|
|
||||||
zipUpDir(myzip3, targetdir, '')
|
|
||||||
zipUpDir(myzip3, tempdir, 'img')
|
|
||||||
myzip3.close()
|
|
||||||
|
|
||||||
shutil.rmtree(tempdir, True)
|
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
sys.stdout=Unbuffered(sys.stdout)
|
||||||
sys.exit(main())
|
sys.exit(main())
|
||||||
|
|
||||||
|
|
|
@ -21,10 +21,21 @@ from struct import unpack
|
||||||
|
|
||||||
|
|
||||||
# local support routines
|
# local support routines
|
||||||
import convert2xml
|
if 'calibre' in sys.modules:
|
||||||
import flatxml2html
|
inCalibre = True
|
||||||
import flatxml2svg
|
else:
|
||||||
import stylexml2css
|
inCalibre = False
|
||||||
|
|
||||||
|
if inCalibre :
|
||||||
|
from calibre_plugins.k4mobidedrm import convert2xml
|
||||||
|
from calibre_plugins.k4mobidedrm import flatxml2html
|
||||||
|
from calibre_plugins.k4mobidedrm import flatxml2svg
|
||||||
|
from calibre_plugins.k4mobidedrm import stylexml2css
|
||||||
|
else :
|
||||||
|
import convert2xml
|
||||||
|
import flatxml2html
|
||||||
|
import flatxml2svg
|
||||||
|
import stylexml2css
|
||||||
|
|
||||||
|
|
||||||
# Get a 7 bit encoded number from a file
|
# Get a 7 bit encoded number from a file
|
||||||
|
@ -504,7 +515,7 @@ def generateBook(bookDir, raw, fixedimage):
|
||||||
opfstr += ' </metadata>\n'
|
opfstr += ' </metadata>\n'
|
||||||
opfstr += '<manifest>\n'
|
opfstr += '<manifest>\n'
|
||||||
opfstr += ' <item id="book" href="book.html" media-type="application/xhtml+xml"/>\n'
|
opfstr += ' <item id="book" href="book.html" media-type="application/xhtml+xml"/>\n'
|
||||||
opfstr += ' <item id="stylesheet" href="style.css" media-type="text.css"/>\n'
|
opfstr += ' <item id="stylesheet" href="style.css" media-type="text/css"/>\n'
|
||||||
# adding image files to manifest
|
# adding image files to manifest
|
||||||
filenames = os.listdir(imgDir)
|
filenames = os.listdir(imgDir)
|
||||||
filenames = sorted(filenames)
|
filenames = sorted(filenames)
|
|
@ -0,0 +1,199 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
from __future__ import with_statement
|
||||||
|
|
||||||
|
# engine to remove drm from Kindle for Mac and Kindle for PC books
|
||||||
|
# for personal use for archiving and converting your ebooks
|
||||||
|
|
||||||
|
# PLEASE DO NOT PIRATE EBOOKS!
|
||||||
|
|
||||||
|
# We want all authors and publishers, and eBook stores to live
|
||||||
|
# long and prosperous lives but at the same time we just want to
|
||||||
|
# be able to read OUR books on whatever device we want and to keep
|
||||||
|
# readable for a long, long time
|
||||||
|
|
||||||
|
# This borrows very heavily from works by CMBDTC, IHeartCabbages, skindle,
|
||||||
|
# unswindle, DarkReverser, ApprenticeAlf, DiapDealer, some_updates
|
||||||
|
# and many many others
|
||||||
|
|
||||||
|
|
||||||
|
__version__ = '3.1'
|
||||||
|
|
||||||
|
class Unbuffered:
|
||||||
|
def __init__(self, stream):
|
||||||
|
self.stream = stream
|
||||||
|
def write(self, data):
|
||||||
|
self.stream.write(data)
|
||||||
|
self.stream.flush()
|
||||||
|
def __getattr__(self, attr):
|
||||||
|
return getattr(self.stream, attr)
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import os, csv, getopt
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
|
||||||
|
class DrmException(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
if 'calibre' in sys.modules:
|
||||||
|
inCalibre = True
|
||||||
|
else:
|
||||||
|
inCalibre = False
|
||||||
|
|
||||||
|
if inCalibre:
|
||||||
|
from calibre_plugins.k4mobidedrm import mobidedrm
|
||||||
|
from calibre_plugins.k4mobidedrm import topazextract
|
||||||
|
from calibre_plugins.k4mobidedrm import kgenpids
|
||||||
|
else:
|
||||||
|
import mobidedrm
|
||||||
|
import topazextract
|
||||||
|
import kgenpids
|
||||||
|
|
||||||
|
|
||||||
|
# cleanup bytestring filenames
|
||||||
|
# borrowed from calibre from calibre/src/calibre/__init__.py
|
||||||
|
# added in removal of non-printing chars
|
||||||
|
# and removal of . at start
|
||||||
|
# convert spaces to underscores
|
||||||
|
def cleanup_name(name):
|
||||||
|
_filename_sanitize = re.compile(r'[\xae\0\\|\?\*<":>\+/]')
|
||||||
|
substitute='_'
|
||||||
|
one = ''.join(char for char in name if char in string.printable)
|
||||||
|
one = _filename_sanitize.sub(substitute, one)
|
||||||
|
one = re.sub(r'\s', ' ', one).strip()
|
||||||
|
one = re.sub(r'^\.+$', '_', one)
|
||||||
|
one = one.replace('..', substitute)
|
||||||
|
# Windows doesn't like path components that end with a period
|
||||||
|
if one.endswith('.'):
|
||||||
|
one = one[:-1]+substitute
|
||||||
|
# Mac and Unix don't like file names that begin with a full stop
|
||||||
|
if len(one) > 0 and one[0] == '.':
|
||||||
|
one = substitute+one[1:]
|
||||||
|
one = one.replace(' ','_')
|
||||||
|
return one
|
||||||
|
|
||||||
|
def decryptBook(infile, outdir, k4, kInfoFiles, serials, pids):
|
||||||
|
# handle the obvious cases at the beginning
|
||||||
|
if not os.path.isfile(infile):
|
||||||
|
print "Error: Input file does not exist"
|
||||||
|
return 1
|
||||||
|
|
||||||
|
mobi = True
|
||||||
|
magic3 = file(infile,'rb').read(3)
|
||||||
|
if magic3 == 'TPZ':
|
||||||
|
mobi = False
|
||||||
|
|
||||||
|
bookname = os.path.splitext(os.path.basename(infile))[0]
|
||||||
|
|
||||||
|
if mobi:
|
||||||
|
mb = mobidedrm.MobiBook(infile)
|
||||||
|
else:
|
||||||
|
mb = topazextract.TopazBook(infile)
|
||||||
|
|
||||||
|
title = mb.getBookTitle()
|
||||||
|
print "Processing Book: ", title
|
||||||
|
filenametitle = cleanup_name(title)
|
||||||
|
outfilename = bookname
|
||||||
|
if len(bookname)>4 and len(filenametitle)>4 and bookname[:4] != filenametitle[:4]:
|
||||||
|
outfilename = outfilename + "_" + filenametitle
|
||||||
|
|
||||||
|
# build pid list
|
||||||
|
md1, md2 = mb.getPIDMetaInfo()
|
||||||
|
pidlst = kgenpids.getPidList(md1, md2, k4, pids, serials, kInfoFiles)
|
||||||
|
|
||||||
|
try:
|
||||||
|
mb.processBook(pidlst)
|
||||||
|
|
||||||
|
except mobidedrm.DrmException, e:
|
||||||
|
print "Error: " + str(e) + "\nDRM Removal Failed.\n"
|
||||||
|
return 1
|
||||||
|
except topazextract.TpzDRMError, e:
|
||||||
|
print "Error: " + str(e) + "\nDRM Removal Failed.\n"
|
||||||
|
return 1
|
||||||
|
except Exception, e:
|
||||||
|
print "Error: " + str(e) + "\nDRM Removal Failed.\n"
|
||||||
|
return 1
|
||||||
|
|
||||||
|
if mobi:
|
||||||
|
outfile = os.path.join(outdir, outfilename + '_nodrm' + '.mobi')
|
||||||
|
mb.getMobiFile(outfile)
|
||||||
|
return 0
|
||||||
|
|
||||||
|
# topaz:
|
||||||
|
print " Creating NoDRM HTMLZ Archive"
|
||||||
|
zipname = os.path.join(outdir, outfilename + '_nodrm' + '.htmlz')
|
||||||
|
mb.getHTMLZip(zipname)
|
||||||
|
|
||||||
|
print " Creating SVG HTMLZ Archive"
|
||||||
|
zipname = os.path.join(outdir, outfilename + '_SVG' + '.htmlz')
|
||||||
|
mb.getSVGZip(zipname)
|
||||||
|
|
||||||
|
print " Creating XML ZIP Archive"
|
||||||
|
zipname = os.path.join(outdir, outfilename + '_XML' + '.zip')
|
||||||
|
mb.getXMLZip(zipname)
|
||||||
|
|
||||||
|
# remove internal temporary directory of Topaz pieces
|
||||||
|
mb.cleanup()
|
||||||
|
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
def usage(progname):
|
||||||
|
print "Removes DRM protection from K4PC/M, Kindle, Mobi and Topaz ebooks"
|
||||||
|
print "Usage:"
|
||||||
|
print " %s [-k <kindle.info>] [-p <pidnums>] [-s <kindleSerialNumbers>] <infile> <outdir> " % progname
|
||||||
|
|
||||||
|
#
|
||||||
|
# Main
|
||||||
|
#
|
||||||
|
def main(argv=sys.argv):
|
||||||
|
progname = os.path.basename(argv[0])
|
||||||
|
|
||||||
|
k4 = False
|
||||||
|
kInfoFiles = []
|
||||||
|
serials = []
|
||||||
|
pids = []
|
||||||
|
|
||||||
|
print ('K4MobiDeDrm v%(__version__)s '
|
||||||
|
'provided by the work of many including DiapDealer, SomeUpdates, IHeartCabbages, CMBDTC, Skindle, DarkReverser, ApprenticeAlf, etc .' % globals())
|
||||||
|
|
||||||
|
print ' '
|
||||||
|
try:
|
||||||
|
opts, args = getopt.getopt(sys.argv[1:], "k:p:s:")
|
||||||
|
except getopt.GetoptError, err:
|
||||||
|
print str(err)
|
||||||
|
usage(progname)
|
||||||
|
sys.exit(2)
|
||||||
|
if len(args)<2:
|
||||||
|
usage(progname)
|
||||||
|
sys.exit(2)
|
||||||
|
|
||||||
|
for o, a in opts:
|
||||||
|
if o == "-k":
|
||||||
|
if a == None :
|
||||||
|
raise DrmException("Invalid parameter for -k")
|
||||||
|
kInfoFiles.append(a)
|
||||||
|
if o == "-p":
|
||||||
|
if a == None :
|
||||||
|
raise DrmException("Invalid parameter for -p")
|
||||||
|
pids = a.split(',')
|
||||||
|
if o == "-s":
|
||||||
|
if a == None :
|
||||||
|
raise DrmException("Invalid parameter for -s")
|
||||||
|
serials = a.split(',')
|
||||||
|
|
||||||
|
# try with built in Kindle Info files
|
||||||
|
k4 = True
|
||||||
|
if sys.platform.startswith('linux'):
|
||||||
|
k4 = False
|
||||||
|
kInfoFiles = None
|
||||||
|
infile = args[0]
|
||||||
|
outdir = args[1]
|
||||||
|
return decryptBook(infile, outdir, k4, kInfoFiles, serials, pids)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
sys.stdout=Unbuffered(sys.stdout)
|
||||||
|
sys.exit(main())
|
||||||
|
|
|
@ -0,0 +1,357 @@
|
||||||
|
# standlone set of Mac OSX specific routines needed for K4DeDRM
|
||||||
|
|
||||||
|
from __future__ import with_statement
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
from struct import pack, unpack, unpack_from
|
||||||
|
|
||||||
|
class DrmException(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
# interface to needed routines in openssl's libcrypto
|
||||||
|
def _load_crypto_libcrypto():
|
||||||
|
from ctypes import CDLL, byref, POINTER, c_void_p, c_char_p, c_int, c_long, \
|
||||||
|
Structure, c_ulong, create_string_buffer, addressof, string_at, cast
|
||||||
|
from ctypes.util import find_library
|
||||||
|
|
||||||
|
libcrypto = find_library('crypto')
|
||||||
|
if libcrypto is None:
|
||||||
|
raise DrmException('libcrypto not found')
|
||||||
|
libcrypto = CDLL(libcrypto)
|
||||||
|
|
||||||
|
AES_MAXNR = 14
|
||||||
|
c_char_pp = POINTER(c_char_p)
|
||||||
|
c_int_p = POINTER(c_int)
|
||||||
|
|
||||||
|
class AES_KEY(Structure):
|
||||||
|
_fields_ = [('rd_key', c_long * (4 * (AES_MAXNR + 1))), ('rounds', c_int)]
|
||||||
|
AES_KEY_p = POINTER(AES_KEY)
|
||||||
|
|
||||||
|
def F(restype, name, argtypes):
|
||||||
|
func = getattr(libcrypto, name)
|
||||||
|
func.restype = restype
|
||||||
|
func.argtypes = argtypes
|
||||||
|
return func
|
||||||
|
|
||||||
|
AES_cbc_encrypt = F(None, 'AES_cbc_encrypt',[c_char_p, c_char_p, c_ulong, AES_KEY_p, c_char_p,c_int])
|
||||||
|
|
||||||
|
AES_set_decrypt_key = F(c_int, 'AES_set_decrypt_key',[c_char_p, c_int, AES_KEY_p])
|
||||||
|
|
||||||
|
PKCS5_PBKDF2_HMAC_SHA1 = F(c_int, 'PKCS5_PBKDF2_HMAC_SHA1',
|
||||||
|
[c_char_p, c_ulong, c_char_p, c_ulong, c_ulong, c_ulong, c_char_p])
|
||||||
|
|
||||||
|
class LibCrypto(object):
|
||||||
|
def __init__(self):
|
||||||
|
self._blocksize = 0
|
||||||
|
self._keyctx = None
|
||||||
|
self.iv = 0
|
||||||
|
|
||||||
|
def set_decrypt_key(self, userkey, iv):
|
||||||
|
self._blocksize = len(userkey)
|
||||||
|
if (self._blocksize != 16) and (self._blocksize != 24) and (self._blocksize != 32) :
|
||||||
|
raise DrmException('AES improper key used')
|
||||||
|
return
|
||||||
|
keyctx = self._keyctx = AES_KEY()
|
||||||
|
self.iv = iv
|
||||||
|
rv = AES_set_decrypt_key(userkey, len(userkey) * 8, keyctx)
|
||||||
|
if rv < 0:
|
||||||
|
raise DrmException('Failed to initialize AES key')
|
||||||
|
|
||||||
|
def decrypt(self, data):
|
||||||
|
out = create_string_buffer(len(data))
|
||||||
|
rv = AES_cbc_encrypt(data, out, len(data), self._keyctx, self.iv, 0)
|
||||||
|
if rv == 0:
|
||||||
|
raise DrmException('AES decryption failed')
|
||||||
|
return out.raw
|
||||||
|
|
||||||
|
def keyivgen(self, passwd, salt):
|
||||||
|
saltlen = len(salt)
|
||||||
|
passlen = len(passwd)
|
||||||
|
iter = 0x3e8
|
||||||
|
keylen = 80
|
||||||
|
out = create_string_buffer(keylen)
|
||||||
|
rv = PKCS5_PBKDF2_HMAC_SHA1(passwd, passlen, salt, saltlen, iter, keylen, out)
|
||||||
|
return out.raw
|
||||||
|
return LibCrypto
|
||||||
|
|
||||||
|
def _load_crypto():
|
||||||
|
LibCrypto = None
|
||||||
|
try:
|
||||||
|
LibCrypto = _load_crypto_libcrypto()
|
||||||
|
except (ImportError, DrmException):
|
||||||
|
pass
|
||||||
|
return LibCrypto
|
||||||
|
|
||||||
|
LibCrypto = _load_crypto()
|
||||||
|
|
||||||
|
#
|
||||||
|
# Utility Routines
|
||||||
|
#
|
||||||
|
|
||||||
|
# crypto digestroutines
|
||||||
|
import hashlib
|
||||||
|
|
||||||
|
def MD5(message):
|
||||||
|
ctx = hashlib.md5()
|
||||||
|
ctx.update(message)
|
||||||
|
return ctx.digest()
|
||||||
|
|
||||||
|
def SHA1(message):
|
||||||
|
ctx = hashlib.sha1()
|
||||||
|
ctx.update(message)
|
||||||
|
return ctx.digest()
|
||||||
|
|
||||||
|
def SHA256(message):
|
||||||
|
ctx = hashlib.sha256()
|
||||||
|
ctx.update(message)
|
||||||
|
return ctx.digest()
|
||||||
|
|
||||||
|
# Various character maps used to decrypt books. Probably supposed to act as obfuscation
|
||||||
|
charMap1 = "n5Pr6St7Uv8Wx9YzAb0Cd1Ef2Gh3Jk4M"
|
||||||
|
charMap2 = "ZB0bYyc1xDdW2wEV3Ff7KkPpL8UuGA4gz-Tme9Nn_tHh5SvXCsIiR6rJjQaqlOoM"
|
||||||
|
|
||||||
|
# For Future Reference from .kinf approach of K4PC
|
||||||
|
charMap5 = "AzB0bYyCeVvaZ3FfUuG4g-TtHh5SsIiR6rJjQq7KkPpL8lOoMm9Nn_c1XxDdW2wE"
|
||||||
|
|
||||||
|
|
||||||
|
def encode(data, map):
|
||||||
|
result = ""
|
||||||
|
for char in data:
|
||||||
|
value = ord(char)
|
||||||
|
Q = (value ^ 0x80) // len(map)
|
||||||
|
R = value % len(map)
|
||||||
|
result += map[Q]
|
||||||
|
result += map[R]
|
||||||
|
return result
|
||||||
|
|
||||||
|
# Hash the bytes in data and then encode the digest with the characters in map
|
||||||
|
def encodeHash(data,map):
|
||||||
|
return encode(MD5(data),map)
|
||||||
|
|
||||||
|
# Decode the string in data with the characters in map. Returns the decoded bytes
|
||||||
|
def decode(data,map):
|
||||||
|
result = ""
|
||||||
|
for i in range (0,len(data)-1,2):
|
||||||
|
high = map.find(data[i])
|
||||||
|
low = map.find(data[i+1])
|
||||||
|
if (high == -1) or (low == -1) :
|
||||||
|
break
|
||||||
|
value = (((high * len(map)) ^ 0x80) & 0xFF) + low
|
||||||
|
result += pack("B",value)
|
||||||
|
return result
|
||||||
|
|
||||||
|
# For Future Reference from .kinf approach of K4PC
|
||||||
|
# generate table of prime number less than or equal to int n
|
||||||
|
def primes(n):
|
||||||
|
if n==2: return [2]
|
||||||
|
elif n<2: return []
|
||||||
|
s=range(3,n+1,2)
|
||||||
|
mroot = n ** 0.5
|
||||||
|
half=(n+1)/2-1
|
||||||
|
i=0
|
||||||
|
m=3
|
||||||
|
while m <= mroot:
|
||||||
|
if s[i]:
|
||||||
|
j=(m*m-3)/2
|
||||||
|
s[j]=0
|
||||||
|
while j<half:
|
||||||
|
s[j]=0
|
||||||
|
j+=m
|
||||||
|
i=i+1
|
||||||
|
m=2*i+3
|
||||||
|
return [2]+[x for x in s if x]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# uses a sub process to get the Hard Drive Serial Number using ioreg
|
||||||
|
# returns with the serial number of drive whose BSD Name is "disk0"
|
||||||
|
def GetVolumeSerialNumber():
|
||||||
|
sernum = os.getenv('MYSERIALNUMBER')
|
||||||
|
if sernum != None:
|
||||||
|
return sernum
|
||||||
|
cmdline = '/usr/sbin/ioreg -l -S -w 0 -r -c AppleAHCIDiskDriver'
|
||||||
|
cmdline = cmdline.encode(sys.getfilesystemencoding())
|
||||||
|
p = subprocess.Popen(cmdline, shell=True, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=False)
|
||||||
|
out1, out2 = p.communicate()
|
||||||
|
reslst = out1.split('\n')
|
||||||
|
cnt = len(reslst)
|
||||||
|
bsdname = None
|
||||||
|
sernum = None
|
||||||
|
foundIt = False
|
||||||
|
for j in xrange(cnt):
|
||||||
|
resline = reslst[j]
|
||||||
|
pp = resline.find('"Serial Number" = "')
|
||||||
|
if pp >= 0:
|
||||||
|
sernum = resline[pp+19:-1]
|
||||||
|
sernum = sernum.strip()
|
||||||
|
bb = resline.find('"BSD Name" = "')
|
||||||
|
if bb >= 0:
|
||||||
|
bsdname = resline[bb+14:-1]
|
||||||
|
bsdname = bsdname.strip()
|
||||||
|
if (bsdname == 'disk0') and (sernum != None):
|
||||||
|
foundIt = True
|
||||||
|
break
|
||||||
|
if not foundIt:
|
||||||
|
sernum = '9999999999'
|
||||||
|
return sernum
|
||||||
|
|
||||||
|
# uses unix env to get username instead of using sysctlbyname
|
||||||
|
def GetUserName():
|
||||||
|
username = os.getenv('USER')
|
||||||
|
return username
|
||||||
|
|
||||||
|
# implements an Pseudo Mac Version of Windows built-in Crypto routine
|
||||||
|
def CryptUnprotectData(encryptedData, salt):
|
||||||
|
sp = GetVolumeSerialNumber() + '!@#' + GetUserName()
|
||||||
|
passwdData = encode(SHA256(sp),charMap1)
|
||||||
|
crp = LibCrypto()
|
||||||
|
key_iv = crp.keyivgen(passwdData, salt)
|
||||||
|
key = key_iv[0:32]
|
||||||
|
iv = key_iv[32:48]
|
||||||
|
crp.set_decrypt_key(key,iv)
|
||||||
|
cleartext = crp.decrypt(encryptedData)
|
||||||
|
return cleartext
|
||||||
|
|
||||||
|
|
||||||
|
# Locate the .kindle-info files
|
||||||
|
def getKindleInfoFiles(kInfoFiles):
|
||||||
|
# first search for current .kindle-info files
|
||||||
|
home = os.getenv('HOME')
|
||||||
|
cmdline = 'find "' + home + '/Library/Application Support" -name ".kindle-info"'
|
||||||
|
cmdline = cmdline.encode(sys.getfilesystemencoding())
|
||||||
|
p1 = subprocess.Popen(cmdline, shell=True, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=False)
|
||||||
|
out1, out2 = p1.communicate()
|
||||||
|
reslst = out1.split('\n')
|
||||||
|
kinfopath = 'NONE'
|
||||||
|
found = False
|
||||||
|
for resline in reslst:
|
||||||
|
if os.path.isfile(resline):
|
||||||
|
kInfoFiles.append(resline)
|
||||||
|
found = True
|
||||||
|
# For Future Reference
|
||||||
|
#
|
||||||
|
# # add any .kinf files
|
||||||
|
# cmdline = 'find "' + home + '/Library/Application Support" -name "rainier*.kinf"'
|
||||||
|
# cmdline = cmdline.encode(sys.getfilesystemencoding())
|
||||||
|
# p1 = subprocess.Popen(cmdline, shell=True, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=False)
|
||||||
|
# out1, out2 = p1.communicate()
|
||||||
|
# reslst = out1.split('\n')
|
||||||
|
# for resline in reslst:
|
||||||
|
# if os.path.isfile(resline):
|
||||||
|
# kInfoFiles.append(resline)
|
||||||
|
# found = True
|
||||||
|
if not found:
|
||||||
|
print('No kindle-info files have been found.')
|
||||||
|
return kInfoFiles
|
||||||
|
|
||||||
|
# determine type of kindle info provided and return a
|
||||||
|
# database of keynames and values
|
||||||
|
def getDBfromFile(kInfoFile):
|
||||||
|
names = ["kindle.account.tokens","kindle.cookie.item","eulaVersionAccepted","login_date","kindle.token.item","login","kindle.key.item","kindle.name.info","kindle.device.info", "MazamaRandomNumber"]
|
||||||
|
DB = {}
|
||||||
|
cnt = 0
|
||||||
|
infoReader = open(kInfoFile, 'r')
|
||||||
|
hdr = infoReader.read(1)
|
||||||
|
data = infoReader.read()
|
||||||
|
|
||||||
|
if data.find('[') != -1 :
|
||||||
|
|
||||||
|
# older style kindle-info file
|
||||||
|
items = data.split('[')
|
||||||
|
for item in items:
|
||||||
|
if item != '':
|
||||||
|
keyhash, rawdata = item.split(':')
|
||||||
|
keyname = "unknown"
|
||||||
|
for name in names:
|
||||||
|
if encodeHash(name,charMap2) == keyhash:
|
||||||
|
keyname = name
|
||||||
|
break
|
||||||
|
if keyname == "unknown":
|
||||||
|
keyname = keyhash
|
||||||
|
encryptedValue = decode(rawdata,charMap2)
|
||||||
|
salt = '16743'
|
||||||
|
cleartext = CryptUnprotectData(encryptedValue, salt)
|
||||||
|
DB[keyname] = decode(cleartext,charMap1)
|
||||||
|
cnt = cnt + 1
|
||||||
|
if cnt == 0:
|
||||||
|
DB = None
|
||||||
|
return DB
|
||||||
|
|
||||||
|
# For Future Reference taken from K4PC 1.5.0 .kinf
|
||||||
|
#
|
||||||
|
# # else newer style .kinf file
|
||||||
|
# # the .kinf file uses "/" to separate it into records
|
||||||
|
# # so remove the trailing "/" to make it easy to use split
|
||||||
|
# data = data[:-1]
|
||||||
|
# items = data.split('/')
|
||||||
|
#
|
||||||
|
# # loop through the item records until all are processed
|
||||||
|
# while len(items) > 0:
|
||||||
|
#
|
||||||
|
# # get the first item record
|
||||||
|
# item = items.pop(0)
|
||||||
|
#
|
||||||
|
# # the first 32 chars of the first record of a group
|
||||||
|
# # is the MD5 hash of the key name encoded by charMap5
|
||||||
|
# keyhash = item[0:32]
|
||||||
|
#
|
||||||
|
# # the raw keyhash string is also used to create entropy for the actual
|
||||||
|
# # CryptProtectData Blob that represents that keys contents
|
||||||
|
# entropy = SHA1(keyhash)
|
||||||
|
#
|
||||||
|
# # the remainder of the first record when decoded with charMap5
|
||||||
|
# # has the ':' split char followed by the string representation
|
||||||
|
# # of the number of records that follow
|
||||||
|
# # and make up the contents
|
||||||
|
# srcnt = decode(item[34:],charMap5)
|
||||||
|
# rcnt = int(srcnt)
|
||||||
|
#
|
||||||
|
# # read and store in rcnt records of data
|
||||||
|
# # that make up the contents value
|
||||||
|
# edlst = []
|
||||||
|
# for i in xrange(rcnt):
|
||||||
|
# item = items.pop(0)
|
||||||
|
# edlst.append(item)
|
||||||
|
#
|
||||||
|
# keyname = "unknown"
|
||||||
|
# for name in names:
|
||||||
|
# if encodeHash(name,charMap5) == keyhash:
|
||||||
|
# keyname = name
|
||||||
|
# break
|
||||||
|
# if keyname == "unknown":
|
||||||
|
# keyname = keyhash
|
||||||
|
#
|
||||||
|
# # the charMap5 encoded contents data has had a length
|
||||||
|
# # of chars (always odd) cut off of the front and moved
|
||||||
|
# # to the end to prevent decoding using charMap5 from
|
||||||
|
# # working properly, and thereby preventing the ensuing
|
||||||
|
# # CryptUnprotectData call from succeeding.
|
||||||
|
#
|
||||||
|
# # The offset into the charMap5 encoded contents seems to be:
|
||||||
|
# # len(contents) - largest prime number less than or equal to int(len(content)/3)
|
||||||
|
# # (in other words split "about" 2/3rds of the way through)
|
||||||
|
#
|
||||||
|
# # move first offsets chars to end to align for decode by charMap5
|
||||||
|
# encdata = "".join(edlst)
|
||||||
|
# contlen = len(encdata)
|
||||||
|
# noffset = contlen - primes(int(contlen/3))[-1]
|
||||||
|
#
|
||||||
|
# # now properly split and recombine
|
||||||
|
# # by moving noffset chars from the start of the
|
||||||
|
# # string to the end of the string
|
||||||
|
# pfx = encdata[0:noffset]
|
||||||
|
# encdata = encdata[noffset:]
|
||||||
|
# encdata = encdata + pfx
|
||||||
|
#
|
||||||
|
# # decode using Map5 to get the CryptProtect Data
|
||||||
|
# encryptedValue = decode(encdata,charMap5)
|
||||||
|
# DB[keyname] = CryptUnprotectData(encryptedValue, entropy, 1)
|
||||||
|
# cnt = cnt + 1
|
||||||
|
|
||||||
|
if cnt == 0:
|
||||||
|
DB = None
|
||||||
|
return DB
|
|
@ -0,0 +1,296 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
# K4PC Windows specific routines
|
||||||
|
|
||||||
|
from __future__ import with_statement
|
||||||
|
|
||||||
|
import sys, os
|
||||||
|
from struct import pack, unpack, unpack_from
|
||||||
|
|
||||||
|
from ctypes import windll, c_char_p, c_wchar_p, c_uint, POINTER, byref, \
|
||||||
|
create_unicode_buffer, create_string_buffer, CFUNCTYPE, addressof, \
|
||||||
|
string_at, Structure, c_void_p, cast
|
||||||
|
|
||||||
|
import _winreg as winreg
|
||||||
|
|
||||||
|
MAX_PATH = 255
|
||||||
|
|
||||||
|
kernel32 = windll.kernel32
|
||||||
|
advapi32 = windll.advapi32
|
||||||
|
crypt32 = windll.crypt32
|
||||||
|
|
||||||
|
import traceback
|
||||||
|
|
||||||
|
# crypto digestroutines
|
||||||
|
import hashlib
|
||||||
|
|
||||||
|
def MD5(message):
|
||||||
|
ctx = hashlib.md5()
|
||||||
|
ctx.update(message)
|
||||||
|
return ctx.digest()
|
||||||
|
|
||||||
|
def SHA1(message):
|
||||||
|
ctx = hashlib.sha1()
|
||||||
|
ctx.update(message)
|
||||||
|
return ctx.digest()
|
||||||
|
|
||||||
|
|
||||||
|
# simple primes table (<= n) calculator
|
||||||
|
def primes(n):
|
||||||
|
if n==2: return [2]
|
||||||
|
elif n<2: return []
|
||||||
|
s=range(3,n+1,2)
|
||||||
|
mroot = n ** 0.5
|
||||||
|
half=(n+1)/2-1
|
||||||
|
i=0
|
||||||
|
m=3
|
||||||
|
while m <= mroot:
|
||||||
|
if s[i]:
|
||||||
|
j=(m*m-3)/2
|
||||||
|
s[j]=0
|
||||||
|
while j<half:
|
||||||
|
s[j]=0
|
||||||
|
j+=m
|
||||||
|
i=i+1
|
||||||
|
m=2*i+3
|
||||||
|
return [2]+[x for x in s if x]
|
||||||
|
|
||||||
|
|
||||||
|
# Various character maps used to decrypt kindle info values.
|
||||||
|
# Probably supposed to act as obfuscation
|
||||||
|
charMap2 = "AaZzB0bYyCc1XxDdW2wEeVv3FfUuG4g-TtHh5SsIiR6rJjQq7KkPpL8lOoMm9Nn_"
|
||||||
|
charMap5 = "AzB0bYyCeVvaZ3FfUuG4g-TtHh5SsIiR6rJjQq7KkPpL8lOoMm9Nn_c1XxDdW2wE"
|
||||||
|
|
||||||
|
class DrmException(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
# Encode the bytes in data with the characters in map
|
||||||
|
def encode(data, map):
|
||||||
|
result = ""
|
||||||
|
for char in data:
|
||||||
|
value = ord(char)
|
||||||
|
Q = (value ^ 0x80) // len(map)
|
||||||
|
R = value % len(map)
|
||||||
|
result += map[Q]
|
||||||
|
result += map[R]
|
||||||
|
return result
|
||||||
|
|
||||||
|
# Hash the bytes in data and then encode the digest with the characters in map
|
||||||
|
def encodeHash(data,map):
|
||||||
|
return encode(MD5(data),map)
|
||||||
|
|
||||||
|
# Decode the string in data with the characters in map. Returns the decoded bytes
|
||||||
|
def decode(data,map):
|
||||||
|
result = ""
|
||||||
|
for i in range (0,len(data)-1,2):
|
||||||
|
high = map.find(data[i])
|
||||||
|
low = map.find(data[i+1])
|
||||||
|
if (high == -1) or (low == -1) :
|
||||||
|
break
|
||||||
|
value = (((high * len(map)) ^ 0x80) & 0xFF) + low
|
||||||
|
result += pack("B",value)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
# interface with Windows OS Routines
|
||||||
|
class DataBlob(Structure):
|
||||||
|
_fields_ = [('cbData', c_uint),
|
||||||
|
('pbData', c_void_p)]
|
||||||
|
DataBlob_p = POINTER(DataBlob)
|
||||||
|
|
||||||
|
|
||||||
|
def GetSystemDirectory():
|
||||||
|
GetSystemDirectoryW = kernel32.GetSystemDirectoryW
|
||||||
|
GetSystemDirectoryW.argtypes = [c_wchar_p, c_uint]
|
||||||
|
GetSystemDirectoryW.restype = c_uint
|
||||||
|
def GetSystemDirectory():
|
||||||
|
buffer = create_unicode_buffer(MAX_PATH + 1)
|
||||||
|
GetSystemDirectoryW(buffer, len(buffer))
|
||||||
|
return buffer.value
|
||||||
|
return GetSystemDirectory
|
||||||
|
GetSystemDirectory = GetSystemDirectory()
|
||||||
|
|
||||||
|
def GetVolumeSerialNumber():
|
||||||
|
GetVolumeInformationW = kernel32.GetVolumeInformationW
|
||||||
|
GetVolumeInformationW.argtypes = [c_wchar_p, c_wchar_p, c_uint,
|
||||||
|
POINTER(c_uint), POINTER(c_uint),
|
||||||
|
POINTER(c_uint), c_wchar_p, c_uint]
|
||||||
|
GetVolumeInformationW.restype = c_uint
|
||||||
|
def GetVolumeSerialNumber(path = GetSystemDirectory().split('\\')[0] + '\\'):
|
||||||
|
vsn = c_uint(0)
|
||||||
|
GetVolumeInformationW(path, None, 0, byref(vsn), None, None, None, 0)
|
||||||
|
return str(vsn.value)
|
||||||
|
return GetVolumeSerialNumber
|
||||||
|
GetVolumeSerialNumber = GetVolumeSerialNumber()
|
||||||
|
|
||||||
|
def getLastError():
|
||||||
|
GetLastError = kernel32.GetLastError
|
||||||
|
GetLastError.argtypes = None
|
||||||
|
GetLastError.restype = c_uint
|
||||||
|
def getLastError():
|
||||||
|
return GetLastError()
|
||||||
|
return getLastError
|
||||||
|
getLastError = getLastError()
|
||||||
|
|
||||||
|
def GetUserName():
|
||||||
|
GetUserNameW = advapi32.GetUserNameW
|
||||||
|
GetUserNameW.argtypes = [c_wchar_p, POINTER(c_uint)]
|
||||||
|
GetUserNameW.restype = c_uint
|
||||||
|
def GetUserName():
|
||||||
|
buffer = create_unicode_buffer(2)
|
||||||
|
size = c_uint(len(buffer))
|
||||||
|
while not GetUserNameW(buffer, byref(size)):
|
||||||
|
errcd = getLastError()
|
||||||
|
if errcd == 234:
|
||||||
|
# bad wine implementation up through wine 1.3.21
|
||||||
|
return "AlternateUserName"
|
||||||
|
buffer = create_unicode_buffer(len(buffer) * 2)
|
||||||
|
size.value = len(buffer)
|
||||||
|
return buffer.value.encode('utf-16-le')[::2]
|
||||||
|
return GetUserName
|
||||||
|
GetUserName = GetUserName()
|
||||||
|
|
||||||
|
def CryptUnprotectData():
|
||||||
|
_CryptUnprotectData = crypt32.CryptUnprotectData
|
||||||
|
_CryptUnprotectData.argtypes = [DataBlob_p, c_wchar_p, DataBlob_p,
|
||||||
|
c_void_p, c_void_p, c_uint, DataBlob_p]
|
||||||
|
_CryptUnprotectData.restype = c_uint
|
||||||
|
def CryptUnprotectData(indata, entropy, flags):
|
||||||
|
indatab = create_string_buffer(indata)
|
||||||
|
indata = DataBlob(len(indata), cast(indatab, c_void_p))
|
||||||
|
entropyb = create_string_buffer(entropy)
|
||||||
|
entropy = DataBlob(len(entropy), cast(entropyb, c_void_p))
|
||||||
|
outdata = DataBlob()
|
||||||
|
if not _CryptUnprotectData(byref(indata), None, byref(entropy),
|
||||||
|
None, None, flags, byref(outdata)):
|
||||||
|
raise DrmException("Failed to Unprotect Data")
|
||||||
|
return string_at(outdata.pbData, outdata.cbData)
|
||||||
|
return CryptUnprotectData
|
||||||
|
CryptUnprotectData = CryptUnprotectData()
|
||||||
|
|
||||||
|
|
||||||
|
# Locate all of the kindle-info style files and return as list
|
||||||
|
def getKindleInfoFiles(kInfoFiles):
|
||||||
|
regkey = winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\\")
|
||||||
|
path = winreg.QueryValueEx(regkey, 'Local AppData')[0]
|
||||||
|
|
||||||
|
# first look for older kindle-info files
|
||||||
|
kinfopath = path +'\\Amazon\\Kindle For PC\\{AMAwzsaPaaZAzmZzZQzgZCAkZ3AjA_AY}\\kindle.info'
|
||||||
|
if not os.path.isfile(kinfopath):
|
||||||
|
print('No kindle.info files have not been found.')
|
||||||
|
else:
|
||||||
|
kInfoFiles.append(kinfopath)
|
||||||
|
|
||||||
|
# now look for newer (K4PC 1.5.0 and later rainier.2.1.1.kinf file
|
||||||
|
kinfopath = path +'\\Amazon\\Kindle For PC\\storage\\rainier.2.1.1.kinf'
|
||||||
|
if not os.path.isfile(kinfopath):
|
||||||
|
print('No .kinf files have not been found.')
|
||||||
|
else:
|
||||||
|
kInfoFiles.append(kinfopath)
|
||||||
|
return kInfoFiles
|
||||||
|
|
||||||
|
|
||||||
|
# determine type of kindle info provided and return a
|
||||||
|
# database of keynames and values
|
||||||
|
def getDBfromFile(kInfoFile):
|
||||||
|
names = ["kindle.account.tokens","kindle.cookie.item","eulaVersionAccepted","login_date","kindle.token.item","login","kindle.key.item","kindle.name.info","kindle.device.info", "MazamaRandomNumber"]
|
||||||
|
DB = {}
|
||||||
|
cnt = 0
|
||||||
|
infoReader = open(kInfoFile, 'r')
|
||||||
|
hdr = infoReader.read(1)
|
||||||
|
data = infoReader.read()
|
||||||
|
|
||||||
|
if data.find('{') != -1 :
|
||||||
|
|
||||||
|
# older style kindle-info file
|
||||||
|
items = data.split('{')
|
||||||
|
for item in items:
|
||||||
|
if item != '':
|
||||||
|
keyhash, rawdata = item.split(':')
|
||||||
|
keyname = "unknown"
|
||||||
|
for name in names:
|
||||||
|
if encodeHash(name,charMap2) == keyhash:
|
||||||
|
keyname = name
|
||||||
|
break
|
||||||
|
if keyname == "unknown":
|
||||||
|
keyname = keyhash
|
||||||
|
encryptedValue = decode(rawdata,charMap2)
|
||||||
|
DB[keyname] = CryptUnprotectData(encryptedValue, "", 0)
|
||||||
|
cnt = cnt + 1
|
||||||
|
if cnt == 0:
|
||||||
|
DB = None
|
||||||
|
return DB
|
||||||
|
|
||||||
|
# else newer style .kinf file
|
||||||
|
# the .kinf file uses "/" to separate it into records
|
||||||
|
# so remove the trailing "/" to make it easy to use split
|
||||||
|
data = data[:-1]
|
||||||
|
items = data.split('/')
|
||||||
|
|
||||||
|
# loop through the item records until all are processed
|
||||||
|
while len(items) > 0:
|
||||||
|
|
||||||
|
# get the first item record
|
||||||
|
item = items.pop(0)
|
||||||
|
|
||||||
|
# the first 32 chars of the first record of a group
|
||||||
|
# is the MD5 hash of the key name encoded by charMap5
|
||||||
|
keyhash = item[0:32]
|
||||||
|
|
||||||
|
# the raw keyhash string is also used to create entropy for the actual
|
||||||
|
# CryptProtectData Blob that represents that keys contents
|
||||||
|
entropy = SHA1(keyhash)
|
||||||
|
|
||||||
|
# the remainder of the first record when decoded with charMap5
|
||||||
|
# has the ':' split char followed by the string representation
|
||||||
|
# of the number of records that follow
|
||||||
|
# and make up the contents
|
||||||
|
srcnt = decode(item[34:],charMap5)
|
||||||
|
rcnt = int(srcnt)
|
||||||
|
|
||||||
|
# read and store in rcnt records of data
|
||||||
|
# that make up the contents value
|
||||||
|
edlst = []
|
||||||
|
for i in xrange(rcnt):
|
||||||
|
item = items.pop(0)
|
||||||
|
edlst.append(item)
|
||||||
|
|
||||||
|
keyname = "unknown"
|
||||||
|
for name in names:
|
||||||
|
if encodeHash(name,charMap5) == keyhash:
|
||||||
|
keyname = name
|
||||||
|
break
|
||||||
|
if keyname == "unknown":
|
||||||
|
keyname = keyhash
|
||||||
|
|
||||||
|
# the charMap5 encoded contents data has had a length
|
||||||
|
# of chars (always odd) cut off of the front and moved
|
||||||
|
# to the end to prevent decoding using charMap5 from
|
||||||
|
# working properly, and thereby preventing the ensuing
|
||||||
|
# CryptUnprotectData call from succeeding.
|
||||||
|
|
||||||
|
# The offset into the charMap5 encoded contents seems to be:
|
||||||
|
# len(contents) - largest prime number less than or equal to int(len(content)/3)
|
||||||
|
# (in other words split "about" 2/3rds of the way through)
|
||||||
|
|
||||||
|
# move first offsets chars to end to align for decode by charMap5
|
||||||
|
encdata = "".join(edlst)
|
||||||
|
contlen = len(encdata)
|
||||||
|
noffset = contlen - primes(int(contlen/3))[-1]
|
||||||
|
|
||||||
|
# now properly split and recombine
|
||||||
|
# by moving noffset chars from the start of the
|
||||||
|
# string to the end of the string
|
||||||
|
pfx = encdata[0:noffset]
|
||||||
|
encdata = encdata[noffset:]
|
||||||
|
encdata = encdata + pfx
|
||||||
|
|
||||||
|
# decode using Map5 to get the CryptProtect Data
|
||||||
|
encryptedValue = decode(encdata,charMap5)
|
||||||
|
DB[keyname] = CryptUnprotectData(encryptedValue, entropy, 1)
|
||||||
|
cnt = cnt + 1
|
||||||
|
|
||||||
|
if cnt == 0:
|
||||||
|
DB = None
|
||||||
|
return DB
|
||||||
|
|
||||||
|
|
|
@ -11,16 +11,28 @@ from struct import pack, unpack, unpack_from
|
||||||
class DrmException(Exception):
|
class DrmException(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
global kindleDatabase
|
|
||||||
global charMap1
|
global charMap1
|
||||||
global charMap2
|
|
||||||
global charMap3
|
global charMap3
|
||||||
global charMap4
|
global charMap4
|
||||||
|
|
||||||
if sys.platform.startswith('win'):
|
if 'calibre' in sys.modules:
|
||||||
from k4pcutils import getKindleInfoFiles, parseKindleInfo, CryptUnprotectData, GetUserName, GetVolumeSerialNumber, charMap2
|
inCalibre = True
|
||||||
if sys.platform.startswith('darwin'):
|
else:
|
||||||
from k4mutils import getKindleInfoFiles, parseKindleInfo, CryptUnprotectData, GetUserName, GetVolumeSerialNumber, charMap2
|
inCalibre = False
|
||||||
|
|
||||||
|
if inCalibre:
|
||||||
|
if sys.platform.startswith('win'):
|
||||||
|
from calibre_plugins.k4mobidedrm.k4pcutils import getKindleInfoFiles, getDBfromFile, GetUserName, GetVolumeSerialNumber
|
||||||
|
|
||||||
|
if sys.platform.startswith('darwin'):
|
||||||
|
from calibre_plugins.k4mobidedrm.k4mutils import getKindleInfoFiles, getDBfromFile, GetUserName, GetVolumeSerialNumber
|
||||||
|
else:
|
||||||
|
if sys.platform.startswith('win'):
|
||||||
|
from k4pcutils import getKindleInfoFiles, getDBfromFile, GetUserName, GetVolumeSerialNumber
|
||||||
|
|
||||||
|
if sys.platform.startswith('darwin'):
|
||||||
|
from k4mutils import getKindleInfoFiles, getDBfromFile, GetUserName, GetVolumeSerialNumber
|
||||||
|
|
||||||
|
|
||||||
charMap1 = "n5Pr6St7Uv8Wx9YzAb0Cd1Ef2Gh3Jk4M"
|
charMap1 = "n5Pr6St7Uv8Wx9YzAb0Cd1Ef2Gh3Jk4M"
|
||||||
charMap3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
charMap3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
||||||
|
@ -66,50 +78,7 @@ def decode(data,map):
|
||||||
value = (((high * len(map)) ^ 0x80) & 0xFF) + low
|
value = (((high * len(map)) ^ 0x80) & 0xFF) + low
|
||||||
result += pack("B",value)
|
result += pack("B",value)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
# Get a record from the Kindle.info file for the key "hashedKey" (already hashed and encoded).
|
|
||||||
# Return the decoded and decrypted record
|
|
||||||
def getKindleInfoValueForHash(hashedKey):
|
|
||||||
global kindleDatabase
|
|
||||||
global charMap1
|
|
||||||
global charMap2
|
|
||||||
encryptedValue = decode(kindleDatabase[hashedKey],charMap2)
|
|
||||||
if sys.platform.startswith('win'):
|
|
||||||
return CryptUnprotectData(encryptedValue,"")
|
|
||||||
else:
|
|
||||||
cleartext = CryptUnprotectData(encryptedValue)
|
|
||||||
return decode(cleartext, charMap1)
|
|
||||||
|
|
||||||
# Get a record from the Kindle.info file for the string in "key" (plaintext).
|
|
||||||
# Return the decoded and decrypted record
|
|
||||||
def getKindleInfoValueForKey(key):
|
|
||||||
global charMap2
|
|
||||||
return getKindleInfoValueForHash(encodeHash(key,charMap2))
|
|
||||||
|
|
||||||
# Find if the original string for a hashed/encoded string is known.
|
|
||||||
# If so return the original string othwise return an empty string.
|
|
||||||
def findNameForHash(hash):
|
|
||||||
global charMap2
|
|
||||||
names = ["kindle.account.tokens","kindle.cookie.item","eulaVersionAccepted","login_date","kindle.token.item","login","kindle.key.item","kindle.name.info","kindle.device.info", "MazamaRandomNumber"]
|
|
||||||
result = ""
|
|
||||||
for name in names:
|
|
||||||
if hash == encodeHash(name, charMap2):
|
|
||||||
result = name
|
|
||||||
break
|
|
||||||
return result
|
|
||||||
|
|
||||||
# Print all the records from the kindle.info file (option -i)
|
|
||||||
def printKindleInfo():
|
|
||||||
for record in kindleDatabase:
|
|
||||||
name = findNameForHash(record)
|
|
||||||
if name != "" :
|
|
||||||
print (name)
|
|
||||||
print ("--------------------------")
|
|
||||||
else :
|
|
||||||
print ("Unknown Record")
|
|
||||||
print getKindleInfoValueForHash(record)
|
|
||||||
print "\n"
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# PID generation routines
|
# PID generation routines
|
||||||
#
|
#
|
||||||
|
@ -222,15 +191,15 @@ def getKindlePid(pidlst, rec209, token, serialnum):
|
||||||
return pidlst
|
return pidlst
|
||||||
|
|
||||||
|
|
||||||
# Parse the EXTH header records and parse the Kindleinfo
|
# parse the Kindleinfo file to calculate the book pid.
|
||||||
# file to calculate the book pid.
|
|
||||||
|
keynames = ["kindle.account.tokens","kindle.cookie.item","eulaVersionAccepted","login_date","kindle.token.item","login","kindle.key.item","kindle.name.info","kindle.device.info", "MazamaRandomNumber"]
|
||||||
|
|
||||||
def getK4Pids(pidlst, rec209, token, kInfoFile):
|
def getK4Pids(pidlst, rec209, token, kInfoFile):
|
||||||
global kindleDatabase
|
|
||||||
global charMap1
|
global charMap1
|
||||||
kindleDatabase = None
|
kindleDatabase = None
|
||||||
try:
|
try:
|
||||||
kindleDatabase = parseKindleInfo(kInfoFile)
|
kindleDatabase = getDBfromFile(kInfoFile)
|
||||||
except Exception, message:
|
except Exception, message:
|
||||||
print(message)
|
print(message)
|
||||||
kindleDatabase = None
|
kindleDatabase = None
|
||||||
|
@ -241,10 +210,10 @@ def getK4Pids(pidlst, rec209, token, kInfoFile):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Get the Mazama Random number
|
# Get the Mazama Random number
|
||||||
MazamaRandomNumber = getKindleInfoValueForKey("MazamaRandomNumber")
|
MazamaRandomNumber = kindleDatabase["MazamaRandomNumber"]
|
||||||
|
|
||||||
# Get the kindle account token
|
# Get the kindle account token
|
||||||
kindleAccountToken = getKindleInfoValueForKey("kindle.account.tokens")
|
kindleAccountToken = kindleDatabase["kindle.account.tokens"]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
print "Keys not found in " + kInfoFile
|
print "Keys not found in " + kInfoFile
|
||||||
return pidlst
|
return pidlst
|
|
@ -51,8 +51,9 @@
|
||||||
# 0.29 - It seems that the ideas about when multibyte trailing characters were
|
# 0.29 - It seems that the ideas about when multibyte trailing characters were
|
||||||
# included in the encryption were wrong. They aren't for DOC compressed
|
# included in the encryption were wrong. They aren't for DOC compressed
|
||||||
# files, but they are for HUFF/CDIC compress files!
|
# files, but they are for HUFF/CDIC compress files!
|
||||||
|
# 0.30 - Modified interface slightly to work better with new calibre plugin style
|
||||||
|
|
||||||
__version__ = '0.29'
|
__version__ = '0.30'
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
@ -163,6 +164,7 @@ class MobiBook:
|
||||||
def __init__(self, infile):
|
def __init__(self, infile):
|
||||||
# initial sanity check on file
|
# initial sanity check on file
|
||||||
self.data_file = file(infile, 'rb').read()
|
self.data_file = file(infile, 'rb').read()
|
||||||
|
self.mobi_data = ''
|
||||||
self.header = self.data_file[0:78]
|
self.header = self.data_file[0:78]
|
||||||
if self.header[0x3C:0x3C+8] != 'BOOKMOBI' and self.header[0x3C:0x3C+8] != 'TEXtREAd':
|
if self.header[0x3C:0x3C+8] != 'BOOKMOBI' and self.header[0x3C:0x3C+8] != 'TEXtREAd':
|
||||||
raise DrmException("invalid file format")
|
raise DrmException("invalid file format")
|
||||||
|
@ -301,13 +303,17 @@ class MobiBook:
|
||||||
break
|
break
|
||||||
return [found_key,pid]
|
return [found_key,pid]
|
||||||
|
|
||||||
|
def getMobiFile(self, outpath):
|
||||||
|
file(outpath,'wb').write(self.mobi_data)
|
||||||
|
|
||||||
def processBook(self, pidlist):
|
def processBook(self, pidlist):
|
||||||
crypto_type, = struct.unpack('>H', self.sect[0xC:0xC+2])
|
crypto_type, = struct.unpack('>H', self.sect[0xC:0xC+2])
|
||||||
print 'Crypto Type is: ', crypto_type
|
print 'Crypto Type is: ', crypto_type
|
||||||
self.crypto_type = crypto_type
|
self.crypto_type = crypto_type
|
||||||
if crypto_type == 0:
|
if crypto_type == 0:
|
||||||
print "This book is not encrypted."
|
print "This book is not encrypted."
|
||||||
return self.data_file
|
self.mobi_data = self.data_file
|
||||||
|
return
|
||||||
if crypto_type != 2 and crypto_type != 1:
|
if crypto_type != 2 and crypto_type != 1:
|
||||||
raise DrmException("Cannot decode unknown Mobipocket encryption type %d" % crypto_type)
|
raise DrmException("Cannot decode unknown Mobipocket encryption type %d" % crypto_type)
|
||||||
|
|
||||||
|
@ -353,33 +359,35 @@ class MobiBook:
|
||||||
|
|
||||||
# decrypt sections
|
# decrypt sections
|
||||||
print "Decrypting. Please wait . . .",
|
print "Decrypting. Please wait . . .",
|
||||||
new_data = self.data_file[:self.sections[1][0]]
|
self.mobi_data = self.data_file[:self.sections[1][0]]
|
||||||
for i in xrange(1, self.records+1):
|
for i in xrange(1, self.records+1):
|
||||||
data = self.loadSection(i)
|
data = self.loadSection(i)
|
||||||
extra_size = getSizeOfTrailingDataEntries(data, len(data), self.extra_data_flags)
|
extra_size = getSizeOfTrailingDataEntries(data, len(data), self.extra_data_flags)
|
||||||
if i%100 == 0:
|
if i%100 == 0:
|
||||||
print ".",
|
print ".",
|
||||||
# print "record %d, extra_size %d" %(i,extra_size)
|
# print "record %d, extra_size %d" %(i,extra_size)
|
||||||
new_data += PC1(found_key, data[0:len(data) - extra_size])
|
self.mobi_data += PC1(found_key, data[0:len(data) - extra_size])
|
||||||
if extra_size > 0:
|
if extra_size > 0:
|
||||||
new_data += data[-extra_size:]
|
self.mobi_data += data[-extra_size:]
|
||||||
if self.num_sections > self.records+1:
|
if self.num_sections > self.records+1:
|
||||||
new_data += self.data_file[self.sections[self.records+1][0]:]
|
self.mobi_data += self.data_file[self.sections[self.records+1][0]:]
|
||||||
self.data_file = new_data
|
|
||||||
print "done"
|
print "done"
|
||||||
return self.data_file
|
return
|
||||||
|
|
||||||
def getUnencryptedBook(infile,pid):
|
def getUnencryptedBook(infile,pid):
|
||||||
if not os.path.isfile(infile):
|
if not os.path.isfile(infile):
|
||||||
raise DrmException('Input File Not Found')
|
raise DrmException('Input File Not Found')
|
||||||
book = MobiBook(infile)
|
book = MobiBook(infile)
|
||||||
return book.processBook([pid])
|
book.processBook([pid])
|
||||||
|
return book.mobi_data
|
||||||
|
|
||||||
def getUnencryptedBookWithList(infile,pidlist):
|
def getUnencryptedBookWithList(infile,pidlist):
|
||||||
if not os.path.isfile(infile):
|
if not os.path.isfile(infile):
|
||||||
raise DrmException('Input File Not Found')
|
raise DrmException('Input File Not Found')
|
||||||
book = MobiBook(infile)
|
book = MobiBook(infile)
|
||||||
return book.processBook(pidlist)
|
book.processBook(pidlist)
|
||||||
|
return book.mobi_data
|
||||||
|
|
||||||
|
|
||||||
def main(argv=sys.argv):
|
def main(argv=sys.argv):
|
||||||
print ('MobiDeDrm v%(__version__)s. '
|
print ('MobiDeDrm v%(__version__)s. '
|
|
@ -10,7 +10,12 @@ class Unbuffered:
|
||||||
return getattr(self.stream, attr)
|
return getattr(self.stream, attr)
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
sys.stdout=Unbuffered(sys.stdout)
|
|
||||||
|
if 'calibre' in sys.modules:
|
||||||
|
inCalibre = True
|
||||||
|
else:
|
||||||
|
inCalibre = False
|
||||||
|
|
||||||
import os, csv, getopt
|
import os, csv, getopt
|
||||||
import zlib, zipfile, tempfile, shutil
|
import zlib, zipfile, tempfile, shutil
|
||||||
from struct import pack
|
from struct import pack
|
||||||
|
@ -18,10 +23,32 @@ from struct import unpack
|
||||||
|
|
||||||
class TpzDRMError(Exception):
|
class TpzDRMError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
# local support routines
|
# local support routines
|
||||||
import kgenpids
|
if inCalibre:
|
||||||
import genbook
|
from calibre_plugins.k4mobidedrm import kgenpids
|
||||||
|
from calibre_plugins.k4mobidedrm import genbook
|
||||||
|
else:
|
||||||
|
import kgenpids
|
||||||
|
import genbook
|
||||||
|
|
||||||
|
|
||||||
|
# recursive zip creation support routine
|
||||||
|
def zipUpDir(myzip, tdir, localname):
|
||||||
|
currentdir = tdir
|
||||||
|
if localname != "":
|
||||||
|
currentdir = os.path.join(currentdir,localname)
|
||||||
|
list = os.listdir(currentdir)
|
||||||
|
for file in list:
|
||||||
|
afilename = file
|
||||||
|
localfilePath = os.path.join(localname, afilename)
|
||||||
|
realfilePath = os.path.join(currentdir,file)
|
||||||
|
if os.path.isfile(realfilePath):
|
||||||
|
myzip.write(realfilePath, localfilePath)
|
||||||
|
elif os.path.isdir(realfilePath):
|
||||||
|
zipUpDir(myzip, tdir, localfilePath)
|
||||||
|
|
||||||
#
|
#
|
||||||
# Utility routines
|
# Utility routines
|
||||||
#
|
#
|
||||||
|
@ -110,9 +137,9 @@ def decryptDkeyRecords(data,PID):
|
||||||
|
|
||||||
|
|
||||||
class TopazBook:
|
class TopazBook:
|
||||||
def __init__(self, filename, outdir):
|
def __init__(self, filename):
|
||||||
self.fo = file(filename, 'rb')
|
self.fo = file(filename, 'rb')
|
||||||
self.outdir = outdir
|
self.outdir = tempfile.mkdtemp()
|
||||||
self.bookPayloadOffset = 0
|
self.bookPayloadOffset = 0
|
||||||
self.bookHeaderRecords = {}
|
self.bookHeaderRecords = {}
|
||||||
self.bookMetadata = {}
|
self.bookMetadata = {}
|
||||||
|
@ -317,21 +344,33 @@ class TopazBook:
|
||||||
file(outputFile, 'wb').write(record)
|
file(outputFile, 'wb').write(record)
|
||||||
print " "
|
print " "
|
||||||
|
|
||||||
|
def getHTMLZip(self, zipname):
|
||||||
|
htmlzip = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
||||||
|
htmlzip.write(os.path.join(self.outdir,'book.html'),'book.html')
|
||||||
|
htmlzip.write(os.path.join(self.outdir,'book.opf'),'book.opf')
|
||||||
|
if os.path.isfile(os.path.join(self.outdir,'cover.jpg')):
|
||||||
|
htmlzip.write(os.path.join(self.outdir,'cover.jpg'),'cover.jpg')
|
||||||
|
htmlzip.write(os.path.join(self.outdir,'style.css'),'style.css')
|
||||||
|
zipUpDir(htmlzip, self.outdir, 'img')
|
||||||
|
htmlzip.close()
|
||||||
|
|
||||||
def zipUpDir(myzip, tempdir,localname):
|
def getSVGZip(self, zipname):
|
||||||
currentdir = tempdir
|
svgzip = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
||||||
if localname != "":
|
svgzip.write(os.path.join(self.outdir,'index_svg.xhtml'),'index_svg.xhtml')
|
||||||
currentdir = os.path.join(currentdir,localname)
|
zipUpDir(svgzip, self.outdir, 'svg')
|
||||||
list = os.listdir(currentdir)
|
zipUpDir(svgzip, self.outdir, 'img')
|
||||||
for file in list:
|
svgzip.close()
|
||||||
afilename = file
|
|
||||||
localfilePath = os.path.join(localname, afilename)
|
|
||||||
realfilePath = os.path.join(currentdir,file)
|
|
||||||
if os.path.isfile(realfilePath):
|
|
||||||
myzip.write(realfilePath, localfilePath)
|
|
||||||
elif os.path.isdir(realfilePath):
|
|
||||||
zipUpDir(myzip, tempdir, localfilePath)
|
|
||||||
|
|
||||||
|
def getXMLZip(self, zipname):
|
||||||
|
xmlzip = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
||||||
|
targetdir = os.path.join(self.outdir,'xml')
|
||||||
|
zipUpDir(xmlzip, targetdir, '')
|
||||||
|
zipUpDir(xmlzip, self.outdir, 'img')
|
||||||
|
xmlzip.close()
|
||||||
|
|
||||||
|
def cleanup(self):
|
||||||
|
if os.path.isdir(self.outdir):
|
||||||
|
shutil.rmtree(self.outdir, True)
|
||||||
|
|
||||||
def usage(progname):
|
def usage(progname):
|
||||||
print "Removes DRM protection from Topaz ebooks and extract the contents"
|
print "Removes DRM protection from Topaz ebooks and extract the contents"
|
||||||
|
@ -383,58 +422,46 @@ def main(argv=sys.argv):
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
bookname = os.path.splitext(os.path.basename(infile))[0]
|
bookname = os.path.splitext(os.path.basename(infile))[0]
|
||||||
tempdir = tempfile.mkdtemp()
|
|
||||||
|
|
||||||
tb = TopazBook(infile, tempdir)
|
tb = TopazBook(infile)
|
||||||
title = tb.getBookTitle()
|
title = tb.getBookTitle()
|
||||||
print "Processing Book: ", title
|
print "Processing Book: ", title
|
||||||
keysRecord, keysRecordRecord = tb.getPIDMetaInfo()
|
keysRecord, keysRecordRecord = tb.getPIDMetaInfo()
|
||||||
pidlst = kgenpids.getPidList(keysRecord, keysRecordRecord, k4, pids, serials, kInfoFiles)
|
pidlst = kgenpids.getPidList(keysRecord, keysRecordRecord, k4, pids, serials, kInfoFiles)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
print "Decrypting Book"
|
||||||
tb.processBook(pidlst)
|
tb.processBook(pidlst)
|
||||||
|
|
||||||
|
print " Creating HTML ZIP Archive"
|
||||||
|
zipname = os.path.join(outdir, bookname + '_nodrm' + '.htmlz')
|
||||||
|
tb.getHTMLZip(zipname)
|
||||||
|
|
||||||
|
print " Creating SVG ZIP Archive"
|
||||||
|
zipname = os.path.join(outdir, bookname + '_SVG' + '.htmlz')
|
||||||
|
tb.getSVGZip(zipname)
|
||||||
|
|
||||||
|
print " Creating XML ZIP Archive"
|
||||||
|
zipname = os.path.join(outdir, bookname + '_XML' + '.zip')
|
||||||
|
tb.getXMLZip(zipname)
|
||||||
|
|
||||||
|
# removing internal temporary directory of pieces
|
||||||
|
tb.cleanup()
|
||||||
|
|
||||||
except TpzDRMError, e:
|
except TpzDRMError, e:
|
||||||
print str(e)
|
print str(e)
|
||||||
print " Creating DeBug Full Zip Archive of Book"
|
tb.cleanup()
|
||||||
zipname = os.path.join(outdir, bookname + '_debug' + '.zip')
|
|
||||||
myzip = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
|
||||||
zipUpDir(myzip, tempdir, '')
|
|
||||||
myzip.close()
|
|
||||||
shutil.rmtree(tempdir, True)
|
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
print " Creating HTML ZIP Archive"
|
except Exception, e:
|
||||||
zipname = os.path.join(outdir, bookname + '_nodrm' + '.zip')
|
print str(e)
|
||||||
myzip1 = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
tb.cleanup
|
||||||
myzip1.write(os.path.join(tempdir,'book.html'),'book.html')
|
return 1
|
||||||
myzip1.write(os.path.join(tempdir,'book.opf'),'book.opf')
|
|
||||||
if os.path.isfile(os.path.join(tempdir,'cover.jpg')):
|
|
||||||
myzip1.write(os.path.join(tempdir,'cover.jpg'),'cover.jpg')
|
|
||||||
myzip1.write(os.path.join(tempdir,'style.css'),'style.css')
|
|
||||||
zipUpDir(myzip1, tempdir, 'img')
|
|
||||||
myzip1.close()
|
|
||||||
|
|
||||||
print " Creating SVG ZIP Archive"
|
|
||||||
zipname = os.path.join(outdir, bookname + '_SVG' + '.zip')
|
|
||||||
myzip2 = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
|
||||||
myzip2.write(os.path.join(tempdir,'index_svg.xhtml'),'index_svg.xhtml')
|
|
||||||
zipUpDir(myzip2, tempdir, 'svg')
|
|
||||||
zipUpDir(myzip2, tempdir, 'img')
|
|
||||||
myzip2.close()
|
|
||||||
|
|
||||||
print " Creating XML ZIP Archive"
|
|
||||||
zipname = os.path.join(outdir, bookname + '_XML' + '.zip')
|
|
||||||
myzip3 = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
|
||||||
targetdir = os.path.join(tempdir,'xml')
|
|
||||||
zipUpDir(myzip3, targetdir, '')
|
|
||||||
zipUpDir(myzip3, tempdir, 'img')
|
|
||||||
myzip3.close()
|
|
||||||
|
|
||||||
shutil.rmtree(tempdir, True)
|
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
sys.stdout=Unbuffered(sys.stdout)
|
||||||
sys.exit(main())
|
sys.exit(main())
|
||||||
|
|
|
@ -1,374 +0,0 @@
|
||||||
#!/usr/bin/env python
|
|
||||||
|
|
||||||
from __future__ import with_statement
|
|
||||||
|
|
||||||
# engine to remove drm from Kindle for Mac and Kindle for PC books
|
|
||||||
# for personal use for archiving and converting your ebooks
|
|
||||||
|
|
||||||
# PLEASE DO NOT PIRATE EBOOKS!
|
|
||||||
|
|
||||||
# We want all authors and publishers, and eBook stores to live
|
|
||||||
# long and prosperous lives but at the same time we just want to
|
|
||||||
# be able to read OUR books on whatever device we want and to keep
|
|
||||||
# readable for a long, long time
|
|
||||||
|
|
||||||
# This borrows very heavily from works by CMBDTC, IHeartCabbages, skindle,
|
|
||||||
# unswindle, DarkReverser, ApprenticeAlf, DiapDealer, some_updates
|
|
||||||
# and many many others
|
|
||||||
|
|
||||||
# It can run standalone to convert K4M/K4PC/Mobi files, or it can be installed as a
|
|
||||||
# plugin for Calibre (http://calibre-ebook.com/about) so that importing
|
|
||||||
# K4 or Mobi with DRM is no londer a multi-step process.
|
|
||||||
#
|
|
||||||
# ***NOTE*** If you are using this script as a calibre plugin for a K4M or K4PC ebook
|
|
||||||
# then calibre must be installed on the same machine and in the same account as K4PC or K4M
|
|
||||||
# for the plugin version to function properly.
|
|
||||||
#
|
|
||||||
# To create a Calibre plugin, rename this file so that the filename
|
|
||||||
# ends in '_plugin.py', put it into a ZIP file with all its supporting python routines
|
|
||||||
# and import that ZIP into Calibre using its plugin configuration GUI.
|
|
||||||
|
|
||||||
|
|
||||||
__version__ = '2.8'
|
|
||||||
|
|
||||||
class Unbuffered:
|
|
||||||
def __init__(self, stream):
|
|
||||||
self.stream = stream
|
|
||||||
def write(self, data):
|
|
||||||
self.stream.write(data)
|
|
||||||
self.stream.flush()
|
|
||||||
def __getattr__(self, attr):
|
|
||||||
return getattr(self.stream, attr)
|
|
||||||
|
|
||||||
import sys
|
|
||||||
import os, csv, getopt
|
|
||||||
import string
|
|
||||||
import binascii
|
|
||||||
import zlib
|
|
||||||
import re
|
|
||||||
import zlib, zipfile, tempfile, shutil
|
|
||||||
from struct import pack, unpack, unpack_from
|
|
||||||
|
|
||||||
class DrmException(Exception):
|
|
||||||
pass
|
|
||||||
|
|
||||||
if 'calibre' in sys.modules:
|
|
||||||
inCalibre = True
|
|
||||||
else:
|
|
||||||
inCalibre = False
|
|
||||||
|
|
||||||
def zipUpDir(myzip, tempdir,localname):
|
|
||||||
currentdir = tempdir
|
|
||||||
if localname != "":
|
|
||||||
currentdir = os.path.join(currentdir,localname)
|
|
||||||
list = os.listdir(currentdir)
|
|
||||||
for file in list:
|
|
||||||
afilename = file
|
|
||||||
localfilePath = os.path.join(localname, afilename)
|
|
||||||
realfilePath = os.path.join(currentdir,file)
|
|
||||||
if os.path.isfile(realfilePath):
|
|
||||||
myzip.write(realfilePath, localfilePath)
|
|
||||||
elif os.path.isdir(realfilePath):
|
|
||||||
zipUpDir(myzip, tempdir, localfilePath)
|
|
||||||
|
|
||||||
# cleanup bytestring filenames
|
|
||||||
# borrowed from calibre from calibre/src/calibre/__init__.py
|
|
||||||
# added in removal of non-printing chars
|
|
||||||
# and removal of . at start
|
|
||||||
# convert spaces to underscores
|
|
||||||
def cleanup_name(name):
|
|
||||||
_filename_sanitize = re.compile(r'[\xae\0\\|\?\*<":>\+/]')
|
|
||||||
substitute='_'
|
|
||||||
one = ''.join(char for char in name if char in string.printable)
|
|
||||||
one = _filename_sanitize.sub(substitute, one)
|
|
||||||
one = re.sub(r'\s', ' ', one).strip()
|
|
||||||
one = re.sub(r'^\.+$', '_', one)
|
|
||||||
one = one.replace('..', substitute)
|
|
||||||
# Windows doesn't like path components that end with a period
|
|
||||||
if one.endswith('.'):
|
|
||||||
one = one[:-1]+substitute
|
|
||||||
# Mac and Unix don't like file names that begin with a full stop
|
|
||||||
if len(one) > 0 and one[0] == '.':
|
|
||||||
one = substitute+one[1:]
|
|
||||||
one = one.replace(' ','_')
|
|
||||||
return one
|
|
||||||
|
|
||||||
def decryptBook(infile, outdir, k4, kInfoFiles, serials, pids):
|
|
||||||
import mobidedrm
|
|
||||||
import topazextract
|
|
||||||
import kgenpids
|
|
||||||
|
|
||||||
# handle the obvious cases at the beginning
|
|
||||||
if not os.path.isfile(infile):
|
|
||||||
print "Error: Input file does not exist"
|
|
||||||
return 1
|
|
||||||
|
|
||||||
mobi = True
|
|
||||||
magic3 = file(infile,'rb').read(3)
|
|
||||||
if magic3 == 'TPZ':
|
|
||||||
mobi = False
|
|
||||||
|
|
||||||
bookname = os.path.splitext(os.path.basename(infile))[0]
|
|
||||||
|
|
||||||
if mobi:
|
|
||||||
mb = mobidedrm.MobiBook(infile)
|
|
||||||
else:
|
|
||||||
tempdir = tempfile.mkdtemp()
|
|
||||||
mb = topazextract.TopazBook(infile, tempdir)
|
|
||||||
|
|
||||||
title = mb.getBookTitle()
|
|
||||||
print "Processing Book: ", title
|
|
||||||
filenametitle = cleanup_name(title)
|
|
||||||
outfilename = bookname
|
|
||||||
if len(bookname)>4 and len(filenametitle)>4 and bookname[:4] != filenametitle[:4]:
|
|
||||||
outfilename = outfilename + "_" + filenametitle
|
|
||||||
|
|
||||||
# build pid list
|
|
||||||
md1, md2 = mb.getPIDMetaInfo()
|
|
||||||
pidlst = kgenpids.getPidList(md1, md2, k4, pids, serials, kInfoFiles)
|
|
||||||
|
|
||||||
try:
|
|
||||||
if mobi:
|
|
||||||
unlocked_file = mb.processBook(pidlst)
|
|
||||||
else:
|
|
||||||
mb.processBook(pidlst)
|
|
||||||
|
|
||||||
except mobidedrm.DrmException, e:
|
|
||||||
print "Error: " + str(e) + "\nDRM Removal Failed.\n"
|
|
||||||
return 1
|
|
||||||
except Exception, e:
|
|
||||||
if not mobi:
|
|
||||||
print "Error: " + str(e) + "\nDRM Removal Failed.\n"
|
|
||||||
print " Creating DeBug Full Zip Archive of Book"
|
|
||||||
zipname = os.path.join(outdir, bookname + '_debug' + '.zip')
|
|
||||||
myzip = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
|
||||||
zipUpDir(myzip, tempdir, '')
|
|
||||||
myzip.close()
|
|
||||||
shutil.rmtree(tempdir, True)
|
|
||||||
return 1
|
|
||||||
pass
|
|
||||||
|
|
||||||
if mobi:
|
|
||||||
outfile = os.path.join(outdir,outfilename + '_nodrm' + '.mobi')
|
|
||||||
file(outfile, 'wb').write(unlocked_file)
|
|
||||||
return 0
|
|
||||||
|
|
||||||
# topaz: build up zip archives of results
|
|
||||||
print " Creating HTML ZIP Archive"
|
|
||||||
zipname = os.path.join(outdir, outfilename + '_nodrm' + '.zip')
|
|
||||||
myzip1 = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
|
||||||
myzip1.write(os.path.join(tempdir,'book.html'),'book.html')
|
|
||||||
myzip1.write(os.path.join(tempdir,'book.opf'),'book.opf')
|
|
||||||
if os.path.isfile(os.path.join(tempdir,'cover.jpg')):
|
|
||||||
myzip1.write(os.path.join(tempdir,'cover.jpg'),'cover.jpg')
|
|
||||||
myzip1.write(os.path.join(tempdir,'style.css'),'style.css')
|
|
||||||
zipUpDir(myzip1, tempdir, 'img')
|
|
||||||
myzip1.close()
|
|
||||||
|
|
||||||
print " Creating SVG ZIP Archive"
|
|
||||||
zipname = os.path.join(outdir, outfilename + '_SVG' + '.zip')
|
|
||||||
myzip2 = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
|
||||||
myzip2.write(os.path.join(tempdir,'index_svg.xhtml'),'index_svg.xhtml')
|
|
||||||
zipUpDir(myzip2, tempdir, 'svg')
|
|
||||||
zipUpDir(myzip2, tempdir, 'img')
|
|
||||||
myzip2.close()
|
|
||||||
|
|
||||||
print " Creating XML ZIP Archive"
|
|
||||||
zipname = os.path.join(outdir, outfilename + '_XML' + '.zip')
|
|
||||||
myzip3 = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
|
|
||||||
targetdir = os.path.join(tempdir,'xml')
|
|
||||||
zipUpDir(myzip3, targetdir, '')
|
|
||||||
zipUpDir(myzip3, tempdir, 'img')
|
|
||||||
myzip3.close()
|
|
||||||
|
|
||||||
shutil.rmtree(tempdir, True)
|
|
||||||
return 0
|
|
||||||
|
|
||||||
|
|
||||||
def usage(progname):
|
|
||||||
print "Removes DRM protection from K4PC/M, Kindle, Mobi and Topaz ebooks"
|
|
||||||
print "Usage:"
|
|
||||||
print " %s [-k <kindle.info>] [-p <pidnums>] [-s <kindleSerialNumbers>] <infile> <outdir> " % progname
|
|
||||||
|
|
||||||
#
|
|
||||||
# Main
|
|
||||||
#
|
|
||||||
def main(argv=sys.argv):
|
|
||||||
progname = os.path.basename(argv[0])
|
|
||||||
|
|
||||||
k4 = False
|
|
||||||
kInfoFiles = []
|
|
||||||
serials = []
|
|
||||||
pids = []
|
|
||||||
|
|
||||||
print ('K4MobiDeDrm v%(__version__)s '
|
|
||||||
'provided by the work of many including DiapDealer, SomeUpdates, IHeartCabbages, CMBDTC, Skindle, DarkReverser, ApprenticeAlf, etc .' % globals())
|
|
||||||
|
|
||||||
print ' '
|
|
||||||
try:
|
|
||||||
opts, args = getopt.getopt(sys.argv[1:], "k:p:s:")
|
|
||||||
except getopt.GetoptError, err:
|
|
||||||
print str(err)
|
|
||||||
usage(progname)
|
|
||||||
sys.exit(2)
|
|
||||||
if len(args)<2:
|
|
||||||
usage(progname)
|
|
||||||
sys.exit(2)
|
|
||||||
|
|
||||||
for o, a in opts:
|
|
||||||
if o == "-k":
|
|
||||||
if a == None :
|
|
||||||
raise DrmException("Invalid parameter for -k")
|
|
||||||
kInfoFiles.append(a)
|
|
||||||
if o == "-p":
|
|
||||||
if a == None :
|
|
||||||
raise DrmException("Invalid parameter for -p")
|
|
||||||
pids = a.split(',')
|
|
||||||
if o == "-s":
|
|
||||||
if a == None :
|
|
||||||
raise DrmException("Invalid parameter for -s")
|
|
||||||
serials = a.split(',')
|
|
||||||
|
|
||||||
# try with built in Kindle Info files
|
|
||||||
k4 = True
|
|
||||||
if sys.platform.startswith('linux'):
|
|
||||||
k4 = False
|
|
||||||
kInfoFiles = None
|
|
||||||
infile = args[0]
|
|
||||||
outdir = args[1]
|
|
||||||
|
|
||||||
return decryptBook(infile, outdir, k4, kInfoFiles, serials, pids)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
sys.stdout=Unbuffered(sys.stdout)
|
|
||||||
sys.exit(main())
|
|
||||||
|
|
||||||
if not __name__ == "__main__" and inCalibre:
|
|
||||||
from calibre.customize import FileTypePlugin
|
|
||||||
|
|
||||||
class K4DeDRM(FileTypePlugin):
|
|
||||||
name = 'K4PC, K4Mac, Kindle Mobi and Topaz DeDRM' # Name of the plugin
|
|
||||||
description = 'Removes DRM from K4PC and Mac, Kindle Mobi and Topaz files. \
|
|
||||||
Provided by the work of many including DiapDealer, SomeUpdates, IHeartCabbages, CMBDTC, Skindle, DarkReverser, ApprenticeAlf, etc.'
|
|
||||||
supported_platforms = ['osx', 'windows', 'linux'] # Platforms this plugin will run on
|
|
||||||
author = 'DiapDealer, SomeUpdates' # The author of this plugin
|
|
||||||
version = (0, 2, 8) # The version number of this plugin
|
|
||||||
file_types = set(['prc','mobi','azw','azw1','tpz']) # The file types that this plugin will be applied to
|
|
||||||
on_import = True # Run this plugin during the import
|
|
||||||
priority = 210 # run this plugin before mobidedrm, k4pcdedrm, k4dedrm
|
|
||||||
|
|
||||||
def run(self, path_to_ebook):
|
|
||||||
from calibre.gui2 import is_ok_to_use_qt
|
|
||||||
from PyQt4.Qt import QMessageBox
|
|
||||||
from calibre.ptempfile import PersistentTemporaryDirectory
|
|
||||||
|
|
||||||
import kgenpids
|
|
||||||
import zlib
|
|
||||||
import zipfile
|
|
||||||
import topazextract
|
|
||||||
import mobidedrm
|
|
||||||
|
|
||||||
k4 = True
|
|
||||||
if sys.platform.startswith('linux'):
|
|
||||||
k4 = False
|
|
||||||
pids = []
|
|
||||||
serials = []
|
|
||||||
kInfoFiles = []
|
|
||||||
|
|
||||||
# Get supplied list of PIDs to try from plugin customization.
|
|
||||||
customvalues = self.site_customization.split(',')
|
|
||||||
for customvalue in customvalues:
|
|
||||||
customvalue = str(customvalue)
|
|
||||||
customvalue = customvalue.strip()
|
|
||||||
if len(customvalue) == 10 or len(customvalue) == 8:
|
|
||||||
pids.append(customvalue)
|
|
||||||
else :
|
|
||||||
if len(customvalue) == 16 and customvalue[0] == 'B':
|
|
||||||
serials.append(customvalue)
|
|
||||||
else:
|
|
||||||
print "%s is not a valid Kindle serial number or PID." % str(customvalue)
|
|
||||||
|
|
||||||
# Load any kindle info files (*.info) included Calibre's config directory.
|
|
||||||
try:
|
|
||||||
# Find Calibre's configuration directory.
|
|
||||||
confpath = os.path.split(os.path.split(self.plugin_path)[0])[0]
|
|
||||||
print 'K4MobiDeDRM: Calibre configuration directory = %s' % confpath
|
|
||||||
files = os.listdir(confpath)
|
|
||||||
filefilter = re.compile("\.info$", re.IGNORECASE)
|
|
||||||
files = filter(filefilter.search, files)
|
|
||||||
|
|
||||||
if files:
|
|
||||||
for filename in files:
|
|
||||||
fpath = os.path.join(confpath, filename)
|
|
||||||
kInfoFiles.append(fpath)
|
|
||||||
print 'K4MobiDeDRM: Kindle info file %s found in config folder.' % filename
|
|
||||||
except IOError:
|
|
||||||
print 'K4MobiDeDRM: Error reading kindle info files from config directory.'
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
mobi = True
|
|
||||||
magic3 = file(path_to_ebook,'rb').read(3)
|
|
||||||
if magic3 == 'TPZ':
|
|
||||||
mobi = False
|
|
||||||
|
|
||||||
bookname = os.path.splitext(os.path.basename(path_to_ebook))[0]
|
|
||||||
|
|
||||||
if mobi:
|
|
||||||
mb = mobidedrm.MobiBook(path_to_ebook)
|
|
||||||
else:
|
|
||||||
tempdir = PersistentTemporaryDirectory()
|
|
||||||
mb = topazextract.TopazBook(path_to_ebook, tempdir)
|
|
||||||
|
|
||||||
title = mb.getBookTitle()
|
|
||||||
md1, md2 = mb.getPIDMetaInfo()
|
|
||||||
pidlst = kgenpids.getPidList(md1, md2, k4, pids, serials, kInfoFiles)
|
|
||||||
|
|
||||||
try:
|
|
||||||
if mobi:
|
|
||||||
unlocked_file = mb.processBook(pidlst)
|
|
||||||
else:
|
|
||||||
mb.processBook(pidlst)
|
|
||||||
|
|
||||||
except mobidedrm.DrmException:
|
|
||||||
#if you reached here then no luck raise and exception
|
|
||||||
if is_ok_to_use_qt():
|
|
||||||
d = QMessageBox(QMessageBox.Warning, "K4MobiDeDRM Plugin", "Error decoding: %s\n" % path_to_ebook)
|
|
||||||
d.show()
|
|
||||||
d.raise_()
|
|
||||||
d.exec_()
|
|
||||||
raise Exception("K4MobiDeDRM plugin could not decode the file")
|
|
||||||
return ""
|
|
||||||
except topazextract.TpzDRMError:
|
|
||||||
#if you reached here then no luck raise and exception
|
|
||||||
if is_ok_to_use_qt():
|
|
||||||
d = QMessageBox(QMessageBox.Warning, "K4MobiDeDRM Plugin", "Error decoding: %s\n" % path_to_ebook)
|
|
||||||
d.show()
|
|
||||||
d.raise_()
|
|
||||||
d.exec_()
|
|
||||||
raise Exception("K4MobiDeDRM plugin could not decode the file")
|
|
||||||
return ""
|
|
||||||
|
|
||||||
print "Success!"
|
|
||||||
if mobi:
|
|
||||||
of = self.temporary_file(bookname+'.mobi')
|
|
||||||
of.write(unlocked_file)
|
|
||||||
of.close()
|
|
||||||
return of.name
|
|
||||||
|
|
||||||
# topaz: build up zip archives of results
|
|
||||||
print " Creating HTML ZIP Archive"
|
|
||||||
of = self.temporary_file(bookname + '.zip')
|
|
||||||
myzip = zipfile.ZipFile(of.name,'w',zipfile.ZIP_DEFLATED, False)
|
|
||||||
myzip.write(os.path.join(tempdir,'book.html'),'book.html')
|
|
||||||
myzip.write(os.path.join(tempdir,'book.opf'),'book.opf')
|
|
||||||
if os.path.isfile(os.path.join(tempdir,'cover.jpg')):
|
|
||||||
myzip.write(os.path.join(tempdir,'cover.jpg'),'cover.jpg')
|
|
||||||
myzip.write(os.path.join(tempdir,'style.css'),'style.css')
|
|
||||||
zipUpDir(myzip, tempdir, 'img')
|
|
||||||
myzip.close()
|
|
||||||
return of.name
|
|
||||||
|
|
||||||
def customization_help(self, gui=False):
|
|
||||||
return 'Enter 10 character PIDs and/or Kindle serial numbers, separated by commas.'
|
|
|
@ -1,200 +0,0 @@
|
||||||
# standlone set of Mac OSX specific routines needed for K4DeDRM
|
|
||||||
|
|
||||||
from __future__ import with_statement
|
|
||||||
import sys
|
|
||||||
import os
|
|
||||||
import subprocess
|
|
||||||
|
|
||||||
|
|
||||||
class DrmException(Exception):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
# interface to needed routines in openssl's libcrypto
|
|
||||||
def _load_crypto_libcrypto():
|
|
||||||
from ctypes import CDLL, byref, POINTER, c_void_p, c_char_p, c_int, c_long, \
|
|
||||||
Structure, c_ulong, create_string_buffer, addressof, string_at, cast
|
|
||||||
from ctypes.util import find_library
|
|
||||||
|
|
||||||
libcrypto = find_library('crypto')
|
|
||||||
if libcrypto is None:
|
|
||||||
raise DrmException('libcrypto not found')
|
|
||||||
libcrypto = CDLL(libcrypto)
|
|
||||||
|
|
||||||
AES_MAXNR = 14
|
|
||||||
c_char_pp = POINTER(c_char_p)
|
|
||||||
c_int_p = POINTER(c_int)
|
|
||||||
|
|
||||||
class AES_KEY(Structure):
|
|
||||||
_fields_ = [('rd_key', c_long * (4 * (AES_MAXNR + 1))), ('rounds', c_int)]
|
|
||||||
AES_KEY_p = POINTER(AES_KEY)
|
|
||||||
|
|
||||||
def F(restype, name, argtypes):
|
|
||||||
func = getattr(libcrypto, name)
|
|
||||||
func.restype = restype
|
|
||||||
func.argtypes = argtypes
|
|
||||||
return func
|
|
||||||
|
|
||||||
AES_cbc_encrypt = F(None, 'AES_cbc_encrypt',[c_char_p, c_char_p, c_ulong, AES_KEY_p, c_char_p,c_int])
|
|
||||||
|
|
||||||
AES_set_decrypt_key = F(c_int, 'AES_set_decrypt_key',[c_char_p, c_int, AES_KEY_p])
|
|
||||||
|
|
||||||
PKCS5_PBKDF2_HMAC_SHA1 = F(c_int, 'PKCS5_PBKDF2_HMAC_SHA1',
|
|
||||||
[c_char_p, c_ulong, c_char_p, c_ulong, c_ulong, c_ulong, c_char_p])
|
|
||||||
|
|
||||||
class LibCrypto(object):
|
|
||||||
def __init__(self):
|
|
||||||
self._blocksize = 0
|
|
||||||
self._keyctx = None
|
|
||||||
self.iv = 0
|
|
||||||
|
|
||||||
def set_decrypt_key(self, userkey, iv):
|
|
||||||
self._blocksize = len(userkey)
|
|
||||||
if (self._blocksize != 16) and (self._blocksize != 24) and (self._blocksize != 32) :
|
|
||||||
raise DrmException('AES improper key used')
|
|
||||||
return
|
|
||||||
keyctx = self._keyctx = AES_KEY()
|
|
||||||
self.iv = iv
|
|
||||||
rv = AES_set_decrypt_key(userkey, len(userkey) * 8, keyctx)
|
|
||||||
if rv < 0:
|
|
||||||
raise DrmException('Failed to initialize AES key')
|
|
||||||
|
|
||||||
def decrypt(self, data):
|
|
||||||
out = create_string_buffer(len(data))
|
|
||||||
rv = AES_cbc_encrypt(data, out, len(data), self._keyctx, self.iv, 0)
|
|
||||||
if rv == 0:
|
|
||||||
raise DrmException('AES decryption failed')
|
|
||||||
return out.raw
|
|
||||||
|
|
||||||
def keyivgen(self, passwd):
|
|
||||||
salt = '16743'
|
|
||||||
saltlen = 5
|
|
||||||
passlen = len(passwd)
|
|
||||||
iter = 0x3e8
|
|
||||||
keylen = 80
|
|
||||||
out = create_string_buffer(keylen)
|
|
||||||
rv = PKCS5_PBKDF2_HMAC_SHA1(passwd, passlen, salt, saltlen, iter, keylen, out)
|
|
||||||
return out.raw
|
|
||||||
return LibCrypto
|
|
||||||
|
|
||||||
def _load_crypto():
|
|
||||||
LibCrypto = None
|
|
||||||
try:
|
|
||||||
LibCrypto = _load_crypto_libcrypto()
|
|
||||||
except (ImportError, DrmException):
|
|
||||||
pass
|
|
||||||
return LibCrypto
|
|
||||||
|
|
||||||
LibCrypto = _load_crypto()
|
|
||||||
|
|
||||||
#
|
|
||||||
# Utility Routines
|
|
||||||
#
|
|
||||||
|
|
||||||
|
|
||||||
# Various character maps used to decrypt books. Probably supposed to act as obfuscation
|
|
||||||
charMap1 = "n5Pr6St7Uv8Wx9YzAb0Cd1Ef2Gh3Jk4M"
|
|
||||||
charMap2 = "ZB0bYyc1xDdW2wEV3Ff7KkPpL8UuGA4gz-Tme9Nn_tHh5SvXCsIiR6rJjQaqlOoM"
|
|
||||||
charMap3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
|
||||||
charMap4 = "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789"
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# uses a sub process to get the Hard Drive Serial Number using ioreg
|
|
||||||
# returns with the serial number of drive whose BSD Name is "disk0"
|
|
||||||
def GetVolumeSerialNumber():
|
|
||||||
sernum = os.getenv('MYSERIALNUMBER')
|
|
||||||
if sernum != None:
|
|
||||||
return sernum
|
|
||||||
cmdline = '/usr/sbin/ioreg -l -S -w 0 -r -c AppleAHCIDiskDriver'
|
|
||||||
cmdline = cmdline.encode(sys.getfilesystemencoding())
|
|
||||||
p = subprocess.Popen(cmdline, shell=True, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=False)
|
|
||||||
out1, out2 = p.communicate()
|
|
||||||
reslst = out1.split('\n')
|
|
||||||
cnt = len(reslst)
|
|
||||||
bsdname = None
|
|
||||||
sernum = None
|
|
||||||
foundIt = False
|
|
||||||
for j in xrange(cnt):
|
|
||||||
resline = reslst[j]
|
|
||||||
pp = resline.find('"Serial Number" = "')
|
|
||||||
if pp >= 0:
|
|
||||||
sernum = resline[pp+19:-1]
|
|
||||||
sernum = sernum.strip()
|
|
||||||
bb = resline.find('"BSD Name" = "')
|
|
||||||
if bb >= 0:
|
|
||||||
bsdname = resline[bb+14:-1]
|
|
||||||
bsdname = bsdname.strip()
|
|
||||||
if (bsdname == 'disk0') and (sernum != None):
|
|
||||||
foundIt = True
|
|
||||||
break
|
|
||||||
if not foundIt:
|
|
||||||
sernum = '9999999999'
|
|
||||||
return sernum
|
|
||||||
|
|
||||||
# uses unix env to get username instead of using sysctlbyname
|
|
||||||
def GetUserName():
|
|
||||||
username = os.getenv('USER')
|
|
||||||
return username
|
|
||||||
|
|
||||||
|
|
||||||
def encode(data, map):
|
|
||||||
result = ""
|
|
||||||
for char in data:
|
|
||||||
value = ord(char)
|
|
||||||
Q = (value ^ 0x80) // len(map)
|
|
||||||
R = value % len(map)
|
|
||||||
result += map[Q]
|
|
||||||
result += map[R]
|
|
||||||
return result
|
|
||||||
|
|
||||||
import hashlib
|
|
||||||
|
|
||||||
def SHA256(message):
|
|
||||||
ctx = hashlib.sha256()
|
|
||||||
ctx.update(message)
|
|
||||||
return ctx.digest()
|
|
||||||
|
|
||||||
# implements an Pseudo Mac Version of Windows built-in Crypto routine
|
|
||||||
def CryptUnprotectData(encryptedData):
|
|
||||||
sp = GetVolumeSerialNumber() + '!@#' + GetUserName()
|
|
||||||
passwdData = encode(SHA256(sp),charMap1)
|
|
||||||
crp = LibCrypto()
|
|
||||||
key_iv = crp.keyivgen(passwdData)
|
|
||||||
key = key_iv[0:32]
|
|
||||||
iv = key_iv[32:48]
|
|
||||||
crp.set_decrypt_key(key,iv)
|
|
||||||
cleartext = crp.decrypt(encryptedData)
|
|
||||||
return cleartext
|
|
||||||
|
|
||||||
|
|
||||||
# Locate the .kindle-info files
|
|
||||||
def getKindleInfoFiles(kInfoFiles):
|
|
||||||
home = os.getenv('HOME')
|
|
||||||
cmdline = 'find "' + home + '/Library/Application Support" -name ".kindle-info"'
|
|
||||||
cmdline = cmdline.encode(sys.getfilesystemencoding())
|
|
||||||
p1 = subprocess.Popen(cmdline, shell=True, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=False)
|
|
||||||
out1, out2 = p1.communicate()
|
|
||||||
reslst = out1.split('\n')
|
|
||||||
kinfopath = 'NONE'
|
|
||||||
found = False
|
|
||||||
cnt = len(reslst)
|
|
||||||
for resline in reslst:
|
|
||||||
if os.path.isfile(resline):
|
|
||||||
kInfoFiles.append(resline)
|
|
||||||
found = True
|
|
||||||
if not found:
|
|
||||||
print('No .kindle-info files have been found.')
|
|
||||||
return kInfoFiles
|
|
||||||
|
|
||||||
# Parse the Kindle.info file and return the records as a list of key-values
|
|
||||||
def parseKindleInfo(kInfoFile):
|
|
||||||
DB = {}
|
|
||||||
infoReader = open(kInfoFile, 'r')
|
|
||||||
infoReader.read(1)
|
|
||||||
data = infoReader.read()
|
|
||||||
items = data.split('[')
|
|
||||||
for item in items:
|
|
||||||
splito = item.split(':')
|
|
||||||
DB[splito[0]] =splito[1]
|
|
||||||
return DB
|
|
|
@ -1,117 +0,0 @@
|
||||||
# K4PC Windows specific routines
|
|
||||||
|
|
||||||
from __future__ import with_statement
|
|
||||||
|
|
||||||
import sys, os
|
|
||||||
|
|
||||||
from ctypes import windll, c_char_p, c_wchar_p, c_uint, POINTER, byref, \
|
|
||||||
create_unicode_buffer, create_string_buffer, CFUNCTYPE, addressof, \
|
|
||||||
string_at, Structure, c_void_p, cast
|
|
||||||
|
|
||||||
import _winreg as winreg
|
|
||||||
|
|
||||||
import traceback
|
|
||||||
|
|
||||||
MAX_PATH = 255
|
|
||||||
|
|
||||||
kernel32 = windll.kernel32
|
|
||||||
advapi32 = windll.advapi32
|
|
||||||
crypt32 = windll.crypt32
|
|
||||||
|
|
||||||
|
|
||||||
# Various character maps used to decrypt books. Probably supposed to act as obfuscation
|
|
||||||
charMap1 = "n5Pr6St7Uv8Wx9YzAb0Cd1Ef2Gh3Jk4M"
|
|
||||||
charMap2 = "AaZzB0bYyCc1XxDdW2wEeVv3FfUuG4g-TtHh5SsIiR6rJjQq7KkPpL8lOoMm9Nn_"
|
|
||||||
charMap3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
|
||||||
charMap4 = "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789"
|
|
||||||
|
|
||||||
class DrmException(Exception):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class DataBlob(Structure):
|
|
||||||
_fields_ = [('cbData', c_uint),
|
|
||||||
('pbData', c_void_p)]
|
|
||||||
DataBlob_p = POINTER(DataBlob)
|
|
||||||
|
|
||||||
|
|
||||||
def GetSystemDirectory():
|
|
||||||
GetSystemDirectoryW = kernel32.GetSystemDirectoryW
|
|
||||||
GetSystemDirectoryW.argtypes = [c_wchar_p, c_uint]
|
|
||||||
GetSystemDirectoryW.restype = c_uint
|
|
||||||
def GetSystemDirectory():
|
|
||||||
buffer = create_unicode_buffer(MAX_PATH + 1)
|
|
||||||
GetSystemDirectoryW(buffer, len(buffer))
|
|
||||||
return buffer.value
|
|
||||||
return GetSystemDirectory
|
|
||||||
GetSystemDirectory = GetSystemDirectory()
|
|
||||||
|
|
||||||
def GetVolumeSerialNumber():
|
|
||||||
GetVolumeInformationW = kernel32.GetVolumeInformationW
|
|
||||||
GetVolumeInformationW.argtypes = [c_wchar_p, c_wchar_p, c_uint,
|
|
||||||
POINTER(c_uint), POINTER(c_uint),
|
|
||||||
POINTER(c_uint), c_wchar_p, c_uint]
|
|
||||||
GetVolumeInformationW.restype = c_uint
|
|
||||||
def GetVolumeSerialNumber(path = GetSystemDirectory().split('\\')[0] + '\\'):
|
|
||||||
vsn = c_uint(0)
|
|
||||||
GetVolumeInformationW(path, None, 0, byref(vsn), None, None, None, 0)
|
|
||||||
return str(vsn.value)
|
|
||||||
return GetVolumeSerialNumber
|
|
||||||
GetVolumeSerialNumber = GetVolumeSerialNumber()
|
|
||||||
|
|
||||||
|
|
||||||
def GetUserName():
|
|
||||||
GetUserNameW = advapi32.GetUserNameW
|
|
||||||
GetUserNameW.argtypes = [c_wchar_p, POINTER(c_uint)]
|
|
||||||
GetUserNameW.restype = c_uint
|
|
||||||
def GetUserName():
|
|
||||||
buffer = create_unicode_buffer(32)
|
|
||||||
size = c_uint(len(buffer))
|
|
||||||
while not GetUserNameW(buffer, byref(size)):
|
|
||||||
buffer = create_unicode_buffer(len(buffer) * 2)
|
|
||||||
size.value = len(buffer)
|
|
||||||
return buffer.value.encode('utf-16-le')[::2]
|
|
||||||
return GetUserName
|
|
||||||
GetUserName = GetUserName()
|
|
||||||
|
|
||||||
|
|
||||||
def CryptUnprotectData():
|
|
||||||
_CryptUnprotectData = crypt32.CryptUnprotectData
|
|
||||||
_CryptUnprotectData.argtypes = [DataBlob_p, c_wchar_p, DataBlob_p,
|
|
||||||
c_void_p, c_void_p, c_uint, DataBlob_p]
|
|
||||||
_CryptUnprotectData.restype = c_uint
|
|
||||||
def CryptUnprotectData(indata, entropy):
|
|
||||||
indatab = create_string_buffer(indata)
|
|
||||||
indata = DataBlob(len(indata), cast(indatab, c_void_p))
|
|
||||||
entropyb = create_string_buffer(entropy)
|
|
||||||
entropy = DataBlob(len(entropy), cast(entropyb, c_void_p))
|
|
||||||
outdata = DataBlob()
|
|
||||||
if not _CryptUnprotectData(byref(indata), None, byref(entropy),
|
|
||||||
None, None, 0, byref(outdata)):
|
|
||||||
raise DrmException("Failed to Unprotect Data")
|
|
||||||
return string_at(outdata.pbData, outdata.cbData)
|
|
||||||
return CryptUnprotectData
|
|
||||||
CryptUnprotectData = CryptUnprotectData()
|
|
||||||
|
|
||||||
# Locate the .kindle-info files
|
|
||||||
def getKindleInfoFiles(kInfoFiles):
|
|
||||||
regkey = winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\\")
|
|
||||||
path = winreg.QueryValueEx(regkey, 'Local AppData')[0]
|
|
||||||
kinfopath = path +'\\Amazon\\Kindle For PC\\{AMAwzsaPaaZAzmZzZQzgZCAkZ3AjA_AY}\\kindle.info'
|
|
||||||
if not os.path.isfile(kinfopath):
|
|
||||||
print('The kindle.info files has not been found.')
|
|
||||||
else:
|
|
||||||
kInfoFiles.append(kinfopath)
|
|
||||||
return kInfoFiles
|
|
||||||
|
|
||||||
# Parse the Kindle.info file and return the records as a list of key-values
|
|
||||||
def parseKindleInfo(kInfoFile):
|
|
||||||
DB = {}
|
|
||||||
infoReader = open(kInfoFile, 'r')
|
|
||||||
infoReader.read(1)
|
|
||||||
data = infoReader.read()
|
|
||||||
items = data.split('{')
|
|
||||||
for item in items:
|
|
||||||
splito = item.split(':')
|
|
||||||
DB[splito[0]] =splito[1]
|
|
||||||
return DB
|
|
|
@ -1,409 +0,0 @@
|
||||||
#!/usr/bin/python
|
|
||||||
#
|
|
||||||
# This is a python script. You need a Python interpreter to run it.
|
|
||||||
# For example, ActiveState Python, which exists for windows.
|
|
||||||
#
|
|
||||||
# Changelog
|
|
||||||
# 0.01 - Initial version
|
|
||||||
# 0.02 - Huffdic compressed books were not properly decrypted
|
|
||||||
# 0.03 - Wasn't checking MOBI header length
|
|
||||||
# 0.04 - Wasn't sanity checking size of data record
|
|
||||||
# 0.05 - It seems that the extra data flags take two bytes not four
|
|
||||||
# 0.06 - And that low bit does mean something after all :-)
|
|
||||||
# 0.07 - The extra data flags aren't present in MOBI header < 0xE8 in size
|
|
||||||
# 0.08 - ...and also not in Mobi header version < 6
|
|
||||||
# 0.09 - ...but they are there with Mobi header version 6, header size 0xE4!
|
|
||||||
# 0.10 - Outputs unencrypted files as-is, so that when run as a Calibre
|
|
||||||
# import filter it works when importing unencrypted files.
|
|
||||||
# Also now handles encrypted files that don't need a specific PID.
|
|
||||||
# 0.11 - use autoflushed stdout and proper return values
|
|
||||||
# 0.12 - Fix for problems with metadata import as Calibre plugin, report errors
|
|
||||||
# 0.13 - Formatting fixes: retabbed file, removed trailing whitespace
|
|
||||||
# and extra blank lines, converted CR/LF pairs at ends of each line,
|
|
||||||
# and other cosmetic fixes.
|
|
||||||
# 0.14 - Working out when the extra data flags are present has been problematic
|
|
||||||
# Versions 7 through 9 have tried to tweak the conditions, but have been
|
|
||||||
# only partially successful. Closer examination of lots of sample
|
|
||||||
# files reveals that a confusion has arisen because trailing data entries
|
|
||||||
# are not encrypted, but it turns out that the multibyte entries
|
|
||||||
# in utf8 file are encrypted. (Although neither kind gets compressed.)
|
|
||||||
# This knowledge leads to a simplification of the test for the
|
|
||||||
# trailing data byte flags - version 5 and higher AND header size >= 0xE4.
|
|
||||||
# 0.15 - Now outputs 'heartbeat', and is also quicker for long files.
|
|
||||||
# 0.16 - And reverts to 'done' not 'done.' at the end for unswindle compatibility.
|
|
||||||
# 0.17 - added modifications to support its use as an imported python module
|
|
||||||
# both inside calibre and also in other places (ie K4DeDRM tools)
|
|
||||||
# 0.17a- disabled the standalone plugin feature since a plugin can not import
|
|
||||||
# a plugin
|
|
||||||
# 0.18 - It seems that multibyte entries aren't encrypted in a v7 file...
|
|
||||||
# Removed the disabled Calibre plug-in code
|
|
||||||
# Permit use of 8-digit PIDs
|
|
||||||
# 0.19 - It seems that multibyte entries aren't encrypted in a v6 file either.
|
|
||||||
# 0.20 - Correction: It seems that multibyte entries are encrypted in a v6 file.
|
|
||||||
# 0.21 - Added support for multiple pids
|
|
||||||
# 0.22 - revised structure to hold MobiBook as a class to allow an extended interface
|
|
||||||
# 0.23 - fixed problem with older files with no EXTH section
|
|
||||||
# 0.24 - add support for type 1 encryption and 'TEXtREAd' books as well
|
|
||||||
# 0.25 - Fixed support for 'BOOKMOBI' type 1 encryption
|
|
||||||
# 0.26 - Now enables Text-To-Speech flag and sets clipping limit to 100%
|
|
||||||
# 0.27 - Correct pid metadata token generation to match that used by skindle (Thank You Bart!)
|
|
||||||
# 0.28 - slight additional changes to metadata token generation (None -> '')
|
|
||||||
# 0.29 - It seems that the ideas about when multibyte trailing characters were
|
|
||||||
# included in the encryption were wrong. They aren't for DOC compressed
|
|
||||||
# files, but they are for HUFF/CDIC compress files!
|
|
||||||
|
|
||||||
__version__ = '0.29'
|
|
||||||
|
|
||||||
import sys
|
|
||||||
|
|
||||||
class Unbuffered:
|
|
||||||
def __init__(self, stream):
|
|
||||||
self.stream = stream
|
|
||||||
def write(self, data):
|
|
||||||
self.stream.write(data)
|
|
||||||
self.stream.flush()
|
|
||||||
def __getattr__(self, attr):
|
|
||||||
return getattr(self.stream, attr)
|
|
||||||
sys.stdout=Unbuffered(sys.stdout)
|
|
||||||
|
|
||||||
import os
|
|
||||||
import struct
|
|
||||||
import binascii
|
|
||||||
|
|
||||||
class DrmException(Exception):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
#
|
|
||||||
# MobiBook Utility Routines
|
|
||||||
#
|
|
||||||
|
|
||||||
# Implementation of Pukall Cipher 1
|
|
||||||
def PC1(key, src, decryption=True):
|
|
||||||
sum1 = 0;
|
|
||||||
sum2 = 0;
|
|
||||||
keyXorVal = 0;
|
|
||||||
if len(key)!=16:
|
|
||||||
print "Bad key length!"
|
|
||||||
return None
|
|
||||||
wkey = []
|
|
||||||
for i in xrange(8):
|
|
||||||
wkey.append(ord(key[i*2])<<8 | ord(key[i*2+1]))
|
|
||||||
dst = ""
|
|
||||||
for i in xrange(len(src)):
|
|
||||||
temp1 = 0;
|
|
||||||
byteXorVal = 0;
|
|
||||||
for j in xrange(8):
|
|
||||||
temp1 ^= wkey[j]
|
|
||||||
sum2 = (sum2+j)*20021 + sum1
|
|
||||||
sum1 = (temp1*346)&0xFFFF
|
|
||||||
sum2 = (sum2+sum1)&0xFFFF
|
|
||||||
temp1 = (temp1*20021+1)&0xFFFF
|
|
||||||
byteXorVal ^= temp1 ^ sum2
|
|
||||||
curByte = ord(src[i])
|
|
||||||
if not decryption:
|
|
||||||
keyXorVal = curByte * 257;
|
|
||||||
curByte = ((curByte ^ (byteXorVal >> 8)) ^ byteXorVal) & 0xFF
|
|
||||||
if decryption:
|
|
||||||
keyXorVal = curByte * 257;
|
|
||||||
for j in xrange(8):
|
|
||||||
wkey[j] ^= keyXorVal;
|
|
||||||
dst+=chr(curByte)
|
|
||||||
return dst
|
|
||||||
|
|
||||||
def checksumPid(s):
|
|
||||||
letters = "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789"
|
|
||||||
crc = (~binascii.crc32(s,-1))&0xFFFFFFFF
|
|
||||||
crc = crc ^ (crc >> 16)
|
|
||||||
res = s
|
|
||||||
l = len(letters)
|
|
||||||
for i in (0,1):
|
|
||||||
b = crc & 0xff
|
|
||||||
pos = (b // l) ^ (b % l)
|
|
||||||
res += letters[pos%l]
|
|
||||||
crc >>= 8
|
|
||||||
return res
|
|
||||||
|
|
||||||
def getSizeOfTrailingDataEntries(ptr, size, flags):
|
|
||||||
def getSizeOfTrailingDataEntry(ptr, size):
|
|
||||||
bitpos, result = 0, 0
|
|
||||||
if size <= 0:
|
|
||||||
return result
|
|
||||||
while True:
|
|
||||||
v = ord(ptr[size-1])
|
|
||||||
result |= (v & 0x7F) << bitpos
|
|
||||||
bitpos += 7
|
|
||||||
size -= 1
|
|
||||||
if (v & 0x80) != 0 or (bitpos >= 28) or (size == 0):
|
|
||||||
return result
|
|
||||||
num = 0
|
|
||||||
testflags = flags >> 1
|
|
||||||
while testflags:
|
|
||||||
if testflags & 1:
|
|
||||||
num += getSizeOfTrailingDataEntry(ptr, size - num)
|
|
||||||
testflags >>= 1
|
|
||||||
# Check the low bit to see if there's multibyte data present.
|
|
||||||
# if multibyte data is included in the encryped data, we'll
|
|
||||||
# have already cleared this flag.
|
|
||||||
if flags & 1:
|
|
||||||
num += (ord(ptr[size - num - 1]) & 0x3) + 1
|
|
||||||
return num
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class MobiBook:
|
|
||||||
def loadSection(self, section):
|
|
||||||
if (section + 1 == self.num_sections):
|
|
||||||
endoff = len(self.data_file)
|
|
||||||
else:
|
|
||||||
endoff = self.sections[section + 1][0]
|
|
||||||
off = self.sections[section][0]
|
|
||||||
return self.data_file[off:endoff]
|
|
||||||
|
|
||||||
def __init__(self, infile):
|
|
||||||
# initial sanity check on file
|
|
||||||
self.data_file = file(infile, 'rb').read()
|
|
||||||
self.header = self.data_file[0:78]
|
|
||||||
if self.header[0x3C:0x3C+8] != 'BOOKMOBI' and self.header[0x3C:0x3C+8] != 'TEXtREAd':
|
|
||||||
raise DrmException("invalid file format")
|
|
||||||
self.magic = self.header[0x3C:0x3C+8]
|
|
||||||
self.crypto_type = -1
|
|
||||||
|
|
||||||
# build up section offset and flag info
|
|
||||||
self.num_sections, = struct.unpack('>H', self.header[76:78])
|
|
||||||
self.sections = []
|
|
||||||
for i in xrange(self.num_sections):
|
|
||||||
offset, a1,a2,a3,a4 = struct.unpack('>LBBBB', self.data_file[78+i*8:78+i*8+8])
|
|
||||||
flags, val = a1, a2<<16|a3<<8|a4
|
|
||||||
self.sections.append( (offset, flags, val) )
|
|
||||||
|
|
||||||
# parse information from section 0
|
|
||||||
self.sect = self.loadSection(0)
|
|
||||||
self.records, = struct.unpack('>H', self.sect[0x8:0x8+2])
|
|
||||||
self.compression, = struct.unpack('>H', self.sect[0x0:0x0+2])
|
|
||||||
|
|
||||||
if self.magic == 'TEXtREAd':
|
|
||||||
print "Book has format: ", self.magic
|
|
||||||
self.extra_data_flags = 0
|
|
||||||
self.mobi_length = 0
|
|
||||||
self.mobi_version = -1
|
|
||||||
self.meta_array = {}
|
|
||||||
return
|
|
||||||
self.mobi_length, = struct.unpack('>L',self.sect[0x14:0x18])
|
|
||||||
self.mobi_version, = struct.unpack('>L',self.sect[0x68:0x6C])
|
|
||||||
print "MOBI header version = %d, length = %d" %(self.mobi_version, self.mobi_length)
|
|
||||||
self.extra_data_flags = 0
|
|
||||||
if (self.mobi_length >= 0xE4) and (self.mobi_version >= 5):
|
|
||||||
self.extra_data_flags, = struct.unpack('>H', self.sect[0xF2:0xF4])
|
|
||||||
print "Extra Data Flags = %d" % self.extra_data_flags
|
|
||||||
if (self.mobi_version < 7) and (self.compression != 17480):
|
|
||||||
# multibyte utf8 data is included in the encryption for mobi_version 6 and below
|
|
||||||
# so clear that byte so that we leave it to be decrypted.
|
|
||||||
self.extra_data_flags &= 0xFFFE
|
|
||||||
|
|
||||||
# if exth region exists parse it for metadata array
|
|
||||||
self.meta_array = {}
|
|
||||||
try:
|
|
||||||
exth_flag, = struct.unpack('>L', self.sect[0x80:0x84])
|
|
||||||
exth = 'NONE'
|
|
||||||
if exth_flag & 0x40:
|
|
||||||
exth = self.sect[16 + self.mobi_length:]
|
|
||||||
if (len(exth) >= 4) and (exth[:4] == 'EXTH'):
|
|
||||||
nitems, = struct.unpack('>I', exth[8:12])
|
|
||||||
pos = 12
|
|
||||||
for i in xrange(nitems):
|
|
||||||
type, size = struct.unpack('>II', exth[pos: pos + 8])
|
|
||||||
content = exth[pos + 8: pos + size]
|
|
||||||
self.meta_array[type] = content
|
|
||||||
# reset the text to speech flag and clipping limit, if present
|
|
||||||
if type == 401 and size == 9:
|
|
||||||
# set clipping limit to 100%
|
|
||||||
self.patchSection(0, "\144", 16 + self.mobi_length + pos + 8)
|
|
||||||
elif type == 404 and size == 9:
|
|
||||||
# make sure text to speech is enabled
|
|
||||||
self.patchSection(0, "\0", 16 + self.mobi_length + pos + 8)
|
|
||||||
# print type, size, content, content.encode('hex')
|
|
||||||
pos += size
|
|
||||||
except:
|
|
||||||
self.meta_array = {}
|
|
||||||
pass
|
|
||||||
|
|
||||||
def getBookTitle(self):
|
|
||||||
title = ''
|
|
||||||
if 503 in self.meta_array:
|
|
||||||
title = self.meta_array[503]
|
|
||||||
else :
|
|
||||||
toff, tlen = struct.unpack('>II', self.sect[0x54:0x5c])
|
|
||||||
tend = toff + tlen
|
|
||||||
title = self.sect[toff:tend]
|
|
||||||
if title == '':
|
|
||||||
title = self.header[:32]
|
|
||||||
title = title.split("\0")[0]
|
|
||||||
return title
|
|
||||||
|
|
||||||
def getPIDMetaInfo(self):
|
|
||||||
rec209 = ''
|
|
||||||
token = ''
|
|
||||||
if 209 in self.meta_array:
|
|
||||||
rec209 = self.meta_array[209]
|
|
||||||
data = rec209
|
|
||||||
# The 209 data comes in five byte groups. Interpret the last four bytes
|
|
||||||
# of each group as a big endian unsigned integer to get a key value
|
|
||||||
# if that key exists in the meta_array, append its contents to the token
|
|
||||||
for i in xrange(0,len(data),5):
|
|
||||||
val, = struct.unpack('>I',data[i+1:i+5])
|
|
||||||
sval = self.meta_array.get(val,'')
|
|
||||||
token += sval
|
|
||||||
return rec209, token
|
|
||||||
|
|
||||||
def patch(self, off, new):
|
|
||||||
self.data_file = self.data_file[:off] + new + self.data_file[off+len(new):]
|
|
||||||
|
|
||||||
def patchSection(self, section, new, in_off = 0):
|
|
||||||
if (section + 1 == self.num_sections):
|
|
||||||
endoff = len(self.data_file)
|
|
||||||
else:
|
|
||||||
endoff = self.sections[section + 1][0]
|
|
||||||
off = self.sections[section][0]
|
|
||||||
assert off + in_off + len(new) <= endoff
|
|
||||||
self.patch(off + in_off, new)
|
|
||||||
|
|
||||||
def parseDRM(self, data, count, pidlist):
|
|
||||||
found_key = None
|
|
||||||
keyvec1 = "\x72\x38\x33\xB0\xB4\xF2\xE3\xCA\xDF\x09\x01\xD6\xE2\xE0\x3F\x96"
|
|
||||||
for pid in pidlist:
|
|
||||||
bigpid = pid.ljust(16,'\0')
|
|
||||||
temp_key = PC1(keyvec1, bigpid, False)
|
|
||||||
temp_key_sum = sum(map(ord,temp_key)) & 0xff
|
|
||||||
found_key = None
|
|
||||||
for i in xrange(count):
|
|
||||||
verification, size, type, cksum, cookie = struct.unpack('>LLLBxxx32s', data[i*0x30:i*0x30+0x30])
|
|
||||||
if cksum == temp_key_sum:
|
|
||||||
cookie = PC1(temp_key, cookie)
|
|
||||||
ver,flags,finalkey,expiry,expiry2 = struct.unpack('>LL16sLL', cookie)
|
|
||||||
if verification == ver and (flags & 0x1F) == 1:
|
|
||||||
found_key = finalkey
|
|
||||||
break
|
|
||||||
if found_key != None:
|
|
||||||
break
|
|
||||||
if not found_key:
|
|
||||||
# Then try the default encoding that doesn't require a PID
|
|
||||||
pid = "00000000"
|
|
||||||
temp_key = keyvec1
|
|
||||||
temp_key_sum = sum(map(ord,temp_key)) & 0xff
|
|
||||||
for i in xrange(count):
|
|
||||||
verification, size, type, cksum, cookie = struct.unpack('>LLLBxxx32s', data[i*0x30:i*0x30+0x30])
|
|
||||||
if cksum == temp_key_sum:
|
|
||||||
cookie = PC1(temp_key, cookie)
|
|
||||||
ver,flags,finalkey,expiry,expiry2 = struct.unpack('>LL16sLL', cookie)
|
|
||||||
if verification == ver:
|
|
||||||
found_key = finalkey
|
|
||||||
break
|
|
||||||
return [found_key,pid]
|
|
||||||
|
|
||||||
def processBook(self, pidlist):
|
|
||||||
crypto_type, = struct.unpack('>H', self.sect[0xC:0xC+2])
|
|
||||||
print 'Crypto Type is: ', crypto_type
|
|
||||||
self.crypto_type = crypto_type
|
|
||||||
if crypto_type == 0:
|
|
||||||
print "This book is not encrypted."
|
|
||||||
return self.data_file
|
|
||||||
if crypto_type != 2 and crypto_type != 1:
|
|
||||||
raise DrmException("Cannot decode unknown Mobipocket encryption type %d" % crypto_type)
|
|
||||||
|
|
||||||
goodpids = []
|
|
||||||
for pid in pidlist:
|
|
||||||
if len(pid)==10:
|
|
||||||
if checksumPid(pid[0:-2]) != pid:
|
|
||||||
print "Warning: PID " + pid + " has incorrect checksum, should have been "+checksumPid(pid[0:-2])
|
|
||||||
goodpids.append(pid[0:-2])
|
|
||||||
elif len(pid)==8:
|
|
||||||
goodpids.append(pid)
|
|
||||||
|
|
||||||
if self.crypto_type == 1:
|
|
||||||
t1_keyvec = "QDCVEPMU675RUBSZ"
|
|
||||||
if self.magic == 'TEXtREAd':
|
|
||||||
bookkey_data = self.sect[0x0E:0x0E+16]
|
|
||||||
elif self.mobi_version < 0:
|
|
||||||
bookkey_data = self.sect[0x90:0x90+16]
|
|
||||||
else:
|
|
||||||
bookkey_data = self.sect[self.mobi_length+16:self.mobi_length+32]
|
|
||||||
pid = "00000000"
|
|
||||||
found_key = PC1(t1_keyvec, bookkey_data)
|
|
||||||
else :
|
|
||||||
# calculate the keys
|
|
||||||
drm_ptr, drm_count, drm_size, drm_flags = struct.unpack('>LLLL', self.sect[0xA8:0xA8+16])
|
|
||||||
if drm_count == 0:
|
|
||||||
raise DrmException("Not yet initialised with PID. Must be opened with Mobipocket Reader first.")
|
|
||||||
found_key, pid = self.parseDRM(self.sect[drm_ptr:drm_ptr+drm_size], drm_count, goodpids)
|
|
||||||
if not found_key:
|
|
||||||
raise DrmException("No key found. Most likely the correct PID has not been given.")
|
|
||||||
# kill the drm keys
|
|
||||||
self.patchSection(0, "\0" * drm_size, drm_ptr)
|
|
||||||
# kill the drm pointers
|
|
||||||
self.patchSection(0, "\xff" * 4 + "\0" * 12, 0xA8)
|
|
||||||
|
|
||||||
if pid=="00000000":
|
|
||||||
print "File has default encryption, no specific PID."
|
|
||||||
else:
|
|
||||||
print "File is encoded with PID "+checksumPid(pid)+"."
|
|
||||||
|
|
||||||
# clear the crypto type
|
|
||||||
self.patchSection(0, "\0" * 2, 0xC)
|
|
||||||
|
|
||||||
# decrypt sections
|
|
||||||
print "Decrypting. Please wait . . .",
|
|
||||||
new_data = self.data_file[:self.sections[1][0]]
|
|
||||||
for i in xrange(1, self.records+1):
|
|
||||||
data = self.loadSection(i)
|
|
||||||
extra_size = getSizeOfTrailingDataEntries(data, len(data), self.extra_data_flags)
|
|
||||||
if i%100 == 0:
|
|
||||||
print ".",
|
|
||||||
# print "record %d, extra_size %d" %(i,extra_size)
|
|
||||||
new_data += PC1(found_key, data[0:len(data) - extra_size])
|
|
||||||
if extra_size > 0:
|
|
||||||
new_data += data[-extra_size:]
|
|
||||||
if self.num_sections > self.records+1:
|
|
||||||
new_data += self.data_file[self.sections[self.records+1][0]:]
|
|
||||||
self.data_file = new_data
|
|
||||||
print "done"
|
|
||||||
return self.data_file
|
|
||||||
|
|
||||||
def getUnencryptedBook(infile,pid):
|
|
||||||
if not os.path.isfile(infile):
|
|
||||||
raise DrmException('Input File Not Found')
|
|
||||||
book = MobiBook(infile)
|
|
||||||
return book.processBook([pid])
|
|
||||||
|
|
||||||
def getUnencryptedBookWithList(infile,pidlist):
|
|
||||||
if not os.path.isfile(infile):
|
|
||||||
raise DrmException('Input File Not Found')
|
|
||||||
book = MobiBook(infile)
|
|
||||||
return book.processBook(pidlist)
|
|
||||||
|
|
||||||
def main(argv=sys.argv):
|
|
||||||
print ('MobiDeDrm v%(__version__)s. '
|
|
||||||
'Copyright 2008-2010 The Dark Reverser.' % globals())
|
|
||||||
if len(argv)<3 or len(argv)>4:
|
|
||||||
print "Removes protection from Mobipocket books"
|
|
||||||
print "Usage:"
|
|
||||||
print " %s <infile> <outfile> [<Comma separated list of PIDs to try>]" % sys.argv[0]
|
|
||||||
return 1
|
|
||||||
else:
|
|
||||||
infile = argv[1]
|
|
||||||
outfile = argv[2]
|
|
||||||
if len(argv) is 4:
|
|
||||||
pidlist = argv[3].split(',')
|
|
||||||
else:
|
|
||||||
pidlist = {}
|
|
||||||
try:
|
|
||||||
stripped_file = getUnencryptedBookWithList(infile, pidlist)
|
|
||||||
file(outfile, 'wb').write(stripped_file)
|
|
||||||
except DrmException, e:
|
|
||||||
print "Error: %s" % e
|
|
||||||
return 1
|
|
||||||
return 0
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
sys.exit(main())
|
|
|
@ -1,299 +0,0 @@
|
||||||
#!/usr/bin/env python
|
|
||||||
# vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
|
|
||||||
|
|
||||||
import sys
|
|
||||||
sys.path.append('lib')
|
|
||||||
import os, os.path, urllib
|
|
||||||
import subprocess
|
|
||||||
from subprocess import Popen, PIPE, STDOUT
|
|
||||||
import subasyncio
|
|
||||||
from subasyncio import Process
|
|
||||||
import Tkinter
|
|
||||||
import Tkconstants
|
|
||||||
import tkFileDialog
|
|
||||||
import tkMessageBox
|
|
||||||
from scrolltextwidget import ScrolledText
|
|
||||||
import binascii
|
|
||||||
import hashlib
|
|
||||||
|
|
||||||
|
|
||||||
#
|
|
||||||
# Returns the SHA1 digest of "message"
|
|
||||||
#
|
|
||||||
def SHA1(message):
|
|
||||||
ctx = hashlib.sha1()
|
|
||||||
ctx.update(message)
|
|
||||||
return ctx.hexdigest()
|
|
||||||
|
|
||||||
|
|
||||||
class MainDialog(Tkinter.Frame):
|
|
||||||
def __init__(self, root):
|
|
||||||
Tkinter.Frame.__init__(self, root, border=5)
|
|
||||||
self.root = root
|
|
||||||
self.interval = 2000
|
|
||||||
self.p2 = None
|
|
||||||
self.status = Tkinter.Label(self, text='Remove Encryption from Kindle for Mac Mobi eBook')
|
|
||||||
self.status.pack(fill=Tkconstants.X, expand=1)
|
|
||||||
body = Tkinter.Frame(self)
|
|
||||||
body.pack(fill=Tkconstants.X, expand=1)
|
|
||||||
sticky = Tkconstants.E + Tkconstants.W
|
|
||||||
body.grid_columnconfigure(1, weight=2)
|
|
||||||
|
|
||||||
Tkinter.Label(body, text='Locate your Kindle Applications').grid(row=0, sticky=Tkconstants.E)
|
|
||||||
self.k4mpath = Tkinter.Entry(body, width=50)
|
|
||||||
self.k4mpath.grid(row=0, column=1, sticky=sticky)
|
|
||||||
self.appname = '/Applications/Kindle for Mac.app'
|
|
||||||
if not os.path.exists(self.appname):
|
|
||||||
self.appname = '/Applications/Kindle.app'
|
|
||||||
cwd = self.appname
|
|
||||||
cwd = cwd.encode('utf-8')
|
|
||||||
self.k4mpath.insert(0, cwd)
|
|
||||||
button = Tkinter.Button(body, text="...", command=self.get_k4mpath)
|
|
||||||
button.grid(row=0, column=2)
|
|
||||||
|
|
||||||
Tkinter.Label(body, text='Directory for Unencrypted Output File').grid(row=1, sticky=Tkconstants.E)
|
|
||||||
self.outpath = Tkinter.Entry(body, width=50)
|
|
||||||
self.outpath.grid(row=1, column=1, sticky=sticky)
|
|
||||||
desktoppath = os.getenv('HOME') + '/Desktop/'
|
|
||||||
desktoppath = desktoppath.encode('utf-8')
|
|
||||||
self.outpath.insert(0, desktoppath)
|
|
||||||
button = Tkinter.Button(body, text="...", command=self.get_outpath)
|
|
||||||
button.grid(row=1, column=2)
|
|
||||||
|
|
||||||
msg1 = 'Conversion Log \n\n'
|
|
||||||
self.stext = ScrolledText(body, bd=5, relief=Tkconstants.RIDGE, height=15, width=60, wrap=Tkconstants.WORD)
|
|
||||||
self.stext.grid(row=3, column=0, columnspan=2,sticky=sticky)
|
|
||||||
self.stext.insert(Tkconstants.END,msg1)
|
|
||||||
|
|
||||||
buttons = Tkinter.Frame(self)
|
|
||||||
buttons.pack()
|
|
||||||
self.sbotton = Tkinter.Button(
|
|
||||||
buttons, text="Start", width=10, command=self.convertit)
|
|
||||||
self.sbotton.pack(side=Tkconstants.LEFT)
|
|
||||||
|
|
||||||
Tkinter.Frame(buttons, width=10).pack(side=Tkconstants.LEFT)
|
|
||||||
self.qbutton = Tkinter.Button(
|
|
||||||
buttons, text="Quit", width=10, command=self.quitting)
|
|
||||||
self.qbutton.pack(side=Tkconstants.RIGHT)
|
|
||||||
|
|
||||||
# read from subprocess pipe without blocking
|
|
||||||
# invoked every interval via the widget "after"
|
|
||||||
# option being used, so need to reset it for the next time
|
|
||||||
def processPipe(self):
|
|
||||||
poll = self.p2.wait('nowait')
|
|
||||||
if poll != None:
|
|
||||||
text = self.p2.readerr()
|
|
||||||
text += self.p2.read()
|
|
||||||
msg = text + '\n\n' + 'Encryption successfully removed\n'
|
|
||||||
if poll != 0:
|
|
||||||
msg = text + '\n\n' + 'Error: Encryption Removal Failed\n'
|
|
||||||
self.showCmdOutput(msg)
|
|
||||||
self.p2 = None
|
|
||||||
self.sbotton.configure(state='normal')
|
|
||||||
return
|
|
||||||
text = self.p2.readerr()
|
|
||||||
text += self.p2.read()
|
|
||||||
self.showCmdOutput(text)
|
|
||||||
# make sure we get invoked again by event loop after interval
|
|
||||||
self.stext.after(self.interval,self.processPipe)
|
|
||||||
return
|
|
||||||
|
|
||||||
# post output from subprocess in scrolled text widget
|
|
||||||
def showCmdOutput(self, msg):
|
|
||||||
if msg and msg !='':
|
|
||||||
msg = msg.encode('utf-8')
|
|
||||||
self.stext.insert(Tkconstants.END,msg)
|
|
||||||
self.stext.yview_pickplace(Tkconstants.END)
|
|
||||||
return
|
|
||||||
|
|
||||||
# run as a subprocess via pipes and collect stdout
|
|
||||||
def mobirdr(self, infile, outfile, pidnum):
|
|
||||||
cmdline = 'python ./lib/mobidedrm.py "' + infile + '" "' + outfile + '" "' + pidnum + '"'
|
|
||||||
cmdline = cmdline.encode(sys.getfilesystemencoding())
|
|
||||||
p2 = Process(cmdline, shell=True, bufsize=1, stdin=None, stdout=PIPE, stderr=PIPE, close_fds=False)
|
|
||||||
return p2
|
|
||||||
|
|
||||||
def get_k4mpath(self):
|
|
||||||
k4mpath = tkFileDialog.askopenfilename(
|
|
||||||
parent=None, title='Select Your Kindle Application',
|
|
||||||
defaultextension='.app', filetypes=[('Kindle for Mac Application', '.app')])
|
|
||||||
|
|
||||||
if k4mpath:
|
|
||||||
k4mpath = os.path.normpath(k4mpath)
|
|
||||||
self.k4mpath.delete(0, Tkconstants.END)
|
|
||||||
self.k4mpath.insert(0, k4mpath)
|
|
||||||
return
|
|
||||||
|
|
||||||
def get_outpath(self):
|
|
||||||
cwd = os.getcwdu()
|
|
||||||
cwd = cwd.encode('utf-8')
|
|
||||||
outpath = tkFileDialog.askdirectory(
|
|
||||||
parent=None, title='Directory to Put Non-DRM eBook into',
|
|
||||||
initialdir=cwd, initialfile=None)
|
|
||||||
if outpath:
|
|
||||||
outpath = os.path.normpath(outpath)
|
|
||||||
self.outpath.delete(0, Tkconstants.END)
|
|
||||||
self.outpath.insert(0, outpath)
|
|
||||||
return
|
|
||||||
|
|
||||||
def quitting(self):
|
|
||||||
# kill any still running subprocess
|
|
||||||
if self.p2 != None:
|
|
||||||
if (self.p2.wait('nowait') == None):
|
|
||||||
self.p2.terminate()
|
|
||||||
self.root.destroy()
|
|
||||||
|
|
||||||
# run as a gdb subprocess via pipes and collect stdout
|
|
||||||
def gdbrdr(self, k4mappfile, gdbcmds):
|
|
||||||
cmdline = '/usr/bin/gdb -q -silent -readnow -batch -x ' + gdbcmds + ' "' + k4mappfile + '"'
|
|
||||||
cmdline = cmdline.encode(sys.getfilesystemencoding())
|
|
||||||
p3 = Process(cmdline, shell=True, bufsize=1, stdin=None, stdout=PIPE, stderr=PIPE, close_fds=False)
|
|
||||||
poll = p3.wait('wait')
|
|
||||||
results = p3.read()
|
|
||||||
pidnum = 'NOTAPID+'
|
|
||||||
topazbook = 0
|
|
||||||
bookpath = 'book not found'
|
|
||||||
# parse the gdb results to get the last pid and the last azw/prc file name in the gdb listing
|
|
||||||
reslst = results.split('\n')
|
|
||||||
cnt = len(reslst)
|
|
||||||
for j in xrange(cnt):
|
|
||||||
resline = reslst[j]
|
|
||||||
pp = resline.find('PID is ')
|
|
||||||
if pp == 0:
|
|
||||||
pidnum = resline[7:]
|
|
||||||
topazbook = 0
|
|
||||||
if pp > 0:
|
|
||||||
pidnum = resline[13:]
|
|
||||||
topazbook = 1
|
|
||||||
fp = resline.find('File is ')
|
|
||||||
if fp >= 0:
|
|
||||||
tp1 = resline.find('.azw')
|
|
||||||
tp2 = resline.find('.prc')
|
|
||||||
tp3 = resline.find('.mbp')
|
|
||||||
if tp1 >= 0 or tp2 >= 0:
|
|
||||||
bookpath = resline[8:]
|
|
||||||
if tp3 >= 0 and topazbook == 1:
|
|
||||||
bookpath = resline[8:-3]
|
|
||||||
bookpath += 'azw'
|
|
||||||
# put code here to get pid and file name
|
|
||||||
return pidnum, bookpath, topazbook
|
|
||||||
|
|
||||||
# convert from 8 digit PID to proper 10 digit PID
|
|
||||||
def checksumPid(self, s):
|
|
||||||
letters = "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789"
|
|
||||||
crc = (~binascii.crc32(s,-1))&0xFFFFFFFF
|
|
||||||
crc = crc ^ (crc >> 16)
|
|
||||||
res = s
|
|
||||||
l = len(letters)
|
|
||||||
for i in (0,1):
|
|
||||||
b = crc & 0xff
|
|
||||||
pos = (b // l) ^ (b % l)
|
|
||||||
res += letters[pos%l]
|
|
||||||
crc >>= 8
|
|
||||||
return res
|
|
||||||
|
|
||||||
# start the process
|
|
||||||
def convertit(self):
|
|
||||||
# dictionary of all known Kindle for Mac Binaries
|
|
||||||
sha1_app_digests = {
|
|
||||||
'e197ed2171ceb44a35c24bd30263b7253331694f' : 'gdb_kindle_cmds_r1.txt',
|
|
||||||
'4f702436171f84acc13bdf9f94fae91525aecef5' : 'gdb_kindle_cmds_r2.txt',
|
|
||||||
'4981b7eb37ccf0b8f63f56e8024b5ab593e8a97c' : 'gdb_kindle_cmds_r3.txt',
|
|
||||||
'82909f0545688f09343e2c8fd8521eeee37d2de6' : 'gdb_kindle_cmds_r4.txt',
|
|
||||||
'e260e3515cd525cd085c70baa6e42e08079edbcd' : 'gdb_kindle_cmds_r4.txt',
|
|
||||||
'no_sha1_digest_key_here_________________' : 'no_gdb_kindle_cmds.txt',
|
|
||||||
}
|
|
||||||
# now disable the button to prevent multiple launches
|
|
||||||
self.sbotton.configure(state='disabled')
|
|
||||||
|
|
||||||
k4mpath = self.k4mpath.get()
|
|
||||||
outpath = self.outpath.get()
|
|
||||||
|
|
||||||
# basic error checking
|
|
||||||
if not k4mpath or not os.path.exists(k4mpath):
|
|
||||||
self.status['text'] = 'Error: Specified Kindle for Mac Application does not exist'
|
|
||||||
self.sbotton.configure(state='normal')
|
|
||||||
return
|
|
||||||
if not outpath:
|
|
||||||
self.status['text'] = 'Error: No output directory specified'
|
|
||||||
self.sbotton.configure(state='normal')
|
|
||||||
return
|
|
||||||
if not os.path.isdir(outpath):
|
|
||||||
self.status['text'] = 'Error specified outputdirectory does not exist'
|
|
||||||
self.sbotton.configure(state='normal')
|
|
||||||
return
|
|
||||||
if not os.path.isfile('/usr/bin/gdb'):
|
|
||||||
self.status['text'] = 'Error: gdb does not exist, install the XCode Develoepr Tools'
|
|
||||||
self.sbotton.configure(state='normal')
|
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
# now check if the K4M app binary is known and if so which gdbcmds to use
|
|
||||||
binary_app_file = k4mpath + '/Contents/MacOS/Kindle for Mac'
|
|
||||||
if not os.path.exists(binary_app_file):
|
|
||||||
binary_app_file = k4mpath + '/Contents/MacOS/Kindle'
|
|
||||||
|
|
||||||
k4mpath = binary_app_file
|
|
||||||
|
|
||||||
digest = SHA1(file(binary_app_file, 'rb').read())
|
|
||||||
|
|
||||||
# print digest
|
|
||||||
gdbcmds = None
|
|
||||||
if digest in sha1_app_digests:
|
|
||||||
gdbcmds = sha1_app_digests[digest]
|
|
||||||
else :
|
|
||||||
self.status['text'] = 'Error: Kindle Application does not match any known version, sha1sum is ' + digest
|
|
||||||
self.sbotton.configure(state='normal')
|
|
||||||
return
|
|
||||||
|
|
||||||
# run Kindle for Mac in gdb to get what we need
|
|
||||||
(pidnum, bookpath, topazbook) = self.gdbrdr(k4mpath, gdbcmds)
|
|
||||||
|
|
||||||
if topazbook == 1:
|
|
||||||
log = 'Warning: ' + bookpath + ' is a Topaz book\n'
|
|
||||||
log += '\n\n'
|
|
||||||
log += 'To convert this book please use the Topaz Tools\n'
|
|
||||||
log += 'With the 8 digit PID: "' + pidnum + '"\n'
|
|
||||||
log += '\n\n'
|
|
||||||
log = log.encode('utf-8')
|
|
||||||
self.stext.insert(Tkconstants.END,log)
|
|
||||||
self.sbotton.configure(state='normal')
|
|
||||||
return
|
|
||||||
|
|
||||||
pidnum = self.checksumPid(pidnum)
|
|
||||||
|
|
||||||
# default output file name to be input file name + '_nodrm.mobi'
|
|
||||||
initname = os.path.splitext(os.path.basename(bookpath))[0]
|
|
||||||
initname += '_nodrm.mobi'
|
|
||||||
outpath += '/' + initname
|
|
||||||
|
|
||||||
log = 'Command = "python mobidedrm.py"\n'
|
|
||||||
log += 'Mobi Path = "'+ bookpath + '"\n'
|
|
||||||
log += 'Output file = "' + outpath + '"\n'
|
|
||||||
log += 'PID = "' + pidnum + '"\n'
|
|
||||||
log += '\n\n'
|
|
||||||
log += 'Please Wait ...\n\n'
|
|
||||||
log = log.encode('utf-8')
|
|
||||||
self.stext.insert(Tkconstants.END,log)
|
|
||||||
self.p2 = self.mobirdr(bookpath, outpath, pidnum)
|
|
||||||
|
|
||||||
# python does not seem to allow you to create
|
|
||||||
# your own eventloop which every other gui does - strange
|
|
||||||
# so need to use the widget "after" command to force
|
|
||||||
# event loop to run non-gui events every interval
|
|
||||||
self.stext.after(self.interval,self.processPipe)
|
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
def main(argv=None):
|
|
||||||
root = Tkinter.Tk()
|
|
||||||
root.title('Kindle for Mac eBook Encryption Removal')
|
|
||||||
root.resizable(True, False)
|
|
||||||
root.minsize(300, 0)
|
|
||||||
MainDialog(root).pack(fill=Tkconstants.X, expand=1)
|
|
||||||
root.mainloop()
|
|
||||||
return 0
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
sys.exit(main())
|
|
|
@ -1,61 +0,0 @@
|
||||||
K4Munswindle
|
|
||||||
|
|
||||||
Prerequisites:
|
|
||||||
|
|
||||||
- Kindle for Mac.app Version 1.0.0 Beta 1 (27214)
|
|
||||||
or
|
|
||||||
Kindle.app Version 1.2.0 (30689)
|
|
||||||
or
|
|
||||||
Kindle.app Version 1.2.1 (30781)
|
|
||||||
or
|
|
||||||
Kindle.app Version 1.2.2 (30814)
|
|
||||||
(this is now the current version)
|
|
||||||
|
|
||||||
- A **recent** version of the XCode Developer Tools **must** be Installed
|
|
||||||
(see your latest Mac OS X Install Disk for the installer, and then use Apple System Updates)
|
|
||||||
|
|
||||||
***PLEASE REMEMBER to UNCHECK the "auto updates" in the Kindle.app Preferences!
|
|
||||||
***otherwise it will always update and K4MUnswindle will stop working
|
|
||||||
|
|
||||||
|
|
||||||
The directions for use are:
|
|
||||||
|
|
||||||
1. double-click on K4Munswindle.pyw
|
|
||||||
|
|
||||||
In the window that opens:
|
|
||||||
|
|
||||||
– hit the first '...' button to locate your Kindle Application
|
|
||||||
if it is not in /Applications
|
|
||||||
|
|
||||||
– hit the second '...' button to select an output directory
|
|
||||||
(defaults to your Desktop)
|
|
||||||
|
|
||||||
– hit the 'Start' button
|
|
||||||
|
|
||||||
After a short delay, your Kindle application should open up automagically
|
|
||||||
|
|
||||||
2. In Kindle for Mac:
|
|
||||||
|
|
||||||
- hit the “Home” button to go home.
|
|
||||||
|
|
||||||
- double-click on ONE of your books.
|
|
||||||
This should open the book.
|
|
||||||
|
|
||||||
3. Once the book you want is open
|
|
||||||
|
|
||||||
- hit the “Home” button and then exit the Kindle for Mac application
|
|
||||||
|
|
||||||
4. Once you have exited the Kindle for Mac application you should see one of the following:
|
|
||||||
|
|
||||||
- If the book you selected was a Topaz Book:
|
|
||||||
|
|
||||||
A Warning message will appear in the Conversion Log indicating
|
|
||||||
that the book you opened was Topaz, along with the 8 digit PID
|
|
||||||
needed to convert it using Topaz_Tools
|
|
||||||
|
|
||||||
- If the book you selected was a Mobi book:
|
|
||||||
|
|
||||||
MobiDeDRM will be automagically started in the Conversion Log
|
|
||||||
window and if successful you should find your decoded book in
|
|
||||||
the output directory.
|
|
||||||
|
|
|
@ -1,13 +0,0 @@
|
||||||
set verbose 0
|
|
||||||
break * 0x00b2a56a
|
|
||||||
commands 1
|
|
||||||
printf "PID is %s\n", $edx
|
|
||||||
continue
|
|
||||||
end
|
|
||||||
break * 0x014ca2af
|
|
||||||
commands 2
|
|
||||||
printf "File is %s\n", $eax
|
|
||||||
continue
|
|
||||||
end
|
|
||||||
condition 2 $eax != 0
|
|
||||||
run
|
|
|
@ -1,18 +0,0 @@
|
||||||
set verbose 0
|
|
||||||
break * 0x00e37be4
|
|
||||||
commands 1
|
|
||||||
printf "PID is %s\n", $eax
|
|
||||||
continue
|
|
||||||
end
|
|
||||||
break * 0x0142cd94
|
|
||||||
commands 2
|
|
||||||
printf "File is %s\n", $eax
|
|
||||||
continue
|
|
||||||
end
|
|
||||||
condition 2 $eax != 0
|
|
||||||
break * 0x01009c88
|
|
||||||
commands 3
|
|
||||||
printf "TOPAZ PID is %s\n", *(long*)($esp+12)
|
|
||||||
continue
|
|
||||||
end
|
|
||||||
run
|
|
|
@ -1,18 +0,0 @@
|
||||||
set verbose 0
|
|
||||||
break * 0x00d3dac4
|
|
||||||
commands 1
|
|
||||||
printf "PID is %s\n", $eax
|
|
||||||
continue
|
|
||||||
end
|
|
||||||
break * 0x0130a384
|
|
||||||
commands 2
|
|
||||||
printf "File is %s\n", $eax
|
|
||||||
continue
|
|
||||||
end
|
|
||||||
condition 2 $eax != 0
|
|
||||||
break * 0x00f0f306
|
|
||||||
commands 3
|
|
||||||
printf "TOPAZ PID is %s\n", *(long*)($esp+12)
|
|
||||||
continue
|
|
||||||
end
|
|
||||||
run
|
|
|
@ -1,18 +0,0 @@
|
||||||
set verbose 0
|
|
||||||
break * 0x00d8f72a
|
|
||||||
commands 1
|
|
||||||
printf "PID is %s\n", *(long*)($esp+4)
|
|
||||||
continue
|
|
||||||
end
|
|
||||||
break * 0x0127498c
|
|
||||||
commands 2
|
|
||||||
printf "File is %s\n", $eax
|
|
||||||
continue
|
|
||||||
end
|
|
||||||
condition 2 $eax != 0
|
|
||||||
break * 0x00e9aec0
|
|
||||||
commands 3
|
|
||||||
printf "TOPAZ PID is %s\n", **(long**)($esp+8)
|
|
||||||
continue
|
|
||||||
end
|
|
||||||
run
|
|
|
@ -1,409 +0,0 @@
|
||||||
#!/usr/bin/python
|
|
||||||
#
|
|
||||||
# This is a python script. You need a Python interpreter to run it.
|
|
||||||
# For example, ActiveState Python, which exists for windows.
|
|
||||||
#
|
|
||||||
# Changelog
|
|
||||||
# 0.01 - Initial version
|
|
||||||
# 0.02 - Huffdic compressed books were not properly decrypted
|
|
||||||
# 0.03 - Wasn't checking MOBI header length
|
|
||||||
# 0.04 - Wasn't sanity checking size of data record
|
|
||||||
# 0.05 - It seems that the extra data flags take two bytes not four
|
|
||||||
# 0.06 - And that low bit does mean something after all :-)
|
|
||||||
# 0.07 - The extra data flags aren't present in MOBI header < 0xE8 in size
|
|
||||||
# 0.08 - ...and also not in Mobi header version < 6
|
|
||||||
# 0.09 - ...but they are there with Mobi header version 6, header size 0xE4!
|
|
||||||
# 0.10 - Outputs unencrypted files as-is, so that when run as a Calibre
|
|
||||||
# import filter it works when importing unencrypted files.
|
|
||||||
# Also now handles encrypted files that don't need a specific PID.
|
|
||||||
# 0.11 - use autoflushed stdout and proper return values
|
|
||||||
# 0.12 - Fix for problems with metadata import as Calibre plugin, report errors
|
|
||||||
# 0.13 - Formatting fixes: retabbed file, removed trailing whitespace
|
|
||||||
# and extra blank lines, converted CR/LF pairs at ends of each line,
|
|
||||||
# and other cosmetic fixes.
|
|
||||||
# 0.14 - Working out when the extra data flags are present has been problematic
|
|
||||||
# Versions 7 through 9 have tried to tweak the conditions, but have been
|
|
||||||
# only partially successful. Closer examination of lots of sample
|
|
||||||
# files reveals that a confusion has arisen because trailing data entries
|
|
||||||
# are not encrypted, but it turns out that the multibyte entries
|
|
||||||
# in utf8 file are encrypted. (Although neither kind gets compressed.)
|
|
||||||
# This knowledge leads to a simplification of the test for the
|
|
||||||
# trailing data byte flags - version 5 and higher AND header size >= 0xE4.
|
|
||||||
# 0.15 - Now outputs 'heartbeat', and is also quicker for long files.
|
|
||||||
# 0.16 - And reverts to 'done' not 'done.' at the end for unswindle compatibility.
|
|
||||||
# 0.17 - added modifications to support its use as an imported python module
|
|
||||||
# both inside calibre and also in other places (ie K4DeDRM tools)
|
|
||||||
# 0.17a- disabled the standalone plugin feature since a plugin can not import
|
|
||||||
# a plugin
|
|
||||||
# 0.18 - It seems that multibyte entries aren't encrypted in a v7 file...
|
|
||||||
# Removed the disabled Calibre plug-in code
|
|
||||||
# Permit use of 8-digit PIDs
|
|
||||||
# 0.19 - It seems that multibyte entries aren't encrypted in a v6 file either.
|
|
||||||
# 0.20 - Correction: It seems that multibyte entries are encrypted in a v6 file.
|
|
||||||
# 0.21 - Added support for multiple pids
|
|
||||||
# 0.22 - revised structure to hold MobiBook as a class to allow an extended interface
|
|
||||||
# 0.23 - fixed problem with older files with no EXTH section
|
|
||||||
# 0.24 - add support for type 1 encryption and 'TEXtREAd' books as well
|
|
||||||
# 0.25 - Fixed support for 'BOOKMOBI' type 1 encryption
|
|
||||||
# 0.26 - Now enables Text-To-Speech flag and sets clipping limit to 100%
|
|
||||||
# 0.27 - Correct pid metadata token generation to match that used by skindle (Thank You Bart!)
|
|
||||||
# 0.28 - slight additional changes to metadata token generation (None -> '')
|
|
||||||
# 0.29 - It seems that the ideas about when multibyte trailing characters were
|
|
||||||
# included in the encryption were wrong. They aren't for DOC compressed
|
|
||||||
# files, but they are for HUFF/CDIC compress files!
|
|
||||||
|
|
||||||
__version__ = '0.29'
|
|
||||||
|
|
||||||
import sys
|
|
||||||
|
|
||||||
class Unbuffered:
|
|
||||||
def __init__(self, stream):
|
|
||||||
self.stream = stream
|
|
||||||
def write(self, data):
|
|
||||||
self.stream.write(data)
|
|
||||||
self.stream.flush()
|
|
||||||
def __getattr__(self, attr):
|
|
||||||
return getattr(self.stream, attr)
|
|
||||||
sys.stdout=Unbuffered(sys.stdout)
|
|
||||||
|
|
||||||
import os
|
|
||||||
import struct
|
|
||||||
import binascii
|
|
||||||
|
|
||||||
class DrmException(Exception):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
#
|
|
||||||
# MobiBook Utility Routines
|
|
||||||
#
|
|
||||||
|
|
||||||
# Implementation of Pukall Cipher 1
|
|
||||||
def PC1(key, src, decryption=True):
|
|
||||||
sum1 = 0;
|
|
||||||
sum2 = 0;
|
|
||||||
keyXorVal = 0;
|
|
||||||
if len(key)!=16:
|
|
||||||
print "Bad key length!"
|
|
||||||
return None
|
|
||||||
wkey = []
|
|
||||||
for i in xrange(8):
|
|
||||||
wkey.append(ord(key[i*2])<<8 | ord(key[i*2+1]))
|
|
||||||
dst = ""
|
|
||||||
for i in xrange(len(src)):
|
|
||||||
temp1 = 0;
|
|
||||||
byteXorVal = 0;
|
|
||||||
for j in xrange(8):
|
|
||||||
temp1 ^= wkey[j]
|
|
||||||
sum2 = (sum2+j)*20021 + sum1
|
|
||||||
sum1 = (temp1*346)&0xFFFF
|
|
||||||
sum2 = (sum2+sum1)&0xFFFF
|
|
||||||
temp1 = (temp1*20021+1)&0xFFFF
|
|
||||||
byteXorVal ^= temp1 ^ sum2
|
|
||||||
curByte = ord(src[i])
|
|
||||||
if not decryption:
|
|
||||||
keyXorVal = curByte * 257;
|
|
||||||
curByte = ((curByte ^ (byteXorVal >> 8)) ^ byteXorVal) & 0xFF
|
|
||||||
if decryption:
|
|
||||||
keyXorVal = curByte * 257;
|
|
||||||
for j in xrange(8):
|
|
||||||
wkey[j] ^= keyXorVal;
|
|
||||||
dst+=chr(curByte)
|
|
||||||
return dst
|
|
||||||
|
|
||||||
def checksumPid(s):
|
|
||||||
letters = "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789"
|
|
||||||
crc = (~binascii.crc32(s,-1))&0xFFFFFFFF
|
|
||||||
crc = crc ^ (crc >> 16)
|
|
||||||
res = s
|
|
||||||
l = len(letters)
|
|
||||||
for i in (0,1):
|
|
||||||
b = crc & 0xff
|
|
||||||
pos = (b // l) ^ (b % l)
|
|
||||||
res += letters[pos%l]
|
|
||||||
crc >>= 8
|
|
||||||
return res
|
|
||||||
|
|
||||||
def getSizeOfTrailingDataEntries(ptr, size, flags):
|
|
||||||
def getSizeOfTrailingDataEntry(ptr, size):
|
|
||||||
bitpos, result = 0, 0
|
|
||||||
if size <= 0:
|
|
||||||
return result
|
|
||||||
while True:
|
|
||||||
v = ord(ptr[size-1])
|
|
||||||
result |= (v & 0x7F) << bitpos
|
|
||||||
bitpos += 7
|
|
||||||
size -= 1
|
|
||||||
if (v & 0x80) != 0 or (bitpos >= 28) or (size == 0):
|
|
||||||
return result
|
|
||||||
num = 0
|
|
||||||
testflags = flags >> 1
|
|
||||||
while testflags:
|
|
||||||
if testflags & 1:
|
|
||||||
num += getSizeOfTrailingDataEntry(ptr, size - num)
|
|
||||||
testflags >>= 1
|
|
||||||
# Check the low bit to see if there's multibyte data present.
|
|
||||||
# if multibyte data is included in the encryped data, we'll
|
|
||||||
# have already cleared this flag.
|
|
||||||
if flags & 1:
|
|
||||||
num += (ord(ptr[size - num - 1]) & 0x3) + 1
|
|
||||||
return num
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class MobiBook:
|
|
||||||
def loadSection(self, section):
|
|
||||||
if (section + 1 == self.num_sections):
|
|
||||||
endoff = len(self.data_file)
|
|
||||||
else:
|
|
||||||
endoff = self.sections[section + 1][0]
|
|
||||||
off = self.sections[section][0]
|
|
||||||
return self.data_file[off:endoff]
|
|
||||||
|
|
||||||
def __init__(self, infile):
|
|
||||||
# initial sanity check on file
|
|
||||||
self.data_file = file(infile, 'rb').read()
|
|
||||||
self.header = self.data_file[0:78]
|
|
||||||
if self.header[0x3C:0x3C+8] != 'BOOKMOBI' and self.header[0x3C:0x3C+8] != 'TEXtREAd':
|
|
||||||
raise DrmException("invalid file format")
|
|
||||||
self.magic = self.header[0x3C:0x3C+8]
|
|
||||||
self.crypto_type = -1
|
|
||||||
|
|
||||||
# build up section offset and flag info
|
|
||||||
self.num_sections, = struct.unpack('>H', self.header[76:78])
|
|
||||||
self.sections = []
|
|
||||||
for i in xrange(self.num_sections):
|
|
||||||
offset, a1,a2,a3,a4 = struct.unpack('>LBBBB', self.data_file[78+i*8:78+i*8+8])
|
|
||||||
flags, val = a1, a2<<16|a3<<8|a4
|
|
||||||
self.sections.append( (offset, flags, val) )
|
|
||||||
|
|
||||||
# parse information from section 0
|
|
||||||
self.sect = self.loadSection(0)
|
|
||||||
self.records, = struct.unpack('>H', self.sect[0x8:0x8+2])
|
|
||||||
self.compression, = struct.unpack('>H', self.sect[0x0:0x0+2])
|
|
||||||
|
|
||||||
if self.magic == 'TEXtREAd':
|
|
||||||
print "Book has format: ", self.magic
|
|
||||||
self.extra_data_flags = 0
|
|
||||||
self.mobi_length = 0
|
|
||||||
self.mobi_version = -1
|
|
||||||
self.meta_array = {}
|
|
||||||
return
|
|
||||||
self.mobi_length, = struct.unpack('>L',self.sect[0x14:0x18])
|
|
||||||
self.mobi_version, = struct.unpack('>L',self.sect[0x68:0x6C])
|
|
||||||
print "MOBI header version = %d, length = %d" %(self.mobi_version, self.mobi_length)
|
|
||||||
self.extra_data_flags = 0
|
|
||||||
if (self.mobi_length >= 0xE4) and (self.mobi_version >= 5):
|
|
||||||
self.extra_data_flags, = struct.unpack('>H', self.sect[0xF2:0xF4])
|
|
||||||
print "Extra Data Flags = %d" % self.extra_data_flags
|
|
||||||
if (self.mobi_version < 7) and (self.compression != 17480):
|
|
||||||
# multibyte utf8 data is included in the encryption for mobi_version 6 and below
|
|
||||||
# so clear that byte so that we leave it to be decrypted.
|
|
||||||
self.extra_data_flags &= 0xFFFE
|
|
||||||
|
|
||||||
# if exth region exists parse it for metadata array
|
|
||||||
self.meta_array = {}
|
|
||||||
try:
|
|
||||||
exth_flag, = struct.unpack('>L', self.sect[0x80:0x84])
|
|
||||||
exth = 'NONE'
|
|
||||||
if exth_flag & 0x40:
|
|
||||||
exth = self.sect[16 + self.mobi_length:]
|
|
||||||
if (len(exth) >= 4) and (exth[:4] == 'EXTH'):
|
|
||||||
nitems, = struct.unpack('>I', exth[8:12])
|
|
||||||
pos = 12
|
|
||||||
for i in xrange(nitems):
|
|
||||||
type, size = struct.unpack('>II', exth[pos: pos + 8])
|
|
||||||
content = exth[pos + 8: pos + size]
|
|
||||||
self.meta_array[type] = content
|
|
||||||
# reset the text to speech flag and clipping limit, if present
|
|
||||||
if type == 401 and size == 9:
|
|
||||||
# set clipping limit to 100%
|
|
||||||
self.patchSection(0, "\144", 16 + self.mobi_length + pos + 8)
|
|
||||||
elif type == 404 and size == 9:
|
|
||||||
# make sure text to speech is enabled
|
|
||||||
self.patchSection(0, "\0", 16 + self.mobi_length + pos + 8)
|
|
||||||
# print type, size, content, content.encode('hex')
|
|
||||||
pos += size
|
|
||||||
except:
|
|
||||||
self.meta_array = {}
|
|
||||||
pass
|
|
||||||
|
|
||||||
def getBookTitle(self):
|
|
||||||
title = ''
|
|
||||||
if 503 in self.meta_array:
|
|
||||||
title = self.meta_array[503]
|
|
||||||
else :
|
|
||||||
toff, tlen = struct.unpack('>II', self.sect[0x54:0x5c])
|
|
||||||
tend = toff + tlen
|
|
||||||
title = self.sect[toff:tend]
|
|
||||||
if title == '':
|
|
||||||
title = self.header[:32]
|
|
||||||
title = title.split("\0")[0]
|
|
||||||
return title
|
|
||||||
|
|
||||||
def getPIDMetaInfo(self):
|
|
||||||
rec209 = ''
|
|
||||||
token = ''
|
|
||||||
if 209 in self.meta_array:
|
|
||||||
rec209 = self.meta_array[209]
|
|
||||||
data = rec209
|
|
||||||
# The 209 data comes in five byte groups. Interpret the last four bytes
|
|
||||||
# of each group as a big endian unsigned integer to get a key value
|
|
||||||
# if that key exists in the meta_array, append its contents to the token
|
|
||||||
for i in xrange(0,len(data),5):
|
|
||||||
val, = struct.unpack('>I',data[i+1:i+5])
|
|
||||||
sval = self.meta_array.get(val,'')
|
|
||||||
token += sval
|
|
||||||
return rec209, token
|
|
||||||
|
|
||||||
def patch(self, off, new):
|
|
||||||
self.data_file = self.data_file[:off] + new + self.data_file[off+len(new):]
|
|
||||||
|
|
||||||
def patchSection(self, section, new, in_off = 0):
|
|
||||||
if (section + 1 == self.num_sections):
|
|
||||||
endoff = len(self.data_file)
|
|
||||||
else:
|
|
||||||
endoff = self.sections[section + 1][0]
|
|
||||||
off = self.sections[section][0]
|
|
||||||
assert off + in_off + len(new) <= endoff
|
|
||||||
self.patch(off + in_off, new)
|
|
||||||
|
|
||||||
def parseDRM(self, data, count, pidlist):
|
|
||||||
found_key = None
|
|
||||||
keyvec1 = "\x72\x38\x33\xB0\xB4\xF2\xE3\xCA\xDF\x09\x01\xD6\xE2\xE0\x3F\x96"
|
|
||||||
for pid in pidlist:
|
|
||||||
bigpid = pid.ljust(16,'\0')
|
|
||||||
temp_key = PC1(keyvec1, bigpid, False)
|
|
||||||
temp_key_sum = sum(map(ord,temp_key)) & 0xff
|
|
||||||
found_key = None
|
|
||||||
for i in xrange(count):
|
|
||||||
verification, size, type, cksum, cookie = struct.unpack('>LLLBxxx32s', data[i*0x30:i*0x30+0x30])
|
|
||||||
if cksum == temp_key_sum:
|
|
||||||
cookie = PC1(temp_key, cookie)
|
|
||||||
ver,flags,finalkey,expiry,expiry2 = struct.unpack('>LL16sLL', cookie)
|
|
||||||
if verification == ver and (flags & 0x1F) == 1:
|
|
||||||
found_key = finalkey
|
|
||||||
break
|
|
||||||
if found_key != None:
|
|
||||||
break
|
|
||||||
if not found_key:
|
|
||||||
# Then try the default encoding that doesn't require a PID
|
|
||||||
pid = "00000000"
|
|
||||||
temp_key = keyvec1
|
|
||||||
temp_key_sum = sum(map(ord,temp_key)) & 0xff
|
|
||||||
for i in xrange(count):
|
|
||||||
verification, size, type, cksum, cookie = struct.unpack('>LLLBxxx32s', data[i*0x30:i*0x30+0x30])
|
|
||||||
if cksum == temp_key_sum:
|
|
||||||
cookie = PC1(temp_key, cookie)
|
|
||||||
ver,flags,finalkey,expiry,expiry2 = struct.unpack('>LL16sLL', cookie)
|
|
||||||
if verification == ver:
|
|
||||||
found_key = finalkey
|
|
||||||
break
|
|
||||||
return [found_key,pid]
|
|
||||||
|
|
||||||
def processBook(self, pidlist):
|
|
||||||
crypto_type, = struct.unpack('>H', self.sect[0xC:0xC+2])
|
|
||||||
print 'Crypto Type is: ', crypto_type
|
|
||||||
self.crypto_type = crypto_type
|
|
||||||
if crypto_type == 0:
|
|
||||||
print "This book is not encrypted."
|
|
||||||
return self.data_file
|
|
||||||
if crypto_type != 2 and crypto_type != 1:
|
|
||||||
raise DrmException("Cannot decode unknown Mobipocket encryption type %d" % crypto_type)
|
|
||||||
|
|
||||||
goodpids = []
|
|
||||||
for pid in pidlist:
|
|
||||||
if len(pid)==10:
|
|
||||||
if checksumPid(pid[0:-2]) != pid:
|
|
||||||
print "Warning: PID " + pid + " has incorrect checksum, should have been "+checksumPid(pid[0:-2])
|
|
||||||
goodpids.append(pid[0:-2])
|
|
||||||
elif len(pid)==8:
|
|
||||||
goodpids.append(pid)
|
|
||||||
|
|
||||||
if self.crypto_type == 1:
|
|
||||||
t1_keyvec = "QDCVEPMU675RUBSZ"
|
|
||||||
if self.magic == 'TEXtREAd':
|
|
||||||
bookkey_data = self.sect[0x0E:0x0E+16]
|
|
||||||
elif self.mobi_version < 0:
|
|
||||||
bookkey_data = self.sect[0x90:0x90+16]
|
|
||||||
else:
|
|
||||||
bookkey_data = self.sect[self.mobi_length+16:self.mobi_length+32]
|
|
||||||
pid = "00000000"
|
|
||||||
found_key = PC1(t1_keyvec, bookkey_data)
|
|
||||||
else :
|
|
||||||
# calculate the keys
|
|
||||||
drm_ptr, drm_count, drm_size, drm_flags = struct.unpack('>LLLL', self.sect[0xA8:0xA8+16])
|
|
||||||
if drm_count == 0:
|
|
||||||
raise DrmException("Not yet initialised with PID. Must be opened with Mobipocket Reader first.")
|
|
||||||
found_key, pid = self.parseDRM(self.sect[drm_ptr:drm_ptr+drm_size], drm_count, goodpids)
|
|
||||||
if not found_key:
|
|
||||||
raise DrmException("No key found. Most likely the correct PID has not been given.")
|
|
||||||
# kill the drm keys
|
|
||||||
self.patchSection(0, "\0" * drm_size, drm_ptr)
|
|
||||||
# kill the drm pointers
|
|
||||||
self.patchSection(0, "\xff" * 4 + "\0" * 12, 0xA8)
|
|
||||||
|
|
||||||
if pid=="00000000":
|
|
||||||
print "File has default encryption, no specific PID."
|
|
||||||
else:
|
|
||||||
print "File is encoded with PID "+checksumPid(pid)+"."
|
|
||||||
|
|
||||||
# clear the crypto type
|
|
||||||
self.patchSection(0, "\0" * 2, 0xC)
|
|
||||||
|
|
||||||
# decrypt sections
|
|
||||||
print "Decrypting. Please wait . . .",
|
|
||||||
new_data = self.data_file[:self.sections[1][0]]
|
|
||||||
for i in xrange(1, self.records+1):
|
|
||||||
data = self.loadSection(i)
|
|
||||||
extra_size = getSizeOfTrailingDataEntries(data, len(data), self.extra_data_flags)
|
|
||||||
if i%100 == 0:
|
|
||||||
print ".",
|
|
||||||
# print "record %d, extra_size %d" %(i,extra_size)
|
|
||||||
new_data += PC1(found_key, data[0:len(data) - extra_size])
|
|
||||||
if extra_size > 0:
|
|
||||||
new_data += data[-extra_size:]
|
|
||||||
if self.num_sections > self.records+1:
|
|
||||||
new_data += self.data_file[self.sections[self.records+1][0]:]
|
|
||||||
self.data_file = new_data
|
|
||||||
print "done"
|
|
||||||
return self.data_file
|
|
||||||
|
|
||||||
def getUnencryptedBook(infile,pid):
|
|
||||||
if not os.path.isfile(infile):
|
|
||||||
raise DrmException('Input File Not Found')
|
|
||||||
book = MobiBook(infile)
|
|
||||||
return book.processBook([pid])
|
|
||||||
|
|
||||||
def getUnencryptedBookWithList(infile,pidlist):
|
|
||||||
if not os.path.isfile(infile):
|
|
||||||
raise DrmException('Input File Not Found')
|
|
||||||
book = MobiBook(infile)
|
|
||||||
return book.processBook(pidlist)
|
|
||||||
|
|
||||||
def main(argv=sys.argv):
|
|
||||||
print ('MobiDeDrm v%(__version__)s. '
|
|
||||||
'Copyright 2008-2010 The Dark Reverser.' % globals())
|
|
||||||
if len(argv)<3 or len(argv)>4:
|
|
||||||
print "Removes protection from Mobipocket books"
|
|
||||||
print "Usage:"
|
|
||||||
print " %s <infile> <outfile> [<Comma separated list of PIDs to try>]" % sys.argv[0]
|
|
||||||
return 1
|
|
||||||
else:
|
|
||||||
infile = argv[1]
|
|
||||||
outfile = argv[2]
|
|
||||||
if len(argv) is 4:
|
|
||||||
pidlist = argv[3].split(',')
|
|
||||||
else:
|
|
||||||
pidlist = {}
|
|
||||||
try:
|
|
||||||
stripped_file = getUnencryptedBookWithList(infile, pidlist)
|
|
||||||
file(outfile, 'wb').write(stripped_file)
|
|
||||||
except DrmException, e:
|
|
||||||
print "Error: %s" % e
|
|
||||||
return 1
|
|
||||||
return 0
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
sys.exit(main())
|
|
|
@ -1,27 +0,0 @@
|
||||||
#!/usr/bin/env python
|
|
||||||
# vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
|
|
||||||
|
|
||||||
import Tkinter
|
|
||||||
import Tkconstants
|
|
||||||
|
|
||||||
# basic scrolled text widget
|
|
||||||
class ScrolledText(Tkinter.Text):
|
|
||||||
def __init__(self, master=None, **kw):
|
|
||||||
self.frame = Tkinter.Frame(master)
|
|
||||||
self.vbar = Tkinter.Scrollbar(self.frame)
|
|
||||||
self.vbar.pack(side=Tkconstants.RIGHT, fill=Tkconstants.Y)
|
|
||||||
kw.update({'yscrollcommand': self.vbar.set})
|
|
||||||
Tkinter.Text.__init__(self, self.frame, **kw)
|
|
||||||
self.pack(side=Tkconstants.LEFT, fill=Tkconstants.BOTH, expand=True)
|
|
||||||
self.vbar['command'] = self.yview
|
|
||||||
# Copy geometry methods of self.frame without overriding Text
|
|
||||||
# methods = hack!
|
|
||||||
text_meths = vars(Tkinter.Text).keys()
|
|
||||||
methods = vars(Tkinter.Pack).keys() + vars(Tkinter.Grid).keys() + vars(Tkinter.Place).keys()
|
|
||||||
methods = set(methods).difference(text_meths)
|
|
||||||
for m in methods:
|
|
||||||
if m[0] != '_' and m != 'config' and m != 'configure':
|
|
||||||
setattr(self, m, getattr(self.frame, m))
|
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
return str(self.frame)
|
|
|
@ -1,149 +0,0 @@
|
||||||
#!/usr/bin/env python
|
|
||||||
# vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
|
|
||||||
|
|
||||||
import os, sys
|
|
||||||
import signal
|
|
||||||
import threading
|
|
||||||
import subprocess
|
|
||||||
from subprocess import Popen, PIPE, STDOUT
|
|
||||||
|
|
||||||
# **heavily** chopped up and modfied version of asyncproc.py
|
|
||||||
# to make it actually work on Windows as well as Mac/Linux
|
|
||||||
# For the original see:
|
|
||||||
# "http://www.lysator.liu.se/~bellman/download/"
|
|
||||||
# author is "Thomas Bellman <bellman@lysator.liu.se>"
|
|
||||||
# available under GPL version 3 or Later
|
|
||||||
|
|
||||||
# create an asynchronous subprocess whose output can be collected in
|
|
||||||
# a non-blocking manner
|
|
||||||
|
|
||||||
# What a mess! Have to use threads just to get non-blocking io
|
|
||||||
# in a cross-platform manner
|
|
||||||
|
|
||||||
# luckily all thread use is hidden within this class
|
|
||||||
|
|
||||||
class Process(object):
|
|
||||||
def __init__(self, *params, **kwparams):
|
|
||||||
if len(params) <= 3:
|
|
||||||
kwparams.setdefault('stdin', subprocess.PIPE)
|
|
||||||
if len(params) <= 4:
|
|
||||||
kwparams.setdefault('stdout', subprocess.PIPE)
|
|
||||||
if len(params) <= 5:
|
|
||||||
kwparams.setdefault('stderr', subprocess.PIPE)
|
|
||||||
self.__pending_input = []
|
|
||||||
self.__collected_outdata = []
|
|
||||||
self.__collected_errdata = []
|
|
||||||
self.__exitstatus = None
|
|
||||||
self.__lock = threading.Lock()
|
|
||||||
self.__inputsem = threading.Semaphore(0)
|
|
||||||
self.__quit = False
|
|
||||||
|
|
||||||
self.__process = subprocess.Popen(*params, **kwparams)
|
|
||||||
|
|
||||||
if self.__process.stdin:
|
|
||||||
self.__stdin_thread = threading.Thread(
|
|
||||||
name="stdin-thread",
|
|
||||||
target=self.__feeder, args=(self.__pending_input,
|
|
||||||
self.__process.stdin))
|
|
||||||
self.__stdin_thread.setDaemon(True)
|
|
||||||
self.__stdin_thread.start()
|
|
||||||
|
|
||||||
if self.__process.stdout:
|
|
||||||
self.__stdout_thread = threading.Thread(
|
|
||||||
name="stdout-thread",
|
|
||||||
target=self.__reader, args=(self.__collected_outdata,
|
|
||||||
self.__process.stdout))
|
|
||||||
self.__stdout_thread.setDaemon(True)
|
|
||||||
self.__stdout_thread.start()
|
|
||||||
|
|
||||||
if self.__process.stderr:
|
|
||||||
self.__stderr_thread = threading.Thread(
|
|
||||||
name="stderr-thread",
|
|
||||||
target=self.__reader, args=(self.__collected_errdata,
|
|
||||||
self.__process.stderr))
|
|
||||||
self.__stderr_thread.setDaemon(True)
|
|
||||||
self.__stderr_thread.start()
|
|
||||||
|
|
||||||
def pid(self):
|
|
||||||
return self.__process.pid
|
|
||||||
|
|
||||||
def kill(self, signal):
|
|
||||||
self.__process.send_signal(signal)
|
|
||||||
|
|
||||||
# check on subprocess (pass in 'nowait') to act like poll
|
|
||||||
def wait(self, flag):
|
|
||||||
if flag.lower() == 'nowait':
|
|
||||||
rc = self.__process.poll()
|
|
||||||
else:
|
|
||||||
rc = self.__process.wait()
|
|
||||||
if rc != None:
|
|
||||||
if self.__process.stdin:
|
|
||||||
self.closeinput()
|
|
||||||
if self.__process.stdout:
|
|
||||||
self.__stdout_thread.join()
|
|
||||||
if self.__process.stderr:
|
|
||||||
self.__stderr_thread.join()
|
|
||||||
return self.__process.returncode
|
|
||||||
|
|
||||||
def terminate(self):
|
|
||||||
if self.__process.stdin:
|
|
||||||
self.closeinput()
|
|
||||||
self.__process.terminate()
|
|
||||||
|
|
||||||
# thread gets data from subprocess stdout
|
|
||||||
def __reader(self, collector, source):
|
|
||||||
while True:
|
|
||||||
data = os.read(source.fileno(), 65536)
|
|
||||||
self.__lock.acquire()
|
|
||||||
collector.append(data)
|
|
||||||
self.__lock.release()
|
|
||||||
if data == "":
|
|
||||||
source.close()
|
|
||||||
break
|
|
||||||
return
|
|
||||||
|
|
||||||
# thread feeds data to subprocess stdin
|
|
||||||
def __feeder(self, pending, drain):
|
|
||||||
while True:
|
|
||||||
self.__inputsem.acquire()
|
|
||||||
self.__lock.acquire()
|
|
||||||
if not pending and self.__quit:
|
|
||||||
drain.close()
|
|
||||||
self.__lock.release()
|
|
||||||
break
|
|
||||||
data = pending.pop(0)
|
|
||||||
self.__lock.release()
|
|
||||||
drain.write(data)
|
|
||||||
|
|
||||||
# non-blocking read of data from subprocess stdout
|
|
||||||
def read(self):
|
|
||||||
self.__lock.acquire()
|
|
||||||
outdata = "".join(self.__collected_outdata)
|
|
||||||
del self.__collected_outdata[:]
|
|
||||||
self.__lock.release()
|
|
||||||
return outdata
|
|
||||||
|
|
||||||
# non-blocking read of data from subprocess stderr
|
|
||||||
def readerr(self):
|
|
||||||
self.__lock.acquire()
|
|
||||||
errdata = "".join(self.__collected_errdata)
|
|
||||||
del self.__collected_errdata[:]
|
|
||||||
self.__lock.release()
|
|
||||||
return errdata
|
|
||||||
|
|
||||||
# non-blocking write to stdin of subprocess
|
|
||||||
def write(self, data):
|
|
||||||
if self.__process.stdin is None:
|
|
||||||
raise ValueError("Writing to process with stdin not a pipe")
|
|
||||||
self.__lock.acquire()
|
|
||||||
self.__pending_input.append(data)
|
|
||||||
self.__inputsem.release()
|
|
||||||
self.__lock.release()
|
|
||||||
|
|
||||||
# close stdinput of subprocess
|
|
||||||
def closeinput(self):
|
|
||||||
self.__lock.acquire()
|
|
||||||
self.__quit = True
|
|
||||||
self.__inputsem.release()
|
|
||||||
self.__lock.release()
|
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
README
|
|
||||||
|
|
||||||
unswindle can be used to find the book specific PID but it needs to be updated for each version of Kindle4PC that Amazon releases (and therefore is also useful for Linux users who have Wine). This program “patches” the Kindle4PC executable and therefore is very release specific.
|
|
||||||
|
|
||||||
Unfortunately unswindle v7 the latest, has not been updated to work with the latest version of Kindle for PC. You will need to find one of the older versions of Kindle4PC and prevent later updates in order to use this tool.
|
|
|
@ -1,883 +0,0 @@
|
||||||
#! /usr/bin/python
|
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
|
|
||||||
# unswindle.pyw, version 7
|
|
||||||
# Copyright © 2009-2010 i♥cabbages
|
|
||||||
|
|
||||||
# Released under the terms of the GNU General Public Licence, version 3 or
|
|
||||||
# later. <http://www.gnu.org/licenses/>
|
|
||||||
|
|
||||||
# Before running this program, you must first install Python 2.6 from
|
|
||||||
# <http://www.python.org/download/>. Save this script file as unswindle.pyw.
|
|
||||||
# Find and save in the same directory a copy of mobidedrm.py. Double-click on
|
|
||||||
# unswindle.pyw. It will run Kindle For PC. Open the book you want to
|
|
||||||
# decrypt. Close Kindle For PC. A dialog will open allowing you to select the
|
|
||||||
# output file. And you're done!
|
|
||||||
|
|
||||||
# Revision history:
|
|
||||||
# 1 - Initial release
|
|
||||||
# 2 - Fixes to work properly on Windows versions >XP
|
|
||||||
# 3 - Fix minor bug in path extraction
|
|
||||||
# 4 - Fix error opening threads; detect Topaz books;
|
|
||||||
# detect unsupported versions of K4PC
|
|
||||||
# 5 - Work with new (20091222) version of K4PC
|
|
||||||
# 6 - Detect and just copy DRM-free books
|
|
||||||
# 7 - Work with new (20100629) version of K4PC
|
|
||||||
|
|
||||||
"""
|
|
||||||
Decrypt Kindle For PC encrypted Mobipocket books.
|
|
||||||
"""
|
|
||||||
|
|
||||||
from __future__ import with_statement
|
|
||||||
|
|
||||||
__license__ = 'GPL v3'
|
|
||||||
|
|
||||||
import sys
|
|
||||||
import os
|
|
||||||
import re
|
|
||||||
import tempfile
|
|
||||||
import shutil
|
|
||||||
import subprocess
|
|
||||||
import struct
|
|
||||||
import hashlib
|
|
||||||
import ctypes
|
|
||||||
from ctypes import *
|
|
||||||
from ctypes.wintypes import *
|
|
||||||
import binascii
|
|
||||||
import _winreg as winreg
|
|
||||||
import Tkinter
|
|
||||||
import Tkconstants
|
|
||||||
import tkMessageBox
|
|
||||||
import tkFileDialog
|
|
||||||
import traceback
|
|
||||||
|
|
||||||
#
|
|
||||||
# _extrawintypes.py
|
|
||||||
|
|
||||||
UBYTE = c_ubyte
|
|
||||||
ULONG_PTR = POINTER(ULONG)
|
|
||||||
PULONG = ULONG_PTR
|
|
||||||
PVOID = LPVOID
|
|
||||||
LPCTSTR = LPTSTR = c_wchar_p
|
|
||||||
LPBYTE = c_char_p
|
|
||||||
SIZE_T = c_uint
|
|
||||||
SIZE_T_p = POINTER(SIZE_T)
|
|
||||||
|
|
||||||
#
|
|
||||||
# _ntdll.py
|
|
||||||
|
|
||||||
NTSTATUS = DWORD
|
|
||||||
|
|
||||||
ntdll = windll.ntdll
|
|
||||||
|
|
||||||
class PROCESS_BASIC_INFORMATION(Structure):
|
|
||||||
_fields_ = [('Reserved1', PVOID),
|
|
||||||
('PebBaseAddress', PVOID),
|
|
||||||
('Reserved2', PVOID * 2),
|
|
||||||
('UniqueProcessId', ULONG_PTR),
|
|
||||||
('Reserved3', PVOID)]
|
|
||||||
|
|
||||||
# NTSTATUS WINAPI NtQueryInformationProcess(
|
|
||||||
# __in HANDLE ProcessHandle,
|
|
||||||
# __in PROCESSINFOCLASS ProcessInformationClass,
|
|
||||||
# __out PVOID ProcessInformation,
|
|
||||||
# __in ULONG ProcessInformationLength,
|
|
||||||
# __out_opt PULONG ReturnLength
|
|
||||||
# );
|
|
||||||
NtQueryInformationProcess = ntdll.NtQueryInformationProcess
|
|
||||||
NtQueryInformationProcess.argtypes = [HANDLE, DWORD, PVOID, ULONG, PULONG]
|
|
||||||
NtQueryInformationProcess.restype = NTSTATUS
|
|
||||||
|
|
||||||
#
|
|
||||||
# _kernel32.py
|
|
||||||
|
|
||||||
INFINITE = 0xffffffff
|
|
||||||
|
|
||||||
CREATE_UNICODE_ENVIRONMENT = 0x00000400
|
|
||||||
DEBUG_ONLY_THIS_PROCESS = 0x00000002
|
|
||||||
DEBUG_PROCESS = 0x00000001
|
|
||||||
|
|
||||||
THREAD_GET_CONTEXT = 0x0008
|
|
||||||
THREAD_QUERY_INFORMATION = 0x0040
|
|
||||||
THREAD_SET_CONTEXT = 0x0010
|
|
||||||
THREAD_SET_INFORMATION = 0x0020
|
|
||||||
|
|
||||||
EXCEPTION_BREAKPOINT = 0x80000003
|
|
||||||
EXCEPTION_SINGLE_STEP = 0x80000004
|
|
||||||
EXCEPTION_ACCESS_VIOLATION = 0xC0000005
|
|
||||||
|
|
||||||
DBG_CONTINUE = 0x00010002L
|
|
||||||
DBG_EXCEPTION_NOT_HANDLED = 0x80010001L
|
|
||||||
|
|
||||||
EXCEPTION_DEBUG_EVENT = 1
|
|
||||||
CREATE_THREAD_DEBUG_EVENT = 2
|
|
||||||
CREATE_PROCESS_DEBUG_EVENT = 3
|
|
||||||
EXIT_THREAD_DEBUG_EVENT = 4
|
|
||||||
EXIT_PROCESS_DEBUG_EVENT = 5
|
|
||||||
LOAD_DLL_DEBUG_EVENT = 6
|
|
||||||
UNLOAD_DLL_DEBUG_EVENT = 7
|
|
||||||
OUTPUT_DEBUG_STRING_EVENT = 8
|
|
||||||
RIP_EVENT = 9
|
|
||||||
|
|
||||||
class DataBlob(Structure):
|
|
||||||
_fields_ = [('cbData', c_uint),
|
|
||||||
('pbData', c_void_p)]
|
|
||||||
DataBlob_p = POINTER(DataBlob)
|
|
||||||
|
|
||||||
class SECURITY_ATTRIBUTES(Structure):
|
|
||||||
_fields_ = [('nLength', DWORD),
|
|
||||||
('lpSecurityDescriptor', LPVOID),
|
|
||||||
('bInheritHandle', BOOL)]
|
|
||||||
LPSECURITY_ATTRIBUTES = POINTER(SECURITY_ATTRIBUTES)
|
|
||||||
|
|
||||||
class STARTUPINFO(Structure):
|
|
||||||
_fields_ = [('cb', DWORD),
|
|
||||||
('lpReserved', LPTSTR),
|
|
||||||
('lpDesktop', LPTSTR),
|
|
||||||
('lpTitle', LPTSTR),
|
|
||||||
('dwX', DWORD),
|
|
||||||
('dwY', DWORD),
|
|
||||||
('dwXSize', DWORD),
|
|
||||||
('dwYSize', DWORD),
|
|
||||||
('dwXCountChars', DWORD),
|
|
||||||
('dwYCountChars', DWORD),
|
|
||||||
('dwFillAttribute', DWORD),
|
|
||||||
('dwFlags', DWORD),
|
|
||||||
('wShowWindow', WORD),
|
|
||||||
('cbReserved2', WORD),
|
|
||||||
('lpReserved2', LPBYTE),
|
|
||||||
('hStdInput', HANDLE),
|
|
||||||
('hStdOutput', HANDLE),
|
|
||||||
('hStdError', HANDLE)]
|
|
||||||
LPSTARTUPINFO = POINTER(STARTUPINFO)
|
|
||||||
|
|
||||||
class PROCESS_INFORMATION(Structure):
|
|
||||||
_fields_ = [('hProcess', HANDLE),
|
|
||||||
('hThread', HANDLE),
|
|
||||||
('dwProcessId', DWORD),
|
|
||||||
('dwThreadId', DWORD)]
|
|
||||||
LPPROCESS_INFORMATION = POINTER(PROCESS_INFORMATION)
|
|
||||||
|
|
||||||
EXCEPTION_MAXIMUM_PARAMETERS = 15
|
|
||||||
class EXCEPTION_RECORD(Structure):
|
|
||||||
pass
|
|
||||||
EXCEPTION_RECORD._fields_ = [
|
|
||||||
('ExceptionCode', DWORD),
|
|
||||||
('ExceptionFlags', DWORD),
|
|
||||||
('ExceptionRecord', POINTER(EXCEPTION_RECORD)),
|
|
||||||
('ExceptionAddress', LPVOID),
|
|
||||||
('NumberParameters', DWORD),
|
|
||||||
('ExceptionInformation', ULONG_PTR * EXCEPTION_MAXIMUM_PARAMETERS)]
|
|
||||||
|
|
||||||
class EXCEPTION_DEBUG_INFO(Structure):
|
|
||||||
_fields_ = [('ExceptionRecord', EXCEPTION_RECORD),
|
|
||||||
('dwFirstChance', DWORD)]
|
|
||||||
|
|
||||||
class CREATE_THREAD_DEBUG_INFO(Structure):
|
|
||||||
_fields_ = [('hThread', HANDLE),
|
|
||||||
('lpThreadLocalBase', LPVOID),
|
|
||||||
('lpStartAddress', LPVOID)]
|
|
||||||
|
|
||||||
class CREATE_PROCESS_DEBUG_INFO(Structure):
|
|
||||||
_fields_ = [('hFile', HANDLE),
|
|
||||||
('hProcess', HANDLE),
|
|
||||||
('hThread', HANDLE),
|
|
||||||
('dwDebugInfoFileOffset', DWORD),
|
|
||||||
('nDebugInfoSize', DWORD),
|
|
||||||
('lpThreadLocalBase', LPVOID),
|
|
||||||
('lpStartAddress', LPVOID),
|
|
||||||
('lpImageName', LPVOID),
|
|
||||||
('fUnicode', WORD)]
|
|
||||||
|
|
||||||
class EXIT_THREAD_DEBUG_INFO(Structure):
|
|
||||||
_fields_ = [('dwExitCode', DWORD)]
|
|
||||||
|
|
||||||
class EXIT_PROCESS_DEBUG_INFO(Structure):
|
|
||||||
_fields_ = [('dwExitCode', DWORD)]
|
|
||||||
|
|
||||||
class LOAD_DLL_DEBUG_INFO(Structure):
|
|
||||||
_fields_ = [('hFile', HANDLE),
|
|
||||||
('lpBaseOfDll', LPVOID),
|
|
||||||
('dwDebugInfoFileOffset', DWORD),
|
|
||||||
('nDebugInfoSize', DWORD),
|
|
||||||
('lpImageName', LPVOID),
|
|
||||||
('fUnicode', WORD)]
|
|
||||||
|
|
||||||
class UNLOAD_DLL_DEBUG_INFO(Structure):
|
|
||||||
_fields_ = [('lpBaseOfDll', LPVOID)]
|
|
||||||
|
|
||||||
class OUTPUT_DEBUG_STRING_INFO(Structure):
|
|
||||||
_fields_ = [('lpDebugStringData', LPSTR),
|
|
||||||
('fUnicode', WORD),
|
|
||||||
('nDebugStringLength', WORD)]
|
|
||||||
|
|
||||||
class RIP_INFO(Structure):
|
|
||||||
_fields_ = [('dwError', DWORD),
|
|
||||||
('dwType', DWORD)]
|
|
||||||
|
|
||||||
class _U(Union):
|
|
||||||
_fields_ = [('Exception', EXCEPTION_DEBUG_INFO),
|
|
||||||
('CreateThread', CREATE_THREAD_DEBUG_INFO),
|
|
||||||
('CreateProcessInfo', CREATE_PROCESS_DEBUG_INFO),
|
|
||||||
('ExitThread', EXIT_THREAD_DEBUG_INFO),
|
|
||||||
('ExitProcess', EXIT_PROCESS_DEBUG_INFO),
|
|
||||||
('LoadDll', LOAD_DLL_DEBUG_INFO),
|
|
||||||
('UnloadDll', UNLOAD_DLL_DEBUG_INFO),
|
|
||||||
('DebugString', OUTPUT_DEBUG_STRING_INFO),
|
|
||||||
('RipInfo', RIP_INFO)]
|
|
||||||
|
|
||||||
class DEBUG_EVENT(Structure):
|
|
||||||
_anonymous_ = ('u',)
|
|
||||||
_fields_ = [('dwDebugEventCode', DWORD),
|
|
||||||
('dwProcessId', DWORD),
|
|
||||||
('dwThreadId', DWORD),
|
|
||||||
('u', _U)]
|
|
||||||
LPDEBUG_EVENT = POINTER(DEBUG_EVENT)
|
|
||||||
|
|
||||||
CONTEXT_X86 = 0x00010000
|
|
||||||
CONTEXT_i386 = CONTEXT_X86
|
|
||||||
CONTEXT_i486 = CONTEXT_X86
|
|
||||||
|
|
||||||
CONTEXT_CONTROL = (CONTEXT_i386 | 0x0001) # SS:SP, CS:IP, FLAGS, BP
|
|
||||||
CONTEXT_INTEGER = (CONTEXT_i386 | 0x0002) # AX, BX, CX, DX, SI, DI
|
|
||||||
CONTEXT_SEGMENTS = (CONTEXT_i386 | 0x0004) # DS, ES, FS, GS
|
|
||||||
CONTEXT_FLOATING_POINT = (CONTEXT_i386 | 0x0008L) # 387 state
|
|
||||||
CONTEXT_DEBUG_REGISTERS = (CONTEXT_i386 | 0x0010L) # DB 0-3,6,7
|
|
||||||
CONTEXT_EXTENDED_REGISTERS = (CONTEXT_i386 | 0x0020L)
|
|
||||||
CONTEXT_FULL = (CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_SEGMENTS)
|
|
||||||
CONTEXT_ALL = (CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_SEGMENTS |
|
|
||||||
CONTEXT_FLOATING_POINT | CONTEXT_DEBUG_REGISTERS |
|
|
||||||
CONTEXT_EXTENDED_REGISTERS)
|
|
||||||
|
|
||||||
SIZE_OF_80387_REGISTERS = 80
|
|
||||||
class FLOATING_SAVE_AREA(Structure):
|
|
||||||
_fields_ = [('ControlWord', DWORD),
|
|
||||||
('StatusWord', DWORD),
|
|
||||||
('TagWord', DWORD),
|
|
||||||
('ErrorOffset', DWORD),
|
|
||||||
('ErrorSelector', DWORD),
|
|
||||||
('DataOffset', DWORD),
|
|
||||||
('DataSelector', DWORD),
|
|
||||||
('RegisterArea', BYTE * SIZE_OF_80387_REGISTERS),
|
|
||||||
('Cr0NpxState', DWORD)]
|
|
||||||
|
|
||||||
MAXIMUM_SUPPORTED_EXTENSION = 512
|
|
||||||
class CONTEXT(Structure):
|
|
||||||
_fields_ = [('ContextFlags', DWORD),
|
|
||||||
('Dr0', DWORD),
|
|
||||||
('Dr1', DWORD),
|
|
||||||
('Dr2', DWORD),
|
|
||||||
('Dr3', DWORD),
|
|
||||||
('Dr6', DWORD),
|
|
||||||
('Dr7', DWORD),
|
|
||||||
('FloatSave', FLOATING_SAVE_AREA),
|
|
||||||
('SegGs', DWORD),
|
|
||||||
('SegFs', DWORD),
|
|
||||||
('SegEs', DWORD),
|
|
||||||
('SegDs', DWORD),
|
|
||||||
('Edi', DWORD),
|
|
||||||
('Esi', DWORD),
|
|
||||||
('Ebx', DWORD),
|
|
||||||
('Edx', DWORD),
|
|
||||||
('Ecx', DWORD),
|
|
||||||
('Eax', DWORD),
|
|
||||||
('Ebp', DWORD),
|
|
||||||
('Eip', DWORD),
|
|
||||||
('SegCs', DWORD),
|
|
||||||
('EFlags', DWORD),
|
|
||||||
('Esp', DWORD),
|
|
||||||
('SegSs', DWORD),
|
|
||||||
('ExtendedRegisters', BYTE * MAXIMUM_SUPPORTED_EXTENSION)]
|
|
||||||
LPCONTEXT = POINTER(CONTEXT)
|
|
||||||
|
|
||||||
class LDT_ENTRY(Structure):
|
|
||||||
_fields_ = [('LimitLow', WORD),
|
|
||||||
('BaseLow', WORD),
|
|
||||||
('BaseMid', UBYTE),
|
|
||||||
('Flags1', UBYTE),
|
|
||||||
('Flags2', UBYTE),
|
|
||||||
('BaseHi', UBYTE)]
|
|
||||||
LPLDT_ENTRY = POINTER(LDT_ENTRY)
|
|
||||||
|
|
||||||
kernel32 = windll.kernel32
|
|
||||||
|
|
||||||
# BOOL WINAPI CloseHandle(
|
|
||||||
# __in HANDLE hObject
|
|
||||||
# );
|
|
||||||
CloseHandle = kernel32.CloseHandle
|
|
||||||
CloseHandle.argtypes = [HANDLE]
|
|
||||||
CloseHandle.restype = BOOL
|
|
||||||
|
|
||||||
# BOOL WINAPI CreateProcess(
|
|
||||||
# __in_opt LPCTSTR lpApplicationName,
|
|
||||||
# __inout_opt LPTSTR lpCommandLine,
|
|
||||||
# __in_opt LPSECURITY_ATTRIBUTES lpProcessAttributes,
|
|
||||||
# __in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes,
|
|
||||||
# __in BOOL bInheritHandles,
|
|
||||||
# __in DWORD dwCreationFlags,
|
|
||||||
# __in_opt LPVOID lpEnvironment,
|
|
||||||
# __in_opt LPCTSTR lpCurrentDirectory,
|
|
||||||
# __in LPSTARTUPINFO lpStartupInfo,
|
|
||||||
# __out LPPROCESS_INFORMATION lpProcessInformation
|
|
||||||
# );
|
|
||||||
CreateProcess = kernel32.CreateProcessW
|
|
||||||
CreateProcess.argtypes = [LPCTSTR, LPTSTR, LPSECURITY_ATTRIBUTES,
|
|
||||||
LPSECURITY_ATTRIBUTES, BOOL, DWORD, LPVOID, LPCTSTR,
|
|
||||||
LPSTARTUPINFO, LPPROCESS_INFORMATION]
|
|
||||||
CreateProcess.restype = BOOL
|
|
||||||
|
|
||||||
# HANDLE WINAPI OpenThread(
|
|
||||||
# __in DWORD dwDesiredAccess,
|
|
||||||
# __in BOOL bInheritHandle,
|
|
||||||
# __in DWORD dwThreadId
|
|
||||||
# );
|
|
||||||
OpenThread = kernel32.OpenThread
|
|
||||||
OpenThread.argtypes = [DWORD, BOOL, DWORD]
|
|
||||||
OpenThread.restype = HANDLE
|
|
||||||
|
|
||||||
# BOOL WINAPI ContinueDebugEvent(
|
|
||||||
# __in DWORD dwProcessId,
|
|
||||||
# __in DWORD dwThreadId,
|
|
||||||
# __in DWORD dwContinueStatus
|
|
||||||
# );
|
|
||||||
ContinueDebugEvent = kernel32.ContinueDebugEvent
|
|
||||||
ContinueDebugEvent.argtypes = [DWORD, DWORD, DWORD]
|
|
||||||
ContinueDebugEvent.restype = BOOL
|
|
||||||
|
|
||||||
# BOOL WINAPI DebugActiveProcess(
|
|
||||||
# __in DWORD dwProcessId
|
|
||||||
# );
|
|
||||||
DebugActiveProcess = kernel32.DebugActiveProcess
|
|
||||||
DebugActiveProcess.argtypes = [DWORD]
|
|
||||||
DebugActiveProcess.restype = BOOL
|
|
||||||
|
|
||||||
# BOOL WINAPI GetThreadContext(
|
|
||||||
# __in HANDLE hThread,
|
|
||||||
# __inout LPCONTEXT lpContext
|
|
||||||
# );
|
|
||||||
GetThreadContext = kernel32.GetThreadContext
|
|
||||||
GetThreadContext.argtypes = [HANDLE, LPCONTEXT]
|
|
||||||
GetThreadContext.restype = BOOL
|
|
||||||
|
|
||||||
# BOOL WINAPI GetThreadSelectorEntry(
|
|
||||||
# __in HANDLE hThread,
|
|
||||||
# __in DWORD dwSelector,
|
|
||||||
# __out LPLDT_ENTRY lpSelectorEntry
|
|
||||||
# );
|
|
||||||
GetThreadSelectorEntry = kernel32.GetThreadSelectorEntry
|
|
||||||
GetThreadSelectorEntry.argtypes = [HANDLE, DWORD, LPLDT_ENTRY]
|
|
||||||
GetThreadSelectorEntry.restype = BOOL
|
|
||||||
|
|
||||||
# BOOL WINAPI ReadProcessMemory(
|
|
||||||
# __in HANDLE hProcess,
|
|
||||||
# __in LPCVOID lpBaseAddress,
|
|
||||||
# __out LPVOID lpBuffer,
|
|
||||||
# __in SIZE_T nSize,
|
|
||||||
# __out SIZE_T *lpNumberOfBytesRead
|
|
||||||
# );
|
|
||||||
ReadProcessMemory = kernel32.ReadProcessMemory
|
|
||||||
ReadProcessMemory.argtypes = [HANDLE, LPCVOID, LPVOID, SIZE_T, SIZE_T_p]
|
|
||||||
ReadProcessMemory.restype = BOOL
|
|
||||||
|
|
||||||
# BOOL WINAPI SetThreadContext(
|
|
||||||
# __in HANDLE hThread,
|
|
||||||
# __in const CONTEXT *lpContext
|
|
||||||
# );
|
|
||||||
SetThreadContext = kernel32.SetThreadContext
|
|
||||||
SetThreadContext.argtypes = [HANDLE, LPCONTEXT]
|
|
||||||
SetThreadContext.restype = BOOL
|
|
||||||
|
|
||||||
# BOOL WINAPI WaitForDebugEvent(
|
|
||||||
# __out LPDEBUG_EVENT lpDebugEvent,
|
|
||||||
# __in DWORD dwMilliseconds
|
|
||||||
# );
|
|
||||||
WaitForDebugEvent = kernel32.WaitForDebugEvent
|
|
||||||
WaitForDebugEvent.argtypes = [LPDEBUG_EVENT, DWORD]
|
|
||||||
WaitForDebugEvent.restype = BOOL
|
|
||||||
|
|
||||||
# BOOL WINAPI WriteProcessMemory(
|
|
||||||
# __in HANDLE hProcess,
|
|
||||||
# __in LPVOID lpBaseAddress,
|
|
||||||
# __in LPCVOID lpBuffer,
|
|
||||||
# __in SIZE_T nSize,
|
|
||||||
# __out SIZE_T *lpNumberOfBytesWritten
|
|
||||||
# );
|
|
||||||
WriteProcessMemory = kernel32.WriteProcessMemory
|
|
||||||
WriteProcessMemory.argtypes = [HANDLE, LPVOID, LPCVOID, SIZE_T, SIZE_T_p]
|
|
||||||
WriteProcessMemory.restype = BOOL
|
|
||||||
|
|
||||||
# BOOL WINAPI FlushInstructionCache(
|
|
||||||
# __in HANDLE hProcess,
|
|
||||||
# __in LPCVOID lpBaseAddress,
|
|
||||||
# __in SIZE_T dwSize
|
|
||||||
# );
|
|
||||||
FlushInstructionCache = kernel32.FlushInstructionCache
|
|
||||||
FlushInstructionCache.argtypes = [HANDLE, LPCVOID, SIZE_T]
|
|
||||||
FlushInstructionCache.restype = BOOL
|
|
||||||
|
|
||||||
|
|
||||||
#
|
|
||||||
# debugger.py
|
|
||||||
|
|
||||||
FLAG_TRACE_BIT = 0x100
|
|
||||||
|
|
||||||
class DebuggerError(Exception):
|
|
||||||
pass
|
|
||||||
|
|
||||||
class Debugger(object):
|
|
||||||
def __init__(self, process_info):
|
|
||||||
self.process_info = process_info
|
|
||||||
self.pid = process_info.dwProcessId
|
|
||||||
self.tid = process_info.dwThreadId
|
|
||||||
self.hprocess = process_info.hProcess
|
|
||||||
self.hthread = process_info.hThread
|
|
||||||
self._threads = {self.tid: self.hthread}
|
|
||||||
self._processes = {self.pid: self.hprocess}
|
|
||||||
self._bps = {}
|
|
||||||
self._inactive = {}
|
|
||||||
|
|
||||||
def read_process_memory(self, addr, size=None, type=str):
|
|
||||||
if issubclass(type, basestring):
|
|
||||||
buf = ctypes.create_string_buffer(size)
|
|
||||||
ref = buf
|
|
||||||
else:
|
|
||||||
size = ctypes.sizeof(type)
|
|
||||||
buf = type()
|
|
||||||
ref = byref(buf)
|
|
||||||
copied = SIZE_T(0)
|
|
||||||
rv = ReadProcessMemory(self.hprocess, addr, ref, size, byref(copied))
|
|
||||||
if not rv:
|
|
||||||
addr = getattr(addr, 'value', addr)
|
|
||||||
raise DebuggerError("could not read memory @ 0x%08x" % (addr,))
|
|
||||||
if copied.value != size:
|
|
||||||
raise DebuggerError("insufficient memory read")
|
|
||||||
if issubclass(type, basestring):
|
|
||||||
return buf.raw
|
|
||||||
return buf
|
|
||||||
|
|
||||||
def set_bp(self, addr, callback, bytev=None):
|
|
||||||
hprocess = self.hprocess
|
|
||||||
if bytev is None:
|
|
||||||
byte = self.read_process_memory(addr, type=ctypes.c_byte)
|
|
||||||
bytev = byte.value
|
|
||||||
else:
|
|
||||||
byte = ctypes.c_byte(0)
|
|
||||||
self._bps[addr] = (bytev, callback)
|
|
||||||
byte.value = 0xcc
|
|
||||||
copied = SIZE_T(0)
|
|
||||||
rv = WriteProcessMemory(hprocess, addr, byref(byte), 1, byref(copied))
|
|
||||||
if not rv:
|
|
||||||
addr = getattr(addr, 'value', addr)
|
|
||||||
raise DebuggerError("could not write memory @ 0x%08x" % (addr,))
|
|
||||||
if copied.value != 1:
|
|
||||||
raise DebuggerError("insufficient memory written")
|
|
||||||
rv = FlushInstructionCache(hprocess, None, 0)
|
|
||||||
if not rv:
|
|
||||||
raise DebuggerError("could not flush instruction cache")
|
|
||||||
return
|
|
||||||
|
|
||||||
def _restore_bps(self):
|
|
||||||
for addr, (bytev, callback) in self._inactive.items():
|
|
||||||
self.set_bp(addr, callback, bytev=bytev)
|
|
||||||
self._inactive.clear()
|
|
||||||
|
|
||||||
def _handle_bp(self, addr):
|
|
||||||
hprocess = self.hprocess
|
|
||||||
hthread = self.hthread
|
|
||||||
bytev, callback = self._inactive[addr] = self._bps.pop(addr)
|
|
||||||
byte = ctypes.c_byte(bytev)
|
|
||||||
copied = SIZE_T(0)
|
|
||||||
rv = WriteProcessMemory(hprocess, addr, byref(byte), 1, byref(copied))
|
|
||||||
if not rv:
|
|
||||||
raise DebuggerError("could not write memory")
|
|
||||||
if copied.value != 1:
|
|
||||||
raise DebuggerError("insufficient memory written")
|
|
||||||
rv = FlushInstructionCache(hprocess, None, 0)
|
|
||||||
if not rv:
|
|
||||||
raise DebuggerError("could not flush instruction cache")
|
|
||||||
context = CONTEXT(ContextFlags=CONTEXT_FULL)
|
|
||||||
rv = GetThreadContext(hthread, byref(context))
|
|
||||||
if not rv:
|
|
||||||
raise DebuggerError("could not get thread context")
|
|
||||||
context.Eip = addr
|
|
||||||
callback(self, context)
|
|
||||||
context.EFlags |= FLAG_TRACE_BIT
|
|
||||||
rv = SetThreadContext(hthread, byref(context))
|
|
||||||
if not rv:
|
|
||||||
raise DebuggerError("could not set thread context")
|
|
||||||
return
|
|
||||||
|
|
||||||
def _get_peb_address(self):
|
|
||||||
hthread = self.hthread
|
|
||||||
hprocess = self.hprocess
|
|
||||||
try:
|
|
||||||
pbi = PROCESS_BASIC_INFORMATION()
|
|
||||||
rv = NtQueryInformationProcess(hprocess, 0, byref(pbi),
|
|
||||||
sizeof(pbi), None)
|
|
||||||
if rv != 0:
|
|
||||||
raise DebuggerError("could not query process information")
|
|
||||||
return pbi.PebBaseAddress
|
|
||||||
except DebuggerError:
|
|
||||||
pass
|
|
||||||
try:
|
|
||||||
context = CONTEXT(ContextFlags=CONTEXT_FULL)
|
|
||||||
rv = GetThreadContext(hthread, byref(context))
|
|
||||||
if not rv:
|
|
||||||
raise DebuggerError("could not get thread context")
|
|
||||||
entry = LDT_ENTRY()
|
|
||||||
rv = GetThreadSelectorEntry(hthread, context.SegFs, byref(entry))
|
|
||||||
if not rv:
|
|
||||||
raise DebuggerError("could not get selector entry")
|
|
||||||
low, mid, high = entry.BaseLow, entry.BaseMid, entry.BaseHi
|
|
||||||
fsbase = low | (mid << 16) | (high << 24)
|
|
||||||
pebaddr = self.read_process_memory(fsbase + 0x30, type=c_voidp)
|
|
||||||
return pebaddr.value
|
|
||||||
except DebuggerError:
|
|
||||||
pass
|
|
||||||
return 0x7ffdf000
|
|
||||||
|
|
||||||
def get_base_address(self):
|
|
||||||
addr = self._get_peb_address() + (2 * 4)
|
|
||||||
baseaddr = self.read_process_memory(addr, type=c_voidp)
|
|
||||||
return baseaddr.value
|
|
||||||
|
|
||||||
def main_loop(self):
|
|
||||||
event = DEBUG_EVENT()
|
|
||||||
finished = False
|
|
||||||
while not finished:
|
|
||||||
rv = WaitForDebugEvent(byref(event), INFINITE)
|
|
||||||
if not rv:
|
|
||||||
raise DebuggerError("could not get debug event")
|
|
||||||
self.pid = pid = event.dwProcessId
|
|
||||||
self.tid = tid = event.dwThreadId
|
|
||||||
self.hprocess = self._processes.get(pid, None)
|
|
||||||
self.hthread = self._threads.get(tid, None)
|
|
||||||
status = DBG_CONTINUE
|
|
||||||
evid = event.dwDebugEventCode
|
|
||||||
if evid == EXCEPTION_DEBUG_EVENT:
|
|
||||||
first = event.Exception.dwFirstChance
|
|
||||||
record = event.Exception.ExceptionRecord
|
|
||||||
exid = record.ExceptionCode
|
|
||||||
flags = record.ExceptionFlags
|
|
||||||
addr = record.ExceptionAddress
|
|
||||||
if exid == EXCEPTION_BREAKPOINT:
|
|
||||||
if addr in self._bps:
|
|
||||||
self._handle_bp(addr)
|
|
||||||
elif exid == EXCEPTION_SINGLE_STEP:
|
|
||||||
self._restore_bps()
|
|
||||||
else:
|
|
||||||
status = DBG_EXCEPTION_NOT_HANDLED
|
|
||||||
elif evid == LOAD_DLL_DEBUG_EVENT:
|
|
||||||
hfile = event.LoadDll.hFile
|
|
||||||
if hfile is not None:
|
|
||||||
rv = CloseHandle(hfile)
|
|
||||||
if not rv:
|
|
||||||
raise DebuggerError("error closing file handle")
|
|
||||||
elif evid == CREATE_THREAD_DEBUG_EVENT:
|
|
||||||
info = event.CreateThread
|
|
||||||
self.hthread = info.hThread
|
|
||||||
self._threads[tid] = self.hthread
|
|
||||||
elif evid == EXIT_THREAD_DEBUG_EVENT:
|
|
||||||
hthread = self._threads.pop(tid, None)
|
|
||||||
if hthread is not None:
|
|
||||||
rv = CloseHandle(hthread)
|
|
||||||
if not rv:
|
|
||||||
raise DebuggerError("error closing thread handle")
|
|
||||||
elif evid == CREATE_PROCESS_DEBUG_EVENT:
|
|
||||||
info = event.CreateProcessInfo
|
|
||||||
self.hprocess = info.hProcess
|
|
||||||
self._processes[pid] = self.hprocess
|
|
||||||
elif evid == EXIT_PROCESS_DEBUG_EVENT:
|
|
||||||
hprocess = self._processes.pop(pid, None)
|
|
||||||
if hprocess is not None:
|
|
||||||
rv = CloseHandle(hprocess)
|
|
||||||
if not rv:
|
|
||||||
raise DebuggerError("error closing process handle")
|
|
||||||
if pid == self.process_info.dwProcessId:
|
|
||||||
finished = True
|
|
||||||
rv = ContinueDebugEvent(pid, tid, status)
|
|
||||||
if not rv:
|
|
||||||
raise DebuggerError("could not continue debug")
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
#
|
|
||||||
# unswindle.py
|
|
||||||
|
|
||||||
KINDLE_REG_KEY = \
|
|
||||||
r'Software\Classes\Amazon.KindleForPC.content\shell\open\command'
|
|
||||||
|
|
||||||
class UnswindleError(Exception):
|
|
||||||
pass
|
|
||||||
|
|
||||||
class PC1KeyGrabber(object):
|
|
||||||
HOOKS = {
|
|
||||||
'b9f7e422094b8c8966a0e881e6358116e03e5b7b': {
|
|
||||||
0x004a719d: '_no_debugger_here',
|
|
||||||
0x005a795b: '_no_debugger_here',
|
|
||||||
0x0054f7e0: '_get_pc1_pid',
|
|
||||||
0x004f9c79: '_get_book_path',
|
|
||||||
},
|
|
||||||
'd5124ee20dab10e44b41a039363f6143725a5417': {
|
|
||||||
0x0041150d: '_i_like_wine',
|
|
||||||
0x004a681d: '_no_debugger_here',
|
|
||||||
0x005a438b: '_no_debugger_here',
|
|
||||||
0x0054c9e0: '_get_pc1_pid',
|
|
||||||
0x004f8ac9: '_get_book_path',
|
|
||||||
},
|
|
||||||
'd791f52dd2ecc68722212d801ad52cb79d1b6fc9': {
|
|
||||||
0x0041724d: '_i_like_wine',
|
|
||||||
0x004bfe3d: '_no_debugger_here',
|
|
||||||
0x005bd9db: '_no_debugger_here',
|
|
||||||
0x00565920: '_get_pc1_pid',
|
|
||||||
0x0050fde9: '_get_book_path',
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
MOBI_EXTENSIONS = set(['.prc', '.pdb', '.mobi', '.azw', '.az1', '.azw1'])
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def supported_version(cls, hexdigest):
|
|
||||||
return (hexdigest in cls.HOOKS)
|
|
||||||
|
|
||||||
def _taddr(self, addr):
|
|
||||||
return (addr - 0x00400000) + self.baseaddr
|
|
||||||
|
|
||||||
def __init__(self, debugger, hexdigest):
|
|
||||||
self.book_path = None
|
|
||||||
self.book_pid = None
|
|
||||||
self.baseaddr = debugger.get_base_address()
|
|
||||||
hooks = self.HOOKS[hexdigest]
|
|
||||||
for addr, mname in hooks.items():
|
|
||||||
debugger.set_bp(self._taddr(addr), getattr(self, mname))
|
|
||||||
|
|
||||||
def _i_like_wine(self, debugger, context):
|
|
||||||
context.Eax = 1
|
|
||||||
return
|
|
||||||
|
|
||||||
def _no_debugger_here(self, debugger, context):
|
|
||||||
context.Eip += 2
|
|
||||||
context.Eax = 0
|
|
||||||
return
|
|
||||||
|
|
||||||
def _get_book_path(self, debugger, context):
|
|
||||||
addr = debugger.read_process_memory(context.Esp, type=ctypes.c_voidp)
|
|
||||||
try:
|
|
||||||
path = debugger.read_process_memory(addr, 4096)
|
|
||||||
except DebuggerError:
|
|
||||||
pgrest = 0x1000 - (addr.value & 0xfff)
|
|
||||||
path = debugger.read_process_memory(addr, pgrest)
|
|
||||||
path = path.decode('utf-16', 'ignore')
|
|
||||||
if u'\0' in path:
|
|
||||||
path = path[:path.index(u'\0')]
|
|
||||||
root, ext = os.path.splitext(path)
|
|
||||||
if ext.lower() not in self.MOBI_EXTENSIONS:
|
|
||||||
return
|
|
||||||
self.book_path = path
|
|
||||||
|
|
||||||
def _get_pc1_pid(self, debugger, context):
|
|
||||||
addr = context.Esp + ctypes.sizeof(ctypes.c_voidp)
|
|
||||||
addr = debugger.read_process_memory(addr, type=ctypes.c_char_p)
|
|
||||||
pid = debugger.read_process_memory(addr, 8)
|
|
||||||
pid = self._checksum_pid(pid)
|
|
||||||
self.book_pid = pid
|
|
||||||
|
|
||||||
def _checksum_pid(self, s):
|
|
||||||
letters = "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789"
|
|
||||||
crc = (~binascii.crc32(s,-1))&0xFFFFFFFF
|
|
||||||
crc = crc ^ (crc >> 16)
|
|
||||||
res = s
|
|
||||||
l = len(letters)
|
|
||||||
for i in (0,1):
|
|
||||||
b = crc & 0xff
|
|
||||||
pos = (b // l) ^ (b % l)
|
|
||||||
res += letters[pos%l]
|
|
||||||
crc >>= 8
|
|
||||||
return res
|
|
||||||
|
|
||||||
class MobiParser(object):
|
|
||||||
def __init__(self, data):
|
|
||||||
self.data = data
|
|
||||||
header = data[0:72]
|
|
||||||
if header[0x3C:0x3C+8] != 'BOOKMOBI':
|
|
||||||
raise UnswindleError("invalid file format")
|
|
||||||
self.nsections = nsections = struct.unpack('>H', data[76:78])[0]
|
|
||||||
self.sections = sections = []
|
|
||||||
for i in xrange(nsections):
|
|
||||||
offset, a1, a2, a3, a4 = \
|
|
||||||
struct.unpack('>LBBBB', data[78+i*8:78+i*8+8])
|
|
||||||
flags, val = a1, ((a2 << 16) | (a3 << 8) | a4)
|
|
||||||
sections.append((offset, flags, val))
|
|
||||||
sect = self.load_section(0)
|
|
||||||
self.crypto_type = struct.unpack('>H', sect[0x0c:0x0c+2])[0]
|
|
||||||
|
|
||||||
def load_section(self, snum):
|
|
||||||
if (snum + 1) == self.nsections:
|
|
||||||
endoff = len(self.data)
|
|
||||||
else:
|
|
||||||
endoff = self.sections[snum + 1][0]
|
|
||||||
off = self.sections[snum][0]
|
|
||||||
return self.data[off:endoff]
|
|
||||||
|
|
||||||
class Unswindler(object):
|
|
||||||
def __init__(self):
|
|
||||||
self._exepath = self._get_exe_path()
|
|
||||||
self._hexdigest = self._get_hexdigest()
|
|
||||||
self._exedir = os.path.dirname(self._exepath)
|
|
||||||
self._mobidedrmpath = self._get_mobidedrm_path()
|
|
||||||
|
|
||||||
def _get_mobidedrm_path(self):
|
|
||||||
basedir = sys.modules[self.__module__].__file__
|
|
||||||
basedir = os.path.dirname(basedir)
|
|
||||||
for basename in ('mobidedrm', 'mobidedrm.py', 'mobidedrm.pyw'):
|
|
||||||
path = os.path.join(basedir, basename)
|
|
||||||
if os.path.isfile(path):
|
|
||||||
return path
|
|
||||||
raise UnswindleError("could not locate MobiDeDRM script")
|
|
||||||
|
|
||||||
def _get_exe_path(self):
|
|
||||||
path = None
|
|
||||||
for root in (winreg.HKEY_CURRENT_USER, winreg.HKEY_LOCAL_MACHINE):
|
|
||||||
try:
|
|
||||||
regkey = winreg.OpenKey(root, KINDLE_REG_KEY)
|
|
||||||
path = winreg.QueryValue(regkey, None)
|
|
||||||
break
|
|
||||||
except WindowsError:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
raise UnswindleError("Kindle For PC installation not found")
|
|
||||||
if '"' in path:
|
|
||||||
path = re.search(r'"(.*?)"', path).group(1)
|
|
||||||
return path
|
|
||||||
|
|
||||||
def _get_hexdigest(self):
|
|
||||||
path = self._exepath
|
|
||||||
sha1 = hashlib.sha1()
|
|
||||||
with open(path, 'rb') as f:
|
|
||||||
data = f.read(4096)
|
|
||||||
while data:
|
|
||||||
sha1.update(data)
|
|
||||||
data = f.read(4096)
|
|
||||||
hexdigest = sha1.hexdigest()
|
|
||||||
if not PC1KeyGrabber.supported_version(hexdigest):
|
|
||||||
raise UnswindleError("Unsupported version of Kindle For PC")
|
|
||||||
return hexdigest
|
|
||||||
|
|
||||||
def _check_topaz(self, path):
|
|
||||||
with open(path, 'rb') as f:
|
|
||||||
magic = f.read(4)
|
|
||||||
if magic == 'TPZ0':
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
def _check_drm_free(self, path):
|
|
||||||
with open(path, 'rb') as f:
|
|
||||||
crypto = MobiParser(f.read()).crypto_type
|
|
||||||
return (crypto == 0)
|
|
||||||
|
|
||||||
def get_book(self):
|
|
||||||
creation_flags = (CREATE_UNICODE_ENVIRONMENT |
|
|
||||||
DEBUG_PROCESS |
|
|
||||||
DEBUG_ONLY_THIS_PROCESS)
|
|
||||||
startup_info = STARTUPINFO()
|
|
||||||
process_info = PROCESS_INFORMATION()
|
|
||||||
path = pid = None
|
|
||||||
try:
|
|
||||||
rv = CreateProcess(self._exepath, None, None, None, False,
|
|
||||||
creation_flags, None, self._exedir,
|
|
||||||
byref(startup_info), byref(process_info))
|
|
||||||
if not rv:
|
|
||||||
raise UnswindleError("failed to launch Kindle For PC")
|
|
||||||
debugger = Debugger(process_info)
|
|
||||||
grabber = PC1KeyGrabber(debugger, self._hexdigest)
|
|
||||||
debugger.main_loop()
|
|
||||||
path = grabber.book_path
|
|
||||||
pid = grabber.book_pid
|
|
||||||
finally:
|
|
||||||
if process_info.hThread is not None:
|
|
||||||
CloseHandle(process_info.hThread)
|
|
||||||
if process_info.hProcess is not None:
|
|
||||||
CloseHandle(process_info.hProcess)
|
|
||||||
if path is None:
|
|
||||||
raise UnswindleError("failed to determine book path")
|
|
||||||
if self._check_topaz(path):
|
|
||||||
raise UnswindleError("cannot decrypt Topaz format book")
|
|
||||||
return (path, pid)
|
|
||||||
|
|
||||||
def decrypt_book(self, inpath, outpath, pid):
|
|
||||||
if self._check_drm_free(inpath):
|
|
||||||
shutil.copy(inpath, outpath)
|
|
||||||
else:
|
|
||||||
self._mobidedrm(inpath, outpath, pid)
|
|
||||||
return
|
|
||||||
|
|
||||||
def _mobidedrm(self, inpath, outpath, pid):
|
|
||||||
# darkreverser didn't protect mobidedrm's script execution to allow
|
|
||||||
# importing, so we have to just run it in a subprocess
|
|
||||||
if pid is None:
|
|
||||||
raise UnswindleError("failed to determine book PID")
|
|
||||||
with tempfile.NamedTemporaryFile(delete=False) as tmpf:
|
|
||||||
tmppath = tmpf.name
|
|
||||||
args = [sys.executable, self._mobidedrmpath, inpath, tmppath, pid]
|
|
||||||
mobidedrm = subprocess.Popen(args, stderr=subprocess.STDOUT,
|
|
||||||
stdout=subprocess.PIPE,
|
|
||||||
universal_newlines=True)
|
|
||||||
output = mobidedrm.communicate()[0]
|
|
||||||
if not output.endswith("done\n"):
|
|
||||||
try:
|
|
||||||
os.remove(tmppath)
|
|
||||||
except OSError:
|
|
||||||
pass
|
|
||||||
raise UnswindleError("problem running MobiDeDRM:\n" + output)
|
|
||||||
shutil.move(tmppath, outpath)
|
|
||||||
return
|
|
||||||
|
|
||||||
class ExceptionDialog(Tkinter.Frame):
|
|
||||||
def __init__(self, root, text):
|
|
||||||
Tkinter.Frame.__init__(self, root, border=5)
|
|
||||||
label = Tkinter.Label(self, text="Unexpected error:",
|
|
||||||
anchor=Tkconstants.W, justify=Tkconstants.LEFT)
|
|
||||||
label.pack(fill=Tkconstants.X, expand=0)
|
|
||||||
self.text = Tkinter.Text(self)
|
|
||||||
self.text.pack(fill=Tkconstants.BOTH, expand=1)
|
|
||||||
self.text.insert(Tkconstants.END, text)
|
|
||||||
|
|
||||||
def gui_main(argv=sys.argv):
|
|
||||||
root = Tkinter.Tk()
|
|
||||||
root.withdraw()
|
|
||||||
progname = os.path.basename(argv[0])
|
|
||||||
try:
|
|
||||||
unswindler = Unswindler()
|
|
||||||
inpath, pid = unswindler.get_book()
|
|
||||||
outpath = tkFileDialog.asksaveasfilename(
|
|
||||||
parent=None, title='Select unencrypted Mobipocket file to produce',
|
|
||||||
defaultextension='.mobi', filetypes=[('MOBI files', '.mobi'),
|
|
||||||
('All files', '.*')])
|
|
||||||
if not outpath:
|
|
||||||
return 0
|
|
||||||
unswindler.decrypt_book(inpath, outpath, pid)
|
|
||||||
except UnswindleError, e:
|
|
||||||
tkMessageBox.showerror("Unswindle For PC", "Error: " + str(e))
|
|
||||||
return 1
|
|
||||||
except Exception:
|
|
||||||
root.wm_state('normal')
|
|
||||||
root.title('Unswindle For PC')
|
|
||||||
text = traceback.format_exc()
|
|
||||||
ExceptionDialog(root, text).pack(fill=Tkconstants.BOTH, expand=1)
|
|
||||||
root.mainloop()
|
|
||||||
return 1
|
|
||||||
|
|
||||||
def cli_main(argv=sys.argv):
|
|
||||||
progname = os.path.basename(argv[0])
|
|
||||||
args = argv[1:]
|
|
||||||
if len(args) != 1:
|
|
||||||
sys.stderr.write("usage: %s OUTFILE\n" % (progname,))
|
|
||||||
return 1
|
|
||||||
outpath = args[0]
|
|
||||||
unswindler = Unswindler()
|
|
||||||
inpath, pid = unswindler.get_book()
|
|
||||||
unswindler.decrypt_book(inpath, outpath, pid)
|
|
||||||
return 0
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
sys.exit(gui_main())
|
|
|
@ -0,0 +1,50 @@
|
||||||
|
Kindle for Android
|
||||||
|
------------------
|
||||||
|
|
||||||
|
Kindle for Android uses a different scheme to generate its books specific PIDs than Kindle for PC, Kindle for iPhone/iPad, and standalone Kindles.
|
||||||
|
|
||||||
|
Unfortunately, K4Android uses an "account secrets" file that would only be available if the device were jail-broken and even then someone would have to figure out how to decode this secret information in order to reverse the process.
|
||||||
|
|
||||||
|
Instead of trying to calculate the correct PIDs for each book from this primary data, "Me" (a commenter who posted to the ApprenticeAlf site) came up with a wonderful idea to simply modify the Kindle 3 for Android application to store the PIDs it uses to decode each book in its "about activity" window. This list of PIDS can then be provided to MobiDeDRM.py, which in its latest incarnations allows a comma separated list of pids to be passed in, to successfully remove the DRM from that book. Effectively "Me" has created an "Unswindle" for the Kindle for Android 3 application!
|
||||||
|
|
||||||
|
Obviously, to use "Me"'s approach, requires an Android Developer's Certificate (to sign the modified application) and access to and knowledge of the developer tools, but does not require anything to be jail-broken.
|
||||||
|
|
||||||
|
This is a copy the detailed instructions supplied by "Me" to the ApprenticeAlf blog in the comments. The kindle3.patch described below is included in this folder in the tools:
|
||||||
|
|
||||||
|
From the ApprenticeAlf Comments:
|
||||||
|
|
||||||
|
"Me" writes:
|
||||||
|
|
||||||
|
A better solution seems to create a patched version of the Kindle apk which either logs or displays it’s PID. I created a patch to both log the pid list and show it in the Kindle application in the about activity screen. The pid list isn’t available until the DRMed book has been opened (and the list seem to differ for different books).
|
||||||
|
|
||||||
|
To create the patched kindle apk a certificate must be created (http://developer.android.com/guide/publishing/app-signing.html#cert) and the apktool must be build from source (all subprojects) as long as version 1.4.2 isn’t released (http://code.google.com/p/android-apktool/wiki/BuildApktool).
|
||||||
|
|
||||||
|
These are the steps to pull the original apk from the Android device, uninstall it, create a patched apk and install that (tested on a rooted device, but I think all steps should also work on non-rooted devices):
|
||||||
|
|
||||||
|
adb pull /data/app/com.amazon.kindle-1.apk kindle3.apk
|
||||||
|
adb uninstall com.amazon.kindle
|
||||||
|
apktool d kindle3.apk kindle3
|
||||||
|
cd kindle3
|
||||||
|
patch -p1 < ..\kindle3.patch
|
||||||
|
cd ..
|
||||||
|
apktool b kindle3 kindle3_patched.apk
|
||||||
|
jarsigner -verbose -keystore kindle.keystore kindle3_patched.apk kindle
|
||||||
|
zipalign -v 4 kindle3_patched.apk kindle3_signed.apk
|
||||||
|
adb install kindle3_signed.apk
|
||||||
|
|
||||||
|
kindle3.patch (based on kindle version 3.0.1.70) is available on pastebin:
|
||||||
|
http://pastebin.com/LNpgkcpP
|
||||||
|
|
||||||
|
Have fun!
|
||||||
|
|
||||||
|
Comment by me — June 9, 2011 @ 9:01 pm | Reply
|
||||||
|
|
||||||
|
Hi me,
|
||||||
|
Wow! Great work!!!!
|
||||||
|
|
||||||
|
With your patch, you have created the equivalent of Unswindle for the Kindle for Android app and it does not even require jailbreaking!
|
||||||
|
|
||||||
|
Very nice work indeed!
|
||||||
|
|
||||||
|
Comment by some_updates — June 10, 2011 @ 4:28 am | Reply
|
||||||
|
|
|
@ -0,0 +1,100 @@
|
||||||
|
diff -ru kindle3_orig/smali/com/amazon/kcp/application/AndroidDeviceInformationProvider.smali kindle3/smali/com/amazon/kcp/application/AndroidDeviceInformationProvider.smali
|
||||||
|
--- kindle3_orig/smali/com/amazon/kcp/application/AndroidDeviceInformationProvider.smali
|
||||||
|
+++ kindle3/smali/com/amazon/kcp/application/AndroidDeviceInformationProvider.smali
|
||||||
|
@@ -11,6 +11,8 @@
|
||||||
|
|
||||||
|
.field private security:Lcom/mobipocket/android/library/reader/AndroidSecurity;
|
||||||
|
|
||||||
|
+.field private pidList:Ljava/lang/String;
|
||||||
|
+
|
||||||
|
|
||||||
|
# direct methods
|
||||||
|
.method public constructor <init>(Lcom/mobipocket/android/library/reader/AndroidSecurity;Lcom/amazon/kcp/application/AndroidDeviceType;)V
|
||||||
|
@@ -28,6 +30,10 @@
|
||||||
|
.line 26
|
||||||
|
iput-object p2, p0, Lcom/amazon/kcp/application/AndroidDeviceInformationProvider;->deviceType:Lcom/amazon/kcp/application/AndroidDeviceType;
|
||||||
|
|
||||||
|
+ const-string v0, "Open DRMed book to show PID list."
|
||||||
|
+
|
||||||
|
+ iput-object v0, p0, Lcom/amazon/kcp/application/AndroidDeviceInformationProvider;->pidList:Ljava/lang/String;
|
||||||
|
+
|
||||||
|
.line 27
|
||||||
|
new-instance v0, Ljava/lang/StringBuilder;
|
||||||
|
|
||||||
|
@@ -175,4 +181,26 @@
|
||||||
|
move-result-object v0
|
||||||
|
|
||||||
|
return-object v0
|
||||||
|
+.end method
|
||||||
|
+
|
||||||
|
+.method public getPidList()Ljava/lang/String;
|
||||||
|
+ .locals 1
|
||||||
|
+
|
||||||
|
+ .prologue
|
||||||
|
+ .line 15
|
||||||
|
+ iget-object v0, p0, Lcom/amazon/kcp/application/AndroidDeviceInformationProvider;->pidList:Ljava/lang/String;
|
||||||
|
+
|
||||||
|
+ return-object v0
|
||||||
|
+.end method
|
||||||
|
+
|
||||||
|
+.method public setPidList(Ljava/lang/String;)V
|
||||||
|
+ .locals 0
|
||||||
|
+ .parameter "value"
|
||||||
|
+
|
||||||
|
+ .prologue
|
||||||
|
+ .line 11
|
||||||
|
+ iput-object p1, p0, Lcom/amazon/kcp/application/AndroidDeviceInformationProvider;->pidList:Ljava/lang/String;
|
||||||
|
+
|
||||||
|
+ .line 12
|
||||||
|
+ return-void
|
||||||
|
.end method
|
||||||
|
diff -ru kindle3_orig/smali/com/amazon/kcp/application/IDeviceInformationProvider.smali kindle3/smali/com/amazon/kcp/application/IDeviceInformationProvider.smali
|
||||||
|
--- kindle3_orig/smali/com/amazon/kcp/application/IDeviceInformationProvider.smali
|
||||||
|
+++ kindle3/smali/com/amazon/kcp/application/IDeviceInformationProvider.smali
|
||||||
|
@@ -27,3 +27,9 @@
|
||||||
|
|
||||||
|
.method public abstract getPid()Ljava/lang/String;
|
||||||
|
.end method
|
||||||
|
+
|
||||||
|
+.method public abstract getPidList()Ljava/lang/String;
|
||||||
|
+.end method
|
||||||
|
+
|
||||||
|
+.method public abstract setPidList(Ljava/lang/String;)V
|
||||||
|
+.end method
|
||||||
|
\ No newline at end of file
|
||||||
|
diff -ru kindle3_orig/smali/com/amazon/kcp/info/AboutActivity.smali kindle3/smali/com/amazon/kcp/info/AboutActivity.smali
|
||||||
|
--- kindle3_orig/smali/com/amazon/kcp/info/AboutActivity.smali
|
||||||
|
+++ kindle3/smali/com/amazon/kcp/info/AboutActivity.smali
|
||||||
|
@@ -32,9 +32,11 @@
|
||||||
|
invoke-direct {v6, v1}, Ljava/util/ArrayList;-><init>(I)V
|
||||||
|
|
||||||
|
.line 36
|
||||||
|
- const v1, 0x7f0b0005
|
||||||
|
+ invoke-static {}, Lcom/amazon/kcp/application/DeviceInformationProviderFactory;->getProvider()Lcom/amazon/kcp/application/IDeviceInformationProvider;
|
||||||
|
|
||||||
|
- invoke-virtual {p0, v1}, Lcom/amazon/kcp/info/AboutActivity;->getString(I)Ljava/lang/String;
|
||||||
|
+ move-result-object v0
|
||||||
|
+
|
||||||
|
+ invoke-interface {v0}, Lcom/amazon/kcp/application/IDeviceInformationProvider;->getPidList()Ljava/lang/String;
|
||||||
|
|
||||||
|
move-result-object v1
|
||||||
|
|
||||||
|
diff -ru kindle3_orig/smali/com/amazon/system/security/Security.smali kindle3/smali/com/amazon/system/security/Security.smali
|
||||||
|
--- kindle3_orig/smali/com/amazon/system/security/Security.smali
|
||||||
|
+++ kindle3/smali/com/amazon/system/security/Security.smali
|
||||||
|
@@ -884,6 +884,15 @@
|
||||||
|
|
||||||
|
.line 332
|
||||||
|
:cond_1
|
||||||
|
+
|
||||||
|
+ const-string v1, "PID list"
|
||||||
|
+ invoke-static {}, Lcom/amazon/kcp/application/DeviceInformationProviderFactory;->getProvider()Lcom/amazon/kcp/application/IDeviceInformationProvider;
|
||||||
|
+ move-result-object v0
|
||||||
|
+ invoke-static {v7}, Ljava/util/Arrays;->toString([Ljava/lang/Object;)Ljava/lang/String;
|
||||||
|
+ move-result-object v2
|
||||||
|
+ invoke-interface {v0, v2}, Lcom/amazon/kcp/application/IDeviceInformationProvider;->setPidList(Ljava/lang/String;)V
|
||||||
|
+ invoke-static {v1, v2}, Landroid/util/Log;->e(Ljava/lang/String;Ljava/lang/String;)I
|
||||||
|
+
|
||||||
|
return-object v7
|
||||||
|
|
||||||
|
:cond_2
|
|
@ -51,8 +51,9 @@
|
||||||
# 0.29 - It seems that the ideas about when multibyte trailing characters were
|
# 0.29 - It seems that the ideas about when multibyte trailing characters were
|
||||||
# included in the encryption were wrong. They aren't for DOC compressed
|
# included in the encryption were wrong. They aren't for DOC compressed
|
||||||
# files, but they are for HUFF/CDIC compress files!
|
# files, but they are for HUFF/CDIC compress files!
|
||||||
|
# 0.30 - Modified interface slightly to work better with new calibre plugin style
|
||||||
|
|
||||||
__version__ = '0.29'
|
__version__ = '0.30'
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
@ -163,6 +164,7 @@ class MobiBook:
|
||||||
def __init__(self, infile):
|
def __init__(self, infile):
|
||||||
# initial sanity check on file
|
# initial sanity check on file
|
||||||
self.data_file = file(infile, 'rb').read()
|
self.data_file = file(infile, 'rb').read()
|
||||||
|
self.mobi_data = ''
|
||||||
self.header = self.data_file[0:78]
|
self.header = self.data_file[0:78]
|
||||||
if self.header[0x3C:0x3C+8] != 'BOOKMOBI' and self.header[0x3C:0x3C+8] != 'TEXtREAd':
|
if self.header[0x3C:0x3C+8] != 'BOOKMOBI' and self.header[0x3C:0x3C+8] != 'TEXtREAd':
|
||||||
raise DrmException("invalid file format")
|
raise DrmException("invalid file format")
|
||||||
|
@ -301,13 +303,17 @@ class MobiBook:
|
||||||
break
|
break
|
||||||
return [found_key,pid]
|
return [found_key,pid]
|
||||||
|
|
||||||
|
def getMobiFile(self, outpath):
|
||||||
|
file(outpath,'wb').write(self.mobi_data)
|
||||||
|
|
||||||
def processBook(self, pidlist):
|
def processBook(self, pidlist):
|
||||||
crypto_type, = struct.unpack('>H', self.sect[0xC:0xC+2])
|
crypto_type, = struct.unpack('>H', self.sect[0xC:0xC+2])
|
||||||
print 'Crypto Type is: ', crypto_type
|
print 'Crypto Type is: ', crypto_type
|
||||||
self.crypto_type = crypto_type
|
self.crypto_type = crypto_type
|
||||||
if crypto_type == 0:
|
if crypto_type == 0:
|
||||||
print "This book is not encrypted."
|
print "This book is not encrypted."
|
||||||
return self.data_file
|
self.mobi_data = self.data_file
|
||||||
|
return
|
||||||
if crypto_type != 2 and crypto_type != 1:
|
if crypto_type != 2 and crypto_type != 1:
|
||||||
raise DrmException("Cannot decode unknown Mobipocket encryption type %d" % crypto_type)
|
raise DrmException("Cannot decode unknown Mobipocket encryption type %d" % crypto_type)
|
||||||
|
|
||||||
|
@ -353,33 +359,35 @@ class MobiBook:
|
||||||
|
|
||||||
# decrypt sections
|
# decrypt sections
|
||||||
print "Decrypting. Please wait . . .",
|
print "Decrypting. Please wait . . .",
|
||||||
new_data = self.data_file[:self.sections[1][0]]
|
self.mobi_data = self.data_file[:self.sections[1][0]]
|
||||||
for i in xrange(1, self.records+1):
|
for i in xrange(1, self.records+1):
|
||||||
data = self.loadSection(i)
|
data = self.loadSection(i)
|
||||||
extra_size = getSizeOfTrailingDataEntries(data, len(data), self.extra_data_flags)
|
extra_size = getSizeOfTrailingDataEntries(data, len(data), self.extra_data_flags)
|
||||||
if i%100 == 0:
|
if i%100 == 0:
|
||||||
print ".",
|
print ".",
|
||||||
# print "record %d, extra_size %d" %(i,extra_size)
|
# print "record %d, extra_size %d" %(i,extra_size)
|
||||||
new_data += PC1(found_key, data[0:len(data) - extra_size])
|
self.mobi_data += PC1(found_key, data[0:len(data) - extra_size])
|
||||||
if extra_size > 0:
|
if extra_size > 0:
|
||||||
new_data += data[-extra_size:]
|
self.mobi_data += data[-extra_size:]
|
||||||
if self.num_sections > self.records+1:
|
if self.num_sections > self.records+1:
|
||||||
new_data += self.data_file[self.sections[self.records+1][0]:]
|
self.mobi_data += self.data_file[self.sections[self.records+1][0]:]
|
||||||
self.data_file = new_data
|
|
||||||
print "done"
|
print "done"
|
||||||
return self.data_file
|
return
|
||||||
|
|
||||||
def getUnencryptedBook(infile,pid):
|
def getUnencryptedBook(infile,pid):
|
||||||
if not os.path.isfile(infile):
|
if not os.path.isfile(infile):
|
||||||
raise DrmException('Input File Not Found')
|
raise DrmException('Input File Not Found')
|
||||||
book = MobiBook(infile)
|
book = MobiBook(infile)
|
||||||
return book.processBook([pid])
|
book.processBook([pid])
|
||||||
|
return book.mobi_data
|
||||||
|
|
||||||
def getUnencryptedBookWithList(infile,pidlist):
|
def getUnencryptedBookWithList(infile,pidlist):
|
||||||
if not os.path.isfile(infile):
|
if not os.path.isfile(infile):
|
||||||
raise DrmException('Input File Not Found')
|
raise DrmException('Input File Not Found')
|
||||||
book = MobiBook(infile)
|
book = MobiBook(infile)
|
||||||
return book.processBook(pidlist)
|
book.processBook(pidlist)
|
||||||
|
return book.mobi_data
|
||||||
|
|
||||||
|
|
||||||
def main(argv=sys.argv):
|
def main(argv=sys.argv):
|
||||||
print ('MobiDeDrm v%(__version__)s. '
|
print ('MobiDeDrm v%(__version__)s. '
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
|
|
||||||
|
|
||||||
Welcome to the tools!
|
Welcome to the tools!
|
||||||
|
|
||||||
The set includes tools to remove DRM from eReader PDB books, Barnes and Noble ePubs, Adobe ePubs, Adobe PDFs, and Kindle/Mobi ebooks (including Topaz).
|
The set includes tools to remove DRM from eReader PDB books, Barnes and Noble ePubs, Adobe ePubs, Adobe PDFs, and Kindle/Mobi ebooks (including Topaz).
|
||||||
|
@ -10,7 +8,7 @@ This ReadMe_First.txt is meant to give users a quick overview of what is availab
|
||||||
|
|
||||||
Calibre Users (Mac OS X, Linux, Windows)
|
Calibre Users (Mac OS X, Linux, Windows)
|
||||||
-------------
|
-------------
|
||||||
If you are a calibre user, the quickest and easiest way to remove DRM form your ebooks is to open the Calibre_Plugins folder and install each of the plugins following the instructions and configuration directions provided in each plugins README file.
|
If you are a calibre user, the quickest and easiest way to remove DRM from your ebooks is to open the Calibre_Plugins folder and install each of the plugins following the instructions and configuration directions provided in each plugins README file.
|
||||||
|
|
||||||
Once installed and configured, you can simply import a DRM book into Calibre and end up with the DeDRM version in the Calibre database.
|
Once installed and configured, you can simply import a DRM book into Calibre and end up with the DeDRM version in the Calibre database.
|
||||||
|
|
||||||
|
@ -20,7 +18,7 @@ These plugins work for Windows, Mac OS X, and Linux
|
||||||
|
|
||||||
Mac OS X Users (Mac OS X 10.5 and 10.6)
|
Mac OS X Users (Mac OS X 10.5 and 10.6)
|
||||||
--------------
|
--------------
|
||||||
Drag the DeDRM X.X.app droplet to your Desktop. Double-click on it once and it will guide you through collecting the data it needs to remove the DRM.
|
From the DeDRM_for_Mac_and_Win folder, drag the DeDRM_X.X.app.zip droplet to your Desktop. Double-click on it once to unzip it to create the DeDRM X.X.app droplet. Double-click on the droplet once and it will guide you through collecting the data it needs to remove the DRM.
|
||||||
|
|
||||||
To use it simply drag a book onto the droplet and it will process the book. This tools supports dragging and dropping of folders of ebooks as well.
|
To use it simply drag a book onto the droplet and it will process the book. This tools supports dragging and dropping of folders of ebooks as well.
|
||||||
|
|
||||||
|
@ -28,25 +26,27 @@ To use it simply drag a book onto the droplet and it will process the book. Thi
|
||||||
|
|
||||||
Windows Users (Xp through Windows 7)
|
Windows Users (Xp through Windows 7)
|
||||||
--------------
|
--------------
|
||||||
Fully extract the DeDRM_WinApp_vX.X.zip. Drag the resulting DeDRM_WinApp_vx.x folder to someplace out of the way on your machine. Open the folder and make a short-cut to DeDRM_Drop_Target and move that short-cut to your Desktop. Double-click on the short-cut and DeDRM will launch it will guide you through collecting the data it needs to remove the DRM. This progeam requires that Python and PyCrypto be properly installed. See below for details.
|
From the DeDRM_for_Mac_and_Win folder, fully extract the DeDRM_WinApp_vX.X.zip. Drag the resulting DeDRM_WinApp_vx.x folder to someplace out of the way on your machine. Open the folder and make a short-cut from the DeDRM_Drop_Target onto your Desktop. Double-click on the short-cut and DeDRM will launch it will guide you through collecting the data it needs to remove the DRM.
|
||||||
|
|
||||||
To use it simply drag ebooks or folders onto the DeDRM_Drop_Target short-cut, and it will remove process the ebooks.
|
***This program requires that Python and PyCrypto be properly installed***. See below for details on which versions are best.
|
||||||
|
|
||||||
|
To use it simply drag ebooks or folders onto the DeDRM_Drop_Target short-cut, and it will process the ebooks.
|
||||||
|
|
||||||
|
|
||||||
Linux Users
|
Linux Users
|
||||||
-----------
|
-----------
|
||||||
A version of the DeDRM drag and drop tool is coming for Linux. In the meantime, you should have no problems runnign the gui tools (or their command line equivalents) described next.
|
Since the state of the Linux Desktop is so jumbled and sad with so many different ways to set it up and different configuration files that depend on your version of Linux, making a DeDRM drag and drop tool for multiple versions of Linux is simply an exercise in futility. That said, you should have no problems running the gui tools (or their command line equivalents) described next.
|
||||||
|
|
||||||
|
|
||||||
Not a Calibre or a DeDRM User?
|
Not a Calibre or a DeDRM User?
|
||||||
------------------------------
|
------------------------------
|
||||||
There are a number of python based tools that have graphical user interfaces to make them easy to use. To use any of these tools, you need to have Python 2.5, 2.6, or 2.7 for 32 bits installed on your machine as well as a matching PyCrypto or OpenSSL for some tools.
|
There are a number of python based tools that have graphical user interfaces to make them easy to use. To use any of these tools, you need to have Python 2.5, 2.6, or 2.7 for 32 bits installed on your machine as well as a matching PyCrypto or OpenSSL for some tools.
|
||||||
|
|
||||||
On Mac OS X (10.5 and 10.6) and Linux (recent versions), your systems already have the proper Python and OpenSSL installed. So nothing need be done, you can already run these tools by double-clicking on the .pyw python scripts.
|
On Mac OS X (10.5 and 10.6) and Linux (recent versions), your systems already have the proper Python and OpenSSL installed. So nothing need be done, you can already run these tools by double-clicking on the .pyw python scripts.
|
||||||
|
|
||||||
Users of Mac OS X 10.3 and 10.4, need to download and install the "32-bit Mac Installer disk Image (2.7.X) for OS X 10.3 and later from http://www.python.org/download/releases/2.7.1/
|
Users of Mac OS X 10.3 and 10.4, need to download and install the "32-bit Mac Installer disk Image (2.7.X) for OS X 10.3 and later from http://www.python.org/download/releases/2.7.1/
|
||||||
|
|
||||||
On Windows, you need to install a 32 bit version of Python (even on Windows 64) plus a matching 32 bit version of PyCrypto *OR* OpenSSL. See the end of this document for details.
|
On Windows, you need to install a 32 bit version of Python (even on Windows 64) plus a matching 32 bit version of PyCrypto *OR* OpenSSL. We ***strongly*** recommend teh free ActiveState's Active Python version. See the end of this document for details.
|
||||||
|
|
||||||
The scripts are organized by type of ebook you need to remove the DRM from. Choose from among:
|
The scripts are organized by type of ebook you need to remove the DRM from. Choose from among:
|
||||||
|
|
||||||
|
@ -54,15 +54,16 @@ The scripts are organized by type of ebook you need to remove the DRM from. Cho
|
||||||
"Adobe_PDF_Tools"
|
"Adobe_PDF_Tools"
|
||||||
"Barnes_and_Noble_ePub_Tools"
|
"Barnes_and_Noble_ePub_Tools"
|
||||||
"eReader_PDB_Tools"
|
"eReader_PDB_Tools"
|
||||||
"KindleBooks_Tools"
|
"KindleBooks_Tools"
|
||||||
|
"Kindle_for_Android_Patch"
|
||||||
|
|
||||||
by simply opening that folder.
|
by simply opening that folder.
|
||||||
|
|
||||||
In the "KindleBooks_Tools" folder the primary tool is in the "KindleBooks" folder.
|
In the "KindleBooks_Tools" folder the primary tool is in the "KindleBooks" folder.
|
||||||
|
|
||||||
If you are a Windows user, or a Linux platform using Wine, or Mac OS X or have trouble running the KindleBooks tools, there are two other tools provided. These are called "Kindle_4_Mac_Unswindle" and "Kindle_4_PC_Unswindle".
|
If you are a Windows user, or a Linux platform using Wine, or Mac OS X or have trouble running the KindleBooks tools, there are two other tools provided. These are called "Kindle_4_Mac_Unswindle" and "Kindle_4_PC_Unswindle".
|
||||||
|
|
||||||
Look for a README inside of the relevant folder to get you started.
|
Look for a README inside of the relevant folder to get you started.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -70,7 +71,7 @@ Additional Tools
|
||||||
----------------------
|
----------------------
|
||||||
Some additional tools are also provided in the "Mobi_Additional_Tools" folder. There are tools for working with "Kindle for iPhone/iPod_Touch/iPad", finding Topaz ebooks, unpacking Mobi ebooks (without DRM) to get to the Mobi markup language inside, and etc.
|
Some additional tools are also provided in the "Mobi_Additional_Tools" folder. There are tools for working with "Kindle for iPhone/iPod_Touch/iPad", finding Topaz ebooks, unpacking Mobi ebooks (without DRM) to get to the Mobi markup language inside, and etc.
|
||||||
|
|
||||||
There is also an "ePub_Fixer" folder that can be used to fix broken DRM epubs that sometimes provided by Adobe and Barnes and Noble that actually violate the zip standard.
|
There is also an "ePub_Fixer" folder that can be used to fix broken DRM epubs that sometimes re provided by Adobe and Barnes and Noble that actually violate the zip/epub standard.
|
||||||
|
|
||||||
Check out their readmes for more info.
|
Check out their readmes for more info.
|
||||||
|
|
||||||
|
@ -78,10 +79,12 @@ Check out their readmes for more info.
|
||||||
|
|
||||||
Windows and Python Tools
|
Windows and Python Tools
|
||||||
------------------------
|
------------------------
|
||||||
We strongly recommend ActiveState's Active Python 2.6 or 2.7 Community Edition for Windows (x86) 32 bits. This can be downloaded for free from:
|
We **strongly** recommend ActiveState's Active Python 2.7 Community Edition for Windows (x86) 32 bits. This can be downloaded for free from:
|
||||||
|
|
||||||
http://www.activestate.com/activepython/downloads
|
http://www.activestate.com/activepython/downloads
|
||||||
|
|
||||||
|
We do **NOT** recommend the version of Python from python.org.
|
||||||
|
The version from python.org is not as complete as most normal Python installations on Linux and even Mac OS X. It is missing various Windows specific libraries, does not install the default Tk Widget kit (for guis) unless you select it as an option in the installer, and does not properly update the system PATH environment variable. Therefore using the default python.org build on Windows is simply an exercise in frustration for most Windows users.
|
||||||
|
|
||||||
In addition, Windows Users need one of PyCrypto OR OpenSSL.
|
In addition, Windows Users need one of PyCrypto OR OpenSSL.
|
||||||
|
|
||||||
|
@ -102,7 +105,7 @@ For PyCrypto:
|
||||||
|
|
||||||
http://www.voidspace.org.uk/python/modules.shtml
|
http://www.voidspace.org.uk/python/modules.shtml
|
||||||
|
|
||||||
Please get the latest PyCrypto meant for Windows 32 bit that matches the version of Python you installed (2.7, or 2.6)
|
Please get the latest PyCrypto meant for Windows 32 bit that matches the version of Python you installed (2.7)
|
||||||
|
|
||||||
Once Windows users have installed Python 2.X for 32 bits, and the matching OpenSSL OR PyCrypto pieces, they too are ready to run the scripts.
|
Once Windows users have installed Python 2.X for 32 bits, and the matching OpenSSL OR PyCrypto pieces, they too are ready to run the scripts.
|
||||||
|
|
||||||
|
|
|
@ -58,8 +58,9 @@
|
||||||
# 0.17 - added support for pycrypto's DES as well
|
# 0.17 - added support for pycrypto's DES as well
|
||||||
# 0.18 - on Windows try PyCrypto first and OpenSSL next
|
# 0.18 - on Windows try PyCrypto first and OpenSSL next
|
||||||
# 0.19 - Modify the interface to allow use of import
|
# 0.19 - Modify the interface to allow use of import
|
||||||
|
# 0.20 - modify to allow use inside new interface for calibre plugins
|
||||||
|
|
||||||
__version__='0.19'
|
__version__='0.20'
|
||||||
|
|
||||||
class Unbuffered:
|
class Unbuffered:
|
||||||
def __init__(self, stream):
|
def __init__(self, stream):
|
||||||
|
@ -71,32 +72,50 @@ class Unbuffered:
|
||||||
return getattr(self.stream, attr)
|
return getattr(self.stream, attr)
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
sys.stdout=Unbuffered(sys.stdout)
|
|
||||||
|
|
||||||
import struct, binascii, getopt, zlib, os, os.path, urllib, tempfile
|
import struct, binascii, getopt, zlib, os, os.path, urllib, tempfile
|
||||||
|
|
||||||
|
if 'calibre' in sys.modules:
|
||||||
|
inCalibre = True
|
||||||
|
else:
|
||||||
|
inCalibre = False
|
||||||
|
|
||||||
Des = None
|
Des = None
|
||||||
if sys.platform.startswith('win'):
|
if sys.platform.startswith('win'):
|
||||||
# first try with pycrypto
|
# first try with pycrypto
|
||||||
import pycrypto_des
|
if inCalibre:
|
||||||
|
from calibre_plugins.erdrpdb2pml import pycrypto_des
|
||||||
|
else:
|
||||||
|
import pycrypto_des
|
||||||
Des = pycrypto_des.load_pycrypto()
|
Des = pycrypto_des.load_pycrypto()
|
||||||
if Des == None:
|
if Des == None:
|
||||||
# they try with openssl
|
# they try with openssl
|
||||||
import openssl_des
|
if inCalibre:
|
||||||
|
from calibre_plugins.erdrpdb2pml import openssl_des
|
||||||
|
else:
|
||||||
|
import openssl_des
|
||||||
Des = openssl_des.load_libcrypto()
|
Des = openssl_des.load_libcrypto()
|
||||||
else:
|
else:
|
||||||
# first try with openssl
|
# first try with openssl
|
||||||
import openssl_des
|
if inCalibre:
|
||||||
|
from calibre_plugins.erdrpdb2pml import openssl_des
|
||||||
|
else:
|
||||||
|
import openssl_des
|
||||||
Des = openssl_des.load_libcrypto()
|
Des = openssl_des.load_libcrypto()
|
||||||
if Des == None:
|
if Des == None:
|
||||||
# then try with pycrypto
|
# then try with pycrypto
|
||||||
import pycrypto_des
|
if inCalibre:
|
||||||
|
from calibre_plugins.erdrpdb2pml import pycrypto_des
|
||||||
|
else:
|
||||||
|
import pycrypto_des
|
||||||
Des = pycrypto_des.load_pycrypto()
|
Des = pycrypto_des.load_pycrypto()
|
||||||
|
|
||||||
# if that did not work then use pure python implementation
|
# if that did not work then use pure python implementation
|
||||||
# of DES and try to speed it up with Psycho
|
# of DES and try to speed it up with Psycho
|
||||||
if Des == None:
|
if Des == None:
|
||||||
import python_des
|
if inCalibre:
|
||||||
|
from calibre_plugins.erdrpdb2pml import python_des
|
||||||
|
else:
|
||||||
|
import python_des
|
||||||
Des = python_des.Des
|
Des = python_des.Des
|
||||||
# Import Psyco if available
|
# Import Psyco if available
|
||||||
try:
|
try:
|
||||||
|
@ -480,5 +499,6 @@ def main(argv=None):
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
sys.stdout=Unbuffered(sys.stdout)
|
||||||
sys.exit(main())
|
sys.exit(main())
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue