From fd51422a3682ac943ddb352f56c6454149127b18 Mon Sep 17 00:00:00 2001 From: mkb79 Date: Mon, 22 Feb 2021 14:16:15 +0100 Subject: [PATCH] Enhance parsing DrmIon files Adding support for parsing plaintext in DrmIon files. This is needed by my kindle project. When downloading an ebook with my package it gives me a metadata file wich is DrmIon encoded. This file containes plaintext instead of encrypted pages. --- DeDRM_plugin/ion.py | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/DeDRM_plugin/ion.py b/DeDRM_plugin/ion.py index de21ca7..209219e 100644 --- a/DeDRM_plugin/ion.py +++ b/DeDRM_plugin/ion.py @@ -993,6 +993,7 @@ class DrmIon(object): elif self.ion.gettypename() in ["com.amazon.drm.EncryptedPage@1.0", "com.amazon.drm.EncryptedPage@2.0"]: decompress = False + decrypt = True ct = None civ = None self.ion.stepin() @@ -1006,7 +1007,23 @@ class DrmIon(object): civ = self.ion.lobvalue() if ct is not None and civ is not None: - self.processpage(ct, civ, outpages, decompress) + self.processpage(ct, civ, outpages, decompress, decrypt) + self.ion.stepout() + + elif self.ion.gettypename() in ["com.amazon.drm.PlainText@1.0", "com.amazon.drm.PlainText@2.0"]: + decompress = False + decrypt = False + plaintext = None + self.ion.stepin() + while self.ion.hasnext(): + self.ion.next() + if self.ion.gettypename() == "com.amazon.drm.Compressed@1.0": + decompress = True + if self.ion.getfieldname() == "data": + plaintext = self.ion.lobvalue() + + if plaintext is not None: + self.processpage(plaintext, None, outpages, decompress, decrypt) self.ion.stepout() self.ion.stepout() @@ -1017,9 +1034,12 @@ class DrmIon(object): def print_(self, lst): self.ion.print_(lst) - def processpage(self, ct, civ, outpages, decompress): - aes = AES.new(self.key[:16], AES.MODE_CBC, civ[:16]) - msg = pkcs7unpad(aes.decrypt(ct), 16) + def processpage(self, ct, civ, outpages, decompress, decrypt): + if decrypt: + aes = AES.new(self.key[:16], AES.MODE_CBC, civ[:16]) + msg = pkcs7unpad(aes.decrypt(ct), 16) + else: + msg = ct if not decompress: outpages.write(msg)