Detect Kobo & Apple DRM in epubtest.py

This commit is contained in:
NoDRM 2021-12-20 21:11:09 +01:00
parent 3eb4eab18d
commit a16e66a023
1 changed files with 24 additions and 15 deletions

View File

@ -175,15 +175,13 @@ def getfiledata(file, zi):
return data return data
def encryption(infile): def encryption(infile):
# returns encryption: one of Unencrypted, Adobe, B&N and Unknown # Supports Adobe (old & new), B&N, Kobo, Apple, Readium LCP.
encryption = "Error When Checking." encryption = "Error"
try: try:
with open(infile,'rb') as infileobject: with open(infile,'rb') as infileobject:
bookdata = infileobject.read(58) bookdata = infileobject.read(58)
# Check for Zip # Check for Zip
if bookdata[0:0+2] == b"PK": if bookdata[0:0+2] == b"PK":
foundrights = False
foundencryption = False
inzip = zipfile.ZipFile(infile,'r') inzip = zipfile.ZipFile(infile,'r')
namelist = set(inzip.namelist()) namelist = set(inzip.namelist())
if ( if (
@ -192,9 +190,18 @@ def encryption(infile):
b"EncryptedContentKey" in inzip.read("META-INF/encryption.xml")): b"EncryptedContentKey" in inzip.read("META-INF/encryption.xml")):
encryption = "Readium LCP" encryption = "Readium LCP"
elif 'META-INF/sinf.xml' in namelist and b"fairplay" in inzip.read("META-INF/sinf.xml"):
# Untested, just found this info on Google
encryption = "Apple"
elif 'META-INF/rights.xml' in namelist and b"<kdrm>" in inzip.read("META-INF/rights.xml"):
# Untested, just found this info on Google
encryption = "Kobo"
elif 'META-INF/rights.xml' not in namelist or 'META-INF/encryption.xml' not in namelist: elif 'META-INF/rights.xml' not in namelist or 'META-INF/encryption.xml' not in namelist:
encryption = "Unencrypted" encryption = "Unencrypted"
else: else:
try:
rights = etree.fromstring(inzip.read('META-INF/rights.xml')) rights = etree.fromstring(inzip.read('META-INF/rights.xml'))
adept = lambda tag: '{%s}%s' % (NSMAP['adept'], tag) adept = lambda tag: '{%s}%s' % (NSMAP['adept'], tag)
expr = './/%s' % (adept('encryptedKey'),) expr = './/%s' % (adept('encryptedKey'),)
@ -206,6 +213,8 @@ def encryption(infile):
elif len(bookkey) == 64: elif len(bookkey) == 64:
encryption = "B&N" encryption = "B&N"
else: else:
encryption = "Unknown (key len " + str(len(bookkey)) + ")"
except:
encryption = "Unknown" encryption = "Unknown"
except: except:
traceback.print_exc() traceback.print_exc()