def decryptepub(infile, outdir, rscpath): errlog = '' # first fix the epub to make sure we do not get errors name, ext = os.path.splitext(os.path.basename(infile)) bpath = os.path.dirname(infile) zippath = os.path.join(bpath,name + '_temp.zip') rv = zipfix.repairBook(infile, zippath) if rv != 0: 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 if ineptepub.adeptBook(zippath): # try with any keyfiles (*.der) in the rscpath files = os.listdir(rscpath) filefilter = re.compile("\.der$", re.IGNORECASE) files = filter(filefilter.search, files) if files: for filename in files: keypath = os.path.join(rscpath, filename) userkey = open(keypath,'rb').read() try: rv = ineptepub.decryptBook(userkey, zippath, outfile) if rv == 0: print "Decrypted Adobe ePub with key file {0}".format(filename) break except Exception, e: errlog += traceback.format_exc() errlog += str(e) rv = 1
def determine_file_type(file): # Returns a file type: # "PDF", "PDB", "MOBI", "TPZ", "LCP", "ADEPT", "ADEPT-PassHash", "KFX-ZIP", "ZIP" or None f = open(file, "rb") fdata = f.read(100) f.close() if fdata.startswith(b"PK\x03\x04"): pass # Either LCP, Adobe, or Amazon elif fdata.startswith(b"%PDF"): return "PDF" elif fdata[0x3c:0x3c + 8] == b"PNRdPPrs" or fdata[0x3c:0x3c + 8] == b"PDctPPrs": return "PDB" elif fdata[0x3c:0x3c + 8] == b"BOOKMOBI" or fdata[0x3c:0x3c + 8] == b"TEXtREAd": return "MOBI" elif fdata.startswith(b"TPZ"): return "TPZ" else: return None # Unknown file type # If it's a ZIP, determine the type. from lcpdedrm import isLCPbook if isLCPbook(file): return "LCP" from ineptepub import adeptBook, isPassHashBook if adeptBook(file): if isPassHashBook(file): return "ADEPT-PassHash" else: return "ADEPT" try: # Amazon / KFX-ZIP has a file that starts with b'\xeaDRMION\xee' in the ZIP. with closing(ZipFile(open(file, "rb"))) as book: for subfilename in book.namelist(): with book.open(subfilename) as subfile: data = subfile.read(8) if data == b'\xeaDRMION\xee': return "KFX-ZIP" except: pass return "ZIP"
def decryptepub(infile, outdir, rscpath): errlog = '' # first fix the epub to make sure we do not get errors name, ext = os.path.splitext(os.path.basename(infile)) bpath = os.path.dirname(infile) zippath = os.path.join(bpath, name + '_temp.zip') rv = zipfix.repairBook(infile, zippath) if rv != 0: 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 if ineptepub.adeptBook(zippath): # try with any keyfiles (*.der) in the rscpath files = os.listdir(rscpath) filefilter = re.compile("\.der$", re.IGNORECASE) files = filter(filefilter.search, files) if files: for filename in files: keypath = os.path.join(rscpath, filename) userkey = open(keypath, 'rb').read() try: rv = ineptepub.decryptBook(userkey, zippath, outfile) if rv == 0: print("Decrypted Adobe ePub with key file {0}".format( filename)) break except Exception as e: errlog += traceback.format_exc() errlog += str(e) rv = 1 # now try with ignoble epub elif ignobleepub.ignobleBook(zippath): # try with any keyfiles (*.b64) in the rscpath files = os.listdir(rscpath) filefilter = re.compile("\.b64$", re.IGNORECASE) files = filter(filefilter.search, files) if files: for filename in files: keypath = os.path.join(rscpath, filename) userkey = open(keypath, 'r').read() #print userkey try: rv = ignobleepub.decryptBook(userkey, zippath, outfile) if rv == 0: print("Decrypted B&N ePub with key file {0}".format( filename)) break except Exception as e: errlog += traceback.format_exc() errlog += str(e) rv = 1 else: encryption = epubtest.encryption(zippath) if encryption == "Unencrypted": print("{0} is not DRMed.".format(name)) rv = 0 else: print("{0} has an unknown encryption.".format(name)) os.remove(zippath) if rv != 0: print(errlog) return rv