2012-12-19 13:48:11 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2011-07-14 06:08:06 +00:00
|
|
|
# standlone set of Mac OSX specific routines needed for KindleBooks
|
2010-12-02 18:10:46 +00:00
|
|
|
|
|
|
|
from __future__ import with_statement
|
2011-06-16 05:59:20 +00:00
|
|
|
|
2010-12-02 18:10:46 +00:00
|
|
|
import sys
|
|
|
|
import os
|
2011-07-14 06:08:06 +00:00
|
|
|
import os.path
|
2012-03-06 18:24:28 +00:00
|
|
|
import re
|
|
|
|
import copy
|
2011-07-14 06:08:06 +00:00
|
|
|
import subprocess
|
2011-06-16 05:59:20 +00:00
|
|
|
from struct import pack, unpack, unpack_from
|
2010-12-02 18:10:46 +00:00
|
|
|
|
2011-01-06 07:10:38 +00:00
|
|
|
class DrmException(Exception):
|
2010-12-30 22:41:07 +00:00
|
|
|
pass
|
2010-12-02 18:10:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
# 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:
|
2012-12-19 13:48:11 +00:00
|
|
|
raise DrmException(u"libcrypto not found")
|
2010-12-02 18:10:46 +00:00
|
|
|
libcrypto = CDLL(libcrypto)
|
|
|
|
|
2012-03-06 18:24:28 +00:00
|
|
|
# From OpenSSL's crypto aes header
|
|
|
|
#
|
|
|
|
# AES_ENCRYPT 1
|
|
|
|
# AES_DECRYPT 0
|
|
|
|
# AES_MAXNR 14 (in bytes)
|
|
|
|
# AES_BLOCK_SIZE 16 (in bytes)
|
2012-11-20 13:28:12 +00:00
|
|
|
#
|
2012-03-06 18:24:28 +00:00
|
|
|
# struct aes_key_st {
|
|
|
|
# unsigned long rd_key[4 *(AES_MAXNR + 1)];
|
|
|
|
# int rounds;
|
|
|
|
# };
|
|
|
|
# typedef struct aes_key_st AES_KEY;
|
|
|
|
#
|
|
|
|
# int AES_set_decrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key);
|
|
|
|
#
|
2012-05-16 16:15:43 +00:00
|
|
|
# note: the ivec string, and output buffer are both mutable
|
2012-03-06 18:24:28 +00:00
|
|
|
# void AES_cbc_encrypt(const unsigned char *in, unsigned char *out,
|
|
|
|
# const unsigned long length, const AES_KEY *key, unsigned char *ivec, const int enc);
|
|
|
|
|
2010-12-02 18:10:46 +00:00
|
|
|
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)
|
2012-03-06 18:24:28 +00:00
|
|
|
|
2010-12-02 18:10:46 +00:00
|
|
|
def F(restype, name, argtypes):
|
|
|
|
func = getattr(libcrypto, name)
|
|
|
|
func.restype = restype
|
|
|
|
func.argtypes = argtypes
|
|
|
|
return func
|
2012-03-06 18:24:28 +00:00
|
|
|
|
2010-12-02 18:10:46 +00:00
|
|
|
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])
|
|
|
|
|
2012-03-06 18:24:28 +00:00
|
|
|
# From OpenSSL's Crypto evp/p5_crpt2.c
|
|
|
|
#
|
|
|
|
# int PKCS5_PBKDF2_HMAC_SHA1(const char *pass, int passlen,
|
|
|
|
# const unsigned char *salt, int saltlen, int iter,
|
|
|
|
# int keylen, unsigned char *out);
|
|
|
|
|
|
|
|
PKCS5_PBKDF2_HMAC_SHA1 = F(c_int, 'PKCS5_PBKDF2_HMAC_SHA1',
|
2010-12-02 18:10:46 +00:00
|
|
|
[c_char_p, c_ulong, c_char_p, c_ulong, c_ulong, c_ulong, c_char_p])
|
2012-03-06 18:24:28 +00:00
|
|
|
|
2010-12-02 18:10:46 +00:00
|
|
|
class LibCrypto(object):
|
|
|
|
def __init__(self):
|
|
|
|
self._blocksize = 0
|
|
|
|
self._keyctx = None
|
2012-03-06 18:24:28 +00:00
|
|
|
self._iv = 0
|
2010-12-02 18:10:46 +00:00
|
|
|
|
|
|
|
def set_decrypt_key(self, userkey, iv):
|
|
|
|
self._blocksize = len(userkey)
|
|
|
|
if (self._blocksize != 16) and (self._blocksize != 24) and (self._blocksize != 32) :
|
2012-12-19 13:48:11 +00:00
|
|
|
raise DrmException(u"AES improper key used")
|
2010-12-02 18:10:46 +00:00
|
|
|
return
|
|
|
|
keyctx = self._keyctx = AES_KEY()
|
2012-03-06 18:24:28 +00:00
|
|
|
self._iv = iv
|
|
|
|
self._userkey = userkey
|
2010-12-02 18:10:46 +00:00
|
|
|
rv = AES_set_decrypt_key(userkey, len(userkey) * 8, keyctx)
|
|
|
|
if rv < 0:
|
2012-12-19 13:48:11 +00:00
|
|
|
raise DrmException(u"Failed to initialize AES key")
|
2010-12-02 18:10:46 +00:00
|
|
|
|
|
|
|
def decrypt(self, data):
|
|
|
|
out = create_string_buffer(len(data))
|
2012-03-06 18:24:28 +00:00
|
|
|
mutable_iv = create_string_buffer(self._iv, len(self._iv))
|
|
|
|
keyctx = self._keyctx
|
|
|
|
rv = AES_cbc_encrypt(data, out, len(data), keyctx, mutable_iv, 0)
|
2010-12-02 18:10:46 +00:00
|
|
|
if rv == 0:
|
2012-12-19 13:48:11 +00:00
|
|
|
raise DrmException(u"AES decryption failed")
|
2010-12-02 18:10:46 +00:00
|
|
|
return out.raw
|
|
|
|
|
2011-07-14 06:08:06 +00:00
|
|
|
def keyivgen(self, passwd, salt, iter, keylen):
|
2011-06-16 05:59:20 +00:00
|
|
|
saltlen = len(salt)
|
2010-12-02 18:10:46 +00:00
|
|
|
passlen = len(passwd)
|
|
|
|
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()
|
2011-01-06 07:10:38 +00:00
|
|
|
except (ImportError, DrmException):
|
2010-12-02 18:10:46 +00:00
|
|
|
pass
|
|
|
|
return LibCrypto
|
|
|
|
|
|
|
|
LibCrypto = _load_crypto()
|
|
|
|
|
|
|
|
#
|
|
|
|
# Utility Routines
|
|
|
|
#
|
|
|
|
|
2011-06-16 05:59:20 +00:00
|
|
|
# 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()
|
2010-12-30 22:41:07 +00:00
|
|
|
|
|
|
|
# Various character maps used to decrypt books. Probably supposed to act as obfuscation
|
2012-12-19 13:48:11 +00:00
|
|
|
charMap1 = 'n5Pr6St7Uv8Wx9YzAb0Cd1Ef2Gh3Jk4M'
|
|
|
|
charMap2 = 'ZB0bYyc1xDdW2wEV3Ff7KkPpL8UuGA4gz-Tme9Nn_tHh5SvXCsIiR6rJjQaqlOoM'
|
2011-06-16 05:59:20 +00:00
|
|
|
|
2012-03-06 18:24:28 +00:00
|
|
|
# For kinf approach of K4Mac 1.6.X or later
|
2012-12-19 13:48:11 +00:00
|
|
|
# On K4PC charMap5 = 'AzB0bYyCeVvaZ3FfUuG4g-TtHh5SsIiR6rJjQq7KkPpL8lOoMm9Nn_c1XxDdW2wE'
|
2011-07-14 06:08:06 +00:00
|
|
|
# For Mac they seem to re-use charMap2 here
|
|
|
|
charMap5 = charMap2
|
2011-06-16 05:59:20 +00:00
|
|
|
|
2012-03-06 18:24:28 +00:00
|
|
|
# new in K4M 1.9.X
|
2012-12-19 13:48:11 +00:00
|
|
|
testMap8 = 'YvaZ3FfUm9Nn_c1XuG4yCAzB0beVg-TtHh5SsIiR6rJjQdW2wEq7KkPpL8lOoMxD'
|
2012-03-06 18:24:28 +00:00
|
|
|
|
|
|
|
|
2011-06-16 05:59:20 +00:00
|
|
|
def encode(data, map):
|
2012-12-19 13:48:11 +00:00
|
|
|
result = ''
|
2011-06-16 05:59:20 +00:00
|
|
|
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):
|
2012-12-19 13:48:11 +00:00
|
|
|
result = ''
|
2011-06-16 05:59:20 +00:00
|
|
|
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
|
2012-12-19 13:48:11 +00:00
|
|
|
result += pack('B',value)
|
2011-06-16 05:59:20 +00:00
|
|
|
return result
|
|
|
|
|
2012-03-06 18:24:28 +00:00
|
|
|
# For K4M 1.6.X and later
|
2011-06-16 05:59:20 +00:00
|
|
|
# generate table of prime number less than or equal to int n
|
|
|
|
def primes(n):
|
|
|
|
if n==2: return [2]
|
|
|
|
elif n<2: return []
|
|
|
|
s=range(3,n+1,2)
|
|
|
|
mroot = n ** 0.5
|
|
|
|
half=(n+1)/2-1
|
|
|
|
i=0
|
|
|
|
m=3
|
|
|
|
while m <= mroot:
|
|
|
|
if s[i]:
|
|
|
|
j=(m*m-3)/2
|
|
|
|
s[j]=0
|
|
|
|
while j<half:
|
|
|
|
s[j]=0
|
|
|
|
j+=m
|
|
|
|
i=i+1
|
|
|
|
m=2*i+3
|
|
|
|
return [2]+[x for x in s if x]
|
2010-12-30 22:41:07 +00:00
|
|
|
|
|
|
|
|
2010-12-02 18:10:46 +00:00
|
|
|
# uses a sub process to get the Hard Drive Serial Number using ioreg
|
2012-12-19 13:48:11 +00:00
|
|
|
# returns with the serial number of drive whose BSD Name is 'disk0'
|
2010-12-02 18:10:46 +00:00
|
|
|
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())
|
2010-12-30 22:41:07 +00:00
|
|
|
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')
|
2010-12-02 18:10:46 +00:00
|
|
|
cnt = len(reslst)
|
|
|
|
bsdname = None
|
|
|
|
sernum = None
|
|
|
|
foundIt = False
|
|
|
|
for j in xrange(cnt):
|
|
|
|
resline = reslst[j]
|
2012-12-19 13:48:11 +00:00
|
|
|
pp = resline.find('\"Serial Number\" = \"')
|
2010-12-02 18:10:46 +00:00
|
|
|
if pp >= 0:
|
|
|
|
sernum = resline[pp+19:-1]
|
|
|
|
sernum = sernum.strip()
|
2012-12-19 13:48:11 +00:00
|
|
|
bb = resline.find('\"BSD Name\" = \"')
|
2010-12-02 18:10:46 +00:00
|
|
|
if bb >= 0:
|
|
|
|
bsdname = resline[bb+14:-1]
|
|
|
|
bsdname = bsdname.strip()
|
|
|
|
if (bsdname == 'disk0') and (sernum != None):
|
|
|
|
foundIt = True
|
|
|
|
break
|
|
|
|
if not foundIt:
|
2011-07-14 06:08:06 +00:00
|
|
|
sernum = ''
|
2010-12-02 18:10:46 +00:00
|
|
|
return sernum
|
|
|
|
|
2011-07-14 06:08:06 +00:00
|
|
|
def GetUserHomeAppSupKindleDirParitionName():
|
|
|
|
home = os.getenv('HOME')
|
2012-09-11 16:57:03 +00:00
|
|
|
dpath = home + '/Library'
|
2011-07-14 06:08:06 +00:00
|
|
|
cmdline = '/sbin/mount'
|
|
|
|
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)
|
|
|
|
disk = ''
|
|
|
|
foundIt = False
|
|
|
|
for j in xrange(cnt):
|
|
|
|
resline = reslst[j]
|
|
|
|
if resline.startswith('/dev'):
|
|
|
|
(devpart, mpath) = resline.split(' on ')
|
|
|
|
dpart = devpart[5:]
|
|
|
|
pp = mpath.find('(')
|
|
|
|
if pp >= 0:
|
|
|
|
mpath = mpath[:pp-1]
|
|
|
|
if dpath.startswith(mpath):
|
|
|
|
disk = dpart
|
|
|
|
return disk
|
|
|
|
|
|
|
|
# uses a sub process to get the UUID of the specified disk partition using ioreg
|
|
|
|
def GetDiskPartitionUUID(diskpart):
|
|
|
|
uuidnum = os.getenv('MYUUIDNUMBER')
|
|
|
|
if uuidnum != None:
|
|
|
|
return uuidnum
|
|
|
|
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
|
|
|
|
uuidnum = None
|
|
|
|
foundIt = False
|
|
|
|
nest = 0
|
|
|
|
uuidnest = -1
|
|
|
|
partnest = -2
|
|
|
|
for j in xrange(cnt):
|
|
|
|
resline = reslst[j]
|
|
|
|
if resline.find('{') >= 0:
|
|
|
|
nest += 1
|
|
|
|
if resline.find('}') >= 0:
|
|
|
|
nest -= 1
|
2012-12-19 13:48:11 +00:00
|
|
|
pp = resline.find('\"UUID\" = \"')
|
2011-07-14 06:08:06 +00:00
|
|
|
if pp >= 0:
|
|
|
|
uuidnum = resline[pp+10:-1]
|
|
|
|
uuidnum = uuidnum.strip()
|
|
|
|
uuidnest = nest
|
|
|
|
if partnest == uuidnest and uuidnest > 0:
|
|
|
|
foundIt = True
|
|
|
|
break
|
2012-12-19 13:48:11 +00:00
|
|
|
bb = resline.find('\"BSD Name\" = \"')
|
2011-07-14 06:08:06 +00:00
|
|
|
if bb >= 0:
|
|
|
|
bsdname = resline[bb+14:-1]
|
|
|
|
bsdname = bsdname.strip()
|
|
|
|
if (bsdname == diskpart):
|
|
|
|
partnest = nest
|
|
|
|
else :
|
|
|
|
partnest = -2
|
|
|
|
if partnest == uuidnest and partnest > 0:
|
|
|
|
foundIt = True
|
|
|
|
break
|
|
|
|
if nest == 0:
|
|
|
|
partnest = -2
|
|
|
|
uuidnest = -1
|
|
|
|
uuidnum = None
|
|
|
|
bsdname = None
|
|
|
|
if not foundIt:
|
|
|
|
uuidnum = ''
|
|
|
|
return uuidnum
|
2012-03-06 18:24:28 +00:00
|
|
|
|
2011-07-14 06:08:06 +00:00
|
|
|
def GetMACAddressMunged():
|
|
|
|
macnum = os.getenv('MYMACNUM')
|
|
|
|
if macnum != None:
|
|
|
|
return macnum
|
|
|
|
cmdline = '/sbin/ifconfig en0'
|
|
|
|
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)
|
|
|
|
macnum = None
|
|
|
|
foundIt = False
|
|
|
|
for j in xrange(cnt):
|
|
|
|
resline = reslst[j]
|
|
|
|
pp = resline.find('ether ')
|
|
|
|
if pp >= 0:
|
|
|
|
macnum = resline[pp+6:-1]
|
|
|
|
macnum = macnum.strip()
|
2012-12-19 13:48:11 +00:00
|
|
|
# print 'original mac', macnum
|
2011-07-14 06:08:06 +00:00
|
|
|
# now munge it up the way Kindle app does
|
|
|
|
# by xoring it with 0xa5 and swapping elements 3 and 4
|
|
|
|
maclst = macnum.split(':')
|
|
|
|
n = len(maclst)
|
|
|
|
if n != 6:
|
|
|
|
fountIt = False
|
|
|
|
break
|
|
|
|
for i in range(6):
|
|
|
|
maclst[i] = int('0x' + maclst[i], 0)
|
|
|
|
mlst = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
|
|
|
|
mlst[5] = maclst[5] ^ 0xa5
|
|
|
|
mlst[4] = maclst[3] ^ 0xa5
|
|
|
|
mlst[3] = maclst[4] ^ 0xa5
|
|
|
|
mlst[2] = maclst[2] ^ 0xa5
|
|
|
|
mlst[1] = maclst[1] ^ 0xa5
|
|
|
|
mlst[0] = maclst[0] ^ 0xa5
|
2012-12-19 13:48:11 +00:00
|
|
|
macnum = '%0.2x%0.2x%0.2x%0.2x%0.2x%0.2x' % (mlst[0], mlst[1], mlst[2], mlst[3], mlst[4], mlst[5])
|
2011-07-14 06:08:06 +00:00
|
|
|
foundIt = True
|
|
|
|
break
|
|
|
|
if not foundIt:
|
|
|
|
macnum = ''
|
|
|
|
return macnum
|
|
|
|
|
|
|
|
|
2012-03-06 18:24:28 +00:00
|
|
|
# uses unix env to get username instead of using sysctlbyname
|
2010-12-02 18:10:46 +00:00
|
|
|
def GetUserName():
|
|
|
|
username = os.getenv('USER')
|
|
|
|
return username
|
|
|
|
|
2011-07-14 06:08:06 +00:00
|
|
|
def isNewInstall():
|
|
|
|
home = os.getenv('HOME')
|
|
|
|
# soccer game fan anyone
|
|
|
|
dpath = home + '/Library/Application Support/Kindle/storage/.pes2011'
|
|
|
|
# print dpath, os.path.exists(dpath)
|
2012-09-11 16:57:03 +00:00
|
|
|
if os.path.exists(dpath):
|
|
|
|
return True
|
|
|
|
dpath = home + '/Library/Containers/com.amazon.Kindle/Data/Library/Application Support/Kindle/storage/.pes2011'
|
|
|
|
# print dpath, os.path.exists(dpath)
|
2011-07-14 06:08:06 +00:00
|
|
|
if os.path.exists(dpath):
|
|
|
|
return True
|
|
|
|
return False
|
2012-03-06 18:24:28 +00:00
|
|
|
|
2011-07-14 06:08:06 +00:00
|
|
|
|
2012-12-19 13:48:11 +00:00
|
|
|
class Memoize:
|
|
|
|
"""Memoize(fn) - an instance which acts like fn but memoizes its arguments
|
|
|
|
Will only work on functions with non-mutable arguments
|
|
|
|
"""
|
|
|
|
def __init__(self, fn):
|
|
|
|
self.fn = fn
|
|
|
|
self.memo = {}
|
|
|
|
def __call__(self, *args):
|
|
|
|
if not self.memo.has_key(args):
|
|
|
|
self.memo[args] = self.fn(*args)
|
|
|
|
return self.memo[args]
|
|
|
|
|
|
|
|
@Memoize
|
2011-07-14 06:08:06 +00:00
|
|
|
def GetIDString():
|
|
|
|
# K4Mac now has an extensive set of ids strings it uses
|
|
|
|
# in encoding pids and in creating unique passwords
|
|
|
|
# for use in its own version of CryptUnprotectDataV2
|
|
|
|
|
|
|
|
# BUT Amazon has now become nasty enough to detect when its app
|
|
|
|
# is being run under a debugger and actually changes code paths
|
2012-03-06 18:24:28 +00:00
|
|
|
# including which one of these strings is chosen, all to try
|
2011-07-14 06:08:06 +00:00
|
|
|
# to prevent reverse engineering
|
|
|
|
|
|
|
|
# Sad really ... they will only hurt their own sales ...
|
|
|
|
# true book lovers really want to keep their books forever
|
2012-03-06 18:24:28 +00:00
|
|
|
# and move them to their devices and DRM prevents that so they
|
|
|
|
# will just buy from someplace else that they can remove
|
2011-07-14 06:08:06 +00:00
|
|
|
# the DRM from
|
|
|
|
|
|
|
|
# Amazon should know by now that true book lover's are not like
|
|
|
|
# penniless kids that pirate music, we do not pirate books
|
|
|
|
|
|
|
|
if isNewInstall():
|
|
|
|
mungedmac = GetMACAddressMunged()
|
|
|
|
if len(mungedmac) > 7:
|
2012-09-09 00:45:24 +00:00
|
|
|
print('Using Munged MAC Address for ID: '+mungedmac)
|
2011-07-14 06:08:06 +00:00
|
|
|
return mungedmac
|
|
|
|
sernum = GetVolumeSerialNumber()
|
|
|
|
if len(sernum) > 7:
|
2012-09-09 00:45:24 +00:00
|
|
|
print('Using Volume Serial Number for ID: '+sernum)
|
2011-07-14 06:08:06 +00:00
|
|
|
return sernum
|
|
|
|
diskpart = GetUserHomeAppSupKindleDirParitionName()
|
|
|
|
uuidnum = GetDiskPartitionUUID(diskpart)
|
|
|
|
if len(uuidnum) > 7:
|
2012-09-09 00:45:24 +00:00
|
|
|
print('Using Disk Partition UUID for ID: '+uuidnum)
|
2011-07-14 06:08:06 +00:00
|
|
|
return uuidnum
|
|
|
|
mungedmac = GetMACAddressMunged()
|
|
|
|
if len(mungedmac) > 7:
|
2012-09-09 00:45:24 +00:00
|
|
|
print('Using Munged MAC Address for ID: '+mungedmac)
|
2011-07-14 06:08:06 +00:00
|
|
|
return mungedmac
|
2012-09-09 00:45:24 +00:00
|
|
|
print('Using Fixed constant 9999999999 for ID.')
|
2011-07-14 06:08:06 +00:00
|
|
|
return '9999999999'
|
|
|
|
|
|
|
|
|
2012-03-06 18:24:28 +00:00
|
|
|
# implements an Pseudo Mac Version of Windows built-in Crypto routine
|
|
|
|
# used by Kindle for Mac versions < 1.6.0
|
|
|
|
class CryptUnprotectData(object):
|
|
|
|
def __init__(self):
|
|
|
|
sernum = GetVolumeSerialNumber()
|
|
|
|
if sernum == '':
|
|
|
|
sernum = '9999999999'
|
|
|
|
sp = sernum + '!@#' + GetUserName()
|
|
|
|
passwdData = encode(SHA256(sp),charMap1)
|
|
|
|
salt = '16743'
|
|
|
|
self.crp = LibCrypto()
|
|
|
|
iter = 0x3e8
|
|
|
|
keylen = 0x80
|
|
|
|
key_iv = self.crp.keyivgen(passwdData, salt, iter, keylen)
|
|
|
|
self.key = key_iv[0:32]
|
|
|
|
self.iv = key_iv[32:48]
|
|
|
|
self.crp.set_decrypt_key(self.key, self.iv)
|
|
|
|
|
|
|
|
def decrypt(self, encryptedData):
|
|
|
|
cleartext = self.crp.decrypt(encryptedData)
|
|
|
|
cleartext = decode(cleartext,charMap1)
|
|
|
|
return cleartext
|
|
|
|
|
|
|
|
|
2011-07-14 06:08:06 +00:00
|
|
|
# implements an Pseudo Mac Version of Windows built-in Crypto routine
|
|
|
|
# used for Kindle for Mac Versions >= 1.6.0
|
2012-03-06 18:24:28 +00:00
|
|
|
class CryptUnprotectDataV2(object):
|
|
|
|
def __init__(self):
|
|
|
|
sp = GetUserName() + ':&%:' + GetIDString()
|
|
|
|
passwdData = encode(SHA256(sp),charMap5)
|
|
|
|
# salt generation as per the code
|
|
|
|
salt = 0x0512981d * 2 * 1 * 1
|
|
|
|
salt = str(salt) + GetUserName()
|
|
|
|
salt = encode(salt,charMap5)
|
|
|
|
self.crp = LibCrypto()
|
|
|
|
iter = 0x800
|
|
|
|
keylen = 0x400
|
|
|
|
key_iv = self.crp.keyivgen(passwdData, salt, iter, keylen)
|
|
|
|
self.key = key_iv[0:32]
|
|
|
|
self.iv = key_iv[32:48]
|
|
|
|
self.crp.set_decrypt_key(self.key, self.iv)
|
|
|
|
|
|
|
|
def decrypt(self, encryptedData):
|
|
|
|
cleartext = self.crp.decrypt(encryptedData)
|
|
|
|
cleartext = decode(cleartext, charMap5)
|
|
|
|
return cleartext
|
|
|
|
|
|
|
|
|
|
|
|
# unprotect the new header blob in .kinf2011
|
|
|
|
# used in Kindle for Mac Version >= 1.9.0
|
|
|
|
def UnprotectHeaderData(encryptedData):
|
|
|
|
passwdData = 'header_key_data'
|
|
|
|
salt = 'HEADER.2011'
|
|
|
|
iter = 0x80
|
|
|
|
keylen = 0x100
|
2011-07-14 06:08:06 +00:00
|
|
|
crp = LibCrypto()
|
|
|
|
key_iv = crp.keyivgen(passwdData, salt, iter, keylen)
|
|
|
|
key = key_iv[0:32]
|
|
|
|
iv = key_iv[32:48]
|
|
|
|
crp.set_decrypt_key(key,iv)
|
|
|
|
cleartext = crp.decrypt(encryptedData)
|
2010-12-02 18:10:46 +00:00
|
|
|
return cleartext
|
|
|
|
|
2010-12-30 22:41:07 +00:00
|
|
|
|
2012-03-06 18:24:28 +00:00
|
|
|
# implements an Pseudo Mac Version of Windows built-in Crypto routine
|
|
|
|
# used for Kindle for Mac Versions >= 1.9.0
|
|
|
|
class CryptUnprotectDataV3(object):
|
|
|
|
def __init__(self, entropy):
|
|
|
|
sp = GetUserName() + '+@#$%+' + GetIDString()
|
|
|
|
passwdData = encode(SHA256(sp),charMap2)
|
|
|
|
salt = entropy
|
|
|
|
self.crp = LibCrypto()
|
|
|
|
iter = 0x800
|
|
|
|
keylen = 0x400
|
|
|
|
key_iv = self.crp.keyivgen(passwdData, salt, iter, keylen)
|
|
|
|
self.key = key_iv[0:32]
|
|
|
|
self.iv = key_iv[32:48]
|
|
|
|
self.crp.set_decrypt_key(self.key, self.iv)
|
|
|
|
|
|
|
|
def decrypt(self, encryptedData):
|
|
|
|
cleartext = self.crp.decrypt(encryptedData)
|
|
|
|
cleartext = decode(cleartext, charMap2)
|
|
|
|
return cleartext
|
|
|
|
|
|
|
|
|
2011-03-28 12:01:05 +00:00
|
|
|
# Locate the .kindle-info files
|
2012-11-20 13:28:12 +00:00
|
|
|
def getKindleInfoFiles():
|
|
|
|
# file searches can take a long time on some systems, so just look in known specific places.
|
|
|
|
kInfoFiles=[]
|
2012-11-07 13:14:25 +00:00
|
|
|
found = False
|
2011-03-28 12:01:05 +00:00
|
|
|
home = os.getenv('HOME')
|
2012-11-20 13:28:12 +00:00
|
|
|
# check for .kinf2011 file in new location (App Store Kindle for Mac)
|
|
|
|
testpath = home + '/Library/Containers/com.amazon.Kindle/Data/Library/Application Support/Kindle/storage/.kinf2011'
|
|
|
|
if os.path.isfile(testpath):
|
|
|
|
kInfoFiles.append(testpath)
|
|
|
|
print('Found k4Mac kinf2011 file: ' + testpath)
|
|
|
|
found = True
|
|
|
|
# check for .kinf2011 files
|
|
|
|
testpath = home + '/Library/Application Support/Kindle/storage/.kinf2011'
|
|
|
|
if os.path.isfile(testpath):
|
|
|
|
kInfoFiles.append(testpath)
|
|
|
|
print('Found k4Mac kinf2011 file: ' + testpath)
|
|
|
|
found = True
|
|
|
|
# check for .rainier-2.1.1-kinf files
|
|
|
|
testpath = home + '/Library/Application Support/Kindle/storage/.rainier-2.1.1-kinf'
|
|
|
|
if os.path.isfile(testpath):
|
|
|
|
kInfoFiles.append(testpath)
|
|
|
|
print('Found k4Mac rainier file: ' + testpath)
|
|
|
|
found = True
|
|
|
|
# check for .rainier-2.1.1-kinf files
|
|
|
|
testpath = home + '/Library/Application Support/Kindle/storage/.kindle-info'
|
|
|
|
if os.path.isfile(testpath):
|
|
|
|
kInfoFiles.append(testpath)
|
|
|
|
print('Found k4Mac kindle-info file: ' + testpath)
|
|
|
|
found = True
|
2011-03-28 12:01:05 +00:00
|
|
|
if not found:
|
2012-11-20 13:28:12 +00:00
|
|
|
print('No k4Mac kindle-info/rainier/kinf2011 files have been found.')
|
2011-03-28 12:01:05 +00:00
|
|
|
return kInfoFiles
|
|
|
|
|
2012-03-06 18:24:28 +00:00
|
|
|
# determine type of kindle info provided and return a
|
2011-06-16 05:59:20 +00:00
|
|
|
# database of keynames and values
|
|
|
|
def getDBfromFile(kInfoFile):
|
2012-12-19 13:48:11 +00:00
|
|
|
|
|
|
|
names = ['kindle.account.tokens','kindle.cookie.item','eulaVersionAccepted','login_date','kindle.token.item','login','kindle.key.item','kindle.name.info','kindle.device.info', 'MazamaRandomNumber', 'max_date', 'SIGVERIF']
|
2011-03-28 12:01:05 +00:00
|
|
|
DB = {}
|
2011-06-16 05:59:20 +00:00
|
|
|
cnt = 0
|
2011-03-28 12:01:05 +00:00
|
|
|
infoReader = open(kInfoFile, 'r')
|
2011-06-16 05:59:20 +00:00
|
|
|
hdr = infoReader.read(1)
|
2011-03-28 12:01:05 +00:00
|
|
|
data = infoReader.read()
|
2011-06-16 05:59:20 +00:00
|
|
|
|
|
|
|
if data.find('[') != -1 :
|
2012-03-06 18:24:28 +00:00
|
|
|
|
2011-06-16 05:59:20 +00:00
|
|
|
# older style kindle-info file
|
2012-03-06 18:24:28 +00:00
|
|
|
cud = CryptUnprotectData()
|
2011-06-16 05:59:20 +00:00
|
|
|
items = data.split('[')
|
|
|
|
for item in items:
|
|
|
|
if item != '':
|
|
|
|
keyhash, rawdata = item.split(':')
|
2012-12-19 13:48:11 +00:00
|
|
|
keyname = 'unknown'
|
2011-06-16 05:59:20 +00:00
|
|
|
for name in names:
|
|
|
|
if encodeHash(name,charMap2) == keyhash:
|
|
|
|
keyname = name
|
|
|
|
break
|
2012-12-19 13:48:11 +00:00
|
|
|
if keyname == 'unknown':
|
2011-06-16 05:59:20 +00:00
|
|
|
keyname = keyhash
|
|
|
|
encryptedValue = decode(rawdata,charMap2)
|
2012-03-06 18:24:28 +00:00
|
|
|
cleartext = cud.decrypt(encryptedValue)
|
2011-07-14 06:08:06 +00:00
|
|
|
DB[keyname] = cleartext
|
2011-06-16 05:59:20 +00:00
|
|
|
cnt = cnt + 1
|
|
|
|
if cnt == 0:
|
|
|
|
DB = None
|
|
|
|
return DB
|
|
|
|
|
2012-03-06 18:24:28 +00:00
|
|
|
if hdr == '/':
|
|
|
|
|
|
|
|
# else newer style .kinf file used by K4Mac >= 1.6.0
|
2012-12-19 13:48:11 +00:00
|
|
|
# the .kinf file uses '/' to separate it into records
|
|
|
|
# so remove the trailing '/' to make it easy to use split
|
2012-03-06 18:24:28 +00:00
|
|
|
data = data[:-1]
|
|
|
|
items = data.split('/')
|
|
|
|
cud = CryptUnprotectDataV2()
|
|
|
|
|
|
|
|
# loop through the item records until all are processed
|
|
|
|
while len(items) > 0:
|
|
|
|
|
|
|
|
# get the first item record
|
|
|
|
item = items.pop(0)
|
|
|
|
|
|
|
|
# the first 32 chars of the first record of a group
|
|
|
|
# is the MD5 hash of the key name encoded by charMap5
|
|
|
|
keyhash = item[0:32]
|
2012-12-19 13:48:11 +00:00
|
|
|
keyname = 'unknown'
|
2012-03-06 18:24:28 +00:00
|
|
|
|
|
|
|
# the raw keyhash string is also used to create entropy for the actual
|
|
|
|
# CryptProtectData Blob that represents that keys contents
|
2012-12-19 13:48:11 +00:00
|
|
|
# 'entropy' not used for K4Mac only K4PC
|
2012-03-06 18:24:28 +00:00
|
|
|
# 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)
|
|
|
|
|
2012-12-19 13:48:11 +00:00
|
|
|
keyname = 'unknown'
|
2012-03-06 18:24:28 +00:00
|
|
|
for name in names:
|
|
|
|
if encodeHash(name,charMap5) == keyhash:
|
|
|
|
keyname = name
|
|
|
|
break
|
2012-12-19 13:48:11 +00:00
|
|
|
if keyname == 'unknown':
|
2012-03-06 18:24:28 +00:00
|
|
|
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)
|
2012-12-19 13:48:11 +00:00
|
|
|
# (in other words split 'about' 2/3rds of the way through)
|
2012-03-06 18:24:28 +00:00
|
|
|
|
|
|
|
# move first offsets chars to end to align for decode by charMap5
|
2012-12-19 13:48:11 +00:00
|
|
|
encdata = ''.join(edlst)
|
2012-03-06 18:24:28 +00:00
|
|
|
contlen = len(encdata)
|
|
|
|
|
|
|
|
# now properly split and recombine
|
|
|
|
# by moving noffset chars from the start of the
|
|
|
|
# string to the end of the string
|
|
|
|
noffset = contlen - primes(int(contlen/3))[-1]
|
|
|
|
pfx = encdata[0:noffset]
|
|
|
|
encdata = encdata[noffset:]
|
|
|
|
encdata = encdata + pfx
|
|
|
|
|
|
|
|
# decode using charMap5 to get the CryptProtect Data
|
|
|
|
encryptedValue = decode(encdata,charMap5)
|
|
|
|
cleartext = cud.decrypt(encryptedValue)
|
|
|
|
DB[keyname] = cleartext
|
|
|
|
cnt = cnt + 1
|
|
|
|
|
|
|
|
if cnt == 0:
|
|
|
|
DB = None
|
|
|
|
return DB
|
|
|
|
|
|
|
|
# the latest .kinf2011 version for K4M 1.9.1
|
|
|
|
# put back the hdr char, it is needed
|
|
|
|
data = hdr + data
|
2011-07-14 06:08:06 +00:00
|
|
|
data = data[:-1]
|
|
|
|
items = data.split('/')
|
|
|
|
|
2012-03-06 18:24:28 +00:00
|
|
|
# the headerblob is the encrypted information needed to build the entropy string
|
|
|
|
headerblob = items.pop(0)
|
|
|
|
encryptedValue = decode(headerblob, charMap1)
|
|
|
|
cleartext = UnprotectHeaderData(encryptedValue)
|
|
|
|
|
|
|
|
# now extract the pieces in the same way
|
|
|
|
# this version is different from K4PC it scales the build number by multipying by 735
|
|
|
|
pattern = re.compile(r'''\[Version:(\d+)\]\[Build:(\d+)\]\[Cksum:([^\]]+)\]\[Guid:([\{\}a-z0-9\-]+)\]''', re.IGNORECASE)
|
|
|
|
for m in re.finditer(pattern, cleartext):
|
|
|
|
entropy = str(int(m.group(2)) * 0x2df) + m.group(4)
|
|
|
|
|
|
|
|
cud = CryptUnprotectDataV3(entropy)
|
|
|
|
|
2011-07-14 06:08:06 +00:00
|
|
|
# loop through the item records until all are processed
|
|
|
|
while len(items) > 0:
|
2012-03-06 18:24:28 +00:00
|
|
|
|
2011-07-14 06:08:06 +00:00
|
|
|
# get the first item record
|
|
|
|
item = items.pop(0)
|
2012-03-06 18:24:28 +00:00
|
|
|
|
2011-07-14 06:08:06 +00:00
|
|
|
# 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]
|
2012-12-19 13:48:11 +00:00
|
|
|
keyname = 'unknown'
|
2011-07-14 06:08:06 +00:00
|
|
|
|
2012-03-06 18:24:28 +00:00
|
|
|
# unlike K4PC the keyhash is not used in generating entropy
|
|
|
|
# entropy = SHA1(keyhash) + added_entropy
|
|
|
|
# entropy = added_entropy
|
|
|
|
|
|
|
|
# the remainder of the first record when decoded with charMap5
|
2011-07-14 06:08:06 +00:00
|
|
|
# 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)
|
2012-03-06 18:24:28 +00:00
|
|
|
|
2011-07-14 06:08:06 +00:00
|
|
|
# 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)
|
2012-03-06 18:24:28 +00:00
|
|
|
|
2012-12-19 13:48:11 +00:00
|
|
|
keyname = 'unknown'
|
2011-07-14 06:08:06 +00:00
|
|
|
for name in names:
|
2012-03-06 18:24:28 +00:00
|
|
|
if encodeHash(name,testMap8) == keyhash:
|
2011-07-14 06:08:06 +00:00
|
|
|
keyname = name
|
|
|
|
break
|
2012-12-19 13:48:11 +00:00
|
|
|
if keyname == 'unknown':
|
2011-07-14 06:08:06 +00:00
|
|
|
keyname = keyhash
|
2012-03-06 18:24:28 +00:00
|
|
|
|
|
|
|
# the testMap8 encoded contents data has had a length
|
2011-07-14 06:08:06 +00:00
|
|
|
# of chars (always odd) cut off of the front and moved
|
2012-03-06 18:24:28 +00:00
|
|
|
# to the end to prevent decoding using testMap8 from
|
|
|
|
# working properly, and thereby preventing the ensuing
|
2011-07-14 06:08:06 +00:00
|
|
|
# CryptUnprotectData call from succeeding.
|
2012-03-06 18:24:28 +00:00
|
|
|
|
|
|
|
# The offset into the testMap8 encoded contents seems to be:
|
2011-07-14 06:08:06 +00:00
|
|
|
# len(contents) - largest prime number less than or equal to int(len(content)/3)
|
2012-12-19 13:48:11 +00:00
|
|
|
# (in other words split 'about' 2/3rds of the way through)
|
2012-03-06 18:24:28 +00:00
|
|
|
|
|
|
|
# move first offsets chars to end to align for decode by testMap8
|
2012-12-19 13:48:11 +00:00
|
|
|
encdata = ''.join(edlst)
|
2011-07-14 06:08:06 +00:00
|
|
|
contlen = len(encdata)
|
|
|
|
|
2012-03-06 18:24:28 +00:00
|
|
|
# now properly split and recombine
|
|
|
|
# by moving noffset chars from the start of the
|
|
|
|
# string to the end of the string
|
2011-07-14 06:08:06 +00:00
|
|
|
noffset = contlen - primes(int(contlen/3))[-1]
|
|
|
|
pfx = encdata[0:noffset]
|
|
|
|
encdata = encdata[noffset:]
|
|
|
|
encdata = encdata + pfx
|
2012-03-06 18:24:28 +00:00
|
|
|
|
|
|
|
# decode using testMap8 to get the CryptProtect Data
|
|
|
|
encryptedValue = decode(encdata,testMap8)
|
|
|
|
cleartext = cud.decrypt(encryptedValue)
|
2011-07-14 06:08:06 +00:00
|
|
|
# print keyname
|
|
|
|
# print cleartext
|
|
|
|
DB[keyname] = cleartext
|
|
|
|
cnt = cnt + 1
|
2011-06-16 05:59:20 +00:00
|
|
|
|
|
|
|
if cnt == 0:
|
|
|
|
DB = None
|
2011-03-28 12:01:05 +00:00
|
|
|
return DB
|