From a94047d778624aa970ea4837329f9b94da5f93e7 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Tue, 1 Jun 2021 14:15:58 +0300 Subject: [PATCH] Fixed configuration encryption/decryption to use b64 encoding --- .../monkey_island/cc/resources/temp_configuration.py | 9 ++++----- .../cc/services/utils/config_encryption.py | 12 ++++++++---- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/monkey/monkey_island/cc/resources/temp_configuration.py b/monkey/monkey_island/cc/resources/temp_configuration.py index 4ec651f63..d82c8c92d 100644 --- a/monkey/monkey_island/cc/resources/temp_configuration.py +++ b/monkey/monkey_island/cc/resources/temp_configuration.py @@ -4,11 +4,10 @@ from dataclasses import dataclass import flask_restful from flask import request -from common.utils.exceptions import ( - InvalidConfigurationError, - # InvalidCredentialsError, - NoCredentialsError, +from common.utils.exceptions import ( # InvalidCredentialsError, FailedDecryption, + InvalidConfigurationError, + NoCredentialsError, ) from monkey_island.cc.resources.auth.auth import jwt_required from monkey_island.cc.services.utils.config_encryption import decrypt_config @@ -32,7 +31,7 @@ class TempConfiguration(flask_restful.Resource): def post(self): request_contents = json.loads(request.data) try: - decrypt_config(request_contents["encrypted_config"], request_contents["password"]) + decrypt_config(request_contents["config"], request_contents["password"]) self.import_config() return ResponseContents().form_response() # except InvalidCredentialsError: diff --git a/monkey/monkey_island/cc/services/utils/config_encryption.py b/monkey/monkey_island/cc/services/utils/config_encryption.py index eb597be7c..dbe09db4c 100644 --- a/monkey/monkey_island/cc/services/utils/config_encryption.py +++ b/monkey/monkey_island/cc/services/utils/config_encryption.py @@ -1,3 +1,4 @@ +import base64 import io import json from typing import Dict @@ -17,15 +18,18 @@ def encrypt_config(config: Dict, password: str) -> str: plaintext_config_stream, ciphertext_config_stream, password, BUFFER_SIZE ) - ciphertext_config_bytes = str(ciphertext_config_stream.getvalue()) - return ciphertext_config_bytes + ciphertext_b64 = base64.b64encode(ciphertext_config_stream.getvalue()) + + return str(ciphertext_b64) -def decrypt_config(enc_config: bytes, password: str) -> Dict: +def decrypt_config(cyphertext: str, password: str) -> Dict: if not password: raise NoCredentialsError - ciphertext_config_stream = io.BytesIO(enc_config) + cyphertext = base64.b64decode(cyphertext) + + ciphertext_config_stream = io.BytesIO(cyphertext) dec_plaintext_config_stream = io.BytesIO() len_ciphertext_config_stream = len(ciphertext_config_stream.getvalue())