Skip to content

Commit

Permalink
Merge pull request #16 from tendai-zw/master
Browse files Browse the repository at this point in the history
Replace MaltegoOAuth's pycrptodome dependency with cryptography lib
  • Loading branch information
Harsha Totakura authored Feb 4, 2021
2 parents 0d418f0 + 036359e commit fa9cc1e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
33 changes: 19 additions & 14 deletions maltego_trx/oauth.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""Maltego OAuth Crypto Helper"""
import base64
from Crypto import Random
from Crypto.Cipher import PKCS1_v1_5, AES
from Crypto.Hash import SHA
from Crypto.PublicKey import RSA

from cryptography.hazmat.backends import default_backend
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


Expand All @@ -16,33 +17,37 @@ class MaltegoOauth:
3. Refresh Token
4. Expires In
Contains 1 Methods:
Contains Methods:
1. decrypt_secrets(private_key_path="pem file", ciphertext="request.getTransformSetting('name from TDS')")
"""

@staticmethod
def _rsa_decrypt(private_key_path=None, ciphertext=None):
def _rsa_decrypt(private_key_path=None, ciphertext=None, password=None):
"""
RSA Decryption function, returns decrypted plaintext in b64 encoding
"""
dsize = SHA.digest_size
sentinel = Random.new().read(20 + dsize)
ciphertext = base64.b64decode(ciphertext)
private_key = RSA.import_key(open(private_key_path).read())
cipher = PKCS1_v1_5.new(private_key)
plaintext = cipher.decrypt(ciphertext, sentinel).decode('utf8')

with open(private_key_path, "rb") as key_file:
private_key = serialization.load_pem_private_key(key_file.read(),
password,
backend=None)
plaintext = private_key.decrypt(ciphertext, asymmetric_padding.PKCS1v15())

return plaintext

@staticmethod
def _aes_decrypt(key=None, ciphertext=None):
"""
AES Decryption function, returns decrypted plaintext value
"""
unpad = lambda s: s[:-ord(s[len(s) - 1:])]
key = base64.b64decode(key)
cipher = Cipher(algorithms.AES(key), modes.ECB(), backend=default_backend())
decryptor = cipher.decryptor();
ciphertext = base64.b64decode(ciphertext)
cipher = AES.new(key, AES.MODE_ECB)
plaintext = unpad(cipher.decrypt(ciphertext)).decode('utf8')
padded_b64_plaintext = decryptor.update(ciphertext) + decryptor.finalize()
unpadder = primitives_padding.PKCS7(128).unpadder()
plaintext = (unpadder.update(padded_b64_plaintext) + unpadder.finalize()).decode('utf8')
return plaintext

@classmethod
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
install_requires=[
'flask>=1',
'six>=1',
'pycryptodome>=3.9.7'
'cryptography>=3.3.1'
],
packages=[
'maltego_trx',
Expand Down
5 changes: 2 additions & 3 deletions tests/test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from maltego_trx.oauth import MaltegoOauth
import os

okta_ciphertoken = "pZ598ZEZ7EwpBQSOJSvCZJKkcWhtbX95K7Q0f0hwbk93O+xaUB4/NegK3r54PH1NReis/Jgt4UbGc5oCuU+R7UDM1icoDUmyCmV1U78iqjHElPdDBHlxPrl2zoXBwcU1iFbukDNy49Xghy3cwwqhmEXg/FKnYUlBQl6jdf06kE1pwfHiRgF5NrJISAD7eCmUybqiAVRDW4lbLeMGk/uSrrCpDQBxd2Em/sYKBzqO87pQRPLll23G9KNxyVinHQksGgc+dI0OPYv9EQcUs7g1JFraUKUeFjHAf/OyGGrp6WH84nGqG35afkH9xsDmySBb5DZWRjef8DzSd7oRagoGq3wKBHDh700mMKf/YkR8L9cK4l1w4yNh7EfIVlCWD5yWJUx4a1lN9CvJmFs7N9A903spVVGeP8avz50raWFb5g/4XhtIpei4ylVFi49dDeVjgb0BR0I4vuU7VDmcwFweAtSfclPb5Xfp98zI9yXnr8DB31gmzf3DIkVFIIFLxZe0gG+BSIEmC7/z0L7J+YvIqoGDq8jC0Ehe9IVXYfK1yluWAsbwj62rNAzkPVSe5l+FMQFrQRXpxZFBMXzphqRAhyYKfGNX/tocoMJoLFp8O3PBRC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OV8PsGy2uOnHRiUL218p1T6+Cm9B2kmL1C/2MM3080NjJVWIfqYsyTGPbR+hURK0RB/oteG5QWPs="
private_key_path = "test_private_key.pem"
Expand All @@ -11,9 +12,7 @@ def testOktaDecrypt():


def testReadKey():
private_key = open(private_key_path).read()
assert private_key.__sizeof__() == 3292

assert os.path.exists(private_key_path)

def testCipherSplit():
encrypted_fields = okta_ciphertoken.split("$")
Expand Down

0 comments on commit fa9cc1e

Please sign in to comment.