cryptosploit_modules.symmetric.aes
View Source
0from base64 import b64encode, b64decode 1from binascii import Error 2from Crypto.Cipher import AES 3from Crypto.Util.Padding import pad, unpad 4from Crypto.Random import get_random_bytes 5 6from cryptosploit.cprint import Printer 7from cryptosploit_modules import BaseModule 8 9 10class AESModule(BaseModule): 11 def __init__(self): 12 super().__init__() 13 self.env.check_var = self.check_var 14 15 def check_var(self, name, value): 16 def is_valid_base64(value): 17 try: 18 return b64decode(value) 19 except Error: 20 return False 21 match name: 22 case "aes_mode": 23 if value not in ["ECB", "CBC", "OFB"]: 24 return False, "Mode must be ECB/CBC/OFB" 25 case "iv": 26 if not (value := is_valid_base64(value)): 27 return False, "Value of initialization vector is not a valid base64" 28 if len(value) != 16: 29 return False, "Initialization vector must be 16 bytes" 30 case "key": 31 if not (value := is_valid_base64(value)): 32 return False, "Value of key is not a valid base64" 33 if len(value) * 8 not in [128, 192, 256]: 34 return False, "Key length must be 128/192/256 bits" 35 case "mode": 36 if value not in ["decrypt", "encrypt"]: 37 return False, "Mode can be only decrypt/encrypt" 38 return True, "" 39 40 def run(self): 41 key = self.env.get_var("key").value 42 if not key: 43 key = get_random_bytes(32) 44 else: 45 key = b64decode(key) 46 aes_mode = self.env.get_var("aes_mode").value 47 res = {"key": b64encode(key).decode()} 48 if aes_mode == "ECB": 49 cipher = AES.new(key, getattr(AES, f"MODE_{aes_mode}")) 50 else: 51 iv = self.env.get_var("iv").value 52 if iv: 53 iv = b64decode(iv) 54 cipher = AES.new(key, getattr(AES, f"MODE_{aes_mode}"), iv=iv) 55 else: 56 cipher = AES.new(key, getattr(AES, f"MODE_{aes_mode}")) 57 iv = cipher.iv 58 res["iv"] = b64encode(iv).decode() 59 if self.env.get_var("mode").value == "encrypt": 60 res["res"] = b64encode( 61 cipher.encrypt(pad(b64decode(self.env.get_var("plaintext").value), AES.block_size)) 62 ).decode() 63 else: 64 res["res"] = b64encode( 65 unpad(cipher.decrypt(b64decode(self.env.get_var("plaintext").value)), AES.block_size) 66 ).decode() 67 Printer.positive(str(res)) 68 69 70module = AESModule
View Source
11class AESModule(BaseModule): 12 def __init__(self): 13 super().__init__() 14 self.env.check_var = self.check_var 15 16 def check_var(self, name, value): 17 def is_valid_base64(value): 18 try: 19 return b64decode(value) 20 except Error: 21 return False 22 match name: 23 case "aes_mode": 24 if value not in ["ECB", "CBC", "OFB"]: 25 return False, "Mode must be ECB/CBC/OFB" 26 case "iv": 27 if not (value := is_valid_base64(value)): 28 return False, "Value of initialization vector is not a valid base64" 29 if len(value) != 16: 30 return False, "Initialization vector must be 16 bytes" 31 case "key": 32 if not (value := is_valid_base64(value)): 33 return False, "Value of key is not a valid base64" 34 if len(value) * 8 not in [128, 192, 256]: 35 return False, "Key length must be 128/192/256 bits" 36 case "mode": 37 if value not in ["decrypt", "encrypt"]: 38 return False, "Mode can be only decrypt/encrypt" 39 return True, "" 40 41 def run(self): 42 key = self.env.get_var("key").value 43 if not key: 44 key = get_random_bytes(32) 45 else: 46 key = b64decode(key) 47 aes_mode = self.env.get_var("aes_mode").value 48 res = {"key": b64encode(key).decode()} 49 if aes_mode == "ECB": 50 cipher = AES.new(key, getattr(AES, f"MODE_{aes_mode}")) 51 else: 52 iv = self.env.get_var("iv").value 53 if iv: 54 iv = b64decode(iv) 55 cipher = AES.new(key, getattr(AES, f"MODE_{aes_mode}"), iv=iv) 56 else: 57 cipher = AES.new(key, getattr(AES, f"MODE_{aes_mode}")) 58 iv = cipher.iv 59 res["iv"] = b64encode(iv).decode() 60 if self.env.get_var("mode").value == "encrypt": 61 res["res"] = b64encode( 62 cipher.encrypt(pad(b64decode(self.env.get_var("plaintext").value), AES.block_size)) 63 ).decode() 64 else: 65 res["res"] = b64encode( 66 unpad(cipher.decrypt(b64decode(self.env.get_var("plaintext").value)), AES.block_size) 67 ).decode() 68 Printer.positive(str(res))
View Source
16 def check_var(self, name, value): 17 def is_valid_base64(value): 18 try: 19 return b64decode(value) 20 except Error: 21 return False 22 match name: 23 case "aes_mode": 24 if value not in ["ECB", "CBC", "OFB"]: 25 return False, "Mode must be ECB/CBC/OFB" 26 case "iv": 27 if not (value := is_valid_base64(value)): 28 return False, "Value of initialization vector is not a valid base64" 29 if len(value) != 16: 30 return False, "Initialization vector must be 16 bytes" 31 case "key": 32 if not (value := is_valid_base64(value)): 33 return False, "Value of key is not a valid base64" 34 if len(value) * 8 not in [128, 192, 256]: 35 return False, "Key length must be 128/192/256 bits" 36 case "mode": 37 if value not in ["decrypt", "encrypt"]: 38 return False, "Mode can be only decrypt/encrypt" 39 return True, ""
View Source
41 def run(self): 42 key = self.env.get_var("key").value 43 if not key: 44 key = get_random_bytes(32) 45 else: 46 key = b64decode(key) 47 aes_mode = self.env.get_var("aes_mode").value 48 res = {"key": b64encode(key).decode()} 49 if aes_mode == "ECB": 50 cipher = AES.new(key, getattr(AES, f"MODE_{aes_mode}")) 51 else: 52 iv = self.env.get_var("iv").value 53 if iv: 54 iv = b64decode(iv) 55 cipher = AES.new(key, getattr(AES, f"MODE_{aes_mode}"), iv=iv) 56 else: 57 cipher = AES.new(key, getattr(AES, f"MODE_{aes_mode}")) 58 iv = cipher.iv 59 res["iv"] = b64encode(iv).decode() 60 if self.env.get_var("mode").value == "encrypt": 61 res["res"] = b64encode( 62 cipher.encrypt(pad(b64decode(self.env.get_var("plaintext").value), AES.block_size)) 63 ).decode() 64 else: 65 res["res"] = b64encode( 66 unpad(cipher.decrypt(b64decode(self.env.get_var("plaintext").value)), AES.block_size) 67 ).decode() 68 Printer.positive(str(res))
Required to be overridden in the child class. Function called by the user
Inherited Members
View Source
11class AESModule(BaseModule): 12 def __init__(self): 13 super().__init__() 14 self.env.check_var = self.check_var 15 16 def check_var(self, name, value): 17 def is_valid_base64(value): 18 try: 19 return b64decode(value) 20 except Error: 21 return False 22 match name: 23 case "aes_mode": 24 if value not in ["ECB", "CBC", "OFB"]: 25 return False, "Mode must be ECB/CBC/OFB" 26 case "iv": 27 if not (value := is_valid_base64(value)): 28 return False, "Value of initialization vector is not a valid base64" 29 if len(value) != 16: 30 return False, "Initialization vector must be 16 bytes" 31 case "key": 32 if not (value := is_valid_base64(value)): 33 return False, "Value of key is not a valid base64" 34 if len(value) * 8 not in [128, 192, 256]: 35 return False, "Key length must be 128/192/256 bits" 36 case "mode": 37 if value not in ["decrypt", "encrypt"]: 38 return False, "Mode can be only decrypt/encrypt" 39 return True, "" 40 41 def run(self): 42 key = self.env.get_var("key").value 43 if not key: 44 key = get_random_bytes(32) 45 else: 46 key = b64decode(key) 47 aes_mode = self.env.get_var("aes_mode").value 48 res = {"key": b64encode(key).decode()} 49 if aes_mode == "ECB": 50 cipher = AES.new(key, getattr(AES, f"MODE_{aes_mode}")) 51 else: 52 iv = self.env.get_var("iv").value 53 if iv: 54 iv = b64decode(iv) 55 cipher = AES.new(key, getattr(AES, f"MODE_{aes_mode}"), iv=iv) 56 else: 57 cipher = AES.new(key, getattr(AES, f"MODE_{aes_mode}")) 58 iv = cipher.iv 59 res["iv"] = b64encode(iv).decode() 60 if self.env.get_var("mode").value == "encrypt": 61 res["res"] = b64encode( 62 cipher.encrypt(pad(b64decode(self.env.get_var("plaintext").value), AES.block_size)) 63 ).decode() 64 else: 65 res["res"] = b64encode( 66 unpad(cipher.decrypt(b64decode(self.env.get_var("plaintext").value)), AES.block_size) 67 ).decode() 68 Printer.positive(str(res))
View Source
16 def check_var(self, name, value): 17 def is_valid_base64(value): 18 try: 19 return b64decode(value) 20 except Error: 21 return False 22 match name: 23 case "aes_mode": 24 if value not in ["ECB", "CBC", "OFB"]: 25 return False, "Mode must be ECB/CBC/OFB" 26 case "iv": 27 if not (value := is_valid_base64(value)): 28 return False, "Value of initialization vector is not a valid base64" 29 if len(value) != 16: 30 return False, "Initialization vector must be 16 bytes" 31 case "key": 32 if not (value := is_valid_base64(value)): 33 return False, "Value of key is not a valid base64" 34 if len(value) * 8 not in [128, 192, 256]: 35 return False, "Key length must be 128/192/256 bits" 36 case "mode": 37 if value not in ["decrypt", "encrypt"]: 38 return False, "Mode can be only decrypt/encrypt" 39 return True, ""
View Source
41 def run(self): 42 key = self.env.get_var("key").value 43 if not key: 44 key = get_random_bytes(32) 45 else: 46 key = b64decode(key) 47 aes_mode = self.env.get_var("aes_mode").value 48 res = {"key": b64encode(key).decode()} 49 if aes_mode == "ECB": 50 cipher = AES.new(key, getattr(AES, f"MODE_{aes_mode}")) 51 else: 52 iv = self.env.get_var("iv").value 53 if iv: 54 iv = b64decode(iv) 55 cipher = AES.new(key, getattr(AES, f"MODE_{aes_mode}"), iv=iv) 56 else: 57 cipher = AES.new(key, getattr(AES, f"MODE_{aes_mode}")) 58 iv = cipher.iv 59 res["iv"] = b64encode(iv).decode() 60 if self.env.get_var("mode").value == "encrypt": 61 res["res"] = b64encode( 62 cipher.encrypt(pad(b64decode(self.env.get_var("plaintext").value), AES.block_size)) 63 ).decode() 64 else: 65 res["res"] = b64encode( 66 unpad(cipher.decrypt(b64decode(self.env.get_var("plaintext").value)), AES.block_size) 67 ).decode() 68 Printer.positive(str(res))
Required to be overridden in the child class. Function called by the user