From 1885caa32fd9b50fce1fdce8ec95ddf7c71cabe3 Mon Sep 17 00:00:00 2001 From: Tendai Marengereke Date: Tue, 19 Jan 2021 16:09:02 +0200 Subject: [PATCH] refactor(MaltegoOAuth): use inbuilt primitives_padding.PKCS7() unpadding --- maltego_trx/oauth.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/maltego_trx/oauth.py b/maltego_trx/oauth.py index 73d77ce..9a9bd38 100644 --- a/maltego_trx/oauth.py +++ b/maltego_trx/oauth.py @@ -2,8 +2,8 @@ import base64 from cryptography.hazmat.backends import default_backend -from cryptography.hazmat.primitives import serialization -from cryptography.hazmat.primitives.asymmetric import padding +from cryptography.hazmat.primitives import serialization, padding as primitives_padding +from cryptography.hazmat.primitives.asymmetric import padding as asymmetric_padding from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes from requests.auth import AuthBase @@ -32,7 +32,7 @@ def _rsa_decrypt(private_key_path=None, ciphertext=None, password=None): private_key = serialization.load_pem_private_key(key_file.read(), password, backend=None) - plaintext = private_key.decrypt(ciphertext, padding.PKCS1v15()) + plaintext = private_key.decrypt(ciphertext, asymmetric_padding.PKCS1v15()) return plaintext @@ -46,9 +46,8 @@ def _aes_decrypt(key=None, ciphertext=None): decryptor = cipher.decryptor(); ciphertext = base64.b64decode(ciphertext) padded_b64_plaintext = decryptor.update(ciphertext) + decryptor.finalize() - - unpad = lambda data: data[:-ord(data[len(data) - 1:])] - plaintext = unpad(padded_b64_plaintext).decode('utf8') + unpadder = primitives_padding.PKCS7(128).unpadder() + plaintext = (unpadder.update(padded_b64_plaintext) + unpadder.finalize()).decode('utf8') return plaintext @classmethod