From e82d2b5c9cd799f0c4a1349dbcf0487a115fecaa Mon Sep 17 00:00:00 2001 From: NoDRM Date: Wed, 2 Aug 2023 18:13:42 +0200 Subject: [PATCH] Fix PDF decryption for 256-bit AES with V=5 --- CHANGELOG.md | 1 + DeDRM_plugin/ineptpdf.py | 15 +++++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b1e5816..3bb9848 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -100,3 +100,4 @@ This is v10.0.9, a release candidate for v10.1.0. I don't expect there to be maj ## Fixes on master (not yet released): - Fix a bug where decrypting a 40-bit RC4 pdf with R=2 didn't work. +- Fix a bug where decrypting a 256-bit AES pdf with V=5 didn't work. diff --git a/DeDRM_plugin/ineptpdf.py b/DeDRM_plugin/ineptpdf.py index e227ddc..7802d87 100755 --- a/DeDRM_plugin/ineptpdf.py +++ b/DeDRM_plugin/ineptpdf.py @@ -1366,8 +1366,7 @@ class PDFDocument(object): def process_with_aes(self, key, encrypt, data, repetitions = 1, iv = None): if iv is None: - keylen = len(key) - iv = bytes([0x00]*keylen) + iv = bytes(bytearray(16)) aes = AES.new(key, AES.MODE_CBC, iv) @@ -1395,10 +1394,18 @@ class PDFDocument(object): raise Exception("K1 < 32 ...") #def process_with_aes(self, key: bytes, encrypt: bool, data: bytes, repetitions: int = 1, iv: bytes = None): E = self.process_with_aes(K[:16], True, K1, 64, K[16:32]) - K = (hashlib.sha256, hashlib.sha384, hashlib.sha512)[sum(E) % 3](E).digest() + E = bytearray(E) + + E_mod_3 = 0 + for i in range(16): + E_mod_3 += E[i] + + E_mod_3 %= 3 + + K = (hashlib.sha256, hashlib.sha384, hashlib.sha512)[E_mod_3](E).digest() if round_number >= 64: - ch = int.from_bytes(E[-1:], "big", signed=False) + ch = E[-1:][0] # get last byte if ch <= round_number - 32: done = True