diff --git a/Adobe_PDF_Tools/ineptpdf.pyw b/Adobe_PDF_Tools/ineptpdf.pyw index ccdd9e4..c9a419b 100644 --- a/Adobe_PDF_Tools/ineptpdf.pyw +++ b/Adobe_PDF_Tools/ineptpdf.pyw @@ -1,5 +1,5 @@ #! /usr/bin/env python -# ineptpdf.pyw, version 7.7 +# ineptpdf.pyw, version 7.9 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.7 - On Windows try PyCrypto first and OpenSSL next # 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. @@ -156,6 +157,7 @@ def _load_crypto_libcrypto(): return out.raw class AES(object): + MODE_CBC = 0 @classmethod def new(cls, userkey, mode, iv): self = AES() @@ -1531,16 +1533,30 @@ class PDFDocument(object): bookkey = bookkey[index:] ebx_V = int_value(param.get('V', 4)) ebx_type = int_value(param.get('EBX_ENCRYPTIONTYPE', 6)) - # added because of the booktype / decryption book session key error - if ebx_V == 3: - V = 3 - elif ebx_V < 4 or ebx_type < 6: - V = ord(bookkey[0]) - bookkey = bookkey[1:] + # added because of improper booktype / decryption book session key errors + if length > 0: + if len(bookkey) == length: + if ebx_V == 3: + V = 3 + 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: - V = 2 - if length and len(bookkey) != length: - raise ADEPTError('error decrypting book session key') + # proper length unknown try with whatever you have + 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]) + if ebx_V == 3: + V = 3 + else: + V = 2 self.decrypt_key = bookkey self.genkey = self.genkey_v3 if V == 3 else self.genkey_v2 self.decipher = self.decrypt_rc4 diff --git a/Calibre_Plugins/K4MobiDeDRM_plugin/__init__.py b/Calibre_Plugins/K4MobiDeDRM_plugin/__init__.py new file mode 100644 index 0000000..971c02b --- /dev/null +++ b/Calibre_Plugins/K4MobiDeDRM_plugin/__init__.py @@ -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.' diff --git a/Calibre_Plugins/K4MobiDeDRM_plugin/genbook.py b/Calibre_Plugins/K4MobiDeDRM_plugin/genbook.py index 11258d1..f779cee 100644 --- a/Calibre_Plugins/K4MobiDeDRM_plugin/genbook.py +++ b/Calibre_Plugins/K4MobiDeDRM_plugin/genbook.py @@ -21,10 +21,21 @@ from struct import unpack # local support routines -import convert2xml -import flatxml2html -import flatxml2svg -import stylexml2css +if 'calibre' in sys.modules: + inCalibre = True +else: + 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 @@ -504,7 +515,7 @@ def generateBook(bookDir, raw, fixedimage): opfstr += ' \n' opfstr += '\n' opfstr += ' \n' - opfstr += ' \n' + opfstr += ' \n' # adding image files to manifest filenames = os.listdir(imgDir) filenames = sorted(filenames) diff --git a/Calibre_Plugins/K4MobiDeDRM_plugin/k4mobidedrm_orig.py b/Calibre_Plugins/K4MobiDeDRM_plugin/k4mobidedrm_orig.py new file mode 100644 index 0000000..14556db --- /dev/null +++ b/Calibre_Plugins/K4MobiDeDRM_plugin/k4mobidedrm_orig.py @@ -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 ] [-p ] [-s ] " % 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()) + diff --git a/Calibre_Plugins/K4MobiDeDRM_plugin/kgenpids.py b/Calibre_Plugins/K4MobiDeDRM_plugin/kgenpids.py index c10f105..c9d8944 100644 --- a/Calibre_Plugins/K4MobiDeDRM_plugin/kgenpids.py +++ b/Calibre_Plugins/K4MobiDeDRM_plugin/kgenpids.py @@ -11,16 +11,28 @@ from struct import pack, unpack, unpack_from class DrmException(Exception): pass -global kindleDatabase global charMap1 -global charMap2 global charMap3 global charMap4 -if sys.platform.startswith('win'): - from k4pcutils import getKindleInfoFiles, parseKindleInfo, CryptUnprotectData, GetUserName, GetVolumeSerialNumber, charMap2 -if sys.platform.startswith('darwin'): - from k4mutils import getKindleInfoFiles, parseKindleInfo, CryptUnprotectData, GetUserName, GetVolumeSerialNumber, charMap2 +if 'calibre' in sys.modules: + inCalibre = True +else: + 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" charMap3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" @@ -66,50 +78,7 @@ def decode(data,map): value = (((high * len(map)) ^ 0x80) & 0xFF) + low result += pack("B",value) 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 # @@ -222,15 +191,15 @@ def getKindlePid(pidlst, rec209, token, serialnum): return pidlst -# Parse the EXTH header records and parse the Kindleinfo -# file to calculate the book pid. +# parse the Kindleinfo 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): - global kindleDatabase global charMap1 kindleDatabase = None try: - kindleDatabase = parseKindleInfo(kInfoFile) + kindleDatabase = getDBfromFile(kInfoFile) except Exception, message: print(message) kindleDatabase = None @@ -241,10 +210,10 @@ def getK4Pids(pidlst, rec209, token, kInfoFile): try: # Get the Mazama Random number - MazamaRandomNumber = getKindleInfoValueForKey("MazamaRandomNumber") + MazamaRandomNumber = kindleDatabase["MazamaRandomNumber"] # Get the kindle account token - kindleAccountToken = getKindleInfoValueForKey("kindle.account.tokens") + kindleAccountToken = kindleDatabase["kindle.account.tokens"] except KeyError: print "Keys not found in " + kInfoFile return pidlst diff --git a/Calibre_Plugins/K4MobiDeDRM_plugin/plugin-import-name-k4mobidedrm.txt b/Calibre_Plugins/K4MobiDeDRM_plugin/plugin-import-name-k4mobidedrm.txt new file mode 100644 index 0000000..e69de29 diff --git a/Calibre_Plugins/K4MobiDeDRM_plugin/topazextract.py b/Calibre_Plugins/K4MobiDeDRM_plugin/topazextract.py index 59bc5fa..12b92e2 100644 --- a/Calibre_Plugins/K4MobiDeDRM_plugin/topazextract.py +++ b/Calibre_Plugins/K4MobiDeDRM_plugin/topazextract.py @@ -10,7 +10,12 @@ class Unbuffered: return getattr(self.stream, attr) import sys -sys.stdout=Unbuffered(sys.stdout) + +if 'calibre' in sys.modules: + inCalibre = True +else: + inCalibre = False + import os, csv, getopt import zlib, zipfile, tempfile, shutil from struct import pack @@ -18,10 +23,32 @@ from struct import unpack class TpzDRMError(Exception): pass + # local support routines -import kgenpids -import genbook +if inCalibre: + 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 # @@ -110,9 +137,9 @@ def decryptDkeyRecords(data,PID): class TopazBook: - def __init__(self, filename, outdir): + def __init__(self, filename): self.fo = file(filename, 'rb') - self.outdir = outdir + self.outdir = tempfile.mkdtemp() self.bookPayloadOffset = 0 self.bookHeaderRecords = {} self.bookMetadata = {} @@ -317,21 +344,33 @@ class TopazBook: file(outputFile, 'wb').write(record) 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): - 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) + def getSVGZip(self, zipname): + svgzip = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False) + svgzip.write(os.path.join(self.outdir,'index_svg.xhtml'),'index_svg.xhtml') + zipUpDir(svgzip, self.outdir, 'svg') + zipUpDir(svgzip, self.outdir, 'img') + svgzip.close() + 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): print "Removes DRM protection from Topaz ebooks and extract the contents" @@ -383,58 +422,46 @@ def main(argv=sys.argv): return 1 bookname = os.path.splitext(os.path.basename(infile))[0] - tempdir = tempfile.mkdtemp() - tb = TopazBook(infile, tempdir) + tb = TopazBook(infile) title = tb.getBookTitle() print "Processing Book: ", title keysRecord, keysRecordRecord = tb.getPIDMetaInfo() pidlst = kgenpids.getPidList(keysRecord, keysRecordRecord, k4, pids, serials, kInfoFiles) try: + print "Decrypting Book" 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: print str(e) - 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) + tb.cleanup() return 1 - print " Creating HTML ZIP Archive" - zipname = os.path.join(outdir, bookname + '_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, 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) + except Exception, e: + print str(e) + tb.cleanup + return 1 return 0 if __name__ == '__main__': + sys.stdout=Unbuffered(sys.stdout) sys.exit(main()) diff --git a/Calibre_Plugins/README-K4MobiDeDRM-plugin.txt b/Calibre_Plugins/README-K4MobiDeDRM-plugin.txt index d0909f5..1086890 100644 --- a/Calibre_Plugins/README-K4MobiDeDRM-plugin.txt +++ b/Calibre_Plugins/README-K4MobiDeDRM-plugin.txt @@ -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. 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: -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: diff --git a/Calibre_Plugins/eReaderPDB2PML_plugin.zip b/Calibre_Plugins/eReaderPDB2PML_plugin.zip index c852068..4fe2084 100644 Binary files a/Calibre_Plugins/eReaderPDB2PML_plugin.zip and b/Calibre_Plugins/eReaderPDB2PML_plugin.zip differ diff --git a/Calibre_Plugins/eReaderPDB2PML_plugin/eReaderPDB2PML_plugin.py b/Calibre_Plugins/eReaderPDB2PML_plugin/__init__.py similarity index 94% rename from Calibre_Plugins/eReaderPDB2PML_plugin/eReaderPDB2PML_plugin.py rename to Calibre_Plugins/eReaderPDB2PML_plugin/__init__.py index 5585cf5..3fb750c 100644 --- a/Calibre_Plugins/eReaderPDB2PML_plugin/eReaderPDB2PML_plugin.py +++ b/Calibre_Plugins/eReaderPDB2PML_plugin/__init__.py @@ -31,10 +31,15 @@ # 0.0.1 - Initial release # 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.4 - minor typos fixed +# 0.0.5 - updated to the new calibre plugin interface import sys, os 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): 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.' supported_platforms = ['linux', 'osx', 'windows'] # Platforms this plugin will run on 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 on_import = True # Run this plugin during the import + minimum_calibre_version = (0, 7, 55) def run(self, path_to_ebook): - from calibre.ptempfile import PersistentTemporaryDirectory - from calibre.constants import iswindows, isosx global bookname, erdr2pml - import erdr2pml infile = path_to_ebook bookname = os.path.splitext(os.path.basename(infile))[0] diff --git a/Calibre_Plugins/eReaderPDB2PML_plugin/erdr2pml.py b/Calibre_Plugins/eReaderPDB2PML_plugin/erdr2pml.py index 6df9e13..0e59d8d 100644 --- a/Calibre_Plugins/eReaderPDB2PML_plugin/erdr2pml.py +++ b/Calibre_Plugins/eReaderPDB2PML_plugin/erdr2pml.py @@ -58,8 +58,9 @@ # 0.17 - added support for pycrypto's DES as well # 0.18 - on Windows try PyCrypto first and OpenSSL next # 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: def __init__(self, stream): @@ -71,32 +72,50 @@ class Unbuffered: return getattr(self.stream, attr) import sys -sys.stdout=Unbuffered(sys.stdout) - import struct, binascii, getopt, zlib, os, os.path, urllib, tempfile +if 'calibre' in sys.modules: + inCalibre = True +else: + inCalibre = False + Des = None if sys.platform.startswith('win'): # 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() if Des == None: # 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() else: # 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() if Des == None: # 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() # if that did not work then use pure python implementation # of DES and try to speed it up with Psycho if Des == None: - import python_des + if inCalibre: + from calibre_plugins.erdrpdb2pml import python_des + else: + import python_des Des = python_des.Des # Import Psyco if available try: @@ -480,5 +499,6 @@ def main(argv=None): if __name__ == "__main__": + sys.stdout=Unbuffered(sys.stdout) sys.exit(main()) diff --git a/Calibre_Plugins/eReaderPDB2PML_plugin/plugin-import-name-erdrpdb2pml.txt b/Calibre_Plugins/eReaderPDB2PML_plugin/plugin-import-name-erdrpdb2pml.txt new file mode 100644 index 0000000..e69de29 diff --git a/Calibre_Plugins/ignobleepub_plugin.zip b/Calibre_Plugins/ignobleepub_plugin.zip index 3e0bfe1..c03ca38 100644 Binary files a/Calibre_Plugins/ignobleepub_plugin.zip and b/Calibre_Plugins/ignobleepub_plugin.zip differ diff --git a/Calibre_Plugins/ignobleepub_plugin/ignobleepub_plugin.py b/Calibre_Plugins/ignobleepub_plugin/ignobleepub_plugin.py index 04ffc33..b63c949 100644 --- a/Calibre_Plugins/ignobleepub_plugin/ignobleepub_plugin.py +++ b/Calibre_Plugins/ignobleepub_plugin/ignobleepub_plugin.py @@ -4,7 +4,7 @@ # Released under the terms of the GNU General Public Licence, version 3 or # later. # -# 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. # 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.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.6 - update ot the new calibre plugin interface """ Decrypt Barnes & Noble ADEPT encrypted EPUB books. @@ -266,6 +267,7 @@ def plugin_main(userkey, inpath, outpath): return 0 from calibre.customize import FileTypePlugin +from calibre.constants import iswindows, isosx class IgnobleDeDRM(FileTypePlugin): name = 'Ignoble Epub DeDRM' @@ -273,8 +275,8 @@ class IgnobleDeDRM(FileTypePlugin): Credit given to I <3 Cabbages for the original stand-alone scripts.' supported_platforms = ['linux', 'osx', 'windows'] author = 'DiapDealer' - version = (0, 1, 5) - minimum_calibre_version = (0, 6, 44) # Compiled python libraries cannot be imported in earlier versions. + version = (0, 1, 6) + minimum_calibre_version = (0, 7, 55) # Compiled python libraries cannot be imported in earlier versions. file_types = set(['epub']) on_import = True @@ -282,10 +284,6 @@ class IgnobleDeDRM(FileTypePlugin): global AES 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() if AES == None or AES2 == None: @@ -341,7 +339,7 @@ class IgnobleDeDRM(FileTypePlugin): for userkey in userkeys: # Create a TemporaryPersistent file to work with. # Check original epub archive for zip errors. - import zipfix + from calibre_plugins.ignobleepub import zipfix inf = self.temporary_file('.epub') try: fr = zipfix.fixZip(path_to_ebook, inf.name) diff --git a/Calibre_Plugins/ineptepub_plugin.zip b/Calibre_Plugins/ineptepub_plugin.zip index 2ce5bce..b62a625 100644 Binary files a/Calibre_Plugins/ineptepub_plugin.zip and b/Calibre_Plugins/ineptepub_plugin.zip differ diff --git a/Calibre_Plugins/ineptepub_plugin/ineptepub_plugin.py b/Calibre_Plugins/ineptepub_plugin/__init__.py similarity index 97% rename from Calibre_Plugins/ineptepub_plugin/ineptepub_plugin.py rename to Calibre_Plugins/ineptepub_plugin/__init__.py index d0541b5..264b9ee 100644 --- a/Calibre_Plugins/ineptepub_plugin/ineptepub_plugin.py +++ b/Calibre_Plugins/ineptepub_plugin/__init__.py @@ -4,7 +4,7 @@ # Released under the terms of the GNU General Public Licence, version 3 or # later. # -# 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. # 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.5 - update zipfix to handle out of position mimetypes # 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. @@ -365,6 +366,7 @@ def plugin_main(userkey, inpath, outpath): return 0 from calibre.customize import FileTypePlugin +from calibre.constants import iswindows, isosx class IneptDeDRM(FileTypePlugin): name = 'Inept Epub DeDRM' @@ -372,8 +374,8 @@ class IneptDeDRM(FileTypePlugin): Credit given to I <3 Cabbages for the original stand-alone scripts.' supported_platforms = ['linux', 'osx', 'windows'] author = 'DiapDealer' - version = (0, 1, 6) - minimum_calibre_version = (0, 6, 44) # Compiled python libraries cannot be imported in earlier versions. + version = (0, 1, 7) + minimum_calibre_version = (0, 7, 55) # Compiled python libraries cannot be imported in earlier versions. file_types = set(['epub']) on_import = True priority = 100 @@ -382,10 +384,6 @@ class IneptDeDRM(FileTypePlugin): global AES 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() if AES == None or RSA == None: @@ -418,7 +416,7 @@ class IneptDeDRM(FileTypePlugin): # Calibre's configuration directory for future use. if iswindows or isosx: # 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: keydata = retrieve_key() userkeys.append(keydata) @@ -439,7 +437,7 @@ class IneptDeDRM(FileTypePlugin): for userkey in userkeys: # Create a TemporaryPersistent file to work with. # Check original epub archive for zip errors. - import zipfix + from calibre_plugins.ineptepub import zipfix inf = self.temporary_file('.epub') try: fr = zipfix.fixZip(path_to_ebook, inf.name) diff --git a/Calibre_Plugins/ineptepub_plugin/plugin-import-name-ineptepub.txt b/Calibre_Plugins/ineptepub_plugin/plugin-import-name-ineptepub.txt new file mode 100644 index 0000000..e69de29 diff --git a/Calibre_Plugins/ineptpdf_plugin.zip b/Calibre_Plugins/ineptpdf_plugin.zip index 3174938..83dd0ae 100644 Binary files a/Calibre_Plugins/ineptpdf_plugin.zip and b/Calibre_Plugins/ineptpdf_plugin.zip differ diff --git a/Calibre_Plugins/ineptpdf_plugin/ineptpdf_plugin.py b/Calibre_Plugins/ineptpdf_plugin/__init__.py similarity index 99% rename from Calibre_Plugins/ineptpdf_plugin/ineptpdf_plugin.py rename to Calibre_Plugins/ineptpdf_plugin/__init__.py index 5e3caf3..e1b6041 100644 --- a/Calibre_Plugins/ineptpdf_plugin/ineptpdf_plugin.py +++ b/Calibre_Plugins/ineptpdf_plugin/__init__.py @@ -1,6 +1,6 @@ #! /usr/bin/env python -# ineptpdf_plugin.py +# ineptpdf plugin __init__.py # Released under the terms of the GNU General Public Licence, version 3 or # later. @@ -11,7 +11,7 @@ # be able to read OUR books on whatever device we want and to keep # 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. # 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.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.4 - update to the new calibre plugin interface """ Decrypts Adobe ADEPT-encrypted PDF files. @@ -174,6 +175,7 @@ def _load_crypto_libcrypto(): return out.raw class AES(object): + MODE_CBC = 0 @classmethod def new(cls, userkey, mode, iv): self = AES() @@ -2126,6 +2128,7 @@ def plugin_main(keypath, inpath, outpath): from calibre.customize import FileTypePlugin +from calibre.constants import iswindows, isosx class IneptPDFDeDRM(FileTypePlugin): name = 'Inept PDF DeDRM' @@ -2133,17 +2136,14 @@ class IneptPDFDeDRM(FileTypePlugin): Credit given to I <3 Cabbages for the original stand-alone scripts.' supported_platforms = ['linux', 'osx', 'windows'] author = 'DiapDealer' - version = (0, 1, 3) - minimum_calibre_version = (0, 6, 44) # Compiled python libraries cannot be imported in earlier versions. + version = (0, 1, 4) + minimum_calibre_version = (0, 7, 55) # for the new plugin interface file_types = set(['pdf']) on_import = True def run(self, path_to_ebook): 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() @@ -2177,7 +2177,7 @@ class IneptPDFDeDRM(FileTypePlugin): # Calibre's configuration directory for future use. if iswindows or isosx: # ADE key retrieval script. - from ade_key import retrieve_key + from calibre_plugins.ineptpdf.ade_key import retrieve_key try: keydata = retrieve_key() userkeys.append(keydata) diff --git a/Calibre_Plugins/ineptpdf_plugin/plugin-import-name-ineptpdf.txt b/Calibre_Plugins/ineptpdf_plugin/plugin-import-name-ineptpdf.txt new file mode 100644 index 0000000..e69de29 diff --git a/Calibre_Plugins/k4mobidedrm_plugin.zip b/Calibre_Plugins/k4mobidedrm_plugin.zip index 295d06f..00782f0 100644 Binary files a/Calibre_Plugins/k4mobidedrm_plugin.zip and b/Calibre_Plugins/k4mobidedrm_plugin.zip differ diff --git a/Calibre_Plugins/k4mobidedrm_plugin/k4mobidedrm_plugin.py b/Calibre_Plugins/k4mobidedrm_plugin/k4mobidedrm_plugin.py deleted file mode 100644 index 64a998a..0000000 --- a/Calibre_Plugins/k4mobidedrm_plugin/k4mobidedrm_plugin.py +++ /dev/null @@ -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 ] [-p ] [-s ] " % 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.' \ No newline at end of file diff --git a/Calibre_Plugins/k4mobidedrm_plugin/k4mutils.py b/Calibre_Plugins/k4mobidedrm_plugin/k4mutils.py index 539723d..534c389 100644 --- a/Calibre_Plugins/k4mobidedrm_plugin/k4mutils.py +++ b/Calibre_Plugins/k4mobidedrm_plugin/k4mutils.py @@ -1,10 +1,12 @@ # 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 @@ -66,9 +68,8 @@ def _load_crypto_libcrypto(): raise DrmException('AES decryption failed') return out.raw - def keyivgen(self, passwd): - salt = '16743' - saltlen = 5 + def keyivgen(self, passwd, salt): + saltlen = len(salt) passlen = len(passwd) iter = 0x3e8 keylen = 80 @@ -91,12 +92,78 @@ 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" -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 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 diff --git a/Calibre_Plugins/k4mobidedrm_plugin/k4pcutils.py b/Calibre_Plugins/k4mobidedrm_plugin/k4pcutils.py index d460a70..690033b 100644 --- a/Calibre_Plugins/k4mobidedrm_plugin/k4pcutils.py +++ b/Calibre_Plugins/k4mobidedrm_plugin/k4pcutils.py @@ -1,8 +1,10 @@ +#!/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, \ @@ -10,25 +12,86 @@ from ctypes import windll, c_char_p, c_wchar_p, c_uint, POINTER, byref, \ import _winreg as winreg -import traceback - MAX_PATH = 255 kernel32 = windll.kernel32 advapi32 = windll.advapi32 crypt32 = windll.crypt32 +import traceback -# Various character maps used to decrypt books. Probably supposed to act as obfuscation -charMap1 = "n5Pr6St7Uv8Wx9YzAb0Cd1Ef2Gh3Jk4M" +# 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 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 + + diff --git a/Calibre_Plugins/k4mobidedrm_plugin/mobidedrm.py b/Calibre_Plugins/k4mobidedrm_plugin/mobidedrm.py index 7aef175..41bb12d 100644 --- a/Calibre_Plugins/k4mobidedrm_plugin/mobidedrm.py +++ b/Calibre_Plugins/k4mobidedrm_plugin/mobidedrm.py @@ -51,8 +51,9 @@ # 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! +# 0.30 - Modified interface slightly to work better with new calibre plugin style -__version__ = '0.29' +__version__ = '0.30' import sys @@ -163,6 +164,7 @@ class MobiBook: def __init__(self, infile): # initial sanity check on file self.data_file = file(infile, 'rb').read() + self.mobi_data = '' 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") @@ -301,13 +303,17 @@ class MobiBook: break return [found_key,pid] + def getMobiFile(self, outpath): + file(outpath,'wb').write(self.mobi_data) + 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 + self.mobi_data = self.data_file + return if crypto_type != 2 and crypto_type != 1: raise DrmException("Cannot decode unknown Mobipocket encryption type %d" % crypto_type) @@ -353,33 +359,35 @@ class MobiBook: # decrypt sections 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): 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]) + self.mobi_data += PC1(found_key, data[0:len(data) - extra_size]) if extra_size > 0: - new_data += data[-extra_size:] + self.mobi_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 + self.mobi_data += self.data_file[self.sections[self.records+1][0]:] print "done" - return self.data_file + return def getUnencryptedBook(infile,pid): if not os.path.isfile(infile): raise DrmException('Input File Not Found') book = MobiBook(infile) - return book.processBook([pid]) + book.processBook([pid]) + return book.mobi_data def getUnencryptedBookWithList(infile,pidlist): if not os.path.isfile(infile): raise DrmException('Input File Not Found') book = MobiBook(infile) - return book.processBook(pidlist) + book.processBook(pidlist) + return book.mobi_data + def main(argv=sys.argv): print ('MobiDeDrm v%(__version__)s. ' diff --git a/DeDRM_Macintosh_Application/DeDRM.app.txt b/DeDRM_Macintosh_Application/DeDRM.app.txt index 26aa8fd..856958e 100644 Binary files a/DeDRM_Macintosh_Application/DeDRM.app.txt and b/DeDRM_Macintosh_Application/DeDRM.app.txt differ diff --git a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Info.plist b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Info.plist index c99622a..aecb257 100644 --- a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Info.plist +++ b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Info.plist @@ -24,7 +24,7 @@ CFBundleExecutable droplet CFBundleGetInfoString - DeDRM 2.6, Written 2010–2011 by Apprentice Alf and others. + DeDRM 2.7, Written 2010–2011 by Apprentice Alf and others. CFBundleIconFile droplet CFBundleInfoDictionaryVersion @@ -34,7 +34,7 @@ CFBundlePackageType APPL CFBundleShortVersionString - 2.6 + 2.7 CFBundleSignature dplt LSMinimumSystemVersion diff --git a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/Scripts/main.scpt b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/Scripts/main.scpt index 4d03f32..13962a2 100644 Binary files a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/Scripts/main.scpt and b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/Scripts/main.scpt differ diff --git a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/erdr2pml.py b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/erdr2pml.py index 6df9e13..0e59d8d 100644 --- a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/erdr2pml.py +++ b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/erdr2pml.py @@ -58,8 +58,9 @@ # 0.17 - added support for pycrypto's DES as well # 0.18 - on Windows try PyCrypto first and OpenSSL next # 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: def __init__(self, stream): @@ -71,32 +72,50 @@ class Unbuffered: return getattr(self.stream, attr) import sys -sys.stdout=Unbuffered(sys.stdout) - import struct, binascii, getopt, zlib, os, os.path, urllib, tempfile +if 'calibre' in sys.modules: + inCalibre = True +else: + inCalibre = False + Des = None if sys.platform.startswith('win'): # 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() if Des == None: # 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() else: # 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() if Des == None: # 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() # if that did not work then use pure python implementation # of DES and try to speed it up with Psycho if Des == None: - import python_des + if inCalibre: + from calibre_plugins.erdrpdb2pml import python_des + else: + import python_des Des = python_des.Des # Import Psyco if available try: @@ -480,5 +499,6 @@ def main(argv=None): if __name__ == "__main__": + sys.stdout=Unbuffered(sys.stdout) sys.exit(main()) diff --git a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/genbook.py b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/genbook.py index 11258d1..f779cee 100644 --- a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/genbook.py +++ b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/genbook.py @@ -21,10 +21,21 @@ from struct import unpack # local support routines -import convert2xml -import flatxml2html -import flatxml2svg -import stylexml2css +if 'calibre' in sys.modules: + inCalibre = True +else: + 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 @@ -504,7 +515,7 @@ def generateBook(bookDir, raw, fixedimage): opfstr += ' \n' opfstr += '\n' opfstr += ' \n' - opfstr += ' \n' + opfstr += ' \n' # adding image files to manifest filenames = os.listdir(imgDir) filenames = sorted(filenames) diff --git a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/ineptpdf.py b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/ineptpdf.py index 40e97cd..c9a419b 100644 --- a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/ineptpdf.py +++ b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/ineptpdf.py @@ -157,6 +157,7 @@ def _load_crypto_libcrypto(): return out.raw class AES(object): + MODE_CBC = 0 @classmethod def new(cls, userkey, mode, iv): self = AES() diff --git a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/k4mobidedrm.py b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/k4mobidedrm.py index 64a998a..14556db 100644 --- a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/k4mobidedrm.py +++ b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/k4mobidedrm.py @@ -16,20 +16,8 @@ from __future__ import with_statement # 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' +__version__ = '3.1' class Unbuffered: def __init__(self, stream): @@ -43,11 +31,7 @@ class Unbuffered: 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 @@ -57,19 +41,15 @@ if 'calibre' in sys.modules: 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) +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 @@ -94,10 +74,6 @@ def cleanup_name(name): 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" @@ -113,8 +89,7 @@ def decryptBook(infile, outdir, k4, kInfoFiles, serials, pids): if mobi: mb = mobidedrm.MobiBook(infile) else: - tempdir = tempfile.mkdtemp() - mb = topazextract.TopazBook(infile, tempdir) + mb = topazextract.TopazBook(infile) title = mb.getBookTitle() 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) try: - if mobi: - unlocked_file = mb.processBook(pidlst) - else: - mb.processBook(pidlst) + 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: - 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 + print "Error: " + str(e) + "\nDRM Removal Failed.\n" + return 1 if mobi: - outfile = os.path.join(outdir,outfilename + '_nodrm' + '.mobi') - file(outfile, 'wb').write(unlocked_file) + outfile = os.path.join(outdir, outfilename + '_nodrm' + '.mobi') + mb.getMobiFile(outfile) 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() + # topaz: + print " Creating NoDRM HTMLZ Archive" + zipname = os.path.join(outdir, outfilename + '_nodrm' + '.htmlz') + mb.getHTMLZip(zipname) - 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 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') - myzip3 = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False) - targetdir = os.path.join(tempdir,'xml') - zipUpDir(myzip3, targetdir, '') - zipUpDir(myzip3, tempdir, 'img') - myzip3.close() + mb.getXMLZip(zipname) + + # remove internal temporary directory of Topaz pieces + mb.cleanup() - shutil.rmtree(tempdir, True) return 0 @@ -236,7 +190,6 @@ def main(argv=sys.argv): kInfoFiles = None infile = args[0] outdir = args[1] - return decryptBook(infile, outdir, k4, kInfoFiles, serials, pids) @@ -244,131 +197,3 @@ 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.' \ No newline at end of file diff --git a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/k4mutils.py b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/k4mutils.py index 539723d..534c389 100644 --- a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/k4mutils.py +++ b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/k4mutils.py @@ -1,10 +1,12 @@ # 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 @@ -66,9 +68,8 @@ def _load_crypto_libcrypto(): raise DrmException('AES decryption failed') return out.raw - def keyivgen(self, passwd): - salt = '16743' - saltlen = 5 + def keyivgen(self, passwd, salt): + saltlen = len(salt) passlen = len(passwd) iter = 0x3e8 keylen = 80 @@ -91,12 +92,78 @@ 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" -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 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 diff --git a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/k4pcutils.py b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/k4pcutils.py index d460a70..690033b 100644 --- a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/k4pcutils.py +++ b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/k4pcutils.py @@ -1,8 +1,10 @@ +#!/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, \ @@ -10,25 +12,86 @@ from ctypes import windll, c_char_p, c_wchar_p, c_uint, POINTER, byref, \ import _winreg as winreg -import traceback - MAX_PATH = 255 kernel32 = windll.kernel32 advapi32 = windll.advapi32 crypt32 = windll.crypt32 +import traceback -# Various character maps used to decrypt books. Probably supposed to act as obfuscation -charMap1 = "n5Pr6St7Uv8Wx9YzAb0Cd1Ef2Gh3Jk4M" +# 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 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 + + diff --git a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/kgenpids.py b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/kgenpids.py index c10f105..c9d8944 100644 --- a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/kgenpids.py +++ b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/kgenpids.py @@ -11,16 +11,28 @@ from struct import pack, unpack, unpack_from class DrmException(Exception): pass -global kindleDatabase global charMap1 -global charMap2 global charMap3 global charMap4 -if sys.platform.startswith('win'): - from k4pcutils import getKindleInfoFiles, parseKindleInfo, CryptUnprotectData, GetUserName, GetVolumeSerialNumber, charMap2 -if sys.platform.startswith('darwin'): - from k4mutils import getKindleInfoFiles, parseKindleInfo, CryptUnprotectData, GetUserName, GetVolumeSerialNumber, charMap2 +if 'calibre' in sys.modules: + inCalibre = True +else: + 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" charMap3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" @@ -66,50 +78,7 @@ def decode(data,map): value = (((high * len(map)) ^ 0x80) & 0xFF) + low result += pack("B",value) 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 # @@ -222,15 +191,15 @@ def getKindlePid(pidlst, rec209, token, serialnum): return pidlst -# Parse the EXTH header records and parse the Kindleinfo -# file to calculate the book pid. +# parse the Kindleinfo 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): - global kindleDatabase global charMap1 kindleDatabase = None try: - kindleDatabase = parseKindleInfo(kInfoFile) + kindleDatabase = getDBfromFile(kInfoFile) except Exception, message: print(message) kindleDatabase = None @@ -241,10 +210,10 @@ def getK4Pids(pidlst, rec209, token, kInfoFile): try: # Get the Mazama Random number - MazamaRandomNumber = getKindleInfoValueForKey("MazamaRandomNumber") + MazamaRandomNumber = kindleDatabase["MazamaRandomNumber"] # Get the kindle account token - kindleAccountToken = getKindleInfoValueForKey("kindle.account.tokens") + kindleAccountToken = kindleDatabase["kindle.account.tokens"] except KeyError: print "Keys not found in " + kInfoFile return pidlst diff --git a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/mobidedrm.py b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/mobidedrm.py index 7aef175..41bb12d 100644 --- a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/mobidedrm.py +++ b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/mobidedrm.py @@ -51,8 +51,9 @@ # 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! +# 0.30 - Modified interface slightly to work better with new calibre plugin style -__version__ = '0.29' +__version__ = '0.30' import sys @@ -163,6 +164,7 @@ class MobiBook: def __init__(self, infile): # initial sanity check on file self.data_file = file(infile, 'rb').read() + self.mobi_data = '' 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") @@ -301,13 +303,17 @@ class MobiBook: break return [found_key,pid] + def getMobiFile(self, outpath): + file(outpath,'wb').write(self.mobi_data) + 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 + self.mobi_data = self.data_file + return if crypto_type != 2 and crypto_type != 1: raise DrmException("Cannot decode unknown Mobipocket encryption type %d" % crypto_type) @@ -353,33 +359,35 @@ class MobiBook: # decrypt sections 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): 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]) + self.mobi_data += PC1(found_key, data[0:len(data) - extra_size]) if extra_size > 0: - new_data += data[-extra_size:] + self.mobi_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 + self.mobi_data += self.data_file[self.sections[self.records+1][0]:] print "done" - return self.data_file + return def getUnencryptedBook(infile,pid): if not os.path.isfile(infile): raise DrmException('Input File Not Found') book = MobiBook(infile) - return book.processBook([pid]) + book.processBook([pid]) + return book.mobi_data def getUnencryptedBookWithList(infile,pidlist): if not os.path.isfile(infile): raise DrmException('Input File Not Found') book = MobiBook(infile) - return book.processBook(pidlist) + book.processBook(pidlist) + return book.mobi_data + def main(argv=sys.argv): print ('MobiDeDrm v%(__version__)s. ' diff --git a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/topazextract.py b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/topazextract.py index 59bc5fa..12b92e2 100644 --- a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/topazextract.py +++ b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/topazextract.py @@ -10,7 +10,12 @@ class Unbuffered: return getattr(self.stream, attr) import sys -sys.stdout=Unbuffered(sys.stdout) + +if 'calibre' in sys.modules: + inCalibre = True +else: + inCalibre = False + import os, csv, getopt import zlib, zipfile, tempfile, shutil from struct import pack @@ -18,10 +23,32 @@ from struct import unpack class TpzDRMError(Exception): pass + # local support routines -import kgenpids -import genbook +if inCalibre: + 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 # @@ -110,9 +137,9 @@ def decryptDkeyRecords(data,PID): class TopazBook: - def __init__(self, filename, outdir): + def __init__(self, filename): self.fo = file(filename, 'rb') - self.outdir = outdir + self.outdir = tempfile.mkdtemp() self.bookPayloadOffset = 0 self.bookHeaderRecords = {} self.bookMetadata = {} @@ -317,21 +344,33 @@ class TopazBook: file(outputFile, 'wb').write(record) 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): - 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) + def getSVGZip(self, zipname): + svgzip = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False) + svgzip.write(os.path.join(self.outdir,'index_svg.xhtml'),'index_svg.xhtml') + zipUpDir(svgzip, self.outdir, 'svg') + zipUpDir(svgzip, self.outdir, 'img') + svgzip.close() + 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): print "Removes DRM protection from Topaz ebooks and extract the contents" @@ -383,58 +422,46 @@ def main(argv=sys.argv): return 1 bookname = os.path.splitext(os.path.basename(infile))[0] - tempdir = tempfile.mkdtemp() - tb = TopazBook(infile, tempdir) + tb = TopazBook(infile) title = tb.getBookTitle() print "Processing Book: ", title keysRecord, keysRecordRecord = tb.getPIDMetaInfo() pidlst = kgenpids.getPidList(keysRecord, keysRecordRecord, k4, pids, serials, kInfoFiles) try: + print "Decrypting Book" 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: print str(e) - 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) + tb.cleanup() return 1 - print " Creating HTML ZIP Archive" - zipname = os.path.join(outdir, bookname + '_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, 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) + except Exception, e: + print str(e) + tb.cleanup + return 1 return 0 if __name__ == '__main__': + sys.stdout=Unbuffered(sys.stdout) sys.exit(main()) diff --git a/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/DeDRM_app.pyw b/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/DeDRM_app.pyw index a319631..1b81ade 100644 --- a/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/DeDRM_app.pyw +++ b/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/DeDRM_app.pyw @@ -124,14 +124,17 @@ class PrefsDialog(Toplevel): button = Tkinter.Button(body, text="...", command=self.get_bnkpath) 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.grid(row=2, column=1, sticky=sticky) prefdir = self.prefs_array['dir'] - infofile = os.path.join(prefdir,'kindle.info') path = '' + infofile = os.path.join(prefdir,'kindle.info') + ainfofile = os.path.join(prefdir,'.kinf') if os.path.isfile(infofile): path = infofile + elif os.path.isfile(ainfofile): + path = ainfofile path = path.encode('utf-8') self.altinfopath.insert(0, path) button = Tkinter.Button(body, text="...", command=self.get_altinfopath) @@ -245,8 +248,8 @@ class PrefsDialog(Toplevel): def get_altinfopath(self): cpath = self.altinfopath.get() - altinfopath = tkFileDialog.askopenfilename(parent=None, title='Select Alternative kindle.info File', - defaultextension='.info', filetypes=[('Kindle Info', '.info'),('All Files', '.*')], + altinfopath = tkFileDialog.askopenfilename(parent=None, title='Select Alternative kindle.info or .kinf File', + defaultextension='.info', filetypes=[('Kindle Info', '.info'),('Kindle KInf','.kinf')('All Files', '.*')], initialdir=cpath) if altinfopath: altinfopath = os.path.normpath(altinfopath) @@ -457,8 +460,7 @@ class ConvDialog(Toplevel): name, ext = os.path.splitext(os.path.basename(infile)) ext = ext.lower() if ext == '.epub': - outfile = os.path.join(outdir, name + '_nodrm.epub') - self.p2 = processEPUB(apphome, infile, outfile, rscpath) + self.p2 = processEPUB(apphome, infile, outdir, rscpath) return 0 if ext == '.pdb': self.p2 = processPDB(apphome, infile, outdir, rscpath) @@ -467,8 +469,7 @@ class ConvDialog(Toplevel): self.p2 = processK4MOBI(apphome, infile, outdir, rscpath) return 0 if ext == '.pdf': - outfile = os.path.join(outdir, name + '_nodrm.pdf') - self.p2 = processPDF(apphome, infile, outfile, rscpath) + self.p2 = processPDF(apphome, infile, outdir, rscpath) return 0 return rv @@ -506,7 +507,7 @@ def processK4MOBI(apphome, infile, outdir, rscpath): parms += '-s "' + serialnums + '" ' files = os.listdir(rscpath) - filefilter = re.compile("\.info$", re.IGNORECASE) + filefilter = re.compile("\.info$|\.kinf$", re.IGNORECASE) files = filter(filefilter.search, files) if files: for filename in files: @@ -516,16 +517,16 @@ def processK4MOBI(apphome, infile, outdir, rscpath): p2 = runit(apphome, cmd, parms) return p2 -def processPDF(apphome, infile, outfile, rscpath): +def processPDF(apphome, infile, outdir, rscpath): cmd = os.path.join('lib','decryptpdf.py') - parms = '"' + infile + '" "' + outfile + '" "' + rscpath + '"' + parms = '"' + infile + '" "' + outdir + '" "' + rscpath + '"' p2 = runit(apphome, cmd, parms) return p2 -def processEPUB(apphome, infile, outfile, rscpath): +def processEPUB(apphome, infile, outdir, rscpath): # invoke routine to check both Adept and Barnes and Noble cmd = os.path.join('lib','decryptepub.py') - parms = '"' + infile + '" "' + outfile + '" "' + rscpath + '"' + parms = '"' + infile + '" "' + outdir + '" "' + rscpath + '"' p2 = runit(apphome, cmd, parms) return p2 diff --git a/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/decryptepub.py b/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/decryptepub.py index b9c9330..93713e4 100644 --- a/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/decryptepub.py +++ b/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/decryptepub.py @@ -24,7 +24,7 @@ def main(argv=sys.argv): if len(args) != 3: return -1 infile = args[0] - outfile = args[1] + outdir = args[1] rscpath = args[2] errlog = '' @@ -37,6 +37,9 @@ def main(argv=sys.argv): print "Error while trying to fix epub" return rv + # determine a good name for the output file + outfile = os.path.join(outdir, name + '_nodrm.epub') + rv = 1 # first try with the Adobe adept epub # try with any keyfiles (*.der) in the rscpath diff --git a/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/decryptpdf.py b/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/decryptpdf.py index f18e75e..82d222a 100644 --- a/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/decryptpdf.py +++ b/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/decryptpdf.py @@ -21,10 +21,15 @@ def main(argv=sys.argv): if len(args) != 3: return -1 infile = args[0] - outfile = args[1] + outdir = args[1] rscpath = args[2] errlog = '' 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 files = os.listdir(rscpath) filefilter = re.compile("\.der$", re.IGNORECASE) diff --git a/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/erdr2pml.py b/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/erdr2pml.py index 6df9e13..0e59d8d 100644 --- a/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/erdr2pml.py +++ b/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/erdr2pml.py @@ -58,8 +58,9 @@ # 0.17 - added support for pycrypto's DES as well # 0.18 - on Windows try PyCrypto first and OpenSSL next # 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: def __init__(self, stream): @@ -71,32 +72,50 @@ class Unbuffered: return getattr(self.stream, attr) import sys -sys.stdout=Unbuffered(sys.stdout) - import struct, binascii, getopt, zlib, os, os.path, urllib, tempfile +if 'calibre' in sys.modules: + inCalibre = True +else: + inCalibre = False + Des = None if sys.platform.startswith('win'): # 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() if Des == None: # 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() else: # 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() if Des == None: # 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() # if that did not work then use pure python implementation # of DES and try to speed it up with Psycho if Des == None: - import python_des + if inCalibre: + from calibre_plugins.erdrpdb2pml import python_des + else: + import python_des Des = python_des.Des # Import Psyco if available try: @@ -480,5 +499,6 @@ def main(argv=None): if __name__ == "__main__": + sys.stdout=Unbuffered(sys.stdout) sys.exit(main()) diff --git a/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/genbook.py b/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/genbook.py index 11258d1..f779cee 100644 --- a/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/genbook.py +++ b/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/genbook.py @@ -21,10 +21,21 @@ from struct import unpack # local support routines -import convert2xml -import flatxml2html -import flatxml2svg -import stylexml2css +if 'calibre' in sys.modules: + inCalibre = True +else: + 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 @@ -504,7 +515,7 @@ def generateBook(bookDir, raw, fixedimage): opfstr += ' \n' opfstr += '\n' opfstr += ' \n' - opfstr += ' \n' + opfstr += ' \n' # adding image files to manifest filenames = os.listdir(imgDir) filenames = sorted(filenames) diff --git a/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/ineptpdf.py b/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/ineptpdf.py index 40e97cd..c9a419b 100644 --- a/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/ineptpdf.py +++ b/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/ineptpdf.py @@ -157,6 +157,7 @@ def _load_crypto_libcrypto(): return out.raw class AES(object): + MODE_CBC = 0 @classmethod def new(cls, userkey, mode, iv): self = AES() diff --git a/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/k4mobidedrm.py b/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/k4mobidedrm.py index 64a998a..14556db 100644 --- a/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/k4mobidedrm.py +++ b/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/k4mobidedrm.py @@ -16,20 +16,8 @@ from __future__ import with_statement # 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' +__version__ = '3.1' class Unbuffered: def __init__(self, stream): @@ -43,11 +31,7 @@ class Unbuffered: 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 @@ -57,19 +41,15 @@ if 'calibre' in sys.modules: 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) +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 @@ -94,10 +74,6 @@ def cleanup_name(name): 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" @@ -113,8 +89,7 @@ def decryptBook(infile, outdir, k4, kInfoFiles, serials, pids): if mobi: mb = mobidedrm.MobiBook(infile) else: - tempdir = tempfile.mkdtemp() - mb = topazextract.TopazBook(infile, tempdir) + mb = topazextract.TopazBook(infile) title = mb.getBookTitle() 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) try: - if mobi: - unlocked_file = mb.processBook(pidlst) - else: - mb.processBook(pidlst) + 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: - 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 + print "Error: " + str(e) + "\nDRM Removal Failed.\n" + return 1 if mobi: - outfile = os.path.join(outdir,outfilename + '_nodrm' + '.mobi') - file(outfile, 'wb').write(unlocked_file) + outfile = os.path.join(outdir, outfilename + '_nodrm' + '.mobi') + mb.getMobiFile(outfile) 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() + # topaz: + print " Creating NoDRM HTMLZ Archive" + zipname = os.path.join(outdir, outfilename + '_nodrm' + '.htmlz') + mb.getHTMLZip(zipname) - 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 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') - myzip3 = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False) - targetdir = os.path.join(tempdir,'xml') - zipUpDir(myzip3, targetdir, '') - zipUpDir(myzip3, tempdir, 'img') - myzip3.close() + mb.getXMLZip(zipname) + + # remove internal temporary directory of Topaz pieces + mb.cleanup() - shutil.rmtree(tempdir, True) return 0 @@ -236,7 +190,6 @@ def main(argv=sys.argv): kInfoFiles = None infile = args[0] outdir = args[1] - return decryptBook(infile, outdir, k4, kInfoFiles, serials, pids) @@ -244,131 +197,3 @@ 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.' \ No newline at end of file diff --git a/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/k4mutils.py b/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/k4mutils.py index 539723d..534c389 100644 --- a/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/k4mutils.py +++ b/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/k4mutils.py @@ -1,10 +1,12 @@ # 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 @@ -66,9 +68,8 @@ def _load_crypto_libcrypto(): raise DrmException('AES decryption failed') return out.raw - def keyivgen(self, passwd): - salt = '16743' - saltlen = 5 + def keyivgen(self, passwd, salt): + saltlen = len(salt) passlen = len(passwd) iter = 0x3e8 keylen = 80 @@ -91,12 +92,78 @@ 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" -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 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 diff --git a/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/k4pcutils.py b/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/k4pcutils.py index d460a70..690033b 100644 --- a/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/k4pcutils.py +++ b/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/k4pcutils.py @@ -1,8 +1,10 @@ +#!/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, \ @@ -10,25 +12,86 @@ from ctypes import windll, c_char_p, c_wchar_p, c_uint, POINTER, byref, \ import _winreg as winreg -import traceback - MAX_PATH = 255 kernel32 = windll.kernel32 advapi32 = windll.advapi32 crypt32 = windll.crypt32 +import traceback -# Various character maps used to decrypt books. Probably supposed to act as obfuscation -charMap1 = "n5Pr6St7Uv8Wx9YzAb0Cd1Ef2Gh3Jk4M" +# 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 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 + + diff --git a/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/kgenpids.py b/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/kgenpids.py index c10f105..c9d8944 100644 --- a/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/kgenpids.py +++ b/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/kgenpids.py @@ -11,16 +11,28 @@ from struct import pack, unpack, unpack_from class DrmException(Exception): pass -global kindleDatabase global charMap1 -global charMap2 global charMap3 global charMap4 -if sys.platform.startswith('win'): - from k4pcutils import getKindleInfoFiles, parseKindleInfo, CryptUnprotectData, GetUserName, GetVolumeSerialNumber, charMap2 -if sys.platform.startswith('darwin'): - from k4mutils import getKindleInfoFiles, parseKindleInfo, CryptUnprotectData, GetUserName, GetVolumeSerialNumber, charMap2 +if 'calibre' in sys.modules: + inCalibre = True +else: + 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" charMap3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" @@ -66,50 +78,7 @@ def decode(data,map): value = (((high * len(map)) ^ 0x80) & 0xFF) + low result += pack("B",value) 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 # @@ -222,15 +191,15 @@ def getKindlePid(pidlst, rec209, token, serialnum): return pidlst -# Parse the EXTH header records and parse the Kindleinfo -# file to calculate the book pid. +# parse the Kindleinfo 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): - global kindleDatabase global charMap1 kindleDatabase = None try: - kindleDatabase = parseKindleInfo(kInfoFile) + kindleDatabase = getDBfromFile(kInfoFile) except Exception, message: print(message) kindleDatabase = None @@ -241,10 +210,10 @@ def getK4Pids(pidlst, rec209, token, kInfoFile): try: # Get the Mazama Random number - MazamaRandomNumber = getKindleInfoValueForKey("MazamaRandomNumber") + MazamaRandomNumber = kindleDatabase["MazamaRandomNumber"] # Get the kindle account token - kindleAccountToken = getKindleInfoValueForKey("kindle.account.tokens") + kindleAccountToken = kindleDatabase["kindle.account.tokens"] except KeyError: print "Keys not found in " + kInfoFile return pidlst diff --git a/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/mobidedrm.py b/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/mobidedrm.py index 7aef175..41bb12d 100644 --- a/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/mobidedrm.py +++ b/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/mobidedrm.py @@ -51,8 +51,9 @@ # 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! +# 0.30 - Modified interface slightly to work better with new calibre plugin style -__version__ = '0.29' +__version__ = '0.30' import sys @@ -163,6 +164,7 @@ class MobiBook: def __init__(self, infile): # initial sanity check on file self.data_file = file(infile, 'rb').read() + self.mobi_data = '' 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") @@ -301,13 +303,17 @@ class MobiBook: break return [found_key,pid] + def getMobiFile(self, outpath): + file(outpath,'wb').write(self.mobi_data) + 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 + self.mobi_data = self.data_file + return if crypto_type != 2 and crypto_type != 1: raise DrmException("Cannot decode unknown Mobipocket encryption type %d" % crypto_type) @@ -353,33 +359,35 @@ class MobiBook: # decrypt sections 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): 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]) + self.mobi_data += PC1(found_key, data[0:len(data) - extra_size]) if extra_size > 0: - new_data += data[-extra_size:] + self.mobi_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 + self.mobi_data += self.data_file[self.sections[self.records+1][0]:] print "done" - return self.data_file + return def getUnencryptedBook(infile,pid): if not os.path.isfile(infile): raise DrmException('Input File Not Found') book = MobiBook(infile) - return book.processBook([pid]) + book.processBook([pid]) + return book.mobi_data def getUnencryptedBookWithList(infile,pidlist): if not os.path.isfile(infile): raise DrmException('Input File Not Found') book = MobiBook(infile) - return book.processBook(pidlist) + book.processBook(pidlist) + return book.mobi_data + def main(argv=sys.argv): print ('MobiDeDrm v%(__version__)s. ' diff --git a/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/topazextract.py b/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/topazextract.py index 59bc5fa..12b92e2 100644 --- a/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/topazextract.py +++ b/DeDRM_Windows_Application/DeDRM_WinApp/DeDRM_lib/lib/topazextract.py @@ -10,7 +10,12 @@ class Unbuffered: return getattr(self.stream, attr) import sys -sys.stdout=Unbuffered(sys.stdout) + +if 'calibre' in sys.modules: + inCalibre = True +else: + inCalibre = False + import os, csv, getopt import zlib, zipfile, tempfile, shutil from struct import pack @@ -18,10 +23,32 @@ from struct import unpack class TpzDRMError(Exception): pass + # local support routines -import kgenpids -import genbook +if inCalibre: + 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 # @@ -110,9 +137,9 @@ def decryptDkeyRecords(data,PID): class TopazBook: - def __init__(self, filename, outdir): + def __init__(self, filename): self.fo = file(filename, 'rb') - self.outdir = outdir + self.outdir = tempfile.mkdtemp() self.bookPayloadOffset = 0 self.bookHeaderRecords = {} self.bookMetadata = {} @@ -317,21 +344,33 @@ class TopazBook: file(outputFile, 'wb').write(record) 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): - 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) + def getSVGZip(self, zipname): + svgzip = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False) + svgzip.write(os.path.join(self.outdir,'index_svg.xhtml'),'index_svg.xhtml') + zipUpDir(svgzip, self.outdir, 'svg') + zipUpDir(svgzip, self.outdir, 'img') + svgzip.close() + 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): print "Removes DRM protection from Topaz ebooks and extract the contents" @@ -383,58 +422,46 @@ def main(argv=sys.argv): return 1 bookname = os.path.splitext(os.path.basename(infile))[0] - tempdir = tempfile.mkdtemp() - tb = TopazBook(infile, tempdir) + tb = TopazBook(infile) title = tb.getBookTitle() print "Processing Book: ", title keysRecord, keysRecordRecord = tb.getPIDMetaInfo() pidlst = kgenpids.getPidList(keysRecord, keysRecordRecord, k4, pids, serials, kInfoFiles) try: + print "Decrypting Book" 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: print str(e) - 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) + tb.cleanup() return 1 - print " Creating HTML ZIP Archive" - zipname = os.path.join(outdir, bookname + '_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, 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) + except Exception, e: + print str(e) + tb.cleanup + return 1 return 0 if __name__ == '__main__': + sys.stdout=Unbuffered(sys.stdout) sys.exit(main()) diff --git a/KindleBooks_Tools/KindleBooks/KindleBooks.pyw b/KindleBooks/KindleBooks.pyw similarity index 100% rename from KindleBooks_Tools/KindleBooks/KindleBooks.pyw rename to KindleBooks/KindleBooks.pyw diff --git a/KindleBooks_Tools/KindleBooks/README_KindleBooks.txt b/KindleBooks/README_KindleBooks.txt similarity index 100% rename from KindleBooks_Tools/KindleBooks/README_KindleBooks.txt rename to KindleBooks/README_KindleBooks.txt diff --git a/KindleBooks_Tools/KindleBooks/lib/cmbtc_v2.2.py b/KindleBooks/lib/cmbtc_v2.2.py similarity index 100% rename from KindleBooks_Tools/KindleBooks/lib/cmbtc_v2.2.py rename to KindleBooks/lib/cmbtc_v2.2.py diff --git a/KindleBooks_Tools/KindleBooks/lib/convert2xml.py b/KindleBooks/lib/convert2xml.py similarity index 100% rename from KindleBooks_Tools/KindleBooks/lib/convert2xml.py rename to KindleBooks/lib/convert2xml.py diff --git a/KindleBooks_Tools/KindleBooks/lib/flatxml2html.py b/KindleBooks/lib/flatxml2html.py similarity index 100% rename from KindleBooks_Tools/KindleBooks/lib/flatxml2html.py rename to KindleBooks/lib/flatxml2html.py diff --git a/KindleBooks_Tools/KindleBooks/lib/flatxml2svg.py b/KindleBooks/lib/flatxml2svg.py similarity index 100% rename from KindleBooks_Tools/KindleBooks/lib/flatxml2svg.py rename to KindleBooks/lib/flatxml2svg.py diff --git a/KindleBooks_Tools/KindleBooks/lib/genbook.py b/KindleBooks/lib/genbook.py similarity index 97% rename from KindleBooks_Tools/KindleBooks/lib/genbook.py rename to KindleBooks/lib/genbook.py index 11258d1..f779cee 100644 --- a/KindleBooks_Tools/KindleBooks/lib/genbook.py +++ b/KindleBooks/lib/genbook.py @@ -21,10 +21,21 @@ from struct import unpack # local support routines -import convert2xml -import flatxml2html -import flatxml2svg -import stylexml2css +if 'calibre' in sys.modules: + inCalibre = True +else: + 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 @@ -504,7 +515,7 @@ def generateBook(bookDir, raw, fixedimage): opfstr += ' \n' opfstr += '\n' opfstr += ' \n' - opfstr += ' \n' + opfstr += ' \n' # adding image files to manifest filenames = os.listdir(imgDir) filenames = sorted(filenames) diff --git a/KindleBooks_Tools/KindleBooks/lib/genxml.py b/KindleBooks/lib/genxml.py similarity index 100% rename from KindleBooks_Tools/KindleBooks/lib/genxml.py rename to KindleBooks/lib/genxml.py diff --git a/KindleBooks_Tools/KindleBooks/lib/k4mdumpkinfo.py b/KindleBooks/lib/k4mdumpkinfo.py similarity index 100% rename from KindleBooks_Tools/KindleBooks/lib/k4mdumpkinfo.py rename to KindleBooks/lib/k4mdumpkinfo.py diff --git a/KindleBooks/lib/k4mobidedrm.py b/KindleBooks/lib/k4mobidedrm.py new file mode 100644 index 0000000..14556db --- /dev/null +++ b/KindleBooks/lib/k4mobidedrm.py @@ -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 ] [-p ] [-s ] " % 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()) + diff --git a/KindleBooks/lib/k4mutils.py b/KindleBooks/lib/k4mutils.py new file mode 100644 index 0000000..534c389 --- /dev/null +++ b/KindleBooks/lib/k4mutils.py @@ -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 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 diff --git a/KindleBooks/lib/k4pcutils.py b/KindleBooks/lib/k4pcutils.py new file mode 100644 index 0000000..690033b --- /dev/null +++ b/KindleBooks/lib/k4pcutils.py @@ -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 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 + + diff --git a/KindleBooks_Tools/KindleBooks/lib/kgenpids.py b/KindleBooks/lib/kgenpids.py similarity index 74% rename from KindleBooks_Tools/KindleBooks/lib/kgenpids.py rename to KindleBooks/lib/kgenpids.py index c10f105..c9d8944 100644 --- a/KindleBooks_Tools/KindleBooks/lib/kgenpids.py +++ b/KindleBooks/lib/kgenpids.py @@ -11,16 +11,28 @@ from struct import pack, unpack, unpack_from class DrmException(Exception): pass -global kindleDatabase global charMap1 -global charMap2 global charMap3 global charMap4 -if sys.platform.startswith('win'): - from k4pcutils import getKindleInfoFiles, parseKindleInfo, CryptUnprotectData, GetUserName, GetVolumeSerialNumber, charMap2 -if sys.platform.startswith('darwin'): - from k4mutils import getKindleInfoFiles, parseKindleInfo, CryptUnprotectData, GetUserName, GetVolumeSerialNumber, charMap2 +if 'calibre' in sys.modules: + inCalibre = True +else: + 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" charMap3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" @@ -66,50 +78,7 @@ def decode(data,map): value = (((high * len(map)) ^ 0x80) & 0xFF) + low result += pack("B",value) 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 # @@ -222,15 +191,15 @@ def getKindlePid(pidlst, rec209, token, serialnum): return pidlst -# Parse the EXTH header records and parse the Kindleinfo -# file to calculate the book pid. +# parse the Kindleinfo 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): - global kindleDatabase global charMap1 kindleDatabase = None try: - kindleDatabase = parseKindleInfo(kInfoFile) + kindleDatabase = getDBfromFile(kInfoFile) except Exception, message: print(message) kindleDatabase = None @@ -241,10 +210,10 @@ def getK4Pids(pidlst, rec209, token, kInfoFile): try: # Get the Mazama Random number - MazamaRandomNumber = getKindleInfoValueForKey("MazamaRandomNumber") + MazamaRandomNumber = kindleDatabase["MazamaRandomNumber"] # Get the kindle account token - kindleAccountToken = getKindleInfoValueForKey("kindle.account.tokens") + kindleAccountToken = kindleDatabase["kindle.account.tokens"] except KeyError: print "Keys not found in " + kInfoFile return pidlst diff --git a/KindleBooks_Tools/Kindle_4_PC_Unswindle/mobidedrm.py b/KindleBooks/lib/mobidedrm.py similarity index 96% rename from KindleBooks_Tools/Kindle_4_PC_Unswindle/mobidedrm.py rename to KindleBooks/lib/mobidedrm.py index 7aef175..41bb12d 100644 --- a/KindleBooks_Tools/Kindle_4_PC_Unswindle/mobidedrm.py +++ b/KindleBooks/lib/mobidedrm.py @@ -51,8 +51,9 @@ # 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! +# 0.30 - Modified interface slightly to work better with new calibre plugin style -__version__ = '0.29' +__version__ = '0.30' import sys @@ -163,6 +164,7 @@ class MobiBook: def __init__(self, infile): # initial sanity check on file self.data_file = file(infile, 'rb').read() + self.mobi_data = '' 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") @@ -301,13 +303,17 @@ class MobiBook: break return [found_key,pid] + def getMobiFile(self, outpath): + file(outpath,'wb').write(self.mobi_data) + 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 + self.mobi_data = self.data_file + return if crypto_type != 2 and crypto_type != 1: raise DrmException("Cannot decode unknown Mobipocket encryption type %d" % crypto_type) @@ -353,33 +359,35 @@ class MobiBook: # decrypt sections 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): 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]) + self.mobi_data += PC1(found_key, data[0:len(data) - extra_size]) if extra_size > 0: - new_data += data[-extra_size:] + self.mobi_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 + self.mobi_data += self.data_file[self.sections[self.records+1][0]:] print "done" - return self.data_file + return def getUnencryptedBook(infile,pid): if not os.path.isfile(infile): raise DrmException('Input File Not Found') book = MobiBook(infile) - return book.processBook([pid]) + book.processBook([pid]) + return book.mobi_data def getUnencryptedBookWithList(infile,pidlist): if not os.path.isfile(infile): raise DrmException('Input File Not Found') book = MobiBook(infile) - return book.processBook(pidlist) + book.processBook(pidlist) + return book.mobi_data + def main(argv=sys.argv): print ('MobiDeDrm v%(__version__)s. ' diff --git a/KindleBooks_Tools/KindleBooks/lib/scrolltextwidget.py b/KindleBooks/lib/scrolltextwidget.py similarity index 100% rename from KindleBooks_Tools/KindleBooks/lib/scrolltextwidget.py rename to KindleBooks/lib/scrolltextwidget.py diff --git a/KindleBooks_Tools/KindleBooks/lib/stylexml2css.py b/KindleBooks/lib/stylexml2css.py similarity index 100% rename from KindleBooks_Tools/KindleBooks/lib/stylexml2css.py rename to KindleBooks/lib/stylexml2css.py diff --git a/KindleBooks_Tools/KindleBooks/lib/subasyncio.py b/KindleBooks/lib/subasyncio.py similarity index 100% rename from KindleBooks_Tools/KindleBooks/lib/subasyncio.py rename to KindleBooks/lib/subasyncio.py diff --git a/KindleBooks_Tools/KindleBooks/lib/topazextract.py b/KindleBooks/lib/topazextract.py similarity index 84% rename from KindleBooks_Tools/KindleBooks/lib/topazextract.py rename to KindleBooks/lib/topazextract.py index 59bc5fa..12b92e2 100644 --- a/KindleBooks_Tools/KindleBooks/lib/topazextract.py +++ b/KindleBooks/lib/topazextract.py @@ -10,7 +10,12 @@ class Unbuffered: return getattr(self.stream, attr) import sys -sys.stdout=Unbuffered(sys.stdout) + +if 'calibre' in sys.modules: + inCalibre = True +else: + inCalibre = False + import os, csv, getopt import zlib, zipfile, tempfile, shutil from struct import pack @@ -18,10 +23,32 @@ from struct import unpack class TpzDRMError(Exception): pass + # local support routines -import kgenpids -import genbook +if inCalibre: + 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 # @@ -110,9 +137,9 @@ def decryptDkeyRecords(data,PID): class TopazBook: - def __init__(self, filename, outdir): + def __init__(self, filename): self.fo = file(filename, 'rb') - self.outdir = outdir + self.outdir = tempfile.mkdtemp() self.bookPayloadOffset = 0 self.bookHeaderRecords = {} self.bookMetadata = {} @@ -317,21 +344,33 @@ class TopazBook: file(outputFile, 'wb').write(record) 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): - 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) + def getSVGZip(self, zipname): + svgzip = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False) + svgzip.write(os.path.join(self.outdir,'index_svg.xhtml'),'index_svg.xhtml') + zipUpDir(svgzip, self.outdir, 'svg') + zipUpDir(svgzip, self.outdir, 'img') + svgzip.close() + 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): print "Removes DRM protection from Topaz ebooks and extract the contents" @@ -383,58 +422,46 @@ def main(argv=sys.argv): return 1 bookname = os.path.splitext(os.path.basename(infile))[0] - tempdir = tempfile.mkdtemp() - tb = TopazBook(infile, tempdir) + tb = TopazBook(infile) title = tb.getBookTitle() print "Processing Book: ", title keysRecord, keysRecordRecord = tb.getPIDMetaInfo() pidlst = kgenpids.getPidList(keysRecord, keysRecordRecord, k4, pids, serials, kInfoFiles) try: + print "Decrypting Book" 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: print str(e) - 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) + tb.cleanup() return 1 - print " Creating HTML ZIP Archive" - zipname = os.path.join(outdir, bookname + '_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, 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) + except Exception, e: + print str(e) + tb.cleanup + return 1 return 0 if __name__ == '__main__': + sys.stdout=Unbuffered(sys.stdout) sys.exit(main()) diff --git a/KindleBooks_Tools/KindleBooks/lib/k4mobidedrm.py b/KindleBooks_Tools/KindleBooks/lib/k4mobidedrm.py deleted file mode 100644 index 64a998a..0000000 --- a/KindleBooks_Tools/KindleBooks/lib/k4mobidedrm.py +++ /dev/null @@ -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 ] [-p ] [-s ] " % 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.' \ No newline at end of file diff --git a/KindleBooks_Tools/KindleBooks/lib/k4mutils.py b/KindleBooks_Tools/KindleBooks/lib/k4mutils.py deleted file mode 100644 index 539723d..0000000 --- a/KindleBooks_Tools/KindleBooks/lib/k4mutils.py +++ /dev/null @@ -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 diff --git a/KindleBooks_Tools/KindleBooks/lib/k4pcutils.py b/KindleBooks_Tools/KindleBooks/lib/k4pcutils.py deleted file mode 100644 index d460a70..0000000 --- a/KindleBooks_Tools/KindleBooks/lib/k4pcutils.py +++ /dev/null @@ -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 diff --git a/KindleBooks_Tools/KindleBooks/lib/mobidedrm.py b/KindleBooks_Tools/KindleBooks/lib/mobidedrm.py deleted file mode 100644 index 7aef175..0000000 --- a/KindleBooks_Tools/KindleBooks/lib/mobidedrm.py +++ /dev/null @@ -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 []" % 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()) diff --git a/KindleBooks_Tools/Kindle_4_Mac_Unswindle/K4Munswindle.pyw b/KindleBooks_Tools/Kindle_4_Mac_Unswindle/K4Munswindle.pyw deleted file mode 100644 index 58a8e96..0000000 --- a/KindleBooks_Tools/Kindle_4_Mac_Unswindle/K4Munswindle.pyw +++ /dev/null @@ -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()) diff --git a/KindleBooks_Tools/Kindle_4_Mac_Unswindle/README_K4Munswindle.txt b/KindleBooks_Tools/Kindle_4_Mac_Unswindle/README_K4Munswindle.txt deleted file mode 100644 index 1f1907b..0000000 --- a/KindleBooks_Tools/Kindle_4_Mac_Unswindle/README_K4Munswindle.txt +++ /dev/null @@ -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. - diff --git a/KindleBooks_Tools/Kindle_4_Mac_Unswindle/gdb_kindle_cmds_r1.txt b/KindleBooks_Tools/Kindle_4_Mac_Unswindle/gdb_kindle_cmds_r1.txt deleted file mode 100644 index dfcd5ab..0000000 --- a/KindleBooks_Tools/Kindle_4_Mac_Unswindle/gdb_kindle_cmds_r1.txt +++ /dev/null @@ -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 diff --git a/KindleBooks_Tools/Kindle_4_Mac_Unswindle/gdb_kindle_cmds_r2.txt b/KindleBooks_Tools/Kindle_4_Mac_Unswindle/gdb_kindle_cmds_r2.txt deleted file mode 100644 index d699d56..0000000 --- a/KindleBooks_Tools/Kindle_4_Mac_Unswindle/gdb_kindle_cmds_r2.txt +++ /dev/null @@ -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 diff --git a/KindleBooks_Tools/Kindle_4_Mac_Unswindle/gdb_kindle_cmds_r3.txt b/KindleBooks_Tools/Kindle_4_Mac_Unswindle/gdb_kindle_cmds_r3.txt deleted file mode 100644 index fcae6d6..0000000 --- a/KindleBooks_Tools/Kindle_4_Mac_Unswindle/gdb_kindle_cmds_r3.txt +++ /dev/null @@ -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 diff --git a/KindleBooks_Tools/Kindle_4_Mac_Unswindle/gdb_kindle_cmds_r4.txt b/KindleBooks_Tools/Kindle_4_Mac_Unswindle/gdb_kindle_cmds_r4.txt deleted file mode 100644 index 2626fc5..0000000 --- a/KindleBooks_Tools/Kindle_4_Mac_Unswindle/gdb_kindle_cmds_r4.txt +++ /dev/null @@ -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 diff --git a/KindleBooks_Tools/Kindle_4_Mac_Unswindle/lib/mobidedrm.py b/KindleBooks_Tools/Kindle_4_Mac_Unswindle/lib/mobidedrm.py deleted file mode 100644 index 7aef175..0000000 --- a/KindleBooks_Tools/Kindle_4_Mac_Unswindle/lib/mobidedrm.py +++ /dev/null @@ -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 []" % 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()) diff --git a/KindleBooks_Tools/Kindle_4_Mac_Unswindle/lib/scrolltextwidget.py b/KindleBooks_Tools/Kindle_4_Mac_Unswindle/lib/scrolltextwidget.py deleted file mode 100644 index 98b4147..0000000 --- a/KindleBooks_Tools/Kindle_4_Mac_Unswindle/lib/scrolltextwidget.py +++ /dev/null @@ -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) diff --git a/KindleBooks_Tools/Kindle_4_Mac_Unswindle/lib/subasyncio.py b/KindleBooks_Tools/Kindle_4_Mac_Unswindle/lib/subasyncio.py deleted file mode 100644 index ed13aa1..0000000 --- a/KindleBooks_Tools/Kindle_4_Mac_Unswindle/lib/subasyncio.py +++ /dev/null @@ -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 " -# 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() - diff --git a/KindleBooks_Tools/Kindle_4_PC_Unswindle/README-unswindlev7.txt b/KindleBooks_Tools/Kindle_4_PC_Unswindle/README-unswindlev7.txt deleted file mode 100644 index 46dc417..0000000 --- a/KindleBooks_Tools/Kindle_4_PC_Unswindle/README-unswindlev7.txt +++ /dev/null @@ -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. diff --git a/KindleBooks_Tools/Kindle_4_PC_Unswindle/unswindle.pyw b/KindleBooks_Tools/Kindle_4_PC_Unswindle/unswindle.pyw deleted file mode 100644 index 8eb0771..0000000 --- a/KindleBooks_Tools/Kindle_4_PC_Unswindle/unswindle.pyw +++ /dev/null @@ -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. - -# Before running this program, you must first install Python 2.6 from -# . 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()) diff --git a/Kindle_for_Android_Patch/ReadMe_K4Android.txt b/Kindle_for_Android_Patch/ReadMe_K4Android.txt new file mode 100644 index 0000000..f85e0fd --- /dev/null +++ b/Kindle_for_Android_Patch/ReadMe_K4Android.txt @@ -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 + diff --git a/Kindle_for_Android_Patch/kindle3.patch b/Kindle_for_Android_Patch/kindle3.patch new file mode 100644 index 0000000..36f63bd --- /dev/null +++ b/Kindle_for_Android_Patch/kindle3.patch @@ -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 (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;->(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 \ No newline at end of file diff --git a/Mobi_Additional_Tools/lib/mobidedrm.py b/Mobi_Additional_Tools/lib/mobidedrm.py index 7aef175..41bb12d 100644 --- a/Mobi_Additional_Tools/lib/mobidedrm.py +++ b/Mobi_Additional_Tools/lib/mobidedrm.py @@ -51,8 +51,9 @@ # 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! +# 0.30 - Modified interface slightly to work better with new calibre plugin style -__version__ = '0.29' +__version__ = '0.30' import sys @@ -163,6 +164,7 @@ class MobiBook: def __init__(self, infile): # initial sanity check on file self.data_file = file(infile, 'rb').read() + self.mobi_data = '' 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") @@ -301,13 +303,17 @@ class MobiBook: break return [found_key,pid] + def getMobiFile(self, outpath): + file(outpath,'wb').write(self.mobi_data) + 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 + self.mobi_data = self.data_file + return if crypto_type != 2 and crypto_type != 1: raise DrmException("Cannot decode unknown Mobipocket encryption type %d" % crypto_type) @@ -353,33 +359,35 @@ class MobiBook: # decrypt sections 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): 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]) + self.mobi_data += PC1(found_key, data[0:len(data) - extra_size]) if extra_size > 0: - new_data += data[-extra_size:] + self.mobi_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 + self.mobi_data += self.data_file[self.sections[self.records+1][0]:] print "done" - return self.data_file + return def getUnencryptedBook(infile,pid): if not os.path.isfile(infile): raise DrmException('Input File Not Found') book = MobiBook(infile) - return book.processBook([pid]) + book.processBook([pid]) + return book.mobi_data def getUnencryptedBookWithList(infile,pidlist): if not os.path.isfile(infile): raise DrmException('Input File Not Found') book = MobiBook(infile) - return book.processBook(pidlist) + book.processBook(pidlist) + return book.mobi_data + def main(argv=sys.argv): print ('MobiDeDrm v%(__version__)s. ' diff --git a/ReadMe_First.txt b/ReadMe_First.txt index 09a9f8a..45d738e 100644 --- a/ReadMe_First.txt +++ b/ReadMe_First.txt @@ -1,5 +1,3 @@ - - 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). @@ -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) ------------- -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. @@ -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) -------------- -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. @@ -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) -------------- -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 ----------- -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? ------------------------------ 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/ -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: @@ -54,15 +54,16 @@ The scripts are organized by type of ebook you need to remove the DRM from. Cho "Adobe_PDF_Tools" "Barnes_and_Noble_ePub_Tools" "eReader_PDB_Tools" - "KindleBooks_Tools" + "KindleBooks_Tools" + "Kindle_for_Android_Patch" 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". -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. -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. @@ -78,10 +79,12 @@ Check out their readmes for more info. 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 +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. @@ -102,7 +105,7 @@ For PyCrypto: 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. diff --git a/eReader_PDB_Tools/lib/erdr2pml.py b/eReader_PDB_Tools/lib/erdr2pml.py index 6df9e13..0e59d8d 100644 --- a/eReader_PDB_Tools/lib/erdr2pml.py +++ b/eReader_PDB_Tools/lib/erdr2pml.py @@ -58,8 +58,9 @@ # 0.17 - added support for pycrypto's DES as well # 0.18 - on Windows try PyCrypto first and OpenSSL next # 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: def __init__(self, stream): @@ -71,32 +72,50 @@ class Unbuffered: return getattr(self.stream, attr) import sys -sys.stdout=Unbuffered(sys.stdout) - import struct, binascii, getopt, zlib, os, os.path, urllib, tempfile +if 'calibre' in sys.modules: + inCalibre = True +else: + inCalibre = False + Des = None if sys.platform.startswith('win'): # 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() if Des == None: # 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() else: # 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() if Des == None: # 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() # if that did not work then use pure python implementation # of DES and try to speed it up with Psycho if Des == None: - import python_des + if inCalibre: + from calibre_plugins.erdrpdb2pml import python_des + else: + import python_des Des = python_des.Des # Import Psyco if available try: @@ -480,5 +499,6 @@ def main(argv=None): if __name__ == "__main__": + sys.stdout=Unbuffered(sys.stdout) sys.exit(main())