PKO{-kkmaltego_crypto_test.py"""Maltego OAuth Crypto Helper""" __version__ = '0.1.5' from Crypto.Cipher import PKCS1_v1_5, AES from Crypto.Util.Padding import pad, unpad from Crypto.PublicKey import RSA from Crypto.Hash import SHA from Crypto import Random import base64 from requests.auth import AuthBase import requests class MaltegoCrypto(object): """ A Crypto Helper Class for Maltego OAuth Secrets recieved from the Transform Distribution Server """ @staticmethod def rsa_decrypt(private_key_path=None, ciphertext=None): 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) return plaintext @staticmethod def aes_decrypt(key=None, ciphertext=None): BLOCK_SIZE = 16 # Bytes pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * \ chr(BLOCK_SIZE - len(s) % BLOCK_SIZE) unpad = lambda s: s[:-ord(s[len(s) - 1:])] key = base64.b64decode(key) ciphertext = base64.b64decode(ciphertext) cipher = AES.new(key, AES.MODE_ECB) plaintext = unpad(cipher.decrypt(ciphertext)).decode('utf8') return plaintext @classmethod def decrypt_secrets(cls,private_key_path = None, encoded_ciphertext=None): encrypted_fields = encoded_ciphertext.split("$") if len(encrypted_fields) == 1 : token = cls.rsa_decrypt(private_key_path,encrypted_fields[0]) token_fieds = { "token":token } elif len(encrypted_fields) == 2: # token token = cls.rsa_decrypt(private_key_path,encrypted_fields[0]) # token_secret token_secret = cls.rsa_decrypt(private_key_path, encrypted_fields[1]) token_fieds = { "token":token, "token_secret": token_secret } elif len(encrypted_fields) == 3: aes_key = cls.rsa_decrypt(private_key_path, encrypted_fields[2]) token = cls.aes_decrypt(aes_key, encrypted_fields[0]) token_secret = cls.aes_decrypt(aes_key,encrypted_fields[1]) token_fieds = { "token":token, "token_secret": token_secret } elif len(encrypted_fields) == 4: # token token = cls.rsa_decrypt(private_key_path,encrypted_fields[0]) # token_secret token_secret = cls.rsa_decrypt(private_key_path, encrypted_fields[1]) # refresh token refresh_token = cls.rsa_decrypt(private_key_path, encrypted_fields [2]) # expires in expires_in = cls.rsa_decrypt(private_key_path, encrypted_fields [3]) token_fieds = { "token":token, "token_secret": token_secret, "refresh_token": refresh_token, "expires_in": expires_in } elif len(encrypted_fields) == 5: aes_key = cls.rsa_decrypt(private_key_path, encrypted_fields[4]) # token token = cls.rsa_decrypt(private_key_path,encrypted_fields[0]) # token_secret token_secret = cls.rsa_decrypt(private_key_path, encrypted_fields[1]) # refresh token refresh_token = cls.rsa_decrypt(private_key_path, encrypted_fields [2]) # expires in expires_in = cls.rsa_decrypt(private_key_path, encrypted_fields [3]) token_fieds = { "token":token, "token_secret": token_secret, "refresh_token": refresh_token, "expires_in": expires_in } else: token_fieds = { "token":"", "token_secret": "", "refresh_token": "", "expires_in": "" } return token_fieds class OAuth2BearerToken(AuthBase): """Implements OAuth2 Bearer access token authentication. Pass this object via the `auth` parameter to a request or a session object in order to authenticate your requests. Example usage, once you have the `access_token`: >>> auth = OAuth2BearerToken(access_token) >>> requests.get("https://api.example.com/hello", auth=auth) With a session: >>> with requests.Session() as s: ... s.auth = auth ... print(s.get("https://api.example.com/hello")) ... """ def __init__(self, access_token): self.access_token = access_token def __call__(self, request): request.headers['Authorization'] = 'Bearer {}'.format( self.access_token ) return requestPK.TO.#*HH+maltego_crypto_test-0.1.5.dist-info/LICENSEThe MIT License (MIT) Copyright (c) 2019 Tendai M Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. PK!HMuSa)maltego_crypto_test-0.1.5.dist-info/WHEEL HM K-*ϳR03rOK-J,/RH,szd&Y)r$[)T&UD"PK!Hhef,maltego_crypto_test-0.1.5.dist-info/METADATAeN0DUqJ(UAjI-pdI,[hzpwތw'G$=uJB(aw #xl{iGrG3hJȖȈp_^U88/>wER푂<@G׭".sUw#A(7kXcᝯ?2{HO~,<8uҘ w Kwsx +!MaWPou~PK!Hc0*maltego_crypto_test-0.1.5.dist-info/RECORD=w0(D0t@h ĊIKũ۝ܡEg႕0+W49:~w`߸v9˻.LgJw'#{ ܭ9ESuov㉽?  0W̭LP}