mirror of
https://github.com/noDRM/DeDRM_tools
synced 2024-12-28 22:24:27 +01:00
mobidedrm 0.15
This commit is contained in:
parent
37696e1495
commit
6c70a073d9
3 changed files with 257 additions and 179 deletions
|
@ -37,8 +37,9 @@
|
||||||
# in utf8 file are encrypted. (Although neither kind gets compressed.)
|
# in utf8 file are encrypted. (Although neither kind gets compressed.)
|
||||||
# This knowledge leads to a simplification of the test for the
|
# This knowledge leads to a simplification of the test for the
|
||||||
# trailing data byte flags - version 5 and higher AND header size >= 0xE4.
|
# trailing data byte flags - version 5 and higher AND header size >= 0xE4.
|
||||||
|
# 0.15 - Now outputs 'hearbeat', and is also quicker for long files.
|
||||||
|
|
||||||
__version__ = '0.14'
|
__version__ = '0.15'
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import struct
|
import struct
|
||||||
|
@ -196,8 +197,7 @@ class DrmStripper:
|
||||||
mobi_length, = struct.unpack('>L',sect[0x14:0x18])
|
mobi_length, = struct.unpack('>L',sect[0x14:0x18])
|
||||||
mobi_version, = struct.unpack('>L',sect[0x68:0x6C])
|
mobi_version, = struct.unpack('>L',sect[0x68:0x6C])
|
||||||
extra_data_flags = 0
|
extra_data_flags = 0
|
||||||
print "MOBI header length = %d" %mobi_length
|
print "MOBI header version = %d, length = %d" %(mobi_version, mobi_length)
|
||||||
print "MOBI header version = %d" %mobi_version
|
|
||||||
if (mobi_length >= 0xE4) and (mobi_version >= 5):
|
if (mobi_length >= 0xE4) and (mobi_version >= 5):
|
||||||
extra_data_flags, = struct.unpack('>H', sect[0xF2:0xF4])
|
extra_data_flags, = struct.unpack('>H', sect[0xF2:0xF4])
|
||||||
print "Extra Data Flags = %d" %extra_data_flags
|
print "Extra Data Flags = %d" %extra_data_flags
|
||||||
|
@ -227,13 +227,22 @@ class DrmStripper:
|
||||||
self.patchSection(0, "\0" * 2, 0xC)
|
self.patchSection(0, "\0" * 2, 0xC)
|
||||||
|
|
||||||
# decrypt sections
|
# decrypt sections
|
||||||
print "Decrypting. Please wait...",
|
print "Decrypting. Please wait . . .",
|
||||||
|
new_data = self.data_file[:self.sections[1][0]]
|
||||||
for i in xrange(1, records+1):
|
for i in xrange(1, records+1):
|
||||||
data = self.loadSection(i)
|
data = self.loadSection(i)
|
||||||
extra_size = getSizeOfTrailingDataEntries(data, len(data), extra_data_flags)
|
extra_size = getSizeOfTrailingDataEntries(data, len(data), extra_data_flags)
|
||||||
|
if i%100 == 0:
|
||||||
|
print ".",
|
||||||
# print "record %d, extra_size %d" %(i,extra_size)
|
# print "record %d, extra_size %d" %(i,extra_size)
|
||||||
self.patchSection(i, PC1(found_key, data[0:len(data) - extra_size]))
|
new_data += PC1(found_key, data[0:len(data) - extra_size])
|
||||||
print "done"
|
if extra_size > 0:
|
||||||
|
new_data += data[-extra_size:]
|
||||||
|
#self.patchSection(i, PC1(found_key, data[0:len(data) - extra_size]))
|
||||||
|
if self.num_sections > records+1:
|
||||||
|
new_data += self.data_file[self.sections[records+1][0]:]
|
||||||
|
self.data_file = new_data
|
||||||
|
print "done."
|
||||||
|
|
||||||
def getResult(self):
|
def getResult(self):
|
||||||
return self.data_file
|
return self.data_file
|
||||||
|
@ -246,7 +255,7 @@ if not __name__ == "__main__":
|
||||||
description = 'Removes DRM from secure Mobi files'
|
description = 'Removes DRM from secure Mobi files'
|
||||||
supported_platforms = ['linux', 'osx', 'windows'] # Platforms this plugin will run on
|
supported_platforms = ['linux', 'osx', 'windows'] # Platforms this plugin will run on
|
||||||
author = 'The Dark Reverser' # The author of this plugin
|
author = 'The Dark Reverser' # The author of this plugin
|
||||||
version = (0, 1, 4) # The version number of this plugin
|
version = (0, 1, 5) # The version number of this plugin
|
||||||
file_types = set(['prc','mobi','azw']) # The file types that this plugin will be applied to
|
file_types = set(['prc','mobi','azw']) # The file types that this plugin will be applied to
|
||||||
on_import = True # Run this plugin during the import
|
on_import = True # Run this plugin during the import
|
||||||
|
|
||||||
|
|
|
@ -37,8 +37,9 @@
|
||||||
# in utf8 file are encrypted. (Although neither kind gets compressed.)
|
# in utf8 file are encrypted. (Although neither kind gets compressed.)
|
||||||
# This knowledge leads to a simplification of the test for the
|
# This knowledge leads to a simplification of the test for the
|
||||||
# trailing data byte flags - version 5 and higher AND header size >= 0xE4.
|
# trailing data byte flags - version 5 and higher AND header size >= 0xE4.
|
||||||
|
# 0.15 - Now outputs 'hearbeat', and is also quicker for long files.
|
||||||
|
|
||||||
__version__ = '0.14'
|
__version__ = '0.15'
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import struct
|
import struct
|
||||||
|
@ -196,8 +197,7 @@ class DrmStripper:
|
||||||
mobi_length, = struct.unpack('>L',sect[0x14:0x18])
|
mobi_length, = struct.unpack('>L',sect[0x14:0x18])
|
||||||
mobi_version, = struct.unpack('>L',sect[0x68:0x6C])
|
mobi_version, = struct.unpack('>L',sect[0x68:0x6C])
|
||||||
extra_data_flags = 0
|
extra_data_flags = 0
|
||||||
print "MOBI header length = %d" %mobi_length
|
print "MOBI header version = %d, length = %d" %(mobi_version, mobi_length)
|
||||||
print "MOBI header version = %d" %mobi_version
|
|
||||||
if (mobi_length >= 0xE4) and (mobi_version >= 5):
|
if (mobi_length >= 0xE4) and (mobi_version >= 5):
|
||||||
extra_data_flags, = struct.unpack('>H', sect[0xF2:0xF4])
|
extra_data_flags, = struct.unpack('>H', sect[0xF2:0xF4])
|
||||||
print "Extra Data Flags = %d" %extra_data_flags
|
print "Extra Data Flags = %d" %extra_data_flags
|
||||||
|
@ -227,13 +227,22 @@ class DrmStripper:
|
||||||
self.patchSection(0, "\0" * 2, 0xC)
|
self.patchSection(0, "\0" * 2, 0xC)
|
||||||
|
|
||||||
# decrypt sections
|
# decrypt sections
|
||||||
print "Decrypting. Please wait...",
|
print "Decrypting. Please wait . . .",
|
||||||
|
new_data = self.data_file[:self.sections[1][0]]
|
||||||
for i in xrange(1, records+1):
|
for i in xrange(1, records+1):
|
||||||
data = self.loadSection(i)
|
data = self.loadSection(i)
|
||||||
extra_size = getSizeOfTrailingDataEntries(data, len(data), extra_data_flags)
|
extra_size = getSizeOfTrailingDataEntries(data, len(data), extra_data_flags)
|
||||||
|
if i%100 == 0:
|
||||||
|
print ".",
|
||||||
# print "record %d, extra_size %d" %(i,extra_size)
|
# print "record %d, extra_size %d" %(i,extra_size)
|
||||||
self.patchSection(i, PC1(found_key, data[0:len(data) - extra_size]))
|
new_data += PC1(found_key, data[0:len(data) - extra_size])
|
||||||
print "done"
|
if extra_size > 0:
|
||||||
|
new_data += data[-extra_size:]
|
||||||
|
#self.patchSection(i, PC1(found_key, data[0:len(data) - extra_size]))
|
||||||
|
if self.num_sections > records+1:
|
||||||
|
new_data += self.data_file[self.sections[records+1][0]:]
|
||||||
|
self.data_file = new_data
|
||||||
|
print "done."
|
||||||
|
|
||||||
def getResult(self):
|
def getResult(self):
|
||||||
return self.data_file
|
return self.data_file
|
||||||
|
@ -246,7 +255,7 @@ if not __name__ == "__main__":
|
||||||
description = 'Removes DRM from secure Mobi files'
|
description = 'Removes DRM from secure Mobi files'
|
||||||
supported_platforms = ['linux', 'osx', 'windows'] # Platforms this plugin will run on
|
supported_platforms = ['linux', 'osx', 'windows'] # Platforms this plugin will run on
|
||||||
author = 'The Dark Reverser' # The author of this plugin
|
author = 'The Dark Reverser' # The author of this plugin
|
||||||
version = (0, 1, 4) # The version number of this plugin
|
version = (0, 1, 5) # The version number of this plugin
|
||||||
file_types = set(['prc','mobi','azw']) # The file types that this plugin will be applied to
|
file_types = set(['prc','mobi','azw']) # The file types that this plugin will be applied to
|
||||||
on_import = True # Run this plugin during the import
|
on_import = True # Run this plugin during the import
|
||||||
|
|
||||||
|
|
|
@ -21,13 +21,43 @@
|
||||||
# 0.07 - The extra data flags aren't present in MOBI header < 0xE8 in size
|
# 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.08 - ...and also not in Mobi header version < 6
|
||||||
# 0.09 - ...but they are there with Mobi header version 6, header size 0xE4!
|
# 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 confusin 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 'hearbeat', and is also quicker for long files.
|
||||||
|
|
||||||
import sys,struct,binascii
|
__version__ = '0.15'
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import struct
|
||||||
|
import binascii
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
class DrmException(Exception):
|
class DrmException(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
#implementation of Pukall Cipher 1
|
# Implementation of Pukall Cipher 1
|
||||||
def PC1(key, src, decryption=True):
|
def PC1(key, src, decryption=True):
|
||||||
sum1 = 0;
|
sum1 = 0;
|
||||||
sum2 = 0;
|
sum2 = 0;
|
||||||
|
@ -62,188 +92,218 @@ def PC1(key, src, decryption=True):
|
||||||
return dst
|
return dst
|
||||||
|
|
||||||
def checksumPid(s):
|
def checksumPid(s):
|
||||||
letters = "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789"
|
letters = "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789"
|
||||||
crc = (~binascii.crc32(s,-1))&0xFFFFFFFF
|
crc = (~binascii.crc32(s,-1))&0xFFFFFFFF
|
||||||
crc = crc ^ (crc >> 16)
|
crc = crc ^ (crc >> 16)
|
||||||
res = s
|
res = s
|
||||||
l = len(letters)
|
l = len(letters)
|
||||||
for i in (0,1):
|
for i in (0,1):
|
||||||
b = crc & 0xff
|
b = crc & 0xff
|
||||||
pos = (b // l) ^ (b % l)
|
pos = (b // l) ^ (b % l)
|
||||||
res += letters[pos%l]
|
res += letters[pos%l]
|
||||||
crc >>= 8
|
crc >>= 8
|
||||||
return res
|
return res
|
||||||
|
|
||||||
def getSizeOfTrailingDataEntries(ptr, size, flags):
|
def getSizeOfTrailingDataEntries(ptr, size, flags):
|
||||||
def getSizeOfTrailingDataEntry(ptr, size):
|
def getSizeOfTrailingDataEntry(ptr, size):
|
||||||
bitpos, result = 0, 0
|
bitpos, result = 0, 0
|
||||||
if size <= 0:
|
if size <= 0:
|
||||||
return result
|
return result
|
||||||
while True:
|
while True:
|
||||||
v = ord(ptr[size-1])
|
v = ord(ptr[size-1])
|
||||||
result |= (v & 0x7F) << bitpos
|
result |= (v & 0x7F) << bitpos
|
||||||
bitpos += 7
|
bitpos += 7
|
||||||
size -= 1
|
size -= 1
|
||||||
if (v & 0x80) != 0 or (bitpos >= 28) or (size == 0):
|
if (v & 0x80) != 0 or (bitpos >= 28) or (size == 0):
|
||||||
return result
|
return result
|
||||||
num = 0
|
num = 0
|
||||||
testflags = flags >> 1
|
testflags = flags >> 1
|
||||||
while testflags:
|
while testflags:
|
||||||
if testflags & 1:
|
if testflags & 1:
|
||||||
num += getSizeOfTrailingDataEntry(ptr, size - num)
|
num += getSizeOfTrailingDataEntry(ptr, size - num)
|
||||||
testflags >>= 1
|
testflags >>= 1
|
||||||
if flags & 1:
|
# Multibyte data, if present, is included in the encryption, so
|
||||||
num += (ord(ptr[size - num - 1]) & 0x3) + 1
|
# we do not need to check the low bit.
|
||||||
return num
|
# if flags & 1:
|
||||||
|
# num += (ord(ptr[size - num - 1]) & 0x3) + 1
|
||||||
|
return num
|
||||||
|
|
||||||
class DrmStripper:
|
class DrmStripper:
|
||||||
def loadSection(self, section):
|
def loadSection(self, section):
|
||||||
if (section + 1 == self.num_sections):
|
if (section + 1 == self.num_sections):
|
||||||
endoff = len(self.data_file)
|
endoff = len(self.data_file)
|
||||||
else:
|
else:
|
||||||
endoff = self.sections[section + 1][0]
|
endoff = self.sections[section + 1][0]
|
||||||
off = self.sections[section][0]
|
off = self.sections[section][0]
|
||||||
return self.data_file[off:endoff]
|
return self.data_file[off:endoff]
|
||||||
|
|
||||||
def patch(self, off, new):
|
def patch(self, off, new):
|
||||||
self.data_file = self.data_file[:off] + new + self.data_file[off+len(new):]
|
self.data_file = self.data_file[:off] + new + self.data_file[off+len(new):]
|
||||||
|
|
||||||
def patchSection(self, section, new, in_off = 0):
|
def patchSection(self, section, new, in_off = 0):
|
||||||
if (section + 1 == self.num_sections):
|
if (section + 1 == self.num_sections):
|
||||||
endoff = len(self.data_file)
|
endoff = len(self.data_file)
|
||||||
else:
|
else:
|
||||||
endoff = self.sections[section + 1][0]
|
endoff = self.sections[section + 1][0]
|
||||||
off = self.sections[section][0]
|
off = self.sections[section][0]
|
||||||
assert off + in_off + len(new) <= endoff
|
assert off + in_off + len(new) <= endoff
|
||||||
self.patch(off + in_off, new)
|
self.patch(off + in_off, new)
|
||||||
|
|
||||||
def parseDRM(self, data, count, pid):
|
def parseDRM(self, data, count, pid):
|
||||||
pid = pid.ljust(16,'\0')
|
pid = pid.ljust(16,'\0')
|
||||||
keyvec1 = "\x72\x38\x33\xB0\xB4\xF2\xE3\xCA\xDF\x09\x01\xD6\xE2\xE0\x3F\x96"
|
keyvec1 = "\x72\x38\x33\xB0\xB4\xF2\xE3\xCA\xDF\x09\x01\xD6\xE2\xE0\x3F\x96"
|
||||||
temp_key = PC1(keyvec1, pid, False)
|
temp_key = PC1(keyvec1, pid, False)
|
||||||
temp_key_sum = sum(map(ord,temp_key)) & 0xff
|
temp_key_sum = sum(map(ord,temp_key)) & 0xff
|
||||||
found_key = None
|
found_key = None
|
||||||
for i in xrange(count):
|
for i in xrange(count):
|
||||||
verification, size, type, cksum, cookie = struct.unpack('>LLLBxxx32s', data[i*0x30:i*0x30+0x30])
|
verification, size, type, cksum, cookie = struct.unpack('>LLLBxxx32s', data[i*0x30:i*0x30+0x30])
|
||||||
cookie = PC1(temp_key, cookie)
|
cookie = PC1(temp_key, cookie)
|
||||||
ver,flags,finalkey,expiry,expiry2 = struct.unpack('>LL16sLL', cookie)
|
ver,flags,finalkey,expiry,expiry2 = struct.unpack('>LL16sLL', cookie)
|
||||||
if verification == ver and cksum == temp_key_sum and (flags & 0x1F) == 1:
|
if verification == ver and cksum == temp_key_sum and (flags & 0x1F) == 1:
|
||||||
found_key = finalkey
|
found_key = finalkey
|
||||||
break
|
break
|
||||||
return found_key
|
if not found_key:
|
||||||
|
# Then try the default encoding that doesn't require a PID
|
||||||
|
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])
|
||||||
|
cookie = PC1(temp_key, cookie)
|
||||||
|
ver,flags,finalkey,expiry,expiry2 = struct.unpack('>LL16sLL', cookie)
|
||||||
|
if verification == ver and cksum == temp_key_sum:
|
||||||
|
found_key = finalkey
|
||||||
|
break
|
||||||
|
return found_key
|
||||||
|
|
||||||
|
def __init__(self, data_file, pid):
|
||||||
|
if checksumPid(pid[0:-2]) != pid:
|
||||||
|
raise DrmException("invalid PID checksum")
|
||||||
|
pid = pid[0:-2]
|
||||||
|
|
||||||
def __init__(self, data_file, pid):
|
self.data_file = data_file
|
||||||
|
header = data_file[0:72]
|
||||||
|
if header[0x3C:0x3C+8] != 'BOOKMOBI':
|
||||||
|
raise DrmException("invalid file format")
|
||||||
|
self.num_sections, = struct.unpack('>H', data_file[76:78])
|
||||||
|
|
||||||
if checksumPid(pid[0:-2]) != pid:
|
self.sections = []
|
||||||
raise DrmException("invalid PID checksum")
|
for i in xrange(self.num_sections):
|
||||||
pid = pid[0:-2]
|
offset, a1,a2,a3,a4 = struct.unpack('>LBBBB', data_file[78+i*8:78+i*8+8])
|
||||||
|
flags, val = a1, a2<<16|a3<<8|a4
|
||||||
|
self.sections.append( (offset, flags, val) )
|
||||||
|
|
||||||
self.data_file = data_file
|
sect = self.loadSection(0)
|
||||||
header = data_file[0:72]
|
records, = struct.unpack('>H', sect[0x8:0x8+2])
|
||||||
if header[0x3C:0x3C+8] != 'BOOKMOBI':
|
mobi_length, = struct.unpack('>L',sect[0x14:0x18])
|
||||||
raise DrmException("invalid file format")
|
mobi_version, = struct.unpack('>L',sect[0x68:0x6C])
|
||||||
self.num_sections, = struct.unpack('>H', data_file[76:78])
|
extra_data_flags = 0
|
||||||
|
print "MOBI header version = %d, length = %d" %(mobi_version, mobi_length)
|
||||||
|
if (mobi_length >= 0xE4) and (mobi_version >= 5):
|
||||||
|
extra_data_flags, = struct.unpack('>H', sect[0xF2:0xF4])
|
||||||
|
print "Extra Data Flags = %d" %extra_data_flags
|
||||||
|
|
||||||
self.sections = []
|
crypto_type, = struct.unpack('>H', sect[0xC:0xC+2])
|
||||||
for i in xrange(self.num_sections):
|
if crypto_type == 0:
|
||||||
offset, a1,a2,a3,a4 = struct.unpack('>LBBBB', data_file[78+i*8:78+i*8+8])
|
print "This book is not encrypted."
|
||||||
flags, val = a1, a2<<16|a3<<8|a4
|
else:
|
||||||
self.sections.append( (offset, flags, val) )
|
if crypto_type == 1:
|
||||||
|
raise DrmException("cannot decode Mobipocket encryption type 1")
|
||||||
|
if crypto_type != 2:
|
||||||
|
raise DrmException("unknown encryption type: %d" % crypto_type)
|
||||||
|
|
||||||
sect = self.loadSection(0)
|
# calculate the keys
|
||||||
records, = struct.unpack('>H', sect[0x8:0x8+2])
|
drm_ptr, drm_count, drm_size, drm_flags = struct.unpack('>LLLL', sect[0xA8:0xA8+16])
|
||||||
mobi_length, = struct.unpack('>L',sect[0x14:0x18])
|
if drm_count == 0:
|
||||||
mobi_version, = struct.unpack('>L',sect[0x68:0x6C])
|
raise DrmException("no PIDs found in this file")
|
||||||
extra_data_flags = 0
|
found_key = self.parseDRM(sect[drm_ptr:drm_ptr+drm_size], drm_count, pid)
|
||||||
print "MOBI header length = %d" %mobi_length
|
if not found_key:
|
||||||
print "MOBI header version = %d" %mobi_version
|
raise DrmException("no key found. maybe the PID is incorrect")
|
||||||
if (mobi_length >= 0xE4) and (mobi_version > 5):
|
|
||||||
extra_data_flags, = struct.unpack('>H', sect[0xF2:0xF4])
|
|
||||||
print "Extra Data Flags = %d" %extra_data_flags
|
|
||||||
|
|
||||||
|
# kill the drm keys
|
||||||
|
self.patchSection(0, "\0" * drm_size, drm_ptr)
|
||||||
|
# kill the drm pointers
|
||||||
|
self.patchSection(0, "\xff" * 4 + "\0" * 12, 0xA8)
|
||||||
|
# clear the crypto type
|
||||||
|
self.patchSection(0, "\0" * 2, 0xC)
|
||||||
|
|
||||||
crypto_type, = struct.unpack('>H', sect[0xC:0xC+2])
|
# decrypt sections
|
||||||
if crypto_type == 0:
|
print "Decrypting. Please wait . . .",
|
||||||
raise DrmException("it seems that this book isn't encrypted")
|
new_data = self.data_file[:self.sections[1][0]]
|
||||||
if crypto_type == 1:
|
for i in xrange(1, records+1):
|
||||||
raise DrmException("cannot decode Mobipocket encryption type 1")
|
data = self.loadSection(i)
|
||||||
if crypto_type != 2:
|
extra_size = getSizeOfTrailingDataEntries(data, len(data), extra_data_flags)
|
||||||
raise DrmException("unknown encryption type: %d" % crypto_type)
|
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:]
|
||||||
|
#self.patchSection(i, PC1(found_key, data[0:len(data) - extra_size]))
|
||||||
|
if self.num_sections > records+1:
|
||||||
|
new_data += self.data_file[self.sections[records+1][0]:]
|
||||||
|
self.data_file = new_data
|
||||||
|
print "done."
|
||||||
|
|
||||||
# calculate the keys
|
def getResult(self):
|
||||||
drm_ptr, drm_count, drm_size, drm_flags = struct.unpack('>LLLL', sect[0xA8:0xA8+16])
|
return self.data_file
|
||||||
if drm_count == 0:
|
|
||||||
raise DrmException("no PIDs found in this file")
|
|
||||||
found_key = self.parseDRM(sect[drm_ptr:drm_ptr+drm_size], drm_count, pid)
|
|
||||||
if not found_key:
|
|
||||||
raise DrmException("no key found. maybe the PID is incorrect")
|
|
||||||
|
|
||||||
# kill the drm keys
|
|
||||||
self.patchSection(0, "\0" * drm_size, drm_ptr)
|
|
||||||
# kill the drm pointers
|
|
||||||
self.patchSection(0, "\xff" * 4 + "\0" * 12, 0xA8)
|
|
||||||
# clear the crypto type
|
|
||||||
self.patchSection(0, "\0" * 2, 0xC)
|
|
||||||
|
|
||||||
# decrypt sections
|
|
||||||
print "Decrypting. Please wait...",
|
|
||||||
for i in xrange(1, records+1):
|
|
||||||
data = self.loadSection(i)
|
|
||||||
extra_size = getSizeOfTrailingDataEntries(data, len(data), extra_data_flags)
|
|
||||||
# print "record %d, extra_size %d" %(i,extra_size)
|
|
||||||
self.patchSection(i, PC1(found_key, data[0:len(data) - extra_size]))
|
|
||||||
print "done"
|
|
||||||
def getResult(self):
|
|
||||||
return self.data_file
|
|
||||||
|
|
||||||
if not __name__ == "__main__":
|
if not __name__ == "__main__":
|
||||||
from calibre.customize import FileTypePlugin
|
from calibre.customize import FileTypePlugin
|
||||||
|
|
||||||
class MobiDeDRM(FileTypePlugin):
|
class MobiDeDRM(FileTypePlugin):
|
||||||
|
name = 'MobiDeDRM' # Name of the plugin
|
||||||
|
description = 'Removes DRM from secure Mobi files'
|
||||||
|
supported_platforms = ['linux', 'osx', 'windows'] # Platforms this plugin will run on
|
||||||
|
author = 'The Dark Reverser' # The author of this plugin
|
||||||
|
version = (0, 1, 5) # The version number of this plugin
|
||||||
|
file_types = set(['prc','mobi','azw']) # The file types that this plugin will be applied to
|
||||||
|
on_import = True # Run this plugin during the import
|
||||||
|
|
||||||
name = 'MobiDeDRM' # Name of the plugin
|
def run(self, path_to_ebook):
|
||||||
description = 'Removes DRM from secure Mobi files'
|
from calibre.gui2 import is_ok_to_use_qt
|
||||||
supported_platforms = ['linux', 'osx', 'windows'] # Platforms this plugin will run on
|
from PyQt4.Qt import QMessageBox
|
||||||
author = 'The Dark Reverser' # The author of this plugin
|
PID = self.site_customization
|
||||||
version = (0, 0, 9) # The version number of this plugin
|
data_file = file(path_to_ebook, 'rb').read()
|
||||||
file_types = set(['prc','mobi','azw']) # The file types that this plugin will be applied to
|
ar = PID.split(',')
|
||||||
on_import = True # Run this plugin during the import
|
for i in ar:
|
||||||
|
try:
|
||||||
|
unlocked_file = DrmStripper(data_file, i).getResult()
|
||||||
|
except DrmException:
|
||||||
|
# ignore the error
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
of = self.temporary_file('.mobi')
|
||||||
|
of.write(unlocked_file)
|
||||||
|
of.close()
|
||||||
|
return of.name
|
||||||
|
if is_ok_to_use_qt():
|
||||||
|
d = QMessageBox(QMessageBox.Warning, "MobiDeDRM Plugin", "Couldn't decode: %s\n\nImporting encrypted version." % path_to_ebook)
|
||||||
|
d.show()
|
||||||
|
d.raise_()
|
||||||
|
d.exec_()
|
||||||
|
return path_to_ebook
|
||||||
|
|
||||||
|
def customization_help(self, gui=False):
|
||||||
def run(self, path_to_ebook):
|
return 'Enter PID (separate multiple PIDs with comma)'
|
||||||
of = self.temporary_file('.mobi')
|
|
||||||
PID = self.site_customization
|
|
||||||
data_file = file(path_to_ebook, 'rb').read()
|
|
||||||
ar = PID.split(',')
|
|
||||||
for i in ar:
|
|
||||||
try:
|
|
||||||
file(of.name, 'wb').write(DrmStripper(data_file, i).getResult())
|
|
||||||
except DrmException:
|
|
||||||
# Hm, we should display an error dialog here.
|
|
||||||
# Dunno how though.
|
|
||||||
# Ignore the dirty hack behind the curtain.
|
|
||||||
# strexcept = 'echo exception: %s > /dev/tty' % e
|
|
||||||
# subprocess.call(strexcept,shell=True)
|
|
||||||
print i + ": not PID for book"
|
|
||||||
else:
|
|
||||||
return of.name
|
|
||||||
|
|
||||||
def customization_help(self, gui=False):
|
|
||||||
return 'Enter PID (separate multiple PIDs with comma)'
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print "MobiDeDrm v0.09. Copyright (c) 2008 The Dark Reverser"
|
sys.stdout=Unbuffered(sys.stdout)
|
||||||
if len(sys.argv)<4:
|
print ('MobiDeDrm v%(__version__)s. '
|
||||||
print "Removes protection from Mobipocket books"
|
'Copyright 2008-2010 The Dark Reverser.' % globals())
|
||||||
print "Usage:"
|
if len(sys.argv)<4:
|
||||||
print " mobidedrm infile.mobi outfile.mobi PID"
|
print "Removes protection from Mobipocket books"
|
||||||
else:
|
print "Usage:"
|
||||||
infile = sys.argv[1]
|
print " %s <infile> <outfile> <PID>" % sys.argv[0]
|
||||||
outfile = sys.argv[2]
|
sys.exit(1)
|
||||||
pid = sys.argv[3]
|
else:
|
||||||
data_file = file(infile, 'rb').read()
|
infile = sys.argv[1]
|
||||||
try:
|
outfile = sys.argv[2]
|
||||||
file(outfile, 'wb').write(DrmStripper(data_file, pid).getResult())
|
pid = sys.argv[3]
|
||||||
except DrmException, e:
|
data_file = file(infile, 'rb').read()
|
||||||
print "Error: %s" % e
|
try:
|
||||||
|
strippedFile = DrmStripper(data_file, pid)
|
||||||
|
file(outfile, 'wb').write(strippedFile.getResult())
|
||||||
|
except DrmException, e:
|
||||||
|
print "Error: %s" % e
|
||||||
|
sys.exit(1)
|
||||||
|
sys.exit(0)
|
Loading…
Reference in a new issue