2020-09-27 04:54:49 -06:00
|
|
|
#!/usr/bin/env python3
|
2012-12-19 06:48:11 -07:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-11-20 06:28:12 -07:00
|
|
|
|
2015-03-25 01:26:33 -06:00
|
|
|
# kgenpids.py
|
2020-09-26 14:22:47 -06:00
|
|
|
# Copyright © 2008-2020 Apprentice Harper et al.
|
2017-07-04 00:05:51 -06:00
|
|
|
|
|
|
|
__license__ = 'GPL v3'
|
2020-09-26 14:22:47 -06:00
|
|
|
__version__ = '3.0'
|
2015-03-25 01:26:33 -06:00
|
|
|
|
|
|
|
# Revision history:
|
|
|
|
# 2.0 - Fix for non-ascii Windows user names
|
2017-07-04 00:05:51 -06:00
|
|
|
# 2.1 - Actual fix for non-ascii WIndows user names.
|
2020-09-26 14:22:47 -06:00
|
|
|
# 2.2 - Return information needed for KFX decryption
|
2020-09-27 04:54:49 -06:00
|
|
|
# 3.0 - Python 3 for calibre 5.0
|
2020-09-26 14:22:47 -06:00
|
|
|
|
2015-03-25 01:26:33 -06:00
|
|
|
|
2013-10-02 12:59:40 -06:00
|
|
|
import sys
|
|
|
|
import os, csv
|
|
|
|
import binascii
|
|
|
|
import zlib
|
|
|
|
import re
|
2012-11-20 06:28:12 -07:00
|
|
|
from struct import pack, unpack, unpack_from
|
2013-10-02 12:59:40 -06:00
|
|
|
import traceback
|
2013-04-05 10:44:48 -06:00
|
|
|
|
2013-10-02 12:59:40 -06:00
|
|
|
class DrmException(Exception):
|
|
|
|
pass
|
2012-11-20 06:28:12 -07:00
|
|
|
|
2013-10-02 12:59:40 -06:00
|
|
|
global charMap1
|
|
|
|
global charMap3
|
|
|
|
global charMap4
|
2013-04-05 10:44:48 -06:00
|
|
|
|
|
|
|
|
2020-09-26 14:22:47 -06:00
|
|
|
charMap1 = b'n5Pr6St7Uv8Wx9YzAb0Cd1Ef2Gh3Jk4M'
|
|
|
|
charMap3 = b'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
|
|
|
|
charMap4 = b'ABCDEFGHIJKLMNPQRSTUVWXYZ123456789'
|
2012-11-20 06:28:12 -07: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()
|
|
|
|
|
|
|
|
|
|
|
|
# Encode the bytes in data with the characters in map
|
2020-10-16 06:58:59 -06:00
|
|
|
# data and map should be byte arrays
|
2012-11-20 06:28:12 -07:00
|
|
|
def encode(data, map):
|
2020-10-16 06:58:59 -06:00
|
|
|
result = b''
|
2012-11-20 06:28:12 -07:00
|
|
|
for char in data:
|
2020-10-16 06:58:59 -06:00
|
|
|
value = char
|
2012-11-20 06:28:12 -07:00
|
|
|
Q = (value ^ 0x80) // len(map)
|
|
|
|
R = value % len(map)
|
2020-10-16 06:58:59 -06:00
|
|
|
result += bytes([map[Q]])
|
|
|
|
result += bytes([map[R]])
|
2012-11-20 06:28:12 -07:00
|
|
|
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 06:48:11 -07:00
|
|
|
result = ''
|
2012-11-20 06:28:12 -07: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 06:48:11 -07:00
|
|
|
result += pack('B',value)
|
2012-11-20 06:28:12 -07:00
|
|
|
return result
|
|
|
|
|
2013-10-02 12:59:40 -06:00
|
|
|
#
|
|
|
|
# PID generation routines
|
|
|
|
#
|
2012-11-20 06:28:12 -07:00
|
|
|
|
2013-10-02 12:59:40 -06:00
|
|
|
# Returns two bit at offset from a bit field
|
|
|
|
def getTwoBitsFromBitField(bitField,offset):
|
|
|
|
byteNumber = offset // 4
|
|
|
|
bitPosition = 6 - 2*(offset % 4)
|
2020-09-26 14:22:47 -06:00
|
|
|
return bitField[byteNumber] >> bitPosition & 3
|
2013-10-02 12:59:40 -06:00
|
|
|
|
|
|
|
# Returns the six bits at offset from a bit field
|
|
|
|
def getSixBitsFromBitField(bitField,offset):
|
|
|
|
offset *= 3
|
|
|
|
value = (getTwoBitsFromBitField(bitField,offset) <<4) + (getTwoBitsFromBitField(bitField,offset+1) << 2) +getTwoBitsFromBitField(bitField,offset+2)
|
|
|
|
return value
|
|
|
|
|
|
|
|
# 8 bits to six bits encoding from hash to generate PID string
|
|
|
|
def encodePID(hash):
|
|
|
|
global charMap3
|
2020-09-26 14:22:47 -06:00
|
|
|
PID = b''
|
2013-10-02 12:59:40 -06:00
|
|
|
for position in range (0,8):
|
2020-09-26 14:22:47 -06:00
|
|
|
PID += bytes([charMap3[getSixBitsFromBitField(hash,position)]])
|
2013-10-02 12:59:40 -06:00
|
|
|
return PID
|
|
|
|
|
|
|
|
# Encryption table used to generate the device PID
|
|
|
|
def generatePidEncryptionTable() :
|
|
|
|
table = []
|
|
|
|
for counter1 in range (0,0x100):
|
|
|
|
value = counter1
|
|
|
|
for counter2 in range (0,8):
|
|
|
|
if (value & 1 == 0) :
|
|
|
|
value = value >> 1
|
|
|
|
else :
|
|
|
|
value = value >> 1
|
|
|
|
value = value ^ 0xEDB88320
|
|
|
|
table.append(value)
|
|
|
|
return table
|
|
|
|
|
|
|
|
# Seed value used to generate the device PID
|
|
|
|
def generatePidSeed(table,dsn) :
|
|
|
|
value = 0
|
|
|
|
for counter in range (0,4) :
|
2020-10-16 06:58:59 -06:00
|
|
|
index = (dsn[counter] ^ value) & 0xFF
|
2013-10-02 12:59:40 -06:00
|
|
|
value = (value >> 8) ^ table[index]
|
|
|
|
return value
|
|
|
|
|
|
|
|
# Generate the device PID
|
|
|
|
def generateDevicePID(table,dsn,nbRoll):
|
|
|
|
global charMap4
|
|
|
|
seed = generatePidSeed(table,dsn)
|
2020-09-26 14:22:47 -06:00
|
|
|
pidAscii = b''
|
2013-10-02 12:59:40 -06:00
|
|
|
pid = [(seed >>24) &0xFF,(seed >> 16) &0xff,(seed >> 8) &0xFF,(seed) & 0xFF,(seed>>24) & 0xFF,(seed >> 16) &0xff,(seed >> 8) &0xFF,(seed) & 0xFF]
|
|
|
|
index = 0
|
|
|
|
for counter in range (0,nbRoll):
|
2020-10-16 06:58:59 -06:00
|
|
|
pid[index] = pid[index] ^ dsn[counter]
|
2013-10-02 12:59:40 -06:00
|
|
|
index = (index+1) %8
|
|
|
|
for counter in range (0,8):
|
|
|
|
index = ((((pid[counter] >>5) & 3) ^ pid[counter]) & 0x1f) + (pid[counter] >> 7)
|
2020-09-26 14:22:47 -06:00
|
|
|
pidAscii += bytes([charMap4[index]])
|
2013-10-02 12:59:40 -06:00
|
|
|
return pidAscii
|
|
|
|
|
|
|
|
def crc32(s):
|
|
|
|
return (~binascii.crc32(s,-1))&0xFFFFFFFF
|
|
|
|
|
|
|
|
# convert from 8 digit PID to 10 digit PID with checksum
|
|
|
|
def checksumPid(s):
|
|
|
|
global charMap4
|
|
|
|
crc = crc32(s)
|
|
|
|
crc = crc ^ (crc >> 16)
|
|
|
|
res = s
|
|
|
|
l = len(charMap4)
|
|
|
|
for i in (0,1):
|
|
|
|
b = crc & 0xff
|
|
|
|
pos = (b // l) ^ (b % l)
|
2020-09-26 14:22:47 -06:00
|
|
|
res += bytes([charMap4[pos%l]])
|
2013-10-02 12:59:40 -06:00
|
|
|
crc >>= 8
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
|
|
# old kindle serial number to fixed pid
|
|
|
|
def pidFromSerial(s, l):
|
|
|
|
global charMap4
|
|
|
|
crc = crc32(s)
|
|
|
|
arr1 = [0]*l
|
2020-09-26 14:22:47 -06:00
|
|
|
for i in range(len(s)):
|
|
|
|
arr1[i%l] ^= s[i]
|
2013-10-02 12:59:40 -06:00
|
|
|
crc_bytes = [crc >> 24 & 0xff, crc >> 16 & 0xff, crc >> 8 & 0xff, crc & 0xff]
|
2020-09-26 14:22:47 -06:00
|
|
|
for i in range(l):
|
2013-10-02 12:59:40 -06:00
|
|
|
arr1[i] ^= crc_bytes[i&3]
|
2020-09-26 14:22:47 -06:00
|
|
|
pid = b""
|
|
|
|
for i in range(l):
|
2013-10-02 12:59:40 -06:00
|
|
|
b = arr1[i] & 0xff
|
2020-09-26 14:22:47 -06:00
|
|
|
pid += bytes([charMap4[(b >> 7) + ((b >> 5 & 3) ^ (b & 0x1f))]])
|
2013-10-02 12:59:40 -06:00
|
|
|
return pid
|
|
|
|
|
|
|
|
|
|
|
|
# Parse the EXTH header records and use the Kindle serial number to calculate the book pid.
|
|
|
|
def getKindlePids(rec209, token, serialnum):
|
2018-03-12 18:34:58 -06:00
|
|
|
if rec209 is None:
|
|
|
|
return [serialnum]
|
|
|
|
|
2013-10-02 12:59:40 -06:00
|
|
|
pids=[]
|
|
|
|
|
2020-09-26 14:22:47 -06:00
|
|
|
if isinstance(serialnum,str):
|
2015-03-25 01:26:33 -06:00
|
|
|
serialnum = serialnum.encode('utf-8')
|
2013-10-02 12:59:40 -06:00
|
|
|
|
|
|
|
# Compute book PID
|
|
|
|
pidHash = SHA1(serialnum+rec209+token)
|
|
|
|
bookPID = encodePID(pidHash)
|
|
|
|
bookPID = checksumPid(bookPID)
|
|
|
|
pids.append(bookPID)
|
|
|
|
|
|
|
|
# compute fixed pid for old pre 2.5 firmware update pid as well
|
2020-09-26 14:22:47 -06:00
|
|
|
kindlePID = pidFromSerial(serialnum, 7) + b"*"
|
2013-10-02 12:59:40 -06:00
|
|
|
kindlePID = checksumPid(kindlePID)
|
|
|
|
pids.append(kindlePID)
|
|
|
|
|
|
|
|
return pids
|
|
|
|
|
|
|
|
|
|
|
|
# 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(rec209, token, kindleDatabase):
|
|
|
|
global charMap1
|
|
|
|
pids = []
|
2018-03-12 18:34:58 -06:00
|
|
|
|
2016-03-13 06:00:57 -06:00
|
|
|
try:
|
|
|
|
# Get the kindle account token, if present
|
2020-10-16 06:58:59 -06:00
|
|
|
kindleAccountToken = bytearray.fromhex((kindleDatabase[1])['kindle.account.tokens'])
|
2016-03-13 06:00:57 -06:00
|
|
|
|
|
|
|
except KeyError:
|
|
|
|
kindleAccountToken=""
|
|
|
|
pass
|
|
|
|
|
2017-07-04 00:05:51 -06:00
|
|
|
try:
|
|
|
|
# Get the DSN token, if present
|
2020-10-16 06:58:59 -06:00
|
|
|
DSN = bytearray.fromhex((kindleDatabase[1])['DSN'])
|
2020-09-27 04:54:49 -06:00
|
|
|
print("Got DSN key from database {0}".format(kindleDatabase[0]))
|
2017-07-04 00:05:51 -06:00
|
|
|
except KeyError:
|
|
|
|
# See if we have the info to generate the DSN
|
|
|
|
try:
|
|
|
|
# Get the Mazama Random number
|
2020-10-16 06:58:59 -06:00
|
|
|
MazamaRandomNumber = bytearray.fromhex((kindleDatabase[1])['MazamaRandomNumber'])
|
2020-09-27 04:54:49 -06:00
|
|
|
#print "Got MazamaRandomNumber from database {0}".format(kindleDatabase[0])
|
2018-03-12 18:34:58 -06:00
|
|
|
|
2017-07-04 00:05:51 -06:00
|
|
|
try:
|
|
|
|
# Get the SerialNumber token, if present
|
2020-10-16 06:58:59 -06:00
|
|
|
IDString = bytearray.fromhex((kindleDatabase[1])['SerialNumber'])
|
2020-09-27 04:54:49 -06:00
|
|
|
print("Got SerialNumber from database {0}".format(kindleDatabase[0]))
|
2017-07-04 00:05:51 -06:00
|
|
|
except KeyError:
|
|
|
|
# Get the IDString we added
|
2020-10-16 06:58:59 -06:00
|
|
|
IDString = bytearray.fromhex((kindleDatabase[1])['IDString'])
|
2017-07-04 00:05:51 -06:00
|
|
|
|
|
|
|
try:
|
|
|
|
# Get the UsernameHash token, if present
|
2020-10-16 06:58:59 -06:00
|
|
|
encodedUsername = bytearray.fromhex((kindleDatabase[1])['UsernameHash'])
|
2020-09-27 04:54:49 -06:00
|
|
|
print("Got UsernameHash from database {0}".format(kindleDatabase[0]))
|
2017-07-04 00:05:51 -06:00
|
|
|
except KeyError:
|
|
|
|
# Get the UserName we added
|
2020-10-16 06:58:59 -06:00
|
|
|
UserName = bytearray.fromhex((kindleDatabase[1])['UserName'])
|
2017-07-04 00:05:51 -06:00
|
|
|
# encode it
|
2020-10-04 13:36:12 -06:00
|
|
|
encodedUsername = encodeHash(UserName,charMap1)
|
2020-09-27 04:54:49 -06:00
|
|
|
#print "encodedUsername",encodedUsername.encode('hex')
|
2017-07-04 00:05:51 -06:00
|
|
|
except KeyError:
|
2020-09-27 04:54:49 -06:00
|
|
|
print("Keys not found in the database {0}.".format(kindleDatabase[0]))
|
2017-07-04 00:05:51 -06:00
|
|
|
return pids
|
|
|
|
|
|
|
|
# Get the ID string used
|
2020-10-04 13:36:12 -06:00
|
|
|
encodedIDString = encodeHash(IDString,charMap1)
|
2020-09-27 04:54:49 -06:00
|
|
|
#print "encodedIDString",encodedIDString.encode('hex')
|
2017-07-04 00:05:51 -06:00
|
|
|
|
|
|
|
# concat, hash and encode to calculate the DSN
|
2020-10-04 13:36:12 -06:00
|
|
|
DSN = encode(SHA1(MazamaRandomNumber+encodedIDString+encodedUsername),charMap1)
|
2020-09-27 04:54:49 -06:00
|
|
|
#print "DSN",DSN.encode('hex')
|
2017-07-04 00:05:51 -06:00
|
|
|
pass
|
2013-04-05 10:44:48 -06:00
|
|
|
|
2018-03-12 18:34:58 -06:00
|
|
|
if rec209 is None:
|
|
|
|
pids.append(DSN+kindleAccountToken)
|
|
|
|
return pids
|
|
|
|
|
2013-10-02 12:59:40 -06:00
|
|
|
# Compute the device PID (for which I can tell, is used for nothing).
|
|
|
|
table = generatePidEncryptionTable()
|
|
|
|
devicePID = generateDevicePID(table,DSN,4)
|
|
|
|
devicePID = checksumPid(devicePID)
|
|
|
|
pids.append(devicePID)
|
2013-04-05 10:44:48 -06:00
|
|
|
|
2013-10-02 12:59:40 -06:00
|
|
|
# Compute book PIDs
|
2013-04-05 10:44:48 -06:00
|
|
|
|
2013-10-02 12:59:40 -06:00
|
|
|
# book pid
|
2020-10-16 06:58:59 -06:00
|
|
|
pidHash = SHA1(DSN+kindleAccountToken+rec209+token)
|
2013-10-02 12:59:40 -06:00
|
|
|
bookPID = encodePID(pidHash)
|
|
|
|
bookPID = checksumPid(bookPID)
|
|
|
|
pids.append(bookPID)
|
2013-04-05 10:44:48 -06:00
|
|
|
|
2013-10-02 12:59:40 -06:00
|
|
|
# variant 1
|
2020-10-16 06:58:59 -06:00
|
|
|
pidHash = SHA1(kindleAccountToken+rec209+token)
|
2013-10-02 12:59:40 -06:00
|
|
|
bookPID = encodePID(pidHash)
|
|
|
|
bookPID = checksumPid(bookPID)
|
|
|
|
pids.append(bookPID)
|
2013-04-05 10:44:48 -06:00
|
|
|
|
2013-10-02 12:59:40 -06:00
|
|
|
# variant 2
|
2020-10-16 06:58:59 -06:00
|
|
|
pidHash = SHA1(DSN+rec209+token)
|
2013-10-02 12:59:40 -06:00
|
|
|
bookPID = encodePID(pidHash)
|
|
|
|
bookPID = checksumPid(bookPID)
|
|
|
|
pids.append(bookPID)
|
2013-04-05 10:44:48 -06:00
|
|
|
|
2013-10-02 12:59:40 -06:00
|
|
|
return pids
|
2013-04-05 10:44:48 -06:00
|
|
|
|
2013-10-02 12:59:40 -06:00
|
|
|
def getPidList(md1, md2, serials=[], kDatabases=[]):
|
|
|
|
pidlst = []
|
2013-04-05 10:44:48 -06:00
|
|
|
|
2013-10-02 12:59:40 -06:00
|
|
|
if kDatabases is None:
|
|
|
|
kDatabases = []
|
|
|
|
if serials is None:
|
|
|
|
serials = []
|
2013-03-20 04:23:54 -06:00
|
|
|
|
2013-10-02 12:59:40 -06:00
|
|
|
for kDatabase in kDatabases:
|
2012-11-20 06:28:12 -07:00
|
|
|
try:
|
2013-10-02 12:59:40 -06:00
|
|
|
pidlst.extend(getK4Pids(md1, md2, kDatabase))
|
2020-09-26 14:22:47 -06:00
|
|
|
except Exception as e:
|
2020-09-27 04:54:49 -06:00
|
|
|
print("Error getting PIDs from database {0}: {1}".format(kDatabase[0],e.args[0]))
|
2013-10-02 12:59:40 -06:00
|
|
|
traceback.print_exc()
|
2013-04-05 10:44:48 -06:00
|
|
|
|
2013-10-02 12:59:40 -06:00
|
|
|
for serialnum in serials:
|
|
|
|
try:
|
|
|
|
pidlst.extend(getKindlePids(md1, md2, serialnum))
|
2020-09-26 14:22:47 -06:00
|
|
|
except Exception as e:
|
2020-09-27 04:54:49 -06:00
|
|
|
print("Error getting PIDs from serial number {0}: {1}".format(serialnum ,e.args[0]))
|
2013-10-02 12:59:40 -06:00
|
|
|
traceback.print_exc()
|
2013-03-20 04:23:54 -06:00
|
|
|
|
2013-10-02 12:59:40 -06:00
|
|
|
return pidlst
|